annotate src/share/vm/opto/loopUnswitch.cpp @ 1563:c18cbe5936b8

6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
author trims
date Thu, 27 May 2010 19:08:38 -0700
parents 98cb887364d3
children f95d63e2154a
rev   line source
duke@0 1 /*
trims@1563 2 * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1563 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1563 20 * or visit www.oracle.com if you need additional information or have any
trims@1563 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 #include "incls/_precompiled.incl"
duke@0 26 #include "incls/_loopUnswitch.cpp.incl"
duke@0 27
duke@0 28 //================= Loop Unswitching =====================
duke@0 29 //
duke@0 30 // orig: transformed:
duke@0 31 // if (invariant-test) then
duke@0 32 // loop loop
duke@0 33 // stmt1 stmt1
duke@0 34 // if (invariant-test) then stmt2
duke@0 35 // stmt2 stmt4
duke@0 36 // else endloop
duke@0 37 // stmt3 else
duke@0 38 // endif loop [clone]
duke@0 39 // stmt4 stmt1 [clone]
duke@0 40 // endloop stmt3
duke@0 41 // stmt4 [clone]
duke@0 42 // endloop
duke@0 43 // endif
duke@0 44 //
duke@0 45 // Note: the "else" clause may be empty
duke@0 46
duke@0 47 //------------------------------policy_unswitching-----------------------------
duke@0 48 // Return TRUE or FALSE if the loop should be unswitched
duke@0 49 // (ie. clone loop with an invariant test that does not exit the loop)
duke@0 50 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
duke@0 51 if( !LoopUnswitching ) {
duke@0 52 return false;
duke@0 53 }
rasbold@108 54 if (!_head->is_Loop()) {
rasbold@108 55 return false;
rasbold@108 56 }
duke@0 57 uint nodes_left = MaxNodeLimit - phase->C->unique();
duke@0 58 if (2 * _body.size() > nodes_left) {
duke@0 59 return false; // Too speculative if running low on nodes.
duke@0 60 }
duke@0 61 LoopNode* head = _head->as_Loop();
duke@0 62 if (head->unswitch_count() + 1 > head->unswitch_max()) {
duke@0 63 return false;
duke@0 64 }
duke@0 65 return phase->find_unswitching_candidate(this) != NULL;
duke@0 66 }
duke@0 67
duke@0 68 //------------------------------find_unswitching_candidate-----------------------------
duke@0 69 // Find candidate "if" for unswitching
duke@0 70 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {
duke@0 71
duke@0 72 // Find first invariant test that doesn't exit the loop
duke@0 73 LoopNode *head = loop->_head->as_Loop();
duke@0 74 IfNode* unswitch_iff = NULL;
duke@0 75 Node* n = head->in(LoopNode::LoopBackControl);
duke@0 76 while (n != head) {
duke@0 77 Node* n_dom = idom(n);
duke@0 78 if (n->is_Region()) {
duke@0 79 if (n_dom->is_If()) {
duke@0 80 IfNode* iff = n_dom->as_If();
duke@0 81 if (iff->in(1)->is_Bool()) {
duke@0 82 BoolNode* bol = iff->in(1)->as_Bool();
duke@0 83 if (bol->in(1)->is_Cmp()) {
duke@0 84 // If condition is invariant and not a loop exit,
duke@0 85 // then found reason to unswitch.
duke@0 86 if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
duke@0 87 unswitch_iff = iff;
duke@0 88 }
duke@0 89 }
duke@0 90 }
duke@0 91 }
duke@0 92 }
duke@0 93 n = n_dom;
duke@0 94 }
duke@0 95 return unswitch_iff;
duke@0 96 }
duke@0 97
duke@0 98 //------------------------------do_unswitching-----------------------------
duke@0 99 // Clone loop with an invariant test (that does not exit) and
duke@0 100 // insert a clone of the test that selects which version to
duke@0 101 // execute.
duke@0 102 void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) {
duke@0 103
duke@0 104 // Find first invariant test that doesn't exit the loop
duke@0 105 LoopNode *head = loop->_head->as_Loop();
duke@0 106
duke@0 107 IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
duke@0 108 assert(unswitch_iff != NULL, "should be at least one");
duke@0 109
duke@0 110 // Need to revert back to normal loop
duke@0 111 if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
duke@0 112 head->as_CountedLoop()->set_normal_loop();
duke@0 113 }
duke@0 114
duke@0 115 ProjNode* proj_true = create_slow_version_of_loop(loop, old_new);
duke@0 116
duke@0 117 assert(proj_true->is_IfTrue() && proj_true->unique_ctrl_out() == head, "by construction");
duke@0 118
duke@0 119 // Increment unswitch count
duke@0 120 LoopNode* head_clone = old_new[head->_idx]->as_Loop();
duke@0 121 int nct = head->unswitch_count() + 1;
duke@0 122 head->set_unswitch_count(nct);
duke@0 123 head_clone->set_unswitch_count(nct);
duke@0 124
duke@0 125 // Add test to new "if" outside of loop
duke@0 126 IfNode* invar_iff = proj_true->in(0)->as_If();
duke@0 127 Node* invar_iff_c = invar_iff->in(0);
duke@0 128 BoolNode* bol = unswitch_iff->in(1)->as_Bool();
duke@0 129 invar_iff->set_req(1, bol);
duke@0 130 invar_iff->_prob = unswitch_iff->_prob;
duke@0 131
duke@0 132 ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
duke@0 133
twisti@643 134 // Hoist invariant casts out of each loop to the appropriate
duke@0 135 // control projection.
duke@0 136
duke@0 137 Node_List worklist;
duke@0 138
duke@0 139 for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
duke@0 140 ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
duke@0 141 // Copy to a worklist for easier manipulation
duke@0 142 for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
duke@0 143 Node* use = proj->fast_out(j);
duke@0 144 if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
duke@0 145 worklist.push(use);
duke@0 146 }
duke@0 147 }
duke@0 148 ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
duke@0 149 while (worklist.size() > 0) {
duke@0 150 Node* use = worklist.pop();
duke@0 151 Node* nuse = use->clone();
duke@0 152 nuse->set_req(0, invar_proj);
duke@0 153 _igvn.hash_delete(use);
duke@0 154 use->set_req(1, nuse);
duke@0 155 _igvn._worklist.push(use);
duke@0 156 register_new_node(nuse, invar_proj);
duke@0 157 // Same for the clone
duke@0 158 Node* use_clone = old_new[use->_idx];
duke@0 159 _igvn.hash_delete(use_clone);
duke@0 160 use_clone->set_req(1, nuse);
duke@0 161 _igvn._worklist.push(use_clone);
duke@0 162 }
duke@0 163 }
duke@0 164
duke@0 165 // Hardwire the control paths in the loops into if(true) and if(false)
duke@0 166 _igvn.hash_delete(unswitch_iff);
duke@0 167 short_circuit_if(unswitch_iff, proj_true);
duke@0 168 _igvn._worklist.push(unswitch_iff);
duke@0 169
duke@0 170 IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
duke@0 171 _igvn.hash_delete(unswitch_iff_clone);
duke@0 172 short_circuit_if(unswitch_iff_clone, proj_false);
duke@0 173 _igvn._worklist.push(unswitch_iff_clone);
duke@0 174
duke@0 175 // Reoptimize loops
duke@0 176 loop->record_for_igvn();
duke@0 177 for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
duke@0 178 Node *n = loop->_body[i];
duke@0 179 Node *n_clone = old_new[n->_idx];
duke@0 180 _igvn._worklist.push(n_clone);
duke@0 181 }
duke@0 182
duke@0 183 #ifndef PRODUCT
duke@0 184 if (TraceLoopUnswitching) {
duke@0 185 tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d",
duke@0 186 head->_idx, unswitch_iff->_idx,
duke@0 187 old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
duke@0 188 }
duke@0 189 #endif
duke@0 190
duke@0 191 C->set_major_progress();
duke@0 192 }
duke@0 193
duke@0 194 //-------------------------create_slow_version_of_loop------------------------
duke@0 195 // Create a slow version of the loop by cloning the loop
duke@0 196 // and inserting an if to select fast-slow versions.
duke@0 197 // Return control projection of the entry to the fast version.
duke@0 198 ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
duke@0 199 Node_List &old_new) {
duke@0 200 LoopNode* head = loop->_head->as_Loop();
duke@0 201 Node* entry = head->in(LoopNode::EntryControl);
duke@0 202 _igvn.hash_delete(entry);
duke@0 203 _igvn._worklist.push(entry);
duke@0 204 IdealLoopTree* outer_loop = loop->_parent;
duke@0 205
duke@0 206 Node *cont = _igvn.intcon(1);
duke@0 207 set_ctrl(cont, C->root());
kvn@216 208 Node* opq = new (C, 2) Opaque1Node(C, cont);
duke@0 209 register_node(opq, outer_loop, entry, dom_depth(entry));
duke@0 210 Node *bol = new (C, 2) Conv2BNode(opq);
duke@0 211 register_node(bol, outer_loop, entry, dom_depth(entry));
duke@0 212 IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN);
duke@0 213 register_node(iff, outer_loop, entry, dom_depth(entry));
duke@0 214 ProjNode* iffast = new (C, 1) IfTrueNode(iff);
duke@0 215 register_node(iffast, outer_loop, iff, dom_depth(iff));
duke@0 216 ProjNode* ifslow = new (C, 1) IfFalseNode(iff);
duke@0 217 register_node(ifslow, outer_loop, iff, dom_depth(iff));
duke@0 218
duke@0 219 // Clone the loop body. The clone becomes the fast loop. The
duke@0 220 // original pre-header will (illegally) have 2 control users (old & new loops).
duke@0 221 clone_loop(loop, old_new, dom_depth(head), iff);
duke@0 222 assert(old_new[head->_idx]->is_Loop(), "" );
duke@0 223
duke@0 224 // Fast (true) control
duke@0 225 _igvn.hash_delete(head);
duke@0 226 head->set_req(LoopNode::EntryControl, iffast);
duke@0 227 set_idom(head, iffast, dom_depth(head));
duke@0 228 _igvn._worklist.push(head);
duke@0 229
duke@0 230 // Slow (false) control
duke@0 231 LoopNode* slow_head = old_new[head->_idx]->as_Loop();
duke@0 232 _igvn.hash_delete(slow_head);
duke@0 233 slow_head->set_req(LoopNode::EntryControl, ifslow);
duke@0 234 set_idom(slow_head, ifslow, dom_depth(slow_head));
duke@0 235 _igvn._worklist.push(slow_head);
duke@0 236
duke@0 237 recompute_dom_depth();
duke@0 238
duke@0 239 return iffast;
duke@0 240 }