src/share/vm/opto/divnode.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 6876
710a3c8b516e
child 9637
eef07cd490d4
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial