src/share/vm/opto/divnode.cpp

Thu, 02 Oct 2008 08:37:44 -0700

author
kvn
date
Thu, 02 Oct 2008 08:37:44 -0700
changeset 835
cc80376deb0c
parent 740
ab075d07f1ba
child 839
78c058bc5cdc
permissions
-rw-r--r--

6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
Summary: Fix loop's probability. Add optimizations to avoid spilling. Change InlineSmallCode to product flag.
Reviewed-by: never

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

mercurial