1 /* 2 * Copyright (c) 1997, 2024, 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 // 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 hook->destruct(phase); 329 330 return new AddLNode(temp1, temp2); 331 } 332 333 334 //--------------------------transform_long_divide------------------------------ 335 // Convert a division by constant divisor into an alternate Ideal graph. 336 // Return null if no transformation occurs. 337 static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) { 338 // Check for invalid divisors 339 assert( divisor != 0L && divisor != min_jlong, 340 "bad divisor for transforming to long multiply" ); 341 342 bool d_pos = divisor >= 0; 343 jlong d = d_pos ? divisor : -divisor; 344 const int N = 64; 345 346 // Result 347 Node *q = nullptr; 348 349 if (d == 1) { 350 // division by +/- 1 351 if (!d_pos) { 352 // Just negate the value 353 q = new SubLNode(phase->longcon(0), dividend); 354 } 355 } else if ( is_power_of_2(d) ) { 356 357 // division by +/- a power of 2 358 359 // See if we can simply do a shift without rounding 360 bool needs_rounding = true; 361 const Type *dt = phase->type(dividend); 362 const TypeLong *dtl = dt->isa_long(); 363 364 if (dtl && dtl->_lo > 0) { 365 // we don't need to round a positive dividend 366 needs_rounding = false; 367 } else if( dividend->Opcode() == Op_AndL ) { 368 // An AND mask of sufficient size clears the low bits and 369 // I can avoid rounding. 370 const TypeLong *andconl_t = phase->type( dividend->in(2) )->isa_long(); 371 if( andconl_t && andconl_t->is_con() ) { 372 jlong andconl = andconl_t->get_con(); 373 if( andconl < 0 && is_power_of_2(-andconl) && (-andconl) >= d ) { 374 if( (-andconl) == d ) // Remove AND if it clears bits which will be shifted 375 dividend = dividend->in(1); 376 needs_rounding = false; 377 } 378 } 379 } 380 381 // Add rounding to the shift to handle the sign bit 382 int l = log2i_graceful(d - 1) + 1; 383 if (needs_rounding) { 384 // Divide-by-power-of-2 can be made into a shift, but you have to do 385 // more math for the rounding. You need to add 0 for positive 386 // numbers, and "i-1" for negative numbers. Example: i=4, so the 387 // shift is by 2. You need to add 3 to negative dividends and 0 to 388 // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1, 389 // (-2+3)>>2 becomes 0, etc. 390 391 // Compute 0 or -1, based on sign bit 392 Node *sign = phase->transform(new RShiftLNode(dividend, phase->intcon(N - 1))); 393 // Mask sign bit to the low sign bits 394 Node *round = phase->transform(new URShiftLNode(sign, phase->intcon(N - l))); 395 // Round up before shifting 396 dividend = phase->transform(new AddLNode(dividend, round)); 397 } 398 399 // Shift for division 400 q = new RShiftLNode(dividend, phase->intcon(l)); 401 402 if (!d_pos) { 403 q = new SubLNode(phase->longcon(0), phase->transform(q)); 404 } 405 } else if ( !Matcher::use_asm_for_ldiv_by_con(d) ) { // Use hardware DIV instruction when 406 // it is faster than code generated below. 407 // Attempt the jlong constant divide -> multiply transform found in 408 // "Division by Invariant Integers using Multiplication" 409 // by Granlund and Montgomery 410 // See also "Hacker's Delight", chapter 10 by Warren. 411 412 jlong magic_const; 413 jint shift_const; 414 if (magic_long_divide_constants(d, magic_const, shift_const)) { 415 // Compute the high half of the dividend x magic multiplication 416 Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const)); 417 418 // The high half of the 128-bit multiply is computed. 419 if (magic_const < 0) { 420 // The magic multiplier is too large for a 64 bit constant. We've adjusted 421 // it down by 2^64, but have to add 1 dividend back in after the multiplication. 422 // This handles the "overflow" case described by Granlund and Montgomery. 423 mul_hi = phase->transform(new AddLNode(dividend, mul_hi)); 424 } 425 426 // Shift over the (adjusted) mulhi 427 if (shift_const != 0) { 428 mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(shift_const))); 429 } 430 431 // Get a 0 or -1 from the sign of the dividend. 432 Node *addend0 = mul_hi; 433 Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1))); 434 435 // If the divisor is negative, swap the order of the input addends; 436 // this has the effect of negating the quotient. 437 if (!d_pos) { 438 Node *temp = addend0; addend0 = addend1; addend1 = temp; 439 } 440 441 // Adjust the final quotient by subtracting -1 (adding 1) 442 // from the mul_hi. 443 q = new SubLNode(addend0, addend1); 444 } 445 } 446 447 return q; 448 } 449 450 template <typename TypeClass, typename Unsigned> 451 Node* unsigned_div_ideal(PhaseGVN* phase, bool can_reshape, Node* div) { 452 // Check for dead control input 453 if (div->in(0) != nullptr && div->remove_dead_region(phase, can_reshape)) { 454 return div; 455 } 456 // Don't bother trying to transform a dead node 457 if (div->in(0) != nullptr && div->in(0)->is_top()) { 458 return nullptr; 459 } 460 461 const Type* t = phase->type(div->in(2)); 462 if (t == Type::TOP) { 463 return nullptr; 464 } 465 const TypeClass* type_divisor = t->cast<TypeClass>(); 466 467 // Check for useless control input 468 // Check for excluding div-zero case 469 if (div->in(0) != nullptr && (type_divisor->_hi < 0 || type_divisor->_lo > 0)) { 470 div->set_req(0, nullptr); // Yank control input 471 return div; 472 } 473 474 if (!type_divisor->is_con()) { 475 return nullptr; 476 } 477 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con()); // Get divisor 478 479 if (divisor == 0 || divisor == 1) { 480 return nullptr; // Dividing by zero constant does not idealize 481 } 482 483 if (is_power_of_2(divisor)) { 484 return make_urshift<TypeClass>(div->in(1), phase->intcon(log2i_graceful(divisor))); 485 } 486 487 return nullptr; 488 } 489 490 491 //============================================================================= 492 //------------------------------Identity--------------------------------------- 493 // If the divisor is 1, we are an identity on the dividend. 494 Node* DivINode::Identity(PhaseGVN* phase) { 495 return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this; 496 } 497 498 //------------------------------Idealize--------------------------------------- 499 // Divides can be changed to multiplies and/or shifts 500 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) { 501 if (in(0) && remove_dead_region(phase, can_reshape)) return this; 502 // Don't bother trying to transform a dead node 503 if( in(0) && in(0)->is_top() ) return nullptr; 504 505 const Type *t = phase->type( in(2) ); 506 if( t == TypeInt::ONE ) // Identity? 507 return nullptr; // Skip it 508 509 const TypeInt *ti = t->isa_int(); 510 if( !ti ) return nullptr; 511 512 // Check for useless control input 513 // Check for excluding div-zero case 514 if (in(0) && (ti->_hi < 0 || ti->_lo > 0)) { 515 set_req(0, nullptr); // Yank control input 516 return this; 517 } 518 519 if( !ti->is_con() ) return nullptr; 520 jint i = ti->get_con(); // Get divisor 521 522 if (i == 0) return nullptr; // Dividing by zero constant does not idealize 523 524 // Dividing by MININT does not optimize as a power-of-2 shift. 525 if( i == min_jint ) return nullptr; 526 527 return transform_int_divide( phase, in(1), i ); 528 } 529 530 //------------------------------Value------------------------------------------ 531 // A DivINode divides its inputs. The third input is a Control input, used to 532 // prevent hoisting the divide above an unsafe test. 533 const Type* DivINode::Value(PhaseGVN* phase) const { 534 // Either input is TOP ==> the result is TOP 535 const Type *t1 = phase->type( in(1) ); 536 const Type *t2 = phase->type( in(2) ); 537 if( t1 == Type::TOP ) return Type::TOP; 538 if( t2 == Type::TOP ) return Type::TOP; 539 540 // x/x == 1 since we always generate the dynamic divisor check for 0. 541 if (in(1) == in(2)) { 542 return TypeInt::ONE; 543 } 544 545 // Either input is BOTTOM ==> the result is the local BOTTOM 546 const Type *bot = bottom_type(); 547 if( (t1 == bot) || (t2 == bot) || 548 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 549 return bot; 550 551 // Divide the two numbers. We approximate. 552 // If divisor is a constant and not zero 553 const TypeInt *i1 = t1->is_int(); 554 const TypeInt *i2 = t2->is_int(); 555 int widen = MAX2(i1->_widen, i2->_widen); 556 557 if( i2->is_con() && i2->get_con() != 0 ) { 558 int32_t d = i2->get_con(); // Divisor 559 jint lo, hi; 560 if( d >= 0 ) { 561 lo = i1->_lo/d; 562 hi = i1->_hi/d; 563 } else { 564 if( d == -1 && i1->_lo == min_jint ) { 565 // 'min_jint/-1' throws arithmetic exception during compilation 566 lo = min_jint; 567 // do not support holes, 'hi' must go to either min_jint or max_jint: 568 // [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint] 569 hi = i1->_hi == min_jint ? min_jint : max_jint; 570 } else { 571 lo = i1->_hi/d; 572 hi = i1->_lo/d; 573 } 574 } 575 return TypeInt::make(lo, hi, widen); 576 } 577 578 // If the dividend is a constant 579 if( i1->is_con() ) { 580 int32_t d = i1->get_con(); 581 if( d < 0 ) { 582 if( d == min_jint ) { 583 // (-min_jint) == min_jint == (min_jint / -1) 584 return TypeInt::make(min_jint, max_jint/2 + 1, widen); 585 } else { 586 return TypeInt::make(d, -d, widen); 587 } 588 } 589 return TypeInt::make(-d, d, widen); 590 } 591 592 // Otherwise we give up all hope 593 return TypeInt::INT; 594 } 595 596 597 //============================================================================= 598 //------------------------------Identity--------------------------------------- 599 // If the divisor is 1, we are an identity on the dividend. 600 Node* DivLNode::Identity(PhaseGVN* phase) { 601 return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this; 602 } 603 604 //------------------------------Idealize--------------------------------------- 605 // Dividing by a power of 2 is a shift. 606 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) { 607 if (in(0) && remove_dead_region(phase, can_reshape)) return this; 608 // Don't bother trying to transform a dead node 609 if( in(0) && in(0)->is_top() ) return nullptr; 610 611 const Type *t = phase->type( in(2) ); 612 if( t == TypeLong::ONE ) // Identity? 613 return nullptr; // Skip it 614 615 const TypeLong *tl = t->isa_long(); 616 if( !tl ) return nullptr; 617 618 // Check for useless control input 619 // Check for excluding div-zero case 620 if (in(0) && (tl->_hi < 0 || tl->_lo > 0)) { 621 set_req(0, nullptr); // Yank control input 622 return this; 623 } 624 625 if( !tl->is_con() ) return nullptr; 626 jlong l = tl->get_con(); // Get divisor 627 628 if (l == 0) return nullptr; // Dividing by zero constant does not idealize 629 630 // Dividing by MINLONG does not optimize as a power-of-2 shift. 631 if( l == min_jlong ) return nullptr; 632 633 return transform_long_divide( phase, in(1), l ); 634 } 635 636 //------------------------------Value------------------------------------------ 637 // A DivLNode divides its inputs. The third input is a Control input, used to 638 // prevent hoisting the divide above an unsafe test. 639 const Type* DivLNode::Value(PhaseGVN* phase) const { 640 // Either input is TOP ==> the result is TOP 641 const Type *t1 = phase->type( in(1) ); 642 const Type *t2 = phase->type( in(2) ); 643 if( t1 == Type::TOP ) return Type::TOP; 644 if( t2 == Type::TOP ) return Type::TOP; 645 646 // x/x == 1 since we always generate the dynamic divisor check for 0. 647 if (in(1) == in(2)) { 648 return TypeLong::ONE; 649 } 650 651 // Either input is BOTTOM ==> the result is the local BOTTOM 652 const Type *bot = bottom_type(); 653 if( (t1 == bot) || (t2 == bot) || 654 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 655 return bot; 656 657 // Divide the two numbers. We approximate. 658 // If divisor is a constant and not zero 659 const TypeLong *i1 = t1->is_long(); 660 const TypeLong *i2 = t2->is_long(); 661 int widen = MAX2(i1->_widen, i2->_widen); 662 663 if( i2->is_con() && i2->get_con() != 0 ) { 664 jlong d = i2->get_con(); // Divisor 665 jlong lo, hi; 666 if( d >= 0 ) { 667 lo = i1->_lo/d; 668 hi = i1->_hi/d; 669 } else { 670 if( d == CONST64(-1) && i1->_lo == min_jlong ) { 671 // 'min_jlong/-1' throws arithmetic exception during compilation 672 lo = min_jlong; 673 // do not support holes, 'hi' must go to either min_jlong or max_jlong: 674 // [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong] 675 hi = i1->_hi == min_jlong ? min_jlong : max_jlong; 676 } else { 677 lo = i1->_hi/d; 678 hi = i1->_lo/d; 679 } 680 } 681 return TypeLong::make(lo, hi, widen); 682 } 683 684 // If the dividend is a constant 685 if( i1->is_con() ) { 686 jlong d = i1->get_con(); 687 if( d < 0 ) { 688 if( d == min_jlong ) { 689 // (-min_jlong) == min_jlong == (min_jlong / -1) 690 return TypeLong::make(min_jlong, max_jlong/2 + 1, widen); 691 } else { 692 return TypeLong::make(d, -d, widen); 693 } 694 } 695 return TypeLong::make(-d, d, widen); 696 } 697 698 // Otherwise we give up all hope 699 return TypeLong::LONG; 700 } 701 702 703 //============================================================================= 704 //------------------------------Value------------------------------------------ 705 // An DivFNode divides its inputs. The third input is a Control input, used to 706 // prevent hoisting the divide above an unsafe test. 707 const Type* DivFNode::Value(PhaseGVN* phase) const { 708 // Either input is TOP ==> the result is TOP 709 const Type *t1 = phase->type( in(1) ); 710 const Type *t2 = phase->type( in(2) ); 711 if( t1 == Type::TOP ) return Type::TOP; 712 if( t2 == Type::TOP ) return Type::TOP; 713 714 // Either input is BOTTOM ==> the result is the local BOTTOM 715 const Type *bot = bottom_type(); 716 if( (t1 == bot) || (t2 == bot) || 717 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 718 return bot; 719 720 // x/x == 1, we ignore 0/0. 721 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) 722 // Does not work for variables because of NaN's 723 if (in(1) == in(2) && t1->base() == Type::FloatCon && 724 !g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) { // could be negative ZERO or NaN 725 return TypeF::ONE; 726 } 727 728 if( t2 == TypeF::ONE ) 729 return t1; 730 731 // If divisor is a constant and not zero, divide them numbers 732 if( t1->base() == Type::FloatCon && 733 t2->base() == Type::FloatCon && 734 t2->getf() != 0.0 ) // could be negative zero 735 return TypeF::make( t1->getf()/t2->getf() ); 736 737 // If the dividend is a constant zero 738 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) 739 // Test TypeF::ZERO is not sufficient as it could be negative zero 740 741 if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 ) 742 return TypeF::ZERO; 743 744 // Otherwise we give up all hope 745 return Type::FLOAT; 746 } 747 748 //------------------------------isA_Copy--------------------------------------- 749 // Dividing by self is 1. 750 // If the divisor is 1, we are an identity on the dividend. 751 Node* DivFNode::Identity(PhaseGVN* phase) { 752 return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this; 753 } 754 755 756 //------------------------------Idealize--------------------------------------- 757 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) { 758 if (in(0) && remove_dead_region(phase, can_reshape)) return this; 759 // Don't bother trying to transform a dead node 760 if( in(0) && in(0)->is_top() ) return nullptr; 761 762 const Type *t2 = phase->type( in(2) ); 763 if( t2 == TypeF::ONE ) // Identity? 764 return nullptr; // Skip it 765 766 const TypeF *tf = t2->isa_float_constant(); 767 if( !tf ) return nullptr; 768 if( tf->base() != Type::FloatCon ) return nullptr; 769 770 // Check for out of range values 771 if( tf->is_nan() || !tf->is_finite() ) return nullptr; 772 773 // Get the value 774 float f = tf->getf(); 775 int exp; 776 777 // Only for special case of dividing by a power of 2 778 if( frexp((double)f, &exp) != 0.5 ) return nullptr; 779 780 // Limit the range of acceptable exponents 781 if( exp < -126 || exp > 126 ) return nullptr; 782 783 // Compute the reciprocal 784 float reciprocal = ((float)1.0) / f; 785 786 assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" ); 787 788 // return multiplication by the reciprocal 789 return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal)))); 790 } 791 792 //============================================================================= 793 //------------------------------Value------------------------------------------ 794 // An DivDNode divides its inputs. The third input is a Control input, used to 795 // prevent hoisting the divide above an unsafe test. 796 const Type* DivDNode::Value(PhaseGVN* phase) const { 797 // Either input is TOP ==> the result is TOP 798 const Type *t1 = phase->type( in(1) ); 799 const Type *t2 = phase->type( in(2) ); 800 if( t1 == Type::TOP ) return Type::TOP; 801 if( t2 == Type::TOP ) return Type::TOP; 802 803 // Either input is BOTTOM ==> the result is the local BOTTOM 804 const Type *bot = bottom_type(); 805 if( (t1 == bot) || (t2 == bot) || 806 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 807 return bot; 808 809 // x/x == 1, we ignore 0/0. 810 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) 811 // Does not work for variables because of NaN's 812 if (in(1) == in(2) && t1->base() == Type::DoubleCon && 813 !g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) { // could be negative ZERO or NaN 814 return TypeD::ONE; 815 } 816 817 if( t2 == TypeD::ONE ) 818 return t1; 819 820 // IA32 would only execute this for non-strict FP, which is never the 821 // case now. 822 #if ! defined(IA32) 823 // If divisor is a constant and not zero, divide them numbers 824 if( t1->base() == Type::DoubleCon && 825 t2->base() == Type::DoubleCon && 826 t2->getd() != 0.0 ) // could be negative zero 827 return TypeD::make( t1->getd()/t2->getd() ); 828 #endif 829 830 // If the dividend is a constant zero 831 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213) 832 // Test TypeF::ZERO is not sufficient as it could be negative zero 833 if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 ) 834 return TypeD::ZERO; 835 836 // Otherwise we give up all hope 837 return Type::DOUBLE; 838 } 839 840 841 //------------------------------isA_Copy--------------------------------------- 842 // Dividing by self is 1. 843 // If the divisor is 1, we are an identity on the dividend. 844 Node* DivDNode::Identity(PhaseGVN* phase) { 845 return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this; 846 } 847 848 //------------------------------Idealize--------------------------------------- 849 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) { 850 if (in(0) && remove_dead_region(phase, can_reshape)) return this; 851 // Don't bother trying to transform a dead node 852 if( in(0) && in(0)->is_top() ) return nullptr; 853 854 const Type *t2 = phase->type( in(2) ); 855 if( t2 == TypeD::ONE ) // Identity? 856 return nullptr; // Skip it 857 858 const TypeD *td = t2->isa_double_constant(); 859 if( !td ) return nullptr; 860 if( td->base() != Type::DoubleCon ) return nullptr; 861 862 // Check for out of range values 863 if( td->is_nan() || !td->is_finite() ) return nullptr; 864 865 // Get the value 866 double d = td->getd(); 867 int exp; 868 869 // Only for special case of dividing by a power of 2 870 if( frexp(d, &exp) != 0.5 ) return nullptr; 871 872 // Limit the range of acceptable exponents 873 if( exp < -1021 || exp > 1022 ) return nullptr; 874 875 // Compute the reciprocal 876 double reciprocal = 1.0 / d; 877 878 assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" ); 879 880 // return multiplication by the reciprocal 881 return (new MulDNode(in(1), phase->makecon(TypeD::make(reciprocal)))); 882 } 883 884 //============================================================================= 885 //------------------------------Identity--------------------------------------- 886 // If the divisor is 1, we are an identity on the dividend. 887 Node* UDivINode::Identity(PhaseGVN* phase) { 888 return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this; 889 } 890 //------------------------------Value------------------------------------------ 891 // A UDivINode divides its inputs. The third input is a Control input, used to 892 // prevent hoisting the divide above an unsafe test. 893 const Type* UDivINode::Value(PhaseGVN* phase) const { 894 // Either input is TOP ==> the result is TOP 895 const Type *t1 = phase->type( in(1) ); 896 const Type *t2 = phase->type( in(2) ); 897 if( t1 == Type::TOP ) return Type::TOP; 898 if( t2 == Type::TOP ) return Type::TOP; 899 900 // x/x == 1 since we always generate the dynamic divisor check for 0. 901 if (in(1) == in(2)) { 902 return TypeInt::ONE; 903 } 904 905 // Either input is BOTTOM ==> the result is the local BOTTOM 906 const Type *bot = bottom_type(); 907 if( (t1 == bot) || (t2 == bot) || 908 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 909 return bot; 910 911 // Otherwise we give up all hope 912 return TypeInt::INT; 913 } 914 915 //------------------------------Idealize--------------------------------------- 916 Node *UDivINode::Ideal(PhaseGVN *phase, bool can_reshape) { 917 return unsigned_div_ideal<TypeInt, juint>(phase, can_reshape, this); 918 } 919 920 //============================================================================= 921 //------------------------------Identity--------------------------------------- 922 // If the divisor is 1, we are an identity on the dividend. 923 Node* UDivLNode::Identity(PhaseGVN* phase) { 924 return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this; 925 } 926 //------------------------------Value------------------------------------------ 927 // A UDivLNode divides its inputs. The third input is a Control input, used to 928 // prevent hoisting the divide above an unsafe test. 929 const Type* UDivLNode::Value(PhaseGVN* phase) const { 930 // Either input is TOP ==> the result is TOP 931 const Type *t1 = phase->type( in(1) ); 932 const Type *t2 = phase->type( in(2) ); 933 if( t1 == Type::TOP ) return Type::TOP; 934 if( t2 == Type::TOP ) return Type::TOP; 935 936 // x/x == 1 since we always generate the dynamic divisor check for 0. 937 if (in(1) == in(2)) { 938 return TypeLong::ONE; 939 } 940 941 // Either input is BOTTOM ==> the result is the local BOTTOM 942 const Type *bot = bottom_type(); 943 if( (t1 == bot) || (t2 == bot) || 944 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 945 return bot; 946 947 // Otherwise we give up all hope 948 return TypeLong::LONG; 949 } 950 951 //------------------------------Idealize--------------------------------------- 952 Node *UDivLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 953 return unsigned_div_ideal<TypeLong, julong>(phase, can_reshape, this); 954 } 955 956 //============================================================================= 957 //------------------------------Idealize--------------------------------------- 958 Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) { 959 // Check for dead control input 960 if( in(0) && remove_dead_region(phase, can_reshape) ) return this; 961 // Don't bother trying to transform a dead node 962 if( in(0) && in(0)->is_top() ) return nullptr; 963 964 // Get the modulus 965 const Type *t = phase->type( in(2) ); 966 if( t == Type::TOP ) return nullptr; 967 const TypeInt *ti = t->is_int(); 968 969 // Check for useless control input 970 // Check for excluding mod-zero case 971 if (in(0) && (ti->_hi < 0 || ti->_lo > 0)) { 972 set_req(0, nullptr); // Yank control input 973 return this; 974 } 975 976 // See if we are MOD'ing by 2^k or 2^k-1. 977 if( !ti->is_con() ) return nullptr; 978 jint con = ti->get_con(); 979 980 Node *hook = new Node(1); 981 982 // First, special check for modulo 2^k-1 983 if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) { 984 uint k = exact_log2(con+1); // Extract k 985 986 // Basic algorithm by David Detlefs. See fastmod_int.java for gory details. 987 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*/}; 988 int trip_count = 1; 989 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k]; 990 991 // If the unroll factor is not too large, and if conditional moves are 992 // ok, then use this case 993 if( trip_count <= 5 && ConditionalMoveLimit != 0 ) { 994 Node *x = in(1); // Value being mod'd 995 Node *divisor = in(2); // Also is mask 996 997 hook->init_req(0, x); // Add a use to x to prevent him from dying 998 // Generate code to reduce X rapidly to nearly 2^k-1. 999 for( int i = 0; i < trip_count; i++ ) { 1000 Node *xl = phase->transform( new AndINode(x,divisor) ); 1001 Node *xh = phase->transform( new RShiftINode(x,phase->intcon(k)) ); // Must be signed 1002 x = phase->transform( new AddINode(xh,xl) ); 1003 hook->set_req(0, x); 1004 } 1005 1006 // Generate sign-fixup code. Was original value positive? 1007 // int hack_res = (i >= 0) ? divisor : 1; 1008 Node *cmp1 = phase->transform( new CmpINode( in(1), phase->intcon(0) ) ); 1009 Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) ); 1010 Node *cmov1= phase->transform( new CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) ); 1011 // if( x >= hack_res ) x -= divisor; 1012 Node *sub = phase->transform( new SubINode( x, divisor ) ); 1013 Node *cmp2 = phase->transform( new CmpINode( x, cmov1 ) ); 1014 Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) ); 1015 // Convention is to not transform the return value of an Ideal 1016 // since Ideal is expected to return a modified 'this' or a new node. 1017 Node *cmov2= new CMoveINode(bol2, x, sub, TypeInt::INT); 1018 // cmov2 is now the mod 1019 1020 // Now remove the bogus extra edges used to keep things alive 1021 hook->destruct(phase); 1022 return cmov2; 1023 } 1024 } 1025 1026 // Fell thru, the unroll case is not appropriate. Transform the modulo 1027 // into a long multiply/int multiply/subtract case 1028 1029 // Cannot handle mod 0, and min_jint isn't handled by the transform 1030 if( con == 0 || con == min_jint ) return nullptr; 1031 1032 // Get the absolute value of the constant; at this point, we can use this 1033 jint pos_con = (con >= 0) ? con : -con; 1034 1035 // integer Mod 1 is always 0 1036 if( pos_con == 1 ) return new ConINode(TypeInt::ZERO); 1037 1038 int log2_con = -1; 1039 1040 // If this is a power of two, they maybe we can mask it 1041 if (is_power_of_2(pos_con)) { 1042 log2_con = log2i_exact(pos_con); 1043 1044 const Type *dt = phase->type(in(1)); 1045 const TypeInt *dti = dt->isa_int(); 1046 1047 // See if this can be masked, if the dividend is non-negative 1048 if( dti && dti->_lo >= 0 ) 1049 return ( new AndINode( in(1), phase->intcon( pos_con-1 ) ) ); 1050 } 1051 1052 // Save in(1) so that it cannot be changed or deleted 1053 hook->init_req(0, in(1)); 1054 1055 // Divide using the transform from DivI to MulL 1056 Node *result = transform_int_divide( phase, in(1), pos_con ); 1057 if (result != nullptr) { 1058 Node *divide = phase->transform(result); 1059 1060 // Re-multiply, using a shift if this is a power of two 1061 Node *mult = nullptr; 1062 1063 if( log2_con >= 0 ) 1064 mult = phase->transform( new LShiftINode( divide, phase->intcon( log2_con ) ) ); 1065 else 1066 mult = phase->transform( new MulINode( divide, phase->intcon( pos_con ) ) ); 1067 1068 // Finally, subtract the multiplied divided value from the original 1069 result = new SubINode( in(1), mult ); 1070 } 1071 1072 // Now remove the bogus extra edges used to keep things alive 1073 hook->destruct(phase); 1074 1075 // return the value 1076 return result; 1077 } 1078 1079 //------------------------------Value------------------------------------------ 1080 const Type* ModINode::Value(PhaseGVN* phase) const { 1081 // Either input is TOP ==> the result is TOP 1082 const Type *t1 = phase->type( in(1) ); 1083 const Type *t2 = phase->type( in(2) ); 1084 if( t1 == Type::TOP ) return Type::TOP; 1085 if( t2 == Type::TOP ) return Type::TOP; 1086 1087 // We always generate the dynamic check for 0. 1088 // 0 MOD X is 0 1089 if( t1 == TypeInt::ZERO ) return TypeInt::ZERO; 1090 // X MOD X is 0 1091 if (in(1) == in(2)) { 1092 return TypeInt::ZERO; 1093 } 1094 1095 // Either input is BOTTOM ==> the result is the local BOTTOM 1096 const Type *bot = bottom_type(); 1097 if( (t1 == bot) || (t2 == bot) || 1098 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1099 return bot; 1100 1101 const TypeInt *i1 = t1->is_int(); 1102 const TypeInt *i2 = t2->is_int(); 1103 if( !i1->is_con() || !i2->is_con() ) { 1104 if( i1->_lo >= 0 && i2->_lo >= 0 ) 1105 return TypeInt::POS; 1106 // If both numbers are not constants, we know little. 1107 return TypeInt::INT; 1108 } 1109 // Mod by zero? Throw exception at runtime! 1110 if( !i2->get_con() ) return TypeInt::POS; 1111 1112 // We must be modulo'ing 2 float constants. 1113 // Check for min_jint % '-1', result is defined to be '0'. 1114 if( i1->get_con() == min_jint && i2->get_con() == -1 ) 1115 return TypeInt::ZERO; 1116 1117 return TypeInt::make( i1->get_con() % i2->get_con() ); 1118 } 1119 1120 //============================================================================= 1121 //------------------------------Idealize--------------------------------------- 1122 1123 template <typename TypeClass, typename Unsigned> 1124 static Node* unsigned_mod_ideal(PhaseGVN* phase, bool can_reshape, Node* mod) { 1125 // Check for dead control input 1126 if (mod->in(0) != nullptr && mod->remove_dead_region(phase, can_reshape)) { 1127 return mod; 1128 } 1129 // Don't bother trying to transform a dead node 1130 if (mod->in(0) != nullptr && mod->in(0)->is_top()) { 1131 return nullptr; 1132 } 1133 1134 // Get the modulus 1135 const Type* t = phase->type(mod->in(2)); 1136 if (t == Type::TOP) { 1137 return nullptr; 1138 } 1139 const TypeClass* type_divisor = t->cast<TypeClass>(); 1140 1141 // Check for useless control input 1142 // Check for excluding mod-zero case 1143 if (mod->in(0) != nullptr && (type_divisor->_hi < 0 || type_divisor->_lo > 0)) { 1144 mod->set_req(0, nullptr); // Yank control input 1145 return mod; 1146 } 1147 1148 if (!type_divisor->is_con()) { 1149 return nullptr; 1150 } 1151 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con()); 1152 1153 if (divisor == 0) { 1154 return nullptr; 1155 } 1156 1157 if (is_power_of_2(divisor)) { 1158 return make_and<TypeClass>(mod->in(1), phase->makecon(TypeClass::make(divisor - 1))); 1159 } 1160 1161 return nullptr; 1162 } 1163 1164 template <typename TypeClass, typename Unsigned, typename Signed> 1165 static const Type* unsigned_mod_value(PhaseGVN* phase, const Node* mod) { 1166 const Type* t1 = phase->type(mod->in(1)); 1167 const Type* t2 = phase->type(mod->in(2)); 1168 if (t1 == Type::TOP) { 1169 return Type::TOP; 1170 } 1171 if (t2 == Type::TOP) { 1172 return Type::TOP; 1173 } 1174 1175 // 0 MOD X is 0 1176 if (t1 == TypeClass::ZERO) { 1177 return TypeClass::ZERO; 1178 } 1179 // X MOD X is 0 1180 if (mod->in(1) == mod->in(2)) { 1181 return TypeClass::ZERO; 1182 } 1183 1184 // Either input is BOTTOM ==> the result is the local BOTTOM 1185 const Type* bot = mod->bottom_type(); 1186 if ((t1 == bot) || (t2 == bot) || 1187 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM)) { 1188 return bot; 1189 } 1190 1191 const TypeClass* type_divisor = t2->cast<TypeClass>(); 1192 if (type_divisor->is_con() && type_divisor->get_con() == 1) { 1193 return TypeClass::ZERO; 1194 } 1195 1196 const TypeClass* type_dividend = t1->cast<TypeClass>(); 1197 if (type_dividend->is_con() && type_divisor->is_con()) { 1198 Unsigned dividend = static_cast<Unsigned>(type_dividend->get_con()); 1199 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con()); 1200 return TypeClass::make(static_cast<Signed>(dividend % divisor)); 1201 } 1202 1203 return bot; 1204 } 1205 1206 Node* UModINode::Ideal(PhaseGVN* phase, bool can_reshape) { 1207 return unsigned_mod_ideal<TypeInt, juint>(phase, can_reshape, this); 1208 } 1209 1210 const Type* UModINode::Value(PhaseGVN* phase) const { 1211 return unsigned_mod_value<TypeInt, juint, jint>(phase, this); 1212 } 1213 1214 //============================================================================= 1215 //------------------------------Idealize--------------------------------------- 1216 Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1217 // Check for dead control input 1218 if( in(0) && remove_dead_region(phase, can_reshape) ) return this; 1219 // Don't bother trying to transform a dead node 1220 if( in(0) && in(0)->is_top() ) return nullptr; 1221 1222 // Get the modulus 1223 const Type *t = phase->type( in(2) ); 1224 if( t == Type::TOP ) return nullptr; 1225 const TypeLong *tl = t->is_long(); 1226 1227 // Check for useless control input 1228 // Check for excluding mod-zero case 1229 if (in(0) && (tl->_hi < 0 || tl->_lo > 0)) { 1230 set_req(0, nullptr); // Yank control input 1231 return this; 1232 } 1233 1234 // See if we are MOD'ing by 2^k or 2^k-1. 1235 if( !tl->is_con() ) return nullptr; 1236 jlong con = tl->get_con(); 1237 1238 Node *hook = new Node(1); 1239 1240 // Expand mod 1241 if(con >= 0 && con < max_jlong && is_power_of_2(con + 1)) { 1242 uint k = log2i_exact(con + 1); // Extract k 1243 1244 // Basic algorithm by David Detlefs. See fastmod_long.java for gory details. 1245 // Used to help a popular random number generator which does a long-mod 1246 // of 2^31-1 and shows up in SpecJBB and SciMark. 1247 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*/}; 1248 int trip_count = 1; 1249 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k]; 1250 1251 // If the unroll factor is not too large, and if conditional moves are 1252 // ok, then use this case 1253 if( trip_count <= 5 && ConditionalMoveLimit != 0 ) { 1254 Node *x = in(1); // Value being mod'd 1255 Node *divisor = in(2); // Also is mask 1256 1257 hook->init_req(0, x); // Add a use to x to prevent him from dying 1258 // Generate code to reduce X rapidly to nearly 2^k-1. 1259 for( int i = 0; i < trip_count; i++ ) { 1260 Node *xl = phase->transform( new AndLNode(x,divisor) ); 1261 Node *xh = phase->transform( new RShiftLNode(x,phase->intcon(k)) ); // Must be signed 1262 x = phase->transform( new AddLNode(xh,xl) ); 1263 hook->set_req(0, x); // Add a use to x to prevent him from dying 1264 } 1265 1266 // Generate sign-fixup code. Was original value positive? 1267 // long hack_res = (i >= 0) ? divisor : CONST64(1); 1268 Node *cmp1 = phase->transform( new CmpLNode( in(1), phase->longcon(0) ) ); 1269 Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) ); 1270 Node *cmov1= phase->transform( new CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) ); 1271 // if( x >= hack_res ) x -= divisor; 1272 Node *sub = phase->transform( new SubLNode( x, divisor ) ); 1273 Node *cmp2 = phase->transform( new CmpLNode( x, cmov1 ) ); 1274 Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) ); 1275 // Convention is to not transform the return value of an Ideal 1276 // since Ideal is expected to return a modified 'this' or a new node. 1277 Node *cmov2= new CMoveLNode(bol2, x, sub, TypeLong::LONG); 1278 // cmov2 is now the mod 1279 1280 // Now remove the bogus extra edges used to keep things alive 1281 hook->destruct(phase); 1282 return cmov2; 1283 } 1284 } 1285 1286 // Fell thru, the unroll case is not appropriate. Transform the modulo 1287 // into a long multiply/int multiply/subtract case 1288 1289 // Cannot handle mod 0, and min_jlong isn't handled by the transform 1290 if( con == 0 || con == min_jlong ) return nullptr; 1291 1292 // Get the absolute value of the constant; at this point, we can use this 1293 jlong pos_con = (con >= 0) ? con : -con; 1294 1295 // integer Mod 1 is always 0 1296 if( pos_con == 1 ) return new ConLNode(TypeLong::ZERO); 1297 1298 int log2_con = -1; 1299 1300 // If this is a power of two, then maybe we can mask it 1301 if (is_power_of_2(pos_con)) { 1302 log2_con = log2i_exact(pos_con); 1303 1304 const Type *dt = phase->type(in(1)); 1305 const TypeLong *dtl = dt->isa_long(); 1306 1307 // See if this can be masked, if the dividend is non-negative 1308 if( dtl && dtl->_lo >= 0 ) 1309 return ( new AndLNode( in(1), phase->longcon( pos_con-1 ) ) ); 1310 } 1311 1312 // Save in(1) so that it cannot be changed or deleted 1313 hook->init_req(0, in(1)); 1314 1315 // Divide using the transform from DivL to MulL 1316 Node *result = transform_long_divide( phase, in(1), pos_con ); 1317 if (result != nullptr) { 1318 Node *divide = phase->transform(result); 1319 1320 // Re-multiply, using a shift if this is a power of two 1321 Node *mult = nullptr; 1322 1323 if( log2_con >= 0 ) 1324 mult = phase->transform( new LShiftLNode( divide, phase->intcon( log2_con ) ) ); 1325 else 1326 mult = phase->transform( new MulLNode( divide, phase->longcon( pos_con ) ) ); 1327 1328 // Finally, subtract the multiplied divided value from the original 1329 result = new SubLNode( in(1), mult ); 1330 } 1331 1332 // Now remove the bogus extra edges used to keep things alive 1333 hook->destruct(phase); 1334 1335 // return the value 1336 return result; 1337 } 1338 1339 //------------------------------Value------------------------------------------ 1340 const Type* ModLNode::Value(PhaseGVN* phase) const { 1341 // Either input is TOP ==> the result is TOP 1342 const Type *t1 = phase->type( in(1) ); 1343 const Type *t2 = phase->type( in(2) ); 1344 if( t1 == Type::TOP ) return Type::TOP; 1345 if( t2 == Type::TOP ) return Type::TOP; 1346 1347 // We always generate the dynamic check for 0. 1348 // 0 MOD X is 0 1349 if( t1 == TypeLong::ZERO ) return TypeLong::ZERO; 1350 // X MOD X is 0 1351 if (in(1) == in(2)) { 1352 return TypeLong::ZERO; 1353 } 1354 1355 // Either input is BOTTOM ==> the result is the local BOTTOM 1356 const Type *bot = bottom_type(); 1357 if( (t1 == bot) || (t2 == bot) || 1358 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1359 return bot; 1360 1361 const TypeLong *i1 = t1->is_long(); 1362 const TypeLong *i2 = t2->is_long(); 1363 if( !i1->is_con() || !i2->is_con() ) { 1364 if( i1->_lo >= CONST64(0) && i2->_lo >= CONST64(0) ) 1365 return TypeLong::POS; 1366 // If both numbers are not constants, we know little. 1367 return TypeLong::LONG; 1368 } 1369 // Mod by zero? Throw exception at runtime! 1370 if( !i2->get_con() ) return TypeLong::POS; 1371 1372 // We must be modulo'ing 2 float constants. 1373 // Check for min_jint % '-1', result is defined to be '0'. 1374 if( i1->get_con() == min_jlong && i2->get_con() == -1 ) 1375 return TypeLong::ZERO; 1376 1377 return TypeLong::make( i1->get_con() % i2->get_con() ); 1378 } 1379 1380 1381 //============================================================================= 1382 //------------------------------Value------------------------------------------ 1383 const Type* ModFNode::Value(PhaseGVN* phase) const { 1384 // Either input is TOP ==> the result is TOP 1385 const Type *t1 = phase->type( in(1) ); 1386 const Type *t2 = phase->type( in(2) ); 1387 if( t1 == Type::TOP ) return Type::TOP; 1388 if( t2 == Type::TOP ) return Type::TOP; 1389 1390 // Either input is BOTTOM ==> the result is the local BOTTOM 1391 const Type *bot = bottom_type(); 1392 if( (t1 == bot) || (t2 == bot) || 1393 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1394 return bot; 1395 1396 // If either number is not a constant, we know nothing. 1397 if ((t1->base() != Type::FloatCon) || (t2->base() != Type::FloatCon)) { 1398 return Type::FLOAT; // note: x%x can be either NaN or 0 1399 } 1400 1401 float f1 = t1->getf(); 1402 float f2 = t2->getf(); 1403 jint x1 = jint_cast(f1); // note: *(int*)&f1, not just (int)f1 1404 jint x2 = jint_cast(f2); 1405 1406 // If either is a NaN, return an input NaN 1407 if (g_isnan(f1)) return t1; 1408 if (g_isnan(f2)) return t2; 1409 1410 // If an operand is infinity or the divisor is +/- zero, punt. 1411 if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jint) 1412 return Type::FLOAT; 1413 1414 // We must be modulo'ing 2 float constants. 1415 // Make sure that the sign of the fmod is equal to the sign of the dividend 1416 jint xr = jint_cast(fmod(f1, f2)); 1417 if ((x1 ^ xr) < 0) { 1418 xr ^= min_jint; 1419 } 1420 1421 return TypeF::make(jfloat_cast(xr)); 1422 } 1423 1424 //============================================================================= 1425 //------------------------------Idealize--------------------------------------- 1426 Node *UModLNode::Ideal(PhaseGVN *phase, bool can_reshape) { 1427 return unsigned_mod_ideal<TypeLong, julong>(phase, can_reshape, this); 1428 } 1429 1430 const Type* UModLNode::Value(PhaseGVN* phase) const { 1431 return unsigned_mod_value<TypeLong, julong, jlong>(phase, this); 1432 } 1433 1434 //============================================================================= 1435 //------------------------------Value------------------------------------------ 1436 const Type* ModDNode::Value(PhaseGVN* phase) const { 1437 // Either input is TOP ==> the result is TOP 1438 const Type *t1 = phase->type( in(1) ); 1439 const Type *t2 = phase->type( in(2) ); 1440 if( t1 == Type::TOP ) return Type::TOP; 1441 if( t2 == Type::TOP ) return Type::TOP; 1442 1443 // Either input is BOTTOM ==> the result is the local BOTTOM 1444 const Type *bot = bottom_type(); 1445 if( (t1 == bot) || (t2 == bot) || 1446 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) 1447 return bot; 1448 1449 // If either number is not a constant, we know nothing. 1450 if ((t1->base() != Type::DoubleCon) || (t2->base() != Type::DoubleCon)) { 1451 return Type::DOUBLE; // note: x%x can be either NaN or 0 1452 } 1453 1454 double f1 = t1->getd(); 1455 double f2 = t2->getd(); 1456 jlong x1 = jlong_cast(f1); // note: *(long*)&f1, not just (long)f1 1457 jlong x2 = jlong_cast(f2); 1458 1459 // If either is a NaN, return an input NaN 1460 if (g_isnan(f1)) return t1; 1461 if (g_isnan(f2)) return t2; 1462 1463 // If an operand is infinity or the divisor is +/- zero, punt. 1464 if (!g_isfinite(f1) || !g_isfinite(f2) || x2 == 0 || x2 == min_jlong) 1465 return Type::DOUBLE; 1466 1467 // We must be modulo'ing 2 double constants. 1468 // Make sure that the sign of the fmod is equal to the sign of the dividend 1469 jlong xr = jlong_cast(fmod(f1, f2)); 1470 if ((x1 ^ xr) < 0) { 1471 xr ^= min_jlong; 1472 } 1473 1474 return TypeD::make(jdouble_cast(xr)); 1475 } 1476 1477 //============================================================================= 1478 1479 DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) { 1480 init_req(0, c); 1481 init_req(1, dividend); 1482 init_req(2, divisor); 1483 } 1484 1485 DivModNode* DivModNode::make(Node* div_or_mod, BasicType bt, bool is_unsigned) { 1486 assert(bt == T_INT || bt == T_LONG, "only int or long input pattern accepted"); 1487 1488 if (bt == T_INT) { 1489 if (is_unsigned) { 1490 return UDivModINode::make(div_or_mod); 1491 } else { 1492 return DivModINode::make(div_or_mod); 1493 } 1494 } else { 1495 if (is_unsigned) { 1496 return UDivModLNode::make(div_or_mod); 1497 } else { 1498 return DivModLNode::make(div_or_mod); 1499 } 1500 } 1501 } 1502 1503 //------------------------------make------------------------------------------ 1504 DivModINode* DivModINode::make(Node* div_or_mod) { 1505 Node* n = div_or_mod; 1506 assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI, 1507 "only div or mod input pattern accepted"); 1508 1509 DivModINode* divmod = new DivModINode(n->in(0), n->in(1), n->in(2)); 1510 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num); 1511 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num); 1512 return divmod; 1513 } 1514 1515 //------------------------------make------------------------------------------ 1516 DivModLNode* DivModLNode::make(Node* div_or_mod) { 1517 Node* n = div_or_mod; 1518 assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL, 1519 "only div or mod input pattern accepted"); 1520 1521 DivModLNode* divmod = new DivModLNode(n->in(0), n->in(1), n->in(2)); 1522 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num); 1523 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num); 1524 return divmod; 1525 } 1526 1527 //------------------------------match------------------------------------------ 1528 // return result(s) along with their RegMask info 1529 Node *DivModINode::match(const ProjNode *proj, const Matcher *match, const RegMask* mask) { 1530 uint ideal_reg = proj->ideal_reg(); 1531 RegMask rm; 1532 if (proj->_con == div_proj_num) { 1533 rm = match->divI_proj_mask(); 1534 } else { 1535 assert(proj->_con == mod_proj_num, "must be div or mod projection"); 1536 rm = match->modI_proj_mask(); 1537 } 1538 return new MachProjNode(this, proj->_con, rm, ideal_reg); 1539 } 1540 1541 1542 //------------------------------match------------------------------------------ 1543 // return result(s) along with their RegMask info 1544 Node *DivModLNode::match(const ProjNode *proj, const Matcher *match, const RegMask* mask) { 1545 uint ideal_reg = proj->ideal_reg(); 1546 RegMask rm; 1547 if (proj->_con == div_proj_num) { 1548 rm = match->divL_proj_mask(); 1549 } else { 1550 assert(proj->_con == mod_proj_num, "must be div or mod projection"); 1551 rm = match->modL_proj_mask(); 1552 } 1553 return new MachProjNode(this, proj->_con, rm, ideal_reg); 1554 } 1555 1556 //------------------------------make------------------------------------------ 1557 UDivModINode* UDivModINode::make(Node* div_or_mod) { 1558 Node* n = div_or_mod; 1559 assert(n->Opcode() == Op_UDivI || n->Opcode() == Op_UModI, 1560 "only div or mod input pattern accepted"); 1561 1562 UDivModINode* divmod = new UDivModINode(n->in(0), n->in(1), n->in(2)); 1563 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num); 1564 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num); 1565 return divmod; 1566 } 1567 1568 //------------------------------make------------------------------------------ 1569 UDivModLNode* UDivModLNode::make(Node* div_or_mod) { 1570 Node* n = div_or_mod; 1571 assert(n->Opcode() == Op_UDivL || n->Opcode() == Op_UModL, 1572 "only div or mod input pattern accepted"); 1573 1574 UDivModLNode* divmod = new UDivModLNode(n->in(0), n->in(1), n->in(2)); 1575 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num); 1576 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num); 1577 return divmod; 1578 } 1579 1580 //------------------------------match------------------------------------------ 1581 // return result(s) along with their RegMask info 1582 Node* UDivModINode::match(const ProjNode* proj, const Matcher* match, const RegMask* mask) { 1583 uint ideal_reg = proj->ideal_reg(); 1584 RegMask rm; 1585 if (proj->_con == div_proj_num) { 1586 rm = match->divI_proj_mask(); 1587 } else { 1588 assert(proj->_con == mod_proj_num, "must be div or mod projection"); 1589 rm = match->modI_proj_mask(); 1590 } 1591 return new MachProjNode(this, proj->_con, rm, ideal_reg); 1592 } 1593 1594 1595 //------------------------------match------------------------------------------ 1596 // return result(s) along with their RegMask info 1597 Node* UDivModLNode::match( const ProjNode* proj, const Matcher* match, const RegMask* mask) { 1598 uint ideal_reg = proj->ideal_reg(); 1599 RegMask rm; 1600 if (proj->_con == div_proj_num) { 1601 rm = match->divL_proj_mask(); 1602 } else { 1603 assert(proj->_con == mod_proj_num, "must be div or mod projection"); 1604 rm = match->modL_proj_mask(); 1605 } 1606 return new MachProjNode(this, proj->_con, rm, ideal_reg); 1607 } --- EOF ---