Fri, 05 Sep 2008 13:33:55 -0700
6676462: JVM sometimes would suddenly consume significant amount of memory
Summary: Add asserts with dead loop checks in AddNode::Ideal().
Reviewed-by: never
1 /*
2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 // Portions of code courtesy of Clifford Click
27 #include "incls/_precompiled.incl"
28 #include "incls/_addnode.cpp.incl"
30 #define MAXFLOAT ((float)3.40282346638528860e+38)
32 // Classic Add functionality. This covers all the usual 'add' behaviors for
33 // an algebraic ring. Add-integer, add-float, add-double, and binary-or are
34 // all inherited from this class. The various identity values are supplied
35 // by virtual functions.
38 //=============================================================================
39 //------------------------------hash-------------------------------------------
40 // Hash function over AddNodes. Needs to be commutative; i.e., I swap
41 // (commute) inputs to AddNodes willy-nilly so the hash function must return
42 // the same value in the presence of edge swapping.
43 uint AddNode::hash() const {
44 return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
45 }
47 //------------------------------Identity---------------------------------------
48 // If either input is a constant 0, return the other input.
49 Node *AddNode::Identity( PhaseTransform *phase ) {
50 const Type *zero = add_id(); // The additive identity
51 if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
52 if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
53 return this;
54 }
56 //------------------------------commute----------------------------------------
57 // Commute operands to move loads and constants to the right.
58 static bool commute( Node *add, int con_left, int con_right ) {
59 Node *in1 = add->in(1);
60 Node *in2 = add->in(2);
62 // Convert "1+x" into "x+1".
63 // Right is a constant; leave it
64 if( con_right ) return false;
65 // Left is a constant; move it right.
66 if( con_left ) {
67 add->swap_edges(1, 2);
68 return true;
69 }
71 // Convert "Load+x" into "x+Load".
72 // Now check for loads
73 if (in2->is_Load()) {
74 if (!in1->is_Load()) {
75 // already x+Load to return
76 return false;
77 }
78 // both are loads, so fall through to sort inputs by idx
79 } else if( in1->is_Load() ) {
80 // Left is a Load and Right is not; move it right.
81 add->swap_edges(1, 2);
82 return true;
83 }
85 PhiNode *phi;
86 // Check for tight loop increments: Loop-phi of Add of loop-phi
87 if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add)
88 return false;
89 if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){
90 add->swap_edges(1, 2);
91 return true;
92 }
94 // Otherwise, sort inputs (commutativity) to help value numbering.
95 if( in1->_idx > in2->_idx ) {
96 add->swap_edges(1, 2);
97 return true;
98 }
99 return false;
100 }
102 //------------------------------Idealize---------------------------------------
103 // If we get here, we assume we are associative!
104 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
105 const Type *t1 = phase->type( in(1) );
106 const Type *t2 = phase->type( in(2) );
107 int con_left = t1->singleton();
108 int con_right = t2->singleton();
110 // Check for commutative operation desired
111 if( commute(this,con_left,con_right) ) return this;
113 AddNode *progress = NULL; // Progress flag
115 // Convert "(x+1)+2" into "x+(1+2)". If the right input is a
116 // constant, and the left input is an add of a constant, flatten the
117 // expression tree.
118 Node *add1 = in(1);
119 Node *add2 = in(2);
120 int add1_op = add1->Opcode();
121 int this_op = Opcode();
122 if( con_right && t2 != Type::TOP && // Right input is a constant?
123 add1_op == this_op ) { // Left input is an Add?
125 // Type of left _in right input
126 const Type *t12 = phase->type( add1->in(2) );
127 if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
128 // Check for rare case of closed data cycle which can happen inside
129 // unreachable loops. In these cases the computation is undefined.
130 #ifdef ASSERT
131 Node *add11 = add1->in(1);
132 int add11_op = add11->Opcode();
133 if( (add1 == add1->in(1))
134 || (add11_op == this_op && add11->in(1) == add1) ) {
135 assert(false, "dead loop in AddNode::Ideal");
136 }
137 #endif
138 // The Add of the flattened expression
139 Node *x1 = add1->in(1);
140 Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 ));
141 PhaseIterGVN *igvn = phase->is_IterGVN();
142 if( igvn ) {
143 set_req_X(2,x2,igvn);
144 set_req_X(1,x1,igvn);
145 } else {
146 set_req(2,x2);
147 set_req(1,x1);
148 }
149 progress = this; // Made progress
150 add1 = in(1);
151 add1_op = add1->Opcode();
152 }
153 }
155 // Convert "(x+1)+y" into "(x+y)+1". Push constants down the expression tree.
156 if( add1_op == this_op && !con_right ) {
157 Node *a12 = add1->in(2);
158 const Type *t12 = phase->type( a12 );
159 if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) ) {
160 assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
161 add2 = add1->clone();
162 add2->set_req(2, in(2));
163 add2 = phase->transform(add2);
164 set_req(1, add2);
165 set_req(2, a12);
166 progress = this;
167 add2 = a12;
168 }
169 }
171 // Convert "x+(y+1)" into "(x+y)+1". Push constants down the expression tree.
172 int add2_op = add2->Opcode();
173 if( add2_op == this_op && !con_left ) {
174 Node *a22 = add2->in(2);
175 const Type *t22 = phase->type( a22 );
176 if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) ) {
177 assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
178 Node *addx = add2->clone();
179 addx->set_req(1, in(1));
180 addx->set_req(2, add2->in(1));
181 addx = phase->transform(addx);
182 set_req(1, addx);
183 set_req(2, a22);
184 progress = this;
185 }
186 }
188 return progress;
189 }
191 //------------------------------Value-----------------------------------------
192 // An add node sums it's two _in. If one input is an RSD, we must mixin
193 // the other input's symbols.
194 const Type *AddNode::Value( PhaseTransform *phase ) const {
195 // Either input is TOP ==> the result is TOP
196 const Type *t1 = phase->type( in(1) );
197 const Type *t2 = phase->type( in(2) );
198 if( t1 == Type::TOP ) return Type::TOP;
199 if( t2 == Type::TOP ) return Type::TOP;
201 // Either input is BOTTOM ==> the result is the local BOTTOM
202 const Type *bot = bottom_type();
203 if( (t1 == bot) || (t2 == bot) ||
204 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
205 return bot;
207 // Check for an addition involving the additive identity
208 const Type *tadd = add_of_identity( t1, t2 );
209 if( tadd ) return tadd;
211 return add_ring(t1,t2); // Local flavor of type addition
212 }
214 //------------------------------add_identity-----------------------------------
215 // Check for addition of the identity
216 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
217 const Type *zero = add_id(); // The additive identity
218 if( t1->higher_equal( zero ) ) return t2;
219 if( t2->higher_equal( zero ) ) return t1;
221 return NULL;
222 }
225 //=============================================================================
226 //------------------------------Idealize---------------------------------------
227 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
228 int op1 = in(1)->Opcode();
229 int op2 = in(2)->Opcode();
230 // Fold (con1-x)+con2 into (con1+con2)-x
231 if( op1 == Op_SubI ) {
232 const Type *t_sub1 = phase->type( in(1)->in(1) );
233 const Type *t_2 = phase->type( in(2) );
234 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
235 return new (phase->C, 3) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),
236 in(1)->in(2) );
237 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
238 if( op2 == Op_SubI ) {
239 // Check for dead cycle: d = (a-b)+(c-d)
240 assert( in(1)->in(2) != this && in(2)->in(2) != this,
241 "dead loop in AddINode::Ideal" );
242 Node *sub = new (phase->C, 3) SubINode(NULL, NULL);
243 sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in(1)->in(1), in(2)->in(1) ) ));
244 sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in(1)->in(2), in(2)->in(2) ) ));
245 return sub;
246 }
247 }
249 // Convert "x+(0-y)" into "(x-y)"
250 if( op2 == Op_SubI && phase->type(in(2)->in(1)) == TypeInt::ZERO )
251 return new (phase->C, 3) SubINode(in(1), in(2)->in(2) );
253 // Convert "(0-y)+x" into "(x-y)"
254 if( op1 == Op_SubI && phase->type(in(1)->in(1)) == TypeInt::ZERO )
255 return new (phase->C, 3) SubINode( in(2), in(1)->in(2) );
257 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
258 // Helps with array allocation math constant folding
259 // See 4790063:
260 // Unrestricted transformation is unsafe for some runtime values of 'x'
261 // ( x == 0, z == 1, y == -1 ) fails
262 // ( x == -5, z == 1, y == 1 ) fails
263 // Transform works for small z and small negative y when the addition
264 // (x + (y << z)) does not cross zero.
265 // Implement support for negative y and (x >= -(y << z))
266 // Have not observed cases where type information exists to support
267 // positive y and (x <= -(y << z))
268 if( op1 == Op_URShiftI && op2 == Op_ConI &&
269 in(1)->in(2)->Opcode() == Op_ConI ) {
270 jint z = phase->type( in(1)->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
271 jint y = phase->type( in(2) )->is_int()->get_con();
273 if( z < 5 && -5 < y && y < 0 ) {
274 const Type *t_in11 = phase->type(in(1)->in(1));
275 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
276 Node *a = phase->transform( new (phase->C, 3) AddINode( in(1)->in(1), phase->intcon(y<<z) ) );
277 return new (phase->C, 3) URShiftINode( a, in(1)->in(2) );
278 }
279 }
280 }
282 return AddNode::Ideal(phase, can_reshape);
283 }
286 //------------------------------Identity---------------------------------------
287 // Fold (x-y)+y OR y+(x-y) into x
288 Node *AddINode::Identity( PhaseTransform *phase ) {
289 if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {
290 return in(1)->in(1);
291 }
292 else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {
293 return in(2)->in(1);
294 }
295 return AddNode::Identity(phase);
296 }
299 //------------------------------add_ring---------------------------------------
300 // Supplied function returns the sum of the inputs. Guaranteed never
301 // to be passed a TOP or BOTTOM type, these are filtered out by
302 // pre-check.
303 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
304 const TypeInt *r0 = t0->is_int(); // Handy access
305 const TypeInt *r1 = t1->is_int();
306 int lo = r0->_lo + r1->_lo;
307 int hi = r0->_hi + r1->_hi;
308 if( !(r0->is_con() && r1->is_con()) ) {
309 // Not both constants, compute approximate result
310 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
311 lo = min_jint; hi = max_jint; // Underflow on the low side
312 }
313 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
314 lo = min_jint; hi = max_jint; // Overflow on the high side
315 }
316 if( lo > hi ) { // Handle overflow
317 lo = min_jint; hi = max_jint;
318 }
319 } else {
320 // both constants, compute precise result using 'lo' and 'hi'
321 // Semantics define overflow and underflow for integer addition
322 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
323 }
324 return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
325 }
328 //=============================================================================
329 //------------------------------Idealize---------------------------------------
330 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
331 int op1 = in(1)->Opcode();
332 int op2 = in(2)->Opcode();
333 // Fold (con1-x)+con2 into (con1+con2)-x
334 if( op1 == Op_SubL ) {
335 const Type *t_sub1 = phase->type( in(1)->in(1) );
336 const Type *t_2 = phase->type( in(2) );
337 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
338 return new (phase->C, 3) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),
339 in(1)->in(2) );
340 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
341 if( op2 == Op_SubL ) {
342 // Check for dead cycle: d = (a-b)+(c-d)
343 assert( in(1)->in(2) != this && in(2)->in(2) != this,
344 "dead loop in AddLNode::Ideal" );
345 Node *sub = new (phase->C, 3) SubLNode(NULL, NULL);
346 sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(1), in(2)->in(1) ) ));
347 sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(2), in(2)->in(2) ) ));
348 return sub;
349 }
350 }
352 // Convert "x+(0-y)" into "(x-y)"
353 if( op2 == Op_SubL && phase->type(in(2)->in(1)) == TypeLong::ZERO )
354 return new (phase->C, 3) SubLNode(in(1), in(2)->in(2) );
356 // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
357 // into "(X<<1)+Y" and let shift-folding happen.
358 if( op2 == Op_AddL &&
359 in(2)->in(1) == in(1) &&
360 op1 != Op_ConL &&
361 0 ) {
362 Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in(1),phase->intcon(1)));
363 return new (phase->C, 3) AddLNode(shift,in(2)->in(2));
364 }
366 return AddNode::Ideal(phase, can_reshape);
367 }
370 //------------------------------Identity---------------------------------------
371 // Fold (x-y)+y OR y+(x-y) into x
372 Node *AddLNode::Identity( PhaseTransform *phase ) {
373 if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {
374 return in(1)->in(1);
375 }
376 else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {
377 return in(2)->in(1);
378 }
379 return AddNode::Identity(phase);
380 }
383 //------------------------------add_ring---------------------------------------
384 // Supplied function returns the sum of the inputs. Guaranteed never
385 // to be passed a TOP or BOTTOM type, these are filtered out by
386 // pre-check.
387 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
388 const TypeLong *r0 = t0->is_long(); // Handy access
389 const TypeLong *r1 = t1->is_long();
390 jlong lo = r0->_lo + r1->_lo;
391 jlong hi = r0->_hi + r1->_hi;
392 if( !(r0->is_con() && r1->is_con()) ) {
393 // Not both constants, compute approximate result
394 if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
395 lo =min_jlong; hi = max_jlong; // Underflow on the low side
396 }
397 if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
398 lo = min_jlong; hi = max_jlong; // Overflow on the high side
399 }
400 if( lo > hi ) { // Handle overflow
401 lo = min_jlong; hi = max_jlong;
402 }
403 } else {
404 // both constants, compute precise result using 'lo' and 'hi'
405 // Semantics define overflow and underflow for integer addition
406 // as expected. In particular: 0x80000000 + 0x80000000 --> 0x0
407 }
408 return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
409 }
412 //=============================================================================
413 //------------------------------add_of_identity--------------------------------
414 // Check for addition of the identity
415 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
416 // x ADD 0 should return x unless 'x' is a -zero
417 //
418 // const Type *zero = add_id(); // The additive identity
419 // jfloat f1 = t1->getf();
420 // jfloat f2 = t2->getf();
421 //
422 // if( t1->higher_equal( zero ) ) return t2;
423 // if( t2->higher_equal( zero ) ) return t1;
425 return NULL;
426 }
428 //------------------------------add_ring---------------------------------------
429 // Supplied function returns the sum of the inputs.
430 // This also type-checks the inputs for sanity. Guaranteed never to
431 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
432 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
433 // We must be adding 2 float constants.
434 return TypeF::make( t0->getf() + t1->getf() );
435 }
437 //------------------------------Ideal------------------------------------------
438 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
439 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
440 return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
441 }
443 // Floating point additions are not associative because of boundary conditions (infinity)
444 return commute(this,
445 phase->type( in(1) )->singleton(),
446 phase->type( in(2) )->singleton() ) ? this : NULL;
447 }
450 //=============================================================================
451 //------------------------------add_of_identity--------------------------------
452 // Check for addition of the identity
453 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
454 // x ADD 0 should return x unless 'x' is a -zero
455 //
456 // const Type *zero = add_id(); // The additive identity
457 // jfloat f1 = t1->getf();
458 // jfloat f2 = t2->getf();
459 //
460 // if( t1->higher_equal( zero ) ) return t2;
461 // if( t2->higher_equal( zero ) ) return t1;
463 return NULL;
464 }
465 //------------------------------add_ring---------------------------------------
466 // Supplied function returns the sum of the inputs.
467 // This also type-checks the inputs for sanity. Guaranteed never to
468 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
469 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
470 // We must be adding 2 double constants.
471 return TypeD::make( t0->getd() + t1->getd() );
472 }
474 //------------------------------Ideal------------------------------------------
475 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
476 if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
477 return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
478 }
480 // Floating point additions are not associative because of boundary conditions (infinity)
481 return commute(this,
482 phase->type( in(1) )->singleton(),
483 phase->type( in(2) )->singleton() ) ? this : NULL;
484 }
487 //=============================================================================
488 //------------------------------Identity---------------------------------------
489 // If one input is a constant 0, return the other input.
490 Node *AddPNode::Identity( PhaseTransform *phase ) {
491 return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
492 }
494 //------------------------------Idealize---------------------------------------
495 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
496 // Bail out if dead inputs
497 if( phase->type( in(Address) ) == Type::TOP ) return NULL;
499 // If the left input is an add of a constant, flatten the expression tree.
500 const Node *n = in(Address);
501 if (n->is_AddP() && n->in(Base) == in(Base)) {
502 const AddPNode *addp = n->as_AddP(); // Left input is an AddP
503 assert( !addp->in(Address)->is_AddP() ||
504 addp->in(Address)->as_AddP() != addp,
505 "dead loop in AddPNode::Ideal" );
506 // Type of left input's right input
507 const Type *t = phase->type( addp->in(Offset) );
508 if( t == Type::TOP ) return NULL;
509 const TypeX *t12 = t->is_intptr_t();
510 if( t12->is_con() ) { // Left input is an add of a constant?
511 // If the right input is a constant, combine constants
512 const Type *temp_t2 = phase->type( in(Offset) );
513 if( temp_t2 == Type::TOP ) return NULL;
514 const TypeX *t2 = temp_t2->is_intptr_t();
515 Node* address;
516 Node* offset;
517 if( t2->is_con() ) {
518 // The Add of the flattened expression
519 address = addp->in(Address);
520 offset = phase->MakeConX(t2->get_con() + t12->get_con());
521 } else {
522 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
523 address = phase->transform(new (phase->C, 4) AddPNode(in(Base),addp->in(Address),in(Offset)));
524 offset = addp->in(Offset);
525 }
526 PhaseIterGVN *igvn = phase->is_IterGVN();
527 if( igvn ) {
528 set_req_X(Address,address,igvn);
529 set_req_X(Offset,offset,igvn);
530 } else {
531 set_req(Address,address);
532 set_req(Offset,offset);
533 }
534 return this;
535 }
536 }
538 // Raw pointers?
539 if( in(Base)->bottom_type() == Type::TOP ) {
540 // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
541 if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
542 Node* offset = in(Offset);
543 return new (phase->C, 2) CastX2PNode(offset);
544 }
545 }
547 // If the right is an add of a constant, push the offset down.
548 // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
549 // The idea is to merge array_base+scaled_index groups together,
550 // and only have different constant offsets from the same base.
551 const Node *add = in(Offset);
552 if( add->Opcode() == Op_AddX && add->in(1) != add ) {
553 const Type *t22 = phase->type( add->in(2) );
554 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
555 set_req(Address, phase->transform(new (phase->C, 4) AddPNode(in(Base),in(Address),add->in(1))));
556 set_req(Offset, add->in(2));
557 return this; // Made progress
558 }
559 }
561 return NULL; // No progress
562 }
564 //------------------------------bottom_type------------------------------------
565 // Bottom-type is the pointer-type with unknown offset.
566 const Type *AddPNode::bottom_type() const {
567 if (in(Address) == NULL) return TypePtr::BOTTOM;
568 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
569 if( !tp ) return Type::TOP; // TOP input means TOP output
570 assert( in(Offset)->Opcode() != Op_ConP, "" );
571 const Type *t = in(Offset)->bottom_type();
572 if( t == Type::TOP )
573 return tp->add_offset(Type::OffsetTop);
574 const TypeX *tx = t->is_intptr_t();
575 intptr_t txoffset = Type::OffsetBot;
576 if (tx->is_con()) { // Left input is an add of a constant?
577 txoffset = tx->get_con();
578 }
579 return tp->add_offset(txoffset);
580 }
582 //------------------------------Value------------------------------------------
583 const Type *AddPNode::Value( PhaseTransform *phase ) const {
584 // Either input is TOP ==> the result is TOP
585 const Type *t1 = phase->type( in(Address) );
586 const Type *t2 = phase->type( in(Offset) );
587 if( t1 == Type::TOP ) return Type::TOP;
588 if( t2 == Type::TOP ) return Type::TOP;
590 // Left input is a pointer
591 const TypePtr *p1 = t1->isa_ptr();
592 // Right input is an int
593 const TypeX *p2 = t2->is_intptr_t();
594 // Add 'em
595 intptr_t p2offset = Type::OffsetBot;
596 if (p2->is_con()) { // Left input is an add of a constant?
597 p2offset = p2->get_con();
598 }
599 return p1->add_offset(p2offset);
600 }
602 //------------------------Ideal_base_and_offset--------------------------------
603 // Split an oop pointer into a base and offset.
604 // (The offset might be Type::OffsetBot in the case of an array.)
605 // Return the base, or NULL if failure.
606 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
607 // second return value:
608 intptr_t& offset) {
609 if (ptr->is_AddP()) {
610 Node* base = ptr->in(AddPNode::Base);
611 Node* addr = ptr->in(AddPNode::Address);
612 Node* offs = ptr->in(AddPNode::Offset);
613 if (base == addr || base->is_top()) {
614 offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
615 if (offset != Type::OffsetBot) {
616 return addr;
617 }
618 }
619 }
620 offset = Type::OffsetBot;
621 return NULL;
622 }
624 //------------------------------unpack_offsets----------------------------------
625 // Collect the AddP offset values into the elements array, giving up
626 // if there are more than length.
627 int AddPNode::unpack_offsets(Node* elements[], int length) {
628 int count = 0;
629 Node* addr = this;
630 Node* base = addr->in(AddPNode::Base);
631 while (addr->is_AddP()) {
632 if (addr->in(AddPNode::Base) != base) {
633 // give up
634 return -1;
635 }
636 elements[count++] = addr->in(AddPNode::Offset);
637 if (count == length) {
638 // give up
639 return -1;
640 }
641 addr = addr->in(AddPNode::Address);
642 }
643 return count;
644 }
646 //------------------------------match_edge-------------------------------------
647 // Do we Match on this edge index or not? Do not match base pointer edge
648 uint AddPNode::match_edge(uint idx) const {
649 return idx > Base;
650 }
652 //---------------------------mach_bottom_type----------------------------------
653 // Utility function for use by ADLC. Implements bottom_type for matched AddP.
654 const Type *AddPNode::mach_bottom_type( const MachNode* n) {
655 Node* base = n->in(Base);
656 const Type *t = base->bottom_type();
657 if ( t == Type::TOP ) {
658 // an untyped pointer
659 return TypeRawPtr::BOTTOM;
660 }
661 const TypePtr* tp = t->isa_oopptr();
662 if ( tp == NULL ) return t;
663 if ( tp->_offset == TypePtr::OffsetBot ) return tp;
665 // We must carefully add up the various offsets...
666 intptr_t offset = 0;
667 const TypePtr* tptr = NULL;
669 uint numopnds = n->num_opnds();
670 uint index = n->oper_input_base();
671 for ( uint i = 1; i < numopnds; i++ ) {
672 MachOper *opnd = n->_opnds[i];
673 // Check for any interesting operand info.
674 // In particular, check for both memory and non-memory operands.
675 // %%%%% Clean this up: use xadd_offset
676 intptr_t con = opnd->constant();
677 if ( con == TypePtr::OffsetBot ) goto bottom_out;
678 offset += con;
679 con = opnd->constant_disp();
680 if ( con == TypePtr::OffsetBot ) goto bottom_out;
681 offset += con;
682 if( opnd->scale() != 0 ) goto bottom_out;
684 // Check each operand input edge. Find the 1 allowed pointer
685 // edge. Other edges must be index edges; track exact constant
686 // inputs and otherwise assume the worst.
687 for ( uint j = opnd->num_edges(); j > 0; j-- ) {
688 Node* edge = n->in(index++);
689 const Type* et = edge->bottom_type();
690 const TypeX* eti = et->isa_intptr_t();
691 if ( eti == NULL ) {
692 // there must be one pointer among the operands
693 guarantee(tptr == NULL, "must be only one pointer operand");
694 tptr = et->isa_oopptr();
695 guarantee(tptr != NULL, "non-int operand must be pointer");
696 if (tptr->higher_equal(tp->add_offset(tptr->offset())))
697 tp = tptr; // Set more precise type for bailout
698 continue;
699 }
700 if ( eti->_hi != eti->_lo ) goto bottom_out;
701 offset += eti->_lo;
702 }
703 }
704 guarantee(tptr != NULL, "must be exactly one pointer operand");
705 return tptr->add_offset(offset);
707 bottom_out:
708 return tp->add_offset(TypePtr::OffsetBot);
709 }
711 //=============================================================================
712 //------------------------------Identity---------------------------------------
713 Node *OrINode::Identity( PhaseTransform *phase ) {
714 // x | x => x
715 if (phase->eqv(in(1), in(2))) {
716 return in(1);
717 }
719 return AddNode::Identity(phase);
720 }
722 //------------------------------add_ring---------------------------------------
723 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
724 // the logical operations the ring's ADD is really a logical OR function.
725 // This also type-checks the inputs for sanity. Guaranteed never to
726 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
727 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
728 const TypeInt *r0 = t0->is_int(); // Handy access
729 const TypeInt *r1 = t1->is_int();
731 // If both args are bool, can figure out better types
732 if ( r0 == TypeInt::BOOL ) {
733 if ( r1 == TypeInt::ONE) {
734 return TypeInt::ONE;
735 } else if ( r1 == TypeInt::BOOL ) {
736 return TypeInt::BOOL;
737 }
738 } else if ( r0 == TypeInt::ONE ) {
739 if ( r1 == TypeInt::BOOL ) {
740 return TypeInt::ONE;
741 }
742 }
744 // If either input is not a constant, just return all integers.
745 if( !r0->is_con() || !r1->is_con() )
746 return TypeInt::INT; // Any integer, but still no symbols.
748 // Otherwise just OR them bits.
749 return TypeInt::make( r0->get_con() | r1->get_con() );
750 }
752 //=============================================================================
753 //------------------------------Identity---------------------------------------
754 Node *OrLNode::Identity( PhaseTransform *phase ) {
755 // x | x => x
756 if (phase->eqv(in(1), in(2))) {
757 return in(1);
758 }
760 return AddNode::Identity(phase);
761 }
763 //------------------------------add_ring---------------------------------------
764 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
765 const TypeLong *r0 = t0->is_long(); // Handy access
766 const TypeLong *r1 = t1->is_long();
768 // If either input is not a constant, just return all integers.
769 if( !r0->is_con() || !r1->is_con() )
770 return TypeLong::LONG; // Any integer, but still no symbols.
772 // Otherwise just OR them bits.
773 return TypeLong::make( r0->get_con() | r1->get_con() );
774 }
776 //=============================================================================
777 //------------------------------add_ring---------------------------------------
778 // Supplied function returns the sum of the inputs IN THE CURRENT RING. For
779 // the logical operations the ring's ADD is really a logical OR function.
780 // This also type-checks the inputs for sanity. Guaranteed never to
781 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
782 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
783 const TypeInt *r0 = t0->is_int(); // Handy access
784 const TypeInt *r1 = t1->is_int();
786 // Complementing a boolean?
787 if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
788 || r1 == TypeInt::BOOL))
789 return TypeInt::BOOL;
791 if( !r0->is_con() || !r1->is_con() ) // Not constants
792 return TypeInt::INT; // Any integer, but still no symbols.
794 // Otherwise just XOR them bits.
795 return TypeInt::make( r0->get_con() ^ r1->get_con() );
796 }
798 //=============================================================================
799 //------------------------------add_ring---------------------------------------
800 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
801 const TypeLong *r0 = t0->is_long(); // Handy access
802 const TypeLong *r1 = t1->is_long();
804 // If either input is not a constant, just return all integers.
805 if( !r0->is_con() || !r1->is_con() )
806 return TypeLong::LONG; // Any integer, but still no symbols.
808 // Otherwise just OR them bits.
809 return TypeLong::make( r0->get_con() ^ r1->get_con() );
810 }
812 //=============================================================================
813 //------------------------------add_ring---------------------------------------
814 // Supplied function returns the sum of the inputs.
815 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
816 const TypeInt *r0 = t0->is_int(); // Handy access
817 const TypeInt *r1 = t1->is_int();
819 // Otherwise just MAX them bits.
820 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
821 }
823 //=============================================================================
824 //------------------------------Idealize---------------------------------------
825 // MINs show up in range-check loop limit calculations. Look for
826 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
827 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
828 Node *progress = NULL;
829 // Force a right-spline graph
830 Node *l = in(1);
831 Node *r = in(2);
832 // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )
833 // to force a right-spline graph for the rest of MinINode::Ideal().
834 if( l->Opcode() == Op_MinI ) {
835 assert( l != l->in(1), "dead loop in MinINode::Ideal" );
836 r = phase->transform(new (phase->C, 3) MinINode(l->in(2),r));
837 l = l->in(1);
838 set_req(1, l);
839 set_req(2, r);
840 return this;
841 }
843 // Get left input & constant
844 Node *x = l;
845 int x_off = 0;
846 if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
847 x->in(2)->is_Con() ) {
848 const Type *t = x->in(2)->bottom_type();
849 if( t == Type::TOP ) return NULL; // No progress
850 x_off = t->is_int()->get_con();
851 x = x->in(1);
852 }
854 // Scan a right-spline-tree for MINs
855 Node *y = r;
856 int y_off = 0;
857 // Check final part of MIN tree
858 if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
859 y->in(2)->is_Con() ) {
860 const Type *t = y->in(2)->bottom_type();
861 if( t == Type::TOP ) return NULL; // No progress
862 y_off = t->is_int()->get_con();
863 y = y->in(1);
864 }
865 if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
866 swap_edges(1, 2);
867 return this;
868 }
871 if( r->Opcode() == Op_MinI ) {
872 assert( r != r->in(2), "dead loop in MinINode::Ideal" );
873 y = r->in(1);
874 // Check final part of MIN tree
875 if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
876 y->in(2)->is_Con() ) {
877 const Type *t = y->in(2)->bottom_type();
878 if( t == Type::TOP ) return NULL; // No progress
879 y_off = t->is_int()->get_con();
880 y = y->in(1);
881 }
883 if( x->_idx > y->_idx )
884 return new (phase->C, 3) MinINode(r->in(1),phase->transform(new (phase->C, 3) MinINode(l,r->in(2))));
886 // See if covers: MIN2(x+c0,MIN2(y+c1,z))
887 if( !phase->eqv(x,y) ) return NULL;
888 // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into
889 // MIN2(x+c0 or x+c1 which less, z).
890 return new (phase->C, 3) MinINode(phase->transform(new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2));
891 } else {
892 // See if covers: MIN2(x+c0,y+c1)
893 if( !phase->eqv(x,y) ) return NULL;
894 // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less.
895 return new (phase->C, 3) AddINode(x,phase->intcon(MIN2(x_off,y_off)));
896 }
898 }
900 //------------------------------add_ring---------------------------------------
901 // Supplied function returns the sum of the inputs.
902 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
903 const TypeInt *r0 = t0->is_int(); // Handy access
904 const TypeInt *r1 = t1->is_int();
906 // Otherwise just MIN them bits.
907 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
908 }