/* * Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * 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. *
*/
//================= Loop Unswitching ===================== // // orig: transformed: // if (invariant-test) then // predicate predicate // loop loop // stmt1 stmt1 // if (invariant-test) then stmt2 // stmt2 stmt4 // else endloop // stmt3 else // endif predicate [clone] // stmt4 loop [clone] // endloop stmt1 [clone] // stmt3 // stmt4 [clone] // endloop // endif // // Note: the "else" clause may be empty
//------------------------------policy_unswitching----------------------------- // Return TRUE or FALSE if the loop should be unswitched // (ie. clone loop with an invariant test that does not exit the loop) bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { if (!LoopUnswitching) { returnfalse;
} if (!_head->is_Loop()) { returnfalse;
}
// If nodes are depleted, some transform has miscalculated its needs.
assert(!phase->exceeding_node_budget(), "sanity");
// check for vectorized loops, any unswitching was already applied if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) { returnfalse;
}
LoopNode* head = _head->as_Loop(); if (head->unswitch_count() + 1 > head->unswitch_max()) { returnfalse;
} if (phase->find_unswitching_candidate(this) == NULL) { returnfalse;
}
// Too speculative if running low on nodes. return phase->may_require_nodes(est_loop_clone_sz(2));
}
// Find first invariant test that doesn't exit the loop
LoopNode *head = loop->_head->as_Loop();
IfNode* unswitch_iff = NULL;
Node* n = head->in(LoopNode::LoopBackControl); while (n != head) {
Node* n_dom = idom(n); if (n->is_Region()) { if (n_dom->is_If()) {
IfNode* iff = n_dom->as_If(); if (iff->in(1)->is_Bool()) {
BoolNode* bol = iff->in(1)->as_Bool(); if (bol->in(1)->is_Cmp()) { // If condition is invariant and not a loop exit, // then found reason to unswitch. if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
unswitch_iff = iff;
}
}
}
}
}
n = n_dom;
} return unswitch_iff;
}
//------------------------------do_unswitching----------------------------- // Clone loop with an invariant test (that does not exit) and // insert a clone of the test that selects which version to // execute. void PhaseIdealLoop::do_unswitching(IdealLoopTree *loop, Node_List &old_new) {
LoopNode *head = loop->_head->as_Loop();
Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); if (find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check) != NULL
|| (UseProfiledLoopPredicate && find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate) != NULL)
|| (UseLoopPredicate && find_predicate_insertion_point(entry, Deoptimization::Reason_predicate) != NULL)) {
assert(entry->is_IfProj(), "sanity - must be ifProj since there is at least one predicate"); if (entry->outcnt() > 1) { // Bailout if there are loop predicates from which there are additional control dependencies (i.e. from // loop entry 'entry') to previously partially peeled statements since this case is not handled and can lead // to wrong execution. Remove this bailout, once this is fixed. return;
}
} // Find first invariant test that doesn't exit the loop
IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
assert(unswitch_iff != NULL, "should be at least one");
// Need to revert back to normal loop if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
head->as_CountedLoop()->set_normal_loop();
}
#ifdef ASSERT
assert(proj_true->is_IfTrue(), "must be true projection");
entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
Node* predicate = find_predicate(entry); if (predicate == NULL) { // No empty predicate
Node* uniqc = proj_true->unique_ctrl_out();
assert((uniqc == head && !head->is_strip_mined()) || (uniqc == head->in(LoopNode::EntryControl)
&& head->is_strip_mined()), "must hold by construction if no predicates");
} else { // There is at least one empty predicate. When calling 'skip_loop_predicates' on each found empty predicate, // we should end up at 'proj_true'.
Node* proj_before_first_empty_predicate = skip_loop_predicates(entry); if (UseProfiledLoopPredicate) {
predicate = find_predicate(proj_before_first_empty_predicate); if (predicate != NULL) {
proj_before_first_empty_predicate = skip_loop_predicates(predicate);
}
} if (UseLoopPredicate) {
predicate = find_predicate(proj_before_first_empty_predicate); if (predicate != NULL) {
proj_before_first_empty_predicate = skip_loop_predicates(predicate);
}
}
assert(proj_true == proj_before_first_empty_predicate, "must hold by construction if at least one predicate");
} #endif // Increment unswitch count
LoopNode* head_clone = old_new[head->_idx]->as_Loop(); int nct = head->unswitch_count() + 1;
head->set_unswitch_count(nct);
head_clone->set_unswitch_count(nct);
// Hoist invariant casts out of each loop to the appropriate // control projection.
Node_List worklist; for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); // Copy to a worklist for easier manipulation for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
Node* use = proj->fast_out(j); if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
worklist.push(use);
}
}
ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); while (worklist.size() > 0) {
Node* use = worklist.pop();
Node* nuse = use->clone();
nuse->set_req(0, invar_proj);
_igvn.replace_input_of(use, 1, nuse);
register_new_node(nuse, invar_proj); // Same for the clone
Node* use_clone = old_new[use->_idx];
_igvn.replace_input_of(use_clone, 1, nuse);
}
}
// Hardwire the control paths in the loops into if(true) and if(false)
_igvn.rehash_node_delayed(unswitch_iff);
dominated_by(proj_true->as_IfProj(), unswitch_iff, false, false);
//-------------------------create_slow_version_of_loop------------------------ // Create a slow version of the loop by cloning the loop // and inserting an if to select fast-slow versions. // Return the inserted if.
IfNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
Node_List &old_new,
IfNode* unswitch_iff,
CloneLoopMode mode) {
LoopNode* head = loop->_head->as_Loop();
Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
_igvn.rehash_node_delayed(entry);
IdealLoopTree* outer_loop = loop->_parent;
head->verify_strip_mined(1);
// Add test to new "if" outside of loop
Node *bol = unswitch_iff->in(1)->as_Bool();
IfNode* iff = (unswitch_iff->Opcode() == Op_RangeCheck) ? new RangeCheckNode(entry, bol, unswitch_iff->_prob, unswitch_iff->_fcnt) : new IfNode(entry, bol, unswitch_iff->_prob, unswitch_iff->_fcnt);
register_node(iff, outer_loop, entry, dom_depth(entry));
ProjNode* iffast = new IfTrueNode(iff);
register_node(iffast, outer_loop, iff, dom_depth(iff));
ProjNode* ifslow = new IfFalseNode(iff);
register_node(ifslow, outer_loop, iff, dom_depth(iff));
// Clone the loop body. The clone becomes the slow loop. The // original pre-header will (illegally) have 3 control users // (old & new loops & new if).
clone_loop(loop, old_new, dom_depth(head->skip_strip_mined()), mode, iff);
assert(old_new[head->_idx]->is_Loop(), "" );
// Fast (true) and Slow (false) control
ProjNode* iffast_pred = iffast;
ProjNode* ifslow_pred = ifslow;
clone_predicates_to_unswitched_loop(loop, old_new, iffast_pred, ifslow_pred);
// Clone the loop body. The clone becomes the slow loop. The // original pre-header will (illegally) have 3 control users // (old & new loops & new if).
clone_loop(loop, old_new, dom_depth(head), CloneIncludesStripMined, iff);
assert(old_new[head->_idx]->is_Loop(), "" );
// Fast (true) control
_igvn.replace_input_of(head->skip_strip_mined(), LoopNode::EntryControl, iffast); // Slow (false) control
_igvn.replace_input_of(slow_head->skip_strip_mined(), LoopNode::EntryControl, ifslow);
recompute_dom_depth();
lk->set_iff(iff);
#ifndef PRODUCT if (TraceLoopOpts ) {
tty->print("\t after replace_input_of: head = %d, ", head->_idx); head->dump();
tty->print("\t after replace_input_of: slow_head = %d, ", slow_head->_idx); slow_head->dump();
} #endif
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.