/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "opto/loopnode.hpp"
#include "opto/addnode.hpp"
#include "opto/callnode.hpp"
#include "opto/connode.hpp"
#include "opto/loopnode.hpp"
#include "opto/mulnode.hpp"
#include "opto/rootnode.hpp"
#include "opto/subnode.hpp"
/*
* The general idea of Loop Predication is to insert a predicate on the entry
* path to a loop, and raise a uncommon trap if the check of the condition fails.
* The condition checks are promoted from inside the loop body, and thus
* the checks inside the loop could be eliminated. Currently, loop predication
* optimization has been applied to remove array range check and loop invariant
* checks (such as null checks).
*/
//-------------------------------is_uncommon_trap_proj----------------------------
// Return true if proj is the form of "proj->[region->..]call_uct"
return false;
if (out->is_CallStaticJava()) {
if (req != 0) {
return true;
}
}
return false; // don't do further after call
}
return false;
}
return false;
}
//-------------------------------is_uncommon_trap_if_pattern-------------------------
// Return true for "if(test)-> proj -> ...
// |
// V
// other_proj->[region->..]call_uct"
//
// "must_reason_predicate" means the uct reason must be Reason_predicate
bool PhaseIdealLoop::is_uncommon_trap_if_pattern(ProjNode *proj, Deoptimization::DeoptReason reason) {
// Variation of a dead If node.
// we need "If(Conv2B(Opaque1(...)))" pattern for reason_predicate
return false;
}
}
return true;
}
return false;
}
//-------------------------------register_control-------------------------
// When called from beautify_loops() idom is not constructed yet.
}
}
//------------------------------create_new_if_for_predicate------------------------
// create a new if above the uct_if_pattern for the predicate to be promoted.
//
// before after
// ---------- ----------
// ctrl ctrl
// | |
// | |
// v v
// iff new_iff
// / \ / \
// / \ / \
// v v v v
// uncommon_proj cont_proj if_uct if_cont
// \ | | | |
// \ | | | |
// v v v | v
// rgn loop | iff
// | | / \
// | | / \
// v | v v
// uncommon_trap | uncommon_proj cont_proj
// \ \ | |
// \ \ | |
// v v v v
// rgn loop
// |
// |
// v
// uncommon_trap
//
//
// We will create a region to guard the uct call if there is no one there.
// The true projecttion (if_cont) of the new_iff is returned.
// This code is also used to clone predicates to clonned loops.
// When called from beautify_loops() idom is not constructed yet.
}
} else {
// Find region's edge corresponding to uncommon_proj
}
// Clonning the predicate to new location.
}
// Create new_iff
if (cont_proj->is_IfFalse()) {
// Swap
}
// if_uct to rgn
// When called from beautify_loops() idom is not constructed yet.
}
// If rgn has phis add new edges which has the same
// value as on original uncommon_proj pass.
bool has_phi = false;
has_phi = true;
}
}
// Attach if_cont to iff
}
}
}
//------------------------------create_new_if_for_predicate------------------------
// Create a new if below new_entry for the predicate to be cloned (IGVN optimization)
assert(PhaseIdealLoop::is_uncommon_trap_if_pattern(cont_proj, reason), "must be a uct if pattern!");
} else {
// Find region's edge corresponding to uncommon_proj
}
// Create new_iff in new location.
if (cont_proj->is_IfFalse()) {
// Swap
}
// if_uct to rgn
// If rgn has phis add corresponding new edges which has the same
// value as on original uncommon_proj pass.
bool has_phi = false;
has_phi = true;
}
}
}
//--------------------------clone_predicate-----------------------
PhaseIterGVN* igvn) {
if (loop_phase != NULL) {
} else {
}
// Match original condition since predicate's projections could be swapped.
if (loop_phase != NULL) {
} else {
}
return new_predicate_proj;
}
//--------------------------clone_loop_predicates-----------------------
// Interface from IGVN
Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) {
}
// Interface from PhaseIdealLoop
Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check) {
}
// Clone loop predicates to cloned loops (peeled, unswitched, split_if).
bool clone_limit_check,
PhaseIterGVN* igvn) {
#ifdef ASSERT
if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) {
assert(false, "not IfTrue, IfFalse, Region or SafePoint");
}
#endif
// Search original predicates
if (LoopLimitCheck) {
if (limit_check_proj != NULL) {
}
}
if (UseLoopPredicate) {
// clone predicate
loop_phase, igvn);
if (TraceLoopPredicate) {
}
}
}
// Clone loop limit check last to insert it before loop.
// Don't clone a limit check which was already finalized
// for this counted loop (only one limit check is needed).
loop_phase, igvn);
if (TraceLoopLimitCheck) {
}
}
return new_entry;
}
//--------------------------skip_loop_predicates------------------------------
// Skip related predicates.
if (LoopLimitCheck) {
}
}
if (UseLoopPredicate) {
break;
}
}
}
return entry;
}
//--------------------------find_predicate_insertion_point-------------------
// Find a good location to insert a predicate
ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) {
return NULL;
}
return NULL;
}
//--------------------------find_predicate------------------------------------
// Find a predicate
if (LoopLimitCheck) {
return entry;
}
}
if (UseLoopPredicate) {
return entry;
}
}
return NULL;
}
//------------------------------Invariance-----------------------------------
// Helper class for loop_predication_impl to compute invariance on the fly and
// clone invariants.
// Helper function to set up the invariance for invariance computation
// If n is a known invariant, set up directly. Otherwise, look up the
// the possibility to push n onto the stack for further processing.
} else if (!n->is_CFG()) {
}
}
}
// Compute invariance for "the_node" and (possibly) all its inputs recursively
// on the fly
visit(n, n);
while (_stack.is_nonempty()) {
// n is invariant if it's inputs are all invariant
bool all_inputs_invariant = true;
all_inputs_invariant = false;
break;
}
}
if (all_inputs_invariant) {
}
} else { // process next input
visit(n, m);
}
}
}
}
// Helper function to set up _old_new map for clone_nodes.
// If n is a known invariant, set up directly ("clone" of n == n).
// Otherwise, push n onto the stack for real cloning.
} else { // to be cloned
}
}
// Clone "n" and (possibly) all its inputs recursively
clone_visit(n);
while (_stack.is_nonempty()) {
// clone invariant node
}
} else { // process next input
clone_visit(m); // visit the input
}
}
}
}
public:
{}
// Map old to n for invariance computation and clone
}
// Driver function to compute invariance
}
// Driver function to clone invariant
clone_nodes(n, ctrl);
}
};
//------------------------------is_range_check_if -----------------------------------
// Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
// Note: this function is particularly designed for loop predication. We require load_range
// and offset to be loop invariant computed on the fly by "invar"
bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
if (!is_loop_exit(iff)) {
return false;
}
return false;
}
return false;
}
return false;
}
return false;
}
// Allow predication on positive values that aren't LoadRanges.
// This allows optimization of loops where the length of the
// array is a known value and doesn't need to be loaded back
// from the array.
return false;
}
}
return false;
}
int scale = 0;
return false;
}
return false;
}
return true;
}
//------------------------------rc_predicate-----------------------------------
// Create a range check predicate
//
// for (i = init; i < limit; i += stride) {
// a[scale*i+offset]
// }
//
// Compute max(scale*i + offset) for init <= i < limit and build the predicate
// as "max(scale*i + offset) u< a.length".
//
// There are two cases for max(scale*i + offset):
// (1) stride*scale > 0
// max(scale*i + offset) = scale*(limit-stride) + offset
// (2) stride*scale < 0
// max(scale*i + offset) = scale*init + offset
if (TraceLoopPredicate) {
predString = new stringStream();
}
if (LoopLimitCheck) {
// With LoopLimitCheck limit is not exact.
// Calculate exact limit here.
// Note, counted loop's test is '<' or '>'.
} else {
}
} else {
}
if (scale != 1) {
}
if (TraceLoopPredicate)
}
if (TraceLoopPredicate) {
}
return bol;
}
//------------------------------ loop_predication_impl--------------------------
// Insert loop predicates for null checks and range checks
if (!UseLoopPredicate) return false;
// Could be a simple region when irreducible loops are present.
return false;
}
// do nothing for infinite loops
return false;
}
if (head->is_valid_counted_loop()) {
// do nothing for iteration-splitted loops
if (!cl->is_normal_loop()) return false;
// Avoid RCE if Counted loop's test is '!='.
}
// Loop limit check predicate should be near the loop.
if (LoopLimitCheck) {
if (predicate_proj != NULL)
}
if (!predicate_proj) {
#ifndef PRODUCT
if (TraceLoopPredicate) {
}
#endif
return false;
}
// Create list of if-projs such that a newer proj dominates all older
// projs in the list, and they all dominate loop->tail()
while (current_proj != head) {
}
}
while (if_proj_list.size() > 0) {
// Following are changed to nonnull when a predicate can be hoisted
// stop processing the remaining projs in the list because the execution of them
// depends on the condition of "iff" (iff->in(1)).
break;
} else {
// Both arms are inside the loop. There are two cases:
// (1) there is one backward branch. In this case, any remaining proj
// in the if_proj list post-dominates "iff". So, the condition of "iff"
// does not determine the execution the remining projs directly, and we
// can safely continue.
// (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj"
// does not dominate loop->tail(), so it can not be in the if_proj list.
continue;
}
}
continue;
}
// Invariant test
// Negate test if necessary
bool negated = false;
negated = true;
}
#ifndef PRODUCT
if (TraceLoopPredicate) {
} else if (TraceLoopOpts) {
}
#endif
// Range check for counted loops
// Build if's for the upper and lower bound tests. The
// lower_bound test will dominate the upper bound test and all
// cloned or created nodes will use the lower bound test as
// their declared control.
ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
// Perform cloning to keep Invariance state correct since the
// late schedule will place invariant things in the loop.
}
// Test the lower bound
// Test the upper bound
Node* upper_bound_bol = rc_predicate(loop, lower_bound_proj, scale, offset, init, limit, stride, rng, true);
// Fall through into rest of the clean up code which will move
// any dependent nodes onto the upper bound test.
#ifndef PRODUCT
if (TraceLoopOpts && !TraceLoopPredicate) {
}
#endif
} else {
// Loop variant check (for example, range check in non-counted loop)
// with uncommon trap.
continue;
}
// Success - attach condition (new_predicate_bol) to predicate if
// Eliminate the old If in the loop body
hoisted = true;
C->set_major_progress();
} // end while
#ifndef PRODUCT
// report that the loop predication has been actually performed
// for this loop
if (TraceLoopPredicate && hoisted) {
}
#endif
return hoisted;
}
//------------------------------loop_predication--------------------------------
// driver routine for loop predication optimization
bool hoisted = false;
// Recursively promote predicates
if (_child) {
}
// self
}
if (_next) { //sibling
}
return hoisted;
}