duke@435: /* mikael@4153: * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "opto/connode.hpp" stefank@2314: #include "opto/loopnode.hpp" stefank@2314: #include "opto/rootnode.hpp" duke@435: duke@435: //================= Loop Unswitching ===================== duke@435: // duke@435: // orig: transformed: duke@435: // if (invariant-test) then kvn@2727: // predicate predicate duke@435: // loop loop duke@435: // stmt1 stmt1 duke@435: // if (invariant-test) then stmt2 duke@435: // stmt2 stmt4 duke@435: // else endloop duke@435: // stmt3 else kvn@2727: // endif predicate [clone] kvn@2727: // stmt4 loop [clone] kvn@2727: // endloop stmt1 [clone] kvn@2727: // stmt3 duke@435: // stmt4 [clone] duke@435: // endloop duke@435: // endif duke@435: // duke@435: // Note: the "else" clause may be empty duke@435: duke@435: //------------------------------policy_unswitching----------------------------- duke@435: // Return TRUE or FALSE if the loop should be unswitched duke@435: // (ie. clone loop with an invariant test that does not exit the loop) duke@435: bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { duke@435: if( !LoopUnswitching ) { duke@435: return false; duke@435: } rasbold@543: if (!_head->is_Loop()) { rasbold@543: return false; rasbold@543: } bharadwaj@4315: uint nodes_left = MaxNodeLimit - phase->C->live_nodes(); duke@435: if (2 * _body.size() > nodes_left) { duke@435: return false; // Too speculative if running low on nodes. duke@435: } duke@435: LoopNode* head = _head->as_Loop(); duke@435: if (head->unswitch_count() + 1 > head->unswitch_max()) { duke@435: return false; duke@435: } duke@435: return phase->find_unswitching_candidate(this) != NULL; duke@435: } duke@435: duke@435: //------------------------------find_unswitching_candidate----------------------------- duke@435: // Find candidate "if" for unswitching duke@435: IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const { duke@435: duke@435: // Find first invariant test that doesn't exit the loop duke@435: LoopNode *head = loop->_head->as_Loop(); duke@435: IfNode* unswitch_iff = NULL; duke@435: Node* n = head->in(LoopNode::LoopBackControl); duke@435: while (n != head) { duke@435: Node* n_dom = idom(n); duke@435: if (n->is_Region()) { duke@435: if (n_dom->is_If()) { duke@435: IfNode* iff = n_dom->as_If(); duke@435: if (iff->in(1)->is_Bool()) { duke@435: BoolNode* bol = iff->in(1)->as_Bool(); duke@435: if (bol->in(1)->is_Cmp()) { duke@435: // If condition is invariant and not a loop exit, duke@435: // then found reason to unswitch. duke@435: if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) { duke@435: unswitch_iff = iff; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: n = n_dom; duke@435: } duke@435: return unswitch_iff; duke@435: } duke@435: duke@435: //------------------------------do_unswitching----------------------------- duke@435: // Clone loop with an invariant test (that does not exit) and duke@435: // insert a clone of the test that selects which version to duke@435: // execute. duke@435: void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) { duke@435: duke@435: // Find first invariant test that doesn't exit the loop duke@435: LoopNode *head = loop->_head->as_Loop(); duke@435: duke@435: IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop); duke@435: assert(unswitch_iff != NULL, "should be at least one"); duke@435: kvn@2665: #ifndef PRODUCT kvn@2665: if (TraceLoopOpts) { kvn@2665: tty->print("Unswitch %d ", head->unswitch_count()+1); kvn@2665: loop->dump_head(); kvn@2665: } kvn@2665: #endif kvn@2665: duke@435: // Need to revert back to normal loop duke@435: if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { duke@435: head->as_CountedLoop()->set_normal_loop(); duke@435: } duke@435: duke@435: ProjNode* proj_true = create_slow_version_of_loop(loop, old_new); duke@435: kvn@2727: #ifdef ASSERT kvn@2727: Node* uniqc = proj_true->unique_ctrl_out(); kvn@2727: Node* entry = head->in(LoopNode::EntryControl); kvn@2727: Node* predicate = find_predicate(entry); kvn@2877: if (predicate != NULL && LoopLimitCheck && UseLoopPredicate) { kvn@2877: // We may have two predicates, find first. kvn@2877: entry = find_predicate(entry->in(0)->in(0)); kvn@2877: if (entry != NULL) predicate = entry; kvn@2877: } kvn@2727: if (predicate != NULL) predicate = predicate->in(0); kvn@2727: assert(proj_true->is_IfTrue() && kvn@2727: (predicate == NULL && uniqc == head || kvn@2727: predicate != NULL && uniqc == predicate), "by construction"); kvn@2727: #endif duke@435: // Increment unswitch count duke@435: LoopNode* head_clone = old_new[head->_idx]->as_Loop(); duke@435: int nct = head->unswitch_count() + 1; duke@435: head->set_unswitch_count(nct); duke@435: head_clone->set_unswitch_count(nct); duke@435: duke@435: // Add test to new "if" outside of loop duke@435: IfNode* invar_iff = proj_true->in(0)->as_If(); duke@435: Node* invar_iff_c = invar_iff->in(0); duke@435: BoolNode* bol = unswitch_iff->in(1)->as_Bool(); duke@435: invar_iff->set_req(1, bol); duke@435: invar_iff->_prob = unswitch_iff->_prob; duke@435: duke@435: ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj(); duke@435: twisti@1040: // Hoist invariant casts out of each loop to the appropriate duke@435: // control projection. duke@435: duke@435: Node_List worklist; duke@435: duke@435: for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) { duke@435: ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); duke@435: // Copy to a worklist for easier manipulation duke@435: for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { duke@435: Node* use = proj->fast_out(j); duke@435: if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { duke@435: worklist.push(use); duke@435: } duke@435: } duke@435: ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); duke@435: while (worklist.size() > 0) { duke@435: Node* use = worklist.pop(); duke@435: Node* nuse = use->clone(); duke@435: nuse->set_req(0, invar_proj); kvn@3847: _igvn.replace_input_of(use, 1, nuse); duke@435: register_new_node(nuse, invar_proj); duke@435: // Same for the clone duke@435: Node* use_clone = old_new[use->_idx]; kvn@3847: _igvn.replace_input_of(use_clone, 1, nuse); duke@435: } duke@435: } duke@435: duke@435: // Hardwire the control paths in the loops into if(true) and if(false) kvn@3847: _igvn.rehash_node_delayed(unswitch_iff); duke@435: short_circuit_if(unswitch_iff, proj_true); duke@435: duke@435: IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); kvn@3847: _igvn.rehash_node_delayed(unswitch_iff_clone); duke@435: short_circuit_if(unswitch_iff_clone, proj_false); duke@435: duke@435: // Reoptimize loops duke@435: loop->record_for_igvn(); duke@435: for(int i = loop->_body.size() - 1; i >= 0 ; i--) { duke@435: Node *n = loop->_body[i]; duke@435: Node *n_clone = old_new[n->_idx]; duke@435: _igvn._worklist.push(n_clone); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: if (TraceLoopUnswitching) { duke@435: tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d", duke@435: head->_idx, unswitch_iff->_idx, duke@435: old_new[head->_idx]->_idx, unswitch_iff_clone->_idx); duke@435: } duke@435: #endif duke@435: duke@435: C->set_major_progress(); duke@435: } duke@435: duke@435: //-------------------------create_slow_version_of_loop------------------------ duke@435: // Create a slow version of the loop by cloning the loop duke@435: // and inserting an if to select fast-slow versions. duke@435: // Return control projection of the entry to the fast version. duke@435: ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop, duke@435: Node_List &old_new) { duke@435: LoopNode* head = loop->_head->as_Loop(); kvn@2877: bool counted_loop = head->is_CountedLoop(); duke@435: Node* entry = head->in(LoopNode::EntryControl); kvn@3847: _igvn.rehash_node_delayed(entry); duke@435: IdealLoopTree* outer_loop = loop->_parent; duke@435: duke@435: Node *cont = _igvn.intcon(1); duke@435: set_ctrl(cont, C->root()); kvn@4115: Node* opq = new (C) Opaque1Node(C, cont); duke@435: register_node(opq, outer_loop, entry, dom_depth(entry)); kvn@4115: Node *bol = new (C) Conv2BNode(opq); duke@435: register_node(bol, outer_loop, entry, dom_depth(entry)); kvn@4115: IfNode* iff = new (C) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN); duke@435: register_node(iff, outer_loop, entry, dom_depth(entry)); kvn@4115: ProjNode* iffast = new (C) IfTrueNode(iff); duke@435: register_node(iffast, outer_loop, iff, dom_depth(iff)); kvn@4115: ProjNode* ifslow = new (C) IfFalseNode(iff); duke@435: register_node(ifslow, outer_loop, iff, dom_depth(iff)); duke@435: duke@435: // Clone the loop body. The clone becomes the fast loop. The kvn@2727: // original pre-header will (illegally) have 3 control users kvn@2727: // (old & new loops & new if). duke@435: clone_loop(loop, old_new, dom_depth(head), iff); duke@435: assert(old_new[head->_idx]->is_Loop(), "" ); duke@435: duke@435: // Fast (true) control kvn@2877: Node* iffast_pred = clone_loop_predicates(entry, iffast, !counted_loop); kvn@3847: _igvn.replace_input_of(head, LoopNode::EntryControl, iffast_pred); kvn@2727: set_idom(head, iffast_pred, dom_depth(head)); duke@435: duke@435: // Slow (false) control kvn@3043: Node* ifslow_pred = clone_loop_predicates(entry, ifslow, !counted_loop); duke@435: LoopNode* slow_head = old_new[head->_idx]->as_Loop(); kvn@3847: _igvn.replace_input_of(slow_head, LoopNode::EntryControl, ifslow_pred); kvn@2727: set_idom(slow_head, ifslow_pred, dom_depth(slow_head)); duke@435: duke@435: recompute_dom_depth(); duke@435: duke@435: return iffast; duke@435: }