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