duke@435: /* duke@435: * Copyright 2006 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_loopUnswitch.cpp.incl" duke@435: duke@435: //================= Loop Unswitching ===================== duke@435: // duke@435: // orig: transformed: duke@435: // if (invariant-test) then 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 duke@435: // endif loop [clone] duke@435: // stmt4 stmt1 [clone] duke@435: // endloop 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: } duke@435: uint nodes_left = MaxNodeLimit - phase->C->unique(); 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: 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: duke@435: assert(proj_true->is_IfTrue() && proj_true->unique_ctrl_out() == head, "by construction"); duke@435: 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: duke@435: // Hoist invariant casts out of each loop to the appropiate 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); duke@435: _igvn.hash_delete(use); duke@435: use->set_req(1, nuse); duke@435: _igvn._worklist.push(use); duke@435: register_new_node(nuse, invar_proj); duke@435: // Same for the clone duke@435: Node* use_clone = old_new[use->_idx]; duke@435: _igvn.hash_delete(use_clone); duke@435: use_clone->set_req(1, nuse); duke@435: _igvn._worklist.push(use_clone); duke@435: } duke@435: } duke@435: duke@435: // Hardwire the control paths in the loops into if(true) and if(false) duke@435: _igvn.hash_delete(unswitch_iff); duke@435: short_circuit_if(unswitch_iff, proj_true); duke@435: _igvn._worklist.push(unswitch_iff); duke@435: duke@435: IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); duke@435: _igvn.hash_delete(unswitch_iff_clone); duke@435: short_circuit_if(unswitch_iff_clone, proj_false); duke@435: _igvn._worklist.push(unswitch_iff_clone); 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(); duke@435: Node* entry = head->in(LoopNode::EntryControl); duke@435: _igvn.hash_delete(entry); duke@435: _igvn._worklist.push(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@651: Node* opq = new (C, 2) Opaque1Node(C, cont); duke@435: register_node(opq, outer_loop, entry, dom_depth(entry)); duke@435: Node *bol = new (C, 2) Conv2BNode(opq); duke@435: register_node(bol, outer_loop, entry, dom_depth(entry)); duke@435: IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN); duke@435: register_node(iff, outer_loop, entry, dom_depth(entry)); duke@435: ProjNode* iffast = new (C, 1) IfTrueNode(iff); duke@435: register_node(iffast, outer_loop, iff, dom_depth(iff)); duke@435: ProjNode* ifslow = new (C, 1) 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 duke@435: // original pre-header will (illegally) have 2 control users (old & new loops). 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 duke@435: _igvn.hash_delete(head); duke@435: head->set_req(LoopNode::EntryControl, iffast); duke@435: set_idom(head, iffast, dom_depth(head)); duke@435: _igvn._worklist.push(head); duke@435: duke@435: // Slow (false) control duke@435: LoopNode* slow_head = old_new[head->_idx]->as_Loop(); duke@435: _igvn.hash_delete(slow_head); duke@435: slow_head->set_req(LoopNode::EntryControl, ifslow); duke@435: set_idom(slow_head, ifslow, dom_depth(slow_head)); duke@435: _igvn._worklist.push(slow_head); duke@435: duke@435: recompute_dom_depth(); duke@435: duke@435: return iffast; duke@435: }