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