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