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/mulnode.hpp"
  33 #include "opto/phaseX.hpp"
  34 #include "opto/subnode.hpp"
  35 
  36 // Portions of code courtesy of Clifford Click
  37 
  38 // Classic Add functionality.  This covers all the usual 'add' behaviors for
  39 // an algebraic ring.  Add-integer, add-float, add-double, and binary-or are
  40 // all inherited from this class.  The various identity values are supplied
  41 // by virtual functions.
  42 
  43 
  44 //=============================================================================
  45 //------------------------------hash-------------------------------------------
  46 // Hash function over AddNodes.  Needs to be commutative; i.e., I swap
  47 // (commute) inputs to AddNodes willy-nilly so the hash function must return
  48 // the same value in the presence of edge swapping.
  49 uint AddNode::hash() const {
  50   return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
  51 }
  52 
  53 //------------------------------Identity---------------------------------------
  54 // If either input is a constant 0, return the other input.
  55 Node* AddNode::Identity(PhaseGVN* phase) {
  56   const Type *zero = add_id();  // The additive identity
  57   if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
  58   if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
  59   return this;
  60 }
  61 
  62 //------------------------------commute----------------------------------------
  63 // Commute operands to move loads and constants to the right.
  64 static bool commute(Node *add, bool con_left, bool con_right) {
  65   Node *in1 = add->in(1);
  66   Node *in2 = add->in(2);
  67 
  68   // Convert "1+x" into "x+1".
  69   // Right is a constant; leave it
  70   if( con_right ) return false;
  71   // Left is a constant; move it right.
  72   if( con_left ) {
  73     add->swap_edges(1, 2);
  74     return true;
  75   }
  76 
  77   // Convert "Load+x" into "x+Load".
  78   // Now check for loads
  79   if (in2->is_Load()) {
  80     if (!in1->is_Load()) {
  81       // already x+Load to return
  82       return false;
  83     }
  84     // both are loads, so fall through to sort inputs by idx
  85   } else if( in1->is_Load() ) {
  86     // Left is a Load and Right is not; move it right.
  87     add->swap_edges(1, 2);
  88     return true;
  89   }
  90 
  91   PhiNode *phi;
  92   // Check for tight loop increments: Loop-phi of Add of loop-phi
  93   if( in1->is_Phi() && (phi = in1->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add)
  94     return false;
  95   if( in2->is_Phi() && (phi = in2->as_Phi()) && !phi->is_copy() && phi->region()->is_Loop() && phi->in(2)==add){
  96     add->swap_edges(1, 2);
  97     return true;
  98   }
  99 
 100   // Otherwise, sort inputs (commutativity) to help value numbering.
 101   if( in1->_idx > in2->_idx ) {
 102     add->swap_edges(1, 2);
 103     return true;
 104   }
 105   return false;
 106 }
 107 
 108 //------------------------------Idealize---------------------------------------
 109 // If we get here, we assume we are associative!
 110 Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 111   const Type *t1 = phase->type( in(1) );
 112   const Type *t2 = phase->type( in(2) );
 113   bool con_left  = t1->singleton();
 114   bool con_right = t2->singleton();
 115 
 116   // Check for commutative operation desired
 117   if( commute(this,con_left,con_right) ) return this;
 118 
 119   AddNode *progress = NULL;             // Progress flag
 120 
 121   // Convert "(x+1)+2" into "x+(1+2)".  If the right input is a
 122   // constant, and the left input is an add of a constant, flatten the
 123   // expression tree.
 124   Node *add1 = in(1);
 125   Node *add2 = in(2);
 126   int add1_op = add1->Opcode();
 127   int this_op = Opcode();
 128   if( con_right && t2 != Type::TOP && // Right input is a constant?
 129       add1_op == this_op ) { // Left input is an Add?
 130 
 131     // Type of left _in right input
 132     const Type *t12 = phase->type( add1->in(2) );
 133     if( t12->singleton() && t12 != Type::TOP ) { // Left input is an add of a constant?
 134       // Check for rare case of closed data cycle which can happen inside
 135       // unreachable loops. In these cases the computation is undefined.
 136 #ifdef ASSERT
 137       Node *add11    = add1->in(1);
 138       int   add11_op = add11->Opcode();
 139       if( (add1 == add1->in(1))
 140          || (add11_op == this_op && add11->in(1) == add1) ) {
 141         assert(false, "dead loop in AddNode::Ideal");
 142       }
 143 #endif
 144       // The Add of the flattened expression
 145       Node *x1 = add1->in(1);
 146       Node *x2 = phase->makecon( add1->as_Add()->add_ring( t2, t12 ));
 147       PhaseIterGVN *igvn = phase->is_IterGVN();
 148       if( igvn ) {
 149         set_req_X(2,x2,igvn);
 150         set_req_X(1,x1,igvn);
 151       } else {
 152         set_req(2,x2);
 153         set_req(1,x1);
 154       }
 155       progress = this;            // Made progress
 156       add1 = in(1);
 157       add1_op = add1->Opcode();
 158     }
 159   }
 160 
 161   // Convert "(x+1)+y" into "(x+y)+1".  Push constants down the expression tree.
 162   if( add1_op == this_op && !con_right ) {
 163     Node *a12 = add1->in(2);
 164     const Type *t12 = phase->type( a12 );
 165     if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
 166        !(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) {
 167       assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
 168       add2 = add1->clone();
 169       add2->set_req(2, in(2));
 170       add2 = phase->transform(add2);
 171       set_req(1, add2);
 172       set_req(2, a12);
 173       progress = this;
 174       add2 = a12;
 175     }
 176   }
 177 
 178   // Convert "x+(y+1)" into "(x+y)+1".  Push constants down the expression tree.
 179   int add2_op = add2->Opcode();
 180   if( add2_op == this_op && !con_left ) {
 181     Node *a22 = add2->in(2);
 182     const Type *t22 = phase->type( a22 );
 183     if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
 184        !(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) {
 185       assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
 186       Node *addx = add2->clone();
 187       addx->set_req(1, in(1));
 188       addx->set_req(2, add2->in(1));
 189       addx = phase->transform(addx);
 190       set_req(1, addx);
 191       set_req(2, a22);
 192       progress = this;
 193       PhaseIterGVN *igvn = phase->is_IterGVN();
 194       if (add2->outcnt() == 0 && igvn) {
 195         // add disconnected.
 196         igvn->_worklist.push(add2);
 197       }
 198     }
 199   }
 200 
 201   return progress;
 202 }
 203 
 204 //------------------------------Value-----------------------------------------
 205 // An add node sums it's two _in.  If one input is an RSD, we must mixin
 206 // the other input's symbols.
 207 const Type* AddNode::Value(PhaseGVN* phase) const {
 208   // Either input is TOP ==> the result is TOP
 209   const Type *t1 = phase->type( in(1) );
 210   const Type *t2 = phase->type( in(2) );
 211   if( t1 == Type::TOP ) return Type::TOP;
 212   if( t2 == Type::TOP ) return Type::TOP;
 213 
 214   // Either input is BOTTOM ==> the result is the local BOTTOM
 215   const Type *bot = bottom_type();
 216   if( (t1 == bot) || (t2 == bot) ||
 217       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 218     return bot;
 219 
 220   // Check for an addition involving the additive identity
 221   const Type *tadd = add_of_identity( t1, t2 );
 222   if( tadd ) return tadd;
 223 
 224   return add_ring(t1,t2);               // Local flavor of type addition
 225 }
 226 
 227 //------------------------------add_identity-----------------------------------
 228 // Check for addition of the identity
 229 const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 230   const Type *zero = add_id();  // The additive identity
 231   if( t1->higher_equal( zero ) ) return t2;
 232   if( t2->higher_equal( zero ) ) return t1;
 233 
 234   return NULL;
 235 }
 236 
 237 
 238 //=============================================================================
 239 
 240 static Node* convert_add_to_muladd(PhaseGVN *phase, Node* in1, Node* in2) {
 241   // Only convert to this type of node if backend supports and if vectorizer supports it as well.
 242   // The reason vectorizer is also checked is because this transformation is explicitly done
 243   // to make vectorizer analysis easier for forms where type transition happens during operation.
 244   if (Matcher::match_rule_supported(Op_MulAddS2I) &&
 245     Matcher::match_rule_supported(Op_MulAddVS2VI)) {
 246     if (in1->Opcode() == Op_MulI && in2->Opcode() == Op_MulI) {
 247       Node* mul_in1 = in1->in(1);
 248       Node* mul_in2 = in1->in(2);
 249       Node* mul_in3 = in2->in(1);
 250       Node* mul_in4 = in2->in(2);
 251 
 252       if (mul_in1->Opcode() == Op_LoadS &&
 253         mul_in2->Opcode() == Op_LoadS &&
 254         mul_in3->Opcode() == Op_LoadS &&
 255         mul_in4->Opcode() == Op_LoadS) {
 256         Node* adr1 = mul_in1->in(MemNode::Address);
 257         Node* adr2 = mul_in2->in(MemNode::Address);
 258         Node* adr3 = mul_in3->in(MemNode::Address);
 259         Node* adr4 = mul_in4->in(MemNode::Address);
 260 
 261         if (adr1->is_AddP() && adr2->is_AddP() && adr3->is_AddP() && adr4->is_AddP()) {
 262           if ((adr1->in(AddPNode::Base) == adr3->in(AddPNode::Base)) &&
 263             (adr2->in(AddPNode::Base) == adr4->in(AddPNode::Base))) {
 264             return new MulAddS2INode(mul_in1, mul_in2, mul_in3, mul_in4);
 265           }
 266         }
 267       }
 268     }
 269   }
 270   return NULL;
 271 }
 272 
 273 //------------------------------Idealize---------------------------------------
 274 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 275   Node* in1 = in(1);
 276   Node* in2 = in(2);
 277   int op1 = in1->Opcode();
 278   int op2 = in2->Opcode();
 279   // Fold (con1-x)+con2 into (con1+con2)-x
 280   if ( op1 == Op_AddI && op2 == Op_SubI ) {
 281     // Swap edges to try optimizations below
 282     in1 = in2;
 283     in2 = in(1);
 284     op1 = op2;
 285     op2 = in2->Opcode();
 286   }
 287   if( op1 == Op_SubI ) {
 288     const Type *t_sub1 = phase->type( in1->in(1) );
 289     const Type *t_2    = phase->type( in2        );
 290     if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
 291       return new SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
 292     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
 293     if( op2 == Op_SubI ) {
 294       // Check for dead cycle: d = (a-b)+(c-d)
 295       assert( in1->in(2) != this && in2->in(2) != this,
 296               "dead loop in AddINode::Ideal" );
 297       Node *sub  = new SubINode(NULL, NULL);
 298       sub->init_req(1, phase->transform(new AddINode(in1->in(1), in2->in(1) ) ));
 299       sub->init_req(2, phase->transform(new AddINode(in1->in(2), in2->in(2) ) ));
 300       return sub;
 301     }
 302     // Convert "(a-b)+(b+c)" into "(a+c)"
 303     if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
 304       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
 305       return new AddINode(in1->in(1), in2->in(2));
 306     }
 307     // Convert "(a-b)+(c+b)" into "(a+c)"
 308     if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
 309       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
 310       return new AddINode(in1->in(1), in2->in(1));
 311     }
 312     // Convert "(a-b)+(b-c)" into "(a-c)"
 313     if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
 314       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
 315       return new SubINode(in1->in(1), in2->in(2));
 316     }
 317     // Convert "(a-b)+(c-a)" into "(c-b)"
 318     if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
 319       assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
 320       return new SubINode(in2->in(1), in1->in(2));
 321     }
 322   }
 323 
 324   // Convert "x+(0-y)" into "(x-y)"
 325   if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
 326     return new SubINode(in1, in2->in(2) );
 327 
 328   // Convert "(0-y)+x" into "(x-y)"
 329   if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
 330     return new SubINode( in2, in1->in(2) );
 331 
 332   // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
 333   // Helps with array allocation math constant folding
 334   // See 4790063:
 335   // Unrestricted transformation is unsafe for some runtime values of 'x'
 336   // ( x ==  0, z == 1, y == -1 ) fails
 337   // ( x == -5, z == 1, y ==  1 ) fails
 338   // Transform works for small z and small negative y when the addition
 339   // (x + (y << z)) does not cross zero.
 340   // Implement support for negative y and (x >= -(y << z))
 341   // Have not observed cases where type information exists to support
 342   // positive y and (x <= -(y << z))
 343   if( op1 == Op_URShiftI && op2 == Op_ConI &&
 344       in1->in(2)->Opcode() == Op_ConI ) {
 345     jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
 346     jint y = phase->type( in2 )->is_int()->get_con();
 347 
 348     if( z < 5 && -5 < y && y < 0 ) {
 349       const Type *t_in11 = phase->type(in1->in(1));
 350       if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
 351         Node *a = phase->transform( new AddINode( in1->in(1), phase->intcon(y<<z) ) );
 352         return new URShiftINode( a, in1->in(2) );
 353       }
 354     }
 355   }
 356 
 357   Node* muladd = convert_add_to_muladd(phase, in1, in2);
 358   if (muladd != NULL) {
 359     return muladd;
 360   }
 361 
 362   return AddNode::Ideal(phase, can_reshape);
 363 }
 364 
 365 
 366 //------------------------------Identity---------------------------------------
 367 // Fold (x-y)+y  OR  y+(x-y)  into  x
 368 Node* AddINode::Identity(PhaseGVN* phase) {
 369   if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {
 370     return in(1)->in(1);
 371   }
 372   else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {
 373     return in(2)->in(1);
 374   }
 375   return AddNode::Identity(phase);
 376 }
 377 
 378 
 379 //------------------------------add_ring---------------------------------------
 380 // Supplied function returns the sum of the inputs.  Guaranteed never
 381 // to be passed a TOP or BOTTOM type, these are filtered out by
 382 // pre-check.
 383 const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
 384   const TypeInt *r0 = t0->is_int(); // Handy access
 385   const TypeInt *r1 = t1->is_int();
 386   int lo = java_add(r0->_lo, r1->_lo);
 387   int hi = java_add(r0->_hi, r1->_hi);
 388   if( !(r0->is_con() && r1->is_con()) ) {
 389     // Not both constants, compute approximate result
 390     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 391       lo = min_jint; hi = max_jint; // Underflow on the low side
 392     }
 393     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 394       lo = min_jint; hi = max_jint; // Overflow on the high side
 395     }
 396     if( lo > hi ) {               // Handle overflow
 397       lo = min_jint; hi = max_jint;
 398     }
 399   } else {
 400     // both constants, compute precise result using 'lo' and 'hi'
 401     // Semantics define overflow and underflow for integer addition
 402     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 403   }
 404   return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 405 }
 406 
 407 
 408 //=============================================================================
 409 //------------------------------Idealize---------------------------------------
 410 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 411   Node* in1 = in(1);
 412   Node* in2 = in(2);
 413   int op1 = in1->Opcode();
 414   int op2 = in2->Opcode();
 415   // Fold (con1-x)+con2 into (con1+con2)-x
 416   if ( op1 == Op_AddL && op2 == Op_SubL ) {
 417     // Swap edges to try optimizations below
 418     in1 = in2;
 419     in2 = in(1);
 420     op1 = op2;
 421     op2 = in2->Opcode();
 422   }
 423   // Fold (con1-x)+con2 into (con1+con2)-x
 424   if( op1 == Op_SubL ) {
 425     const Type *t_sub1 = phase->type( in1->in(1) );
 426     const Type *t_2    = phase->type( in2        );
 427     if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
 428       return new SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
 429     // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
 430     if( op2 == Op_SubL ) {
 431       // Check for dead cycle: d = (a-b)+(c-d)
 432       assert( in1->in(2) != this && in2->in(2) != this,
 433               "dead loop in AddLNode::Ideal" );
 434       Node *sub  = new SubLNode(NULL, NULL);
 435       sub->init_req(1, phase->transform(new AddLNode(in1->in(1), in2->in(1) ) ));
 436       sub->init_req(2, phase->transform(new AddLNode(in1->in(2), in2->in(2) ) ));
 437       return sub;
 438     }
 439     // Convert "(a-b)+(b+c)" into "(a+c)"
 440     if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
 441       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
 442       return new AddLNode(in1->in(1), in2->in(2));
 443     }
 444     // Convert "(a-b)+(c+b)" into "(a+c)"
 445     if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
 446       assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
 447       return new AddLNode(in1->in(1), in2->in(1));
 448     }
 449     // Convert "(a-b)+(b-c)" into "(a-c)"
 450     if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
 451       assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
 452       return new SubLNode(in1->in(1), in2->in(2));
 453     }
 454     // Convert "(a-b)+(c-a)" into "(c-b)"
 455     if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {
 456       assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
 457       return new SubLNode(in2->in(1), in1->in(2));
 458     }
 459   }
 460 
 461   // Convert "x+(0-y)" into "(x-y)"
 462   if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
 463     return new SubLNode( in1, in2->in(2) );
 464 
 465   // Convert "(0-y)+x" into "(x-y)"
 466   if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )
 467     return new SubLNode( in2, in1->in(2) );
 468 
 469   // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
 470   // into "(X<<1)+Y" and let shift-folding happen.
 471   if( op2 == Op_AddL &&
 472       in2->in(1) == in1 &&
 473       op1 != Op_ConL &&
 474       0 ) {
 475     Node *shift = phase->transform(new LShiftLNode(in1,phase->intcon(1)));
 476     return new AddLNode(shift,in2->in(2));
 477   }
 478 
 479   return AddNode::Ideal(phase, can_reshape);
 480 }
 481 
 482 
 483 //------------------------------Identity---------------------------------------
 484 // Fold (x-y)+y  OR  y+(x-y)  into  x
 485 Node* AddLNode::Identity(PhaseGVN* phase) {
 486   if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {
 487     return in(1)->in(1);
 488   }
 489   else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {
 490     return in(2)->in(1);
 491   }
 492   return AddNode::Identity(phase);
 493 }
 494 
 495 
 496 //------------------------------add_ring---------------------------------------
 497 // Supplied function returns the sum of the inputs.  Guaranteed never
 498 // to be passed a TOP or BOTTOM type, these are filtered out by
 499 // pre-check.
 500 const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
 501   const TypeLong *r0 = t0->is_long(); // Handy access
 502   const TypeLong *r1 = t1->is_long();
 503   jlong lo = java_add(r0->_lo, r1->_lo);
 504   jlong hi = java_add(r0->_hi, r1->_hi);
 505   if( !(r0->is_con() && r1->is_con()) ) {
 506     // Not both constants, compute approximate result
 507     if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
 508       lo =min_jlong; hi = max_jlong; // Underflow on the low side
 509     }
 510     if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
 511       lo = min_jlong; hi = max_jlong; // Overflow on the high side
 512     }
 513     if( lo > hi ) {               // Handle overflow
 514       lo = min_jlong; hi = max_jlong;
 515     }
 516   } else {
 517     // both constants, compute precise result using 'lo' and 'hi'
 518     // Semantics define overflow and underflow for integer addition
 519     // as expected.  In particular: 0x80000000 + 0x80000000 --> 0x0
 520   }
 521   return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
 522 }
 523 
 524 
 525 //=============================================================================
 526 //------------------------------add_of_identity--------------------------------
 527 // Check for addition of the identity
 528 const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 529   // x ADD 0  should return x unless 'x' is a -zero
 530   //
 531   // const Type *zero = add_id();     // The additive identity
 532   // jfloat f1 = t1->getf();
 533   // jfloat f2 = t2->getf();
 534   //
 535   // if( t1->higher_equal( zero ) ) return t2;
 536   // if( t2->higher_equal( zero ) ) return t1;
 537 
 538   return NULL;
 539 }
 540 
 541 //------------------------------add_ring---------------------------------------
 542 // Supplied function returns the sum of the inputs.
 543 // This also type-checks the inputs for sanity.  Guaranteed never to
 544 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 545 const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
 546   // We must be adding 2 float constants.
 547   return TypeF::make( t0->getf() + t1->getf() );
 548 }
 549 
 550 //------------------------------Ideal------------------------------------------
 551 Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 552   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 553     return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
 554   }
 555 
 556   // Floating point additions are not associative because of boundary conditions (infinity)
 557   return commute(this,
 558                  phase->type( in(1) )->singleton(),
 559                  phase->type( in(2) )->singleton() ) ? this : NULL;
 560 }
 561 
 562 
 563 //=============================================================================
 564 //------------------------------add_of_identity--------------------------------
 565 // Check for addition of the identity
 566 const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
 567   // x ADD 0  should return x unless 'x' is a -zero
 568   //
 569   // const Type *zero = add_id();     // The additive identity
 570   // jfloat f1 = t1->getf();
 571   // jfloat f2 = t2->getf();
 572   //
 573   // if( t1->higher_equal( zero ) ) return t2;
 574   // if( t2->higher_equal( zero ) ) return t1;
 575 
 576   return NULL;
 577 }
 578 //------------------------------add_ring---------------------------------------
 579 // Supplied function returns the sum of the inputs.
 580 // This also type-checks the inputs for sanity.  Guaranteed never to
 581 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 582 const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
 583   // We must be adding 2 double constants.
 584   return TypeD::make( t0->getd() + t1->getd() );
 585 }
 586 
 587 //------------------------------Ideal------------------------------------------
 588 Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 589   if( IdealizedNumerics && !phase->C->method()->is_strict() ) {
 590     return AddNode::Ideal(phase, can_reshape); // commutative and associative transforms
 591   }
 592 
 593   // Floating point additions are not associative because of boundary conditions (infinity)
 594   return commute(this,
 595                  phase->type( in(1) )->singleton(),
 596                  phase->type( in(2) )->singleton() ) ? this : NULL;
 597 }
 598 
 599 
 600 //=============================================================================
 601 //------------------------------Identity---------------------------------------
 602 // If one input is a constant 0, return the other input.
 603 Node* AddPNode::Identity(PhaseGVN* phase) {
 604   return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
 605 }
 606 
 607 //------------------------------Idealize---------------------------------------
 608 Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 609   // Bail out if dead inputs
 610   if( phase->type( in(Address) ) == Type::TOP ) return NULL;
 611 
 612   // If the left input is an add of a constant, flatten the expression tree.
 613   const Node *n = in(Address);
 614   if (n->is_AddP() && n->in(Base) == in(Base)) {
 615     const AddPNode *addp = n->as_AddP(); // Left input is an AddP
 616     assert( !addp->in(Address)->is_AddP() ||
 617              addp->in(Address)->as_AddP() != addp,
 618             "dead loop in AddPNode::Ideal" );
 619     // Type of left input's right input
 620     const Type *t = phase->type( addp->in(Offset) );
 621     if( t == Type::TOP ) return NULL;
 622     const TypeX *t12 = t->is_intptr_t();
 623     if( t12->is_con() ) {       // Left input is an add of a constant?
 624       // If the right input is a constant, combine constants
 625       const Type *temp_t2 = phase->type( in(Offset) );
 626       if( temp_t2 == Type::TOP ) return NULL;
 627       const TypeX *t2 = temp_t2->is_intptr_t();
 628       Node* address;
 629       Node* offset;
 630       if( t2->is_con() ) {
 631         // The Add of the flattened expression
 632         address = addp->in(Address);
 633         offset  = phase->MakeConX(t2->get_con() + t12->get_con());
 634       } else {
 635         // Else move the constant to the right.  ((A+con)+B) into ((A+B)+con)
 636         address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
 637         offset  = addp->in(Offset);
 638       }
 639       PhaseIterGVN *igvn = phase->is_IterGVN();
 640       if( igvn ) {
 641         set_req_X(Address,address,igvn);
 642         set_req_X(Offset,offset,igvn);
 643       } else {
 644         set_req(Address,address);
 645         set_req(Offset,offset);
 646       }
 647       return this;
 648     }
 649   }
 650 
 651   // Raw pointers?
 652   if( in(Base)->bottom_type() == Type::TOP ) {
 653     // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
 654     if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
 655       Node* offset = in(Offset);
 656       return new CastX2PNode(offset);
 657     }
 658   }
 659 
 660   // If the right is an add of a constant, push the offset down.
 661   // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
 662   // The idea is to merge array_base+scaled_index groups together,
 663   // and only have different constant offsets from the same base.
 664   const Node *add = in(Offset);
 665   if( add->Opcode() == Op_AddX && add->in(1) != add ) {
 666     const Type *t22 = phase->type( add->in(2) );
 667     if( t22->singleton() && (t22 != Type::TOP) ) {  // Right input is an add of a constant?
 668       set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
 669       set_req(Offset, add->in(2));
 670       PhaseIterGVN *igvn = phase->is_IterGVN();
 671       if (add->outcnt() == 0 && igvn) {
 672         // add disconnected.
 673         igvn->_worklist.push((Node*)add);
 674       }
 675       return this;              // Made progress
 676     }
 677   }
 678 
 679   return NULL;                  // No progress
 680 }
 681 
 682 //------------------------------bottom_type------------------------------------
 683 // Bottom-type is the pointer-type with unknown offset.
 684 const Type *AddPNode::bottom_type() const {
 685   if (in(Address) == NULL)  return TypePtr::BOTTOM;
 686   const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
 687   if( !tp ) return Type::TOP;   // TOP input means TOP output
 688   assert( in(Offset)->Opcode() != Op_ConP, "" );
 689   const Type *t = in(Offset)->bottom_type();
 690   if( t == Type::TOP )
 691     return tp->add_offset(Type::OffsetTop);
 692   const TypeX *tx = t->is_intptr_t();
 693   intptr_t txoffset = Type::OffsetBot;
 694   if (tx->is_con()) {   // Left input is an add of a constant?
 695     txoffset = tx->get_con();
 696   }
 697   return tp->add_offset(txoffset);
 698 }
 699 
 700 //------------------------------Value------------------------------------------
 701 const Type* AddPNode::Value(PhaseGVN* phase) const {
 702   // Either input is TOP ==> the result is TOP
 703   const Type *t1 = phase->type( in(Address) );
 704   const Type *t2 = phase->type( in(Offset) );
 705   if( t1 == Type::TOP ) return Type::TOP;
 706   if( t2 == Type::TOP ) return Type::TOP;
 707 
 708   // Left input is a pointer
 709   const TypePtr *p1 = t1->isa_ptr();
 710   // Right input is an int
 711   const TypeX *p2 = t2->is_intptr_t();
 712   // Add 'em
 713   intptr_t p2offset = Type::OffsetBot;
 714   if (p2->is_con()) {   // Left input is an add of a constant?
 715     p2offset = p2->get_con();
 716   }
 717   return p1->add_offset(p2offset);
 718 }
 719 
 720 //------------------------Ideal_base_and_offset--------------------------------
 721 // Split an oop pointer into a base and offset.
 722 // (The offset might be Type::OffsetBot in the case of an array.)
 723 // Return the base, or NULL if failure.
 724 Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseTransform* phase,
 725                                       // second return value:
 726                                       intptr_t& offset) {
 727   if (ptr->is_AddP()) {
 728     Node* base = ptr->in(AddPNode::Base);
 729     Node* addr = ptr->in(AddPNode::Address);
 730     Node* offs = ptr->in(AddPNode::Offset);
 731     if (base == addr || base->is_top()) {
 732       offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
 733       if (offset != Type::OffsetBot) {
 734         return addr;
 735       }
 736     }
 737   }
 738   offset = Type::OffsetBot;
 739   return NULL;
 740 }
 741 
 742 //------------------------------unpack_offsets----------------------------------
 743 // Collect the AddP offset values into the elements array, giving up
 744 // if there are more than length.
 745 int AddPNode::unpack_offsets(Node* elements[], int length) {
 746   int count = 0;
 747   Node* addr = this;
 748   Node* base = addr->in(AddPNode::Base);
 749   while (addr->is_AddP()) {
 750     if (addr->in(AddPNode::Base) != base) {
 751       // give up
 752       return -1;
 753     }
 754     elements[count++] = addr->in(AddPNode::Offset);
 755     if (count == length) {
 756       // give up
 757       return -1;
 758     }
 759     addr = addr->in(AddPNode::Address);
 760   }
 761   if (addr != base) {
 762     return -1;
 763   }
 764   return count;
 765 }
 766 
 767 //------------------------------match_edge-------------------------------------
 768 // Do we Match on this edge index or not?  Do not match base pointer edge
 769 uint AddPNode::match_edge(uint idx) const {
 770   return idx > Base;
 771 }
 772 
 773 //=============================================================================
 774 //------------------------------Identity---------------------------------------
 775 Node* OrINode::Identity(PhaseGVN* phase) {
 776   // x | x => x
 777   if (phase->eqv(in(1), in(2))) {
 778     return in(1);
 779   }
 780 
 781   return AddNode::Identity(phase);
 782 }
 783 
 784 //------------------------------add_ring---------------------------------------
 785 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 786 // the logical operations the ring's ADD is really a logical OR function.
 787 // This also type-checks the inputs for sanity.  Guaranteed never to
 788 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 789 const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
 790   const TypeInt *r0 = t0->is_int(); // Handy access
 791   const TypeInt *r1 = t1->is_int();
 792 
 793   // If both args are bool, can figure out better types
 794   if ( r0 == TypeInt::BOOL ) {
 795     if ( r1 == TypeInt::ONE) {
 796       return TypeInt::ONE;
 797     } else if ( r1 == TypeInt::BOOL ) {
 798       return TypeInt::BOOL;
 799     }
 800   } else if ( r0 == TypeInt::ONE ) {
 801     if ( r1 == TypeInt::BOOL ) {
 802       return TypeInt::ONE;
 803     }
 804   }
 805 
 806   // If either input is not a constant, just return all integers.
 807   if( !r0->is_con() || !r1->is_con() )
 808     return TypeInt::INT;        // Any integer, but still no symbols.
 809 
 810   // Otherwise just OR them bits.
 811   return TypeInt::make( r0->get_con() | r1->get_con() );
 812 }
 813 
 814 //=============================================================================
 815 //------------------------------Identity---------------------------------------
 816 Node* OrLNode::Identity(PhaseGVN* phase) {
 817   // x | x => x
 818   if (phase->eqv(in(1), in(2))) {
 819     return in(1);
 820   }
 821 
 822   return AddNode::Identity(phase);
 823 }
 824 
 825 //------------------------------add_ring---------------------------------------
 826 const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
 827   const TypeLong *r0 = t0->is_long(); // Handy access
 828   const TypeLong *r1 = t1->is_long();
 829 
 830   // If either input is not a constant, just return all integers.
 831   if( !r0->is_con() || !r1->is_con() )
 832     return TypeLong::LONG;      // Any integer, but still no symbols.
 833 
 834   // Otherwise just OR them bits.
 835   return TypeLong::make( r0->get_con() | r1->get_con() );
 836 }
 837 
 838 //=============================================================================
 839 //------------------------------add_ring---------------------------------------
 840 // Supplied function returns the sum of the inputs IN THE CURRENT RING.  For
 841 // the logical operations the ring's ADD is really a logical OR function.
 842 // This also type-checks the inputs for sanity.  Guaranteed never to
 843 // be passed a TOP or BOTTOM type, these are filtered out by pre-check.
 844 const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
 845   const TypeInt *r0 = t0->is_int(); // Handy access
 846   const TypeInt *r1 = t1->is_int();
 847 
 848   // Complementing a boolean?
 849   if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
 850                                || r1 == TypeInt::BOOL))
 851     return TypeInt::BOOL;
 852 
 853   if( !r0->is_con() || !r1->is_con() ) // Not constants
 854     return TypeInt::INT;        // Any integer, but still no symbols.
 855 
 856   // Otherwise just XOR them bits.
 857   return TypeInt::make( r0->get_con() ^ r1->get_con() );
 858 }
 859 
 860 //=============================================================================
 861 //------------------------------add_ring---------------------------------------
 862 const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
 863   const TypeLong *r0 = t0->is_long(); // Handy access
 864   const TypeLong *r1 = t1->is_long();
 865 
 866   // If either input is not a constant, just return all integers.
 867   if( !r0->is_con() || !r1->is_con() )
 868     return TypeLong::LONG;      // Any integer, but still no symbols.
 869 
 870   // Otherwise just OR them bits.
 871   return TypeLong::make( r0->get_con() ^ r1->get_con() );
 872 }
 873 
 874 //=============================================================================
 875 //------------------------------add_ring---------------------------------------
 876 // Supplied function returns the sum of the inputs.
 877 const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
 878   const TypeInt *r0 = t0->is_int(); // Handy access
 879   const TypeInt *r1 = t1->is_int();
 880 
 881   // Otherwise just MAX them bits.
 882   return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
 883 }
 884 
 885 //=============================================================================
 886 //------------------------------Idealize---------------------------------------
 887 // MINs show up in range-check loop limit calculations.  Look for
 888 // "MIN2(x+c0,MIN2(y,x+c1))".  Pick the smaller constant: "MIN2(x+c0,y)"
 889 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 890   Node *progress = NULL;
 891   // Force a right-spline graph
 892   Node *l = in(1);
 893   Node *r = in(2);
 894   // Transform  MinI1( MinI2(a,b), c)  into  MinI1( a, MinI2(b,c) )
 895   // to force a right-spline graph for the rest of MinINode::Ideal().
 896   if( l->Opcode() == Op_MinI ) {
 897     assert( l != l->in(1), "dead loop in MinINode::Ideal" );
 898     r = phase->transform(new MinINode(l->in(2),r));
 899     l = l->in(1);
 900     set_req(1, l);
 901     set_req(2, r);
 902     return this;
 903   }
 904 
 905   // Get left input & constant
 906   Node *x = l;
 907   int x_off = 0;
 908   if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
 909       x->in(2)->is_Con() ) {
 910     const Type *t = x->in(2)->bottom_type();
 911     if( t == Type::TOP ) return NULL;  // No progress
 912     x_off = t->is_int()->get_con();
 913     x = x->in(1);
 914   }
 915 
 916   // Scan a right-spline-tree for MINs
 917   Node *y = r;
 918   int y_off = 0;
 919   // Check final part of MIN tree
 920   if( y->Opcode() == Op_AddI && // Check for "y+c1" and collect constant
 921       y->in(2)->is_Con() ) {
 922     const Type *t = y->in(2)->bottom_type();
 923     if( t == Type::TOP ) return NULL;  // No progress
 924     y_off = t->is_int()->get_con();
 925     y = y->in(1);
 926   }
 927   if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
 928     swap_edges(1, 2);
 929     return this;
 930   }
 931 
 932 
 933   if( r->Opcode() == Op_MinI ) {
 934     assert( r != r->in(2), "dead loop in MinINode::Ideal" );
 935     y = r->in(1);
 936     // Check final part of MIN tree
 937     if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
 938         y->in(2)->is_Con() ) {
 939       const Type *t = y->in(2)->bottom_type();
 940       if( t == Type::TOP ) return NULL;  // No progress
 941       y_off = t->is_int()->get_con();
 942       y = y->in(1);
 943     }
 944 
 945     if( x->_idx > y->_idx )
 946       return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
 947 
 948     // See if covers: MIN2(x+c0,MIN2(y+c1,z))
 949     if( !phase->eqv(x,y) ) return NULL;
 950     // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into
 951     // MIN2(x+c0 or x+c1 which less, z).
 952     return new MinINode(phase->transform(new AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2));
 953   } else {
 954     // See if covers: MIN2(x+c0,y+c1)
 955     if( !phase->eqv(x,y) ) return NULL;
 956     // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less.
 957     return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
 958   }
 959 
 960 }
 961 
 962 //------------------------------add_ring---------------------------------------
 963 // Supplied function returns the sum of the inputs.
 964 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
 965   const TypeInt *r0 = t0->is_int(); // Handy access
 966   const TypeInt *r1 = t1->is_int();
 967 
 968   // Otherwise just MIN them bits.
 969   return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
 970 }