src/share/vm/opto/loopUnswitch.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial