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