1 /*
2  * Copyright (c) 2011, 2018, 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/convertnode.hpp"
31 #include "opto/loopnode.hpp"
32 #include "opto/matcher.hpp"
33 #include "opto/mulnode.hpp"
34 #include "opto/opaquenode.hpp"
35 #include "opto/rootnode.hpp"
36 #include "opto/subnode.hpp"
37 #include <fenv.h>
38 #include <math.h>
39 
40 /*
41  * The general idea of Loop Predication is to insert a predicate on the entry
42  * path to a loop, and raise a uncommon trap if the check of the condition fails.
43  * The condition checks are promoted from inside the loop body, and thus
44  * the checks inside the loop could be eliminated. Currently, loop predication
45  * optimization has been applied to remove array range check and loop invariant
46  * checks (such as null checks).
47  *
48  * There are at least 3 kinds of predicates: a place holder inserted
49  * at parse time, the tests added by predication above the place
50  * holder (referred to as concrete predicates), skeleton predicates
51  * that are added between main loop and pre loop to protect C2 from
52  * inconsistencies in some rare cases of over unrolling. Skeleton
53  * predicates themselves are expanded and updated as unrolling
54  * proceeds. They don't compile to any code.
55  *
56 */
57 
58 //-------------------------------register_control-------------------------
register_control(Node * n,IdealLoopTree * loop,Node * pred,bool update_body)59 void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred, bool update_body) {
60   assert(n->is_CFG(), "msust be control node");
61   _igvn.register_new_node_with_optimizer(n);
62   if (update_body) {
63     loop->_body.push(n);
64   }
65   set_loop(n, loop);
66   // When called from beautify_loops() idom is not constructed yet.
67   if (_idom != NULL) {
68     set_idom(n, pred, dom_depth(pred));
69   }
70 }
71 
72 //------------------------------create_new_if_for_predicate------------------------
73 // create a new if above the uct_if_pattern for the predicate to be promoted.
74 //
75 //          before                                after
76 //        ----------                           ----------
77 //           ctrl                                 ctrl
78 //            |                                     |
79 //            |                                     |
80 //            v                                     v
81 //           iff                                 new_iff
82 //          /    \                                /      \
83 //         /      \                              /        \
84 //        v        v                            v          v
85 //  uncommon_proj cont_proj                   if_uct     if_cont
86 // \      |        |                           |          |
87 //  \     |        |                           |          |
88 //   v    v        v                           |          v
89 //     rgn       loop                          |         iff
90 //      |                                      |        /     \
91 //      |                                      |       /       \
92 //      v                                      |      v         v
93 // uncommon_trap                               | uncommon_proj cont_proj
94 //                                           \  \    |           |
95 //                                            \  \   |           |
96 //                                             v  v  v           v
97 //                                               rgn           loop
98 //                                                |
99 //                                                |
100 //                                                v
101 //                                           uncommon_trap
102 //
103 //
104 // We will create a region to guard the uct call if there is no one there.
105 // The continuation projection (if_cont) of the new_iff is returned which
106 // is by default a true projection if 'if_cont_is_true_proj' is true.
107 // Otherwise, the continuation projection is set up to be the false
108 // projection. This code is also used to clone predicates to cloned loops.
create_new_if_for_predicate(ProjNode * cont_proj,Node * new_entry,Deoptimization::DeoptReason reason,int opcode,bool if_cont_is_true_proj)109 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
110                                                       Deoptimization::DeoptReason reason,
111                                                       int opcode, bool if_cont_is_true_proj) {
112   assert(cont_proj->is_uncommon_trap_if_pattern(reason), "must be a uct if pattern!");
113   IfNode* iff = cont_proj->in(0)->as_If();
114 
115   ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
116   Node     *rgn   = uncommon_proj->unique_ctrl_out();
117   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
118 
119   uint proj_index = 1; // region's edge corresponding to uncommon_proj
120   if (!rgn->is_Region()) { // create a region to guard the call
121     assert(rgn->is_Call(), "must be call uct");
122     CallNode* call = rgn->as_Call();
123     IdealLoopTree* loop = get_loop(call);
124     rgn = new RegionNode(1);
125     Node* uncommon_proj_orig = uncommon_proj;
126     uncommon_proj = uncommon_proj->clone()->as_Proj();
127     register_control(uncommon_proj, loop, iff);
128     rgn->add_req(uncommon_proj);
129     register_control(rgn, loop, uncommon_proj);
130     _igvn.replace_input_of(call, 0, rgn);
131     // When called from beautify_loops() idom is not constructed yet.
132     if (_idom != NULL) {
133       set_idom(call, rgn, dom_depth(rgn));
134     }
135     // Move nodes pinned on the projection or whose control is set to
136     // the projection to the region.
137     lazy_replace(uncommon_proj_orig, rgn);
138   } else {
139     // Find region's edge corresponding to uncommon_proj
140     for (; proj_index < rgn->req(); proj_index++)
141       if (rgn->in(proj_index) == uncommon_proj) break;
142     assert(proj_index < rgn->req(), "sanity");
143   }
144 
145   Node* entry = iff->in(0);
146   if (new_entry != NULL) {
147     // Clonning the predicate to new location.
148     entry = new_entry;
149   }
150   // Create new_iff
151   IdealLoopTree* lp = get_loop(entry);
152   IfNode* new_iff = NULL;
153   if (opcode == Op_If) {
154     new_iff = new IfNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
155   } else {
156     assert(opcode == Op_RangeCheck, "no other if variant here");
157     new_iff = new RangeCheckNode(entry, iff->in(1), iff->_prob, iff->_fcnt);
158   }
159   register_control(new_iff, lp, entry);
160   Node* if_cont;
161   Node* if_uct;
162   if (if_cont_is_true_proj) {
163     if_cont = new IfTrueNode(new_iff);
164     if_uct  = new IfFalseNode(new_iff);
165   } else {
166     if_uct  = new IfTrueNode(new_iff);
167     if_cont = new IfFalseNode(new_iff);
168   }
169 
170   if (cont_proj->is_IfFalse()) {
171     // Swap
172     Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
173   }
174   register_control(if_cont, lp, new_iff);
175   register_control(if_uct, get_loop(rgn), new_iff);
176 
177   // if_uct to rgn
178   _igvn.hash_delete(rgn);
179   rgn->add_req(if_uct);
180   // When called from beautify_loops() idom is not constructed yet.
181   if (_idom != NULL) {
182     Node* ridom = idom(rgn);
183     Node* nrdom = dom_lca_internal(ridom, new_iff);
184     set_idom(rgn, nrdom, dom_depth(rgn));
185   }
186 
187   // If rgn has phis add new edges which has the same
188   // value as on original uncommon_proj pass.
189   assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last");
190   bool has_phi = false;
191   for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) {
192     Node* use = rgn->fast_out(i);
193     if (use->is_Phi() && use->outcnt() > 0) {
194       assert(use->in(0) == rgn, "");
195       _igvn.rehash_node_delayed(use);
196       use->add_req(use->in(proj_index));
197       has_phi = true;
198     }
199   }
200   assert(!has_phi || rgn->req() > 3, "no phis when region is created");
201 
202   if (new_entry == NULL) {
203     // Attach if_cont to iff
204     _igvn.replace_input_of(iff, 0, if_cont);
205     if (_idom != NULL) {
206       set_idom(iff, if_cont, dom_depth(iff));
207     }
208   }
209   return if_cont->as_Proj();
210 }
211 
212 //--------------------------clone_predicate-----------------------
clone_loop_predicate(ProjNode * predicate_proj,Node * new_entry,Deoptimization::DeoptReason reason,bool is_slow_loop,uint idx_before_clone,Node_List & old_new)213 ProjNode* PhaseIdealLoop::clone_loop_predicate(ProjNode* predicate_proj, Node* new_entry, Deoptimization::DeoptReason reason,
214                                                bool is_slow_loop, uint idx_before_clone, Node_List &old_new) {
215   ProjNode* new_predicate_proj = create_new_if_for_predicate(predicate_proj, new_entry, reason, Op_If);
216   IfNode* iff = new_predicate_proj->in(0)->as_If();
217   Node* ctrl  = iff->in(0);
218 
219   // Match original condition since predicate's projections could be swapped.
220   assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
221   Node* opq = new Opaque1Node(C, predicate_proj->in(0)->in(1)->in(1)->in(1));
222   C->add_predicate_opaq(opq);
223   Node* bol = new Conv2BNode(opq);
224   register_new_node(opq, ctrl);
225   register_new_node(bol, ctrl);
226   _igvn.hash_delete(iff);
227   iff->set_req(1, bol);
228   clone_concrete_loop_predicates(reason, predicate_proj, new_predicate_proj, is_slow_loop, idx_before_clone, old_new);
229   return new_predicate_proj;
230 }
231 
232 // Clones all non-empty loop predicates (including skeleton predicates) starting at 'old_predicate_proj' to 'new_predicate_proj'
233 // and rewires the control edges of data nodes in the loop to the old predicates to the new cloned predicates.
clone_concrete_loop_predicates(Deoptimization::DeoptReason reason,ProjNode * old_predicate_proj,ProjNode * new_predicate_proj,bool is_slow_loop,uint idx_before_clone,Node_List & old_new)234 void PhaseIdealLoop::clone_concrete_loop_predicates(Deoptimization::DeoptReason reason, ProjNode* old_predicate_proj,
235                                                     ProjNode* new_predicate_proj, bool is_slow_loop, uint idx_before_clone,
236                                                     Node_List &old_new) {
237   assert(old_predicate_proj->is_Proj(), "must be projection");
238   IfNode* iff = old_predicate_proj->in(0)->as_If();
239   ProjNode* uncommon_proj = iff->proj_out(1 - old_predicate_proj->as_Proj()->_con);
240   Node* rgn = uncommon_proj->unique_ctrl_out();
241   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
242   assert(iff->in(1)->in(1)->Opcode() == Op_Opaque1, "unexpected predicate shape");
243   Node* predicate = iff->in(0);
244   Node* current_proj = old_predicate_proj;
245   Node* prev_proj = current_proj;
246   Unique_Node_List list;
247   while (predicate != NULL && predicate->is_Proj() && predicate->in(0)->is_If()) {
248     iff = predicate->in(0)->as_If();
249     uncommon_proj = iff->proj_out(1 - predicate->as_Proj()->_con);
250     if (uncommon_proj->unique_ctrl_out() != rgn)
251       break;
252     if (iff->is_RangeCheck()) {
253       // Only need to clone range check predicates as those can be changed and duplicated by inserting pre/main/post loops
254       // and doing loop unrolling. Push the original predicates on a list to later process them in reverse order to keep the
255       // original predicate order.
256       list.push(predicate);
257 #ifdef ASSERT
258     } else {
259       // All other If predicates should not have a control input to nodes belonging to the original loop
260       for (DUIterator i = predicate->outs(); predicate->has_out(i); i++) {
261         Node* old_node = predicate->out(i);
262         Node* new_node = old_new[old_node->_idx];
263         if (!old_node->is_CFG() && new_node != NULL && old_node->_idx >= idx_before_clone) {
264           assert(false, "should not be part of the original loop");
265         }
266       }
267 #endif
268     }
269     predicate = predicate->in(0)->in(0);
270   }
271 
272   // Process in reverse order such that 'create_new_if_for_predicate' can be used and the original order is maintained
273   for (int i = list.size()-1; i >= 0; i--) {
274     predicate = list.at(i);
275     assert(predicate->in(0)->is_If(), "must be If node");
276     iff = predicate->in(0)->as_If();
277     assert(predicate->is_Proj() && predicate->as_Proj()->is_IfProj() && iff->is_RangeCheck(), "predicate must be a projection of a range check");
278     IfProjNode* predicate_proj = predicate->as_IfProj();
279 
280     // cloned_proj is the same type of projection as the original predicate projection (IfTrue or IfFalse)
281     ProjNode* cloned_proj = create_new_if_for_predicate(new_predicate_proj, NULL, reason, Op_RangeCheck, predicate_proj->is_IfTrue());
282 
283     // Replace bool input by input from original predicate
284     _igvn.replace_input_of(cloned_proj->in(0), 1, iff->in(1));
285 
286     if (is_slow_loop) {
287       for (DUIterator i = predicate->outs(); predicate->has_out(i); i++) {
288         Node* slow_node = predicate->out(i);
289         Node* fast_node = old_new[slow_node->_idx];
290         if (!slow_node->is_CFG() && fast_node != NULL && slow_node->_idx > idx_before_clone) {
291           // 'slow_node' is a data node and part of the slow loop. This is a clone of the fast loop node
292           // which was temporarily added below in order to verify that 'slow_node' is a clone of 'fast_node'.
293           // Update the control input and reset the mapping for 'slow_node' back to NULL.
294           _igvn.replace_input_of(slow_node, 0, cloned_proj);
295           old_new.map(slow_node->_idx, NULL);
296           --i;
297         }
298         assert(slow_node->_idx <= idx_before_clone || old_new[slow_node->_idx] == NULL, "mapping of cloned nodes must be null");
299       }
300 
301       // Let old predicates before unswitched loops which were cloned die if all their control edges were rewired
302       // to the cloned predicates in the unswitched loops.
303       if (predicate->outcnt() == 1) {
304         _igvn.replace_input_of(iff, 1, _igvn.intcon(predicate_proj->_con));
305       }
306     } else {
307       // Fast loop
308       for (DUIterator i = predicate->outs(); predicate->has_out(i); i++) {
309         Node* fast_node = predicate->out(i);
310         Node* slow_node = old_new[fast_node->_idx];
311         if (!fast_node->is_CFG() && slow_node != NULL && slow_node->_idx > idx_before_clone) {
312           // 'fast_node' is a data node and part of the fast loop. Add the clone of the fast loop node
313           // to the 'old_new' mapping in order to verify later when cloning the predicates for the slow loop
314           // that 'slow_node' is a clone of 'fast_node'. Update the control input for 'fast_node'.
315           _igvn.replace_input_of(fast_node, 0, cloned_proj);
316           assert(old_new[slow_node->_idx] == NULL, "mapping must be null for cloned nodes");
317           old_new.map(slow_node->_idx, fast_node);
318           --i;
319         }
320       }
321     }
322   }
323 }
324 
325 //--------------------------clone_loop_predicates-----------------------
326 // Clone loop predicates to cloned loops when unswitching a loop.
clone_loop_predicates(Node * old_entry,Node * new_entry,bool clone_limit_check,bool is_slow_loop,uint idx_before_clone,Node_List & old_new)327 Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check,
328                                             bool is_slow_loop, uint idx_before_clone, Node_List &old_new) {
329 #ifdef ASSERT
330   assert(LoopUnswitching, "sanity - only called when unswitching a loop");
331   if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) {
332     if (new_entry != NULL)
333       new_entry->dump();
334     assert(false, "not IfTrue, IfFalse, Region or SafePoint");
335   }
336 #endif
337   // Search original predicates
338   Node* entry = old_entry;
339   ProjNode* limit_check_proj = NULL;
340   limit_check_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
341   if (limit_check_proj != NULL) {
342     entry = skip_loop_predicates(entry);
343   }
344   ProjNode* profile_predicate_proj = NULL;
345   ProjNode* predicate_proj = NULL;
346   if (UseProfiledLoopPredicate) {
347     profile_predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate);
348     if (profile_predicate_proj != NULL) {
349       entry = skip_loop_predicates(entry);
350     }
351   }
352   if (UseLoopPredicate) {
353     predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
354   }
355   if (predicate_proj != NULL) { // right pattern that can be used by loop predication
356     // clone predicate
357     new_entry = clone_loop_predicate(predicate_proj, new_entry, Deoptimization::Reason_predicate, is_slow_loop,
358                                      idx_before_clone, old_new);
359     assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate");
360     if (TraceLoopPredicate) {
361       tty->print("Loop Predicate cloned: ");
362       debug_only( new_entry->in(0)->dump(); );
363     }
364   }
365   if (profile_predicate_proj != NULL) { // right pattern that can be used by loop predication
366     // clone predicate
367     new_entry = clone_loop_predicate(profile_predicate_proj, new_entry,Deoptimization::Reason_profile_predicate,
368                                      is_slow_loop, idx_before_clone, old_new);
369     assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate");
370     if (TraceLoopPredicate) {
371       tty->print("Loop Predicate cloned: ");
372       debug_only( new_entry->in(0)->dump(); );
373     }
374   }
375   if (limit_check_proj != NULL && clone_limit_check) {
376     // Clone loop limit check last to insert it before loop.
377     // Don't clone a limit check which was already finalized
378     // for this counted loop (only one limit check is needed).
379     new_entry = clone_loop_predicate(limit_check_proj, new_entry, Deoptimization::Reason_loop_limit_check,
380                                      is_slow_loop, idx_before_clone, old_new);
381     assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone limit check");
382     if (TraceLoopLimitCheck) {
383       tty->print("Loop Limit Check cloned: ");
384       debug_only( new_entry->in(0)->dump(); )
385     }
386   }
387   return new_entry;
388 }
389 
390 //--------------------------skip_loop_predicates------------------------------
391 // Skip related predicates.
skip_loop_predicates(Node * entry)392 Node* PhaseIdealLoop::skip_loop_predicates(Node* entry) {
393   IfNode* iff = entry->in(0)->as_If();
394   ProjNode* uncommon_proj = iff->proj_out(1 - entry->as_Proj()->_con);
395   Node* rgn = uncommon_proj->unique_ctrl_out();
396   assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
397   entry = entry->in(0)->in(0);
398   while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) {
399     uncommon_proj = entry->in(0)->as_If()->proj_out(1 - entry->as_Proj()->_con);
400     if (uncommon_proj->unique_ctrl_out() != rgn)
401       break;
402     entry = entry->in(0)->in(0);
403   }
404   return entry;
405 }
406 
skip_all_loop_predicates(Node * entry)407 Node* PhaseIdealLoop::skip_all_loop_predicates(Node* entry) {
408   Node* predicate = NULL;
409   predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
410   if (predicate != NULL) {
411     entry = skip_loop_predicates(entry);
412   }
413   if (UseProfiledLoopPredicate) {
414     predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate);
415     if (predicate != NULL) { // right pattern that can be used by loop predication
416       entry = skip_loop_predicates(entry);
417     }
418   }
419   if (UseLoopPredicate) {
420     predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
421     if (predicate != NULL) { // right pattern that can be used by loop predication
422       entry = skip_loop_predicates(entry);
423     }
424   }
425   return entry;
426 }
427 
428 //--------------------------find_predicate_insertion_point-------------------
429 // Find a good location to insert a predicate
find_predicate_insertion_point(Node * start_c,Deoptimization::DeoptReason reason)430 ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) {
431   if (start_c == NULL || !start_c->is_Proj())
432     return NULL;
433   if (start_c->as_Proj()->is_uncommon_trap_if_pattern(reason)) {
434     return start_c->as_Proj();
435   }
436   return NULL;
437 }
438 
439 //--------------------------find_predicate------------------------------------
440 // Find a predicate
find_predicate(Node * entry)441 Node* PhaseIdealLoop::find_predicate(Node* entry) {
442   Node* predicate = NULL;
443   predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
444   if (predicate != NULL) { // right pattern that can be used by loop predication
445     return entry;
446   }
447   if (UseLoopPredicate) {
448     predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
449     if (predicate != NULL) { // right pattern that can be used by loop predication
450       return entry;
451     }
452   }
453   if (UseProfiledLoopPredicate) {
454     predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate);
455     if (predicate != NULL) { // right pattern that can be used by loop predication
456       return entry;
457     }
458   }
459   return NULL;
460 }
461 
462 //------------------------------Invariance-----------------------------------
463 // Helper class for loop_predication_impl to compute invariance on the fly and
464 // clone invariants.
465 class Invariance : public StackObj {
466   VectorSet _visited, _invariant;
467   Node_Stack _stack;
468   VectorSet _clone_visited;
469   Node_List _old_new; // map of old to new (clone)
470   IdealLoopTree* _lpt;
471   PhaseIdealLoop* _phase;
472 
473   // Helper function to set up the invariance for invariance computation
474   // If n is a known invariant, set up directly. Otherwise, look up the
475   // the possibility to push n onto the stack for further processing.
visit(Node * use,Node * n)476   void visit(Node* use, Node* n) {
477     if (_lpt->is_invariant(n)) { // known invariant
478       _invariant.set(n->_idx);
479     } else if (!n->is_CFG()) {
480       Node *n_ctrl = _phase->ctrl_or_self(n);
481       Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG
482       if (_phase->is_dominator(n_ctrl, u_ctrl)) {
483         _stack.push(n, n->in(0) == NULL ? 1 : 0);
484       }
485     }
486   }
487 
488   // Compute invariance for "the_node" and (possibly) all its inputs recursively
489   // on the fly
compute_invariance(Node * n)490   void compute_invariance(Node* n) {
491     assert(_visited.test(n->_idx), "must be");
492     visit(n, n);
493     while (_stack.is_nonempty()) {
494       Node*  n = _stack.node();
495       uint idx = _stack.index();
496       if (idx == n->req()) { // all inputs are processed
497         _stack.pop();
498         // n is invariant if it's inputs are all invariant
499         bool all_inputs_invariant = true;
500         for (uint i = 0; i < n->req(); i++) {
501           Node* in = n->in(i);
502           if (in == NULL) continue;
503           assert(_visited.test(in->_idx), "must have visited input");
504           if (!_invariant.test(in->_idx)) { // bad guy
505             all_inputs_invariant = false;
506             break;
507           }
508         }
509         if (all_inputs_invariant) {
510           // If n's control is a predicate that was moved out of the
511           // loop, it was marked invariant but n is only invariant if
512           // it depends only on that test. Otherwise, unless that test
513           // is out of the loop, it's not invariant.
514           if (n->is_CFG() || n->depends_only_on_test() || n->in(0) == NULL || !_phase->is_member(_lpt, n->in(0))) {
515             _invariant.set(n->_idx); // I am a invariant too
516           }
517         }
518       } else { // process next input
519         _stack.set_index(idx + 1);
520         Node* m = n->in(idx);
521         if (m != NULL && !_visited.test_set(m->_idx)) {
522           visit(n, m);
523         }
524       }
525     }
526   }
527 
528   // Helper function to set up _old_new map for clone_nodes.
529   // If n is a known invariant, set up directly ("clone" of n == n).
530   // Otherwise, push n onto the stack for real cloning.
clone_visit(Node * n)531   void clone_visit(Node* n) {
532     assert(_invariant.test(n->_idx), "must be invariant");
533     if (_lpt->is_invariant(n)) { // known invariant
534       _old_new.map(n->_idx, n);
535     } else { // to be cloned
536       assert(!n->is_CFG(), "should not see CFG here");
537       _stack.push(n, n->in(0) == NULL ? 1 : 0);
538     }
539   }
540 
541   // Clone "n" and (possibly) all its inputs recursively
clone_nodes(Node * n,Node * ctrl)542   void clone_nodes(Node* n, Node* ctrl) {
543     clone_visit(n);
544     while (_stack.is_nonempty()) {
545       Node*  n = _stack.node();
546       uint idx = _stack.index();
547       if (idx == n->req()) { // all inputs processed, clone n!
548         _stack.pop();
549         // clone invariant node
550         Node* n_cl = n->clone();
551         _old_new.map(n->_idx, n_cl);
552         _phase->register_new_node(n_cl, ctrl);
553         for (uint i = 0; i < n->req(); i++) {
554           Node* in = n_cl->in(i);
555           if (in == NULL) continue;
556           n_cl->set_req(i, _old_new[in->_idx]);
557         }
558       } else { // process next input
559         _stack.set_index(idx + 1);
560         Node* m = n->in(idx);
561         if (m != NULL && !_clone_visited.test_set(m->_idx)) {
562           clone_visit(m); // visit the input
563         }
564       }
565     }
566   }
567 
568  public:
Invariance(Arena * area,IdealLoopTree * lpt)569   Invariance(Arena* area, IdealLoopTree* lpt) :
570     _visited(area), _invariant(area),
571     _stack(area, 10 /* guess */),
572     _clone_visited(area), _old_new(area),
573     _lpt(lpt), _phase(lpt->_phase)
574   {
575     LoopNode* head = _lpt->_head->as_Loop();
576     Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
577     if (entry->outcnt() != 1) {
578       // If a node is pinned between the predicates and the loop
579       // entry, we won't be able to move any node in the loop that
580       // depends on it above it in a predicate. Mark all those nodes
581       // as non loop invariatnt.
582       Unique_Node_List wq;
583       wq.push(entry);
584       for (uint next = 0; next < wq.size(); ++next) {
585         Node *n = wq.at(next);
586         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
587           Node* u = n->fast_out(i);
588           if (!u->is_CFG()) {
589             Node* c = _phase->get_ctrl(u);
590             if (_lpt->is_member(_phase->get_loop(c)) || _phase->is_dominator(c, head)) {
591               _visited.set(u->_idx);
592               wq.push(u);
593             }
594           }
595         }
596       }
597     }
598   }
599 
600   // Map old to n for invariance computation and clone
map_ctrl(Node * old,Node * n)601   void map_ctrl(Node* old, Node* n) {
602     assert(old->is_CFG() && n->is_CFG(), "must be");
603     _old_new.map(old->_idx, n); // "clone" of old is n
604     _invariant.set(old->_idx);  // old is invariant
605     _clone_visited.set(old->_idx);
606   }
607 
608   // Driver function to compute invariance
is_invariant(Node * n)609   bool is_invariant(Node* n) {
610     if (!_visited.test_set(n->_idx))
611       compute_invariance(n);
612     return (_invariant.test(n->_idx) != 0);
613   }
614 
615   // Driver function to clone invariant
clone(Node * n,Node * ctrl)616   Node* clone(Node* n, Node* ctrl) {
617     assert(ctrl->is_CFG(), "must be");
618     assert(_invariant.test(n->_idx), "must be an invariant");
619     if (!_clone_visited.test(n->_idx))
620       clone_nodes(n, ctrl);
621     return _old_new[n->_idx];
622   }
623 };
624 
625 //------------------------------is_range_check_if -----------------------------------
626 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
627 // Note: this function is particularly designed for loop predication. We require load_range
628 //       and offset to be loop invariant computed on the fly by "invar"
is_range_check_if(IfNode * iff,PhaseIdealLoop * phase,Invariance & invar) const629 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
630   if (!is_loop_exit(iff)) {
631     return false;
632   }
633   if (!iff->in(1)->is_Bool()) {
634     return false;
635   }
636   const BoolNode *bol = iff->in(1)->as_Bool();
637   if (bol->_test._test != BoolTest::lt) {
638     return false;
639   }
640   if (!bol->in(1)->is_Cmp()) {
641     return false;
642   }
643   const CmpNode *cmp = bol->in(1)->as_Cmp();
644   if (cmp->Opcode() != Op_CmpU) {
645     return false;
646   }
647   Node* range = cmp->in(2);
648   if (range->Opcode() != Op_LoadRange && !iff->is_RangeCheck()) {
649     const TypeInt* tint = phase->_igvn.type(range)->isa_int();
650     if (tint == NULL || tint->empty() || tint->_lo < 0) {
651       // Allow predication on positive values that aren't LoadRanges.
652       // This allows optimization of loops where the length of the
653       // array is a known value and doesn't need to be loaded back
654       // from the array.
655       return false;
656     }
657   }
658   if (!invar.is_invariant(range)) {
659     return false;
660   }
661   Node *iv     = _head->as_CountedLoop()->phi();
662   int   scale  = 0;
663   Node *offset = NULL;
664   if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) {
665     return false;
666   }
667   if (offset && !invar.is_invariant(offset)) { // offset must be invariant
668     return false;
669   }
670   return true;
671 }
672 
673 //------------------------------rc_predicate-----------------------------------
674 // Create a range check predicate
675 //
676 // for (i = init; i < limit; i += stride) {
677 //    a[scale*i+offset]
678 // }
679 //
680 // Compute max(scale*i + offset) for init <= i < limit and build the predicate
681 // as "max(scale*i + offset) u< a.length".
682 //
683 // There are two cases for max(scale*i + offset):
684 // (1) stride*scale > 0
685 //   max(scale*i + offset) = scale*(limit-stride) + offset
686 // (2) stride*scale < 0
687 //   max(scale*i + offset) = scale*init + offset
rc_predicate(IdealLoopTree * loop,Node * ctrl,int scale,Node * offset,Node * init,Node * limit,jint stride,Node * range,bool upper,bool & overflow)688 BoolNode* PhaseIdealLoop::rc_predicate(IdealLoopTree *loop, Node* ctrl,
689                                        int scale, Node* offset,
690                                        Node* init, Node* limit, jint stride,
691                                        Node* range, bool upper, bool &overflow) {
692   jint con_limit  = (limit != NULL && limit->is_Con())  ? limit->get_int()  : 0;
693   jint con_init   = init->is_Con()   ? init->get_int()   : 0;
694   jint con_offset = offset->is_Con() ? offset->get_int() : 0;
695 
696   stringStream* predString = NULL;
697   if (TraceLoopPredicate) {
698     predString = new stringStream();
699     predString->print("rc_predicate ");
700   }
701 
702   overflow = false;
703   Node* max_idx_expr = NULL;
704   const TypeInt* idx_type = TypeInt::INT;
705   if ((stride > 0) == (scale > 0) == upper) {
706     guarantee(limit != NULL, "sanity");
707     if (TraceLoopPredicate) {
708       if (limit->is_Con()) {
709         predString->print("(%d ", con_limit);
710       } else {
711         predString->print("(limit ");
712       }
713       predString->print("- %d) ", stride);
714     }
715     // Check if (limit - stride) may overflow
716     const TypeInt* limit_type = _igvn.type(limit)->isa_int();
717     jint limit_lo = limit_type->_lo;
718     jint limit_hi = limit_type->_hi;
719     if ((stride > 0 && (java_subtract(limit_lo, stride) < limit_lo)) ||
720         (stride < 0 && (java_subtract(limit_hi, stride) > limit_hi))) {
721       // No overflow possible
722       ConINode* con_stride = _igvn.intcon(stride);
723       set_ctrl(con_stride, C->root());
724       max_idx_expr = new SubINode(limit, con_stride);
725       idx_type = TypeInt::make(limit_lo - stride, limit_hi - stride, limit_type->_widen);
726     } else {
727       // May overflow
728       overflow = true;
729       limit = new ConvI2LNode(limit);
730       register_new_node(limit, ctrl);
731       ConLNode* con_stride = _igvn.longcon(stride);
732       set_ctrl(con_stride, C->root());
733       max_idx_expr = new SubLNode(limit, con_stride);
734     }
735     register_new_node(max_idx_expr, ctrl);
736   } else {
737     if (TraceLoopPredicate) {
738       if (init->is_Con()) {
739         predString->print("%d ", con_init);
740       } else {
741         predString->print("init ");
742       }
743     }
744     idx_type = _igvn.type(init)->isa_int();
745     max_idx_expr = init;
746   }
747 
748   if (scale != 1) {
749     ConNode* con_scale = _igvn.intcon(scale);
750     set_ctrl(con_scale, C->root());
751     if (TraceLoopPredicate) {
752       predString->print("* %d ", scale);
753     }
754     // Check if (scale * max_idx_expr) may overflow
755     const TypeInt* scale_type = TypeInt::make(scale);
756     MulINode* mul = new MulINode(max_idx_expr, con_scale);
757     idx_type = (TypeInt*)mul->mul_ring(idx_type, scale_type);
758     if (overflow || TypeInt::INT->higher_equal(idx_type)) {
759       // May overflow
760       mul->destruct();
761       if (!overflow) {
762         max_idx_expr = new ConvI2LNode(max_idx_expr);
763         register_new_node(max_idx_expr, ctrl);
764       }
765       overflow = true;
766       con_scale = _igvn.longcon(scale);
767       set_ctrl(con_scale, C->root());
768       max_idx_expr = new MulLNode(max_idx_expr, con_scale);
769     } else {
770       // No overflow possible
771       max_idx_expr = mul;
772     }
773     register_new_node(max_idx_expr, ctrl);
774   }
775 
776   if (offset && (!offset->is_Con() || con_offset != 0)){
777     if (TraceLoopPredicate) {
778       if (offset->is_Con()) {
779         predString->print("+ %d ", con_offset);
780       } else {
781         predString->print("+ offset");
782       }
783     }
784     // Check if (max_idx_expr + offset) may overflow
785     const TypeInt* offset_type = _igvn.type(offset)->isa_int();
786     jint lo = java_add(idx_type->_lo, offset_type->_lo);
787     jint hi = java_add(idx_type->_hi, offset_type->_hi);
788     if (overflow || (lo > hi) ||
789         ((idx_type->_lo & offset_type->_lo) < 0 && lo >= 0) ||
790         ((~(idx_type->_hi | offset_type->_hi)) < 0 && hi < 0)) {
791       // May overflow
792       if (!overflow) {
793         max_idx_expr = new ConvI2LNode(max_idx_expr);
794         register_new_node(max_idx_expr, ctrl);
795       }
796       overflow = true;
797       offset = new ConvI2LNode(offset);
798       register_new_node(offset, ctrl);
799       max_idx_expr = new AddLNode(max_idx_expr, offset);
800     } else {
801       // No overflow possible
802       max_idx_expr = new AddINode(max_idx_expr, offset);
803     }
804     register_new_node(max_idx_expr, ctrl);
805   }
806 
807   CmpNode* cmp = NULL;
808   if (overflow) {
809     // Integer expressions may overflow, do long comparison
810     range = new ConvI2LNode(range);
811     register_new_node(range, ctrl);
812     cmp = new CmpULNode(max_idx_expr, range);
813   } else {
814     cmp = new CmpUNode(max_idx_expr, range);
815   }
816   register_new_node(cmp, ctrl);
817   BoolNode* bol = new BoolNode(cmp, BoolTest::lt);
818   register_new_node(bol, ctrl);
819 
820   if (TraceLoopPredicate) {
821     predString->print_cr("<u range");
822     tty->print("%s", predString->base());
823     predString->~stringStream();
824   }
825   return bol;
826 }
827 
828 // Should loop predication look not only in the path from tail to head
829 // but also in branches of the loop body?
loop_predication_should_follow_branches(IdealLoopTree * loop,ProjNode * predicate_proj,float & loop_trip_cnt)830 bool PhaseIdealLoop::loop_predication_should_follow_branches(IdealLoopTree *loop, ProjNode *predicate_proj, float& loop_trip_cnt) {
831   if (!UseProfiledLoopPredicate) {
832     return false;
833   }
834 
835   if (predicate_proj == NULL) {
836     return false;
837   }
838 
839   LoopNode* head = loop->_head->as_Loop();
840   bool follow_branches = true;
841   IdealLoopTree* l = loop->_child;
842   // For leaf loops and loops with a single inner loop
843   while (l != NULL && follow_branches) {
844     IdealLoopTree* child = l;
845     if (child->_child != NULL &&
846         child->_head->is_OuterStripMinedLoop()) {
847       assert(child->_child->_next == NULL, "only one inner loop for strip mined loop");
848       assert(child->_child->_head->is_CountedLoop() && child->_child->_head->as_CountedLoop()->is_strip_mined(), "inner loop should be strip mined");
849       child = child->_child;
850     }
851     if (child->_child != NULL || child->_irreducible) {
852       follow_branches = false;
853     }
854     l = l->_next;
855   }
856   if (follow_branches) {
857     loop->compute_profile_trip_cnt(this);
858     if (head->is_profile_trip_failed()) {
859       follow_branches = false;
860     } else {
861       loop_trip_cnt = head->profile_trip_cnt();
862       if (head->is_CountedLoop()) {
863         CountedLoopNode* cl = head->as_CountedLoop();
864         if (cl->phi() != NULL) {
865           const TypeInt* t = _igvn.type(cl->phi())->is_int();
866           float worst_case_trip_cnt = ((float)t->_hi - t->_lo) / ABS(cl->stride_con());
867           if (worst_case_trip_cnt < loop_trip_cnt) {
868             loop_trip_cnt = worst_case_trip_cnt;
869           }
870         }
871       }
872     }
873   }
874   return follow_branches;
875 }
876 
877 // Compute probability of reaching some CFG node from a fixed
878 // dominating CFG node
879 class PathFrequency {
880 private:
881   Node* _dom; // frequencies are computed relative to this node
882   Node_Stack _stack;
883   GrowableArray<float> _freqs_stack; // keep track of intermediate result at regions
884   GrowableArray<float> _freqs; // cache frequencies
885   PhaseIdealLoop* _phase;
886 
set_rounding(int mode)887   void set_rounding(int mode) {
888     // fesetround is broken on windows
889     NOT_WINDOWS(fesetround(mode);)
890   }
891 
check_frequency(float f)892   void check_frequency(float f) {
893     NOT_WINDOWS(assert(f <= 1 && f >= 0, "Incorrect frequency");)
894   }
895 
896 public:
PathFrequency(Node * dom,PhaseIdealLoop * phase)897   PathFrequency(Node* dom, PhaseIdealLoop* phase)
898     : _dom(dom), _stack(0), _phase(phase) {
899   }
900 
to(Node * n)901   float to(Node* n) {
902     // post order walk on the CFG graph from n to _dom
903     set_rounding(FE_TOWARDZERO); // make sure rounding doesn't push frequency above 1
904     IdealLoopTree* loop = _phase->get_loop(_dom);
905     Node* c = n;
906     for (;;) {
907       assert(_phase->get_loop(c) == loop, "have to be in the same loop");
908       if (c == _dom || _freqs.at_grow(c->_idx, -1) >= 0) {
909         float f = c == _dom ? 1 : _freqs.at(c->_idx);
910         Node* prev = c;
911         while (_stack.size() > 0 && prev == c) {
912           Node* n = _stack.node();
913           if (!n->is_Region()) {
914             if (_phase->get_loop(n) != _phase->get_loop(n->in(0))) {
915               // Found an inner loop: compute frequency of reaching this
916               // exit from the loop head by looking at the number of
917               // times each loop exit was taken
918               IdealLoopTree* inner_loop = _phase->get_loop(n->in(0));
919               LoopNode* inner_head = inner_loop->_head->as_Loop();
920               assert(_phase->get_loop(n) == loop, "only 1 inner loop");
921               if (inner_head->is_OuterStripMinedLoop()) {
922                 inner_head->verify_strip_mined(1);
923                 if (n->in(0) == inner_head->in(LoopNode::LoopBackControl)->in(0)) {
924                   n = n->in(0)->in(0)->in(0);
925                 }
926                 inner_loop = inner_loop->_child;
927                 inner_head = inner_loop->_head->as_Loop();
928                 inner_head->verify_strip_mined(1);
929               }
930               set_rounding(FE_UPWARD);  // make sure rounding doesn't push frequency above 1
931               float loop_exit_cnt = 0.0f;
932               for (uint i = 0; i < inner_loop->_body.size(); i++) {
933                 Node *n = inner_loop->_body[i];
934                 float c = inner_loop->compute_profile_trip_cnt_helper(n);
935                 loop_exit_cnt += c;
936               }
937               set_rounding(FE_TOWARDZERO);
938               float cnt = -1;
939               if (n->in(0)->is_If()) {
940                 IfNode* iff = n->in(0)->as_If();
941                 float p = n->in(0)->as_If()->_prob;
942                 if (n->Opcode() == Op_IfFalse) {
943                   p = 1 - p;
944                 }
945                 if (p > PROB_MIN) {
946                   cnt = p * iff->_fcnt;
947                 } else {
948                   cnt = 0;
949                 }
950               } else {
951                 assert(n->in(0)->is_Jump(), "unsupported node kind");
952                 JumpNode* jmp = n->in(0)->as_Jump();
953                 float p = n->in(0)->as_Jump()->_probs[n->as_JumpProj()->_con];
954                 cnt = p * jmp->_fcnt;
955               }
956               float this_exit_f = cnt > 0 ? cnt / loop_exit_cnt : 0;
957               check_frequency(this_exit_f);
958               f = f * this_exit_f;
959               check_frequency(f);
960             } else {
961               float p = -1;
962               if (n->in(0)->is_If()) {
963                 p = n->in(0)->as_If()->_prob;
964                 if (n->Opcode() == Op_IfFalse) {
965                   p = 1 - p;
966                 }
967               } else {
968                 assert(n->in(0)->is_Jump(), "unsupported node kind");
969                 p = n->in(0)->as_Jump()->_probs[n->as_JumpProj()->_con];
970               }
971               f = f * p;
972               check_frequency(f);
973             }
974             _freqs.at_put_grow(n->_idx, (float)f, -1);
975             _stack.pop();
976           } else {
977             float prev_f = _freqs_stack.pop();
978             float new_f = f;
979             f = new_f + prev_f;
980             check_frequency(f);
981             uint i = _stack.index();
982             if (i < n->req()) {
983               c = n->in(i);
984               _stack.set_index(i+1);
985               _freqs_stack.push(f);
986             } else {
987               _freqs.at_put_grow(n->_idx, f, -1);
988               _stack.pop();
989             }
990           }
991         }
992         if (_stack.size() == 0) {
993           set_rounding(FE_TONEAREST);
994           check_frequency(f);
995           return f;
996         }
997       } else if (c->is_Loop()) {
998         ShouldNotReachHere();
999         c = c->in(LoopNode::EntryControl);
1000       } else if (c->is_Region()) {
1001         _freqs_stack.push(0);
1002         _stack.push(c, 2);
1003         c = c->in(1);
1004       } else {
1005         if (c->is_IfProj()) {
1006           IfNode* iff = c->in(0)->as_If();
1007           if (iff->_prob == PROB_UNKNOWN) {
1008             // assume never taken
1009             _freqs.at_put_grow(c->_idx, 0, -1);
1010           } else if (_phase->get_loop(c) != _phase->get_loop(iff)) {
1011             if (iff->_fcnt == COUNT_UNKNOWN) {
1012               // assume never taken
1013               _freqs.at_put_grow(c->_idx, 0, -1);
1014             } else {
1015               // skip over loop
1016               _stack.push(c, 1);
1017               c = _phase->get_loop(c->in(0))->_head->as_Loop()->skip_strip_mined()->in(LoopNode::EntryControl);
1018             }
1019           } else {
1020             _stack.push(c, 1);
1021             c = iff;
1022           }
1023         } else if (c->is_JumpProj()) {
1024           JumpNode* jmp = c->in(0)->as_Jump();
1025           if (_phase->get_loop(c) != _phase->get_loop(jmp)) {
1026             if (jmp->_fcnt == COUNT_UNKNOWN) {
1027               // assume never taken
1028               _freqs.at_put_grow(c->_idx, 0, -1);
1029             } else {
1030               // skip over loop
1031               _stack.push(c, 1);
1032               c = _phase->get_loop(c->in(0))->_head->as_Loop()->skip_strip_mined()->in(LoopNode::EntryControl);
1033             }
1034           } else {
1035             _stack.push(c, 1);
1036             c = jmp;
1037           }
1038         } else if (c->Opcode() == Op_CatchProj &&
1039                    c->in(0)->Opcode() == Op_Catch &&
1040                    c->in(0)->in(0)->is_Proj() &&
1041                    c->in(0)->in(0)->in(0)->is_Call()) {
1042           // assume exceptions are never thrown
1043           uint con = c->as_Proj()->_con;
1044           if (con == CatchProjNode::fall_through_index) {
1045             Node* call = c->in(0)->in(0)->in(0)->in(0);
1046             if (_phase->get_loop(call) != _phase->get_loop(c)) {
1047               _freqs.at_put_grow(c->_idx, 0, -1);
1048             } else {
1049               c = call;
1050             }
1051           } else {
1052             assert(con >= CatchProjNode::catch_all_index, "what else?");
1053             _freqs.at_put_grow(c->_idx, 0, -1);
1054           }
1055         } else if (c->unique_ctrl_out() == NULL && !c->is_If() && !c->is_Jump()) {
1056           ShouldNotReachHere();
1057         } else {
1058           c = c->in(0);
1059         }
1060       }
1061     }
1062     ShouldNotReachHere();
1063     return -1;
1064   }
1065 };
1066 
loop_predication_follow_branches(Node * n,IdealLoopTree * loop,float loop_trip_cnt,PathFrequency & pf,Node_Stack & stack,VectorSet & seen,Node_List & if_proj_list)1067 void PhaseIdealLoop::loop_predication_follow_branches(Node *n, IdealLoopTree *loop, float loop_trip_cnt,
1068                                                       PathFrequency& pf, Node_Stack& stack, VectorSet& seen,
1069                                                       Node_List& if_proj_list) {
1070   assert(n->is_Region(), "start from a region");
1071   Node* tail = loop->tail();
1072   stack.push(n, 1);
1073   do {
1074     Node* c = stack.node();
1075     assert(c->is_Region() || c->is_IfProj(), "only region here");
1076     uint i = stack.index();
1077 
1078     if (i < c->req()) {
1079       stack.set_index(i+1);
1080       Node* in = c->in(i);
1081       while (!is_dominator(in, tail) && !seen.test_set(in->_idx)) {
1082         IdealLoopTree* in_loop = get_loop(in);
1083         if (in_loop != loop) {
1084           in = in_loop->_head->in(LoopNode::EntryControl);
1085         } else if (in->is_Region()) {
1086           stack.push(in, 1);
1087           break;
1088         } else if (in->is_IfProj() &&
1089                    in->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) &&
1090                    (in->in(0)->Opcode() == Op_If ||
1091                     in->in(0)->Opcode() == Op_RangeCheck)) {
1092           if (pf.to(in) * loop_trip_cnt >= 1) {
1093             stack.push(in, 1);
1094           }
1095           in = in->in(0);
1096         } else {
1097           in = in->in(0);
1098         }
1099       }
1100     } else {
1101       if (c->is_IfProj()) {
1102         if_proj_list.push(c);
1103       }
1104       stack.pop();
1105     }
1106 
1107   } while (stack.size() > 0);
1108 }
1109 
1110 
loop_predication_impl_helper(IdealLoopTree * loop,ProjNode * proj,ProjNode * predicate_proj,CountedLoopNode * cl,ConNode * zero,Invariance & invar,Deoptimization::DeoptReason reason)1111 bool PhaseIdealLoop::loop_predication_impl_helper(IdealLoopTree *loop, ProjNode* proj, ProjNode *predicate_proj,
1112                                                   CountedLoopNode *cl, ConNode* zero, Invariance& invar,
1113                                                   Deoptimization::DeoptReason reason) {
1114   // Following are changed to nonnull when a predicate can be hoisted
1115   ProjNode* new_predicate_proj = NULL;
1116   IfNode*   iff  = proj->in(0)->as_If();
1117   Node*     test = iff->in(1);
1118   if (!test->is_Bool()){ //Conv2B, ...
1119     return false;
1120   }
1121   BoolNode* bol = test->as_Bool();
1122   if (invar.is_invariant(bol)) {
1123     // Invariant test
1124     new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL,
1125                                                      reason,
1126                                                      iff->Opcode());
1127     Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0);
1128     BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool();
1129 
1130     // Negate test if necessary
1131     bool negated = false;
1132     if (proj->_con != predicate_proj->_con) {
1133       new_predicate_bol = new BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate());
1134       register_new_node(new_predicate_bol, ctrl);
1135       negated = true;
1136     }
1137     IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If();
1138     _igvn.hash_delete(new_predicate_iff);
1139     new_predicate_iff->set_req(1, new_predicate_bol);
1140 #ifndef PRODUCT
1141     if (TraceLoopPredicate) {
1142       tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx);
1143       loop->dump_head();
1144     } else if (TraceLoopOpts) {
1145       tty->print("Predicate IC ");
1146       loop->dump_head();
1147     }
1148 #endif
1149   } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
1150     // Range check for counted loops
1151     const Node*    cmp    = bol->in(1)->as_Cmp();
1152     Node*          idx    = cmp->in(1);
1153     assert(!invar.is_invariant(idx), "index is variant");
1154     Node* rng = cmp->in(2);
1155     assert(rng->Opcode() == Op_LoadRange || iff->is_RangeCheck() || _igvn.type(rng)->is_int()->_lo >= 0, "must be");
1156     assert(invar.is_invariant(rng), "range must be invariant");
1157     int scale    = 1;
1158     Node* offset = zero;
1159     bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset);
1160     assert(ok, "must be index expression");
1161 
1162     Node* init    = cl->init_trip();
1163     // Limit is not exact.
1164     // Calculate exact limit here.
1165     // Note, counted loop's test is '<' or '>'.
1166     Node* limit   = exact_limit(loop);
1167     int  stride   = cl->stride()->get_int();
1168 
1169     // Build if's for the upper and lower bound tests.  The
1170     // lower_bound test will dominate the upper bound test and all
1171     // cloned or created nodes will use the lower bound test as
1172     // their declared control.
1173 
1174     // Perform cloning to keep Invariance state correct since the
1175     // late schedule will place invariant things in the loop.
1176     Node *ctrl = predicate_proj->in(0)->as_If()->in(0);
1177     rng = invar.clone(rng, ctrl);
1178     if (offset && offset != zero) {
1179       assert(invar.is_invariant(offset), "offset must be loop invariant");
1180       offset = invar.clone(offset, ctrl);
1181     }
1182     // If predicate expressions may overflow in the integer range, longs are used.
1183     bool overflow = false;
1184 
1185     // Test the lower bound
1186     BoolNode* lower_bound_bol = rc_predicate(loop, ctrl, scale, offset, init, limit, stride, rng, false, overflow);
1187     // Negate test if necessary
1188     bool negated = false;
1189     if (proj->_con != predicate_proj->_con) {
1190       lower_bound_bol = new BoolNode(lower_bound_bol->in(1), lower_bound_bol->_test.negate());
1191       register_new_node(lower_bound_bol, ctrl);
1192       negated = true;
1193     }
1194     ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, reason, overflow ? Op_If : iff->Opcode());
1195     IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If();
1196     _igvn.hash_delete(lower_bound_iff);
1197     lower_bound_iff->set_req(1, lower_bound_bol);
1198     if (TraceLoopPredicate) tty->print_cr("lower bound check if: %s %d ", negated ? " negated" : "", lower_bound_iff->_idx);
1199 
1200     // Test the upper bound
1201     BoolNode* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true, overflow);
1202     negated = false;
1203     if (proj->_con != predicate_proj->_con) {
1204       upper_bound_bol = new BoolNode(upper_bound_bol->in(1), upper_bound_bol->_test.negate());
1205       register_new_node(upper_bound_bol, ctrl);
1206       negated = true;
1207     }
1208     ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, reason, overflow ? Op_If : iff->Opcode());
1209     assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
1210     IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If();
1211     _igvn.hash_delete(upper_bound_iff);
1212     upper_bound_iff->set_req(1, upper_bound_bol);
1213     if (TraceLoopPredicate) tty->print_cr("upper bound check if: %s %d ", negated ? " negated" : "", lower_bound_iff->_idx);
1214 
1215     // Fall through into rest of the clean up code which will move
1216     // any dependent nodes onto the upper bound test.
1217     new_predicate_proj = upper_bound_proj;
1218 
1219     if (iff->is_RangeCheck()) {
1220       new_predicate_proj = insert_initial_skeleton_predicate(iff, loop, proj, predicate_proj, upper_bound_proj, scale, offset, init, limit, stride, rng, overflow, reason);
1221     }
1222 
1223 #ifndef PRODUCT
1224     if (TraceLoopOpts && !TraceLoopPredicate) {
1225       tty->print("Predicate RC ");
1226       loop->dump_head();
1227     }
1228 #endif
1229   } else {
1230     // Loop variant check (for example, range check in non-counted loop)
1231     // with uncommon trap.
1232     return false;
1233   }
1234   assert(new_predicate_proj != NULL, "sanity");
1235   // Success - attach condition (new_predicate_bol) to predicate if
1236   invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate
1237 
1238   // Eliminate the old If in the loop body
1239   dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con );
1240 
1241   C->set_major_progress();
1242   return true;
1243 }
1244 
1245 
1246 // After pre/main/post loops are created, we'll put a copy of some
1247 // range checks between the pre and main loop to validate the value
1248 // of the main loop induction variable. Make a copy of the predicates
1249 // here with an opaque node as a place holder for the value (will be
1250 // updated by PhaseIdealLoop::clone_skeleton_predicate()).
insert_initial_skeleton_predicate(IfNode * iff,IdealLoopTree * loop,ProjNode * proj,ProjNode * predicate_proj,ProjNode * upper_bound_proj,int scale,Node * offset,Node * init,Node * limit,jint stride,Node * rng,bool & overflow,Deoptimization::DeoptReason reason)1251 ProjNode* PhaseIdealLoop::insert_initial_skeleton_predicate(IfNode* iff, IdealLoopTree *loop,
1252                                                             ProjNode* proj, ProjNode *predicate_proj,
1253                                                             ProjNode* upper_bound_proj,
1254                                                             int scale, Node* offset,
1255                                                             Node* init, Node* limit, jint stride,
1256                                                             Node* rng, bool &overflow,
1257                                                             Deoptimization::DeoptReason reason) {
1258   // First predicate for the initial value on first loop iteration
1259   assert(proj->_con && predicate_proj->_con, "not a range check?");
1260   Node* opaque_init = new OpaqueLoopInitNode(C, init);
1261   register_new_node(opaque_init, upper_bound_proj);
1262   BoolNode* bol = rc_predicate(loop, upper_bound_proj, scale, offset, opaque_init, limit, stride, rng, (stride > 0) != (scale > 0), overflow);
1263   Node* opaque_bol = new Opaque4Node(C, bol, _igvn.intcon(1)); // This will go away once loop opts are over
1264   register_new_node(opaque_bol, upper_bound_proj);
1265   ProjNode* new_proj = create_new_if_for_predicate(predicate_proj, NULL, reason, overflow ? Op_If : iff->Opcode());
1266   _igvn.replace_input_of(new_proj->in(0), 1, opaque_bol);
1267   assert(opaque_init->outcnt() > 0, "should be used");
1268 
1269   // Second predicate for init + (current stride - initial stride)
1270   // This is identical to the previous predicate initially but as
1271   // unrolling proceeds current stride is updated.
1272   Node* init_stride = loop->_head->as_CountedLoop()->stride();
1273   Node* opaque_stride = new OpaqueLoopStrideNode(C, init_stride);
1274   register_new_node(opaque_stride, new_proj);
1275   Node* max_value = new SubINode(opaque_stride, init_stride);
1276   register_new_node(max_value, new_proj);
1277   max_value = new AddINode(opaque_init, max_value);
1278   register_new_node(max_value, new_proj);
1279   bol = rc_predicate(loop, new_proj, scale, offset, max_value, limit, stride, rng, (stride > 0) != (scale > 0), overflow);
1280   opaque_bol = new Opaque4Node(C, bol, _igvn.intcon(1));
1281   register_new_node(opaque_bol, new_proj);
1282   new_proj = create_new_if_for_predicate(predicate_proj, NULL, reason, overflow ? Op_If : iff->Opcode());
1283   _igvn.replace_input_of(new_proj->in(0), 1, opaque_bol);
1284   assert(max_value->outcnt() > 0, "should be used");
1285 
1286   return new_proj;
1287 }
1288 
1289 //------------------------------ loop_predication_impl--------------------------
1290 // Insert loop predicates for null checks and range checks
loop_predication_impl(IdealLoopTree * loop)1291 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
1292   if (!UseLoopPredicate) return false;
1293 
1294   if (!loop->_head->is_Loop()) {
1295     // Could be a simple region when irreducible loops are present.
1296     return false;
1297   }
1298   LoopNode* head = loop->_head->as_Loop();
1299 
1300   if (head->unique_ctrl_out()->Opcode() == Op_NeverBranch) {
1301     // do nothing for infinite loops
1302     return false;
1303   }
1304 
1305   if (head->is_OuterStripMinedLoop()) {
1306     return false;
1307   }
1308 
1309   CountedLoopNode *cl = NULL;
1310   if (head->is_valid_counted_loop()) {
1311     cl = head->as_CountedLoop();
1312     // do nothing for iteration-splitted loops
1313     if (!cl->is_normal_loop()) return false;
1314     // Avoid RCE if Counted loop's test is '!='.
1315     BoolTest::mask bt = cl->loopexit()->test_trip();
1316     if (bt != BoolTest::lt && bt != BoolTest::gt)
1317       cl = NULL;
1318   }
1319 
1320   Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
1321   ProjNode *loop_limit_proj = NULL;
1322   ProjNode *predicate_proj = NULL;
1323   ProjNode *profile_predicate_proj = NULL;
1324   // Loop limit check predicate should be near the loop.
1325   loop_limit_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
1326   if (loop_limit_proj != NULL) {
1327     entry = skip_loop_predicates(loop_limit_proj);
1328   }
1329   bool has_profile_predicates = false;
1330   profile_predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate);
1331   if (profile_predicate_proj != NULL) {
1332     Node* n = skip_loop_predicates(entry);
1333     // Check if predicates were already added to the profile predicate
1334     // block
1335     if (n != entry->in(0)->in(0) || n->outcnt() != 1) {
1336       has_profile_predicates = true;
1337     }
1338     entry = n;
1339   }
1340   predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
1341 
1342   float loop_trip_cnt = -1;
1343   bool follow_branches = loop_predication_should_follow_branches(loop, profile_predicate_proj, loop_trip_cnt);
1344   assert(!follow_branches || loop_trip_cnt >= 0, "negative trip count?");
1345 
1346   if (predicate_proj == NULL && !follow_branches) {
1347 #ifndef PRODUCT
1348     if (TraceLoopPredicate) {
1349       tty->print("missing predicate:");
1350       loop->dump_head();
1351       head->dump(1);
1352     }
1353 #endif
1354     return false;
1355   }
1356   ConNode* zero = _igvn.intcon(0);
1357   set_ctrl(zero, C->root());
1358 
1359   ResourceArea *area = Thread::current()->resource_area();
1360   Invariance invar(area, loop);
1361 
1362   // Create list of if-projs such that a newer proj dominates all older
1363   // projs in the list, and they all dominate loop->tail()
1364   Node_List if_proj_list(area);
1365   Node_List regions(area);
1366   Node *current_proj = loop->tail(); //start from tail
1367 
1368 
1369   Node_List controls(area);
1370   while (current_proj != head) {
1371     if (loop == get_loop(current_proj) && // still in the loop ?
1372         current_proj->is_Proj()        && // is a projection  ?
1373         (current_proj->in(0)->Opcode() == Op_If ||
1374          current_proj->in(0)->Opcode() == Op_RangeCheck)) { // is a if projection ?
1375       if_proj_list.push(current_proj);
1376     }
1377     if (follow_branches &&
1378         current_proj->Opcode() == Op_Region &&
1379         loop == get_loop(current_proj)) {
1380       regions.push(current_proj);
1381     }
1382     current_proj = idom(current_proj);
1383   }
1384 
1385   bool hoisted = false; // true if at least one proj is promoted
1386 
1387   if (!has_profile_predicates) {
1388     while (if_proj_list.size() > 0) {
1389       Node* n = if_proj_list.pop();
1390 
1391       ProjNode* proj = n->as_Proj();
1392       IfNode*   iff  = proj->in(0)->as_If();
1393 
1394       CallStaticJavaNode* call = proj->is_uncommon_trap_if_pattern(Deoptimization::Reason_none);
1395       if (call == NULL) {
1396         if (loop->is_loop_exit(iff)) {
1397           // stop processing the remaining projs in the list because the execution of them
1398           // depends on the condition of "iff" (iff->in(1)).
1399           break;
1400         } else {
1401           // Both arms are inside the loop. There are two cases:
1402           // (1) there is one backward branch. In this case, any remaining proj
1403           //     in the if_proj list post-dominates "iff". So, the condition of "iff"
1404           //     does not determine the execution the remining projs directly, and we
1405           //     can safely continue.
1406           // (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj"
1407           //     does not dominate loop->tail(), so it can not be in the if_proj list.
1408           continue;
1409         }
1410       }
1411       Deoptimization::DeoptReason reason = Deoptimization::trap_request_reason(call->uncommon_trap_request());
1412       if (reason == Deoptimization::Reason_predicate) {
1413         break;
1414       }
1415 
1416       if (predicate_proj != NULL) {
1417         hoisted = loop_predication_impl_helper(loop, proj, predicate_proj, cl, zero, invar, Deoptimization::Reason_predicate) | hoisted;
1418       }
1419     } // end while
1420   }
1421 
1422   if (follow_branches) {
1423     PathFrequency pf(loop->_head, this);
1424 
1425     // Some projections were skipped by regular predicates because of
1426     // an early loop exit. Try them with profile data.
1427     while (if_proj_list.size() > 0) {
1428       Node* proj = if_proj_list.pop();
1429       float f = pf.to(proj);
1430       if (proj->as_Proj()->is_uncommon_trap_if_pattern(Deoptimization::Reason_none) &&
1431           f * loop_trip_cnt >= 1) {
1432         hoisted = loop_predication_impl_helper(loop, proj->as_Proj(), profile_predicate_proj, cl, zero, invar, Deoptimization::Reason_profile_predicate) | hoisted;
1433       }
1434     }
1435 
1436     // And look into all branches
1437     Node_Stack stack(0);
1438     VectorSet seen(Thread::current()->resource_area());
1439     Node_List if_proj_list_freq(area);
1440     while (regions.size() > 0) {
1441       Node* c = regions.pop();
1442       loop_predication_follow_branches(c, loop, loop_trip_cnt, pf, stack, seen, if_proj_list_freq);
1443     }
1444 
1445     for (uint i = 0; i < if_proj_list_freq.size(); i++) {
1446       ProjNode* proj = if_proj_list_freq.at(i)->as_Proj();
1447       hoisted = loop_predication_impl_helper(loop, proj, profile_predicate_proj, cl, zero, invar, Deoptimization::Reason_profile_predicate) | hoisted;
1448     }
1449   }
1450 
1451 #ifndef PRODUCT
1452   // report that the loop predication has been actually performed
1453   // for this loop
1454   if (TraceLoopPredicate && hoisted) {
1455     tty->print("Loop Predication Performed:");
1456     loop->dump_head();
1457   }
1458 #endif
1459 
1460   head->verify_strip_mined(1);
1461 
1462   return hoisted;
1463 }
1464 
1465 //------------------------------loop_predication--------------------------------
1466 // driver routine for loop predication optimization
loop_predication(PhaseIdealLoop * phase)1467 bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) {
1468   bool hoisted = false;
1469   // Recursively promote predicates
1470   if (_child) {
1471     hoisted = _child->loop_predication( phase);
1472   }
1473 
1474   // self
1475   if (!_irreducible && !tail()->is_top()) {
1476     hoisted |= phase->loop_predication_impl(this);
1477   }
1478 
1479   if (_next) { //sibling
1480     hoisted |= _next->loop_predication( phase);
1481   }
1482 
1483   return hoisted;
1484 }
1485