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