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