src/share/vm/opto/loopUnswitch.cpp

Tue, 15 Apr 2008 10:49:32 -0700

author
kvn
date
Tue, 15 Apr 2008 10:49:32 -0700
changeset 520
f3b3fe64f59f
parent 435
a61af66fc99e
child 543
a761c2d3b76a
permissions
-rw-r--r--

6692301: Side effect in NumberFormat tests with -server -Xcomp
Summary: Optimization in CmpPNode::sub() removed the valid compare instruction because of false positive answer from detect_dominating_control().
Reviewed-by: jrose, sgoldman

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

mercurial