duke@435: /* twisti@1002: * Copyright 1997-2009 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Portions of code courtesy of Clifford Click duke@435: duke@435: // Optimization - Graph Style duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_divnode.cpp.incl" duke@435: #include duke@435: rasbold@580: //----------------------magic_int_divide_constants----------------------------- rasbold@580: // Compute magic multiplier and shift constant for converting a 32 bit divide rasbold@580: // by constant into a multiply/shift/add series. Return false if calculations rasbold@580: // fail. rasbold@580: // twisti@1040: // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with rasbold@580: // minor type name and parameter changes. rasbold@580: static bool magic_int_divide_constants(jint d, jint &M, jint &s) { rasbold@580: int32_t p; rasbold@580: uint32_t ad, anc, delta, q1, r1, q2, r2, t; rasbold@580: const uint32_t two31 = 0x80000000L; // 2**31. rasbold@580: rasbold@580: ad = ABS(d); rasbold@580: if (d == 0 || d == 1) return false; rasbold@580: t = two31 + ((uint32_t)d >> 31); rasbold@580: anc = t - 1 - t%ad; // Absolute value of nc. rasbold@580: p = 31; // Init. p. rasbold@580: q1 = two31/anc; // Init. q1 = 2**p/|nc|. rasbold@580: r1 = two31 - q1*anc; // Init. r1 = rem(2**p, |nc|). rasbold@580: q2 = two31/ad; // Init. q2 = 2**p/|d|. rasbold@580: r2 = two31 - q2*ad; // Init. r2 = rem(2**p, |d|). rasbold@580: do { rasbold@580: p = p + 1; rasbold@580: q1 = 2*q1; // Update q1 = 2**p/|nc|. rasbold@580: r1 = 2*r1; // Update r1 = rem(2**p, |nc|). rasbold@580: if (r1 >= anc) { // (Must be an unsigned rasbold@580: q1 = q1 + 1; // comparison here). rasbold@580: r1 = r1 - anc; rasbold@580: } rasbold@580: q2 = 2*q2; // Update q2 = 2**p/|d|. rasbold@580: r2 = 2*r2; // Update r2 = rem(2**p, |d|). rasbold@580: if (r2 >= ad) { // (Must be an unsigned rasbold@580: q2 = q2 + 1; // comparison here). rasbold@580: r2 = r2 - ad; rasbold@580: } rasbold@580: delta = ad - r2; rasbold@580: } while (q1 < delta || (q1 == delta && r1 == 0)); rasbold@580: rasbold@580: M = q2 + 1; rasbold@580: if (d < 0) M = -M; // Magic number and rasbold@580: s = p - 32; // shift amount to return. rasbold@580: rasbold@580: return true; rasbold@580: } rasbold@580: rasbold@580: //--------------------------transform_int_divide------------------------------- rasbold@580: // Convert a division by constant divisor into an alternate Ideal graph. rasbold@580: // Return NULL if no transformation occurs. rasbold@580: static Node *transform_int_divide( PhaseGVN *phase, Node *dividend, jint divisor ) { duke@435: duke@435: // Check for invalid divisors rasbold@580: assert( divisor != 0 && divisor != min_jint, rasbold@580: "bad divisor for transforming to long multiply" ); duke@435: duke@435: bool d_pos = divisor >= 0; rasbold@580: jint d = d_pos ? divisor : -divisor; duke@435: const int N = 32; duke@435: duke@435: // Result rasbold@580: Node *q = NULL; duke@435: duke@435: if (d == 1) { rasbold@580: // division by +/- 1 rasbold@580: if (!d_pos) { rasbold@580: // Just negate the value duke@435: q = new (phase->C, 3) SubINode(phase->intcon(0), dividend); duke@435: } rasbold@580: } else if ( is_power_of_2(d) ) { rasbold@580: // division by +/- a power of 2 duke@435: duke@435: // See if we can simply do a shift without rounding duke@435: bool needs_rounding = true; duke@435: const Type *dt = phase->type(dividend); duke@435: const TypeInt *dti = dt->isa_int(); rasbold@580: if (dti && dti->_lo >= 0) { rasbold@580: // we don't need to round a positive dividend duke@435: needs_rounding = false; rasbold@580: } else if( dividend->Opcode() == Op_AndI ) { rasbold@580: // An AND mask of sufficient size clears the low bits and rasbold@580: // I can avoid rounding. kvn@835: const TypeInt *andconi_t = phase->type( dividend->in(2) )->isa_int(); kvn@835: if( andconi_t && andconi_t->is_con() ) { kvn@835: jint andconi = andconi_t->get_con(); kvn@835: if( andconi < 0 && is_power_of_2(-andconi) && (-andconi) >= d ) { kvn@835: dividend = dividend->in(1); kvn@835: needs_rounding = false; kvn@835: } duke@435: } duke@435: } duke@435: duke@435: // Add rounding to the shift to handle the sign bit rasbold@580: int l = log2_intptr(d-1)+1; rasbold@580: if (needs_rounding) { rasbold@580: // Divide-by-power-of-2 can be made into a shift, but you have to do rasbold@580: // more math for the rounding. You need to add 0 for positive rasbold@580: // numbers, and "i-1" for negative numbers. Example: i=4, so the rasbold@580: // shift is by 2. You need to add 3 to negative dividends and 0 to rasbold@580: // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1, rasbold@580: // (-2+3)>>2 becomes 0, etc. rasbold@580: rasbold@580: // Compute 0 or -1, based on sign bit rasbold@580: Node *sign = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N - 1))); rasbold@580: // Mask sign bit to the low sign bits rasbold@580: Node *round = phase->transform(new (phase->C, 3) URShiftINode(sign, phase->intcon(N - l))); rasbold@580: // Round up before shifting rasbold@580: dividend = phase->transform(new (phase->C, 3) AddINode(dividend, round)); duke@435: } duke@435: rasbold@580: // Shift for division duke@435: q = new (phase->C, 3) RShiftINode(dividend, phase->intcon(l)); duke@435: rasbold@580: if (!d_pos) { duke@435: q = new (phase->C, 3) SubINode(phase->intcon(0), phase->transform(q)); rasbold@580: } rasbold@580: } else { rasbold@580: // Attempt the jint constant divide -> multiply transform found in rasbold@580: // "Division by Invariant Integers using Multiplication" rasbold@580: // by Granlund and Montgomery rasbold@580: // See also "Hacker's Delight", chapter 10 by Warren. rasbold@580: rasbold@580: jint magic_const; rasbold@580: jint shift_const; rasbold@580: if (magic_int_divide_constants(d, magic_const, shift_const)) { rasbold@580: Node *magic = phase->longcon(magic_const); rasbold@580: Node *dividend_long = phase->transform(new (phase->C, 2) ConvI2LNode(dividend)); rasbold@580: rasbold@580: // Compute the high half of the dividend x magic multiplication rasbold@580: Node *mul_hi = phase->transform(new (phase->C, 3) MulLNode(dividend_long, magic)); rasbold@580: rasbold@580: if (magic_const < 0) { rasbold@580: mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(N))); rasbold@580: mul_hi = phase->transform(new (phase->C, 2) ConvL2INode(mul_hi)); rasbold@580: rasbold@580: // The magic multiplier is too large for a 32 bit constant. We've adjusted rasbold@580: // it down by 2^32, but have to add 1 dividend back in after the multiplication. rasbold@580: // This handles the "overflow" case described by Granlund and Montgomery. rasbold@580: mul_hi = phase->transform(new (phase->C, 3) AddINode(dividend, mul_hi)); rasbold@580: rasbold@580: // Shift over the (adjusted) mulhi rasbold@580: if (shift_const != 0) { rasbold@580: mul_hi = phase->transform(new (phase->C, 3) RShiftINode(mul_hi, phase->intcon(shift_const))); rasbold@580: } rasbold@580: } else { rasbold@580: // No add is required, we can merge the shifts together. rasbold@580: mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(N + shift_const))); rasbold@580: mul_hi = phase->transform(new (phase->C, 2) ConvL2INode(mul_hi)); rasbold@580: } rasbold@580: rasbold@580: // Get a 0 or -1 from the sign of the dividend. rasbold@580: Node *addend0 = mul_hi; rasbold@580: Node *addend1 = phase->transform(new (phase->C, 3) RShiftINode(dividend, phase->intcon(N-1))); rasbold@580: rasbold@580: // If the divisor is negative, swap the order of the input addends; rasbold@580: // this has the effect of negating the quotient. rasbold@580: if (!d_pos) { rasbold@580: Node *temp = addend0; addend0 = addend1; addend1 = temp; rasbold@580: } rasbold@580: rasbold@580: // Adjust the final quotient by subtracting -1 (adding 1) rasbold@580: // from the mul_hi. rasbold@580: q = new (phase->C, 3) SubINode(addend0, addend1); rasbold@580: } duke@435: } duke@435: rasbold@580: return q; rasbold@580: } duke@435: rasbold@580: //---------------------magic_long_divide_constants----------------------------- rasbold@580: // Compute magic multiplier and shift constant for converting a 64 bit divide rasbold@580: // by constant into a multiply/shift/add series. Return false if calculations rasbold@580: // fail. rasbold@580: // twisti@1040: // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with rasbold@580: // minor type name and parameter changes. Adjusted to 64 bit word width. rasbold@580: static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) { rasbold@580: int64_t p; rasbold@580: uint64_t ad, anc, delta, q1, r1, q2, r2, t; rasbold@580: const uint64_t two63 = 0x8000000000000000LL; // 2**63. rasbold@580: rasbold@580: ad = ABS(d); rasbold@580: if (d == 0 || d == 1) return false; rasbold@580: t = two63 + ((uint64_t)d >> 63); rasbold@580: anc = t - 1 - t%ad; // Absolute value of nc. rasbold@580: p = 63; // Init. p. rasbold@580: q1 = two63/anc; // Init. q1 = 2**p/|nc|. rasbold@580: r1 = two63 - q1*anc; // Init. r1 = rem(2**p, |nc|). rasbold@580: q2 = two63/ad; // Init. q2 = 2**p/|d|. rasbold@580: r2 = two63 - q2*ad; // Init. r2 = rem(2**p, |d|). rasbold@580: do { rasbold@580: p = p + 1; rasbold@580: q1 = 2*q1; // Update q1 = 2**p/|nc|. rasbold@580: r1 = 2*r1; // Update r1 = rem(2**p, |nc|). rasbold@580: if (r1 >= anc) { // (Must be an unsigned rasbold@580: q1 = q1 + 1; // comparison here). rasbold@580: r1 = r1 - anc; rasbold@580: } rasbold@580: q2 = 2*q2; // Update q2 = 2**p/|d|. rasbold@580: r2 = 2*r2; // Update r2 = rem(2**p, |d|). rasbold@580: if (r2 >= ad) { // (Must be an unsigned rasbold@580: q2 = q2 + 1; // comparison here). rasbold@580: r2 = r2 - ad; rasbold@580: } rasbold@580: delta = ad - r2; rasbold@580: } while (q1 < delta || (q1 == delta && r1 == 0)); rasbold@580: rasbold@580: M = q2 + 1; rasbold@580: if (d < 0) M = -M; // Magic number and rasbold@580: s = p - 64; // shift amount to return. rasbold@580: rasbold@580: return true; rasbold@580: } rasbold@580: rasbold@580: //---------------------long_by_long_mulhi-------------------------------------- rasbold@580: // Generate ideal node graph for upper half of a 64 bit x 64 bit multiplication twisti@1002: static Node* long_by_long_mulhi(PhaseGVN* phase, Node* dividend, jlong magic_const) { rasbold@580: // If the architecture supports a 64x64 mulhi, there is rasbold@580: // no need to synthesize it in ideal nodes. rasbold@580: if (Matcher::has_match_rule(Op_MulHiL)) { twisti@1002: Node* v = phase->longcon(magic_const); rasbold@580: return new (phase->C, 3) MulHiLNode(dividend, v); duke@435: } duke@435: twisti@1002: // Taken from Hacker's Delight, Fig. 8-2. Multiply high signed. twisti@1002: // (http://www.hackersdelight.org/HDcode/mulhs.c) twisti@1002: // twisti@1002: // int mulhs(int u, int v) { twisti@1002: // unsigned u0, v0, w0; twisti@1002: // int u1, v1, w1, w2, t; twisti@1002: // twisti@1002: // u0 = u & 0xFFFF; u1 = u >> 16; twisti@1002: // v0 = v & 0xFFFF; v1 = v >> 16; twisti@1002: // w0 = u0*v0; twisti@1002: // t = u1*v0 + (w0 >> 16); twisti@1002: // w1 = t & 0xFFFF; twisti@1002: // w2 = t >> 16; twisti@1002: // w1 = u0*v1 + w1; twisti@1002: // return u1*v1 + w2 + (w1 >> 16); twisti@1002: // } twisti@1002: // twisti@1002: // Note: The version above is for 32x32 multiplications, while the twisti@1002: // following inline comments are adapted to 64x64. twisti@1002: rasbold@580: const int N = 64; duke@435: twisti@1002: // u0 = u & 0xFFFFFFFF; u1 = u >> 32; twisti@1002: Node* u0 = phase->transform(new (phase->C, 3) AndLNode(dividend, phase->longcon(0xFFFFFFFF))); twisti@1002: Node* u1 = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N / 2))); rasbold@580: twisti@1002: // v0 = v & 0xFFFFFFFF; v1 = v >> 32; twisti@1002: Node* v0 = phase->longcon(magic_const & 0xFFFFFFFF); twisti@1002: Node* v1 = phase->longcon(magic_const >> (N / 2)); rasbold@580: twisti@1002: // w0 = u0*v0; twisti@1002: Node* w0 = phase->transform(new (phase->C, 3) MulLNode(u0, v0)); rasbold@580: twisti@1002: // t = u1*v0 + (w0 >> 32); twisti@1002: Node* u1v0 = phase->transform(new (phase->C, 3) MulLNode(u1, v0)); twisti@1002: Node* temp = phase->transform(new (phase->C, 3) URShiftLNode(w0, phase->intcon(N / 2))); twisti@1002: Node* t = phase->transform(new (phase->C, 3) AddLNode(u1v0, temp)); rasbold@729: twisti@1002: // w1 = t & 0xFFFFFFFF; twisti@1002: Node* w1 = new (phase->C, 3) AndLNode(t, phase->longcon(0xFFFFFFFF)); rasbold@729: twisti@1002: // w2 = t >> 32; twisti@1002: Node* w2 = new (phase->C, 3) RShiftLNode(t, phase->intcon(N / 2)); rasbold@580: twisti@1002: // 6732154: Construct both w1 and w2 before transforming, so t twisti@1002: // doesn't go dead prematurely. twisti@1002: w1 = phase->transform(w1); twisti@1002: w2 = phase->transform(w2); twisti@1002: twisti@1002: // w1 = u0*v1 + w1; twisti@1002: Node* u0v1 = phase->transform(new (phase->C, 3) MulLNode(u0, v1)); twisti@1002: w1 = phase->transform(new (phase->C, 3) AddLNode(u0v1, w1)); twisti@1002: twisti@1002: // return u1*v1 + w2 + (w1 >> 32); twisti@1002: Node* u1v1 = phase->transform(new (phase->C, 3) MulLNode(u1, v1)); twisti@1002: Node* temp1 = phase->transform(new (phase->C, 3) AddLNode(u1v1, w2)); twisti@1002: Node* temp2 = phase->transform(new (phase->C, 3) RShiftLNode(w1, phase->intcon(N / 2))); twisti@1002: twisti@1002: return new (phase->C, 3) AddLNode(temp1, temp2); rasbold@580: } rasbold@580: rasbold@580: rasbold@580: //--------------------------transform_long_divide------------------------------ rasbold@580: // Convert a division by constant divisor into an alternate Ideal graph. rasbold@580: // Return NULL if no transformation occurs. rasbold@580: static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) { rasbold@580: // Check for invalid divisors rasbold@580: assert( divisor != 0L && divisor != min_jlong, rasbold@580: "bad divisor for transforming to long multiply" ); rasbold@580: rasbold@580: bool d_pos = divisor >= 0; rasbold@580: jlong d = d_pos ? divisor : -divisor; rasbold@580: const int N = 64; rasbold@580: rasbold@580: // Result rasbold@580: Node *q = NULL; rasbold@580: rasbold@580: if (d == 1) { rasbold@580: // division by +/- 1 rasbold@580: if (!d_pos) { rasbold@580: // Just negate the value rasbold@580: q = new (phase->C, 3) SubLNode(phase->longcon(0), dividend); rasbold@580: } rasbold@580: } else if ( is_power_of_2_long(d) ) { rasbold@580: rasbold@580: // division by +/- a power of 2 rasbold@580: rasbold@580: // See if we can simply do a shift without rounding rasbold@580: bool needs_rounding = true; rasbold@580: const Type *dt = phase->type(dividend); rasbold@580: const TypeLong *dtl = dt->isa_long(); rasbold@580: rasbold@580: if (dtl && dtl->_lo > 0) { rasbold@580: // we don't need to round a positive dividend rasbold@580: needs_rounding = false; rasbold@580: } else if( dividend->Opcode() == Op_AndL ) { rasbold@580: // An AND mask of sufficient size clears the low bits and rasbold@580: // I can avoid rounding. kvn@835: const TypeLong *andconl_t = phase->type( dividend->in(2) )->isa_long(); kvn@835: if( andconl_t && andconl_t->is_con() ) { kvn@835: jlong andconl = andconl_t->get_con(); kvn@835: if( andconl < 0 && is_power_of_2_long(-andconl) && (-andconl) >= d ) { kvn@835: dividend = dividend->in(1); kvn@835: needs_rounding = false; kvn@835: } rasbold@580: } rasbold@580: } rasbold@580: rasbold@580: // Add rounding to the shift to handle the sign bit rasbold@580: int l = log2_long(d-1)+1; rasbold@580: if (needs_rounding) { rasbold@580: // Divide-by-power-of-2 can be made into a shift, but you have to do rasbold@580: // more math for the rounding. You need to add 0 for positive rasbold@580: // numbers, and "i-1" for negative numbers. Example: i=4, so the rasbold@580: // shift is by 2. You need to add 3 to negative dividends and 0 to rasbold@580: // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1, rasbold@580: // (-2+3)>>2 becomes 0, etc. rasbold@580: rasbold@580: // Compute 0 or -1, based on sign bit rasbold@580: Node *sign = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N - 1))); rasbold@580: // Mask sign bit to the low sign bits rasbold@580: Node *round = phase->transform(new (phase->C, 3) URShiftLNode(sign, phase->intcon(N - l))); rasbold@580: // Round up before shifting rasbold@580: dividend = phase->transform(new (phase->C, 3) AddLNode(dividend, round)); rasbold@580: } rasbold@580: rasbold@580: // Shift for division rasbold@580: q = new (phase->C, 3) RShiftLNode(dividend, phase->intcon(l)); rasbold@580: rasbold@580: if (!d_pos) { rasbold@580: q = new (phase->C, 3) SubLNode(phase->longcon(0), phase->transform(q)); rasbold@580: } rasbold@580: } else { rasbold@580: // Attempt the jlong constant divide -> multiply transform found in rasbold@580: // "Division by Invariant Integers using Multiplication" rasbold@580: // by Granlund and Montgomery rasbold@580: // See also "Hacker's Delight", chapter 10 by Warren. rasbold@580: rasbold@580: jlong magic_const; rasbold@580: jint shift_const; rasbold@580: if (magic_long_divide_constants(d, magic_const, shift_const)) { rasbold@580: // Compute the high half of the dividend x magic multiplication rasbold@580: Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const)); rasbold@580: rasbold@580: // The high half of the 128-bit multiply is computed. rasbold@580: if (magic_const < 0) { rasbold@580: // The magic multiplier is too large for a 64 bit constant. We've adjusted rasbold@580: // it down by 2^64, but have to add 1 dividend back in after the multiplication. rasbold@580: // This handles the "overflow" case described by Granlund and Montgomery. rasbold@580: mul_hi = phase->transform(new (phase->C, 3) AddLNode(dividend, mul_hi)); rasbold@580: } rasbold@580: rasbold@580: // Shift over the (adjusted) mulhi rasbold@580: if (shift_const != 0) { rasbold@580: mul_hi = phase->transform(new (phase->C, 3) RShiftLNode(mul_hi, phase->intcon(shift_const))); rasbold@580: } rasbold@580: rasbold@580: // Get a 0 or -1 from the sign of the dividend. rasbold@580: Node *addend0 = mul_hi; rasbold@580: Node *addend1 = phase->transform(new (phase->C, 3) RShiftLNode(dividend, phase->intcon(N-1))); rasbold@580: rasbold@580: // If the divisor is negative, swap the order of the input addends; rasbold@580: // this has the effect of negating the quotient. rasbold@580: if (!d_pos) { rasbold@580: Node *temp = addend0; addend0 = addend1; addend1 = temp; rasbold@580: } rasbold@580: rasbold@580: // Adjust the final quotient by subtracting -1 (adding 1) rasbold@580: // from the mul_hi. rasbold@580: q = new (phase->C, 3) SubLNode(addend0, addend1); rasbold@580: } duke@435: } duke@435: rasbold@580: return q; duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: // If the divisor is 1, we are an identity on the dividend. duke@435: Node *DivINode::Identity( PhaseTransform *phase ) { duke@435: return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: // Divides can be changed to multiplies and/or shifts duke@435: Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if (in(0) && remove_dead_region(phase, can_reshape)) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: const Type *t = phase->type( in(2) ); duke@435: if( t == TypeInt::ONE ) // Identity? duke@435: return NULL; // Skip it duke@435: duke@435: const TypeInt *ti = t->isa_int(); duke@435: if( !ti ) return NULL; duke@435: if( !ti->is_con() ) return NULL; rasbold@580: jint i = ti->get_con(); // Get divisor duke@435: duke@435: if (i == 0) return NULL; // Dividing by zero constant does not idealize duke@435: duke@435: set_req(0,NULL); // Dividing by a not-zero constant; no faulting duke@435: duke@435: // Dividing by MININT does not optimize as a power-of-2 shift. duke@435: if( i == min_jint ) return NULL; duke@435: rasbold@580: return transform_int_divide( phase, in(1), i ); duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A DivINode divides its inputs. The third input is a Control input, used to duke@435: // prevent hoisting the divide above an unsafe test. duke@435: const Type *DivINode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // x/x == 1 since we always generate the dynamic divisor check for 0. duke@435: if( phase->eqv( in(1), in(2) ) ) duke@435: return TypeInt::ONE; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: // Divide the two numbers. We approximate. duke@435: // If divisor is a constant and not zero duke@435: const TypeInt *i1 = t1->is_int(); duke@435: const TypeInt *i2 = t2->is_int(); duke@435: int widen = MAX2(i1->_widen, i2->_widen); duke@435: duke@435: if( i2->is_con() && i2->get_con() != 0 ) { duke@435: int32 d = i2->get_con(); // Divisor duke@435: jint lo, hi; duke@435: if( d >= 0 ) { duke@435: lo = i1->_lo/d; duke@435: hi = i1->_hi/d; duke@435: } else { duke@435: if( d == -1 && i1->_lo == min_jint ) { duke@435: // 'min_jint/-1' throws arithmetic exception during compilation duke@435: lo = min_jint; duke@435: // do not support holes, 'hi' must go to either min_jint or max_jint: duke@435: // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint] duke@435: hi = i1->_hi == min_jint ? min_jint : max_jint; duke@435: } else { duke@435: lo = i1->_hi/d; duke@435: hi = i1->_lo/d; duke@435: } duke@435: } duke@435: return TypeInt::make(lo, hi, widen); duke@435: } duke@435: duke@435: // If the dividend is a constant duke@435: if( i1->is_con() ) { duke@435: int32 d = i1->get_con(); duke@435: if( d < 0 ) { duke@435: if( d == min_jint ) { duke@435: // (-min_jint) == min_jint == (min_jint / -1) duke@435: return TypeInt::make(min_jint, max_jint/2 + 1, widen); duke@435: } else { duke@435: return TypeInt::make(d, -d, widen); duke@435: } duke@435: } duke@435: return TypeInt::make(-d, d, widen); duke@435: } duke@435: duke@435: // Otherwise we give up all hope duke@435: return TypeInt::INT; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Identity--------------------------------------- duke@435: // If the divisor is 1, we are an identity on the dividend. duke@435: Node *DivLNode::Identity( PhaseTransform *phase ) { duke@435: return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: // Dividing by a power of 2 is a shift. duke@435: Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) { duke@435: if (in(0) && remove_dead_region(phase, can_reshape)) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: const Type *t = phase->type( in(2) ); rasbold@580: if( t == TypeLong::ONE ) // Identity? duke@435: return NULL; // Skip it duke@435: rasbold@580: const TypeLong *tl = t->isa_long(); rasbold@580: if( !tl ) return NULL; rasbold@580: if( !tl->is_con() ) return NULL; rasbold@580: jlong l = tl->get_con(); // Get divisor rasbold@580: rasbold@580: if (l == 0) return NULL; // Dividing by zero constant does not idealize rasbold@580: rasbold@580: set_req(0,NULL); // Dividing by a not-zero constant; no faulting duke@435: duke@435: // Dividing by MININT does not optimize as a power-of-2 shift. rasbold@580: if( l == min_jlong ) return NULL; duke@435: rasbold@580: return transform_long_divide( phase, in(1), l ); duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: // A DivLNode divides its inputs. The third input is a Control input, used to duke@435: // prevent hoisting the divide above an unsafe test. duke@435: const Type *DivLNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // x/x == 1 since we always generate the dynamic divisor check for 0. duke@435: if( phase->eqv( in(1), in(2) ) ) duke@435: return TypeLong::ONE; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: // Divide the two numbers. We approximate. duke@435: // If divisor is a constant and not zero duke@435: const TypeLong *i1 = t1->is_long(); duke@435: const TypeLong *i2 = t2->is_long(); duke@435: int widen = MAX2(i1->_widen, i2->_widen); duke@435: duke@435: if( i2->is_con() && i2->get_con() != 0 ) { duke@435: jlong d = i2->get_con(); // Divisor duke@435: jlong lo, hi; duke@435: if( d >= 0 ) { duke@435: lo = i1->_lo/d; duke@435: hi = i1->_hi/d; duke@435: } else { duke@435: if( d == CONST64(-1) && i1->_lo == min_jlong ) { duke@435: // 'min_jlong/-1' throws arithmetic exception during compilation duke@435: lo = min_jlong; duke@435: // do not support holes, 'hi' must go to either min_jlong or max_jlong: duke@435: // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong] duke@435: hi = i1->_hi == min_jlong ? min_jlong : max_jlong; duke@435: } else { duke@435: lo = i1->_hi/d; duke@435: hi = i1->_lo/d; duke@435: } duke@435: } duke@435: return TypeLong::make(lo, hi, widen); duke@435: } duke@435: duke@435: // If the dividend is a constant duke@435: if( i1->is_con() ) { duke@435: jlong d = i1->get_con(); duke@435: if( d < 0 ) { duke@435: if( d == min_jlong ) { duke@435: // (-min_jlong) == min_jlong == (min_jlong / -1) duke@435: return TypeLong::make(min_jlong, max_jlong/2 + 1, widen); duke@435: } else { duke@435: return TypeLong::make(d, -d, widen); duke@435: } duke@435: } duke@435: return TypeLong::make(-d, d, widen); duke@435: } duke@435: duke@435: // Otherwise we give up all hope duke@435: return TypeLong::LONG; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // An DivFNode divides its inputs. The third input is a Control input, used to duke@435: // prevent hoisting the divide above an unsafe test. duke@435: const Type *DivFNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: // x/x == 1, we ignore 0/0. duke@435: // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) jrose@566: // Does not work for variables because of NaN's duke@435: if( phase->eqv( in(1), in(2) ) && t1->base() == Type::FloatCon) duke@435: if (!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) // could be negative ZERO or NaN duke@435: return TypeF::ONE; duke@435: duke@435: if( t2 == TypeF::ONE ) duke@435: return t1; duke@435: duke@435: // If divisor is a constant and not zero, divide them numbers duke@435: if( t1->base() == Type::FloatCon && duke@435: t2->base() == Type::FloatCon && duke@435: t2->getf() != 0.0 ) // could be negative zero duke@435: return TypeF::make( t1->getf()/t2->getf() ); duke@435: duke@435: // If the dividend is a constant zero duke@435: // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) duke@435: // Test TypeF::ZERO is not sufficient as it could be negative zero duke@435: duke@435: if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 ) duke@435: return TypeF::ZERO; duke@435: duke@435: // Otherwise we give up all hope duke@435: return Type::FLOAT; duke@435: } duke@435: duke@435: //------------------------------isA_Copy--------------------------------------- duke@435: // Dividing by self is 1. duke@435: // If the divisor is 1, we are an identity on the dividend. duke@435: Node *DivFNode::Identity( PhaseTransform *phase ) { duke@435: return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this; duke@435: } duke@435: duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if (in(0) && remove_dead_region(phase, can_reshape)) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t2 == TypeF::ONE ) // Identity? duke@435: return NULL; // Skip it duke@435: duke@435: const TypeF *tf = t2->isa_float_constant(); duke@435: if( !tf ) return NULL; duke@435: if( tf->base() != Type::FloatCon ) return NULL; duke@435: duke@435: // Check for out of range values duke@435: if( tf->is_nan() || !tf->is_finite() ) return NULL; duke@435: duke@435: // Get the value duke@435: float f = tf->getf(); duke@435: int exp; duke@435: duke@435: // Only for special case of dividing by a power of 2 duke@435: if( frexp((double)f, &exp) != 0.5 ) return NULL; duke@435: duke@435: // Limit the range of acceptable exponents duke@435: if( exp < -126 || exp > 126 ) return NULL; duke@435: duke@435: // Compute the reciprocal duke@435: float reciprocal = ((float)1.0) / f; duke@435: duke@435: assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" ); duke@435: duke@435: // return multiplication by the reciprocal duke@435: return (new (phase->C, 3) MulFNode(in(1), phase->makecon(TypeF::make(reciprocal)))); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: // An DivDNode divides its inputs. The third input is a Control input, used to jrose@566: // prevent hoisting the divide above an unsafe test. duke@435: const Type *DivDNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: // x/x == 1, we ignore 0/0. duke@435: // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) duke@435: // Does not work for variables because of NaN's duke@435: if( phase->eqv( in(1), in(2) ) && t1->base() == Type::DoubleCon) duke@435: if (!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) // could be negative ZERO or NaN duke@435: return TypeD::ONE; duke@435: duke@435: if( t2 == TypeD::ONE ) duke@435: return t1; duke@435: rasbold@839: #if defined(IA32) rasbold@839: if (!phase->C->method()->is_strict()) rasbold@839: // Can't trust native compilers to properly fold strict double rasbold@839: // division with round-to-zero on this platform. rasbold@839: #endif rasbold@839: { rasbold@839: // If divisor is a constant and not zero, divide them numbers rasbold@839: if( t1->base() == Type::DoubleCon && rasbold@839: t2->base() == Type::DoubleCon && rasbold@839: t2->getd() != 0.0 ) // could be negative zero rasbold@839: return TypeD::make( t1->getd()/t2->getd() ); rasbold@839: } duke@435: duke@435: // If the dividend is a constant zero duke@435: // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) duke@435: // Test TypeF::ZERO is not sufficient as it could be negative zero duke@435: if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 ) duke@435: return TypeD::ZERO; duke@435: duke@435: // Otherwise we give up all hope duke@435: return Type::DOUBLE; duke@435: } duke@435: duke@435: duke@435: //------------------------------isA_Copy--------------------------------------- duke@435: // Dividing by self is 1. duke@435: // If the divisor is 1, we are an identity on the dividend. duke@435: Node *DivDNode::Identity( PhaseTransform *phase ) { duke@435: return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this; duke@435: } duke@435: duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: if (in(0) && remove_dead_region(phase, can_reshape)) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t2 == TypeD::ONE ) // Identity? duke@435: return NULL; // Skip it duke@435: duke@435: const TypeD *td = t2->isa_double_constant(); duke@435: if( !td ) return NULL; duke@435: if( td->base() != Type::DoubleCon ) return NULL; duke@435: duke@435: // Check for out of range values duke@435: if( td->is_nan() || !td->is_finite() ) return NULL; duke@435: duke@435: // Get the value duke@435: double d = td->getd(); duke@435: int exp; duke@435: duke@435: // Only for special case of dividing by a power of 2 duke@435: if( frexp(d, &exp) != 0.5 ) return NULL; duke@435: duke@435: // Limit the range of acceptable exponents duke@435: if( exp < -1021 || exp > 1022 ) return NULL; duke@435: duke@435: // Compute the reciprocal duke@435: double reciprocal = 1.0 / d; duke@435: duke@435: assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" ); duke@435: duke@435: // return multiplication by the reciprocal duke@435: return (new (phase->C, 3) MulDNode(in(1), phase->makecon(TypeD::make(reciprocal)))); duke@435: } duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Check for dead control input kvn@740: if( in(0) && remove_dead_region(phase, can_reshape) ) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: // Get the modulus duke@435: const Type *t = phase->type( in(2) ); duke@435: if( t == Type::TOP ) return NULL; duke@435: const TypeInt *ti = t->is_int(); duke@435: duke@435: // Check for useless control input duke@435: // Check for excluding mod-zero case duke@435: if( in(0) && (ti->_hi < 0 || ti->_lo > 0) ) { duke@435: set_req(0, NULL); // Yank control input duke@435: return this; duke@435: } duke@435: duke@435: // See if we are MOD'ing by 2^k or 2^k-1. duke@435: if( !ti->is_con() ) return NULL; duke@435: jint con = ti->get_con(); duke@435: duke@435: Node *hook = new (phase->C, 1) Node(1); duke@435: duke@435: // First, special check for modulo 2^k-1 duke@435: if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) { duke@435: uint k = exact_log2(con+1); // Extract k duke@435: duke@435: // Basic algorithm by David Detlefs. See fastmod_int.java for gory details. duke@435: 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*/}; duke@435: int trip_count = 1; duke@435: if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k]; duke@435: duke@435: // If the unroll factor is not too large, and if conditional moves are duke@435: // ok, then use this case duke@435: if( trip_count <= 5 && ConditionalMoveLimit != 0 ) { duke@435: Node *x = in(1); // Value being mod'd duke@435: Node *divisor = in(2); // Also is mask duke@435: duke@435: hook->init_req(0, x); // Add a use to x to prevent him from dying duke@435: // Generate code to reduce X rapidly to nearly 2^k-1. duke@435: for( int i = 0; i < trip_count; i++ ) { rasbold@580: Node *xl = phase->transform( new (phase->C, 3) AndINode(x,divisor) ); rasbold@580: Node *xh = phase->transform( new (phase->C, 3) RShiftINode(x,phase->intcon(k)) ); // Must be signed rasbold@580: x = phase->transform( new (phase->C, 3) AddINode(xh,xl) ); rasbold@580: hook->set_req(0, x); duke@435: } duke@435: duke@435: // Generate sign-fixup code. Was original value positive? duke@435: // int hack_res = (i >= 0) ? divisor : 1; duke@435: Node *cmp1 = phase->transform( new (phase->C, 3) CmpINode( in(1), phase->intcon(0) ) ); duke@435: Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) ); duke@435: Node *cmov1= phase->transform( new (phase->C, 4) CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) ); duke@435: // if( x >= hack_res ) x -= divisor; duke@435: Node *sub = phase->transform( new (phase->C, 3) SubINode( x, divisor ) ); duke@435: Node *cmp2 = phase->transform( new (phase->C, 3) CmpINode( x, cmov1 ) ); duke@435: Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) ); duke@435: // Convention is to not transform the return value of an Ideal duke@435: // since Ideal is expected to return a modified 'this' or a new node. duke@435: Node *cmov2= new (phase->C, 4) CMoveINode(bol2, x, sub, TypeInt::INT); duke@435: // cmov2 is now the mod duke@435: duke@435: // Now remove the bogus extra edges used to keep things alive duke@435: if (can_reshape) { duke@435: phase->is_IterGVN()->remove_dead_node(hook); duke@435: } else { duke@435: hook->set_req(0, NULL); // Just yank bogus edge during Parse phase duke@435: } duke@435: return cmov2; duke@435: } duke@435: } duke@435: duke@435: // Fell thru, the unroll case is not appropriate. Transform the modulo duke@435: // into a long multiply/int multiply/subtract case duke@435: duke@435: // Cannot handle mod 0, and min_jint isn't handled by the transform duke@435: if( con == 0 || con == min_jint ) return NULL; duke@435: duke@435: // Get the absolute value of the constant; at this point, we can use this duke@435: jint pos_con = (con >= 0) ? con : -con; duke@435: duke@435: // integer Mod 1 is always 0 duke@435: if( pos_con == 1 ) return new (phase->C, 1) ConINode(TypeInt::ZERO); duke@435: duke@435: int log2_con = -1; duke@435: duke@435: // If this is a power of two, they maybe we can mask it duke@435: if( is_power_of_2(pos_con) ) { duke@435: log2_con = log2_intptr((intptr_t)pos_con); duke@435: duke@435: const Type *dt = phase->type(in(1)); duke@435: const TypeInt *dti = dt->isa_int(); duke@435: duke@435: // See if this can be masked, if the dividend is non-negative duke@435: if( dti && dti->_lo >= 0 ) duke@435: return ( new (phase->C, 3) AndINode( in(1), phase->intcon( pos_con-1 ) ) ); duke@435: } duke@435: duke@435: // Save in(1) so that it cannot be changed or deleted duke@435: hook->init_req(0, in(1)); duke@435: duke@435: // Divide using the transform from DivI to MulL rasbold@580: Node *result = transform_int_divide( phase, in(1), pos_con ); rasbold@580: if (result != NULL) { rasbold@580: Node *divide = phase->transform(result); duke@435: rasbold@580: // Re-multiply, using a shift if this is a power of two rasbold@580: Node *mult = NULL; duke@435: rasbold@580: if( log2_con >= 0 ) rasbold@580: mult = phase->transform( new (phase->C, 3) LShiftINode( divide, phase->intcon( log2_con ) ) ); rasbold@580: else rasbold@580: mult = phase->transform( new (phase->C, 3) MulINode( divide, phase->intcon( pos_con ) ) ); duke@435: rasbold@580: // Finally, subtract the multiplied divided value from the original rasbold@580: result = new (phase->C, 3) SubINode( in(1), mult ); rasbold@580: } duke@435: duke@435: // Now remove the bogus extra edges used to keep things alive duke@435: if (can_reshape) { duke@435: phase->is_IterGVN()->remove_dead_node(hook); duke@435: } else { duke@435: hook->set_req(0, NULL); // Just yank bogus edge during Parse phase duke@435: } duke@435: duke@435: // return the value duke@435: return result; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *ModINode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // We always generate the dynamic check for 0. duke@435: // 0 MOD X is 0 duke@435: if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; duke@435: // X MOD X is 0 duke@435: if( phase->eqv( in(1), in(2) ) ) return TypeInt::ZERO; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: const TypeInt *i1 = t1->is_int(); duke@435: const TypeInt *i2 = t2->is_int(); duke@435: if( !i1->is_con() || !i2->is_con() ) { duke@435: if( i1->_lo >= 0 && i2->_lo >= 0 ) duke@435: return TypeInt::POS; duke@435: // If both numbers are not constants, we know little. duke@435: return TypeInt::INT; duke@435: } duke@435: // Mod by zero? Throw exception at runtime! duke@435: if( !i2->get_con() ) return TypeInt::POS; duke@435: duke@435: // We must be modulo'ing 2 float constants. duke@435: // Check for min_jint % '-1', result is defined to be '0'. duke@435: if( i1->get_con() == min_jint && i2->get_con() == -1 ) duke@435: return TypeInt::ZERO; duke@435: duke@435: return TypeInt::make( i1->get_con() % i2->get_con() ); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Idealize--------------------------------------- duke@435: Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) { duke@435: // Check for dead control input kvn@740: if( in(0) && remove_dead_region(phase, can_reshape) ) return this; kvn@740: // Don't bother trying to transform a dead node kvn@740: if( in(0) && in(0)->is_top() ) return NULL; duke@435: duke@435: // Get the modulus duke@435: const Type *t = phase->type( in(2) ); duke@435: if( t == Type::TOP ) return NULL; rasbold@580: const TypeLong *tl = t->is_long(); duke@435: duke@435: // Check for useless control input duke@435: // Check for excluding mod-zero case rasbold@580: if( in(0) && (tl->_hi < 0 || tl->_lo > 0) ) { duke@435: set_req(0, NULL); // Yank control input duke@435: return this; duke@435: } duke@435: duke@435: // See if we are MOD'ing by 2^k or 2^k-1. rasbold@580: if( !tl->is_con() ) return NULL; rasbold@580: jlong con = tl->get_con(); rasbold@580: rasbold@580: Node *hook = new (phase->C, 1) Node(1); duke@435: duke@435: // Expand mod rasbold@580: if( con >= 0 && con < max_jlong && is_power_of_2_long(con+1) ) { twisti@1003: uint k = exact_log2_long(con+1); // Extract k rasbold@580: duke@435: // Basic algorithm by David Detlefs. See fastmod_long.java for gory details. duke@435: // Used to help a popular random number generator which does a long-mod duke@435: // of 2^31-1 and shows up in SpecJBB and SciMark. duke@435: 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*/}; duke@435: int trip_count = 1; duke@435: if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k]; duke@435: rasbold@580: // If the unroll factor is not too large, and if conditional moves are rasbold@580: // ok, then use this case rasbold@580: if( trip_count <= 5 && ConditionalMoveLimit != 0 ) { rasbold@580: Node *x = in(1); // Value being mod'd rasbold@580: Node *divisor = in(2); // Also is mask duke@435: rasbold@580: hook->init_req(0, x); // Add a use to x to prevent him from dying rasbold@580: // Generate code to reduce X rapidly to nearly 2^k-1. rasbold@580: for( int i = 0; i < trip_count; i++ ) { duke@435: Node *xl = phase->transform( new (phase->C, 3) AndLNode(x,divisor) ); duke@435: Node *xh = phase->transform( new (phase->C, 3) RShiftLNode(x,phase->intcon(k)) ); // Must be signed duke@435: x = phase->transform( new (phase->C, 3) AddLNode(xh,xl) ); duke@435: hook->set_req(0, x); // Add a use to x to prevent him from dying rasbold@580: } rasbold@580: rasbold@580: // Generate sign-fixup code. Was original value positive? rasbold@580: // long hack_res = (i >= 0) ? divisor : CONST64(1); rasbold@580: Node *cmp1 = phase->transform( new (phase->C, 3) CmpLNode( in(1), phase->longcon(0) ) ); rasbold@580: Node *bol1 = phase->transform( new (phase->C, 2) BoolNode( cmp1, BoolTest::ge ) ); rasbold@580: Node *cmov1= phase->transform( new (phase->C, 4) CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) ); rasbold@580: // if( x >= hack_res ) x -= divisor; rasbold@580: Node *sub = phase->transform( new (phase->C, 3) SubLNode( x, divisor ) ); rasbold@580: Node *cmp2 = phase->transform( new (phase->C, 3) CmpLNode( x, cmov1 ) ); rasbold@580: Node *bol2 = phase->transform( new (phase->C, 2) BoolNode( cmp2, BoolTest::ge ) ); rasbold@580: // Convention is to not transform the return value of an Ideal rasbold@580: // since Ideal is expected to return a modified 'this' or a new node. rasbold@580: Node *cmov2= new (phase->C, 4) CMoveLNode(bol2, x, sub, TypeLong::LONG); rasbold@580: // cmov2 is now the mod rasbold@580: rasbold@580: // Now remove the bogus extra edges used to keep things alive rasbold@580: if (can_reshape) { rasbold@580: phase->is_IterGVN()->remove_dead_node(hook); rasbold@580: } else { rasbold@580: hook->set_req(0, NULL); // Just yank bogus edge during Parse phase rasbold@580: } rasbold@580: return cmov2; duke@435: } rasbold@580: } duke@435: rasbold@580: // Fell thru, the unroll case is not appropriate. Transform the modulo rasbold@580: // into a long multiply/int multiply/subtract case rasbold@580: rasbold@580: // Cannot handle mod 0, and min_jint isn't handled by the transform rasbold@580: if( con == 0 || con == min_jlong ) return NULL; rasbold@580: rasbold@580: // Get the absolute value of the constant; at this point, we can use this rasbold@580: jlong pos_con = (con >= 0) ? con : -con; rasbold@580: rasbold@580: // integer Mod 1 is always 0 rasbold@580: if( pos_con == 1 ) return new (phase->C, 1) ConLNode(TypeLong::ZERO); rasbold@580: rasbold@580: int log2_con = -1; rasbold@580: twisti@1040: // If this is a power of two, then maybe we can mask it rasbold@580: if( is_power_of_2_long(pos_con) ) { rasbold@580: log2_con = log2_long(pos_con); rasbold@580: rasbold@580: const Type *dt = phase->type(in(1)); rasbold@580: const TypeLong *dtl = dt->isa_long(); rasbold@580: rasbold@580: // See if this can be masked, if the dividend is non-negative rasbold@580: if( dtl && dtl->_lo >= 0 ) rasbold@580: return ( new (phase->C, 3) AndLNode( in(1), phase->longcon( pos_con-1 ) ) ); duke@435: } rasbold@580: rasbold@580: // Save in(1) so that it cannot be changed or deleted rasbold@580: hook->init_req(0, in(1)); rasbold@580: rasbold@580: // Divide using the transform from DivI to MulL rasbold@580: Node *result = transform_long_divide( phase, in(1), pos_con ); rasbold@580: if (result != NULL) { rasbold@580: Node *divide = phase->transform(result); rasbold@580: rasbold@580: // Re-multiply, using a shift if this is a power of two rasbold@580: Node *mult = NULL; rasbold@580: rasbold@580: if( log2_con >= 0 ) rasbold@580: mult = phase->transform( new (phase->C, 3) LShiftLNode( divide, phase->intcon( log2_con ) ) ); rasbold@580: else rasbold@580: mult = phase->transform( new (phase->C, 3) MulLNode( divide, phase->longcon( pos_con ) ) ); rasbold@580: rasbold@580: // Finally, subtract the multiplied divided value from the original rasbold@580: result = new (phase->C, 3) SubLNode( in(1), mult ); rasbold@580: } rasbold@580: rasbold@580: // Now remove the bogus extra edges used to keep things alive rasbold@580: if (can_reshape) { rasbold@580: phase->is_IterGVN()->remove_dead_node(hook); rasbold@580: } else { rasbold@580: hook->set_req(0, NULL); // Just yank bogus edge during Parse phase rasbold@580: } rasbold@580: rasbold@580: // return the value rasbold@580: return result; duke@435: } duke@435: duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *ModLNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // We always generate the dynamic check for 0. duke@435: // 0 MOD X is 0 duke@435: if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; duke@435: // X MOD X is 0 duke@435: if( phase->eqv( in(1), in(2) ) ) return TypeLong::ZERO; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: duke@435: const TypeLong *i1 = t1->is_long(); duke@435: const TypeLong *i2 = t2->is_long(); duke@435: if( !i1->is_con() || !i2->is_con() ) { duke@435: if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) ) duke@435: return TypeLong::POS; duke@435: // If both numbers are not constants, we know little. duke@435: return TypeLong::LONG; duke@435: } duke@435: // Mod by zero? Throw exception at runtime! duke@435: if( !i2->get_con() ) return TypeLong::POS; duke@435: duke@435: // We must be modulo'ing 2 float constants. duke@435: // Check for min_jint % '-1', result is defined to be '0'. duke@435: if( i1->get_con() == min_jlong && i2->get_con() == -1 ) duke@435: return TypeLong::ZERO; duke@435: duke@435: return TypeLong::make( i1->get_con() % i2->get_con() ); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *ModFNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: jrose@566: // If either number is not a constant, we know nothing. jrose@566: if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) { jrose@566: return Type::FLOAT; // note: x%x can be either NaN or 0 jrose@566: } jrose@566: jrose@566: float f1 = t1->getf(); jrose@566: float f2 = t2->getf(); jrose@566: jint x1 = jint_cast(f1); // note: *(int*)&f1, not just (int)f1 jrose@566: jint x2 = jint_cast(f2); jrose@566: duke@435: // If either is a NaN, return an input NaN jrose@566: if (g_isnan(f1)) return t1; jrose@566: if (g_isnan(f2)) return t2; duke@435: jrose@566: // If an operand is infinity or the divisor is +/- zero, punt. jrose@566: if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint) duke@435: return Type::FLOAT; duke@435: duke@435: // We must be modulo'ing 2 float constants. duke@435: // Make sure that the sign of the fmod is equal to the sign of the dividend jrose@566: jint xr = jint_cast(fmod(f1, f2)); jrose@566: if ((x1 ^ xr) < 0) { jrose@566: xr ^= min_jint; duke@435: } jrose@566: jrose@566: return TypeF::make(jfloat_cast(xr)); duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: //------------------------------Value------------------------------------------ duke@435: const Type *ModDNode::Value( PhaseTransform *phase ) const { duke@435: // Either input is TOP ==> the result is TOP duke@435: const Type *t1 = phase->type( in(1) ); duke@435: const Type *t2 = phase->type( in(2) ); duke@435: if( t1 == Type::TOP ) return Type::TOP; duke@435: if( t2 == Type::TOP ) return Type::TOP; duke@435: duke@435: // Either input is BOTTOM ==> the result is the local BOTTOM duke@435: const Type *bot = bottom_type(); duke@435: if( (t1 == bot) || (t2 == bot) || duke@435: (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) duke@435: return bot; duke@435: jrose@566: // If either number is not a constant, we know nothing. jrose@566: if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) { jrose@566: return Type::DOUBLE; // note: x%x can be either NaN or 0 duke@435: } duke@435: jrose@566: double f1 = t1->getd(); jrose@566: double f2 = t2->getd(); jrose@566: jlong x1 = jlong_cast(f1); // note: *(long*)&f1, not just (long)f1 jrose@566: jlong x2 = jlong_cast(f2); duke@435: jrose@566: // If either is a NaN, return an input NaN jrose@566: if (g_isnan(f1)) return t1; jrose@566: if (g_isnan(f2)) return t2; duke@435: jrose@566: // If an operand is infinity or the divisor is +/- zero, punt. jrose@566: if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong) duke@435: return Type::DOUBLE; duke@435: duke@435: // We must be modulo'ing 2 double constants. jrose@566: // Make sure that the sign of the fmod is equal to the sign of the dividend jrose@566: jlong xr = jlong_cast(fmod(f1, f2)); jrose@566: if ((x1 ^ xr) < 0) { jrose@566: xr ^= min_jlong; jrose@566: } jrose@566: jrose@566: return TypeD::make(jdouble_cast(xr)); duke@435: } duke@435: duke@435: //============================================================================= duke@435: duke@435: DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) { duke@435: init_req(0, c); duke@435: init_req(1, dividend); duke@435: init_req(2, divisor); duke@435: } duke@435: duke@435: //------------------------------make------------------------------------------ duke@435: DivModINode* DivModINode::make(Compile* C, Node* div_or_mod) { duke@435: Node* n = div_or_mod; duke@435: assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI, duke@435: "only div or mod input pattern accepted"); duke@435: duke@435: DivModINode* divmod = new (C, 3) DivModINode(n->in(0), n->in(1), n->in(2)); duke@435: Node* dproj = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num); duke@435: Node* mproj = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num); duke@435: return divmod; duke@435: } duke@435: duke@435: //------------------------------make------------------------------------------ duke@435: DivModLNode* DivModLNode::make(Compile* C, Node* div_or_mod) { duke@435: Node* n = div_or_mod; duke@435: assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL, duke@435: "only div or mod input pattern accepted"); duke@435: duke@435: DivModLNode* divmod = new (C, 3) DivModLNode(n->in(0), n->in(1), n->in(2)); duke@435: Node* dproj = new (C, 1) ProjNode(divmod, DivModNode::div_proj_num); duke@435: Node* mproj = new (C, 1) ProjNode(divmod, DivModNode::mod_proj_num); duke@435: return divmod; duke@435: } duke@435: duke@435: //------------------------------match------------------------------------------ duke@435: // return result(s) along with their RegMask info duke@435: Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) { duke@435: uint ideal_reg = proj->ideal_reg(); duke@435: RegMask rm; duke@435: if (proj->_con == div_proj_num) { duke@435: rm = match->divI_proj_mask(); duke@435: } else { duke@435: assert(proj->_con == mod_proj_num, "must be div or mod projection"); duke@435: rm = match->modI_proj_mask(); duke@435: } duke@435: return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg); duke@435: } duke@435: duke@435: duke@435: //------------------------------match------------------------------------------ duke@435: // return result(s) along with their RegMask info duke@435: Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) { duke@435: uint ideal_reg = proj->ideal_reg(); duke@435: RegMask rm; duke@435: if (proj->_con == div_proj_num) { duke@435: rm = match->divL_proj_mask(); duke@435: } else { duke@435: assert(proj->_con == mod_proj_num, "must be div or mod projection"); duke@435: rm = match->modL_proj_mask(); duke@435: } duke@435: return new (match->C, 1)MachProjNode(this, proj->_con, rm, ideal_reg); duke@435: }