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