1 /*
2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "memory/allocation.inline.hpp"
27 #include "opto/addnode.hpp"
28 #include "opto/castnode.hpp"
29 #include "opto/cfgnode.hpp"
30 #include "opto/connode.hpp"
31 #include "opto/machnode.hpp"
32 #include "opto/movenode.hpp"
33 #include "opto/mulnode.hpp"
34 #include "opto/phaseX.hpp"
35 #include "opto/subnode.hpp"
36 
37 // Portions of code courtesy of Clifford Click
38 
39 // Classic Add functionality.  This covers all the usual 'add' behaviors for
40 // an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
41 // all inherited from this class.  The various identity values are supplied
42 // by virtual functions.
43 
44 
45 //=============================================================================
46 //------------------------------hash-------------------------------------------
47 // Hash function over AddNodes.  Needs to be commutative; i.e., I swap
48 // (commute) inputs to AddNodes willy-nilly so the hash function must return
49 // the same value in the presence of edge swapping.
hash() const50 uint AddNode::hash() const {
51   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
52 }
53 
54 //------------------------------Identity---------------------------------------
55 // If either input is a constant 0, return the other input.
Identity(PhaseGVN * phase)56 Node* AddNode::Identity(PhaseGVN* phase) {
57   const Type *zero = add_id();  // The additive identity
58   if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
59   if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
60   return this;
61 }
62 
63 //------------------------------commute----------------------------------------
64 // Commute operands to move loads and constants to the right.
commute(Node * add,bool con_left,bool con_right)65 static bool commute(Node *add, bool con_left, bool con_right) {
66   Node *in1 = add->in(1);
67   Node *in2 = add->in(2);
68 
69   // Convert "1+x" into "x+1".
70   // Right is a constant; leave it
71   if( con_right ) return false;
72   // Left is a constant; move it right.
73   if( con_left ) {
74     add->swap_edges(1, 2);
75     return true;
76   }
77 
78   // Convert "Load+x" into "x+Load".
79   // Now check for loads
80   if (in2->is_Load()) {
81     if (!in1->is_Load()) {
82       // already x+Load to return
83       return false;
84     }
85     // both are loads, so fall through to sort inputs by idx
86   } else if( in1->is_Load() ) {
87     // Left is a Load and Right is not; move it right.
88     add->swap_edges(1, 2);
89     return true;
90   }
91 
92   PhiNode *phi;
93   // Check for tight loop increments: Loop-phi of Add of loop-phi
94   if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
95     return false;
96   if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
97     add->swap_edges(1, 2);
98     return true;
99   }
100 
101   // Otherwise, sort inputs (commutativity) to help value numbering.
102   if( in1->_idx > in2->_idx ) {
103     add->swap_edges(1, 2);
104     return true;
105   }
106   return false;
107 }
108 
109 //------------------------------Idealize---------------------------------------
110 // If we get here, we assume we are associative!
Ideal(PhaseGVN * phase,bool can_reshape)111 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
112   const Type *t1 = phase->type(in(1));
113   const Type *t2 = phase->type(in(2));
114   bool con_left  = t1->singleton();
115   bool con_right = t2->singleton();
116 
117   // Check for commutative operation desired
118   if (commute(this, con_left, con_right)) return this;
119 
120   AddNode *progress = NULL;             // Progress flag
121 
122   // Convert "(x+1)+2" into "x+(1+2)".  If the right input is a
123   // constant, and the left input is an add of a constant, flatten the
124   // expression tree.
125   Node *add1 = in(1);
126   Node *add2 = in(2);
127   int add1_op = add1->Opcode();
128   int this_op = Opcode();
129   if (con_right && t2 != Type::TOP && // Right input is a constant?
130       add1_op == this_op) { // Left input is an Add?
131 
132     // Type of left _in right input
133     const Type *t12 = phase->type(add1->in(2));
134     if (t12->singleton() && t12 != Type::TOP) { // Left input is an add of a constant?
135       // Check for rare case of closed data cycle which can happen inside
136       // unreachable loops. In these cases the computation is undefined.
137 #ifdef ASSERT
138       Node *add11    = add1->in(1);
139       int   add11_op = add11->Opcode();
140       if ((add1 == add1->in(1))
141           || (add11_op == this_op && add11->in(1) == add1)) {
142         assert(false, "dead loop in AddNode::Ideal");
143       }
144 #endif
145       // The Add of the flattened expression
146       Node *x1 = add1->in(1);
147       Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
148       PhaseIterGVN *igvn = phase->is_IterGVN();
149       if (igvn) {
150         set_req_X(2,x2,igvn);
151         set_req_X(1,x1,igvn);
152       } else {
153         set_req(2,x2);
154         set_req(1,x1);
155       }
156       progress = this;            // Made progress
157       add1 = in(1);
158       add1_op = add1->Opcode();
159     }
160   }
161 
162   // Convert "(x+1)+y" into "(x+y)+1".  Push constants down the expression tree.
163   if (add1_op == this_op && !con_right) {
164     Node *a12 = add1->in(2);
165     const Type *t12 = phase->type( a12 );
166     if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
167         !(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
168       assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
169       add2 = add1->clone();
170       add2->set_req(2, in(2));
171       add2 = phase->transform(add2);
172       set_req(1, add2);
173       set_req(2, a12);
174       progress = this;
175       add2 = a12;
176     }
177   }
178 
179   // Convert "x+(y+1)" into "(x+y)+1".  Push constants down the expression tree.
180   int add2_op = add2->Opcode();
181   if (add2_op == this_op && !con_left) {
182     Node *a22 = add2->in(2);
183     const Type *t22 = phase->type( a22 );
184     if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
185         !(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
186       assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
187       Node *addx = add2->clone();
188       addx->set_req(1, in(1));
189       addx->set_req(2, add2->in(1));
190       addx = phase->transform(addx);
191       set_req(1, addx);
192       set_req(2, a22);
193       progress = this;
194       PhaseIterGVN* igvn = phase->is_IterGVN();
195       if (add2->outcnt() == 0 && igvn) {
196         // add disconnected.
197         igvn->_worklist.push(add2);
198       }
199     }
200   }
201 
202   return progress;
203 }
204 
205 //------------------------------Value-----------------------------------------
206 // An add node sums it's two _in.  If one input is an RSD, we must mixin
207 // the other input's symbols.
Value(PhaseGVN * phase) const208 const Type* AddNode::Value(PhaseGVN* phase) const {
209   // Either input is TOP ==> the result is TOP
210   const Type *t1 = phase->type( in(1) );
211   const Type *t2 = phase->type( in(2) );
212   if( t1 == Type::TOP ) return Type::TOP;
213   if( t2 == Type::TOP ) return Type::TOP;
214 
215   // Either input is BOTTOM ==> the result is the local BOTTOM
216   const Type *bot = bottom_type();
217   if( (t1 == bot) || (t2 == bot) ||
218       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
219     return bot;
220 
221   // Check for an addition involving the additive identity
222   const Type *tadd = add_of_identity( t1, t2 );
223   if( tadd ) return tadd;
224 
225   return add_ring(t1,t2);               // Local flavor of type addition
226 }
227 
228 //------------------------------add_identity-----------------------------------
229 // Check for addition of the identity
add_of_identity(const Type * t1,const Type * t2) const230 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
231   const Type *zero = add_id();  // The additive identity
232   if( t1->higher_equal( zero ) ) return t2;
233   if( t2->higher_equal( zero ) ) return t1;
234 
235   return NULL;
236 }
237 
make(Node * in1,Node * in2,BasicType bt)238 AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
239   switch (bt) {
240     case T_INT:
241       return new AddINode(in1, in2);
242     case T_LONG:
243       return new AddLNode(in1, in2);
244     default:
245       fatal("Not implemented for %s", type2name(bt));
246   }
247   return NULL;
248 }
249 
250 //=============================================================================
251 //------------------------------Idealize---------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)252 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
253   Node* in1 = in(1);
254   Node* in2 = in(2);
255   int op1 = in1->Opcode();
256   int op2 = in2->Opcode();
257   // Fold (con1-x)+con2 into (con1+con2)-x
258   if ( op1 == Op_AddI && op2 == Op_SubI ) {
259     // Swap edges to try optimizations below
260     in1 = in2;
261     in2 = in(1);
262     op1 = op2;
263     op2 = in2->Opcode();
264   }
265   if( op1 == Op_SubI ) {
266     const Type *t_sub1 = phase->type( in1->in(1) );
267     const Type *t_2    = phase->type( in2        );
268     if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
269       return new SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
270     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
271     if( op2 == Op_SubI ) {
272       // Check for dead cycle: d = (a-b)+(c-d)
273       assert( in1->in(2) != this && in2->in(2) != this,
274               "dead loop in AddINode::Ideal" );
275       Node *sub  = new SubINode(NULL, NULL);
276       sub->init_req(1, phase->transform(new AddINode(in1->in(1), in2->in(1) ) ));
277       sub->init_req(2, phase->transform(new AddINode(in1->in(2), in2->in(2) ) ));
278       return sub;
279     }
280     // Convert "(a-b)+(b+c)" into "(a+c)"
281     if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
282       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
283       return new AddINode(in1->in(1), in2->in(2));
284     }
285     // Convert "(a-b)+(c+b)" into "(a+c)"
286     if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
287       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
288       return new AddINode(in1->in(1), in2->in(1));
289     }
290     // Convert "(a-b)+(b-c)" into "(a-c)"
291     if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
292       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
293       return new SubINode(in1->in(1), in2->in(2));
294     }
295     // Convert "(a-b)+(c-a)" into "(c-b)"
296     if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
297       assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
298       return new SubINode(in2->in(1), in1->in(2));
299     }
300   }
301 
302   // Convert "x+(0-y)" into "(x-y)"
303   if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
304     return new SubINode(in1, in2->in(2) );
305 
306   // Convert "(0-y)+x" into "(x-y)"
307   if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
308     return new SubINode( in2, in1->in(2) );
309 
310   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
311   // Helps with array allocation math constant folding
312   // See 4790063:
313   // Unrestricted transformation is unsafe for some runtime values of 'x'
314   // ( x ==  0, z == 1, y == -1 ) fails
315   // ( x == -5, z == 1, y ==  1 ) fails
316   // Transform works for small z and small negative y when the addition
317   // (x + (y << z)) does not cross zero.
318   // Implement support for negative y and (x >= -(y << z))
319   // Have not observed cases where type information exists to support
320   // positive y and (x <= -(y << z))
321   if( op1 == Op_URShiftI && op2 == Op_ConI &&
322       in1->in(2)->Opcode() == Op_ConI ) {
323     jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
324     jint y = phase->type( in2 )->is_int()->get_con();
325 
326     if( z < 5 && -5 < y && y < 0 ) {
327       const Type *t_in11 = phase->type(in1->in(1));
328       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
329         Node *a = phase->transform( new AddINode( in1->in(1), phase->intcon(y<<z) ) );
330         return new URShiftINode( a, in1->in(2) );
331       }
332     }
333   }
334 
335   return AddNode::Ideal(phase, can_reshape);
336 }
337 
338 
339 //------------------------------Identity---------------------------------------
340 // Fold (x-y)+y  OR  y+(x-y)  into  x
Identity(PhaseGVN * phase)341 Node* AddINode::Identity(PhaseGVN* phase) {
342   if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
343     return in(1)->in(1);
344   } else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
345     return in(2)->in(1);
346   }
347   return AddNode::Identity(phase);
348 }
349 
350 
351 //------------------------------add_ring---------------------------------------
352 // Supplied function returns the sum of the inputs.  Guaranteed never
353 // to be passed a TOP or BOTTOM type, these are filtered out by
354 // pre-check.
add_ring(const Type * t0,const Type * t1) const355 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
356   const TypeInt *r0 = t0->is_int(); // Handy access
357   const TypeInt *r1 = t1->is_int();
358   int lo = java_add(r0->_lo, r1->_lo);
359   int hi = java_add(r0->_hi, r1->_hi);
360   if( !(r0->is_con() && r1->is_con()) ) {
361     // Not both constants, compute approximate result
362     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
363       lo = min_jint; hi = max_jint; // Underflow on the low side
364     }
365     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
366       lo = min_jint; hi = max_jint; // Overflow on the high side
367     }
368     if( lo > hi ) {               // Handle overflow
369       lo = min_jint; hi = max_jint;
370     }
371   } else {
372     // both constants, compute precise result using 'lo' and 'hi'
373     // Semantics define overflow and underflow for integer addition
374     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
375   }
376   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
377 }
378 
379 
380 //=============================================================================
381 //------------------------------Idealize---------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)382 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
383   Node* in1 = in(1);
384   Node* in2 = in(2);
385   int op1 = in1->Opcode();
386   int op2 = in2->Opcode();
387   // Fold (con1-x)+con2 into (con1+con2)-x
388   if ( op1 == Op_AddL && op2 == Op_SubL ) {
389     // Swap edges to try optimizations below
390     in1 = in2;
391     in2 = in(1);
392     op1 = op2;
393     op2 = in2->Opcode();
394   }
395   // Fold (con1-x)+con2 into (con1+con2)-x
396   if( op1 == Op_SubL ) {
397     const Type *t_sub1 = phase->type( in1->in(1) );
398     const Type *t_2    = phase->type( in2        );
399     if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
400       return new SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
401     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
402     if( op2 == Op_SubL ) {
403       // Check for dead cycle: d = (a-b)+(c-d)
404       assert( in1->in(2) != this && in2->in(2) != this,
405               "dead loop in AddLNode::Ideal" );
406       Node *sub  = new SubLNode(NULL, NULL);
407       sub->init_req(1, phase->transform(new AddLNode(in1->in(1), in2->in(1) ) ));
408       sub->init_req(2, phase->transform(new AddLNode(in1->in(2), in2->in(2) ) ));
409       return sub;
410     }
411     // Convert "(a-b)+(b+c)" into "(a+c)"
412     if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
413       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
414       return new AddLNode(in1->in(1), in2->in(2));
415     }
416     // Convert "(a-b)+(c+b)" into "(a+c)"
417     if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
418       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
419       return new AddLNode(in1->in(1), in2->in(1));
420     }
421     // Convert "(a-b)+(b-c)" into "(a-c)"
422     if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
423       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
424       return new SubLNode(in1->in(1), in2->in(2));
425     }
426     // Convert "(a-b)+(c-a)" into "(c-b)"
427     if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {
428       assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
429       return new SubLNode(in2->in(1), in1->in(2));
430     }
431   }
432 
433   // Convert "x+(0-y)" into "(x-y)"
434   if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
435     return new SubLNode( in1, in2->in(2) );
436 
437   // Convert "(0-y)+x" into "(x-y)"
438   if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )
439     return new SubLNode( in2, in1->in(2) );
440 
441   // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
442   // into "(X<<1)+Y" and let shift-folding happen.
443   if( op2 == Op_AddL &&
444       in2->in(1) == in1 &&
445       op1 != Op_ConL &&
446       0 ) {
447     Node *shift = phase->transform(new LShiftLNode(in1,phase->intcon(1)));
448     return new AddLNode(shift,in2->in(2));
449   }
450 
451   return AddNode::Ideal(phase, can_reshape);
452 }
453 
454 
455 //------------------------------Identity---------------------------------------
456 // Fold (x-y)+y  OR  y+(x-y)  into  x
Identity(PhaseGVN * phase)457 Node* AddLNode::Identity(PhaseGVN* phase) {
458   if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
459     return in(1)->in(1);
460   } else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
461     return in(2)->in(1);
462   }
463   return AddNode::Identity(phase);
464 }
465 
466 
467 //------------------------------add_ring---------------------------------------
468 // Supplied function returns the sum of the inputs.  Guaranteed never
469 // to be passed a TOP or BOTTOM type, these are filtered out by
470 // pre-check.
add_ring(const Type * t0,const Type * t1) const471 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
472   const TypeLong *r0 = t0->is_long(); // Handy access
473   const TypeLong *r1 = t1->is_long();
474   jlong lo = java_add(r0->_lo, r1->_lo);
475   jlong hi = java_add(r0->_hi, r1->_hi);
476   if( !(r0->is_con() && r1->is_con()) ) {
477     // Not both constants, compute approximate result
478     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
479       lo =min_jlong; hi = max_jlong; // Underflow on the low side
480     }
481     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
482       lo = min_jlong; hi = max_jlong; // Overflow on the high side
483     }
484     if( lo > hi ) {               // Handle overflow
485       lo = min_jlong; hi = max_jlong;
486     }
487   } else {
488     // both constants, compute precise result using 'lo' and 'hi'
489     // Semantics define overflow and underflow for integer addition
490     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
491   }
492   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
493 }
494 
495 
496 //=============================================================================
497 //------------------------------add_of_identity--------------------------------
498 // Check for addition of the identity
add_of_identity(const Type * t1,const Type * t2) const499 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
500   // x ADD 0  should return x unless 'x' is a -zero
501   //
502   // const Type *zero = add_id();     // The additive identity
503   // jfloat f1 = t1->getf();
504   // jfloat f2 = t2->getf();
505   //
506   // if( t1->higher_equal( zero ) ) return t2;
507   // if( t2->higher_equal( zero ) ) return t1;
508 
509   return NULL;
510 }
511 
512 //------------------------------add_ring---------------------------------------
513 // Supplied function returns the sum of the inputs.
514 // This also type-checks the inputs for sanity.  Guaranteed never to
515 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
add_ring(const Type * t0,const Type * t1) const516 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
517   // We must be adding 2 float constants.
518   return TypeF::make( t0->getf() + t1->getf() );
519 }
520 
521 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)522 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
523   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
524     return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
525   }
526 
527   // Floating point additions are not associative because of boundary conditions (infinity)
528   return commute(this,
529                  phase->type( in(1) )->singleton(),
530                  phase->type( in(2) )->singleton() ) ? this : NULL;
531 }
532 
533 
534 //=============================================================================
535 //------------------------------add_of_identity--------------------------------
536 // Check for addition of the identity
add_of_identity(const Type * t1,const Type * t2) const537 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
538   // x ADD 0  should return x unless 'x' is a -zero
539   //
540   // const Type *zero = add_id();     // The additive identity
541   // jfloat f1 = t1->getf();
542   // jfloat f2 = t2->getf();
543   //
544   // if( t1->higher_equal( zero ) ) return t2;
545   // if( t2->higher_equal( zero ) ) return t1;
546 
547   return NULL;
548 }
549 //------------------------------add_ring---------------------------------------
550 // Supplied function returns the sum of the inputs.
551 // This also type-checks the inputs for sanity.  Guaranteed never to
552 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
add_ring(const Type * t0,const Type * t1) const553 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
554   // We must be adding 2 double constants.
555   return TypeD::make( t0->getd() + t1->getd() );
556 }
557 
558 //------------------------------Ideal------------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)559 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
560   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
561     return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
562   }
563 
564   // Floating point additions are not associative because of boundary conditions (infinity)
565   return commute(this,
566                  phase->type( in(1) )->singleton(),
567                  phase->type( in(2) )->singleton() ) ? this : NULL;
568 }
569 
570 
571 //=============================================================================
572 //------------------------------Identity---------------------------------------
573 // If one input is a constant 0, return the other input.
Identity(PhaseGVN * phase)574 Node* AddPNode::Identity(PhaseGVN* phase) {
575   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
576 }
577 
578 //------------------------------Idealize---------------------------------------
Ideal(PhaseGVN * phase,bool can_reshape)579 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
580   // Bail out if dead inputs
581   if( phase->type( in(Address) ) == Type::TOP ) return NULL;
582 
583   // If the left input is an add of a constant, flatten the expression tree.
584   const Node *n = in(Address);
585   if (n->is_AddP() && n->in(Base) == in(Base)) {
586     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
587     assert( !addp->in(Address)->is_AddP() ||
588              addp->in(Address)->as_AddP() != addp,
589             "dead loop in AddPNode::Ideal" );
590     // Type of left input's right input
591     const Type *t = phase->type( addp->in(Offset) );
592     if( t == Type::TOP ) return NULL;
593     const TypeX *t12 = t->is_intptr_t();
594     if( t12->is_con() ) {       // Left input is an add of a constant?
595       // If the right input is a constant, combine constants
596       const Type *temp_t2 = phase->type( in(Offset) );
597       if( temp_t2 == Type::TOP ) return NULL;
598       const TypeX *t2 = temp_t2->is_intptr_t();
599       Node* address;
600       Node* offset;
601       if( t2->is_con() ) {
602         // The Add of the flattened expression
603         address = addp->in(Address);
604         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
605       } else {
606         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
607         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
608         offset  = addp->in(Offset);
609       }
610       PhaseIterGVN *igvn = phase->is_IterGVN();
611       if( igvn ) {
612         set_req_X(Address,address,igvn);
613         set_req_X(Offset,offset,igvn);
614       } else {
615         set_req(Address,address);
616         set_req(Offset,offset);
617       }
618       return this;
619     }
620   }
621 
622   // Raw pointers?
623   if( in(Base)->bottom_type() == Type::TOP ) {
624     // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
625     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
626       Node* offset = in(Offset);
627       return new CastX2PNode(offset);
628     }
629   }
630 
631   // If the right is an add of a constant, push the offset down.
632   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
633   // The idea is to merge array_base+scaled_index groups together,
634   // and only have different constant offsets from the same base.
635   const Node *add = in(Offset);
636   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
637     const Type *t22 = phase->type( add->in(2) );
638     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
639       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
640       set_req(Offset, add->in(2));
641       PhaseIterGVN* igvn = phase->is_IterGVN();
642       if (add->outcnt() == 0 && igvn) {
643         // add disconnected.
644         igvn->_worklist.push((Node*)add);
645       }
646       return this;              // Made progress
647     }
648   }
649 
650   return NULL;                  // No progress
651 }
652 
653 //------------------------------bottom_type------------------------------------
654 // Bottom-type is the pointer-type with unknown offset.
bottom_type() const655 const Type *AddPNode::bottom_type() const {
656   if (in(Address) == NULL)  return TypePtr::BOTTOM;
657   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
658   if( !tp ) return Type::TOP;   // TOP input means TOP output
659   assert( in(Offset)->Opcode() != Op_ConP, "" );
660   const Type *t = in(Offset)->bottom_type();
661   if( t == Type::TOP )
662     return tp->add_offset(Type::OffsetTop);
663   const TypeX *tx = t->is_intptr_t();
664   intptr_t txoffset = Type::OffsetBot;
665   if (tx->is_con()) {   // Left input is an add of a constant?
666     txoffset = tx->get_con();
667   }
668   return tp->add_offset(txoffset);
669 }
670 
671 //------------------------------Value------------------------------------------
Value(PhaseGVN * phase) const672 const Type* AddPNode::Value(PhaseGVN* phase) const {
673   // Either input is TOP ==> the result is TOP
674   const Type *t1 = phase->type( in(Address) );
675   const Type *t2 = phase->type( in(Offset) );
676   if( t1 == Type::TOP ) return Type::TOP;
677   if( t2 == Type::TOP ) return Type::TOP;
678 
679   // Left input is a pointer
680   const TypePtr *p1 = t1->isa_ptr();
681   // Right input is an int
682   const TypeX *p2 = t2->is_intptr_t();
683   // Add 'em
684   intptr_t p2offset = Type::OffsetBot;
685   if (p2->is_con()) {   // Left input is an add of a constant?
686     p2offset = p2->get_con();
687   }
688   return p1->add_offset(p2offset);
689 }
690 
691 //------------------------Ideal_base_and_offset--------------------------------
692 // Split an oop pointer into a base and offset.
693 // (The offset might be Type::OffsetBot in the case of an array.)
694 // Return the base, or NULL if failure.
Ideal_base_and_offset(Node * ptr,PhaseTransform * phase,intptr_t & offset)695 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
696                                       // second return value:
697                                       intptr_t& offset) {
698   if (ptr->is_AddP()) {
699     Node* base = ptr->in(AddPNode::Base);
700     Node* addr = ptr->in(AddPNode::Address);
701     Node* offs = ptr->in(AddPNode::Offset);
702     if (base == addr || base->is_top()) {
703       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
704       if (offset != Type::OffsetBot) {
705         return addr;
706       }
707     }
708   }
709   offset = Type::OffsetBot;
710   return NULL;
711 }
712 
713 //------------------------------unpack_offsets----------------------------------
714 // Collect the AddP offset values into the elements array, giving up
715 // if there are more than length.
unpack_offsets(Node * elements[],int length)716 int AddPNode::unpack_offsets(Node* elements[], int length) {
717   int count = 0;
718   Node* addr = this;
719   Node* base = addr->in(AddPNode::Base);
720   while (addr->is_AddP()) {
721     if (addr->in(AddPNode::Base) != base) {
722       // give up
723       return -1;
724     }
725     elements[count++] = addr->in(AddPNode::Offset);
726     if (count == length) {
727       // give up
728       return -1;
729     }
730     addr = addr->in(AddPNode::Address);
731   }
732   if (addr != base) {
733     return -1;
734   }
735   return count;
736 }
737 
738 //------------------------------match_edge-------------------------------------
739 // Do we Match on this edge index or not?  Do not match base pointer edge
match_edge(uint idx) const740 uint AddPNode::match_edge(uint idx) const {
741   return idx > Base;
742 }
743 
744 //=============================================================================
745 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)746 Node* OrINode::Identity(PhaseGVN* phase) {
747   // x | x => x
748   if (in(1) == in(2)) {
749     return in(1);
750   }
751 
752   return AddNode::Identity(phase);
753 }
754 
755 // Find shift value for Integer or Long OR.
rotate_shift(PhaseGVN * phase,Node * lshift,Node * rshift,int mask)756 Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
757   // val << norm_con_shift | val >> ({32|64} - norm_con_shift) => rotate_left val, norm_con_shift
758   const TypeInt* lshift_t = phase->type(lshift)->isa_int();
759   const TypeInt* rshift_t = phase->type(rshift)->isa_int();
760   if (lshift_t != NULL && lshift_t->is_con() &&
761       rshift_t != NULL && rshift_t->is_con() &&
762       ((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
763     return phase->intcon(lshift_t->get_con() & mask);
764   }
765   // val << var_shift | val >> ({0|32|64} - var_shift) => rotate_left val, var_shift
766   if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
767     const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
768     if (shift_t != NULL && shift_t->is_con() &&
769         (shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
770       return lshift;
771     }
772   }
773   return NULL;
774 }
775 
Ideal(PhaseGVN * phase,bool can_reshape)776 Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
777   int lopcode = in(1)->Opcode();
778   int ropcode = in(2)->Opcode();
779   if (Matcher::match_rule_supported(Op_RotateLeft) &&
780       lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
781     Node* lshift = in(1)->in(2);
782     Node* rshift = in(2)->in(2);
783     Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
784     if (shift != NULL) {
785       return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
786     }
787     return NULL;
788   }
789   if (Matcher::match_rule_supported(Op_RotateRight) &&
790       lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
791     Node* rshift = in(1)->in(2);
792     Node* lshift = in(2)->in(2);
793     Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
794     if (shift != NULL) {
795       return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
796     }
797   }
798   return NULL;
799 }
800 
801 //------------------------------add_ring---------------------------------------
802 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
803 // the logical operations the ring's ADD is really a logical OR function.
804 // This also type-checks the inputs for sanity.  Guaranteed never to
805 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
add_ring(const Type * t0,const Type * t1) const806 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
807   const TypeInt *r0 = t0->is_int(); // Handy access
808   const TypeInt *r1 = t1->is_int();
809 
810   // If both args are bool, can figure out better types
811   if ( r0 == TypeInt::BOOL ) {
812     if ( r1 == TypeInt::ONE) {
813       return TypeInt::ONE;
814     } else if ( r1 == TypeInt::BOOL ) {
815       return TypeInt::BOOL;
816     }
817   } else if ( r0 == TypeInt::ONE ) {
818     if ( r1 == TypeInt::BOOL ) {
819       return TypeInt::ONE;
820     }
821   }
822 
823   // If either input is not a constant, just return all integers.
824   if( !r0->is_con() || !r1->is_con() )
825     return TypeInt::INT;        // Any integer, but still no symbols.
826 
827   // Otherwise just OR them bits.
828   return TypeInt::make( r0->get_con() | r1->get_con() );
829 }
830 
831 //=============================================================================
832 //------------------------------Identity---------------------------------------
Identity(PhaseGVN * phase)833 Node* OrLNode::Identity(PhaseGVN* phase) {
834   // x | x => x
835   if (in(1) == in(2)) {
836     return in(1);
837   }
838 
839   return AddNode::Identity(phase);
840 }
841 
Ideal(PhaseGVN * phase,bool can_reshape)842 Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
843   int lopcode = in(1)->Opcode();
844   int ropcode = in(2)->Opcode();
845   if (Matcher::match_rule_supported(Op_RotateLeft) &&
846       lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
847     Node* lshift = in(1)->in(2);
848     Node* rshift = in(2)->in(2);
849     Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
850     if (shift != NULL) {
851       return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
852     }
853     return NULL;
854   }
855   if (Matcher::match_rule_supported(Op_RotateRight) &&
856       lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
857     Node* rshift = in(1)->in(2);
858     Node* lshift = in(2)->in(2);
859     Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
860     if (shift != NULL) {
861       return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
862     }
863   }
864   return NULL;
865 }
866 
867 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const868 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
869   const TypeLong *r0 = t0->is_long(); // Handy access
870   const TypeLong *r1 = t1->is_long();
871 
872   // If either input is not a constant, just return all integers.
873   if( !r0->is_con() || !r1->is_con() )
874     return TypeLong::LONG;      // Any integer, but still no symbols.
875 
876   // Otherwise just OR them bits.
877   return TypeLong::make( r0->get_con() | r1->get_con() );
878 }
879 
880 //=============================================================================
881 //------------------------------add_ring---------------------------------------
882 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
883 // the logical operations the ring's ADD is really a logical OR function.
884 // This also type-checks the inputs for sanity.  Guaranteed never to
885 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
add_ring(const Type * t0,const Type * t1) const886 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
887   const TypeInt *r0 = t0->is_int(); // Handy access
888   const TypeInt *r1 = t1->is_int();
889 
890   // Complementing a boolean?
891   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
892                                || r1 == TypeInt::BOOL))
893     return TypeInt::BOOL;
894 
895   if( !r0->is_con() || !r1->is_con() ) // Not constants
896     return TypeInt::INT;        // Any integer, but still no symbols.
897 
898   // Otherwise just XOR them bits.
899   return TypeInt::make( r0->get_con() ^ r1->get_con() );
900 }
901 
902 //=============================================================================
903 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const904 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
905   const TypeLong *r0 = t0->is_long(); // Handy access
906   const TypeLong *r1 = t1->is_long();
907 
908   // If either input is not a constant, just return all integers.
909   if( !r0->is_con() || !r1->is_con() )
910     return TypeLong::LONG;      // Any integer, but still no symbols.
911 
912   // Otherwise just OR them bits.
913   return TypeLong::make( r0->get_con() ^ r1->get_con() );
914 }
915 
916 
build_min_max(Node * a,Node * b,bool is_max,bool is_unsigned,const Type * t,PhaseGVN & gvn)917 Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
918   bool is_int = gvn.type(a)->isa_int();
919   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
920   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
921   Node* hook = NULL;
922   if (gvn.is_IterGVN()) {
923     // Make sure a and b are not destroyed
924     hook = new Node(2);
925     hook->init_req(0, a);
926     hook->init_req(1, b);
927   }
928   Node* res = NULL;
929   if (!is_unsigned) {
930     if (is_max) {
931       if (is_int) {
932         res =  gvn.transform(new MaxINode(a, b));
933         assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
934       } else {
935         Node* cmp = gvn.transform(new CmpLNode(a, b));
936         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
937         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
938       }
939     } else {
940       if (is_int) {
941         Node* res =  gvn.transform(new MinINode(a, b));
942         assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
943       } else {
944         Node* cmp = gvn.transform(new CmpLNode(b, a));
945         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
946         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
947       }
948     }
949   } else {
950     if (is_max) {
951       if (is_int) {
952         Node* cmp = gvn.transform(new CmpUNode(a, b));
953         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
954         res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
955       } else {
956         Node* cmp = gvn.transform(new CmpULNode(a, b));
957         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
958         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
959       }
960     } else {
961       if (is_int) {
962         Node* cmp = gvn.transform(new CmpUNode(b, a));
963         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
964         res = gvn.transform(new CMoveINode(bol, a, b, t->is_int()));
965       } else {
966         Node* cmp = gvn.transform(new CmpULNode(b, a));
967         Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
968         res = gvn.transform(new CMoveLNode(bol, a, b, t->is_long()));
969       }
970     }
971   }
972   if (hook != NULL) {
973     hook->destruct(&gvn);
974   }
975   return res;
976 }
977 
build_min_max_diff_with_zero(Node * a,Node * b,bool is_max,const Type * t,PhaseGVN & gvn)978 Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
979   bool is_int = gvn.type(a)->isa_int();
980   assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
981   assert(is_int == (gvn.type(b)->isa_int() != NULL), "inconsistent inputs");
982   Node* zero = NULL;
983   if (is_int) {
984     zero = gvn.intcon(0);
985   } else {
986     zero = gvn.longcon(0);
987   }
988   Node* hook = NULL;
989   if (gvn.is_IterGVN()) {
990     // Make sure a and b are not destroyed
991     hook = new Node(2);
992     hook->init_req(0, a);
993     hook->init_req(1, b);
994   }
995   Node* res = NULL;
996   if (is_max) {
997     if (is_int) {
998       Node* cmp = gvn.transform(new CmpINode(a, b));
999       Node* sub = gvn.transform(new SubINode(a, b));
1000       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1001       res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1002     } else {
1003       Node* cmp = gvn.transform(new CmpLNode(a, b));
1004       Node* sub = gvn.transform(new SubLNode(a, b));
1005       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1006       res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1007     }
1008   } else {
1009     if (is_int) {
1010       Node* cmp = gvn.transform(new CmpINode(b, a));
1011       Node* sub = gvn.transform(new SubINode(a, b));
1012       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1013       res = gvn.transform(new CMoveINode(bol, sub, zero, t->is_int()));
1014     } else {
1015       Node* cmp = gvn.transform(new CmpLNode(b, a));
1016       Node* sub = gvn.transform(new SubLNode(a, b));
1017       Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1018       res = gvn.transform(new CMoveLNode(bol, sub, zero, t->is_long()));
1019     }
1020   }
1021   if (hook != NULL) {
1022     hook->destruct(&gvn);
1023   }
1024   return res;
1025 }
1026 
1027 //=============================================================================
1028 //------------------------------add_ring---------------------------------------
1029 // Supplied function returns the sum of the inputs.
add_ring(const Type * t0,const Type * t1) const1030 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1031   const TypeInt *r0 = t0->is_int(); // Handy access
1032   const TypeInt *r1 = t1->is_int();
1033 
1034   // Otherwise just MAX them bits.
1035   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1036 }
1037 
1038 // Check if addition of an integer with type 't' and a constant 'c' can overflow
can_overflow(const TypeInt * t,jint c)1039 static bool can_overflow(const TypeInt* t, jint c) {
1040   jint t_lo = t->_lo;
1041   jint t_hi = t->_hi;
1042   return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1043           (c > 0 && (java_add(t_hi, c) < t_hi)));
1044 }
1045 
1046 //=============================================================================
1047 //------------------------------Idealize---------------------------------------
1048 // MINs show up in range-check loop limit calculations.  Look for
1049 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
Ideal(PhaseGVN * phase,bool can_reshape)1050 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1051   Node *progress = NULL;
1052   // Force a right-spline graph
1053   Node *l = in(1);
1054   Node *r = in(2);
1055   // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
1056   // to force a right-spline graph for the rest of MinINode::Ideal().
1057   if( l->Opcode() == Op_MinI ) {
1058     assert( l != l->in(1), "dead loop in MinINode::Ideal" );
1059     r = phase->transform(new MinINode(l->in(2),r));
1060     l = l->in(1);
1061     set_req(1, l);
1062     set_req(2, r);
1063     return this;
1064   }
1065 
1066   // Get left input & constant
1067   Node *x = l;
1068   jint x_off = 0;
1069   if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
1070       x->in(2)->is_Con() ) {
1071     const Type *t = x->in(2)->bottom_type();
1072     if( t == Type::TOP ) return NULL;  // No progress
1073     x_off = t->is_int()->get_con();
1074     x = x->in(1);
1075   }
1076 
1077   // Scan a right-spline-tree for MINs
1078   Node *y = r;
1079   jint y_off = 0;
1080   // Check final part of MIN tree
1081   if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
1082       y->in(2)->is_Con() ) {
1083     const Type *t = y->in(2)->bottom_type();
1084     if( t == Type::TOP ) return NULL;  // No progress
1085     y_off = t->is_int()->get_con();
1086     y = y->in(1);
1087   }
1088   if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
1089     swap_edges(1, 2);
1090     return this;
1091   }
1092 
1093   const TypeInt* tx = phase->type(x)->isa_int();
1094 
1095   if( r->Opcode() == Op_MinI ) {
1096     assert( r != r->in(2), "dead loop in MinINode::Ideal" );
1097     y = r->in(1);
1098     // Check final part of MIN tree
1099     if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
1100         y->in(2)->is_Con() ) {
1101       const Type *t = y->in(2)->bottom_type();
1102       if( t == Type::TOP ) return NULL;  // No progress
1103       y_off = t->is_int()->get_con();
1104       y = y->in(1);
1105     }
1106 
1107     if( x->_idx > y->_idx )
1108       return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
1109 
1110     // Transform MIN2(x + c0, MIN2(x + c1, z)) into MIN2(x + MIN2(c0, c1), z)
1111     // if x == y and the additions can't overflow.
1112     if (x == y && tx != NULL &&
1113         !can_overflow(tx, x_off) &&
1114         !can_overflow(tx, y_off)) {
1115       return new MinINode(phase->transform(new AddINode(x, phase->intcon(MIN2(x_off, y_off)))), r->in(2));
1116     }
1117   } else {
1118     // Transform MIN2(x + c0, y + c1) into x + MIN2(c0, c1)
1119     // if x == y and the additions can't overflow.
1120     if (x == y && tx != NULL &&
1121         !can_overflow(tx, x_off) &&
1122         !can_overflow(tx, y_off)) {
1123       return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
1124     }
1125   }
1126   return NULL;
1127 }
1128 
1129 //------------------------------add_ring---------------------------------------
1130 // Supplied function returns the sum of the inputs.
add_ring(const Type * t0,const Type * t1) const1131 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1132   const TypeInt *r0 = t0->is_int(); // Handy access
1133   const TypeInt *r1 = t1->is_int();
1134 
1135   // Otherwise just MIN them bits.
1136   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1137 }
1138 
1139 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const1140 const Type *MinFNode::add_ring( const Type *t0, const Type *t1 ) const {
1141   const TypeF *r0 = t0->is_float_constant();
1142   const TypeF *r1 = t1->is_float_constant();
1143 
1144   if (r0->is_nan()) {
1145     return r0;
1146   }
1147   if (r1->is_nan()) {
1148     return r1;
1149   }
1150 
1151   float f0 = r0->getf();
1152   float f1 = r1->getf();
1153   if (f0 != 0.0f || f1 != 0.0f) {
1154     return f0 < f1 ? r0 : r1;
1155   }
1156 
1157   // handle min of 0.0, -0.0 case.
1158   return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1159 }
1160 
1161 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const1162 const Type *MinDNode::add_ring( const Type *t0, const Type *t1 ) const {
1163   const TypeD *r0 = t0->is_double_constant();
1164   const TypeD *r1 = t1->is_double_constant();
1165 
1166   if (r0->is_nan()) {
1167     return r0;
1168   }
1169   if (r1->is_nan()) {
1170     return r1;
1171   }
1172 
1173   double d0 = r0->getd();
1174   double d1 = r1->getd();
1175   if (d0 != 0.0 || d1 != 0.0) {
1176     return d0 < d1 ? r0 : r1;
1177   }
1178 
1179   // handle min of 0.0, -0.0 case.
1180   return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1181 }
1182 
1183 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const1184 const Type *MaxFNode::add_ring( const Type *t0, const Type *t1 ) const {
1185   const TypeF *r0 = t0->is_float_constant();
1186   const TypeF *r1 = t1->is_float_constant();
1187 
1188   if (r0->is_nan()) {
1189     return r0;
1190   }
1191   if (r1->is_nan()) {
1192     return r1;
1193   }
1194 
1195   float f0 = r0->getf();
1196   float f1 = r1->getf();
1197   if (f0 != 0.0f || f1 != 0.0f) {
1198     return f0 > f1 ? r0 : r1;
1199   }
1200 
1201   // handle max of 0.0,-0.0 case.
1202   return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1203 }
1204 
1205 //------------------------------add_ring---------------------------------------
add_ring(const Type * t0,const Type * t1) const1206 const Type *MaxDNode::add_ring( const Type *t0, const Type *t1 ) const {
1207   const TypeD *r0 = t0->is_double_constant();
1208   const TypeD *r1 = t1->is_double_constant();
1209 
1210   if (r0->is_nan()) {
1211     return r0;
1212   }
1213   if (r1->is_nan()) {
1214     return r1;
1215   }
1216 
1217   double d0 = r0->getd();
1218   double d1 = r1->getd();
1219   if (d0 != 0.0 || d1 != 0.0) {
1220     return d0 > d1 ? r0 : r1;
1221   }
1222 
1223   // handle max of 0.0, -0.0 case.
1224   return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;
1225 }
1226