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