author | kvn |
Sat, 02 Apr 2011 10:54:15 -0700 | |
changeset 9101 | ff58f9a8e31c |
parent 8732 | 16fc1c68714b |
child 9124 | f60dee480d49 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
7397 | 2 |
* Copyright (c) 2006, 2010, 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" |
|
28 |
#include "opto/loopnode.hpp" |
|
29 |
#include "opto/rootnode.hpp" |
|
1 | 30 |
|
31 |
//================= Loop Unswitching ===================== |
|
32 |
// |
|
33 |
// orig: transformed: |
|
34 |
// if (invariant-test) then |
|
9101 | 35 |
// predicate predicate |
1 | 36 |
// loop loop |
37 |
// stmt1 stmt1 |
|
38 |
// if (invariant-test) then stmt2 |
|
39 |
// stmt2 stmt4 |
|
40 |
// else endloop |
|
41 |
// stmt3 else |
|
9101 | 42 |
// endif predicate [clone] |
43 |
// stmt4 loop [clone] |
|
44 |
// endloop stmt1 [clone] |
|
45 |
// stmt3 |
|
1 | 46 |
// stmt4 [clone] |
47 |
// endloop |
|
48 |
// endif |
|
49 |
// |
|
50 |
// Note: the "else" clause may be empty |
|
51 |
||
52 |
//------------------------------policy_unswitching----------------------------- |
|
53 |
// Return TRUE or FALSE if the loop should be unswitched |
|
54 |
// (ie. clone loop with an invariant test that does not exit the loop) |
|
55 |
bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { |
|
56 |
if( !LoopUnswitching ) { |
|
57 |
return false; |
|
58 |
} |
|
355 | 59 |
if (!_head->is_Loop()) { |
60 |
return false; |
|
61 |
} |
|
1 | 62 |
uint nodes_left = MaxNodeLimit - phase->C->unique(); |
63 |
if (2 * _body.size() > nodes_left) { |
|
64 |
return false; // Too speculative if running low on nodes. |
|
65 |
} |
|
66 |
LoopNode* head = _head->as_Loop(); |
|
67 |
if (head->unswitch_count() + 1 > head->unswitch_max()) { |
|
68 |
return false; |
|
69 |
} |
|
70 |
return phase->find_unswitching_candidate(this) != NULL; |
|
71 |
} |
|
72 |
||
73 |
//------------------------------find_unswitching_candidate----------------------------- |
|
74 |
// Find candidate "if" for unswitching |
|
75 |
IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const { |
|
76 |
||
77 |
// Find first invariant test that doesn't exit the loop |
|
78 |
LoopNode *head = loop->_head->as_Loop(); |
|
79 |
IfNode* unswitch_iff = NULL; |
|
80 |
Node* n = head->in(LoopNode::LoopBackControl); |
|
81 |
while (n != head) { |
|
82 |
Node* n_dom = idom(n); |
|
83 |
if (n->is_Region()) { |
|
84 |
if (n_dom->is_If()) { |
|
85 |
IfNode* iff = n_dom->as_If(); |
|
86 |
if (iff->in(1)->is_Bool()) { |
|
87 |
BoolNode* bol = iff->in(1)->as_Bool(); |
|
88 |
if (bol->in(1)->is_Cmp()) { |
|
89 |
// If condition is invariant and not a loop exit, |
|
90 |
// then found reason to unswitch. |
|
91 |
if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) { |
|
92 |
unswitch_iff = iff; |
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
n = n_dom; |
|
99 |
} |
|
100 |
return unswitch_iff; |
|
101 |
} |
|
102 |
||
103 |
//------------------------------do_unswitching----------------------------- |
|
104 |
// Clone loop with an invariant test (that does not exit) and |
|
105 |
// insert a clone of the test that selects which version to |
|
106 |
// execute. |
|
107 |
void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) { |
|
108 |
||
109 |
// Find first invariant test that doesn't exit the loop |
|
110 |
LoopNode *head = loop->_head->as_Loop(); |
|
111 |
||
112 |
IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop); |
|
113 |
assert(unswitch_iff != NULL, "should be at least one"); |
|
114 |
||
8732
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
115 |
#ifndef PRODUCT |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
116 |
if (TraceLoopOpts) { |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
117 |
tty->print("Unswitch %d ", head->unswitch_count()+1); |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
118 |
loop->dump_head(); |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
119 |
} |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
120 |
#endif |
16fc1c68714b
7008866: Missing loop predicate for loop with multiple entries
kvn
parents:
7397
diff
changeset
|
121 |
|
1 | 122 |
// Need to revert back to normal loop |
123 |
if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { |
|
124 |
head->as_CountedLoop()->set_normal_loop(); |
|
125 |
} |
|
126 |
||
127 |
ProjNode* proj_true = create_slow_version_of_loop(loop, old_new); |
|
128 |
||
9101 | 129 |
#ifdef ASSERT |
130 |
Node* uniqc = proj_true->unique_ctrl_out(); |
|
131 |
Node* entry = head->in(LoopNode::EntryControl); |
|
132 |
Node* predicate = find_predicate(entry); |
|
133 |
if (predicate != NULL) predicate = predicate->in(0); |
|
134 |
assert(proj_true->is_IfTrue() && |
|
135 |
(predicate == NULL && uniqc == head || |
|
136 |
predicate != NULL && uniqc == predicate), "by construction"); |
|
137 |
#endif |
|
1 | 138 |
// Increment unswitch count |
139 |
LoopNode* head_clone = old_new[head->_idx]->as_Loop(); |
|
140 |
int nct = head->unswitch_count() + 1; |
|
141 |
head->set_unswitch_count(nct); |
|
142 |
head_clone->set_unswitch_count(nct); |
|
143 |
||
144 |
// Add test to new "if" outside of loop |
|
145 |
IfNode* invar_iff = proj_true->in(0)->as_If(); |
|
146 |
Node* invar_iff_c = invar_iff->in(0); |
|
147 |
BoolNode* bol = unswitch_iff->in(1)->as_Bool(); |
|
148 |
invar_iff->set_req(1, bol); |
|
149 |
invar_iff->_prob = unswitch_iff->_prob; |
|
150 |
||
151 |
ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj(); |
|
152 |
||
2131 | 153 |
// Hoist invariant casts out of each loop to the appropriate |
1 | 154 |
// control projection. |
155 |
||
156 |
Node_List worklist; |
|
157 |
||
158 |
for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) { |
|
159 |
ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); |
|
160 |
// Copy to a worklist for easier manipulation |
|
161 |
for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { |
|
162 |
Node* use = proj->fast_out(j); |
|
163 |
if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { |
|
164 |
worklist.push(use); |
|
165 |
} |
|
166 |
} |
|
167 |
ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); |
|
168 |
while (worklist.size() > 0) { |
|
169 |
Node* use = worklist.pop(); |
|
170 |
Node* nuse = use->clone(); |
|
171 |
nuse->set_req(0, invar_proj); |
|
172 |
_igvn.hash_delete(use); |
|
173 |
use->set_req(1, nuse); |
|
174 |
_igvn._worklist.push(use); |
|
175 |
register_new_node(nuse, invar_proj); |
|
176 |
// Same for the clone |
|
177 |
Node* use_clone = old_new[use->_idx]; |
|
178 |
_igvn.hash_delete(use_clone); |
|
179 |
use_clone->set_req(1, nuse); |
|
180 |
_igvn._worklist.push(use_clone); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
// Hardwire the control paths in the loops into if(true) and if(false) |
|
185 |
_igvn.hash_delete(unswitch_iff); |
|
186 |
short_circuit_if(unswitch_iff, proj_true); |
|
187 |
_igvn._worklist.push(unswitch_iff); |
|
188 |
||
189 |
IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); |
|
190 |
_igvn.hash_delete(unswitch_iff_clone); |
|
191 |
short_circuit_if(unswitch_iff_clone, proj_false); |
|
192 |
_igvn._worklist.push(unswitch_iff_clone); |
|
193 |
||
194 |
// Reoptimize loops |
|
195 |
loop->record_for_igvn(); |
|
196 |
for(int i = loop->_body.size() - 1; i >= 0 ; i--) { |
|
197 |
Node *n = loop->_body[i]; |
|
198 |
Node *n_clone = old_new[n->_idx]; |
|
199 |
_igvn._worklist.push(n_clone); |
|
200 |
} |
|
201 |
||
202 |
#ifndef PRODUCT |
|
203 |
if (TraceLoopUnswitching) { |
|
204 |
tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d", |
|
205 |
head->_idx, unswitch_iff->_idx, |
|
206 |
old_new[head->_idx]->_idx, unswitch_iff_clone->_idx); |
|
207 |
} |
|
208 |
#endif |
|
209 |
||
210 |
C->set_major_progress(); |
|
211 |
} |
|
212 |
||
213 |
//-------------------------create_slow_version_of_loop------------------------ |
|
214 |
// Create a slow version of the loop by cloning the loop |
|
215 |
// and inserting an if to select fast-slow versions. |
|
216 |
// Return control projection of the entry to the fast version. |
|
217 |
ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop, |
|
218 |
Node_List &old_new) { |
|
219 |
LoopNode* head = loop->_head->as_Loop(); |
|
220 |
Node* entry = head->in(LoopNode::EntryControl); |
|
221 |
_igvn.hash_delete(entry); |
|
222 |
_igvn._worklist.push(entry); |
|
223 |
IdealLoopTree* outer_loop = loop->_parent; |
|
224 |
||
225 |
Node *cont = _igvn.intcon(1); |
|
226 |
set_ctrl(cont, C->root()); |
|
762
1b26adb5fea1
6715633: when matching a memory node the adr_type should not change
kvn
parents:
355
diff
changeset
|
227 |
Node* opq = new (C, 2) Opaque1Node(C, cont); |
1 | 228 |
register_node(opq, outer_loop, entry, dom_depth(entry)); |
229 |
Node *bol = new (C, 2) Conv2BNode(opq); |
|
230 |
register_node(bol, outer_loop, entry, dom_depth(entry)); |
|
231 |
IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN); |
|
232 |
register_node(iff, outer_loop, entry, dom_depth(entry)); |
|
233 |
ProjNode* iffast = new (C, 1) IfTrueNode(iff); |
|
234 |
register_node(iffast, outer_loop, iff, dom_depth(iff)); |
|
235 |
ProjNode* ifslow = new (C, 1) IfFalseNode(iff); |
|
236 |
register_node(ifslow, outer_loop, iff, dom_depth(iff)); |
|
237 |
||
238 |
// Clone the loop body. The clone becomes the fast loop. The |
|
9101 | 239 |
// original pre-header will (illegally) have 3 control users |
240 |
// (old & new loops & new if). |
|
1 | 241 |
clone_loop(loop, old_new, dom_depth(head), iff); |
242 |
assert(old_new[head->_idx]->is_Loop(), "" ); |
|
243 |
||
244 |
// Fast (true) control |
|
9101 | 245 |
Node* iffast_pred = clone_loop_predicates(entry, iffast); |
1 | 246 |
_igvn.hash_delete(head); |
9101 | 247 |
head->set_req(LoopNode::EntryControl, iffast_pred); |
248 |
set_idom(head, iffast_pred, dom_depth(head)); |
|
1 | 249 |
_igvn._worklist.push(head); |
250 |
||
251 |
// Slow (false) control |
|
9101 | 252 |
Node* ifslow_pred = move_loop_predicates(entry, ifslow); |
1 | 253 |
LoopNode* slow_head = old_new[head->_idx]->as_Loop(); |
254 |
_igvn.hash_delete(slow_head); |
|
9101 | 255 |
slow_head->set_req(LoopNode::EntryControl, ifslow_pred); |
256 |
set_idom(slow_head, ifslow_pred, dom_depth(slow_head)); |
|
1 | 257 |
_igvn._worklist.push(slow_head); |
258 |
||
259 |
recompute_dom_depth(); |
|
260 |
||
261 |
return iffast; |
|
262 |
} |