src/share/vm/opto/divnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8034812 Sdiff src/share/vm/opto

src/share/vm/opto/divnode.cpp

Print this page




  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 (phase->C) 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 (phase->C) RShiftINode(dividend, phase->intcon(N - 1)));
 147       // Mask sign bit to the low sign bits
 148       Node *round = phase->transform(new (phase->C) URShiftINode(sign, phase->intcon(N - l)));
 149       // Round up before shifting
 150       dividend = phase->transform(new (phase->C) AddINode(dividend, round));
 151     }
 152 
 153     // Shift for division
 154     q = new (phase->C) RShiftINode(dividend, phase->intcon(l));
 155 
 156     if (!d_pos) {
 157       q = new (phase->C) 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 (phase->C) ConvI2LNode(dividend));
 170 
 171       // Compute the high half of the dividend x magic multiplication
 172       Node *mul_hi = phase->transform(new (phase->C) MulLNode(dividend_long, magic));
 173 
 174       if (magic_const < 0) {
 175         mul_hi = phase->transform(new (phase->C) RShiftLNode(mul_hi, phase->intcon(N)));
 176         mul_hi = phase->transform(new (phase->C) 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 (phase->C) AddINode(dividend, mul_hi));
 182 
 183         // Shift over the (adjusted) mulhi
 184         if (shift_const != 0) {
 185           mul_hi = phase->transform(new (phase->C) 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 (phase->C) RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
 190         mul_hi = phase->transform(new (phase->C) 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 (phase->C) 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 (phase->C) 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;


 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 (phase->C) 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 (phase->C) Node(4);
 291 
 292   // u0 = u & 0xFFFFFFFF;  u1 = u >> 32;
 293   Node* u0 = phase->transform(new (phase->C) AndLNode(dividend, phase->longcon(0xFFFFFFFF)));
 294   Node* u1 = phase->transform(new (phase->C) 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 (phase->C) MulLNode(u0, v0));
 304 
 305   // t = u1*v0 + (w0 >> 32);
 306   Node* u1v0 = phase->transform(new (phase->C) MulLNode(u1, v0));
 307   Node* temp = phase->transform(new (phase->C) URShiftLNode(w0, phase->intcon(N / 2)));
 308   Node* t    = phase->transform(new (phase->C) AddLNode(u1v0, temp));
 309   hook->init_req(2, t);
 310 
 311   // w1 = t & 0xFFFFFFFF;
 312   Node* w1 = phase->transform(new (phase->C) AndLNode(t, phase->longcon(0xFFFFFFFF)));
 313   hook->init_req(3, w1);
 314 
 315   // w2 = t >> 32;
 316   Node* w2 = phase->transform(new (phase->C) RShiftLNode(t, phase->intcon(N / 2)));
 317 
 318   // w1 = u0*v1 + w1;
 319   Node* u0v1 = phase->transform(new (phase->C) MulLNode(u0, v1));
 320   w1         = phase->transform(new (phase->C) AddLNode(u0v1, w1));
 321 
 322   // return u1*v1 + w2 + (w1 >> 32);
 323   Node* u1v1  = phase->transform(new (phase->C) MulLNode(u1, v1));
 324   Node* temp1 = phase->transform(new (phase->C) AddLNode(u1v1, w2));
 325   Node* temp2 = phase->transform(new (phase->C) 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 (phase->C) 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 (phase->C) 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 (phase->C) RShiftLNode(dividend, phase->intcon(N - 1)));
 400       // Mask sign bit to the low sign bits
 401       Node *round = phase->transform(new (phase->C) URShiftLNode(sign, phase->intcon(N - l)));
 402       // Round up before shifting
 403       dividend = phase->transform(new (phase->C) AddLNode(dividend, round));
 404     }
 405 
 406     // Shift for division
 407     q = new (phase->C) RShiftLNode(dividend, phase->intcon(l));
 408 
 409     if (!d_pos) {
 410       q = new (phase->C) 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 (phase->C) AddLNode(dividend, mul_hi));
 431       }
 432 
 433       // Shift over the (adjusted) mulhi
 434       if (shift_const != 0) {
 435         mul_hi = phase->transform(new (phase->C) 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 (phase->C) 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 (phase->C) 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 


 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 (phase->C) 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.


 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 (phase->C) 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 (phase->C) 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 (phase->C) AndINode(x,divisor) );
 882         Node *xh = phase->transform( new (phase->C) RShiftINode(x,phase->intcon(k)) ); // Must be signed
 883         x = phase->transform( new (phase->C) 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 (phase->C) CmpINode( in(1), phase->intcon(0) ) );
 890       Node *bol1 = phase->transform( new (phase->C) BoolNode( cmp1, BoolTest::ge ) );
 891       Node *cmov1= phase->transform( new (phase->C) CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
 892       // if( x >= hack_res ) x -= divisor;
 893       Node *sub  = phase->transform( new (phase->C) SubINode( x, divisor ) );
 894       Node *cmp2 = phase->transform( new (phase->C) CmpINode( x, cmov1 ) );
 895       Node *bol2 = phase->transform( new (phase->C) 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 (phase->C) 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 (phase->C) 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 (phase->C) 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 (phase->C) LShiftINode( divide, phase->intcon( log2_con ) ) );
 950     else
 951       mult = phase->transform( new (phase->C) MulINode( divide, phase->intcon( pos_con ) ) );
 952 
 953     // Finally, subtract the multiplied divided value from the original
 954     result = new (phase->C) 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;


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 (phase->C) 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 (phase->C) AndLNode(x,divisor) );
1055         Node *xh = phase->transform( new (phase->C) RShiftLNode(x,phase->intcon(k)) ); // Must be signed
1056         x = phase->transform( new (phase->C) 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 (phase->C) CmpLNode( in(1), phase->longcon(0) ) );
1063       Node *bol1 = phase->transform( new (phase->C) BoolNode( cmp1, BoolTest::ge ) );
1064       Node *cmov1= phase->transform( new (phase->C) CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
1065       // if( x >= hack_res ) x -= divisor;
1066       Node *sub  = phase->transform( new (phase->C) SubLNode( x, divisor ) );
1067       Node *cmp2 = phase->transform( new (phase->C) CmpLNode( x, cmov1 ) );
1068       Node *bol2 = phase->transform( new (phase->C) 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 (phase->C) 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 (phase->C) 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 (phase->C) 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 (phase->C) LShiftLNode( divide, phase->intcon( log2_con ) ) );
1123     else
1124       mult = phase->transform( new (phase->C) MulLNode( divide, phase->longcon( pos_con ) ) );
1125 
1126     // Finally, subtract the multiplied divided value from the original
1127     result = new (phase->C) 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;


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 (C) DivModINode(n->in(0), n->in(1), n->in(2));
1283   Node*        dproj  = new (C) ProjNode(divmod, DivModNode::div_proj_num);
1284   Node*        mproj  = new (C) 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 (C) DivModLNode(n->in(0), n->in(1), n->in(2));
1295   Node*        dproj  = new (C) ProjNode(divmod, DivModNode::div_proj_num);
1296   Node*        mproj  = new (C) 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 (match->C)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 (match->C)MachProjNode(this, proj->_con, rm, ideal_reg);
1327 }


  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;


 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 


 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.


 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;


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;


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 }
src/share/vm/opto/divnode.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File