1 /*
2 * Copyright (c) 1997, 2026, 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 "memory/allocation.inline.hpp"
26 #include "opto/addnode.hpp"
27 #include "opto/connode.hpp"
28 #include "opto/convertnode.hpp"
29 #include "opto/divnode.hpp"
30 #include "opto/machnode.hpp"
31 #include "opto/matcher.hpp"
32 #include "opto/movenode.hpp"
33 #include "opto/mulnode.hpp"
34 #include "opto/phaseX.hpp"
35 #include "opto/runtime.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 ModFloatingNode::ModFloatingNode(Compile* C, const TypeFunc* tf, address addr, const char* name) : CallLeafPureNode(tf, addr, name) {
46 add_flag(Flag_is_macro);
47 C->add_macro_node(this);
48 }
49
50 ModDNode::ModDNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::drem), "drem") {
51 init_req(TypeFunc::Parms + 0, a);
52 init_req(TypeFunc::Parms + 1, C->top());
53 init_req(TypeFunc::Parms + 2, b);
54 init_req(TypeFunc::Parms + 3, C->top());
55 }
56
57 ModFNode::ModFNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::modf_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::frem), "frem") {
58 init_req(TypeFunc::Parms + 0, a);
59 init_req(TypeFunc::Parms + 1, b);
60 }
61
62 //----------------------magic_int_divide_constants-----------------------------
63 // Compute magic multiplier and shift constant for converting a 32 bit divide
64 // by constant into a multiply/shift/add series. Return false if calculations
65 // fail.
66 //
67 // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
68 // minor type name and parameter changes.
69 static bool magic_int_divide_constants(jint d, jint &M, jint &s) {
70 int32_t p;
71 uint32_t ad, anc, delta, q1, r1, q2, r2, t;
72 const uint32_t two31 = 0x80000000L; // 2**31.
73
74 ad = ABS(d);
75 if (d == 0 || d == 1) return false;
76 t = two31 + ((uint32_t)d >> 31);
77 anc = t - 1 - t%ad; // Absolute value of nc.
78 p = 31; // Init. p.
79 q1 = two31/anc; // Init. q1 = 2**p/|nc|.
80 r1 = two31 - q1*anc; // Init. r1 = rem(2**p, |nc|).
81 q2 = two31/ad; // Init. q2 = 2**p/|d|.
82 r2 = two31 - q2*ad; // Init. r2 = rem(2**p, |d|).
83 do {
84 p = p + 1;
85 q1 = 2*q1; // Update q1 = 2**p/|nc|.
86 r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
87 if (r1 >= anc) { // (Must be an unsigned
88 q1 = q1 + 1; // comparison here).
89 r1 = r1 - anc;
90 }
91 q2 = 2*q2; // Update q2 = 2**p/|d|.
92 r2 = 2*r2; // Update r2 = rem(2**p, |d|).
93 if (r2 >= ad) { // (Must be an unsigned
94 q2 = q2 + 1; // comparison here).
95 r2 = r2 - ad;
96 }
97 delta = ad - r2;
98 } while (q1 < delta || (q1 == delta && r1 == 0));
99
100 M = q2 + 1;
101 if (d < 0) M = -M; // Magic number and
102 s = p - 32; // shift amount to return.
103
104 return true;
105 }
106
107 //--------------------------transform_int_divide-------------------------------
108 // Convert a division by constant divisor into an alternate Ideal graph.
109 // Return null if no transformation occurs.
110 static Node *transform_int_divide( PhaseGVN *phase, Node *dividend, jint divisor ) {
111
112 // Check for invalid divisors
113 assert( divisor != 0 && divisor != min_jint,
114 "bad divisor for transforming to long multiply" );
115
116 bool d_pos = divisor >= 0;
117 jint d = d_pos ? divisor : -divisor;
118 const int N = 32;
119
120 // Result
121 Node *q = nullptr;
122
123 if (d == 1) {
124 // division by +/- 1
125 if (!d_pos) {
126 // Just negate the value
127 q = new SubINode(phase->intcon(0), dividend);
128 }
129 } else if ( is_power_of_2(d) ) {
130 // division by +/- a power of 2
131
132 // See if we can simply do a shift without rounding
133 bool needs_rounding = true;
134 const Type *dt = phase->type(dividend);
135 const TypeInt *dti = dt->isa_int();
136 if (dti && dti->_lo >= 0) {
137 // we don't need to round a positive dividend
138 needs_rounding = false;
139 } else if( dividend->Opcode() == Op_AndI ) {
140 // An AND mask of sufficient size clears the low bits and
141 // I can avoid rounding.
142 const TypeInt *andconi_t = phase->type( dividend->in(2) )->isa_int();
143 if( andconi_t && andconi_t->is_con() ) {
144 jint andconi = andconi_t->get_con();
145 if( andconi < 0 && is_power_of_2(-andconi) && (-andconi) >= d ) {
146 if( (-andconi) == d ) // Remove AND if it clears bits which will be shifted
147 dividend = dividend->in(1);
148 needs_rounding = false;
149 }
150 }
151 }
152
153 // Add rounding to the shift to handle the sign bit
154 int l = log2i_graceful(d - 1) + 1;
155 if (needs_rounding) {
156 // Divide-by-power-of-2 can be made into a shift, but you have to do
157 // more math for the rounding. You need to add 0 for positive
158 // numbers, and "i-1" for negative numbers. Example: i=4, so the
159 // shift is by 2. You need to add 3 to negative dividends and 0 to
160 // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
161 // (-2+3)>>2 becomes 0, etc.
162
163 // Compute 0 or -1, based on sign bit
164 Node *sign = phase->transform(new RShiftINode(dividend, phase->intcon(N - 1)));
165 // Mask sign bit to the low sign bits
166 Node *round = phase->transform(new URShiftINode(sign, phase->intcon(N - l)));
167 // Round up before shifting
168 dividend = phase->transform(new AddINode(dividend, round));
169 }
170
171 // Shift for division
172 q = new RShiftINode(dividend, phase->intcon(l));
173
174 if (!d_pos) {
175 q = new SubINode(phase->intcon(0), phase->transform(q));
176 }
177 } else {
178 // Attempt the jint constant divide -> multiply transform found in
179 // "Division by Invariant Integers using Multiplication"
180 // by Granlund and Montgomery
181 // See also "Hacker's Delight", chapter 10 by Warren.
182
183 jint magic_const;
184 jint shift_const;
185 if (magic_int_divide_constants(d, magic_const, shift_const)) {
186 Node *magic = phase->longcon(magic_const);
187 Node *dividend_long = phase->transform(new ConvI2LNode(dividend));
188
189 // Compute the high half of the dividend x magic multiplication
190 Node *mul_hi = phase->transform(new MulLNode(dividend_long, magic));
191
192 if (magic_const < 0) {
193 mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N)));
194 mul_hi = phase->transform(new ConvL2INode(mul_hi));
195
196 // The magic multiplier is too large for a 32 bit constant. We've adjusted
197 // it down by 2^32, but have to add 1 dividend back in after the multiplication.
198 // This handles the "overflow" case described by Granlund and Montgomery.
199 mul_hi = phase->transform(new AddINode(dividend, mul_hi));
200
201 // Shift over the (adjusted) mulhi
202 if (shift_const != 0) {
203 mul_hi = phase->transform(new RShiftINode(mul_hi, phase->intcon(shift_const)));
204 }
205 } else {
206 // No add is required, we can merge the shifts together.
207 mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
208 mul_hi = phase->transform(new ConvL2INode(mul_hi));
209 }
210
211 // Get a 0 or -1 from the sign of the dividend.
212 Node *addend0 = mul_hi;
213 Node *addend1 = phase->transform(new RShiftINode(dividend, phase->intcon(N-1)));
214
215 // If the divisor is negative, swap the order of the input addends;
216 // this has the effect of negating the quotient.
217 if (!d_pos) {
218 Node *temp = addend0; addend0 = addend1; addend1 = temp;
219 }
220
221 // Adjust the final quotient by subtracting -1 (adding 1)
222 // from the mul_hi.
223 q = new SubINode(addend0, addend1);
224 }
225 }
226
227 return q;
228 }
229
230 //---------------------magic_long_divide_constants-----------------------------
231 // Compute magic multiplier and shift constant for converting a 64 bit divide
232 // by constant into a multiply/shift/add series. Return false if calculations
233 // fail.
234 //
235 // Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
236 // minor type name and parameter changes. Adjusted to 64 bit word width.
237 static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) {
238 int64_t p;
239 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
240 const uint64_t two63 = UCONST64(0x8000000000000000); // 2**63.
241
242 ad = ABS(d);
243 if (d == 0 || d == 1) return false;
244 t = two63 + ((uint64_t)d >> 63);
245 anc = t - 1 - t%ad; // Absolute value of nc.
246 p = 63; // Init. p.
247 q1 = two63/anc; // Init. q1 = 2**p/|nc|.
248 r1 = two63 - q1*anc; // Init. r1 = rem(2**p, |nc|).
249 q2 = two63/ad; // Init. q2 = 2**p/|d|.
250 r2 = two63 - q2*ad; // Init. r2 = rem(2**p, |d|).
251 do {
252 p = p + 1;
253 q1 = 2*q1; // Update q1 = 2**p/|nc|.
254 r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
255 if (r1 >= anc) { // (Must be an unsigned
256 q1 = q1 + 1; // comparison here).
257 r1 = r1 - anc;
258 }
259 q2 = 2*q2; // Update q2 = 2**p/|d|.
260 r2 = 2*r2; // Update r2 = rem(2**p, |d|).
261 if (r2 >= ad) { // (Must be an unsigned
262 q2 = q2 + 1; // comparison here).
263 r2 = r2 - ad;
264 }
265 delta = ad - r2;
266 } while (q1 < delta || (q1 == delta && r1 == 0));
267
268 M = q2 + 1;
269 if (d < 0) M = -M; // Magic number and
270 s = p - 64; // shift amount to return.
271
272 return true;
273 }
274
275 //---------------------long_by_long_mulhi--------------------------------------
276 // Generate ideal node graph for upper half of a 64 bit x 64 bit multiplication
277 static Node* long_by_long_mulhi(PhaseGVN* phase, Node* dividend, jlong magic_const) {
278 // If the architecture supports a 64x64 mulhi, there is
279 // no need to synthesize it in ideal nodes.
280 if (Matcher::has_match_rule(Op_MulHiL)) {
281 Node* v = phase->longcon(magic_const);
282 return new MulHiLNode(dividend, v);
283 }
284
285 // Taken from Hacker's Delight, Fig. 8-2. Multiply high signed.
286 //
287 // int mulhs(int u, int v) {
288 // unsigned u0, v0, w0;
289 // int u1, v1, w1, w2, t;
290 //
291 // u0 = u & 0xFFFF; u1 = u >> 16;
292 // v0 = v & 0xFFFF; v1 = v >> 16;
293 // w0 = u0*v0;
294 // t = u1*v0 + (w0 >> 16);
295 // w1 = t & 0xFFFF;
296 // w2 = t >> 16;
297 // w1 = u0*v1 + w1;
298 // return u1*v1 + w2 + (w1 >> 16);
299 // }
300 //
301 // Note: The version above is for 32x32 multiplications, while the
302 // following inline comments are adapted to 64x64.
303
304 const int N = 64;
305
306 // Dummy node to keep intermediate nodes alive during construction
307 Node* hook = new Node(4);
308
309 // u0 = u & 0xFFFFFFFF; u1 = u >> 32;
310 Node* u0 = phase->transform(new AndLNode(dividend, phase->longcon(0xFFFFFFFF)));
311 Node* u1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N / 2)));
312 hook->init_req(0, u0);
313 hook->init_req(1, u1);
314
315 // v0 = v & 0xFFFFFFFF; v1 = v >> 32;
316 Node* v0 = phase->longcon(magic_const & 0xFFFFFFFF);
317 Node* v1 = phase->longcon(magic_const >> (N / 2));
318
319 // w0 = u0*v0;
320 Node* w0 = phase->transform(new MulLNode(u0, v0));
321
322 // t = u1*v0 + (w0 >> 32);
323 Node* u1v0 = phase->transform(new MulLNode(u1, v0));
324 Node* temp = phase->transform(new URShiftLNode(w0, phase->intcon(N / 2)));
325 Node* t = phase->transform(new AddLNode(u1v0, temp));
326 hook->init_req(2, t);
327
328 // w1 = t & 0xFFFFFFFF;
329 Node* w1 = phase->transform(new AndLNode(t, phase->longcon(0xFFFFFFFF)));
330 hook->init_req(3, w1);
331
332 // w2 = t >> 32;
333 Node* w2 = phase->transform(new RShiftLNode(t, phase->intcon(N / 2)));
334
335 // w1 = u0*v1 + w1;
336 Node* u0v1 = phase->transform(new MulLNode(u0, v1));
337 w1 = phase->transform(new AddLNode(u0v1, w1));
338
339 // return u1*v1 + w2 + (w1 >> 32);
340 Node* u1v1 = phase->transform(new MulLNode(u1, v1));
341 Node* temp1 = phase->transform(new AddLNode(u1v1, w2));
342 Node* temp2 = phase->transform(new RShiftLNode(w1, phase->intcon(N / 2)));
343
344 // Remove the bogus extra edges used to keep things alive
345 hook->destruct(phase);
346
347 return new AddLNode(temp1, temp2);
348 }
349
350
351 //--------------------------transform_long_divide------------------------------
352 // Convert a division by constant divisor into an alternate Ideal graph.
353 // Return null if no transformation occurs.
354 static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) {
355 // Check for invalid divisors
356 assert( divisor != 0L && divisor != min_jlong,
357 "bad divisor for transforming to long multiply" );
358
359 bool d_pos = divisor >= 0;
360 jlong d = d_pos ? divisor : -divisor;
361 const int N = 64;
362
363 // Result
364 Node *q = nullptr;
365
366 if (d == 1) {
367 // division by +/- 1
368 if (!d_pos) {
369 // Just negate the value
370 q = new SubLNode(phase->longcon(0), dividend);
371 }
372 } else if ( is_power_of_2(d) ) {
373
374 // division by +/- a power of 2
375
376 // See if we can simply do a shift without rounding
377 bool needs_rounding = true;
378 const Type *dt = phase->type(dividend);
379 const TypeLong *dtl = dt->isa_long();
380
381 if (dtl && dtl->_lo > 0) {
382 // we don't need to round a positive dividend
383 needs_rounding = false;
384 } else if( dividend->Opcode() == Op_AndL ) {
385 // An AND mask of sufficient size clears the low bits and
386 // I can avoid rounding.
387 const TypeLong *andconl_t = phase->type( dividend->in(2) )->isa_long();
388 if( andconl_t && andconl_t->is_con() ) {
389 jlong andconl = andconl_t->get_con();
390 if( andconl < 0 && is_power_of_2(-andconl) && (-andconl) >= d ) {
391 if( (-andconl) == d ) // Remove AND if it clears bits which will be shifted
392 dividend = dividend->in(1);
393 needs_rounding = false;
394 }
395 }
396 }
397
398 // Add rounding to the shift to handle the sign bit
399 int l = log2i_graceful(d - 1) + 1;
400 if (needs_rounding) {
401 // Divide-by-power-of-2 can be made into a shift, but you have to do
402 // more math for the rounding. You need to add 0 for positive
403 // numbers, and "i-1" for negative numbers. Example: i=4, so the
404 // shift is by 2. You need to add 3 to negative dividends and 0 to
405 // positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
406 // (-2+3)>>2 becomes 0, etc.
407
408 // Compute 0 or -1, based on sign bit
409 Node *sign = phase->transform(new RShiftLNode(dividend, phase->intcon(N - 1)));
410 // Mask sign bit to the low sign bits
411 Node *round = phase->transform(new URShiftLNode(sign, phase->intcon(N - l)));
412 // Round up before shifting
413 dividend = phase->transform(new AddLNode(dividend, round));
414 }
415
416 // Shift for division
417 q = new RShiftLNode(dividend, phase->intcon(l));
418
419 if (!d_pos) {
420 q = new SubLNode(phase->longcon(0), phase->transform(q));
421 }
422 } else {
423 // Attempt the jlong constant divide -> multiply transform found in
424 // "Division by Invariant Integers using Multiplication"
425 // by Granlund and Montgomery
426 // See also "Hacker's Delight", chapter 10 by Warren.
427
428 jlong magic_const;
429 jint shift_const;
430 if (magic_long_divide_constants(d, magic_const, shift_const)) {
431 // Compute the high half of the dividend x magic multiplication
432 Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const));
433
434 // The high half of the 128-bit multiply is computed.
435 if (magic_const < 0) {
436 // The magic multiplier is too large for a 64 bit constant. We've adjusted
437 // it down by 2^64, but have to add 1 dividend back in after the multiplication.
438 // This handles the "overflow" case described by Granlund and Montgomery.
439 mul_hi = phase->transform(new AddLNode(dividend, mul_hi));
440 }
441
442 // Shift over the (adjusted) mulhi
443 if (shift_const != 0) {
444 mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(shift_const)));
445 }
446
447 // Get a 0 or -1 from the sign of the dividend.
448 Node *addend0 = mul_hi;
449 Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1)));
450
451 // If the divisor is negative, swap the order of the input addends;
452 // this has the effect of negating the quotient.
453 if (!d_pos) {
454 Node *temp = addend0; addend0 = addend1; addend1 = temp;
455 }
456
457 // Adjust the final quotient by subtracting -1 (adding 1)
458 // from the mul_hi.
459 q = new SubLNode(addend0, addend1);
460 }
461 }
462
463 return q;
464 }
465
466 template <typename TypeClass, typename Unsigned>
467 Node* unsigned_div_ideal(PhaseGVN* phase, bool can_reshape, Node* div) {
468 // Check for dead control input
469 if (div->in(0) != nullptr && div->remove_dead_region(phase, can_reshape)) {
470 return div;
471 }
472 // Don't bother trying to transform a dead node
473 if (div->in(0) != nullptr && div->in(0)->is_top()) {
474 return nullptr;
475 }
476
477 const Type* t = phase->type(div->in(2));
478 if (t == Type::TOP) {
479 return nullptr;
480 }
481 const TypeClass* type_divisor = t->cast<TypeClass>();
482
483 // Check for useless control input
484 // Check for excluding div-zero case
485 if (div->in(0) != nullptr && (type_divisor->_hi < 0 || type_divisor->_lo > 0)) {
486 div->set_req(0, nullptr); // Yank control input
487 return div;
488 }
489
490 if (!type_divisor->is_con()) {
491 return nullptr;
492 }
493 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con()); // Get divisor
494
495 if (divisor == 0 || divisor == 1) {
496 return nullptr; // Dividing by zero constant does not idealize
497 }
498
499 if (is_power_of_2(divisor)) {
500 return make_urshift<TypeClass>(div->in(1), phase->intcon(log2i_graceful(divisor)));
501 }
502
503 return nullptr;
504 }
505
506 template<typename IntegerType>
507 static const IntegerType* compute_signed_div_type(const IntegerType* i1, const IntegerType* i2) {
508 typedef typename IntegerType::NativeType NativeType;
509 assert(!i2->is_con() || i2->get_con() != 0, "Can't handle zero constant divisor");
510 int widen = MAX2(i1->_widen, i2->_widen);
511
512 // Case A: divisor range spans zero (i2->_lo < 0 < i2->_hi)
513 // We split into two subproblems to avoid division by 0:
514 // - negative part: [i2->_lo, −1]
515 // - positive part: [1, i2->_hi]
516 // Then we union the results by taking the min of all lower‐bounds and
517 // the max of all upper‐bounds from the two halves.
518 if (i2->_lo < 0 && i2->_hi > 0) {
519 // Handle negative part of the divisor range
520 const IntegerType* neg_part = compute_signed_div_type(i1, IntegerType::make(i2->_lo, -1, widen));
521 // Handle positive part of the divisor range
522 const IntegerType* pos_part = compute_signed_div_type(i1, IntegerType::make(1, i2->_hi, widen));
523 // Merge results
524 NativeType new_lo = MIN2(neg_part->_lo, pos_part->_lo);
525 NativeType new_hi = MAX2(neg_part->_hi, pos_part->_hi);
526 assert(new_hi >= new_lo, "sanity");
527 return IntegerType::make(new_lo, new_hi, widen);
528 }
529
530 // Case B: divisor range does NOT span zero.
531 // Here i2 is entirely negative or entirely positive.
532 // Then i1/i2 is monotonic in i1 and i2 (when i2 keeps the same sign).
533 // Therefore the extrema occur at the four “corners”:
534 // (i1->_lo, i2->_hi), (i1->_lo, i2->_lo), (i1->_hi, i2->_lo), (i1->_hi, i2->_hi).
535 // We compute all four and take the min and max.
536 // A special case handles overflow when dividing the most‐negative value by −1.
537
538 // adjust i2 bounds to not include zero, as zero always throws
539 NativeType i2_lo = i2->_lo == 0 ? 1 : i2->_lo;
540 NativeType i2_hi = i2->_hi == 0 ? -1 : i2->_hi;
541 constexpr NativeType min_val = std::numeric_limits<NativeType>::min();
542 static_assert(min_val == min_jint || min_val == min_jlong, "min has to be either min_jint or min_jlong");
543 constexpr NativeType max_val = std::numeric_limits<NativeType>::max();
544 static_assert(max_val == max_jint || max_val == max_jlong, "max has to be either max_jint or max_jlong");
545
546 // Special overflow case: min_val / (-1) == min_val (cf. JVMS§6.5 idiv/ldiv)
547 // We need to be careful that we never run min_val / (-1) in C++ code, as this overflow is UB there
548 if (i1->_lo == min_val && i2_hi == -1) {
549 NativeType new_lo = min_val;
550 NativeType new_hi;
551 // compute new_hi depending on whether divisor or dividend is non-constant.
552 // i2 is purely in the negative domain here (as i2_hi is -1)
553 // which means the maximum value this division can yield is either
554 if (!i1->is_con()) {
555 // a) non-constant dividend: i1 could be min_val + 1.
556 // -> i1 / i2 = (min_val + 1) / -1 = max_val is possible.
557 new_hi = max_val;
558 assert((min_val + 1) / -1 == new_hi, "new_hi should be max_val");
559 } else if (i2_lo != i2_hi) {
560 // b) i1 is constant min_val, i2 is non-constant.
561 // if i2 = -1 -> i1 / i2 = min_val / -1 = min_val
562 // if i2 < -1 -> i1 / i2 <= min_val / -2 = (max_val / 2) + 1
563 new_hi = (max_val / 2) + 1;
564 assert(min_val / -2 == new_hi, "new_hi should be (max_val / 2) + 1)");
565 } else {
566 // c) i1 is constant min_val, i2 is constant -1.
567 // -> i1 / i2 = min_val / -1 = min_val
568 new_hi = min_val;
569 }
570
571 #ifdef ASSERT
572 // validate new_hi for non-constant divisor
573 if (i2_lo != i2_hi) {
574 assert(i2_lo != -1, "Special case not possible here, as i2_lo has to be < i2_hi");
575 NativeType result = i1->_lo / i2_lo;
576 assert(new_hi >= result, "computed wrong value for new_hi");
577 }
578
579 // validate new_hi for non-constant dividend
580 if (!i1->is_con()) {
581 assert(i2_hi > min_val, "Special case not possible here, as i1->_hi has to be > min");
582 NativeType result1 = i1->_hi / i2_lo;
583 NativeType result2 = i1->_hi / i2_hi;
584 assert(new_hi >= result1 && new_hi >= result2, "computed wrong value for new_hi");
585 }
586 #endif
587
588 return IntegerType::make(new_lo, new_hi, widen);
589 }
590 assert((i1->_lo != min_val && i1->_hi != min_val) || (i2_hi != -1 && i2_lo != -1), "should have filtered out before");
591
592 // Special case not possible here, calculate all corners normally
593 NativeType corner1 = i1->_lo / i2_lo;
594 NativeType corner2 = i1->_lo / i2_hi;
595 NativeType corner3 = i1->_hi / i2_lo;
596 NativeType corner4 = i1->_hi / i2_hi;
597
598 NativeType new_lo = MIN4(corner1, corner2, corner3, corner4);
599 NativeType new_hi = MAX4(corner1, corner2, corner3, corner4);
600 return IntegerType::make(new_lo, new_hi, widen);
601 }
602
603 //=============================================================================
604 //------------------------------Identity---------------------------------------
605 // If the divisor is 1, we are an identity on the dividend.
606 Node* DivINode::Identity(PhaseGVN* phase) {
607 return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
608 }
609
610 //------------------------------Idealize---------------------------------------
611 // Divides can be changed to multiplies and/or shifts
612 Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
613 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
614 // Don't bother trying to transform a dead node
615 if( in(0) && in(0)->is_top() ) return nullptr;
616
617 const Type *t = phase->type( in(2) );
618 if( t == TypeInt::ONE ) // Identity?
619 return nullptr; // Skip it
620
621 const TypeInt *ti = t->isa_int();
622 if( !ti ) return nullptr;
623
624 // Check for useless control input
625 // Check for excluding div-zero case
626 if (in(0) && (ti->_hi < 0 || ti->_lo > 0)) {
627 set_req(0, nullptr); // Yank control input
628 return this;
629 }
630
631 if( !ti->is_con() ) return nullptr;
632 jint i = ti->get_con(); // Get divisor
633
634 if (i == 0) return nullptr; // Dividing by zero constant does not idealize
635
636 // Dividing by MININT does not optimize as a power-of-2 shift.
637 if( i == min_jint ) return nullptr;
638
639 return transform_int_divide( phase, in(1), i );
640 }
641
642 //------------------------------Value------------------------------------------
643 // A DivINode divides its inputs. The third input is a Control input, used to
644 // prevent hoisting the divide above an unsafe test.
645 const Type* DivINode::Value(PhaseGVN* phase) const {
646 // Either input is TOP ==> the result is TOP
647 const Type* t1 = phase->type(in(1));
648 const Type* t2 = phase->type(in(2));
649 if (t1 == Type::TOP || t2 == Type::TOP) {
650 return Type::TOP;
651 }
652
653 if (t2 == TypeInt::ZERO) {
654 // this division will always throw an exception
655 return Type::TOP;
656 }
657
658 // x/x == 1 since we always generate the dynamic divisor check for 0.
659 if (in(1) == in(2)) {
660 return TypeInt::ONE;
661 }
662
663 const TypeInt* i1 = t1->is_int();
664 const TypeInt* i2 = t2->is_int();
665
666 return compute_signed_div_type<TypeInt>(i1, i2);
667 }
668
669
670 //=============================================================================
671 //------------------------------Identity---------------------------------------
672 // If the divisor is 1, we are an identity on the dividend.
673 Node* DivLNode::Identity(PhaseGVN* phase) {
674 return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
675 }
676
677 //------------------------------Idealize---------------------------------------
678 // Dividing by a power of 2 is a shift.
679 Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
680 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
681 // Don't bother trying to transform a dead node
682 if( in(0) && in(0)->is_top() ) return nullptr;
683
684 const Type *t = phase->type( in(2) );
685 if( t == TypeLong::ONE ) // Identity?
686 return nullptr; // Skip it
687
688 const TypeLong *tl = t->isa_long();
689 if( !tl ) return nullptr;
690
691 // Check for useless control input
692 // Check for excluding div-zero case
693 if (in(0) && (tl->_hi < 0 || tl->_lo > 0)) {
694 set_req(0, nullptr); // Yank control input
695 return this;
696 }
697
698 if( !tl->is_con() ) return nullptr;
699 jlong l = tl->get_con(); // Get divisor
700
701 if (l == 0) return nullptr; // Dividing by zero constant does not idealize
702
703 // Dividing by MINLONG does not optimize as a power-of-2 shift.
704 if( l == min_jlong ) return nullptr;
705
706 return transform_long_divide( phase, in(1), l );
707 }
708
709 //------------------------------Value------------------------------------------
710 // A DivLNode divides its inputs. The third input is a Control input, used to
711 // prevent hoisting the divide above an unsafe test.
712 const Type* DivLNode::Value(PhaseGVN* phase) const {
713 // Either input is TOP ==> the result is TOP
714 const Type* t1 = phase->type(in(1));
715 const Type* t2 = phase->type(in(2));
716 if (t1 == Type::TOP || t2 == Type::TOP) {
717 return Type::TOP;
718 }
719
720 if (t2 == TypeLong::ZERO) {
721 // this division will always throw an exception
722 return Type::TOP;
723 }
724
725 // x/x == 1 since we always generate the dynamic divisor check for 0.
726 if (in(1) == in(2)) {
727 return TypeLong::ONE;
728 }
729
730 const TypeLong* i1 = t1->is_long();
731 const TypeLong* i2 = t2->is_long();
732
733 return compute_signed_div_type<TypeLong>(i1, i2);
734 }
735
736
737 //=============================================================================
738 //------------------------------Value------------------------------------------
739 // An DivFNode divides its inputs. The third input is a Control input, used to
740 // prevent hoisting the divide above an unsafe test.
741 const Type* DivFNode::Value(PhaseGVN* phase) const {
742 // Either input is TOP ==> the result is TOP
743 const Type *t1 = phase->type( in(1) );
744 const Type *t2 = phase->type( in(2) );
745 if( t1 == Type::TOP ) return Type::TOP;
746 if( t2 == Type::TOP ) return Type::TOP;
747
748 // Either input is BOTTOM ==> the result is the local BOTTOM
749 const Type *bot = bottom_type();
750 if( (t1 == bot) || (t2 == bot) ||
751 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
752 return bot;
753
754 // x/x == 1, we ignore 0/0.
755 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
756 // Does not work for variables because of NaN's
757 if (in(1) == in(2) && t1->base() == Type::FloatCon &&
758 !g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) { // could be negative ZERO or NaN
759 return TypeF::ONE;
760 }
761
762 if( t2 == TypeF::ONE )
763 return t1;
764
765 // If divisor is a constant and not zero, divide them numbers
766 if( t1->base() == Type::FloatCon &&
767 t2->base() == Type::FloatCon &&
768 t2->getf() != 0.0 ) // could be negative zero
769 return TypeF::make( t1->getf()/t2->getf() );
770
771 // If the dividend is a constant zero
772 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
773 // Test TypeF::ZERO is not sufficient as it could be negative zero
774
775 if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
776 return TypeF::ZERO;
777
778 // Otherwise we give up all hope
779 return Type::FLOAT;
780 }
781
782 //------------------------------isA_Copy---------------------------------------
783 // Dividing by self is 1.
784 // If the divisor is 1, we are an identity on the dividend.
785 Node* DivFNode::Identity(PhaseGVN* phase) {
786 return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
787 }
788
789
790 //------------------------------Idealize---------------------------------------
791 Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
792 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
793 // Don't bother trying to transform a dead node
794 if( in(0) && in(0)->is_top() ) return nullptr;
795
796 const Type *t2 = phase->type( in(2) );
797 if( t2 == TypeF::ONE ) // Identity?
798 return nullptr; // Skip it
799
800 const TypeF *tf = t2->isa_float_constant();
801 if( !tf ) return nullptr;
802 if( tf->base() != Type::FloatCon ) return nullptr;
803
804 // Check for out of range values
805 if( tf->is_nan() || !tf->is_finite() ) return nullptr;
806
807 // Get the value
808 float f = tf->getf();
809 int exp;
810
811 // Only for special case of dividing by a power of 2
812 if( frexp((double)f, &exp) != 0.5 ) return nullptr;
813
814 // Limit the range of acceptable exponents
815 if( exp < -126 || exp > 126 ) return nullptr;
816
817 // Compute the reciprocal
818 float reciprocal = ((float)1.0) / f;
819
820 assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
821
822 // return multiplication by the reciprocal
823 return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
824 }
825 //=============================================================================
826 //------------------------------Value------------------------------------------
827 // An DivHFNode divides its inputs. The third input is a Control input, used to
828 // prevent hoisting the divide above an unsafe test.
829 const Type* DivHFNode::Value(PhaseGVN* phase) const {
830 // Either input is TOP ==> the result is TOP
831 const Type* t1 = phase->type(in(1));
832 const Type* t2 = phase->type(in(2));
833 if(t1 == Type::TOP) { return Type::TOP; }
834 if(t2 == Type::TOP) { return Type::TOP; }
835
836 // Either input is BOTTOM ==> the result is the local BOTTOM
837 const Type* bot = bottom_type();
838 if((t1 == bot) || (t2 == bot) ||
839 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM)) {
840 return bot;
841 }
842
843 if (t1->base() == Type::HalfFloatCon &&
844 t2->base() == Type::HalfFloatCon) {
845 // IEEE 754 floating point comparison treats 0.0 and -0.0 as equals.
846
847 // Division of a zero by a zero results in NaN.
848 if (t1->getf() == 0.0f && t2->getf() == 0.0f) {
849 return TypeH::make(NAN);
850 }
851
852 // As per C++ standard section 7.6.5 (expr.mul), behavior is undefined only if
853 // the second operand is 0.0. In all other situations, we can expect a standard-compliant
854 // C++ compiler to generate code following IEEE 754 semantics.
855 if (t2->getf() == 0.0) {
856 // If either operand is NaN, the result is NaN
857 if (g_isnan(t1->getf())) {
858 return TypeH::make(NAN);
859 } else {
860 // Division of a nonzero finite value by a zero results in a signed infinity. Also,
861 // division of an infinity by a finite value results in a signed infinity.
862 bool res_sign_neg = (jint_cast(t1->getf()) < 0) ^ (jint_cast(t2->getf()) < 0);
863 const TypeF* res = res_sign_neg ? TypeF::NEG_INF : TypeF::POS_INF;
864 return TypeH::make(res->getf());
865 }
866 }
867
868 return TypeH::make(t1->getf() / t2->getf());
869 }
870
871 // Otherwise we give up all hope
872 return Type::HALF_FLOAT;
873 }
874
875 //-----------------------------------------------------------------------------
876 // Dividing by self is 1.
877 // IF the divisor is 1, we are an identity on the dividend.
878 Node* DivHFNode::Identity(PhaseGVN* phase) {
879 return (phase->type( in(2) ) == TypeH::ONE) ? in(1) : this;
880 }
881
882
883 //------------------------------Idealize---------------------------------------
884 Node* DivHFNode::Ideal(PhaseGVN* phase, bool can_reshape) {
885 if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) return this;
886 // Don't bother trying to transform a dead node
887 if (in(0) != nullptr && in(0)->is_top()) { return nullptr; }
888
889 const Type* t2 = phase->type(in(2));
890 if (t2 == TypeH::ONE) { // Identity?
891 return nullptr; // Skip it
892 }
893 const TypeH* tf = t2->isa_half_float_constant();
894 if(tf == nullptr) { return nullptr; }
895 if(tf->base() != Type::HalfFloatCon) { return nullptr; }
896
897 // Check for out of range values
898 if(tf->is_nan() || !tf->is_finite()) { return nullptr; }
899
900 // Get the value
901 float f = tf->getf();
902 int exp;
903
904 // Consider the following geometric progression series of POT(power of two) numbers.
905 // 0.5 x 2^0 = 0.5, 0.5 x 2^1 = 1.0, 0.5 x 2^2 = 2.0, 0.5 x 2^3 = 4.0 ... 0.5 x 2^n,
906 // In all the above cases, normalized mantissa returned by frexp routine will
907 // be exactly equal to 0.5 while exponent will be 0,1,2,3...n
908 // Perform division to multiplication transform only if divisor is a POT value.
909 if(frexp((double)f, &exp) != 0.5) { return nullptr; }
910
911 // Limit the range of acceptable exponents
912 if(exp < -14 || exp > 15) { return nullptr; }
913
914 // Since divisor is a POT number, hence its reciprocal will never
915 // overflow 11 bits precision range of Float16
916 // value if exponent returned by frexp routine strictly lie
917 // within the exponent range of normal min(0x1.0P-14) and
918 // normal max(0x1.ffcP+15) values.
919 // Thus we can safely compute the reciprocal of divisor without
920 // any concerns about the precision loss and transform the division
921 // into a multiplication operation.
922 float reciprocal = ((float)1.0) / f;
923
924 assert(frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2");
925
926 // return multiplication by the reciprocal
927 return (new MulHFNode(in(1), phase->makecon(TypeH::make(reciprocal))));
928 }
929
930 //=============================================================================
931 //------------------------------Value------------------------------------------
932 // An DivDNode divides its inputs. The third input is a Control input, used to
933 // prevent hoisting the divide above an unsafe test.
934 const Type* DivDNode::Value(PhaseGVN* phase) const {
935 // Either input is TOP ==> the result is TOP
936 const Type *t1 = phase->type( in(1) );
937 const Type *t2 = phase->type( in(2) );
938 if( t1 == Type::TOP ) return Type::TOP;
939 if( t2 == Type::TOP ) return Type::TOP;
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 // x/x == 1, we ignore 0/0.
948 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
949 // Does not work for variables because of NaN's
950 if (in(1) == in(2) && t1->base() == Type::DoubleCon &&
951 !g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) { // could be negative ZERO or NaN
952 return TypeD::ONE;
953 }
954
955 if( t2 == TypeD::ONE )
956 return t1;
957
958 // If divisor is a constant and not zero, divide them numbers
959 if( t1->base() == Type::DoubleCon &&
960 t2->base() == Type::DoubleCon &&
961 t2->getd() != 0.0 ) // could be negative zero
962 return TypeD::make( t1->getd()/t2->getd() );
963
964 // If the dividend is a constant zero
965 // Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
966 // Test TypeF::ZERO is not sufficient as it could be negative zero
967 if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
968 return TypeD::ZERO;
969
970 // Otherwise we give up all hope
971 return Type::DOUBLE;
972 }
973
974
975 //------------------------------isA_Copy---------------------------------------
976 // Dividing by self is 1.
977 // If the divisor is 1, we are an identity on the dividend.
978 Node* DivDNode::Identity(PhaseGVN* phase) {
979 return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
980 }
981
982 //------------------------------Idealize---------------------------------------
983 Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
984 if (in(0) && remove_dead_region(phase, can_reshape)) return this;
985 // Don't bother trying to transform a dead node
986 if( in(0) && in(0)->is_top() ) return nullptr;
987
988 const Type *t2 = phase->type( in(2) );
989 if( t2 == TypeD::ONE ) // Identity?
990 return nullptr; // Skip it
991
992 const TypeD *td = t2->isa_double_constant();
993 if( !td ) return nullptr;
994 if( td->base() != Type::DoubleCon ) return nullptr;
995
996 // Check for out of range values
997 if( td->is_nan() || !td->is_finite() ) return nullptr;
998
999 // Get the value
1000 double d = td->getd();
1001 int exp;
1002
1003 // Only for special case of dividing by a power of 2
1004 if( frexp(d, &exp) != 0.5 ) return nullptr;
1005
1006 // Limit the range of acceptable exponents
1007 if( exp < -1021 || exp > 1022 ) return nullptr;
1008
1009 // Compute the reciprocal
1010 double reciprocal = 1.0 / d;
1011
1012 assert( frexp(reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
1013
1014 // return multiplication by the reciprocal
1015 return (new MulDNode(in(1), phase->makecon(TypeD::make(reciprocal))));
1016 }
1017
1018 //=============================================================================
1019 //------------------------------Identity---------------------------------------
1020 // If the divisor is 1, we are an identity on the dividend.
1021 Node* UDivINode::Identity(PhaseGVN* phase) {
1022 return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
1023 }
1024 //------------------------------Value------------------------------------------
1025 // A UDivINode divides its inputs. The third input is a Control input, used to
1026 // prevent hoisting the divide above an unsafe test.
1027 const Type* UDivINode::Value(PhaseGVN* phase) const {
1028 // Either input is TOP ==> the result is TOP
1029 const Type *t1 = phase->type( in(1) );
1030 const Type *t2 = phase->type( in(2) );
1031 if( t1 == Type::TOP ) return Type::TOP;
1032 if( t2 == Type::TOP ) return Type::TOP;
1033
1034 // x/x == 1 since we always generate the dynamic divisor check for 0.
1035 if (in(1) == in(2)) {
1036 return TypeInt::ONE;
1037 }
1038
1039 // Either input is BOTTOM ==> the result is the local BOTTOM
1040 const Type *bot = bottom_type();
1041 if( (t1 == bot) || (t2 == bot) ||
1042 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1043 return bot;
1044
1045 // Otherwise we give up all hope
1046 return TypeInt::INT;
1047 }
1048
1049 //------------------------------Idealize---------------------------------------
1050 Node *UDivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1051 return unsigned_div_ideal<TypeInt, juint>(phase, can_reshape, this);
1052 }
1053
1054 //=============================================================================
1055 //------------------------------Identity---------------------------------------
1056 // If the divisor is 1, we are an identity on the dividend.
1057 Node* UDivLNode::Identity(PhaseGVN* phase) {
1058 return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
1059 }
1060 //------------------------------Value------------------------------------------
1061 // A UDivLNode divides its inputs. The third input is a Control input, used to
1062 // prevent hoisting the divide above an unsafe test.
1063 const Type* UDivLNode::Value(PhaseGVN* phase) const {
1064 // Either input is TOP ==> the result is TOP
1065 const Type *t1 = phase->type( in(1) );
1066 const Type *t2 = phase->type( in(2) );
1067 if( t1 == Type::TOP ) return Type::TOP;
1068 if( t2 == Type::TOP ) return Type::TOP;
1069
1070 // x/x == 1 since we always generate the dynamic divisor check for 0.
1071 if (in(1) == in(2)) {
1072 return TypeLong::ONE;
1073 }
1074
1075 // Either input is BOTTOM ==> the result is the local BOTTOM
1076 const Type *bot = bottom_type();
1077 if( (t1 == bot) || (t2 == bot) ||
1078 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
1079 return bot;
1080
1081 // Otherwise we give up all hope
1082 return TypeLong::LONG;
1083 }
1084
1085 //------------------------------Idealize---------------------------------------
1086 Node *UDivLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1087 return unsigned_div_ideal<TypeLong, julong>(phase, can_reshape, this);
1088 }
1089
1090 //=============================================================================
1091 //------------------------------Idealize---------------------------------------
1092 Node *ModINode::Ideal(PhaseGVN *phase, bool can_reshape) {
1093 // Check for dead control input
1094 if( in(0) && remove_dead_region(phase, can_reshape) ) return this;
1095 // Don't bother trying to transform a dead node
1096 if( in(0) && in(0)->is_top() ) return nullptr;
1097
1098 // Get the modulus
1099 const Type *t = phase->type( in(2) );
1100 if( t == Type::TOP ) return nullptr;
1101 const TypeInt *ti = t->is_int();
1102
1103 // Check for useless control input
1104 // Check for excluding mod-zero case
1105 if (in(0) && (ti->_hi < 0 || ti->_lo > 0)) {
1106 set_req(0, nullptr); // Yank control input
1107 return this;
1108 }
1109
1110 // See if we are MOD'ing by 2^k or 2^k-1.
1111 if( !ti->is_con() ) return nullptr;
1112 jint con = ti->get_con();
1113
1114 // First, special check for modulo 2^k-1
1115 if( con >= 0 && con < max_jint && is_power_of_2(con+1) ) {
1116 uint k = exact_log2(con+1); // Extract k
1117
1118 // Basic algorithm by David Detlefs. See fastmod_int.java for gory details.
1119 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*/};
1120 int trip_count = 1;
1121 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
1122
1123 // If the unroll factor is not too large, and if conditional moves are
1124 // ok, then use this case
1125 if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
1126 Node *x = in(1); // Value being mod'd
1127 Node *divisor = in(2); // Also is mask
1128
1129 // Add a use to x to prevent it from dying
1130 Node* hook = new Node(1);
1131 hook->init_req(0, x);
1132 // Generate code to reduce X rapidly to nearly 2^k-1.
1133 for( int i = 0; i < trip_count; i++ ) {
1134 Node *xl = phase->transform( new AndINode(x,divisor) );
1135 Node *xh = phase->transform( new RShiftINode(x,phase->intcon(k)) ); // Must be signed
1136 x = phase->transform( new AddINode(xh,xl) );
1137 hook->set_req(0, x);
1138 }
1139
1140 // Generate sign-fixup code. Was original value positive?
1141 // int hack_res = (i >= 0) ? divisor : 1;
1142 Node *cmp1 = phase->transform( new CmpINode( in(1), phase->intcon(0) ) );
1143 Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) );
1144 Node *cmov1= phase->transform( new CMoveINode(bol1, phase->intcon(1), divisor, TypeInt::POS) );
1145 // if( x >= hack_res ) x -= divisor;
1146 Node *sub = phase->transform( new SubINode( x, divisor ) );
1147 Node *cmp2 = phase->transform( new CmpINode( x, cmov1 ) );
1148 Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) );
1149 // Convention is to not transform the return value of an Ideal
1150 // since Ideal is expected to return a modified 'this' or a new node.
1151 Node *cmov2= new CMoveINode(bol2, x, sub, TypeInt::INT);
1152 // cmov2 is now the mod
1153
1154 // Now remove the bogus extra edges used to keep things alive
1155 hook->destruct(phase);
1156 return cmov2;
1157 }
1158 }
1159
1160 // Fell thru, the unroll case is not appropriate. Transform the modulo
1161 // into a long multiply/int multiply/subtract case
1162
1163 // Cannot handle mod 0, and min_jint isn't handled by the transform
1164 if( con == 0 || con == min_jint ) return nullptr;
1165
1166 // Get the absolute value of the constant; at this point, we can use this
1167 jint pos_con = (con >= 0) ? con : -con;
1168
1169 // integer Mod 1 is always 0
1170 if( pos_con == 1 ) return new ConINode(TypeInt::ZERO);
1171
1172 int log2_con = -1;
1173
1174 // If this is a power of two, they maybe we can mask it
1175 if (is_power_of_2(pos_con)) {
1176 log2_con = log2i_exact(pos_con);
1177
1178 const Type *dt = phase->type(in(1));
1179 const TypeInt *dti = dt->isa_int();
1180
1181 // See if this can be masked, if the dividend is non-negative
1182 if( dti && dti->_lo >= 0 )
1183 return ( new AndINode( in(1), phase->intcon( pos_con-1 ) ) );
1184 }
1185
1186 // Save in(1) so that it cannot be changed or deleted
1187 Node* hook = new Node(1);
1188 hook->init_req(0, in(1));
1189
1190 // Divide using the transform from DivI to MulL
1191 Node *result = transform_int_divide( phase, in(1), pos_con );
1192 if (result != nullptr) {
1193 Node *divide = phase->transform(result);
1194
1195 // Re-multiply, using a shift if this is a power of two
1196 Node *mult = nullptr;
1197
1198 if( log2_con >= 0 )
1199 mult = phase->transform( new LShiftINode( divide, phase->intcon( log2_con ) ) );
1200 else
1201 mult = phase->transform( new MulINode( divide, phase->intcon( pos_con ) ) );
1202
1203 // Finally, subtract the multiplied divided value from the original
1204 result = new SubINode( in(1), mult );
1205 }
1206
1207 // Now remove the bogus extra edges used to keep things alive
1208 hook->destruct(phase);
1209
1210 // return the value
1211 return result;
1212 }
1213
1214 //------------------------------Value------------------------------------------
1215 static const Type* mod_value(const PhaseGVN* phase, const Node* in1, const Node* in2, const BasicType bt) {
1216 assert(bt == T_INT || bt == T_LONG, "unexpected basic type");
1217 // Either input is TOP ==> the result is TOP
1218 const Type* t1 = phase->type(in1);
1219 const Type* t2 = phase->type(in2);
1220 if (t1 == Type::TOP) { return Type::TOP; }
1221 if (t2 == Type::TOP) { return Type::TOP; }
1222
1223 // Mod by zero? Throw exception at runtime!
1224 if (t2 == TypeInteger::zero(bt)) {
1225 return Type::TOP;
1226 }
1227
1228 // We always generate the dynamic check for 0.
1229 // 0 MOD X is 0
1230 if (t1 == TypeInteger::zero(bt)) { return t1; }
1231
1232 // X MOD X is 0
1233 if (in1 == in2) {
1234 return TypeInteger::zero(bt);
1235 }
1236
1237 const TypeInteger* i1 = t1->is_integer(bt);
1238 const TypeInteger* i2 = t2->is_integer(bt);
1239 if (i1->is_con() && i2->is_con()) {
1240 // We must be modulo'ing 2 int constants.
1241 // Special case: min_jlong % '-1' is UB, and e.g., x86 triggers a division error.
1242 // Any value % -1 is 0, so we can return 0 and avoid that scenario.
1243 if (i2->get_con_as_long(bt) == -1) {
1244 return TypeInteger::zero(bt);
1245 }
1246 return TypeInteger::make(i1->get_con_as_long(bt) % i2->get_con_as_long(bt), bt);
1247 }
1248 // We checked that t2 is not the zero constant. Hence, at least i2->_lo or i2->_hi must be non-zero,
1249 // and hence its absoute value is bigger than zero. Hence, the magnitude of the divisor (i.e. the
1250 // largest absolute value for any value in i2) must be in the range [1, 2^31] or [1, 2^63], depending
1251 // on the BasicType.
1252 julong divisor_magnitude = MAX2(g_uabs(i2->lo_as_long()), g_uabs(i2->hi_as_long()));
1253 // JVMS lrem bytecode: "the magnitude of the result is always less than the magnitude of the divisor"
1254 // "less than" means we can subtract 1 to get an inclusive upper bound in [0, 2^31-1] or [0, 2^63-1], respectively
1255 jlong hi = static_cast<jlong>(divisor_magnitude - 1);
1256 jlong lo = -hi;
1257 // JVMS lrem bytecode: "the result of the remainder operation can be negative only if the dividend
1258 // is negative and can be positive only if the dividend is positive"
1259 // Note that with a dividend with bounds e.g. lo == -4 and hi == -1 can still result in values
1260 // below lo; i.e., -3 % 3 == 0.
1261 // That means we cannot restrict the bound that is closer to zero beyond knowing its sign (or zero).
1262 if (i1->hi_as_long() <= 0) {
1263 // all dividends are not positive, so the result is not positive
1264 hi = 0;
1265 // if the dividend is known to be closer to zero, use that as a lower limit
1266 lo = MAX2(lo, i1->lo_as_long());
1267 } else if (i1->lo_as_long() >= 0) {
1268 // all dividends are not negative, so the result is not negative
1269 lo = 0;
1270 // if the dividend is known to be closer to zero, use that as an upper limit
1271 hi = MIN2(hi, i1->hi_as_long());
1272 } else {
1273 // Mixed signs, so we don't know the sign of the result, but the result is
1274 // either the dividend itself or a value closer to zero than the dividend,
1275 // and it is closer to zero than the divisor.
1276 // As we know i1->_lo < 0 and i1->_hi > 0, we can use these bounds directly.
1277 lo = MAX2(lo, i1->lo_as_long());
1278 hi = MIN2(hi, i1->hi_as_long());
1279 }
1280 return TypeInteger::make(lo, hi, MAX2(i1->_widen, i2->_widen), bt);
1281 }
1282
1283 const Type* ModINode::Value(PhaseGVN* phase) const {
1284 return mod_value(phase, in(1), in(2), T_INT);
1285 }
1286
1287 //=============================================================================
1288 //------------------------------Idealize---------------------------------------
1289
1290 template <typename TypeClass, typename Unsigned>
1291 static Node* unsigned_mod_ideal(PhaseGVN* phase, bool can_reshape, Node* mod) {
1292 // Check for dead control input
1293 if (mod->in(0) != nullptr && mod->remove_dead_region(phase, can_reshape)) {
1294 return mod;
1295 }
1296 // Don't bother trying to transform a dead node
1297 if (mod->in(0) != nullptr && mod->in(0)->is_top()) {
1298 return nullptr;
1299 }
1300
1301 // Get the modulus
1302 const Type* t = phase->type(mod->in(2));
1303 if (t == Type::TOP) {
1304 return nullptr;
1305 }
1306 const TypeClass* type_divisor = t->cast<TypeClass>();
1307
1308 // Check for useless control input
1309 // Check for excluding mod-zero case
1310 if (mod->in(0) != nullptr && (type_divisor->_hi < 0 || type_divisor->_lo > 0)) {
1311 mod->set_req(0, nullptr); // Yank control input
1312 return mod;
1313 }
1314
1315 if (!type_divisor->is_con()) {
1316 return nullptr;
1317 }
1318 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con());
1319
1320 if (divisor == 0) {
1321 return nullptr;
1322 }
1323
1324 if (is_power_of_2(divisor)) {
1325 return make_and<TypeClass>(mod->in(1), phase->makecon(TypeClass::make(divisor - 1)));
1326 }
1327
1328 return nullptr;
1329 }
1330
1331 template <typename TypeClass, typename Unsigned, typename Signed>
1332 static const Type* unsigned_mod_value(PhaseGVN* phase, const Node* mod) {
1333 const Type* t1 = phase->type(mod->in(1));
1334 const Type* t2 = phase->type(mod->in(2));
1335 if (t1 == Type::TOP) {
1336 return Type::TOP;
1337 }
1338 if (t2 == Type::TOP) {
1339 return Type::TOP;
1340 }
1341
1342 // 0 MOD X is 0
1343 if (t1 == TypeClass::ZERO) {
1344 return TypeClass::ZERO;
1345 }
1346 // X MOD X is 0
1347 if (mod->in(1) == mod->in(2)) {
1348 return TypeClass::ZERO;
1349 }
1350
1351 // Either input is BOTTOM ==> the result is the local BOTTOM
1352 const Type* bot = mod->bottom_type();
1353 if ((t1 == bot) || (t2 == bot) ||
1354 (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM)) {
1355 return bot;
1356 }
1357
1358 const TypeClass* type_divisor = t2->cast<TypeClass>();
1359 if (type_divisor->is_con() && type_divisor->get_con() == 1) {
1360 return TypeClass::ZERO;
1361 }
1362
1363 // Mod by zero? Throw an exception at runtime!
1364 if (type_divisor->is_con() && type_divisor->get_con() == 0) {
1365 return TypeClass::POS;
1366 }
1367
1368 const TypeClass* type_dividend = t1->cast<TypeClass>();
1369 if (type_dividend->is_con() && type_divisor->is_con()) {
1370 Unsigned dividend = static_cast<Unsigned>(type_dividend->get_con());
1371 Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con());
1372 return TypeClass::make(static_cast<Signed>(dividend % divisor));
1373 }
1374
1375 return bot;
1376 }
1377
1378 Node* UModINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1379 return unsigned_mod_ideal<TypeInt, juint>(phase, can_reshape, this);
1380 }
1381
1382 const Type* UModINode::Value(PhaseGVN* phase) const {
1383 return unsigned_mod_value<TypeInt, juint, jint>(phase, this);
1384 }
1385
1386 //=============================================================================
1387 //------------------------------Idealize---------------------------------------
1388 Node *ModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1389 // Check for dead control input
1390 if( in(0) && remove_dead_region(phase, can_reshape) ) return this;
1391 // Don't bother trying to transform a dead node
1392 if( in(0) && in(0)->is_top() ) return nullptr;
1393
1394 // Get the modulus
1395 const Type *t = phase->type( in(2) );
1396 if( t == Type::TOP ) return nullptr;
1397 const TypeLong *tl = t->is_long();
1398
1399 // Check for useless control input
1400 // Check for excluding mod-zero case
1401 if (in(0) && (tl->_hi < 0 || tl->_lo > 0)) {
1402 set_req(0, nullptr); // Yank control input
1403 return this;
1404 }
1405
1406 // See if we are MOD'ing by 2^k or 2^k-1.
1407 if( !tl->is_con() ) return nullptr;
1408 jlong con = tl->get_con();
1409
1410 // Expand mod
1411 if(con >= 0 && con < max_jlong && is_power_of_2(con + 1)) {
1412 uint k = log2i_exact(con + 1); // Extract k
1413
1414 // Basic algorithm by David Detlefs. See fastmod_long.java for gory details.
1415 // Used to help a popular random number generator which does a long-mod
1416 // of 2^31-1 and shows up in SpecJBB and SciMark.
1417 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*/};
1418 int trip_count = 1;
1419 if( k < ARRAY_SIZE(unroll_factor)) trip_count = unroll_factor[k];
1420
1421 // If the unroll factor is not too large, and if conditional moves are
1422 // ok, then use this case
1423 if( trip_count <= 5 && ConditionalMoveLimit != 0 ) {
1424 Node *x = in(1); // Value being mod'd
1425 Node *divisor = in(2); // Also is mask
1426
1427 // Add a use to x to prevent it from dying
1428 Node* hook = new Node(1);
1429 hook->init_req(0, x);
1430 // Generate code to reduce X rapidly to nearly 2^k-1.
1431 for( int i = 0; i < trip_count; i++ ) {
1432 Node *xl = phase->transform( new AndLNode(x,divisor) );
1433 Node *xh = phase->transform( new RShiftLNode(x,phase->intcon(k)) ); // Must be signed
1434 x = phase->transform( new AddLNode(xh,xl) );
1435 hook->set_req(0, x); // Add a use to x to prevent it from dying
1436 }
1437
1438 // Generate sign-fixup code. Was original value positive?
1439 // long hack_res = (i >= 0) ? divisor : CONST64(1);
1440 Node *cmp1 = phase->transform( new CmpLNode( in(1), phase->longcon(0) ) );
1441 Node *bol1 = phase->transform( new BoolNode( cmp1, BoolTest::ge ) );
1442 Node *cmov1= phase->transform( new CMoveLNode(bol1, phase->longcon(1), divisor, TypeLong::LONG) );
1443 // if( x >= hack_res ) x -= divisor;
1444 Node *sub = phase->transform( new SubLNode( x, divisor ) );
1445 Node *cmp2 = phase->transform( new CmpLNode( x, cmov1 ) );
1446 Node *bol2 = phase->transform( new BoolNode( cmp2, BoolTest::ge ) );
1447 // Convention is to not transform the return value of an Ideal
1448 // since Ideal is expected to return a modified 'this' or a new node.
1449 Node *cmov2= new CMoveLNode(bol2, x, sub, TypeLong::LONG);
1450 // cmov2 is now the mod
1451
1452 // Now remove the bogus extra edges used to keep things alive
1453 hook->destruct(phase);
1454 return cmov2;
1455 }
1456 }
1457
1458 // Fell thru, the unroll case is not appropriate. Transform the modulo
1459 // into a long multiply/int multiply/subtract case
1460
1461 // Cannot handle mod 0, and min_jlong isn't handled by the transform
1462 if( con == 0 || con == min_jlong ) return nullptr;
1463
1464 // Get the absolute value of the constant; at this point, we can use this
1465 jlong pos_con = (con >= 0) ? con : -con;
1466
1467 // integer Mod 1 is always 0
1468 if( pos_con == 1 ) return new ConLNode(TypeLong::ZERO);
1469
1470 int log2_con = -1;
1471
1472 // If this is a power of two, then maybe we can mask it
1473 if (is_power_of_2(pos_con)) {
1474 log2_con = log2i_exact(pos_con);
1475
1476 const Type *dt = phase->type(in(1));
1477 const TypeLong *dtl = dt->isa_long();
1478
1479 // See if this can be masked, if the dividend is non-negative
1480 if( dtl && dtl->_lo >= 0 )
1481 return ( new AndLNode( in(1), phase->longcon( pos_con-1 ) ) );
1482 }
1483
1484 // Save in(1) so that it cannot be changed or deleted
1485 // Add a use to x to prevent him from dying
1486 Node* hook = new Node(1);
1487 hook->init_req(0, in(1));
1488
1489 // Divide using the transform from DivL to MulL
1490 Node *result = transform_long_divide( phase, in(1), pos_con );
1491 if (result != nullptr) {
1492 Node *divide = phase->transform(result);
1493
1494 // Re-multiply, using a shift if this is a power of two
1495 Node *mult = nullptr;
1496
1497 if( log2_con >= 0 )
1498 mult = phase->transform( new LShiftLNode( divide, phase->intcon( log2_con ) ) );
1499 else
1500 mult = phase->transform( new MulLNode( divide, phase->longcon( pos_con ) ) );
1501
1502 // Finally, subtract the multiplied divided value from the original
1503 result = new SubLNode( in(1), mult );
1504 }
1505
1506 // Now remove the bogus extra edges used to keep things alive
1507 hook->destruct(phase);
1508
1509 // return the value
1510 return result;
1511 }
1512
1513 //------------------------------Value------------------------------------------
1514 const Type* ModLNode::Value(PhaseGVN* phase) const {
1515 return mod_value(phase, in(1), in(2), T_LONG);
1516 }
1517
1518 Node *UModLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
1519 return unsigned_mod_ideal<TypeLong, julong>(phase, can_reshape, this);
1520 }
1521
1522 const Type* UModLNode::Value(PhaseGVN* phase) const {
1523 return unsigned_mod_value<TypeLong, julong, jlong>(phase, this);
1524 }
1525
1526 const Type* ModFNode::get_result_if_constant(const Type* dividend, const Type* divisor) const {
1527 // If either number is not a constant, we know nothing.
1528 if ((dividend->base() != Type::FloatCon) || (divisor->base() != Type::FloatCon)) {
1529 return nullptr; // note: x%x can be either NaN or 0
1530 }
1531
1532 float dividend_f = dividend->getf();
1533 float divisor_f = divisor->getf();
1534 jint dividend_i = jint_cast(dividend_f); // note: *(int*)&f1, not just (int)f1
1535 jint divisor_i = jint_cast(divisor_f);
1536
1537 // If either is a NaN, return an input NaN
1538 if (g_isnan(dividend_f)) {
1539 return dividend;
1540 }
1541 if (g_isnan(divisor_f)) {
1542 return divisor;
1543 }
1544
1545 // If an operand is infinity or the divisor is +/- zero, punt.
1546 if (!g_isfinite(dividend_f) || !g_isfinite(divisor_f) || divisor_i == 0 || divisor_i == min_jint) {
1547 return nullptr;
1548 }
1549
1550 // We must be modulo'ing 2 float constants.
1551 // Make sure that the sign of the fmod is equal to the sign of the dividend
1552 jint xr = jint_cast(fmod(dividend_f, divisor_f));
1553 if ((dividend_i ^ xr) < 0) {
1554 xr ^= min_jint;
1555 }
1556
1557 return TypeF::make(jfloat_cast(xr));
1558 }
1559
1560 const Type* ModDNode::get_result_if_constant(const Type* dividend, const Type* divisor) const {
1561 // If either number is not a constant, we know nothing.
1562 if ((dividend->base() != Type::DoubleCon) || (divisor->base() != Type::DoubleCon)) {
1563 return nullptr; // note: x%x can be either NaN or 0
1564 }
1565
1566 double dividend_d = dividend->getd();
1567 double divisor_d = divisor->getd();
1568 jlong dividend_l = jlong_cast(dividend_d); // note: *(long*)&f1, not just (long)f1
1569 jlong divisor_l = jlong_cast(divisor_d);
1570
1571 // If either is a NaN, return an input NaN
1572 if (g_isnan(dividend_d)) {
1573 return dividend;
1574 }
1575 if (g_isnan(divisor_d)) {
1576 return divisor;
1577 }
1578
1579 // If an operand is infinity or the divisor is +/- zero, punt.
1580 if (!g_isfinite(dividend_d) || !g_isfinite(divisor_d) || divisor_l == 0 || divisor_l == min_jlong) {
1581 return nullptr;
1582 }
1583
1584 // We must be modulo'ing 2 double constants.
1585 // Make sure that the sign of the fmod is equal to the sign of the dividend
1586 jlong xr = jlong_cast(fmod(dividend_d, divisor_d));
1587 if ((dividend_l ^ xr) < 0) {
1588 xr ^= min_jlong;
1589 }
1590
1591 return TypeD::make(jdouble_cast(xr));
1592 }
1593
1594 const Type* ModFloatingNode::Value(PhaseGVN* phase) const {
1595 const Type* t = CallLeafPureNode::Value(phase);
1596 if (t == Type::TOP) { return Type::TOP; }
1597 const Type* dividend_type = phase->type(dividend());
1598 const Type* divisor_type = phase->type(divisor());
1599 if (dividend_type == Type::TOP || divisor_type == Type::TOP) {
1600 return Type::TOP;
1601 }
1602 const Type* constant_result = get_result_if_constant(dividend_type, divisor_type);
1603 if (constant_result != nullptr) {
1604 const TypeTuple* tt = t->is_tuple();
1605 uint cnt = tt->cnt();
1606 uint param_cnt = cnt - TypeFunc::Parms;
1607 const Type** fields = TypeTuple::fields(param_cnt);
1608 fields[TypeFunc::Parms] = constant_result;
1609 if (param_cnt > 1) { fields[TypeFunc::Parms + 1] = Type::HALF; }
1610 return TypeTuple::make(cnt, fields);
1611 }
1612 return t;
1613 }
1614
1615 //=============================================================================
1616
1617 DivModNode::DivModNode( Node *c, Node *dividend, Node *divisor ) : MultiNode(3) {
1618 init_req(0, c);
1619 init_req(1, dividend);
1620 init_req(2, divisor);
1621 }
1622
1623 DivModNode* DivModNode::make(Node* div_or_mod, BasicType bt, bool is_unsigned) {
1624 assert(bt == T_INT || bt == T_LONG, "only int or long input pattern accepted");
1625
1626 if (bt == T_INT) {
1627 if (is_unsigned) {
1628 return UDivModINode::make(div_or_mod);
1629 } else {
1630 return DivModINode::make(div_or_mod);
1631 }
1632 } else {
1633 if (is_unsigned) {
1634 return UDivModLNode::make(div_or_mod);
1635 } else {
1636 return DivModLNode::make(div_or_mod);
1637 }
1638 }
1639 }
1640
1641 //------------------------------make------------------------------------------
1642 DivModINode* DivModINode::make(Node* div_or_mod) {
1643 Node* n = div_or_mod;
1644 assert(n->Opcode() == Op_DivI || n->Opcode() == Op_ModI,
1645 "only div or mod input pattern accepted");
1646
1647 DivModINode* divmod = new DivModINode(n->in(0), n->in(1), n->in(2));
1648 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num);
1649 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num);
1650 return divmod;
1651 }
1652
1653 //------------------------------make------------------------------------------
1654 DivModLNode* DivModLNode::make(Node* div_or_mod) {
1655 Node* n = div_or_mod;
1656 assert(n->Opcode() == Op_DivL || n->Opcode() == Op_ModL,
1657 "only div or mod input pattern accepted");
1658
1659 DivModLNode* divmod = new DivModLNode(n->in(0), n->in(1), n->in(2));
1660 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num);
1661 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num);
1662 return divmod;
1663 }
1664
1665 //------------------------------match------------------------------------------
1666 // return result(s) along with their RegMask info
1667 Node *DivModINode::match( const ProjNode *proj, const Matcher *match ) {
1668 uint ideal_reg = proj->ideal_reg();
1669 RegMask rm;
1670 if (proj->_con == div_proj_num) {
1671 rm.assignFrom(match->divI_proj_mask());
1672 } else {
1673 assert(proj->_con == mod_proj_num, "must be div or mod projection");
1674 rm.assignFrom(match->modI_proj_mask());
1675 }
1676 return new MachProjNode(this, proj->_con, rm, ideal_reg);
1677 }
1678
1679
1680 //------------------------------match------------------------------------------
1681 // return result(s) along with their RegMask info
1682 Node *DivModLNode::match( const ProjNode *proj, const Matcher *match ) {
1683 uint ideal_reg = proj->ideal_reg();
1684 RegMask rm;
1685 if (proj->_con == div_proj_num) {
1686 rm.assignFrom(match->divL_proj_mask());
1687 } else {
1688 assert(proj->_con == mod_proj_num, "must be div or mod projection");
1689 rm.assignFrom(match->modL_proj_mask());
1690 }
1691 return new MachProjNode(this, proj->_con, rm, ideal_reg);
1692 }
1693
1694 //------------------------------make------------------------------------------
1695 UDivModINode* UDivModINode::make(Node* div_or_mod) {
1696 Node* n = div_or_mod;
1697 assert(n->Opcode() == Op_UDivI || n->Opcode() == Op_UModI,
1698 "only div or mod input pattern accepted");
1699
1700 UDivModINode* divmod = new UDivModINode(n->in(0), n->in(1), n->in(2));
1701 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num);
1702 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num);
1703 return divmod;
1704 }
1705
1706 //------------------------------make------------------------------------------
1707 UDivModLNode* UDivModLNode::make(Node* div_or_mod) {
1708 Node* n = div_or_mod;
1709 assert(n->Opcode() == Op_UDivL || n->Opcode() == Op_UModL,
1710 "only div or mod input pattern accepted");
1711
1712 UDivModLNode* divmod = new UDivModLNode(n->in(0), n->in(1), n->in(2));
1713 Node* dproj = new ProjNode(divmod, DivModNode::div_proj_num);
1714 Node* mproj = new ProjNode(divmod, DivModNode::mod_proj_num);
1715 return divmod;
1716 }
1717
1718 //------------------------------match------------------------------------------
1719 // return result(s) along with their RegMask info
1720 Node* UDivModINode::match( const ProjNode *proj, const Matcher *match ) {
1721 uint ideal_reg = proj->ideal_reg();
1722 RegMask rm;
1723 if (proj->_con == div_proj_num) {
1724 rm.assignFrom(match->divI_proj_mask());
1725 } else {
1726 assert(proj->_con == mod_proj_num, "must be div or mod projection");
1727 rm.assignFrom(match->modI_proj_mask());
1728 }
1729 return new MachProjNode(this, proj->_con, rm, ideal_reg);
1730 }
1731
1732
1733 //------------------------------match------------------------------------------
1734 // return result(s) along with their RegMask info
1735 Node* UDivModLNode::match( const ProjNode *proj, const Matcher *match ) {
1736 uint ideal_reg = proj->ideal_reg();
1737 RegMask rm;
1738 if (proj->_con == div_proj_num) {
1739 rm.assignFrom(match->divL_proj_mask());
1740 } else {
1741 assert(proj->_con == mod_proj_num, "must be div or mod projection");
1742 rm.assignFrom(match->modL_proj_mask());
1743 }
1744 return new MachProjNode(this, proj->_con, rm, ideal_reg);
1745 }