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