author | thartmann |
Mon, 04 Jul 2016 09:14:02 +0200 | |
changeset 40044 | 4e8299073922 |
parent 37287 | c2660335bf81 |
child 45965 | e29c1363af9a |
child 46630 | 75aa3e39d02c |
permissions | -rw-r--r-- |
9101 | 1 |
/* |
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23528
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
9101 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
#include "precompiled.hpp" |
|
26 |
#include "opto/loopnode.hpp" |
|
27 |
#include "opto/addnode.hpp" |
|
28 |
#include "opto/callnode.hpp" |
|
29 |
#include "opto/connode.hpp" |
|
23528 | 30 |
#include "opto/convertnode.hpp" |
9101 | 31 |
#include "opto/loopnode.hpp" |
32 |
#include "opto/mulnode.hpp" |
|
23528 | 33 |
#include "opto/opaquenode.hpp" |
9101 | 34 |
#include "opto/rootnode.hpp" |
35 |
#include "opto/subnode.hpp" |
|
36 |
||
37 |
/* |
|
38 |
* The general idea of Loop Predication is to insert a predicate on the entry |
|
39 |
* path to a loop, and raise a uncommon trap if the check of the condition fails. |
|
40 |
* The condition checks are promoted from inside the loop body, and thus |
|
41 |
* the checks inside the loop could be eliminated. Currently, loop predication |
|
42 |
* optimization has been applied to remove array range check and loop invariant |
|
43 |
* checks (such as null checks). |
|
44 |
*/ |
|
45 |
||
46 |
//-------------------------------register_control------------------------- |
|
47 |
void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred) { |
|
48 |
assert(n->is_CFG(), "must be control node"); |
|
49 |
_igvn.register_new_node_with_optimizer(n); |
|
50 |
loop->_body.push(n); |
|
51 |
set_loop(n, loop); |
|
52 |
// When called from beautify_loops() idom is not constructed yet. |
|
53 |
if (_idom != NULL) { |
|
54 |
set_idom(n, pred, dom_depth(pred)); |
|
55 |
} |
|
56 |
} |
|
57 |
||
58 |
//------------------------------create_new_if_for_predicate------------------------ |
|
59 |
// create a new if above the uct_if_pattern for the predicate to be promoted. |
|
60 |
// |
|
61 |
// before after |
|
62 |
// ---------- ---------- |
|
63 |
// ctrl ctrl |
|
64 |
// | | |
|
65 |
// | | |
|
66 |
// v v |
|
67 |
// iff new_iff |
|
68 |
// / \ / \ |
|
69 |
// / \ / \ |
|
70 |
// v v v v |
|
71 |
// uncommon_proj cont_proj if_uct if_cont |
|
72 |
// \ | | | | |
|
73 |
// \ | | | | |
|
74 |
// v v v | v |
|
75 |
// rgn loop | iff |
|
76 |
// | | / \ |
|
77 |
// | | / \ |
|
78 |
// v | v v |
|
79 |
// uncommon_trap | uncommon_proj cont_proj |
|
80 |
// \ \ | | |
|
81 |
// \ \ | | |
|
82 |
// v v v v |
|
83 |
// rgn loop |
|
84 |
// | |
|
85 |
// | |
|
86 |
// v |
|
87 |
// uncommon_trap |
|
88 |
// |
|
89 |
// |
|
90 |
// We will create a region to guard the uct call if there is no one there. |
|
91 |
// The true projecttion (if_cont) of the new_iff is returned. |
|
30309
da3efc8ed2cb
8078426: mb/jvm/compiler/InterfaceCalls/testAC2 - assert(predicate_proj == 0L) failed: only one predicate entry expected
roland
parents:
26166
diff
changeset
|
92 |
// This code is also used to clone predicates to cloned loops. |
9101 | 93 |
ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, |
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
94 |
Deoptimization::DeoptReason reason, |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
95 |
int opcode) { |
21089
e1986ff6fe2e
8024069: replace_in_map() should operate on parent maps
roland
parents:
17383
diff
changeset
|
96 |
assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); |
9101 | 97 |
IfNode* iff = cont_proj->in(0)->as_If(); |
98 |
||
99 |
ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); |
|
100 |
Node *rgn = uncommon_proj->unique_ctrl_out(); |
|
101 |
assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); |
|
102 |
||
103 |
uint proj_index = 1; // region's edge corresponding to uncommon_proj |
|
104 |
if (!rgn->is_Region()) { // create a region to guard the call |
|
105 |
assert(rgn->is_Call(), "must be call uct"); |
|
106 |
CallNode* call = rgn->as_Call(); |
|
107 |
IdealLoopTree* loop = get_loop(call); |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
108 |
rgn = new RegionNode(1); |
9101 | 109 |
rgn->add_req(uncommon_proj); |
110 |
register_control(rgn, loop, uncommon_proj); |
|
25913
81dbc151e91c
8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents:
24923
diff
changeset
|
111 |
_igvn.replace_input_of(call, 0, rgn); |
9101 | 112 |
// When called from beautify_loops() idom is not constructed yet. |
113 |
if (_idom != NULL) { |
|
114 |
set_idom(call, rgn, dom_depth(rgn)); |
|
115 |
} |
|
32733 | 116 |
for (DUIterator_Fast imax, i = uncommon_proj->fast_outs(imax); i < imax; i++) { |
117 |
Node* n = uncommon_proj->fast_out(i); |
|
118 |
if (n->is_Load() || n->is_Store()) { |
|
119 |
_igvn.replace_input_of(n, 0, rgn); |
|
120 |
--i; --imax; |
|
121 |
} |
|
122 |
} |
|
9101 | 123 |
} else { |
124 |
// Find region's edge corresponding to uncommon_proj |
|
125 |
for (; proj_index < rgn->req(); proj_index++) |
|
126 |
if (rgn->in(proj_index) == uncommon_proj) break; |
|
127 |
assert(proj_index < rgn->req(), "sanity"); |
|
128 |
} |
|
129 |
||
130 |
Node* entry = iff->in(0); |
|
131 |
if (new_entry != NULL) { |
|
132 |
// Clonning the predicate to new location. |
|
133 |
entry = new_entry; |
|
134 |
} |
|
135 |
// Create new_iff |
|
136 |
IdealLoopTree* lp = get_loop(entry); |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
137 |
IfNode* new_iff = NULL; |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
138 |
if (opcode == Op_If) { |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
139 |
new_iff = new IfNode(entry, iff->in(1), iff->_prob, iff->_fcnt); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
140 |
} else { |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
141 |
assert(opcode == Op_RangeCheck, "no other if variant here"); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
142 |
new_iff = new RangeCheckNode(entry, iff->in(1), iff->_prob, iff->_fcnt); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
143 |
} |
9101 | 144 |
register_control(new_iff, lp, entry); |
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
145 |
Node *if_cont = new IfTrueNode(new_iff); |
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
146 |
Node *if_uct = new IfFalseNode(new_iff); |
9101 | 147 |
if (cont_proj->is_IfFalse()) { |
148 |
// Swap |
|
149 |
Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; |
|
150 |
} |
|
151 |
register_control(if_cont, lp, new_iff); |
|
152 |
register_control(if_uct, get_loop(rgn), new_iff); |
|
153 |
||
154 |
// if_uct to rgn |
|
155 |
_igvn.hash_delete(rgn); |
|
156 |
rgn->add_req(if_uct); |
|
157 |
// When called from beautify_loops() idom is not constructed yet. |
|
158 |
if (_idom != NULL) { |
|
159 |
Node* ridom = idom(rgn); |
|
160 |
Node* nrdom = dom_lca(ridom, new_iff); |
|
161 |
set_idom(rgn, nrdom, dom_depth(rgn)); |
|
162 |
} |
|
163 |
||
164 |
// If rgn has phis add new edges which has the same |
|
165 |
// value as on original uncommon_proj pass. |
|
166 |
assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); |
|
167 |
bool has_phi = false; |
|
168 |
for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { |
|
169 |
Node* use = rgn->fast_out(i); |
|
170 |
if (use->is_Phi() && use->outcnt() > 0) { |
|
171 |
assert(use->in(0) == rgn, ""); |
|
12958
009b6c9586d8
7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents:
10263
diff
changeset
|
172 |
_igvn.rehash_node_delayed(use); |
9101 | 173 |
use->add_req(use->in(proj_index)); |
174 |
has_phi = true; |
|
175 |
} |
|
176 |
} |
|
177 |
assert(!has_phi || rgn->req() > 3, "no phis when region is created"); |
|
178 |
||
179 |
if (new_entry == NULL) { |
|
180 |
// Attach if_cont to iff |
|
25913
81dbc151e91c
8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents:
24923
diff
changeset
|
181 |
_igvn.replace_input_of(iff, 0, if_cont); |
9101 | 182 |
if (_idom != NULL) { |
183 |
set_idom(iff, if_cont, dom_depth(iff)); |
|
184 |
} |
|
185 |
} |
|
186 |
return if_cont->as_Proj(); |
|
187 |
} |
|
188 |
||
189 |
//------------------------------create_new_if_for_predicate------------------------ |
|
190 |
// Create a new if below new_entry for the predicate to be cloned (IGVN optimization) |
|
191 |
ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry, |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
192 |
Deoptimization::DeoptReason reason, |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
193 |
int opcode) { |
9101 | 194 |
assert(new_entry != 0, "only used for clone predicate"); |
21089
e1986ff6fe2e
8024069: replace_in_map() should operate on parent maps
roland
parents:
17383
diff
changeset
|
195 |
assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!"); |
9101 | 196 |
IfNode* iff = cont_proj->in(0)->as_If(); |
197 |
||
198 |
ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con); |
|
199 |
Node *rgn = uncommon_proj->unique_ctrl_out(); |
|
200 |
assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); |
|
201 |
||
202 |
uint proj_index = 1; // region's edge corresponding to uncommon_proj |
|
203 |
if (!rgn->is_Region()) { // create a region to guard the call |
|
204 |
assert(rgn->is_Call(), "must be call uct"); |
|
205 |
CallNode* call = rgn->as_Call(); |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
206 |
rgn = new RegionNode(1); |
9101 | 207 |
register_new_node_with_optimizer(rgn); |
208 |
rgn->add_req(uncommon_proj); |
|
25913
81dbc151e91c
8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents:
24923
diff
changeset
|
209 |
replace_input_of(call, 0, rgn); |
9101 | 210 |
} else { |
211 |
// Find region's edge corresponding to uncommon_proj |
|
212 |
for (; proj_index < rgn->req(); proj_index++) |
|
213 |
if (rgn->in(proj_index) == uncommon_proj) break; |
|
214 |
assert(proj_index < rgn->req(), "sanity"); |
|
215 |
} |
|
216 |
||
217 |
// Create new_iff in new location. |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
218 |
IfNode* new_iff = NULL; |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
219 |
if (opcode == Op_If) { |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
220 |
new_iff = new IfNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
221 |
} else { |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
222 |
assert(opcode == Op_RangeCheck, "no other if variant here"); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
223 |
new_iff = new RangeCheckNode(new_entry, iff->in(1), iff->_prob, iff->_fcnt); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
224 |
} |
9101 | 225 |
|
226 |
register_new_node_with_optimizer(new_iff); |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
227 |
Node *if_cont = new IfTrueNode(new_iff); |
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
228 |
Node *if_uct = new IfFalseNode(new_iff); |
9101 | 229 |
if (cont_proj->is_IfFalse()) { |
230 |
// Swap |
|
231 |
Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp; |
|
232 |
} |
|
233 |
register_new_node_with_optimizer(if_cont); |
|
234 |
register_new_node_with_optimizer(if_uct); |
|
235 |
||
236 |
// if_uct to rgn |
|
237 |
hash_delete(rgn); |
|
238 |
rgn->add_req(if_uct); |
|
239 |
||
240 |
// If rgn has phis add corresponding new edges which has the same |
|
241 |
// value as on original uncommon_proj pass. |
|
242 |
assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last"); |
|
243 |
bool has_phi = false; |
|
244 |
for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) { |
|
245 |
Node* use = rgn->fast_out(i); |
|
246 |
if (use->is_Phi() && use->outcnt() > 0) { |
|
12958
009b6c9586d8
7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents:
10263
diff
changeset
|
247 |
rehash_node_delayed(use); |
9101 | 248 |
use->add_req(use->in(proj_index)); |
249 |
has_phi = true; |
|
250 |
} |
|
251 |
} |
|
252 |
assert(!has_phi || rgn->req() > 3, "no phis when region is created"); |
|
253 |
||
254 |
return if_cont->as_Proj(); |
|
255 |
} |
|
256 |
||
257 |
//--------------------------clone_predicate----------------------- |
|
258 |
ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry, |
|
259 |
Deoptimization::DeoptReason reason, |
|
260 |
PhaseIdealLoop* loop_phase, |
|
261 |
PhaseIterGVN* igvn) { |
|
262 |
ProjNode* new_predicate_proj; |
|
263 |
if (loop_phase != NULL) { |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
264 |
new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason, Op_If); |
9101 | 265 |
} else { |
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
266 |
new_predicate_proj = igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason, Op_If); |
9101 | 267 |
} |
268 |
IfNode* iff = new_predicate_proj->in(0)->as_If(); |
|
269 |
Node* ctrl = iff->in(0); |
|
270 |
||
271 |
// Match original condition since predicate's projections could be swapped. |
|
272 |
assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be"); |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
273 |
Node* opq = new Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1)); |
9101 | 274 |
igvn->C->add_predicate_opaq(opq); |
275 |
||
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
276 |
Node* bol = new Conv2BNode(opq); |
9101 | 277 |
if (loop_phase != NULL) { |
278 |
loop_phase->register_new_node(opq, ctrl); |
|
279 |
loop_phase->register_new_node(bol, ctrl); |
|
280 |
} else { |
|
281 |
igvn->register_new_node_with_optimizer(opq); |
|
282 |
igvn->register_new_node_with_optimizer(bol); |
|
283 |
} |
|
284 |
igvn->hash_delete(iff); |
|
285 |
iff->set_req(1, bol); |
|
286 |
return new_predicate_proj; |
|
287 |
} |
|
288 |
||
289 |
||
290 |
//--------------------------clone_loop_predicates----------------------- |
|
291 |
// Interface from IGVN |
|
9446 | 292 |
Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { |
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
293 |
return PhaseIdealLoop::clone_loop_predicates(old_entry, new_entry, clone_limit_check, NULL, this); |
9101 | 294 |
} |
295 |
||
296 |
// Interface from PhaseIdealLoop |
|
9446 | 297 |
Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) { |
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
298 |
return clone_loop_predicates(old_entry, new_entry, clone_limit_check, this, &this->_igvn); |
9101 | 299 |
} |
300 |
||
301 |
// Clone loop predicates to cloned loops (peeled, unswitched, split_if). |
|
302 |
Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, |
|
9446 | 303 |
bool clone_limit_check, |
9101 | 304 |
PhaseIdealLoop* loop_phase, |
305 |
PhaseIterGVN* igvn) { |
|
306 |
#ifdef ASSERT |
|
307 |
if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) { |
|
308 |
if (new_entry != NULL) |
|
309 |
new_entry->dump(); |
|
310 |
assert(false, "not IfTrue, IfFalse, Region or SafePoint"); |
|
311 |
} |
|
312 |
#endif |
|
313 |
// Search original predicates |
|
314 |
Node* entry = old_entry; |
|
9446 | 315 |
ProjNode* limit_check_proj = NULL; |
37287
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
316 |
limit_check_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
317 |
if (limit_check_proj != NULL) { |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
318 |
entry = entry->in(0)->in(0); |
9446 | 319 |
} |
9101 | 320 |
if (UseLoopPredicate) { |
321 |
ProjNode* predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); |
|
322 |
if (predicate_proj != NULL) { // right pattern that can be used by loop predication |
|
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
323 |
// clone predicate |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
324 |
new_entry = clone_predicate(predicate_proj, new_entry, |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
325 |
Deoptimization::Reason_predicate, |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
326 |
loop_phase, igvn); |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
327 |
assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate"); |
9101 | 328 |
if (TraceLoopPredicate) { |
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
329 |
tty->print("Loop Predicate cloned: "); |
9101 | 330 |
debug_only( new_entry->in(0)->dump(); ) |
331 |
} |
|
332 |
} |
|
333 |
} |
|
9446 | 334 |
if (limit_check_proj != NULL && clone_limit_check) { |
335 |
// Clone loop limit check last to insert it before loop. |
|
336 |
// Don't clone a limit check which was already finalized |
|
337 |
// for this counted loop (only one limit check is needed). |
|
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
338 |
new_entry = clone_predicate(limit_check_proj, new_entry, |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
339 |
Deoptimization::Reason_loop_limit_check, |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
340 |
loop_phase, igvn); |
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
341 |
assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone limit check"); |
9446 | 342 |
if (TraceLoopLimitCheck) { |
10258
10c77b8c8d3e
7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents:
10253
diff
changeset
|
343 |
tty->print("Loop Limit Check cloned: "); |
9446 | 344 |
debug_only( new_entry->in(0)->dump(); ) |
345 |
} |
|
346 |
} |
|
9101 | 347 |
return new_entry; |
348 |
} |
|
349 |
||
350 |
//--------------------------skip_loop_predicates------------------------------ |
|
351 |
// Skip related predicates. |
|
352 |
Node* PhaseIdealLoop::skip_loop_predicates(Node* entry) { |
|
353 |
Node* predicate = NULL; |
|
37287
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
354 |
predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
355 |
if (predicate != NULL) { |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
356 |
entry = entry->in(0)->in(0); |
9446 | 357 |
} |
9101 | 358 |
if (UseLoopPredicate) { |
359 |
predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); |
|
360 |
if (predicate != NULL) { // right pattern that can be used by loop predication |
|
361 |
IfNode* iff = entry->in(0)->as_If(); |
|
362 |
ProjNode* uncommon_proj = iff->proj_out(1 - entry->as_Proj()->_con); |
|
363 |
Node* rgn = uncommon_proj->unique_ctrl_out(); |
|
364 |
assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct"); |
|
365 |
entry = entry->in(0)->in(0); |
|
366 |
while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) { |
|
367 |
uncommon_proj = entry->in(0)->as_If()->proj_out(1 - entry->as_Proj()->_con); |
|
368 |
if (uncommon_proj->unique_ctrl_out() != rgn) |
|
369 |
break; |
|
370 |
entry = entry->in(0)->in(0); |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
return entry; |
|
375 |
} |
|
376 |
||
377 |
//--------------------------find_predicate_insertion_point------------------- |
|
378 |
// Find a good location to insert a predicate |
|
379 |
ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) { |
|
380 |
if (start_c == NULL || !start_c->is_Proj()) |
|
381 |
return NULL; |
|
21089
e1986ff6fe2e
8024069: replace_in_map() should operate on parent maps
roland
parents:
17383
diff
changeset
|
382 |
if (start_c->as_Proj()->is_uncommon_trap_if_pattern(reason)) { |
9101 | 383 |
return start_c->as_Proj(); |
384 |
} |
|
385 |
return NULL; |
|
386 |
} |
|
387 |
||
388 |
//--------------------------find_predicate------------------------------------ |
|
389 |
// Find a predicate |
|
390 |
Node* PhaseIdealLoop::find_predicate(Node* entry) { |
|
391 |
Node* predicate = NULL; |
|
37287
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
392 |
predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
393 |
if (predicate != NULL) { // right pattern that can be used by loop predication |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
394 |
return entry; |
9446 | 395 |
} |
9101 | 396 |
if (UseLoopPredicate) { |
397 |
predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); |
|
398 |
if (predicate != NULL) { // right pattern that can be used by loop predication |
|
399 |
return entry; |
|
400 |
} |
|
401 |
} |
|
402 |
return NULL; |
|
403 |
} |
|
404 |
||
405 |
//------------------------------Invariance----------------------------------- |
|
406 |
// Helper class for loop_predication_impl to compute invariance on the fly and |
|
407 |
// clone invariants. |
|
408 |
class Invariance : public StackObj { |
|
409 |
VectorSet _visited, _invariant; |
|
410 |
Node_Stack _stack; |
|
411 |
VectorSet _clone_visited; |
|
412 |
Node_List _old_new; // map of old to new (clone) |
|
413 |
IdealLoopTree* _lpt; |
|
414 |
PhaseIdealLoop* _phase; |
|
415 |
||
416 |
// Helper function to set up the invariance for invariance computation |
|
417 |
// If n is a known invariant, set up directly. Otherwise, look up the |
|
418 |
// the possibility to push n onto the stack for further processing. |
|
419 |
void visit(Node* use, Node* n) { |
|
420 |
if (_lpt->is_invariant(n)) { // known invariant |
|
421 |
_invariant.set(n->_idx); |
|
422 |
} else if (!n->is_CFG()) { |
|
423 |
Node *n_ctrl = _phase->ctrl_or_self(n); |
|
424 |
Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG |
|
425 |
if (_phase->is_dominator(n_ctrl, u_ctrl)) { |
|
426 |
_stack.push(n, n->in(0) == NULL ? 1 : 0); |
|
427 |
} |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
// Compute invariance for "the_node" and (possibly) all its inputs recursively |
|
432 |
// on the fly |
|
433 |
void compute_invariance(Node* n) { |
|
434 |
assert(_visited.test(n->_idx), "must be"); |
|
435 |
visit(n, n); |
|
436 |
while (_stack.is_nonempty()) { |
|
437 |
Node* n = _stack.node(); |
|
438 |
uint idx = _stack.index(); |
|
439 |
if (idx == n->req()) { // all inputs are processed |
|
440 |
_stack.pop(); |
|
441 |
// n is invariant if it's inputs are all invariant |
|
442 |
bool all_inputs_invariant = true; |
|
443 |
for (uint i = 0; i < n->req(); i++) { |
|
444 |
Node* in = n->in(i); |
|
445 |
if (in == NULL) continue; |
|
446 |
assert(_visited.test(in->_idx), "must have visited input"); |
|
447 |
if (!_invariant.test(in->_idx)) { // bad guy |
|
448 |
all_inputs_invariant = false; |
|
449 |
break; |
|
450 |
} |
|
451 |
} |
|
452 |
if (all_inputs_invariant) { |
|
31035
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
453 |
// If n's control is a predicate that was moved out of the |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
454 |
// loop, it was marked invariant but n is only invariant if |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
455 |
// it depends only on that test. Otherwise, unless that test |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
456 |
// is out of the loop, it's not invariant. |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
457 |
if (n->is_CFG() || n->depends_only_on_test() || n->in(0) == NULL || !_phase->is_member(_lpt, n->in(0))) { |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
458 |
_invariant.set(n->_idx); // I am a invariant too |
0f0743952c41
8077504: Unsafe load can loose control dependency and cause crash
roland
parents:
30309
diff
changeset
|
459 |
} |
9101 | 460 |
} |
461 |
} else { // process next input |
|
462 |
_stack.set_index(idx + 1); |
|
463 |
Node* m = n->in(idx); |
|
464 |
if (m != NULL && !_visited.test_set(m->_idx)) { |
|
465 |
visit(n, m); |
|
466 |
} |
|
467 |
} |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
// Helper function to set up _old_new map for clone_nodes. |
|
472 |
// If n is a known invariant, set up directly ("clone" of n == n). |
|
473 |
// Otherwise, push n onto the stack for real cloning. |
|
474 |
void clone_visit(Node* n) { |
|
475 |
assert(_invariant.test(n->_idx), "must be invariant"); |
|
476 |
if (_lpt->is_invariant(n)) { // known invariant |
|
477 |
_old_new.map(n->_idx, n); |
|
478 |
} else { // to be cloned |
|
479 |
assert(!n->is_CFG(), "should not see CFG here"); |
|
480 |
_stack.push(n, n->in(0) == NULL ? 1 : 0); |
|
481 |
} |
|
482 |
} |
|
483 |
||
484 |
// Clone "n" and (possibly) all its inputs recursively |
|
485 |
void clone_nodes(Node* n, Node* ctrl) { |
|
486 |
clone_visit(n); |
|
487 |
while (_stack.is_nonempty()) { |
|
488 |
Node* n = _stack.node(); |
|
489 |
uint idx = _stack.index(); |
|
490 |
if (idx == n->req()) { // all inputs processed, clone n! |
|
491 |
_stack.pop(); |
|
492 |
// clone invariant node |
|
493 |
Node* n_cl = n->clone(); |
|
494 |
_old_new.map(n->_idx, n_cl); |
|
495 |
_phase->register_new_node(n_cl, ctrl); |
|
496 |
for (uint i = 0; i < n->req(); i++) { |
|
497 |
Node* in = n_cl->in(i); |
|
498 |
if (in == NULL) continue; |
|
499 |
n_cl->set_req(i, _old_new[in->_idx]); |
|
500 |
} |
|
501 |
} else { // process next input |
|
502 |
_stack.set_index(idx + 1); |
|
503 |
Node* m = n->in(idx); |
|
504 |
if (m != NULL && !_clone_visited.test_set(m->_idx)) { |
|
505 |
clone_visit(m); // visit the input |
|
506 |
} |
|
507 |
} |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
public: |
|
512 |
Invariance(Arena* area, IdealLoopTree* lpt) : |
|
513 |
_lpt(lpt), _phase(lpt->_phase), |
|
514 |
_visited(area), _invariant(area), _stack(area, 10 /* guess */), |
|
515 |
_clone_visited(area), _old_new(area) |
|
35558
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
516 |
{ |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
517 |
Node* head = _lpt->_head; |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
518 |
Node* entry = head->in(LoopNode::EntryControl); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
519 |
if (entry->outcnt() != 1) { |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
520 |
// If a node is pinned between the predicates and the loop |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
521 |
// entry, we won't be able to move any node in the loop that |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
522 |
// depends on it above it in a predicate. Mark all those nodes |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
523 |
// as non loop invariatnt. |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
524 |
Unique_Node_List wq; |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
525 |
wq.push(entry); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
526 |
for (uint next = 0; next < wq.size(); ++next) { |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
527 |
Node *n = wq.at(next); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
528 |
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
529 |
Node* u = n->fast_out(i); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
530 |
if (!u->is_CFG()) { |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
531 |
Node* c = _phase->get_ctrl(u); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
532 |
if (_lpt->is_member(_phase->get_loop(c)) || _phase->is_dominator(c, head)) { |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
533 |
_visited.set(u->_idx); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
534 |
wq.push(u); |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
535 |
} |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
536 |
} |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
537 |
} |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
538 |
} |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
539 |
} |
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
540 |
} |
9101 | 541 |
|
542 |
// Map old to n for invariance computation and clone |
|
543 |
void map_ctrl(Node* old, Node* n) { |
|
544 |
assert(old->is_CFG() && n->is_CFG(), "must be"); |
|
545 |
_old_new.map(old->_idx, n); // "clone" of old is n |
|
546 |
_invariant.set(old->_idx); // old is invariant |
|
547 |
_clone_visited.set(old->_idx); |
|
548 |
} |
|
549 |
||
550 |
// Driver function to compute invariance |
|
551 |
bool is_invariant(Node* n) { |
|
552 |
if (!_visited.test_set(n->_idx)) |
|
553 |
compute_invariance(n); |
|
554 |
return (_invariant.test(n->_idx) != 0); |
|
555 |
} |
|
556 |
||
557 |
// Driver function to clone invariant |
|
558 |
Node* clone(Node* n, Node* ctrl) { |
|
559 |
assert(ctrl->is_CFG(), "must be"); |
|
560 |
assert(_invariant.test(n->_idx), "must be an invariant"); |
|
561 |
if (!_clone_visited.test(n->_idx)) |
|
562 |
clone_nodes(n, ctrl); |
|
563 |
return _old_new[n->_idx]; |
|
564 |
} |
|
565 |
}; |
|
566 |
||
567 |
//------------------------------is_range_check_if ----------------------------------- |
|
568 |
// Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format |
|
569 |
// Note: this function is particularly designed for loop predication. We require load_range |
|
570 |
// and offset to be loop invariant computed on the fly by "invar" |
|
571 |
bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const { |
|
572 |
if (!is_loop_exit(iff)) { |
|
573 |
return false; |
|
574 |
} |
|
575 |
if (!iff->in(1)->is_Bool()) { |
|
576 |
return false; |
|
577 |
} |
|
578 |
const BoolNode *bol = iff->in(1)->as_Bool(); |
|
579 |
if (bol->_test._test != BoolTest::lt) { |
|
580 |
return false; |
|
581 |
} |
|
582 |
if (!bol->in(1)->is_Cmp()) { |
|
583 |
return false; |
|
584 |
} |
|
585 |
const CmpNode *cmp = bol->in(1)->as_Cmp(); |
|
586 |
if (cmp->Opcode() != Op_CmpU) { |
|
587 |
return false; |
|
588 |
} |
|
589 |
Node* range = cmp->in(2); |
|
34180
f0ec91019db2
8042997: Make intrinsic some or all check index/range methods
roland
parents:
34164
diff
changeset
|
590 |
if (range->Opcode() != Op_LoadRange && !iff->is_RangeCheck()) { |
9101 | 591 |
const TypeInt* tint = phase->_igvn.type(range)->isa_int(); |
9446 | 592 |
if (tint == NULL || tint->empty() || tint->_lo < 0) { |
9101 | 593 |
// Allow predication on positive values that aren't LoadRanges. |
594 |
// This allows optimization of loops where the length of the |
|
595 |
// array is a known value and doesn't need to be loaded back |
|
596 |
// from the array. |
|
597 |
return false; |
|
598 |
} |
|
599 |
} |
|
600 |
if (!invar.is_invariant(range)) { |
|
601 |
return false; |
|
602 |
} |
|
603 |
Node *iv = _head->as_CountedLoop()->phi(); |
|
604 |
int scale = 0; |
|
605 |
Node *offset = NULL; |
|
606 |
if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) { |
|
607 |
return false; |
|
608 |
} |
|
609 |
if (offset && !invar.is_invariant(offset)) { // offset must be invariant |
|
610 |
return false; |
|
611 |
} |
|
612 |
return true; |
|
613 |
} |
|
614 |
||
615 |
//------------------------------rc_predicate----------------------------------- |
|
616 |
// Create a range check predicate |
|
617 |
// |
|
618 |
// for (i = init; i < limit; i += stride) { |
|
619 |
// a[scale*i+offset] |
|
620 |
// } |
|
621 |
// |
|
622 |
// Compute max(scale*i + offset) for init <= i < limit and build the predicate |
|
623 |
// as "max(scale*i + offset) u< a.length". |
|
624 |
// |
|
625 |
// There are two cases for max(scale*i + offset): |
|
626 |
// (1) stride*scale > 0 |
|
627 |
// max(scale*i + offset) = scale*(limit-stride) + offset |
|
628 |
// (2) stride*scale < 0 |
|
629 |
// max(scale*i + offset) = scale*init + offset |
|
9446 | 630 |
BoolNode* PhaseIdealLoop::rc_predicate(IdealLoopTree *loop, Node* ctrl, |
9101 | 631 |
int scale, Node* offset, |
632 |
Node* init, Node* limit, Node* stride, |
|
633 |
Node* range, bool upper) { |
|
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
634 |
stringStream* predString = NULL; |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
635 |
if (TraceLoopPredicate) { |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
636 |
predString = new stringStream(); |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
637 |
predString->print("rc_predicate "); |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
638 |
} |
9101 | 639 |
|
640 |
Node* max_idx_expr = init; |
|
641 |
int stride_con = stride->get_int(); |
|
642 |
if ((stride_con > 0) == (scale > 0) == upper) { |
|
37287
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
643 |
// Limit is not exact. |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
644 |
// Calculate exact limit here. |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
645 |
// Note, counted loop's test is '<' or '>'. |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
646 |
limit = exact_limit(loop); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
647 |
max_idx_expr = new SubINode(limit, stride); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
648 |
register_new_node(max_idx_expr, ctrl); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
649 |
if (TraceLoopPredicate) predString->print("(limit - stride) "); |
9101 | 650 |
} else { |
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
651 |
if (TraceLoopPredicate) predString->print("init "); |
9101 | 652 |
} |
653 |
||
654 |
if (scale != 1) { |
|
655 |
ConNode* con_scale = _igvn.intcon(scale); |
|
35558
9b3b2740c3ec
8146792: Predicate moved after partial peel may lead to broken graph
roland
parents:
34180
diff
changeset
|
656 |
set_ctrl(con_scale, C->root()); |
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
657 |
max_idx_expr = new MulINode(max_idx_expr, con_scale); |
9101 | 658 |
register_new_node(max_idx_expr, ctrl); |
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
659 |
if (TraceLoopPredicate) predString->print("* %d ", scale); |
9101 | 660 |
} |
661 |
||
662 |
if (offset && (!offset->is_Con() || offset->get_int() != 0)){ |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
663 |
max_idx_expr = new AddINode(max_idx_expr, offset); |
9101 | 664 |
register_new_node(max_idx_expr, ctrl); |
665 |
if (TraceLoopPredicate) |
|
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
666 |
if (offset->is_Con()) predString->print("+ %d ", offset->get_int()); |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
667 |
else predString->print("+ offset "); |
9101 | 668 |
} |
669 |
||
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
670 |
CmpUNode* cmp = new CmpUNode(max_idx_expr, range); |
9101 | 671 |
register_new_node(cmp, ctrl); |
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
672 |
BoolNode* bol = new BoolNode(cmp, BoolTest::lt); |
9101 | 673 |
register_new_node(bol, ctrl); |
674 |
||
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
675 |
if (TraceLoopPredicate) { |
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
676 |
predString->print_cr("<u range"); |
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
23528
diff
changeset
|
677 |
tty->print("%s", predString->as_string()); |
9437
9981851b4b8c
7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp
never
parents:
9101
diff
changeset
|
678 |
} |
9101 | 679 |
return bol; |
680 |
} |
|
681 |
||
682 |
//------------------------------ loop_predication_impl-------------------------- |
|
683 |
// Insert loop predicates for null checks and range checks |
|
684 |
bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) { |
|
685 |
if (!UseLoopPredicate) return false; |
|
686 |
||
687 |
if (!loop->_head->is_Loop()) { |
|
688 |
// Could be a simple region when irreducible loops are present. |
|
689 |
return false; |
|
690 |
} |
|
9446 | 691 |
LoopNode* head = loop->_head->as_Loop(); |
9101 | 692 |
|
9446 | 693 |
if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) { |
9101 | 694 |
// do nothing for infinite loops |
695 |
return false; |
|
696 |
} |
|
697 |
||
698 |
CountedLoopNode *cl = NULL; |
|
10263
fa58671dde31
7077439: Possible reference through NULL in loopPredicate.cpp:726
kvn
parents:
10258
diff
changeset
|
699 |
if (head->is_valid_counted_loop()) { |
9446 | 700 |
cl = head->as_CountedLoop(); |
9101 | 701 |
// do nothing for iteration-splitted loops |
702 |
if (!cl->is_normal_loop()) return false; |
|
10253
35b975b1e8f3
7070134: Hotspot crashes with sigsegv from PorterStemmer
kvn
parents:
9446
diff
changeset
|
703 |
// Avoid RCE if Counted loop's test is '!='. |
35b975b1e8f3
7070134: Hotspot crashes with sigsegv from PorterStemmer
kvn
parents:
9446
diff
changeset
|
704 |
BoolTest::mask bt = cl->loopexit()->test_trip(); |
35b975b1e8f3
7070134: Hotspot crashes with sigsegv from PorterStemmer
kvn
parents:
9446
diff
changeset
|
705 |
if (bt != BoolTest::lt && bt != BoolTest::gt) |
35b975b1e8f3
7070134: Hotspot crashes with sigsegv from PorterStemmer
kvn
parents:
9446
diff
changeset
|
706 |
cl = NULL; |
9101 | 707 |
} |
708 |
||
9446 | 709 |
Node* entry = head->in(LoopNode::EntryControl); |
710 |
ProjNode *predicate_proj = NULL; |
|
711 |
// Loop limit check predicate should be near the loop. |
|
37287
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
712 |
predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check); |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
713 |
if (predicate_proj != NULL) |
c2660335bf81
8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents:
35558
diff
changeset
|
714 |
entry = predicate_proj->in(0)->in(0); |
9446 | 715 |
predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate); |
9101 | 716 |
if (!predicate_proj) { |
717 |
#ifndef PRODUCT |
|
718 |
if (TraceLoopPredicate) { |
|
719 |
tty->print("missing predicate:"); |
|
720 |
loop->dump_head(); |
|
9446 | 721 |
head->dump(1); |
9101 | 722 |
} |
723 |
#endif |
|
724 |
return false; |
|
725 |
} |
|
726 |
ConNode* zero = _igvn.intcon(0); |
|
727 |
set_ctrl(zero, C->root()); |
|
728 |
||
729 |
ResourceArea *area = Thread::current()->resource_area(); |
|
730 |
Invariance invar(area, loop); |
|
731 |
||
732 |
// Create list of if-projs such that a newer proj dominates all older |
|
733 |
// projs in the list, and they all dominate loop->tail() |
|
734 |
Node_List if_proj_list(area); |
|
735 |
Node *current_proj = loop->tail(); //start from tail |
|
736 |
while (current_proj != head) { |
|
737 |
if (loop == get_loop(current_proj) && // still in the loop ? |
|
738 |
current_proj->is_Proj() && // is a projection ? |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
739 |
(current_proj->in(0)->Opcode() == Op_If || |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
740 |
current_proj->in(0)->Opcode() == Op_RangeCheck)) { // is a if projection ? |
9101 | 741 |
if_proj_list.push(current_proj); |
742 |
} |
|
743 |
current_proj = idom(current_proj); |
|
744 |
} |
|
745 |
||
746 |
bool hoisted = false; // true if at least one proj is promoted |
|
747 |
while (if_proj_list.size() > 0) { |
|
748 |
// Following are changed to nonnull when a predicate can be hoisted |
|
749 |
ProjNode* new_predicate_proj = NULL; |
|
750 |
||
751 |
ProjNode* proj = if_proj_list.pop()->as_Proj(); |
|
752 |
IfNode* iff = proj->in(0)->as_If(); |
|
753 |
||
21089
e1986ff6fe2e
8024069: replace_in_map() should operate on parent maps
roland
parents:
17383
diff
changeset
|
754 |
if (!proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none)) { |
9101 | 755 |
if (loop->is_loop_exit(iff)) { |
756 |
// stop processing the remaining projs in the list because the execution of them |
|
757 |
// depends on the condition of "iff" (iff->in(1)). |
|
758 |
break; |
|
759 |
} else { |
|
760 |
// Both arms are inside the loop. There are two cases: |
|
761 |
// (1) there is one backward branch. In this case, any remaining proj |
|
762 |
// in the if_proj list post-dominates "iff". So, the condition of "iff" |
|
763 |
// does not determine the execution the remining projs directly, and we |
|
764 |
// can safely continue. |
|
765 |
// (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj" |
|
766 |
// does not dominate loop->tail(), so it can not be in the if_proj list. |
|
767 |
continue; |
|
768 |
} |
|
769 |
} |
|
770 |
||
771 |
Node* test = iff->in(1); |
|
772 |
if (!test->is_Bool()){ //Conv2B, ... |
|
773 |
continue; |
|
774 |
} |
|
775 |
BoolNode* bol = test->as_Bool(); |
|
776 |
if (invar.is_invariant(bol)) { |
|
777 |
// Invariant test |
|
778 |
new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL, |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
779 |
Deoptimization::Reason_predicate, |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
780 |
iff->Opcode()); |
9101 | 781 |
Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0); |
782 |
BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool(); |
|
783 |
||
784 |
// Negate test if necessary |
|
785 |
bool negated = false; |
|
786 |
if (proj->_con != predicate_proj->_con) { |
|
24923
9631f7d691dc
8034812: remove IDX_INIT macro hack in Node class
thartmann
parents:
24424
diff
changeset
|
787 |
new_predicate_bol = new BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate()); |
9101 | 788 |
register_new_node(new_predicate_bol, ctrl); |
789 |
negated = true; |
|
790 |
} |
|
791 |
IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If(); |
|
792 |
_igvn.hash_delete(new_predicate_iff); |
|
793 |
new_predicate_iff->set_req(1, new_predicate_bol); |
|
794 |
#ifndef PRODUCT |
|
795 |
if (TraceLoopPredicate) { |
|
796 |
tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx); |
|
797 |
loop->dump_head(); |
|
798 |
} else if (TraceLoopOpts) { |
|
799 |
tty->print("Predicate IC "); |
|
800 |
loop->dump_head(); |
|
801 |
} |
|
802 |
#endif |
|
26166 | 803 |
} else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) { |
9101 | 804 |
// Range check for counted loops |
805 |
const Node* cmp = bol->in(1)->as_Cmp(); |
|
806 |
Node* idx = cmp->in(1); |
|
807 |
assert(!invar.is_invariant(idx), "index is variant"); |
|
808 |
Node* rng = cmp->in(2); |
|
9446 | 809 |
assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be"); |
9101 | 810 |
assert(invar.is_invariant(rng), "range must be invariant"); |
811 |
int scale = 1; |
|
812 |
Node* offset = zero; |
|
813 |
bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset); |
|
814 |
assert(ok, "must be index expression"); |
|
815 |
||
816 |
Node* init = cl->init_trip(); |
|
817 |
Node* limit = cl->limit(); |
|
818 |
Node* stride = cl->stride(); |
|
819 |
||
820 |
// Build if's for the upper and lower bound tests. The |
|
821 |
// lower_bound test will dominate the upper bound test and all |
|
822 |
// cloned or created nodes will use the lower bound test as |
|
823 |
// their declared control. |
|
34164
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
824 |
ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode()); |
a9e6034d7707
8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents:
32733
diff
changeset
|
825 |
ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate, iff->Opcode()); |
9101 | 826 |
assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate"); |
827 |
Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0); |
|
828 |
||
829 |
// Perform cloning to keep Invariance state correct since the |
|
830 |
// late schedule will place invariant things in the loop. |
|
831 |
rng = invar.clone(rng, ctrl); |
|
832 |
if (offset && offset != zero) { |
|
833 |
assert(invar.is_invariant(offset), "offset must be loop invariant"); |
|
834 |
offset = invar.clone(offset, ctrl); |
|
835 |
} |
|
836 |
||
837 |
// Test the lower bound |
|
26166 | 838 |
BoolNode* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false); |
839 |
// Negate test if necessary |
|
840 |
bool negated = false; |
|
841 |
if (proj->_con != predicate_proj->_con) { |
|
842 |
lower_bound_bol = new BoolNode(lower_bound_bol->in(1), lower_bound_bol->_test.negate()); |
|
843 |
register_new_node(lower_bound_bol, ctrl); |
|
844 |
negated = true; |
|
845 |
} |
|
9101 | 846 |
IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If(); |
847 |
_igvn.hash_delete(lower_bound_iff); |
|
848 |
lower_bound_iff->set_req(1, lower_bound_bol); |
|
26166 | 849 |
if (TraceLoopPredicate) tty->print_cr("lower bound check if: %s %d ", negated ? " negated" : "", lower_bound_iff->_idx); |
9101 | 850 |
|
851 |
// Test the upper bound |
|
26166 | 852 |
BoolNode* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true); |
853 |
negated = false; |
|
854 |
if (proj->_con != predicate_proj->_con) { |
|
855 |
upper_bound_bol = new BoolNode(upper_bound_bol->in(1), upper_bound_bol->_test.negate()); |
|
856 |
register_new_node(upper_bound_bol, ctrl); |
|
857 |
negated = true; |
|
858 |
} |
|
9101 | 859 |
IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If(); |
860 |
_igvn.hash_delete(upper_bound_iff); |
|
861 |
upper_bound_iff->set_req(1, upper_bound_bol); |
|
26166 | 862 |
if (TraceLoopPredicate) tty->print_cr("upper bound check if: %s %d ", negated ? " negated" : "", lower_bound_iff->_idx); |
9101 | 863 |
|
864 |
// Fall through into rest of the clean up code which will move |
|
865 |
// any dependent nodes onto the upper bound test. |
|
866 |
new_predicate_proj = upper_bound_proj; |
|
867 |
||
868 |
#ifndef PRODUCT |
|
869 |
if (TraceLoopOpts && !TraceLoopPredicate) { |
|
870 |
tty->print("Predicate RC "); |
|
871 |
loop->dump_head(); |
|
872 |
} |
|
873 |
#endif |
|
874 |
} else { |
|
875 |
// Loop variant check (for example, range check in non-counted loop) |
|
876 |
// with uncommon trap. |
|
877 |
continue; |
|
878 |
} |
|
879 |
assert(new_predicate_proj != NULL, "sanity"); |
|
880 |
// Success - attach condition (new_predicate_bol) to predicate if |
|
881 |
invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate |
|
882 |
||
883 |
// Eliminate the old If in the loop body |
|
884 |
dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con ); |
|
885 |
||
886 |
hoisted = true; |
|
887 |
C->set_major_progress(); |
|
888 |
} // end while |
|
889 |
||
890 |
#ifndef PRODUCT |
|
891 |
// report that the loop predication has been actually performed |
|
892 |
// for this loop |
|
893 |
if (TraceLoopPredicate && hoisted) { |
|
894 |
tty->print("Loop Predication Performed:"); |
|
895 |
loop->dump_head(); |
|
896 |
} |
|
897 |
#endif |
|
898 |
||
899 |
return hoisted; |
|
900 |
} |
|
901 |
||
902 |
//------------------------------loop_predication-------------------------------- |
|
903 |
// driver routine for loop predication optimization |
|
904 |
bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) { |
|
905 |
bool hoisted = false; |
|
906 |
// Recursively promote predicates |
|
907 |
if (_child) { |
|
908 |
hoisted = _child->loop_predication( phase); |
|
909 |
} |
|
910 |
||
911 |
// self |
|
912 |
if (!_irreducible && !tail()->is_top()) { |
|
913 |
hoisted |= phase->loop_predication_impl(this); |
|
914 |
} |
|
915 |
||
916 |
if (_next) { //sibling |
|
917 |
hoisted |= _next->loop_predication( phase); |
|
918 |
} |
|
919 |
||
920 |
return hoisted; |
|
921 |
} |