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