annotate src/share/vm/opto/divnode.cpp @ 14649:f6301b007a16

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