1 /*
   2  * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 // Portions of code courtesy of Clifford Click
  26 
  27 // Optimization - Graph Style
  28 
  29 #include "incls/_precompiled.incl"
  30 #include "incls/_divnode.cpp.incl"
  31 #include <math.h>
  32 
  33 //----------------------magic_int_divide_constants-----------------------------
  34 // Compute magic multiplier and shift constant for converting a 32 bit divide
  35 // by constant into a multiply/shift/add series. Return false if calculations
  36 // fail.
  37 //
  38 // Borrowed almost verbatum from Hacker's Delight by Henry S. Warren, Jr. with
  39 // minor type name and parameter changes.
  40 static bool magic_int_divide_constants(jint d, jint &M, jint &s) {
  41   int32_t p;
  42   uint32_t ad, anc, delta, q1, r1, q2, r2, t;
  43   const uint32_t two31 = 0x80000000L;     // 2**31.
  44 
  45   ad = ABS(d);
  46   if (d == 0 || d == 1) return false;
  47   t = two31 + ((uint32_t)d >> 31);
  48   anc = t - 1 - t%ad;     // Absolute value of nc.
  49   p = 31;                 // Init. p.
  50   q1 = two31/anc;         // Init. q1 = 2**p/|nc|.
  51   r1 = two31 - q1*anc;    // Init. r1 = rem(2**p, |nc|).
  52   q2 = two31/ad;          // Init. q2 = 2**p/|d|.
  53   r2 = two31 - q2*ad;     // Init. r2 = rem(2**p, |d|).
  54   do {
  55     p = p + 1;
  56     q1 = 2*q1;            // Update q1 = 2**p/|nc|.
  57     r1 = 2*r1;            // Update r1 = rem(2**p, |nc|).
  58     if (r1 >= anc) {      // (Must be an unsigned
  59       q1 = q1 + 1;        // comparison here).
  60       r1 = r1 - anc;
  61     }
  62     q2 = 2*q2;            // Update q2 = 2**p/|d|.
  63     r2 = 2*r2;            // Update r2 = rem(2**p, |d|).
  64     if (r2 >= ad) {       // (Must be an unsigned
  65       q2 = q2 + 1;        // comparison here).
  66       r2 = r2 - ad;
  67     }
  68     delta = ad - r2;
  69   } while (q1 < delta || (q1 == delta && r1 == 0));
  70 
  71   M = q2 + 1;
  72   if (d < 0) M = -M;      // Magic number and
  73   s = p - 32;             // shift amount to return.
  74 
  75   return true;
  76 }
  77 
  78 //--------------------------transform_int_divide-------------------------------
  79 // Convert a division by constant divisor into an alternate Ideal graph.
  80 // Return NULL if no transformation occurs.
  81 static Node *transform_int_divide( PhaseGVN *phase, Node *dividend, jint divisor ) {
  82 
  83   // Check for invalid divisors
  84   assert( divisor != 0 && divisor != min_jint,
  85           "bad divisor for transforming to long multiply" );
  86 
  87   bool d_pos = divisor >= 0;
  88   jint d = d_pos ? divisor : -divisor;
  89   const int N = 32;
  90 
  91   // Result
  92   Node *q = NULL;
  93 
  94   if (d == 1) {
  95     // division by +/- 1
  96     if (!d_pos) {
  97       // Just negate the value
  98       q = new (phase->C, 3) SubINode(phase->intcon(0), dividend);
  99     }
 100   } else if ( is_power_of_2(d) ) {
 101     // division by +/- a power of 2
 102 
 103     // See if we can simply do a shift without rounding
 104     bool needs_rounding = true;
 105     const Type *dt = phase->type(dividend);
 106     const TypeInt *dti = dt->isa_int();
 107     if (dti && dti->_lo >= 0) {
 108       // we don't need to round a positive dividend
 109       needs_rounding = false;
 110     } else if( dividend->Opcode() == Op_AndI ) {
 111       // An AND mask of sufficient size clears the low bits and
 112       // I can avoid rounding.
 113       const TypeInt *andconi = phase->type( dividend->in(2) )->isa_int();
 114       if( andconi && andconi->is_con(-d) ) {
 115         dividend = dividend->in(1);
 116         needs_rounding = false;
 117       }
 118     }
 119 
 120     // Add rounding to the shift to handle the sign bit
 121     int l = log2_intptr(d-1)+1;
 122     if (needs_rounding) {
 123       // Divide-by-power-of-2 can be made into a shift, but you have to do
 124       // more math for the rounding.  You need to add 0 for positive
 125       // numbers, and "i-1" for negative numbers.  Example: i=4, so the
 126       // shift is by 2.  You need to add 3 to negative dividends and 0 to
 127       // positive ones.  So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
 128       // (-2+3)>>2 becomes 0, etc.
 129 
 130       // Compute 0 or -1, based on sign bit
 131       Node *sign = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N - 1)));
 132       // Mask sign bit to the low sign bits
 133       Node *round = phase->transform(new (phase->C, 3) URShiftINode(sign, phase->intcon(N - l)));
 134       // Round up before shifting
 135       dividend = phase->transform(new (phase->C, 3) AddINode(dividend, round));
 136     }
 137 
 138     // Shift for division
 139     q = new (phase->C, 3) RShiftINode(dividend, phase->intcon(l));
 140 
 141     if (!d_pos) {
 142       q = new (phase->C, 3) SubINode(phase->intcon(0), phase->transform(q));
 143     }
 144   } else {
 145     // Attempt the jint constant divide -> multiply transform found in
 146     //   "Division by Invariant Integers using Multiplication"
 147     //     by Granlund and Montgomery
 148     // See also "Hacker's Delight", chapter 10 by Warren.
 149 
 150     jint magic_const;
 151     jint shift_const;
 152     if (magic_int_divide_constants(d, magic_const, shift_const)) {
 153       Node *magic = phase->longcon(magic_const);
 154       Node *dividend_long = phase->transform(new (phase->C, 2) ConvI2LNode(dividend));
 155 
 156       // Compute the high half of the dividend x magic multiplication
 157       Node *mul_hi = phase->transform(new (phase->C, 3) MulLNode(dividend_long, magic));
 158 
 159       if (magic_const < 0) {
 160         mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(N)));
 161         mul_hi = phase->transform(new (phase->C, 2) ConvL2INode(mul_hi));
 162 
 163         // The magic multiplier is too large for a 32 bit constant. We've adjusted
 164         // it down by 2^32, but have to add 1 dividend back in after the multiplication.
 165         // This handles the "overflow" case described by Granlund and Montgomery.
 166         mul_hi = phase->transform(new (phase->C, 3) AddINode(dividend, mul_hi));
 167 
 168         // Shift over the (adjusted) mulhi
 169         if (shift_const != 0) {
 170           mul_hi = phase->transform(new (phase->C, 3) RShiftINode(mul_hi, phase->intcon(shift_const)));
 171         }
 172       } else {
 173         // No add is required, we can merge the shifts together.
 174         mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
 175         mul_hi = phase->transform(new (phase->C, 2) ConvL2INode(mul_hi));
 176       }
 177 
 178       // Get a 0 or -1 from the sign of the dividend.
 179       Node *addend0 = mul_hi;
 180       Node *addend1 = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N-1)));
 181 
 182       // If the divisor is negative, swap the order of the input addends;
 183       // this has the effect of negating the quotient.
 184       if (!d_pos) {
 185         Node *temp = addend0; addend0 = addend1; addend1 = temp;
 186       }
 187 
 188       // Adjust the final quotient by subtracting -1 (adding 1)
 189       // from the mul_hi.
 190       q = new (phase->C, 3) SubINode(addend0, addend1);
 191     }
 192   }
 193 
 194   return q;
 195 }
 196 
 197 //---------------------magic_long_divide_constants-----------------------------
 198 // Compute magic multiplier and shift constant for converting a 64 bit divide
 199 // by constant into a multiply/shift/add series. Return false if calculations
 200 // fail.
 201 //
 202 // Borrowed almost verbatum from Hacker's Delight by Henry S. Warren, Jr. with
 203 // minor type name and parameter changes.  Adjusted to 64 bit word width.
 204 static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) {
 205   int64_t p;
 206   uint64_t ad, anc, delta, q1, r1, q2, r2, t;
 207   const uint64_t two63 = 0x8000000000000000LL;     // 2**63.
 208 
 209   ad = ABS(d);
 210   if (d == 0 || d == 1) return false;
 211   t = two63 + ((uint64_t)d >> 63);
 212   anc = t - 1 - t%ad;     // Absolute value of nc.
 213   p = 63;                 // Init. p.
 214   q1 = two63/anc;         // Init. q1 = 2**p/|nc|.
 215   r1 = two63 - q1*anc;    // Init. r1 = rem(2**p, |nc|).
 216   q2 = two63/ad;          // Init. q2 = 2**p/|d|.
 217   r2 = two63 - q2*ad;     // Init. r2 = rem(2**p, |d|).
 218   do {
 219     p = p + 1;
 220     q1 = 2*q1;            // Update q1 = 2**p/|nc|.
 221     r1 = 2*r1;            // Update r1 = rem(2**p, |nc|).
 222     if (r1 >= anc) {      // (Must be an unsigned
 223       q1 = q1 + 1;        // comparison here).
 224       r1 = r1 - anc;
 225     }
 226     q2 = 2*q2;            // Update q2 = 2**p/|d|.
 227     r2 = 2*r2;            // Update r2 = rem(2**p, |d|).
 228     if (r2 >= ad) {       // (Must be an unsigned
 229       q2 = q2 + 1;        // comparison here).
 230       r2 = r2 - ad;
 231     }
 232     delta = ad - r2;
 233   } while (q1 < delta || (q1 == delta && r1 == 0));
 234 
 235   M = q2 + 1;
 236   if (d < 0) M = -M;      // Magic number and
 237   s = p - 64;             // shift amount to return.
 238 
 239   return true;
 240 }
 241 
 242 //---------------------long_by_long_mulhi--------------------------------------
 243 // Generate ideal node graph for upper half of a 64 bit x 64 bit multiplication
 244 static Node *long_by_long_mulhi( PhaseGVN *phase, Node *dividend, jlong magic_const) {
 245   // If the architecture supports a 64x64 mulhi, there is
 246   // no need to synthesize it in ideal nodes.
 247   if (Matcher::has_match_rule(Op_MulHiL)) {
 248     Node *v = phase->longcon(magic_const);
 249     return new (phase->C, 3) MulHiLNode(dividend, v);
 250   }
 251 
 252   const int N = 64;
 253 
 254   Node *u_hi = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N / 2)));
 255   Node *u_lo = phase->transform(new (phase->C, 3) AndLNode(dividend, phase->longcon(0xFFFFFFFF)));
 256 
 257   Node *v_hi = phase->longcon(magic_const >> N/2);
 258   Node *v_lo = phase->longcon(magic_const & 0XFFFFFFFF);
 259 
 260   Node *hihi_product = phase->transform(new (phase->C, 3) MulLNode(u_hi, v_hi));
 261   Node *hilo_product = phase->transform(new (phase->C, 3) MulLNode(u_hi, v_lo));
 262   Node *lohi_product = phase->transform(new (phase->C, 3) MulLNode(u_lo, v_hi));
 263   Node *lolo_product = phase->transform(new (phase->C, 3) MulLNode(u_lo, v_lo));
 264 
 265   Node *t1 = phase->transform(new (phase->C, 3) URShiftLNode(lolo_product, phase->intcon(N / 2)));
 266   Node *t2 = phase->transform(new (phase->C, 3) AddLNode(hilo_product, t1));
 267 
 268   // Construct both t3 and t4 before transforming so t2 doesn't go dead
 269   // prematurely.
 270   Node *t3 = new (phase->C, 3) RShiftLNode(t2, phase->intcon(N / 2));
 271   Node *t4 = new (phase->C, 3) AndLNode(t2, phase->longcon(0xFFFFFFFF));
 272   t3 = phase->transform(t3);
 273   t4 = phase->transform(t4);
 274 
 275   Node *t5 = phase->transform(new (phase->C, 3) AddLNode(t4, lohi_product));
 276   Node *t6 = phase->transform(new (phase->C, 3) RShiftLNode(t5, phase->intcon(N / 2)));
 277   Node *t7 = phase->transform(new (phase->C, 3) AddLNode(t3, hihi_product));
 278 
 279   return new (phase->C, 3) AddLNode(t7, t6);
 280 }
 281 
 282 
 283 //--------------------------transform_long_divide------------------------------
 284 // Convert a division by constant divisor into an alternate Ideal graph.
 285 // Return NULL if no transformation occurs.
 286 static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) {
 287   // Check for invalid divisors
 288   assert( divisor != 0L && divisor != min_jlong,
 289           "bad divisor for transforming to long multiply" );
 290 
 291   bool d_pos = divisor >= 0;
 292   jlong d = d_pos ? divisor : -divisor;
 293   const int N = 64;
 294 
 295   // Result
 296   Node *q = NULL;
 297 
 298   if (d == 1) {
 299     // division by +/- 1
 300     if (!d_pos) {
 301       // Just negate the value
 302       q = new (phase->C, 3) SubLNode(phase->longcon(0), dividend);
 303     }
 304   } else if ( is_power_of_2_long(d) ) {
 305 
 306     // division by +/- a power of 2
 307 
 308     // See if we can simply do a shift without rounding
 309     bool needs_rounding = true;
 310     const Type *dt = phase->type(dividend);
 311     const TypeLong *dtl = dt->isa_long();
 312 
 313     if (dtl && dtl->_lo > 0) {
 314       // we don't need to round a positive dividend
 315       needs_rounding = false;
 316     } else if( dividend->Opcode() == Op_AndL ) {
 317       // An AND mask of sufficient size clears the low bits and
 318       // I can avoid rounding.
 319       const TypeLong *andconl = phase->type( dividend->in(2) )->isa_long();
 320       if( andconl && andconl->is_con(-d)) {
 321         dividend = dividend->in(1);
 322         needs_rounding = false;
 323       }
 324     }
 325 
 326     // Add rounding to the shift to handle the sign bit
 327     int l = log2_long(d-1)+1;
 328     if (needs_rounding) {
 329       // Divide-by-power-of-2 can be made into a shift, but you have to do
 330       // more math for the rounding.  You need to add 0 for positive
 331       // numbers, and "i-1" for negative numbers.  Example: i=4, so the
 332       // shift is by 2.  You need to add 3 to negative dividends and 0 to
 333       // positive ones.  So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
 334       // (-2+3)>>2 becomes 0, etc.
 335 
 336       // Compute 0 or -1, based on sign bit
 337       Node *sign = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N - 1)));
 338       // Mask sign bit to the low sign bits
 339       Node *round = phase->transform(new (phase->C, 3) URShiftLNode(sign, phase->intcon(N - l)));
 340       // Round up before shifting
 341       dividend = phase->transform(new (phase->C, 3) AddLNode(dividend, round));
 342     }
 343 
 344     // Shift for division
 345     q = new (phase->C, 3) RShiftLNode(dividend, phase->intcon(l));
 346 
 347     if (!d_pos) {
 348       q = new (phase->C, 3) SubLNode(phase->longcon(0), phase->transform(q));
 349     }
 350   } else {
 351     // Attempt the jlong constant divide -> multiply transform found in
 352     //   "Division by Invariant Integers using Multiplication"
 353     //     by Granlund and Montgomery
 354     // See also "Hacker's Delight", chapter 10 by Warren.
 355 
 356     jlong magic_const;
 357     jint shift_const;
 358     if (magic_long_divide_constants(d, magic_const, shift_const)) {
 359       // Compute the high half of the dividend x magic multiplication
 360       Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const));
 361 
 362       // The high half of the 128-bit multiply is computed.
 363       if (magic_const < 0) {
 364         // The magic multiplier is too large for a 64 bit constant. We've adjusted
 365         // it down by 2^64, but have to add 1 dividend back in after the multiplication.
 366         // This handles the "overflow" case described by Granlund and Montgomery.
 367         mul_hi = phase->transform(new (phase->C, 3) AddLNode(dividend, mul_hi));
 368       }
 369 
 370       // Shift over the (adjusted) mulhi
 371       if (shift_const != 0) {
 372         mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(shift_const)));
 373       }
 374 
 375       // Get a 0 or -1 from the sign of the dividend.
 376       Node *addend0 = mul_hi;
 377       Node *addend1 = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N-1)));
 378 
 379       // If the divisor is negative, swap the order of the input addends;
 380       // this has the effect of negating the quotient.
 381       if (!d_pos) {
 382         Node *temp = addend0; addend0 = addend1; addend1 = temp;
 383       }
 384 
 385       // Adjust the final quotient by subtracting -1 (adding 1)
 386       // from the mul_hi.
 387       q = new (phase->C, 3) SubLNode(addend0, addend1);
 388     }
 389   }
 390 
 391   return q;
 392 }
 393 
 394 //=============================================================================
 395 //------------------------------Identity---------------------------------------
 396 // If the divisor is 1, we are an identity on the dividend.
 397 Node *DivINode::Identity( PhaseTransform *phase ) {
 398   return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
 399 }
 400 
 401 //------------------------------Idealize---------------------------------------
 402 // Divides can be changed to multiplies and/or shifts
 403 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 404   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 405   // Don't bother trying to transform a dead node
 406   if( in(0) && in(0)->is_top() )  return NULL;
 407 
 408   const Type *t = phase->type( in(2) );
 409   if( t == TypeInt::ONE )       // Identity?
 410     return NULL;                // Skip it
 411 
 412   const TypeInt *ti = t->isa_int();
 413   if( !ti ) return NULL;
 414   if( !ti->is_con() ) return NULL;
 415   jint i = ti->get_con();       // Get divisor
 416 
 417   if (i == 0) return NULL;      // Dividing by zero constant does not idealize
 418 
 419   set_req(0,NULL);              // Dividing by a not-zero constant; no faulting
 420 
 421   // Dividing by MININT does not optimize as a power-of-2 shift.
 422   if( i == min_jint ) return NULL;
 423 
 424   return transform_int_divide( phase, in(1), i );
 425 }
 426 
 427 //------------------------------Value------------------------------------------
 428 // A DivINode divides its inputs.  The third input is a Control input, used to
 429 // prevent hoisting the divide above an unsafe test.
 430 const Type *DivINode::Value( PhaseTransform *phase ) const {
 431   // Either input is TOP ==> the result is TOP
 432   const Type *t1 = phase->type( in(1) );
 433   const Type *t2 = phase->type( in(2) );
 434   if( t1 == Type::TOP ) return Type::TOP;
 435   if( t2 == Type::TOP ) return Type::TOP;
 436 
 437   // x/x == 1 since we always generate the dynamic divisor check for 0.
 438   if( phase->eqv( in(1), in(2) ) )
 439     return TypeInt::ONE;
 440 
 441   // Either input is BOTTOM ==> the result is the local BOTTOM
 442   const Type *bot = bottom_type();
 443   if( (t1 == bot) || (t2 == bot) ||
 444       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 445     return bot;
 446 
 447   // Divide the two numbers.  We approximate.
 448   // If divisor is a constant and not zero
 449   const TypeInt *i1 = t1->is_int();
 450   const TypeInt *i2 = t2->is_int();
 451   int widen = MAX2(i1->_widen, i2->_widen);
 452 
 453   if( i2->is_con() && i2->get_con() != 0 ) {
 454     int32 d = i2->get_con(); // Divisor
 455     jint lo, hi;
 456     if( d >= 0 ) {
 457       lo = i1->_lo/d;
 458       hi = i1->_hi/d;
 459     } else {
 460       if( d == -1 && i1->_lo == min_jint ) {
 461         // 'min_jint/-1' throws arithmetic exception during compilation
 462         lo = min_jint;
 463         // do not support holes, 'hi' must go to either min_jint or max_jint:
 464         // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint]
 465         hi = i1->_hi == min_jint ? min_jint : max_jint;
 466       } else {
 467         lo = i1->_hi/d;
 468         hi = i1->_lo/d;
 469       }
 470     }
 471     return TypeInt::make(lo, hi, widen);
 472   }
 473 
 474   // If the dividend is a constant
 475   if( i1->is_con() ) {
 476     int32 d = i1->get_con();
 477     if( d < 0 ) {
 478       if( d == min_jint ) {
 479         //  (-min_jint) == min_jint == (min_jint / -1)
 480         return TypeInt::make(min_jint, max_jint/2 + 1, widen);
 481       } else {
 482         return TypeInt::make(d, -d, widen);
 483       }
 484     }
 485     return TypeInt::make(-d, d, widen);
 486   }
 487 
 488   // Otherwise we give up all hope
 489   return TypeInt::INT;
 490 }
 491 
 492 
 493 //=============================================================================
 494 //------------------------------Identity---------------------------------------
 495 // If the divisor is 1, we are an identity on the dividend.
 496 Node *DivLNode::Identity( PhaseTransform *phase ) {
 497   return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
 498 }
 499 
 500 //------------------------------Idealize---------------------------------------
 501 // Dividing by a power of 2 is a shift.
 502 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
 503   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 504   // Don't bother trying to transform a dead node
 505   if( in(0) && in(0)->is_top() )  return NULL;
 506 
 507   const Type *t = phase->type( in(2) );
 508   if( t == TypeLong::ONE )      // Identity?
 509     return NULL;                // Skip it
 510 
 511   const TypeLong *tl = t->isa_long();
 512   if( !tl ) return NULL;
 513   if( !tl->is_con() ) return NULL;
 514   jlong l = tl->get_con();      // Get divisor
 515 
 516   if (l == 0) return NULL;      // Dividing by zero constant does not idealize
 517 
 518   set_req(0,NULL);              // Dividing by a not-zero constant; no faulting
 519 
 520   // Dividing by MININT does not optimize as a power-of-2 shift.
 521   if( l == min_jlong ) return NULL;
 522 
 523   return transform_long_divide( phase, in(1), l );
 524 }
 525 
 526 //------------------------------Value------------------------------------------
 527 // A DivLNode divides its inputs.  The third input is a Control input, used to
 528 // prevent hoisting the divide above an unsafe test.
 529 const Type *DivLNode::Value( PhaseTransform *phase ) const {
 530   // Either input is TOP ==> the result is TOP
 531   const Type *t1 = phase->type( in(1) );
 532   const Type *t2 = phase->type( in(2) );
 533   if( t1 == Type::TOP ) return Type::TOP;
 534   if( t2 == Type::TOP ) return Type::TOP;
 535 
 536   // x/x == 1 since we always generate the dynamic divisor check for 0.
 537   if( phase->eqv( in(1), in(2) ) )
 538     return TypeLong::ONE;
 539 
 540   // Either input is BOTTOM ==> the result is the local BOTTOM
 541   const Type *bot = bottom_type();
 542   if( (t1 == bot) || (t2 == bot) ||
 543       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 544     return bot;
 545 
 546   // Divide the two numbers.  We approximate.
 547   // If divisor is a constant and not zero
 548   const TypeLong *i1 = t1->is_long();
 549   const TypeLong *i2 = t2->is_long();
 550   int widen = MAX2(i1->_widen, i2->_widen);
 551 
 552   if( i2->is_con() && i2->get_con() != 0 ) {
 553     jlong d = i2->get_con();    // Divisor
 554     jlong lo, hi;
 555     if( d >= 0 ) {
 556       lo = i1->_lo/d;
 557       hi = i1->_hi/d;
 558     } else {
 559       if( d == CONST64(-1) && i1->_lo == min_jlong ) {
 560         // 'min_jlong/-1' throws arithmetic exception during compilation
 561         lo = min_jlong;
 562         // do not support holes, 'hi' must go to either min_jlong or max_jlong:
 563         // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong]
 564         hi = i1->_hi == min_jlong ? min_jlong : max_jlong;
 565       } else {
 566         lo = i1->_hi/d;
 567         hi = i1->_lo/d;
 568       }
 569     }
 570     return TypeLong::make(lo, hi, widen);
 571   }
 572 
 573   // If the dividend is a constant
 574   if( i1->is_con() ) {
 575     jlong d = i1->get_con();
 576     if( d < 0 ) {
 577       if( d == min_jlong ) {
 578         //  (-min_jlong) == min_jlong == (min_jlong / -1)
 579         return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
 580       } else {
 581         return TypeLong::make(d, -d, widen);
 582       }
 583     }
 584     return TypeLong::make(-d, d, widen);
 585   }
 586 
 587   // Otherwise we give up all hope
 588   return TypeLong::LONG;
 589 }
 590 
 591 
 592 //=============================================================================
 593 //------------------------------Value------------------------------------------
 594 // An DivFNode divides its inputs.  The third input is a Control input, used to
 595 // prevent hoisting the divide above an unsafe test.
 596 const Type *DivFNode::Value( PhaseTransform *phase ) const {
 597   // Either input is TOP ==> the result is TOP
 598   const Type *t1 = phase->type( in(1) );
 599   const Type *t2 = phase->type( in(2) );
 600   if( t1 == Type::TOP ) return Type::TOP;
 601   if( t2 == Type::TOP ) return Type::TOP;
 602 
 603   // Either input is BOTTOM ==> the result is the local BOTTOM
 604   const Type *bot = bottom_type();
 605   if( (t1 == bot) || (t2 == bot) ||
 606       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 607     return bot;
 608 
 609   // x/x == 1, we ignore 0/0.
 610   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 611   // Does not work for variables because of NaN's
 612   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
 613     if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
 614       return TypeF::ONE;
 615 
 616   if( t2 == TypeF::ONE )
 617     return t1;
 618 
 619   // If divisor is a constant and not zero, divide them numbers
 620   if( t1->base() == Type::FloatCon &&
 621       t2->base() == Type::FloatCon &&
 622       t2->getf() != 0.0 ) // could be negative zero
 623     return TypeF::make( t1->getf()/t2->getf() );
 624 
 625   // If the dividend is a constant zero
 626   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 627   // Test TypeF::ZERO is not sufficient as it could be negative zero
 628 
 629   if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
 630     return TypeF::ZERO;
 631 
 632   // Otherwise we give up all hope
 633   return Type::FLOAT;
 634 }
 635 
 636 //------------------------------isA_Copy---------------------------------------
 637 // Dividing by self is 1.
 638 // If the divisor is 1, we are an identity on the dividend.
 639 Node *DivFNode::Identity( PhaseTransform *phase ) {
 640   return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
 641 }
 642 
 643 
 644 //------------------------------Idealize---------------------------------------
 645 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 646   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 647   // Don't bother trying to transform a dead node
 648   if( in(0) && in(0)->is_top() )  return NULL;
 649 
 650   const Type *t2 = phase->type( in(2) );
 651   if( t2 == TypeF::ONE )         // Identity?
 652     return NULL;                // Skip it
 653 
 654   const TypeF *tf = t2->isa_float_constant();
 655   if( !tf ) return NULL;
 656   if( tf->base() != Type::FloatCon ) return NULL;
 657 
 658   // Check for out of range values
 659   if( tf->is_nan() || !tf->is_finite() ) return NULL;
 660 
 661   // Get the value
 662   float f = tf->getf();
 663   int exp;
 664 
 665   // Only for special case of dividing by a power of 2
 666   if( frexp((double)f, &exp) != 0.5 ) return NULL;
 667 
 668   // Limit the range of acceptable exponents
 669   if( exp < -126 || exp > 126 ) return NULL;
 670 
 671   // Compute the reciprocal
 672   float reciprocal = ((float)1.0) / f;
 673 
 674   assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 675 
 676   // return multiplication by the reciprocal
 677   return (new (phase->C, 3) MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
 678 }
 679 
 680 //=============================================================================
 681 //------------------------------Value------------------------------------------
 682 // An DivDNode divides its inputs.  The third input is a Control input, used to
 683 // prevent hoisting the divide above an unsafe test.
 684 const Type *DivDNode::Value( PhaseTransform *phase ) const {
 685   // Either input is TOP ==> the result is TOP
 686   const Type *t1 = phase->type( in(1) );
 687   const Type *t2 = phase->type( in(2) );
 688   if( t1 == Type::TOP ) return Type::TOP;
 689   if( t2 == Type::TOP ) return Type::TOP;
 690 
 691   // Either input is BOTTOM ==> the result is the local BOTTOM
 692   const Type *bot = bottom_type();
 693   if( (t1 == bot) || (t2 == bot) ||
 694       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 695     return bot;
 696 
 697   // x/x == 1, we ignore 0/0.
 698   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 699   // Does not work for variables because of NaN's
 700   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
 701     if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
 702       return TypeD::ONE;
 703 
 704   if( t2 == TypeD::ONE )
 705     return t1;
 706 
 707   // If divisor is a constant and not zero, divide them numbers
 708   if( t1->base() == Type::DoubleCon &&
 709       t2->base() == Type::DoubleCon &&
 710       t2->getd() != 0.0 ) // could be negative zero
 711     return TypeD::make( t1->getd()/t2->getd() );
 712 
 713   // If the dividend is a constant zero
 714   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 715   // Test TypeF::ZERO is not sufficient as it could be negative zero
 716   if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
 717     return TypeD::ZERO;
 718 
 719   // Otherwise we give up all hope
 720   return Type::DOUBLE;
 721 }
 722 
 723 
 724 //------------------------------isA_Copy---------------------------------------
 725 // Dividing by self is 1.
 726 // If the divisor is 1, we are an identity on the dividend.
 727 Node *DivDNode::Identity( PhaseTransform *phase ) {
 728   return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
 729 }
 730 
 731 //------------------------------Idealize---------------------------------------
 732 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 733   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 734   // Don't bother trying to transform a dead node
 735   if( in(0) && in(0)->is_top() )  return NULL;
 736 
 737   const Type *t2 = phase->type( in(2) );
 738   if( t2 == TypeD::ONE )         // Identity?
 739     return NULL;                // Skip it
 740 
 741   const TypeD *td = t2->isa_double_constant();
 742   if( !td ) return NULL;
 743   if( td->base() != Type::DoubleCon ) return NULL;
 744 
 745   // Check for out of range values
 746   if( td->is_nan() || !td->is_finite() ) return NULL;
 747 
 748   // Get the value
 749   double d = td->getd();
 750   int exp;
 751 
 752   // Only for special case of dividing by a power of 2
 753   if( frexp(d, &exp) != 0.5 ) return NULL;
 754 
 755   // Limit the range of acceptable exponents
 756   if( exp < -1021 || exp > 1022 ) return NULL;
 757 
 758   // Compute the reciprocal
 759   double reciprocal = 1.0 / d;
 760 
 761   assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 762 
 763   // return multiplication by the reciprocal
 764   return (new (phase->C, 3) MulDNode(in(1), phase->makecon(TypeD::make(reciprocal))));
 765 }
 766 
 767 //=============================================================================
 768 //------------------------------Idealize---------------------------------------
 769 Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 770   // Check for dead control input
 771   if( in(0) && remove_dead_region(phase, can_reshape) )  return this;
 772   // Don't bother trying to transform a dead node
 773   if( in(0) && in(0)->is_top() )  return NULL;
 774 
 775   // Get the modulus
 776   const Type *t = phase->type( in(2) );
 777   if( t == Type::TOP ) return NULL;
 778   const TypeInt *ti = t->is_int();
 779 
 780   // Check for useless control input
 781   // Check for excluding mod-zero case
 782   if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) {
 783     set_req(0, NULL);        // Yank control input
 784     return this;
 785   }
 786 
 787   // See if we are MOD'ing by 2^k or 2^k-1.
 788   if( !ti->is_con() ) return NULL;
 789   jint con = ti->get_con();
 790 
 791   Node *hook = new (phase->C, 1) Node(1);
 792 
 793   // First, special check for modulo 2^k-1
 794   if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) {
 795     uint k = exact_log2(con+1);  // Extract k
 796 
 797     // Basic algorithm by David Detlefs.  See fastmod_int.java for gory details.
 798     static int unroll_factor[] = { 999, 999, 29, 14, 9, 7, 5, 4, 4, 3, 3, 2, 2, 2, 2, 2, 1 /*past here we assume 1 forever*/};
 799     int trip_count = 1;
 800     if( k < ARRAY_SIZE(unroll_factor))  trip_count = unroll_factor[k];
 801 
 802     // If the unroll factor is not too large, and if conditional moves are
 803     // ok, then use this case
 804     if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
 805       Node *x = in(1);            // Value being mod'd
 806       Node *divisor = in(2);      // Also is mask
 807 
 808       hook->init_req(0, x);       // Add a use to x to prevent him from dying
 809       // Generate code to reduce X rapidly to nearly 2^k-1.
 810       for( int i = 0; i < trip_count; i++ ) {
 811         Node *xl = phase->transform( new (phase->C, 3) AndINode(x,divisor) );
 812         Node *xh = phase->transform( new (phase->C, 3) RShiftINode(x,phase->intcon(k)) ); // Must be signed
 813         x = phase->transform( new (phase->C, 3) AddINode(xh,xl) );
 814         hook->set_req(0, x);
 815       }
 816 
 817       // Generate sign-fixup code.  Was original value positive?
 818       // int hack_res = (i >= 0) ? divisor : 1;
 819       Node *cmp1 = phase->transform( new (phase->C, 3) CmpINode( in(1), phase->intcon(0) ) );
 820       Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
 821       Node *cmov1= phase->transform( new (phase->C, 4) CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
 822       // if( x >= hack_res ) x -= divisor;
 823       Node *sub  = phase->transform( new (phase->C, 3) SubINode( x, divisor ) );
 824       Node *cmp2 = phase->transform( new (phase->C, 3) CmpINode( x, cmov1 ) );
 825       Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
 826       // Convention is to not transform the return value of an Ideal
 827       // since Ideal is expected to return a modified 'this' or a new node.
 828       Node *cmov2= new (phase->C, 4) CMoveINode(bol2, x, sub, TypeInt::INT);
 829       // cmov2 is now the mod
 830 
 831       // Now remove the bogus extra edges used to keep things alive
 832       if (can_reshape) {
 833         phase->is_IterGVN()->remove_dead_node(hook);
 834       } else {
 835         hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
 836       }
 837       return cmov2;
 838     }
 839   }
 840 
 841   // Fell thru, the unroll case is not appropriate. Transform the modulo
 842   // into a long multiply/int multiply/subtract case
 843 
 844   // Cannot handle mod 0, and min_jint isn't handled by the transform
 845   if( con == 0 || con == min_jint ) return NULL;
 846 
 847   // Get the absolute value of the constant; at this point, we can use this
 848   jint pos_con = (con >= 0) ? con : -con;
 849 
 850   // integer Mod 1 is always 0
 851   if( pos_con == 1 ) return new (phase->C, 1) ConINode(TypeInt::ZERO);
 852 
 853   int log2_con = -1;
 854 
 855   // If this is a power of two, they maybe we can mask it
 856   if( is_power_of_2(pos_con) ) {
 857     log2_con = log2_intptr((intptr_t)pos_con);
 858 
 859     const Type *dt = phase->type(in(1));
 860     const TypeInt *dti = dt->isa_int();
 861 
 862     // See if this can be masked, if the dividend is non-negative
 863     if( dti && dti->_lo >= 0 )
 864       return ( new (phase->C, 3) AndINode( in(1), phase->intcon( pos_con-1 ) ) );
 865   }
 866 
 867   // Save in(1) so that it cannot be changed or deleted
 868   hook->init_req(0, in(1));
 869 
 870   // Divide using the transform from DivI to MulL
 871   Node *result = transform_int_divide( phase, in(1), pos_con );
 872   if (result != NULL) {
 873     Node *divide = phase->transform(result);
 874 
 875     // Re-multiply, using a shift if this is a power of two
 876     Node *mult = NULL;
 877 
 878     if( log2_con >= 0 )
 879       mult = phase->transform( new (phase->C, 3) LShiftINode( divide, phase->intcon( log2_con ) ) );
 880     else
 881       mult = phase->transform( new (phase->C, 3) MulINode( divide, phase->intcon( pos_con ) ) );
 882 
 883     // Finally, subtract the multiplied divided value from the original
 884     result = new (phase->C, 3) SubINode( in(1), mult );
 885   }
 886 
 887   // Now remove the bogus extra edges used to keep things alive
 888   if (can_reshape) {
 889     phase->is_IterGVN()->remove_dead_node(hook);
 890   } else {
 891     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
 892   }
 893 
 894   // return the value
 895   return result;
 896 }
 897 
 898 //------------------------------Value------------------------------------------
 899 const Type *ModINode::Value( PhaseTransform *phase ) const {
 900   // Either input is TOP ==> the result is TOP
 901   const Type *t1 = phase->type( in(1) );
 902   const Type *t2 = phase->type( in(2) );
 903   if( t1 == Type::TOP ) return Type::TOP;
 904   if( t2 == Type::TOP ) return Type::TOP;
 905 
 906   // We always generate the dynamic check for 0.
 907   // 0 MOD X is 0
 908   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 909   // X MOD X is 0
 910   if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
 911 
 912   // Either input is BOTTOM ==> the result is the local BOTTOM
 913   const Type *bot = bottom_type();
 914   if( (t1 == bot) || (t2 == bot) ||
 915       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 916     return bot;
 917 
 918   const TypeInt *i1 = t1->is_int();
 919   const TypeInt *i2 = t2->is_int();
 920   if( !i1->is_con() || !i2->is_con() ) {
 921     if( i1->_lo >= 0 && i2->_lo >= 0 )
 922       return TypeInt::POS;
 923     // If both numbers are not constants, we know little.
 924     return TypeInt::INT;
 925   }
 926   // Mod by zero?  Throw exception at runtime!
 927   if( !i2->get_con() ) return TypeInt::POS;
 928 
 929   // We must be modulo'ing 2 float constants.
 930   // Check for min_jint % '-1', result is defined to be '0'.
 931   if( i1->get_con() == min_jint && i2->get_con() == -1 )
 932     return TypeInt::ZERO;
 933 
 934   return TypeInt::make( i1->get_con() % i2->get_con() );
 935 }
 936 
 937 
 938 //=============================================================================
 939 //------------------------------Idealize---------------------------------------
 940 Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 941   // Check for dead control input
 942   if( in(0) && remove_dead_region(phase, can_reshape) )  return this;
 943   // Don't bother trying to transform a dead node
 944   if( in(0) && in(0)->is_top() )  return NULL;
 945 
 946   // Get the modulus
 947   const Type *t = phase->type( in(2) );
 948   if( t == Type::TOP ) return NULL;
 949   const TypeLong *tl = t->is_long();
 950 
 951   // Check for useless control input
 952   // Check for excluding mod-zero case
 953   if( in(0) && (tl->_hi < 0 || tl->_lo > 0) ) {
 954     set_req(0, NULL);        // Yank control input
 955     return this;
 956   }
 957 
 958   // See if we are MOD'ing by 2^k or 2^k-1.
 959   if( !tl->is_con() ) return NULL;
 960   jlong con = tl->get_con();
 961 
 962   Node *hook = new (phase->C, 1) Node(1);
 963 
 964   // Expand mod
 965   if( con >= 0 && con < max_jlong && is_power_of_2_long(con+1) ) {
 966     uint k = log2_long(con);       // Extract k
 967 
 968     // Basic algorithm by David Detlefs.  See fastmod_long.java for gory details.
 969     // Used to help a popular random number generator which does a long-mod
 970     // of 2^31-1 and shows up in SpecJBB and SciMark.
 971     static int unroll_factor[] = { 999, 999, 61, 30, 20, 15, 12, 10, 8, 7, 6, 6, 5, 5, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1 /*past here we assume 1 forever*/};
 972     int trip_count = 1;
 973     if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
 974 
 975     // If the unroll factor is not too large, and if conditional moves are
 976     // ok, then use this case
 977     if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
 978       Node *x = in(1);            // Value being mod'd
 979       Node *divisor = in(2);      // Also is mask
 980 
 981       hook->init_req(0, x);       // Add a use to x to prevent him from dying
 982       // Generate code to reduce X rapidly to nearly 2^k-1.
 983       for( int i = 0; i < trip_count; i++ ) {
 984         Node *xl = phase->transform( new (phase->C, 3) AndLNode(x,divisor) );
 985         Node *xh = phase->transform( new (phase->C, 3) RShiftLNode(x,phase->intcon(k)) ); // Must be signed
 986         x = phase->transform( new (phase->C, 3) AddLNode(xh,xl) );
 987         hook->set_req(0, x);    // Add a use to x to prevent him from dying
 988       }
 989 
 990       // Generate sign-fixup code.  Was original value positive?
 991       // long hack_res = (i >= 0) ? divisor : CONST64(1);
 992       Node *cmp1 = phase->transform( new (phase->C, 3) CmpLNode( in(1), phase->longcon(0) ) );
 993       Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
 994       Node *cmov1= phase->transform( new (phase->C, 4) CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
 995       // if( x >= hack_res ) x -= divisor;
 996       Node *sub  = phase->transform( new (phase->C, 3) SubLNode( x, divisor ) );
 997       Node *cmp2 = phase->transform( new (phase->C, 3) CmpLNode( x, cmov1 ) );
 998       Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
 999       // Convention is to not transform the return value of an Ideal
1000       // since Ideal is expected to return a modified 'this' or a new node.
1001       Node *cmov2= new (phase->C, 4) CMoveLNode(bol2, x, sub, TypeLong::LONG);
1002       // cmov2 is now the mod
1003 
1004       // Now remove the bogus extra edges used to keep things alive
1005       if (can_reshape) {
1006         phase->is_IterGVN()->remove_dead_node(hook);
1007       } else {
1008         hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
1009       }
1010       return cmov2;
1011     }
1012   }
1013 
1014   // Fell thru, the unroll case is not appropriate. Transform the modulo
1015   // into a long multiply/int multiply/subtract case
1016 
1017   // Cannot handle mod 0, and min_jint isn't handled by the transform
1018   if( con == 0 || con == min_jlong ) return NULL;
1019 
1020   // Get the absolute value of the constant; at this point, we can use this
1021   jlong pos_con = (con >= 0) ? con : -con;
1022 
1023   // integer Mod 1 is always 0
1024   if( pos_con == 1 ) return new (phase->C, 1) ConLNode(TypeLong::ZERO);
1025 
1026   int log2_con = -1;
1027 
1028   // If this is a power of two, they maybe we can mask it
1029   if( is_power_of_2_long(pos_con) ) {
1030     log2_con = log2_long(pos_con);
1031 
1032     const Type *dt = phase->type(in(1));
1033     const TypeLong *dtl = dt->isa_long();
1034 
1035     // See if this can be masked, if the dividend is non-negative
1036     if( dtl && dtl->_lo >= 0 )
1037       return ( new (phase->C, 3) AndLNode( in(1), phase->longcon( pos_con-1 ) ) );
1038   }
1039 
1040   // Save in(1) so that it cannot be changed or deleted
1041   hook->init_req(0, in(1));
1042 
1043   // Divide using the transform from DivI to MulL
1044   Node *result = transform_long_divide( phase, in(1), pos_con );
1045   if (result != NULL) {
1046     Node *divide = phase->transform(result);
1047 
1048     // Re-multiply, using a shift if this is a power of two
1049     Node *mult = NULL;
1050 
1051     if( log2_con >= 0 )
1052       mult = phase->transform( new (phase->C, 3) LShiftLNode( divide, phase->intcon( log2_con ) ) );
1053     else
1054       mult = phase->transform( new (phase->C, 3) MulLNode( divide, phase->longcon( pos_con ) ) );
1055 
1056     // Finally, subtract the multiplied divided value from the original
1057     result = new (phase->C, 3) SubLNode( in(1), mult );
1058   }
1059 
1060   // Now remove the bogus extra edges used to keep things alive
1061   if (can_reshape) {
1062     phase->is_IterGVN()->remove_dead_node(hook);
1063   } else {
1064     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
1065   }
1066 
1067   // return the value
1068   return result;
1069 }
1070 
1071 //------------------------------Value------------------------------------------
1072 const Type *ModLNode::Value( PhaseTransform *phase ) const {
1073   // Either input is TOP ==> the result is TOP
1074   const Type *t1 = phase->type( in(1) );
1075   const Type *t2 = phase->type( in(2) );
1076   if( t1 == Type::TOP ) return Type::TOP;
1077   if( t2 == Type::TOP ) return Type::TOP;
1078 
1079   // We always generate the dynamic check for 0.
1080   // 0 MOD X is 0
1081   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1082   // X MOD X is 0
1083   if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
1084 
1085   // Either input is BOTTOM ==> the result is the local BOTTOM
1086   const Type *bot = bottom_type();
1087   if( (t1 == bot) || (t2 == bot) ||
1088       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1089     return bot;
1090 
1091   const TypeLong *i1 = t1->is_long();
1092   const TypeLong *i2 = t2->is_long();
1093   if( !i1->is_con() || !i2->is_con() ) {
1094     if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
1095       return TypeLong::POS;
1096     // If both numbers are not constants, we know little.
1097     return TypeLong::LONG;
1098   }
1099   // Mod by zero?  Throw exception at runtime!
1100   if( !i2->get_con() ) return TypeLong::POS;
1101 
1102   // We must be modulo'ing 2 float constants.
1103   // Check for min_jint % '-1', result is defined to be '0'.
1104   if( i1->get_con() == min_jlong && i2->get_con() == -1 )
1105     return TypeLong::ZERO;
1106 
1107   return TypeLong::make( i1->get_con() % i2->get_con() );
1108 }
1109 
1110 
1111 //=============================================================================
1112 //------------------------------Value------------------------------------------
1113 const Type *ModFNode::Value( PhaseTransform *phase ) const {
1114   // Either input is TOP ==> the result is TOP
1115   const Type *t1 = phase->type( in(1) );
1116   const Type *t2 = phase->type( in(2) );
1117   if( t1 == Type::TOP ) return Type::TOP;
1118   if( t2 == Type::TOP ) return Type::TOP;
1119 
1120   // Either input is BOTTOM ==> the result is the local BOTTOM
1121   const Type *bot = bottom_type();
1122   if( (t1 == bot) || (t2 == bot) ||
1123       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1124     return bot;
1125 
1126   // If either number is not a constant, we know nothing.
1127   if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) {
1128     return Type::FLOAT;         // note: x%x can be either NaN or 0
1129   }
1130 
1131   float f1 = t1->getf();
1132   float f2 = t2->getf();
1133   jint  x1 = jint_cast(f1);     // note:  *(int*)&f1, not just (int)f1
1134   jint  x2 = jint_cast(f2);
1135 
1136   // If either is a NaN, return an input NaN
1137   if (g_isnan(f1))    return t1;
1138   if (g_isnan(f2))    return t2;
1139 
1140   // If an operand is infinity or the divisor is +/- zero, punt.
1141   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint)
1142     return Type::FLOAT;
1143 
1144   // We must be modulo'ing 2 float constants.
1145   // Make sure that the sign of the fmod is equal to the sign of the dividend
1146   jint xr = jint_cast(fmod(f1, f2));
1147   if ((x1 ^ xr) < 0) {
1148     xr ^= min_jint;
1149   }
1150 
1151   return TypeF::make(jfloat_cast(xr));
1152 }
1153 
1154 
1155 //=============================================================================
1156 //------------------------------Value------------------------------------------
1157 const Type *ModDNode::Value( PhaseTransform *phase ) const {
1158   // Either input is TOP ==> the result is TOP
1159   const Type *t1 = phase->type( in(1) );
1160   const Type *t2 = phase->type( in(2) );
1161   if( t1 == Type::TOP ) return Type::TOP;
1162   if( t2 == Type::TOP ) return Type::TOP;
1163 
1164   // Either input is BOTTOM ==> the result is the local BOTTOM
1165   const Type *bot = bottom_type();
1166   if( (t1 == bot) || (t2 == bot) ||
1167       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1168     return bot;
1169 
1170   // If either number is not a constant, we know nothing.
1171   if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) {
1172     return Type::DOUBLE;        // note: x%x can be either NaN or 0
1173   }
1174 
1175   double f1 = t1->getd();
1176   double f2 = t2->getd();
1177   jlong  x1 = jlong_cast(f1);   // note:  *(long*)&f1, not just (long)f1
1178   jlong  x2 = jlong_cast(f2);
1179 
1180   // If either is a NaN, return an input NaN
1181   if (g_isnan(f1))    return t1;
1182   if (g_isnan(f2))    return t2;
1183 
1184   // If an operand is infinity or the divisor is +/- zero, punt.
1185   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong)
1186     return Type::DOUBLE;
1187 
1188   // We must be modulo'ing 2 double constants.
1189   // Make sure that the sign of the fmod is equal to the sign of the dividend
1190   jlong xr = jlong_cast(fmod(f1, f2));
1191   if ((x1 ^ xr) < 0) {
1192     xr ^= min_jlong;
1193   }
1194 
1195   return TypeD::make(jdouble_cast(xr));
1196 }
1197 
1198 //=============================================================================
1199 
1200 DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) {
1201   init_req(0, c);
1202   init_req(1, dividend);
1203   init_req(2, divisor);
1204 }
1205 
1206 //------------------------------make------------------------------------------
1207 DivModINode* DivModINode::make(Compile* C, Node* div_or_mod) {
1208   Node* n = div_or_mod;
1209   assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI,
1210          "only div or mod input pattern accepted");
1211 
1212   DivModINode* divmod = new (C, 3) DivModINode(n->in(0), n->in(1), n->in(2));
1213   Node*        dproj  = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
1214   Node*        mproj  = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
1215   return divmod;
1216 }
1217 
1218 //------------------------------make------------------------------------------
1219 DivModLNode* DivModLNode::make(Compile* C, Node* div_or_mod) {
1220   Node* n = div_or_mod;
1221   assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL,
1222          "only div or mod input pattern accepted");
1223 
1224   DivModLNode* divmod = new (C, 3) DivModLNode(n->in(0), n->in(1), n->in(2));
1225   Node*        dproj  = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
1226   Node*        mproj  = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
1227   return divmod;
1228 }
1229 
1230 //------------------------------match------------------------------------------
1231 // return result(s) along with their RegMask info
1232 Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) {
1233   uint ideal_reg = proj->ideal_reg();
1234   RegMask rm;
1235   if (proj->_con == div_proj_num) {
1236     rm = match->divI_proj_mask();
1237   } else {
1238     assert(proj->_con == mod_proj_num, "must be div or mod projection");
1239     rm = match->modI_proj_mask();
1240   }
1241   return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
1242 }
1243 
1244 
1245 //------------------------------match------------------------------------------
1246 // return result(s) along with their RegMask info
1247 Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) {
1248   uint ideal_reg = proj->ideal_reg();
1249   RegMask rm;
1250   if (proj->_con == div_proj_num) {
1251     rm = match->divL_proj_mask();
1252   } else {
1253     assert(proj->_con == mod_proj_num, "must be div or mod projection");
1254     rm = match->modL_proj_mask();
1255   }
1256   return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
1257 }