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