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   Node *t3 = phase->transform(new (phase->C, 3) RShiftLNode(t2, phase->intcon(N / 2)));
 268   Node *t4 = phase->transform(new (phase->C, 3) AndLNode(t2, phase->longcon(0xFFFFFFFF)));
 269   Node *t5 = phase->transform(new (phase->C, 3) AddLNode(t4, lohi_product));
 270   Node *t6 = phase->transform(new (phase->C, 3) RShiftLNode(t5, phase->intcon(N / 2)));
 271   Node *t7 = phase->transform(new (phase->C, 3) AddLNode(t3, hihi_product));
 272 
 273   return new (phase->C, 3) AddLNode(t7, t6);
 274 }
 275 
 276 
 277 //--------------------------transform_long_divide------------------------------
 278 // Convert a division by constant divisor into an alternate Ideal graph.
 279 // Return NULL if no transformation occurs.
 280 static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) {
 281   // Check for invalid divisors
 282   assert( divisor != 0L && divisor != min_jlong,
 283           "bad divisor for transforming to long multiply" );
 284 
 285   bool d_pos = divisor >= 0;
 286   jlong d = d_pos ? divisor : -divisor;
 287   const int N = 64;
 288 
 289   // Result
 290   Node *q = NULL;
 291 
 292   if (d == 1) {
 293     // division by +/- 1
 294     if (!d_pos) {
 295       // Just negate the value
 296       q = new (phase->C, 3) SubLNode(phase->longcon(0), dividend);
 297     }
 298   } else if ( is_power_of_2_long(d) ) {
 299 
 300     // division by +/- a power of 2
 301 
 302     // See if we can simply do a shift without rounding
 303     bool needs_rounding = true;
 304     const Type *dt = phase->type(dividend);
 305     const TypeLong *dtl = dt->isa_long();
 306 
 307     if (dtl && dtl->_lo > 0) {
 308       // we don't need to round a positive dividend
 309       needs_rounding = false;
 310     } else if( dividend->Opcode() == Op_AndL ) {
 311       // An AND mask of sufficient size clears the low bits and
 312       // I can avoid rounding.
 313       const TypeLong *andconl = phase->type( dividend->in(2) )->isa_long();
 314       if( andconl && andconl->is_con(-d)) {
 315         dividend = dividend->in(1);
 316         needs_rounding = false;
 317       }
 318     }
 319 
 320     // Add rounding to the shift to handle the sign bit
 321     int l = log2_long(d-1)+1;
 322     if (needs_rounding) {
 323       // Divide-by-power-of-2 can be made into a shift, but you have to do
 324       // more math for the rounding.  You need to add 0 for positive
 325       // numbers, and "i-1" for negative numbers.  Example: i=4, so the
 326       // shift is by 2.  You need to add 3 to negative dividends and 0 to
 327       // positive ones.  So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
 328       // (-2+3)>>2 becomes 0, etc.
 329 
 330       // Compute 0 or -1, based on sign bit
 331       Node *sign = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N - 1)));
 332       // Mask sign bit to the low sign bits
 333       Node *round = phase->transform(new (phase->C, 3) URShiftLNode(sign, phase->intcon(N - l)));
 334       // Round up before shifting
 335       dividend = phase->transform(new (phase->C, 3) AddLNode(dividend, round));
 336     }
 337 
 338     // Shift for division
 339     q = new (phase->C, 3) RShiftLNode(dividend, phase->intcon(l));
 340 
 341     if (!d_pos) {
 342       q = new (phase->C, 3) SubLNode(phase->longcon(0), phase->transform(q));
 343     }
 344   } else {
 345     // Attempt the jlong constant divide -> multiply transform found in
 346     //   "Division by Invariant Integers using Multiplication"
 347     //     by Granlund and Montgomery
 348     // See also "Hacker's Delight", chapter 10 by Warren.
 349 
 350     jlong magic_const;
 351     jint shift_const;
 352     if (magic_long_divide_constants(d, magic_const, shift_const)) {
 353       // Compute the high half of the dividend x magic multiplication
 354       Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const));
 355 
 356       // The high half of the 128-bit multiply is computed.
 357       if (magic_const < 0) {
 358         // The magic multiplier is too large for a 64 bit constant. We've adjusted
 359         // it down by 2^64, but have to add 1 dividend back in after the multiplication.
 360         // This handles the "overflow" case described by Granlund and Montgomery.
 361         mul_hi = phase->transform(new (phase->C, 3) AddLNode(dividend, mul_hi));
 362       }
 363 
 364       // Shift over the (adjusted) mulhi
 365       if (shift_const != 0) {
 366         mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(shift_const)));
 367       }
 368 
 369       // Get a 0 or -1 from the sign of the dividend.
 370       Node *addend0 = mul_hi;
 371       Node *addend1 = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N-1)));
 372 
 373       // If the divisor is negative, swap the order of the input addends;
 374       // this has the effect of negating the quotient.
 375       if (!d_pos) {
 376         Node *temp = addend0; addend0 = addend1; addend1 = temp;
 377       }
 378 
 379       // Adjust the final quotient by subtracting -1 (adding 1)
 380       // from the mul_hi.
 381       q = new (phase->C, 3) SubLNode(addend0, addend1);
 382     }
 383   }
 384 
 385   return q;
 386 }
 387 
 388 //=============================================================================
 389 //------------------------------Identity---------------------------------------
 390 // If the divisor is 1, we are an identity on the dividend.
 391 Node *DivINode::Identity( PhaseTransform *phase ) {
 392   return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
 393 }
 394 
 395 //------------------------------Idealize---------------------------------------
 396 // Divides can be changed to multiplies and/or shifts
 397 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 398   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 399 
 400   const Type *t = phase->type( in(2) );
 401   if( t == TypeInt::ONE )       // Identity?
 402     return NULL;                // Skip it
 403 
 404   const TypeInt *ti = t->isa_int();
 405   if( !ti ) return NULL;
 406   if( !ti->is_con() ) return NULL;
 407   jint i = ti->get_con();       // Get divisor
 408 
 409   if (i == 0) return NULL;      // Dividing by zero constant does not idealize
 410 
 411   set_req(0,NULL);              // Dividing by a not-zero constant; no faulting
 412 
 413   // Dividing by MININT does not optimize as a power-of-2 shift.
 414   if( i == min_jint ) return NULL;
 415 
 416   return transform_int_divide( phase, in(1), i );
 417 }
 418 
 419 //------------------------------Value------------------------------------------
 420 // A DivINode divides its inputs.  The third input is a Control input, used to
 421 // prevent hoisting the divide above an unsafe test.
 422 const Type *DivINode::Value( PhaseTransform *phase ) const {
 423   // Either input is TOP ==> the result is TOP
 424   const Type *t1 = phase->type( in(1) );
 425   const Type *t2 = phase->type( in(2) );
 426   if( t1 == Type::TOP ) return Type::TOP;
 427   if( t2 == Type::TOP ) return Type::TOP;
 428 
 429   // x/x == 1 since we always generate the dynamic divisor check for 0.
 430   if( phase->eqv( in(1), in(2) ) )
 431     return TypeInt::ONE;
 432 
 433   // Either input is BOTTOM ==> the result is the local BOTTOM
 434   const Type *bot = bottom_type();
 435   if( (t1 == bot) || (t2 == bot) ||
 436       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 437     return bot;
 438 
 439   // Divide the two numbers.  We approximate.
 440   // If divisor is a constant and not zero
 441   const TypeInt *i1 = t1->is_int();
 442   const TypeInt *i2 = t2->is_int();
 443   int widen = MAX2(i1->_widen, i2->_widen);
 444 
 445   if( i2->is_con() && i2->get_con() != 0 ) {
 446     int32 d = i2->get_con(); // Divisor
 447     jint lo, hi;
 448     if( d >= 0 ) {
 449       lo = i1->_lo/d;
 450       hi = i1->_hi/d;
 451     } else {
 452       if( d == -1 && i1->_lo == min_jint ) {
 453         // 'min_jint/-1' throws arithmetic exception during compilation
 454         lo = min_jint;
 455         // do not support holes, 'hi' must go to either min_jint or max_jint:
 456         // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint]
 457         hi = i1->_hi == min_jint ? min_jint : max_jint;
 458       } else {
 459         lo = i1->_hi/d;
 460         hi = i1->_lo/d;
 461       }
 462     }
 463     return TypeInt::make(lo, hi, widen);
 464   }
 465 
 466   // If the dividend is a constant
 467   if( i1->is_con() ) {
 468     int32 d = i1->get_con();
 469     if( d < 0 ) {
 470       if( d == min_jint ) {
 471         //  (-min_jint) == min_jint == (min_jint / -1)
 472         return TypeInt::make(min_jint, max_jint/2 + 1, widen);
 473       } else {
 474         return TypeInt::make(d, -d, widen);
 475       }
 476     }
 477     return TypeInt::make(-d, d, widen);
 478   }
 479 
 480   // Otherwise we give up all hope
 481   return TypeInt::INT;
 482 }
 483 
 484 
 485 //=============================================================================
 486 //------------------------------Identity---------------------------------------
 487 // If the divisor is 1, we are an identity on the dividend.
 488 Node *DivLNode::Identity( PhaseTransform *phase ) {
 489   return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
 490 }
 491 
 492 //------------------------------Idealize---------------------------------------
 493 // Dividing by a power of 2 is a shift.
 494 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
 495   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 496 
 497   const Type *t = phase->type( in(2) );
 498   if( t == TypeLong::ONE )      // Identity?
 499     return NULL;                // Skip it
 500 
 501   const TypeLong *tl = t->isa_long();
 502   if( !tl ) return NULL;
 503   if( !tl->is_con() ) return NULL;
 504   jlong l = tl->get_con();      // Get divisor
 505 
 506   if (l == 0) return NULL;      // Dividing by zero constant does not idealize
 507 
 508   set_req(0,NULL);              // Dividing by a not-zero constant; no faulting
 509 
 510   // Dividing by MININT does not optimize as a power-of-2 shift.
 511   if( l == min_jlong ) return NULL;
 512 
 513   return transform_long_divide( phase, in(1), l );
 514 }
 515 
 516 //------------------------------Value------------------------------------------
 517 // A DivLNode divides its inputs.  The third input is a Control input, used to
 518 // prevent hoisting the divide above an unsafe test.
 519 const Type *DivLNode::Value( PhaseTransform *phase ) const {
 520   // Either input is TOP ==> the result is TOP
 521   const Type *t1 = phase->type( in(1) );
 522   const Type *t2 = phase->type( in(2) );
 523   if( t1 == Type::TOP ) return Type::TOP;
 524   if( t2 == Type::TOP ) return Type::TOP;
 525 
 526   // x/x == 1 since we always generate the dynamic divisor check for 0.
 527   if( phase->eqv( in(1), in(2) ) )
 528     return TypeLong::ONE;
 529 
 530   // Either input is BOTTOM ==> the result is the local BOTTOM
 531   const Type *bot = bottom_type();
 532   if( (t1 == bot) || (t2 == bot) ||
 533       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 534     return bot;
 535 
 536   // Divide the two numbers.  We approximate.
 537   // If divisor is a constant and not zero
 538   const TypeLong *i1 = t1->is_long();
 539   const TypeLong *i2 = t2->is_long();
 540   int widen = MAX2(i1->_widen, i2->_widen);
 541 
 542   if( i2->is_con() && i2->get_con() != 0 ) {
 543     jlong d = i2->get_con();    // Divisor
 544     jlong lo, hi;
 545     if( d >= 0 ) {
 546       lo = i1->_lo/d;
 547       hi = i1->_hi/d;
 548     } else {
 549       if( d == CONST64(-1) && i1->_lo == min_jlong ) {
 550         // 'min_jlong/-1' throws arithmetic exception during compilation
 551         lo = min_jlong;
 552         // do not support holes, 'hi' must go to either min_jlong or max_jlong:
 553         // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong]
 554         hi = i1->_hi == min_jlong ? min_jlong : max_jlong;
 555       } else {
 556         lo = i1->_hi/d;
 557         hi = i1->_lo/d;
 558       }
 559     }
 560     return TypeLong::make(lo, hi, widen);
 561   }
 562 
 563   // If the dividend is a constant
 564   if( i1->is_con() ) {
 565     jlong d = i1->get_con();
 566     if( d < 0 ) {
 567       if( d == min_jlong ) {
 568         //  (-min_jlong) == min_jlong == (min_jlong / -1)
 569         return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
 570       } else {
 571         return TypeLong::make(d, -d, widen);
 572       }
 573     }
 574     return TypeLong::make(-d, d, widen);
 575   }
 576 
 577   // Otherwise we give up all hope
 578   return TypeLong::LONG;
 579 }
 580 
 581 
 582 //=============================================================================
 583 //------------------------------Value------------------------------------------
 584 // An DivFNode divides its inputs.  The third input is a Control input, used to
 585 // prevent hoisting the divide above an unsafe test.
 586 const Type *DivFNode::Value( PhaseTransform *phase ) const {
 587   // Either input is TOP ==> the result is TOP
 588   const Type *t1 = phase->type( in(1) );
 589   const Type *t2 = phase->type( in(2) );
 590   if( t1 == Type::TOP ) return Type::TOP;
 591   if( t2 == Type::TOP ) return Type::TOP;
 592 
 593   // Either input is BOTTOM ==> the result is the local BOTTOM
 594   const Type *bot = bottom_type();
 595   if( (t1 == bot) || (t2 == bot) ||
 596       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 597     return bot;
 598 
 599   // x/x == 1, we ignore 0/0.
 600   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 601   // Does not work for variables because of NaN's
 602   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon)
 603     if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN
 604       return TypeF::ONE;
 605 
 606   if( t2 == TypeF::ONE )
 607     return t1;
 608 
 609   // If divisor is a constant and not zero, divide them numbers
 610   if( t1->base() == Type::FloatCon &&
 611       t2->base() == Type::FloatCon &&
 612       t2->getf() != 0.0 ) // could be negative zero
 613     return TypeF::make( t1->getf()/t2->getf() );
 614 
 615   // If the dividend is a constant zero
 616   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 617   // Test TypeF::ZERO is not sufficient as it could be negative zero
 618 
 619   if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
 620     return TypeF::ZERO;
 621 
 622   // Otherwise we give up all hope
 623   return Type::FLOAT;
 624 }
 625 
 626 //------------------------------isA_Copy---------------------------------------
 627 // Dividing by self is 1.
 628 // If the divisor is 1, we are an identity on the dividend.
 629 Node *DivFNode::Identity( PhaseTransform *phase ) {
 630   return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
 631 }
 632 
 633 
 634 //------------------------------Idealize---------------------------------------
 635 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 636   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 637 
 638   const Type *t2 = phase->type( in(2) );
 639   if( t2 == TypeF::ONE )         // Identity?
 640     return NULL;                // Skip it
 641 
 642   const TypeF *tf = t2->isa_float_constant();
 643   if( !tf ) return NULL;
 644   if( tf->base() != Type::FloatCon ) return NULL;
 645 
 646   // Check for out of range values
 647   if( tf->is_nan() || !tf->is_finite() ) return NULL;
 648 
 649   // Get the value
 650   float f = tf->getf();
 651   int exp;
 652 
 653   // Only for special case of dividing by a power of 2
 654   if( frexp((double)f, &exp) != 0.5 ) return NULL;
 655 
 656   // Limit the range of acceptable exponents
 657   if( exp < -126 || exp > 126 ) return NULL;
 658 
 659   // Compute the reciprocal
 660   float reciprocal = ((float)1.0) / f;
 661 
 662   assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 663 
 664   // return multiplication by the reciprocal
 665   return (new (phase->C, 3) MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
 666 }
 667 
 668 //=============================================================================
 669 //------------------------------Value------------------------------------------
 670 // An DivDNode divides its inputs.  The third input is a Control input, used to
 671 // prevent hoisting the divide above an unsafe test.
 672 const Type *DivDNode::Value( PhaseTransform *phase ) const {
 673   // Either input is TOP ==> the result is TOP
 674   const Type *t1 = phase->type( in(1) );
 675   const Type *t2 = phase->type( in(2) );
 676   if( t1 == Type::TOP ) return Type::TOP;
 677   if( t2 == Type::TOP ) return Type::TOP;
 678 
 679   // Either input is BOTTOM ==> the result is the local BOTTOM
 680   const Type *bot = bottom_type();
 681   if( (t1 == bot) || (t2 == bot) ||
 682       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 683     return bot;
 684 
 685   // x/x == 1, we ignore 0/0.
 686   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 687   // Does not work for variables because of NaN's
 688   if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon)
 689     if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN
 690       return TypeD::ONE;
 691 
 692   if( t2 == TypeD::ONE )
 693     return t1;
 694 
 695   // If divisor is a constant and not zero, divide them numbers
 696   if( t1->base() == Type::DoubleCon &&
 697       t2->base() == Type::DoubleCon &&
 698       t2->getd() != 0.0 ) // could be negative zero
 699     return TypeD::make( t1->getd()/t2->getd() );
 700 
 701   // If the dividend is a constant zero
 702   // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
 703   // Test TypeF::ZERO is not sufficient as it could be negative zero
 704   if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
 705     return TypeD::ZERO;
 706 
 707   // Otherwise we give up all hope
 708   return Type::DOUBLE;
 709 }
 710 
 711 
 712 //------------------------------isA_Copy---------------------------------------
 713 // Dividing by self is 1.
 714 // If the divisor is 1, we are an identity on the dividend.
 715 Node *DivDNode::Identity( PhaseTransform *phase ) {
 716   return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
 717 }
 718 
 719 //------------------------------Idealize---------------------------------------
 720 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 721   if (in(0) && remove_dead_region(phase, can_reshape))  return this;
 722 
 723   const Type *t2 = phase->type( in(2) );
 724   if( t2 == TypeD::ONE )         // Identity?
 725     return NULL;                // Skip it
 726 
 727   const TypeD *td = t2->isa_double_constant();
 728   if( !td ) return NULL;
 729   if( td->base() != Type::DoubleCon ) return NULL;
 730 
 731   // Check for out of range values
 732   if( td->is_nan() || !td->is_finite() ) return NULL;
 733 
 734   // Get the value
 735   double d = td->getd();
 736   int exp;
 737 
 738   // Only for special case of dividing by a power of 2
 739   if( frexp(d, &exp) != 0.5 ) return NULL;
 740 
 741   // Limit the range of acceptable exponents
 742   if( exp < -1021 || exp > 1022 ) return NULL;
 743 
 744   // Compute the reciprocal
 745   double reciprocal = 1.0 / d;
 746 
 747   assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
 748 
 749   // return multiplication by the reciprocal
 750   return (new (phase->C, 3) MulDNode(in(1), phase->makecon(TypeD::make(reciprocal))));
 751 }
 752 
 753 //=============================================================================
 754 //------------------------------Idealize---------------------------------------
 755 Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) {
 756   // Check for dead control input
 757   if( remove_dead_region(phase, can_reshape) )  return this;
 758 
 759   // Get the modulus
 760   const Type *t = phase->type( in(2) );
 761   if( t == Type::TOP ) return NULL;
 762   const TypeInt *ti = t->is_int();
 763 
 764   // Check for useless control input
 765   // Check for excluding mod-zero case
 766   if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) {
 767     set_req(0, NULL);        // Yank control input
 768     return this;
 769   }
 770 
 771   // See if we are MOD'ing by 2^k or 2^k-1.
 772   if( !ti->is_con() ) return NULL;
 773   jint con = ti->get_con();
 774 
 775   Node *hook = new (phase->C, 1) Node(1);
 776 
 777   // First, special check for modulo 2^k-1
 778   if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) {
 779     uint k = exact_log2(con+1);  // Extract k
 780 
 781     // Basic algorithm by David Detlefs.  See fastmod_int.java for gory details.
 782     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*/};
 783     int trip_count = 1;
 784     if( k < ARRAY_SIZE(unroll_factor))  trip_count = unroll_factor[k];
 785 
 786     // If the unroll factor is not too large, and if conditional moves are
 787     // ok, then use this case
 788     if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
 789       Node *x = in(1);            // Value being mod'd
 790       Node *divisor = in(2);      // Also is mask
 791 
 792       hook->init_req(0, x);       // Add a use to x to prevent him from dying
 793       // Generate code to reduce X rapidly to nearly 2^k-1.
 794       for( int i = 0; i < trip_count; i++ ) {
 795         Node *xl = phase->transform( new (phase->C, 3) AndINode(x,divisor) );
 796         Node *xh = phase->transform( new (phase->C, 3) RShiftINode(x,phase->intcon(k)) ); // Must be signed
 797         x = phase->transform( new (phase->C, 3) AddINode(xh,xl) );
 798         hook->set_req(0, x);
 799       }
 800 
 801       // Generate sign-fixup code.  Was original value positive?
 802       // int hack_res = (i >= 0) ? divisor : 1;
 803       Node *cmp1 = phase->transform( new (phase->C, 3) CmpINode( in(1), phase->intcon(0) ) );
 804       Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
 805       Node *cmov1= phase->transform( new (phase->C, 4) CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
 806       // if( x >= hack_res ) x -= divisor;
 807       Node *sub  = phase->transform( new (phase->C, 3) SubINode( x, divisor ) );
 808       Node *cmp2 = phase->transform( new (phase->C, 3) CmpINode( x, cmov1 ) );
 809       Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
 810       // Convention is to not transform the return value of an Ideal
 811       // since Ideal is expected to return a modified 'this' or a new node.
 812       Node *cmov2= new (phase->C, 4) CMoveINode(bol2, x, sub, TypeInt::INT);
 813       // cmov2 is now the mod
 814 
 815       // Now remove the bogus extra edges used to keep things alive
 816       if (can_reshape) {
 817         phase->is_IterGVN()->remove_dead_node(hook);
 818       } else {
 819         hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
 820       }
 821       return cmov2;
 822     }
 823   }
 824 
 825   // Fell thru, the unroll case is not appropriate. Transform the modulo
 826   // into a long multiply/int multiply/subtract case
 827 
 828   // Cannot handle mod 0, and min_jint isn't handled by the transform
 829   if( con == 0 || con == min_jint ) return NULL;
 830 
 831   // Get the absolute value of the constant; at this point, we can use this
 832   jint pos_con = (con >= 0) ? con : -con;
 833 
 834   // integer Mod 1 is always 0
 835   if( pos_con == 1 ) return new (phase->C, 1) ConINode(TypeInt::ZERO);
 836 
 837   int log2_con = -1;
 838 
 839   // If this is a power of two, they maybe we can mask it
 840   if( is_power_of_2(pos_con) ) {
 841     log2_con = log2_intptr((intptr_t)pos_con);
 842 
 843     const Type *dt = phase->type(in(1));
 844     const TypeInt *dti = dt->isa_int();
 845 
 846     // See if this can be masked, if the dividend is non-negative
 847     if( dti && dti->_lo >= 0 )
 848       return ( new (phase->C, 3) AndINode( in(1), phase->intcon( pos_con-1 ) ) );
 849   }
 850 
 851   // Save in(1) so that it cannot be changed or deleted
 852   hook->init_req(0, in(1));
 853 
 854   // Divide using the transform from DivI to MulL
 855   Node *result = transform_int_divide( phase, in(1), pos_con );
 856   if (result != NULL) {
 857     Node *divide = phase->transform(result);
 858 
 859     // Re-multiply, using a shift if this is a power of two
 860     Node *mult = NULL;
 861 
 862     if( log2_con >= 0 )
 863       mult = phase->transform( new (phase->C, 3) LShiftINode( divide, phase->intcon( log2_con ) ) );
 864     else
 865       mult = phase->transform( new (phase->C, 3) MulINode( divide, phase->intcon( pos_con ) ) );
 866 
 867     // Finally, subtract the multiplied divided value from the original
 868     result = new (phase->C, 3) SubINode( in(1), mult );
 869   }
 870 
 871   // Now remove the bogus extra edges used to keep things alive
 872   if (can_reshape) {
 873     phase->is_IterGVN()->remove_dead_node(hook);
 874   } else {
 875     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
 876   }
 877 
 878   // return the value
 879   return result;
 880 }
 881 
 882 //------------------------------Value------------------------------------------
 883 const Type *ModINode::Value( PhaseTransform *phase ) const {
 884   // Either input is TOP ==> the result is TOP
 885   const Type *t1 = phase->type( in(1) );
 886   const Type *t2 = phase->type( in(2) );
 887   if( t1 == Type::TOP ) return Type::TOP;
 888   if( t2 == Type::TOP ) return Type::TOP;
 889 
 890   // We always generate the dynamic check for 0.
 891   // 0 MOD X is 0
 892   if( t1 == TypeInt::ZERO ) return TypeInt::ZERO;
 893   // X MOD X is 0
 894   if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO;
 895 
 896   // Either input is BOTTOM ==> the result is the local BOTTOM
 897   const Type *bot = bottom_type();
 898   if( (t1 == bot) || (t2 == bot) ||
 899       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
 900     return bot;
 901 
 902   const TypeInt *i1 = t1->is_int();
 903   const TypeInt *i2 = t2->is_int();
 904   if( !i1->is_con() || !i2->is_con() ) {
 905     if( i1->_lo >= 0 && i2->_lo >= 0 )
 906       return TypeInt::POS;
 907     // If both numbers are not constants, we know little.
 908     return TypeInt::INT;
 909   }
 910   // Mod by zero?  Throw exception at runtime!
 911   if( !i2->get_con() ) return TypeInt::POS;
 912 
 913   // We must be modulo'ing 2 float constants.
 914   // Check for min_jint % '-1', result is defined to be '0'.
 915   if( i1->get_con() == min_jint && i2->get_con() == -1 )
 916     return TypeInt::ZERO;
 917 
 918   return TypeInt::make( i1->get_con() % i2->get_con() );
 919 }
 920 
 921 
 922 //=============================================================================
 923 //------------------------------Idealize---------------------------------------
 924 Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
 925   // Check for dead control input
 926   if( remove_dead_region(phase, can_reshape) )  return this;
 927 
 928   // Get the modulus
 929   const Type *t = phase->type( in(2) );
 930   if( t == Type::TOP ) return NULL;
 931   const TypeLong *tl = t->is_long();
 932 
 933   // Check for useless control input
 934   // Check for excluding mod-zero case
 935   if( in(0) && (tl->_hi < 0 || tl->_lo > 0) ) {
 936     set_req(0, NULL);        // Yank control input
 937     return this;
 938   }
 939 
 940   // See if we are MOD'ing by 2^k or 2^k-1.
 941   if( !tl->is_con() ) return NULL;
 942   jlong con = tl->get_con();
 943 
 944   Node *hook = new (phase->C, 1) Node(1);
 945 
 946   // Expand mod
 947   if( con >= 0 && con < max_jlong && is_power_of_2_long(con+1) ) {
 948     uint k = log2_long(con);       // Extract k
 949 
 950     // Basic algorithm by David Detlefs.  See fastmod_long.java for gory details.
 951     // Used to help a popular random number generator which does a long-mod
 952     // of 2^31-1 and shows up in SpecJBB and SciMark.
 953     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*/};
 954     int trip_count = 1;
 955     if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
 956 
 957     // If the unroll factor is not too large, and if conditional moves are
 958     // ok, then use this case
 959     if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
 960       Node *x = in(1);            // Value being mod'd
 961       Node *divisor = in(2);      // Also is mask
 962 
 963       hook->init_req(0, x);       // Add a use to x to prevent him from dying
 964       // Generate code to reduce X rapidly to nearly 2^k-1.
 965       for( int i = 0; i < trip_count; i++ ) {
 966         Node *xl = phase->transform( new (phase->C, 3) AndLNode(x,divisor) );
 967         Node *xh = phase->transform( new (phase->C, 3) RShiftLNode(x,phase->intcon(k)) ); // Must be signed
 968         x = phase->transform( new (phase->C, 3) AddLNode(xh,xl) );
 969         hook->set_req(0, x);    // Add a use to x to prevent him from dying
 970       }
 971 
 972       // Generate sign-fixup code.  Was original value positive?
 973       // long hack_res = (i >= 0) ? divisor : CONST64(1);
 974       Node *cmp1 = phase->transform( new (phase->C, 3) CmpLNode( in(1), phase->longcon(0) ) );
 975       Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) );
 976       Node *cmov1= phase->transform( new (phase->C, 4) CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
 977       // if( x >= hack_res ) x -= divisor;
 978       Node *sub  = phase->transform( new (phase->C, 3) SubLNode( x, divisor ) );
 979       Node *cmp2 = phase->transform( new (phase->C, 3) CmpLNode( x, cmov1 ) );
 980       Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) );
 981       // Convention is to not transform the return value of an Ideal
 982       // since Ideal is expected to return a modified 'this' or a new node.
 983       Node *cmov2= new (phase->C, 4) CMoveLNode(bol2, x, sub, TypeLong::LONG);
 984       // cmov2 is now the mod
 985 
 986       // Now remove the bogus extra edges used to keep things alive
 987       if (can_reshape) {
 988         phase->is_IterGVN()->remove_dead_node(hook);
 989       } else {
 990         hook->set_req(0, NULL);   // Just yank bogus edge during Parse phase
 991       }
 992       return cmov2;
 993     }
 994   }
 995 
 996   // Fell thru, the unroll case is not appropriate. Transform the modulo
 997   // into a long multiply/int multiply/subtract case
 998 
 999   // Cannot handle mod 0, and min_jint isn't handled by the transform
1000   if( con == 0 || con == min_jlong ) return NULL;
1001 
1002   // Get the absolute value of the constant; at this point, we can use this
1003   jlong pos_con = (con >= 0) ? con : -con;
1004 
1005   // integer Mod 1 is always 0
1006   if( pos_con == 1 ) return new (phase->C, 1) ConLNode(TypeLong::ZERO);
1007 
1008   int log2_con = -1;
1009 
1010   // If this is a power of two, they maybe we can mask it
1011   if( is_power_of_2_long(pos_con) ) {
1012     log2_con = log2_long(pos_con);
1013 
1014     const Type *dt = phase->type(in(1));
1015     const TypeLong *dtl = dt->isa_long();
1016 
1017     // See if this can be masked, if the dividend is non-negative
1018     if( dtl && dtl->_lo >= 0 )
1019       return ( new (phase->C, 3) AndLNode( in(1), phase->longcon( pos_con-1 ) ) );
1020   }
1021 
1022   // Save in(1) so that it cannot be changed or deleted
1023   hook->init_req(0, in(1));
1024 
1025   // Divide using the transform from DivI to MulL
1026   Node *result = transform_long_divide( phase, in(1), pos_con );
1027   if (result != NULL) {
1028     Node *divide = phase->transform(result);
1029 
1030     // Re-multiply, using a shift if this is a power of two
1031     Node *mult = NULL;
1032 
1033     if( log2_con >= 0 )
1034       mult = phase->transform( new (phase->C, 3) LShiftLNode( divide, phase->intcon( log2_con ) ) );
1035     else
1036       mult = phase->transform( new (phase->C, 3) MulLNode( divide, phase->longcon( pos_con ) ) );
1037 
1038     // Finally, subtract the multiplied divided value from the original
1039     result = new (phase->C, 3) SubLNode( in(1), mult );
1040   }
1041 
1042   // Now remove the bogus extra edges used to keep things alive
1043   if (can_reshape) {
1044     phase->is_IterGVN()->remove_dead_node(hook);
1045   } else {
1046     hook->set_req(0, NULL);       // Just yank bogus edge during Parse phase
1047   }
1048 
1049   // return the value
1050   return result;
1051 }
1052 
1053 //------------------------------Value------------------------------------------
1054 const Type *ModLNode::Value( PhaseTransform *phase ) const {
1055   // Either input is TOP ==> the result is TOP
1056   const Type *t1 = phase->type( in(1) );
1057   const Type *t2 = phase->type( in(2) );
1058   if( t1 == Type::TOP ) return Type::TOP;
1059   if( t2 == Type::TOP ) return Type::TOP;
1060 
1061   // We always generate the dynamic check for 0.
1062   // 0 MOD X is 0
1063   if( t1 == TypeLong::ZERO ) return TypeLong::ZERO;
1064   // X MOD X is 0
1065   if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO;
1066 
1067   // Either input is BOTTOM ==> the result is the local BOTTOM
1068   const Type *bot = bottom_type();
1069   if( (t1 == bot) || (t2 == bot) ||
1070       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1071     return bot;
1072 
1073   const TypeLong *i1 = t1->is_long();
1074   const TypeLong *i2 = t2->is_long();
1075   if( !i1->is_con() || !i2->is_con() ) {
1076     if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) )
1077       return TypeLong::POS;
1078     // If both numbers are not constants, we know little.
1079     return TypeLong::LONG;
1080   }
1081   // Mod by zero?  Throw exception at runtime!
1082   if( !i2->get_con() ) return TypeLong::POS;
1083 
1084   // We must be modulo'ing 2 float constants.
1085   // Check for min_jint % '-1', result is defined to be '0'.
1086   if( i1->get_con() == min_jlong && i2->get_con() == -1 )
1087     return TypeLong::ZERO;
1088 
1089   return TypeLong::make( i1->get_con() % i2->get_con() );
1090 }
1091 
1092 
1093 //=============================================================================
1094 //------------------------------Value------------------------------------------
1095 const Type *ModFNode::Value( PhaseTransform *phase ) const {
1096   // Either input is TOP ==> the result is TOP
1097   const Type *t1 = phase->type( in(1) );
1098   const Type *t2 = phase->type( in(2) );
1099   if( t1 == Type::TOP ) return Type::TOP;
1100   if( t2 == Type::TOP ) return Type::TOP;
1101 
1102   // Either input is BOTTOM ==> the result is the local BOTTOM
1103   const Type *bot = bottom_type();
1104   if( (t1 == bot) || (t2 == bot) ||
1105       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1106     return bot;
1107 
1108   // If either number is not a constant, we know nothing.
1109   if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) {
1110     return Type::FLOAT;         // note: x%x can be either NaN or 0
1111   }
1112 
1113   float f1 = t1->getf();
1114   float f2 = t2->getf();
1115   jint  x1 = jint_cast(f1);     // note:  *(int*)&f1, not just (int)f1
1116   jint  x2 = jint_cast(f2);
1117 
1118   // If either is a NaN, return an input NaN
1119   if (g_isnan(f1))    return t1;
1120   if (g_isnan(f2))    return t2;
1121 
1122   // If an operand is infinity or the divisor is +/- zero, punt.
1123   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint)
1124     return Type::FLOAT;
1125 
1126   // We must be modulo'ing 2 float constants.
1127   // Make sure that the sign of the fmod is equal to the sign of the dividend
1128   jint xr = jint_cast(fmod(f1, f2));
1129   if ((x1 ^ xr) < 0) {
1130     xr ^= min_jint;
1131   }
1132 
1133   return TypeF::make(jfloat_cast(xr));
1134 }
1135 
1136 
1137 //=============================================================================
1138 //------------------------------Value------------------------------------------
1139 const Type *ModDNode::Value( PhaseTransform *phase ) const {
1140   // Either input is TOP ==> the result is TOP
1141   const Type *t1 = phase->type( in(1) );
1142   const Type *t2 = phase->type( in(2) );
1143   if( t1 == Type::TOP ) return Type::TOP;
1144   if( t2 == Type::TOP ) return Type::TOP;
1145 
1146   // Either input is BOTTOM ==> the result is the local BOTTOM
1147   const Type *bot = bottom_type();
1148   if( (t1 == bot) || (t2 == bot) ||
1149       (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1150     return bot;
1151 
1152   // If either number is not a constant, we know nothing.
1153   if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) {
1154     return Type::DOUBLE;        // note: x%x can be either NaN or 0
1155   }
1156 
1157   double f1 = t1->getd();
1158   double f2 = t2->getd();
1159   jlong  x1 = jlong_cast(f1);   // note:  *(long*)&f1, not just (long)f1
1160   jlong  x2 = jlong_cast(f2);
1161 
1162   // If either is a NaN, return an input NaN
1163   if (g_isnan(f1))    return t1;
1164   if (g_isnan(f2))    return t2;
1165 
1166   // If an operand is infinity or the divisor is +/- zero, punt.
1167   if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong)
1168     return Type::DOUBLE;
1169 
1170   // We must be modulo'ing 2 double constants.
1171   // Make sure that the sign of the fmod is equal to the sign of the dividend
1172   jlong xr = jlong_cast(fmod(f1, f2));
1173   if ((x1 ^ xr) < 0) {
1174     xr ^= min_jlong;
1175   }
1176 
1177   return TypeD::make(jdouble_cast(xr));
1178 }
1179 
1180 //=============================================================================
1181 
1182 DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) {
1183   init_req(0, c);
1184   init_req(1, dividend);
1185   init_req(2, divisor);
1186 }
1187 
1188 //------------------------------make------------------------------------------
1189 DivModINode* DivModINode::make(Compile* C, Node* div_or_mod) {
1190   Node* n = div_or_mod;
1191   assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI,
1192          "only div or mod input pattern accepted");
1193 
1194   DivModINode* divmod = new (C, 3) DivModINode(n->in(0), n->in(1), n->in(2));
1195   Node*        dproj  = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
1196   Node*        mproj  = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
1197   return divmod;
1198 }
1199 
1200 //------------------------------make------------------------------------------
1201 DivModLNode* DivModLNode::make(Compile* C, Node* div_or_mod) {
1202   Node* n = div_or_mod;
1203   assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL,
1204          "only div or mod input pattern accepted");
1205 
1206   DivModLNode* divmod = new (C, 3) DivModLNode(n->in(0), n->in(1), n->in(2));
1207   Node*        dproj  = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num);
1208   Node*        mproj  = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num);
1209   return divmod;
1210 }
1211 
1212 //------------------------------match------------------------------------------
1213 // return result(s) along with their RegMask info
1214 Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) {
1215   uint ideal_reg = proj->ideal_reg();
1216   RegMask rm;
1217   if (proj->_con == div_proj_num) {
1218     rm = match->divI_proj_mask();
1219   } else {
1220     assert(proj->_con == mod_proj_num, "must be div or mod projection");
1221     rm = match->modI_proj_mask();
1222   }
1223   return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
1224 }
1225 
1226 
1227 //------------------------------match------------------------------------------
1228 // return result(s) along with their RegMask info
1229 Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) {
1230   uint ideal_reg = proj->ideal_reg();
1231   RegMask rm;
1232   if (proj->_con == div_proj_num) {
1233     rm = match->divL_proj_mask();
1234   } else {
1235     assert(proj->_con == mod_proj_num, "must be div or mod projection");
1236     rm = match->modL_proj_mask();
1237   }
1238   return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg);
1239 }