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