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