annotate src/share/vm/opto/gcm.cpp @ 196:d1605aabd0a1 jdk7-b30

6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
author xdono
date Wed, 02 Jul 2008 12:55:16 -0700
parents 6152cbb08ce9
children 9c2ecc2ffb12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
196
d1605aabd0a1 6719955: Update copyright year
xdono
parents: 31
diff changeset
2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // Portions of code courtesy of Clifford Click
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // Optimization - Graph Style
a61af66fc99e Initial load
duke
parents:
diff changeset
28
a61af66fc99e Initial load
duke
parents:
diff changeset
29 #include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
30 #include "incls/_gcm.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 //----------------------------schedule_node_into_block-------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // Insert node n into block b. Look for projections of n and make sure they
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // are in b also.
a61af66fc99e Initial load
duke
parents:
diff changeset
35 void PhaseCFG::schedule_node_into_block( Node *n, Block *b ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // Set basic block of n, Add n to b,
a61af66fc99e Initial load
duke
parents:
diff changeset
37 _bbs.map(n->_idx, b);
a61af66fc99e Initial load
duke
parents:
diff changeset
38 b->add_inst(n);
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 // After Matching, nearly any old Node may have projections trailing it.
a61af66fc99e Initial load
duke
parents:
diff changeset
41 // These are usually machine-dependent flags. In any case, they might
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // float to another block below this one. Move them up.
a61af66fc99e Initial load
duke
parents:
diff changeset
43 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
44 Node* use = n->fast_out(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
45 if (use->is_Proj()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 Block* buse = _bbs[use->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
47 if (buse != b) { // In wrong block?
a61af66fc99e Initial load
duke
parents:
diff changeset
48 if (buse != NULL)
a61af66fc99e Initial load
duke
parents:
diff changeset
49 buse->find_remove(use); // Remove from wrong block
a61af66fc99e Initial load
duke
parents:
diff changeset
50 _bbs.map(use->_idx, b); // Re-insert in this block
a61af66fc99e Initial load
duke
parents:
diff changeset
51 b->add_inst(use);
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53 }
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55 }
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57
a61af66fc99e Initial load
duke
parents:
diff changeset
58 //------------------------------schedule_pinned_nodes--------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // Set the basic block for Nodes pinned into blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
60 void PhaseCFG::schedule_pinned_nodes( VectorSet &visited ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // Allocate node stack of size C->unique()+8 to avoid frequent realloc
a61af66fc99e Initial load
duke
parents:
diff changeset
62 GrowableArray <Node *> spstack(C->unique()+8);
a61af66fc99e Initial load
duke
parents:
diff changeset
63 spstack.push(_root);
a61af66fc99e Initial load
duke
parents:
diff changeset
64 while ( spstack.is_nonempty() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
65 Node *n = spstack.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
66 if( !visited.test_set(n->_idx) ) { // Test node and flag it as visited
a61af66fc99e Initial load
duke
parents:
diff changeset
67 if( n->pinned() && !_bbs.lookup(n->_idx) ) { // Pinned? Nail it down!
a61af66fc99e Initial load
duke
parents:
diff changeset
68 Node *input = n->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
69 assert( input, "pinned Node must have Control" );
a61af66fc99e Initial load
duke
parents:
diff changeset
70 while( !input->is_block_start() )
a61af66fc99e Initial load
duke
parents:
diff changeset
71 input = input->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
72 Block *b = _bbs[input->_idx]; // Basic block of controlling input
a61af66fc99e Initial load
duke
parents:
diff changeset
73 schedule_node_into_block(n, b);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75 for( int i = n->req() - 1; i >= 0; --i ) { // For all inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if( n->in(i) != NULL )
a61af66fc99e Initial load
duke
parents:
diff changeset
77 spstack.push(n->in(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79 }
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // Assert that new input b2 is dominated by all previous inputs.
a61af66fc99e Initial load
duke
parents:
diff changeset
85 // Check this by by seeing that it is dominated by b1, the deepest
a61af66fc99e Initial load
duke
parents:
diff changeset
86 // input observed until b2.
a61af66fc99e Initial load
duke
parents:
diff changeset
87 static void assert_dom(Block* b1, Block* b2, Node* n, Block_Array &bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
88 if (b1 == NULL) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
89 assert(b1->_dom_depth < b2->_dom_depth, "sanity");
a61af66fc99e Initial load
duke
parents:
diff changeset
90 Block* tmp = b2;
a61af66fc99e Initial load
duke
parents:
diff changeset
91 while (tmp != b1 && tmp != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
92 tmp = tmp->_idom;
a61af66fc99e Initial load
duke
parents:
diff changeset
93 }
a61af66fc99e Initial load
duke
parents:
diff changeset
94 if (tmp != b1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 // Detected an unschedulable graph. Print some nice stuff and die.
a61af66fc99e Initial load
duke
parents:
diff changeset
96 tty->print_cr("!!! Unschedulable graph !!!");
a61af66fc99e Initial load
duke
parents:
diff changeset
97 for (uint j=0; j<n->len(); j++) { // For all inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
98 Node* inn = n->in(j); // Get input
a61af66fc99e Initial load
duke
parents:
diff changeset
99 if (inn == NULL) continue; // Ignore NULL, missing inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
100 Block* inb = bbs[inn->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
101 tty->print("B%d idom=B%d depth=%2d ",inb->_pre_order,
a61af66fc99e Initial load
duke
parents:
diff changeset
102 inb->_idom ? inb->_idom->_pre_order : 0, inb->_dom_depth);
a61af66fc99e Initial load
duke
parents:
diff changeset
103 inn->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 tty->print("Failing node: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
106 n->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 assert(false, "unscheduable graph");
a61af66fc99e Initial load
duke
parents:
diff changeset
108 }
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 static Block* find_deepest_input(Node* n, Block_Array &bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // Find the last input dominated by all other inputs.
a61af66fc99e Initial load
duke
parents:
diff changeset
114 Block* deepb = NULL; // Deepest block so far
a61af66fc99e Initial load
duke
parents:
diff changeset
115 int deepb_dom_depth = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 for (uint k = 0; k < n->len(); k++) { // For all inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
117 Node* inn = n->in(k); // Get input
a61af66fc99e Initial load
duke
parents:
diff changeset
118 if (inn == NULL) continue; // Ignore NULL, missing inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
119 Block* inb = bbs[inn->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
120 assert(inb != NULL, "must already have scheduled this input");
a61af66fc99e Initial load
duke
parents:
diff changeset
121 if (deepb_dom_depth < (int) inb->_dom_depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 // The new inb must be dominated by the previous deepb.
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // The various inputs must be linearly ordered in the dom
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // tree, or else there will not be a unique deepest block.
a61af66fc99e Initial load
duke
parents:
diff changeset
125 DEBUG_ONLY(assert_dom(deepb, inb, n, bbs));
a61af66fc99e Initial load
duke
parents:
diff changeset
126 deepb = inb; // Save deepest block
a61af66fc99e Initial load
duke
parents:
diff changeset
127 deepb_dom_depth = deepb->_dom_depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
130 assert(deepb != NULL, "must be at least one input to n");
a61af66fc99e Initial load
duke
parents:
diff changeset
131 return deepb;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 //------------------------------schedule_early---------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
136 // Find the earliest Block any instruction can be placed in. Some instructions
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // are pinned into Blocks. Unpinned instructions can appear in last block in
a61af66fc99e Initial load
duke
parents:
diff changeset
138 // which all their inputs occur.
a61af66fc99e Initial load
duke
parents:
diff changeset
139 bool PhaseCFG::schedule_early(VectorSet &visited, Node_List &roots) {
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // Allocate stack with enough space to avoid frequent realloc
a61af66fc99e Initial load
duke
parents:
diff changeset
141 Node_Stack nstack(roots.Size() + 8); // (unique >> 1) + 24 from Java2D stats
a61af66fc99e Initial load
duke
parents:
diff changeset
142 // roots.push(_root); _root will be processed among C->top() inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
143 roots.push(C->top());
a61af66fc99e Initial load
duke
parents:
diff changeset
144 visited.set(C->top()->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 while (roots.size() != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 // Use local variables nstack_top_n & nstack_top_i to cache values
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // on stack's top.
a61af66fc99e Initial load
duke
parents:
diff changeset
149 Node *nstack_top_n = roots.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
150 uint nstack_top_i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
151 //while_nstack_nonempty:
a61af66fc99e Initial load
duke
parents:
diff changeset
152 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // Get parent node and next input's index from stack's top.
a61af66fc99e Initial load
duke
parents:
diff changeset
154 Node *n = nstack_top_n;
a61af66fc99e Initial load
duke
parents:
diff changeset
155 uint i = nstack_top_i;
a61af66fc99e Initial load
duke
parents:
diff changeset
156
a61af66fc99e Initial load
duke
parents:
diff changeset
157 if (i == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 // Special control input processing.
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // While I am here, go ahead and look for Nodes which are taking control
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // from a is_block_proj Node. After I inserted RegionNodes to make proper
a61af66fc99e Initial load
duke
parents:
diff changeset
161 // blocks, the control at a is_block_proj more properly comes from the
a61af66fc99e Initial load
duke
parents:
diff changeset
162 // Region being controlled by the block_proj Node.
a61af66fc99e Initial load
duke
parents:
diff changeset
163 const Node *in0 = n->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
164 if (in0 != NULL) { // Control-dependent?
a61af66fc99e Initial load
duke
parents:
diff changeset
165 const Node *p = in0->is_block_proj();
a61af66fc99e Initial load
duke
parents:
diff changeset
166 if (p != NULL && p != n) { // Control from a block projection?
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // Find trailing Region
a61af66fc99e Initial load
duke
parents:
diff changeset
168 Block *pb = _bbs[in0->_idx]; // Block-projection already has basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
169 uint j = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
170 if (pb->_num_succs != 1) { // More then 1 successor?
a61af66fc99e Initial load
duke
parents:
diff changeset
171 // Search for successor
a61af66fc99e Initial load
duke
parents:
diff changeset
172 uint max = pb->_nodes.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
173 assert( max > 1, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
174 uint start = max - pb->_num_succs;
a61af66fc99e Initial load
duke
parents:
diff changeset
175 // Find which output path belongs to projection
a61af66fc99e Initial load
duke
parents:
diff changeset
176 for (j = start; j < max; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 if( pb->_nodes[j] == in0 )
a61af66fc99e Initial load
duke
parents:
diff changeset
178 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
179 }
a61af66fc99e Initial load
duke
parents:
diff changeset
180 assert( j < max, "must find" );
a61af66fc99e Initial load
duke
parents:
diff changeset
181 // Change control to match head of successor basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
182 j -= start;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 n->set_req(0, pb->_succs[j]->head());
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186 } else { // n->in(0) == NULL
a61af66fc99e Initial load
duke
parents:
diff changeset
187 if (n->req() == 1) { // This guy is a constant with NO inputs?
a61af66fc99e Initial load
duke
parents:
diff changeset
188 n->set_req(0, _root);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 }
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 // First, visit all inputs and force them to get a block. If an
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // input is already in a block we quit following inputs (to avoid
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // cycles). Instead we put that Node on a worklist to be handled
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // later (since IT'S inputs may not have a block yet).
a61af66fc99e Initial load
duke
parents:
diff changeset
197 bool done = true; // Assume all n's inputs will be processed
a61af66fc99e Initial load
duke
parents:
diff changeset
198 while (i < n->len()) { // For all inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
199 Node *in = n->in(i); // Get input
a61af66fc99e Initial load
duke
parents:
diff changeset
200 ++i;
a61af66fc99e Initial load
duke
parents:
diff changeset
201 if (in == NULL) continue; // Ignore NULL, missing inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
202 int is_visited = visited.test_set(in->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
203 if (!_bbs.lookup(in->_idx)) { // Missing block selection?
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (is_visited) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 // assert( !visited.test(in->_idx), "did not schedule early" );
a61af66fc99e Initial load
duke
parents:
diff changeset
206 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
207 }
a61af66fc99e Initial load
duke
parents:
diff changeset
208 nstack.push(n, i); // Save parent node and next input's index.
a61af66fc99e Initial load
duke
parents:
diff changeset
209 nstack_top_n = in; // Process current input now.
a61af66fc99e Initial load
duke
parents:
diff changeset
210 nstack_top_i = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
211 done = false; // Not all n's inputs processed.
a61af66fc99e Initial load
duke
parents:
diff changeset
212 break; // continue while_nstack_nonempty;
a61af66fc99e Initial load
duke
parents:
diff changeset
213 } else if (!is_visited) { // Input not yet visited?
a61af66fc99e Initial load
duke
parents:
diff changeset
214 roots.push(in); // Visit this guy later, using worklist
a61af66fc99e Initial load
duke
parents:
diff changeset
215 }
a61af66fc99e Initial load
duke
parents:
diff changeset
216 }
a61af66fc99e Initial load
duke
parents:
diff changeset
217 if (done) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 // All of n's inputs have been processed, complete post-processing.
a61af66fc99e Initial load
duke
parents:
diff changeset
219
a61af66fc99e Initial load
duke
parents:
diff changeset
220 // Some instructions are pinned into a block. These include Region,
a61af66fc99e Initial load
duke
parents:
diff changeset
221 // Phi, Start, Return, and other control-dependent instructions and
a61af66fc99e Initial load
duke
parents:
diff changeset
222 // any projections which depend on them.
a61af66fc99e Initial load
duke
parents:
diff changeset
223 if (!n->pinned()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
224 // Set earliest legal block.
a61af66fc99e Initial load
duke
parents:
diff changeset
225 _bbs.map(n->_idx, find_deepest_input(n, _bbs));
a61af66fc99e Initial load
duke
parents:
diff changeset
226 }
a61af66fc99e Initial load
duke
parents:
diff changeset
227
a61af66fc99e Initial load
duke
parents:
diff changeset
228 if (nstack.is_empty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 // Finished all nodes on stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
230 // Process next node on the worklist 'roots'.
a61af66fc99e Initial load
duke
parents:
diff changeset
231 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }
a61af66fc99e Initial load
duke
parents:
diff changeset
233 // Get saved parent node and next input's index.
a61af66fc99e Initial load
duke
parents:
diff changeset
234 nstack_top_n = nstack.node();
a61af66fc99e Initial load
duke
parents:
diff changeset
235 nstack_top_i = nstack.index();
a61af66fc99e Initial load
duke
parents:
diff changeset
236 nstack.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
237 } // if (done)
a61af66fc99e Initial load
duke
parents:
diff changeset
238 } // while (true)
a61af66fc99e Initial load
duke
parents:
diff changeset
239 } // while (roots.size() != 0)
a61af66fc99e Initial load
duke
parents:
diff changeset
240 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 //------------------------------dom_lca----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // Find least common ancestor in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
245 // LCA is a current notion of LCA, to be raised above 'this'.
a61af66fc99e Initial load
duke
parents:
diff changeset
246 // As a convenient boundary condition, return 'this' if LCA is NULL.
a61af66fc99e Initial load
duke
parents:
diff changeset
247 // Find the LCA of those two nodes.
a61af66fc99e Initial load
duke
parents:
diff changeset
248 Block* Block::dom_lca(Block* LCA) {
a61af66fc99e Initial load
duke
parents:
diff changeset
249 if (LCA == NULL || LCA == this) return this;
a61af66fc99e Initial load
duke
parents:
diff changeset
250
a61af66fc99e Initial load
duke
parents:
diff changeset
251 Block* anc = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
252 while (anc->_dom_depth > LCA->_dom_depth)
a61af66fc99e Initial load
duke
parents:
diff changeset
253 anc = anc->_idom; // Walk up till anc is as high as LCA
a61af66fc99e Initial load
duke
parents:
diff changeset
254
a61af66fc99e Initial load
duke
parents:
diff changeset
255 while (LCA->_dom_depth > anc->_dom_depth)
a61af66fc99e Initial load
duke
parents:
diff changeset
256 LCA = LCA->_idom; // Walk up till LCA is as high as anc
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 while (LCA != anc) { // Walk both up till they are the same
a61af66fc99e Initial load
duke
parents:
diff changeset
259 LCA = LCA->_idom;
a61af66fc99e Initial load
duke
parents:
diff changeset
260 anc = anc->_idom;
a61af66fc99e Initial load
duke
parents:
diff changeset
261 }
a61af66fc99e Initial load
duke
parents:
diff changeset
262
a61af66fc99e Initial load
duke
parents:
diff changeset
263 return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 //--------------------------raise_LCA_above_use--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
267 // We are placing a definition, and have been given a def->use edge.
a61af66fc99e Initial load
duke
parents:
diff changeset
268 // The definition must dominate the use, so move the LCA upward in the
a61af66fc99e Initial load
duke
parents:
diff changeset
269 // dominator tree to dominate the use. If the use is a phi, adjust
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // the LCA only with the phi input paths which actually use this def.
a61af66fc99e Initial load
duke
parents:
diff changeset
271 static Block* raise_LCA_above_use(Block* LCA, Node* use, Node* def, Block_Array &bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
272 Block* buse = bbs[use->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
273 if (buse == NULL) return LCA; // Unused killing Projs have no use block
a61af66fc99e Initial load
duke
parents:
diff changeset
274 if (!use->is_Phi()) return buse->dom_lca(LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
275 uint pmax = use->req(); // Number of Phi inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
276 // Why does not this loop just break after finding the matching input to
a61af66fc99e Initial load
duke
parents:
diff changeset
277 // the Phi? Well...it's like this. I do not have true def-use/use-def
a61af66fc99e Initial load
duke
parents:
diff changeset
278 // chains. Means I cannot distinguish, from the def-use direction, which
a61af66fc99e Initial load
duke
parents:
diff changeset
279 // of many use-defs lead from the same use to the same def. That is, this
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // Phi might have several uses of the same def. Each use appears in a
a61af66fc99e Initial load
duke
parents:
diff changeset
281 // different predecessor block. But when I enter here, I cannot distinguish
a61af66fc99e Initial load
duke
parents:
diff changeset
282 // which use-def edge I should find the predecessor block for. So I find
a61af66fc99e Initial load
duke
parents:
diff changeset
283 // them all. Means I do a little extra work if a Phi uses the same value
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // more than once.
a61af66fc99e Initial load
duke
parents:
diff changeset
285 for (uint j=1; j<pmax; j++) { // For all inputs
a61af66fc99e Initial load
duke
parents:
diff changeset
286 if (use->in(j) == def) { // Found matching input?
a61af66fc99e Initial load
duke
parents:
diff changeset
287 Block* pred = bbs[buse->pred(j)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
288 LCA = pred->dom_lca(LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
289 }
a61af66fc99e Initial load
duke
parents:
diff changeset
290 }
a61af66fc99e Initial load
duke
parents:
diff changeset
291 return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
292 }
a61af66fc99e Initial load
duke
parents:
diff changeset
293
a61af66fc99e Initial load
duke
parents:
diff changeset
294 //----------------------------raise_LCA_above_marks----------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
295 // Return a new LCA that dominates LCA and any of its marked predecessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
296 // Search all my parents up to 'early' (exclusive), looking for predecessors
a61af66fc99e Initial load
duke
parents:
diff changeset
297 // which are marked with the given index. Return the LCA (in the dom tree)
a61af66fc99e Initial load
duke
parents:
diff changeset
298 // of all marked blocks. If there are none marked, return the original
a61af66fc99e Initial load
duke
parents:
diff changeset
299 // LCA.
a61af66fc99e Initial load
duke
parents:
diff changeset
300 static Block* raise_LCA_above_marks(Block* LCA, node_idx_t mark,
a61af66fc99e Initial load
duke
parents:
diff changeset
301 Block* early, Block_Array &bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 Block_List worklist;
a61af66fc99e Initial load
duke
parents:
diff changeset
303 worklist.push(LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
304 while (worklist.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
305 Block* mid = worklist.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
306 if (mid == early) continue; // stop searching here
a61af66fc99e Initial load
duke
parents:
diff changeset
307
a61af66fc99e Initial load
duke
parents:
diff changeset
308 // Test and set the visited bit.
a61af66fc99e Initial load
duke
parents:
diff changeset
309 if (mid->raise_LCA_visited() == mark) continue; // already visited
a61af66fc99e Initial load
duke
parents:
diff changeset
310 mid->set_raise_LCA_visited(mark);
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 // Don't process the current LCA, otherwise the search may terminate early
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (mid != LCA && mid->raise_LCA_mark() == mark) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // Raise the LCA.
a61af66fc99e Initial load
duke
parents:
diff changeset
315 LCA = mid->dom_lca(LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
316 if (LCA == early) break; // stop searching everywhere
a61af66fc99e Initial load
duke
parents:
diff changeset
317 assert(early->dominates(LCA), "early is high enough");
a61af66fc99e Initial load
duke
parents:
diff changeset
318 // Resume searching at that point, skipping intermediate levels.
a61af66fc99e Initial load
duke
parents:
diff changeset
319 worklist.push(LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
320 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 // Keep searching through this block's predecessors.
a61af66fc99e Initial load
duke
parents:
diff changeset
322 for (uint j = 1, jmax = mid->num_preds(); j < jmax; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 Block* mid_parent = bbs[ mid->pred(j)->_idx ];
a61af66fc99e Initial load
duke
parents:
diff changeset
324 worklist.push(mid_parent);
a61af66fc99e Initial load
duke
parents:
diff changeset
325 }
a61af66fc99e Initial load
duke
parents:
diff changeset
326 }
a61af66fc99e Initial load
duke
parents:
diff changeset
327 }
a61af66fc99e Initial load
duke
parents:
diff changeset
328 return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 //--------------------------memory_early_block--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
332 // This is a variation of find_deepest_input, the heart of schedule_early.
a61af66fc99e Initial load
duke
parents:
diff changeset
333 // Find the "early" block for a load, if we considered only memory and
a61af66fc99e Initial load
duke
parents:
diff changeset
334 // address inputs, that is, if other data inputs were ignored.
a61af66fc99e Initial load
duke
parents:
diff changeset
335 //
a61af66fc99e Initial load
duke
parents:
diff changeset
336 // Because a subset of edges are considered, the resulting block will
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // be earlier (at a shallower dom_depth) than the true schedule_early
a61af66fc99e Initial load
duke
parents:
diff changeset
338 // point of the node. We compute this earlier block as a more permissive
a61af66fc99e Initial load
duke
parents:
diff changeset
339 // site for anti-dependency insertion, but only if subsume_loads is enabled.
a61af66fc99e Initial load
duke
parents:
diff changeset
340 static Block* memory_early_block(Node* load, Block* early, Block_Array &bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 Node* base;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 Node* index;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 Node* store = load->in(MemNode::Memory);
a61af66fc99e Initial load
duke
parents:
diff changeset
344 load->as_Mach()->memory_inputs(base, index);
a61af66fc99e Initial load
duke
parents:
diff changeset
345
a61af66fc99e Initial load
duke
parents:
diff changeset
346 assert(base != NodeSentinel && index != NodeSentinel,
a61af66fc99e Initial load
duke
parents:
diff changeset
347 "unexpected base/index inputs");
a61af66fc99e Initial load
duke
parents:
diff changeset
348
a61af66fc99e Initial load
duke
parents:
diff changeset
349 Node* mem_inputs[4];
a61af66fc99e Initial load
duke
parents:
diff changeset
350 int mem_inputs_length = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
351 if (base != NULL) mem_inputs[mem_inputs_length++] = base;
a61af66fc99e Initial load
duke
parents:
diff changeset
352 if (index != NULL) mem_inputs[mem_inputs_length++] = index;
a61af66fc99e Initial load
duke
parents:
diff changeset
353 if (store != NULL) mem_inputs[mem_inputs_length++] = store;
a61af66fc99e Initial load
duke
parents:
diff changeset
354
a61af66fc99e Initial load
duke
parents:
diff changeset
355 // In the comparision below, add one to account for the control input,
a61af66fc99e Initial load
duke
parents:
diff changeset
356 // which may be null, but always takes up a spot in the in array.
a61af66fc99e Initial load
duke
parents:
diff changeset
357 if (mem_inputs_length + 1 < (int) load->req()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
358 // This "load" has more inputs than just the memory, base and index inputs.
a61af66fc99e Initial load
duke
parents:
diff changeset
359 // For purposes of checking anti-dependences, we need to start
a61af66fc99e Initial load
duke
parents:
diff changeset
360 // from the early block of only the address portion of the instruction,
a61af66fc99e Initial load
duke
parents:
diff changeset
361 // and ignore other blocks that may have factored into the wider
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // schedule_early calculation.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 if (load->in(0) != NULL) mem_inputs[mem_inputs_length++] = load->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
364
a61af66fc99e Initial load
duke
parents:
diff changeset
365 Block* deepb = NULL; // Deepest block so far
a61af66fc99e Initial load
duke
parents:
diff changeset
366 int deepb_dom_depth = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 for (int i = 0; i < mem_inputs_length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
368 Block* inb = bbs[mem_inputs[i]->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
369 if (deepb_dom_depth < (int) inb->_dom_depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
370 // The new inb must be dominated by the previous deepb.
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // The various inputs must be linearly ordered in the dom
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // tree, or else there will not be a unique deepest block.
a61af66fc99e Initial load
duke
parents:
diff changeset
373 DEBUG_ONLY(assert_dom(deepb, inb, load, bbs));
a61af66fc99e Initial load
duke
parents:
diff changeset
374 deepb = inb; // Save deepest block
a61af66fc99e Initial load
duke
parents:
diff changeset
375 deepb_dom_depth = deepb->_dom_depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
376 }
a61af66fc99e Initial load
duke
parents:
diff changeset
377 }
a61af66fc99e Initial load
duke
parents:
diff changeset
378 early = deepb;
a61af66fc99e Initial load
duke
parents:
diff changeset
379 }
a61af66fc99e Initial load
duke
parents:
diff changeset
380
a61af66fc99e Initial load
duke
parents:
diff changeset
381 return early;
a61af66fc99e Initial load
duke
parents:
diff changeset
382 }
a61af66fc99e Initial load
duke
parents:
diff changeset
383
a61af66fc99e Initial load
duke
parents:
diff changeset
384 //--------------------------insert_anti_dependences---------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
385 // A load may need to witness memory that nearby stores can overwrite.
a61af66fc99e Initial load
duke
parents:
diff changeset
386 // For each nearby store, either insert an "anti-dependence" edge
a61af66fc99e Initial load
duke
parents:
diff changeset
387 // from the load to the store, or else move LCA upward to force the
a61af66fc99e Initial load
duke
parents:
diff changeset
388 // load to (eventually) be scheduled in a block above the store.
a61af66fc99e Initial load
duke
parents:
diff changeset
389 //
a61af66fc99e Initial load
duke
parents:
diff changeset
390 // Do not add edges to stores on distinct control-flow paths;
a61af66fc99e Initial load
duke
parents:
diff changeset
391 // only add edges to stores which might interfere.
a61af66fc99e Initial load
duke
parents:
diff changeset
392 //
a61af66fc99e Initial load
duke
parents:
diff changeset
393 // Return the (updated) LCA. There will not be any possibly interfering
a61af66fc99e Initial load
duke
parents:
diff changeset
394 // store between the load's "early block" and the updated LCA.
a61af66fc99e Initial load
duke
parents:
diff changeset
395 // Any stores in the updated LCA will have new precedence edges
a61af66fc99e Initial load
duke
parents:
diff changeset
396 // back to the load. The caller is expected to schedule the load
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // in the LCA, in which case the precedence edges will make LCM
a61af66fc99e Initial load
duke
parents:
diff changeset
398 // preserve anti-dependences. The caller may also hoist the load
a61af66fc99e Initial load
duke
parents:
diff changeset
399 // above the LCA, if it is not the early block.
a61af66fc99e Initial load
duke
parents:
diff changeset
400 Block* PhaseCFG::insert_anti_dependences(Block* LCA, Node* load, bool verify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
401 assert(load->needs_anti_dependence_check(), "must be a load of some sort");
a61af66fc99e Initial load
duke
parents:
diff changeset
402 assert(LCA != NULL, "");
a61af66fc99e Initial load
duke
parents:
diff changeset
403 DEBUG_ONLY(Block* LCA_orig = LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
404
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // Compute the alias index. Loads and stores with different alias indices
a61af66fc99e Initial load
duke
parents:
diff changeset
406 // do not need anti-dependence edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
407 uint load_alias_idx = C->get_alias_index(load->adr_type());
a61af66fc99e Initial load
duke
parents:
diff changeset
408 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
409 if (load_alias_idx == Compile::AliasIdxBot && C->AliasLevel() > 0 &&
a61af66fc99e Initial load
duke
parents:
diff changeset
410 (PrintOpto || VerifyAliases ||
a61af66fc99e Initial load
duke
parents:
diff changeset
411 PrintMiscellaneous && (WizardMode || Verbose))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
412 // Load nodes should not consume all of memory.
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // Reporting a bottom type indicates a bug in adlc.
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // If some particular type of node validly consumes all of memory,
a61af66fc99e Initial load
duke
parents:
diff changeset
415 // sharpen the preceding "if" to exclude it, so we can catch bugs here.
a61af66fc99e Initial load
duke
parents:
diff changeset
416 tty->print_cr("*** Possible Anti-Dependence Bug: Load consumes all of memory.");
a61af66fc99e Initial load
duke
parents:
diff changeset
417 load->dump(2);
a61af66fc99e Initial load
duke
parents:
diff changeset
418 if (VerifyAliases) assert(load_alias_idx != Compile::AliasIdxBot, "");
a61af66fc99e Initial load
duke
parents:
diff changeset
419 }
a61af66fc99e Initial load
duke
parents:
diff changeset
420 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
421 assert(load_alias_idx || (load->is_Mach() && load->as_Mach()->ideal_Opcode() == Op_StrComp),
a61af66fc99e Initial load
duke
parents:
diff changeset
422 "String compare is only known 'load' that does not conflict with any stores");
a61af66fc99e Initial load
duke
parents:
diff changeset
423
a61af66fc99e Initial load
duke
parents:
diff changeset
424 if (!C->alias_type(load_alias_idx)->is_rewritable()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
425 // It is impossible to spoil this load by putting stores before it,
a61af66fc99e Initial load
duke
parents:
diff changeset
426 // because we know that the stores will never update the value
a61af66fc99e Initial load
duke
parents:
diff changeset
427 // which 'load' must witness.
a61af66fc99e Initial load
duke
parents:
diff changeset
428 return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
430
a61af66fc99e Initial load
duke
parents:
diff changeset
431 node_idx_t load_index = load->_idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
432
a61af66fc99e Initial load
duke
parents:
diff changeset
433 // Note the earliest legal placement of 'load', as determined by
a61af66fc99e Initial load
duke
parents:
diff changeset
434 // by the unique point in the dom tree where all memory effects
a61af66fc99e Initial load
duke
parents:
diff changeset
435 // and other inputs are first available. (Computed by schedule_early.)
a61af66fc99e Initial load
duke
parents:
diff changeset
436 // For normal loads, 'early' is the shallowest place (dom graph wise)
a61af66fc99e Initial load
duke
parents:
diff changeset
437 // to look for anti-deps between this load and any store.
a61af66fc99e Initial load
duke
parents:
diff changeset
438 Block* early = _bbs[load_index];
a61af66fc99e Initial load
duke
parents:
diff changeset
439
a61af66fc99e Initial load
duke
parents:
diff changeset
440 // If we are subsuming loads, compute an "early" block that only considers
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // memory or address inputs. This block may be different than the
a61af66fc99e Initial load
duke
parents:
diff changeset
442 // schedule_early block in that it could be at an even shallower depth in the
a61af66fc99e Initial load
duke
parents:
diff changeset
443 // dominator tree, and allow for a broader discovery of anti-dependences.
a61af66fc99e Initial load
duke
parents:
diff changeset
444 if (C->subsume_loads()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
445 early = memory_early_block(load, early, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
446 }
a61af66fc99e Initial load
duke
parents:
diff changeset
447
a61af66fc99e Initial load
duke
parents:
diff changeset
448 ResourceArea *area = Thread::current()->resource_area();
a61af66fc99e Initial load
duke
parents:
diff changeset
449 Node_List worklist_mem(area); // prior memory state to store
a61af66fc99e Initial load
duke
parents:
diff changeset
450 Node_List worklist_store(area); // possible-def to explore
31
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
451 Node_List worklist_visited(area); // visited mergemem nodes
0
a61af66fc99e Initial load
duke
parents:
diff changeset
452 Node_List non_early_stores(area); // all relevant stores outside of early
a61af66fc99e Initial load
duke
parents:
diff changeset
453 bool must_raise_LCA = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
454
a61af66fc99e Initial load
duke
parents:
diff changeset
455 #ifdef TRACK_PHI_INPUTS
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // %%% This extra checking fails because MergeMem nodes are not GVNed.
a61af66fc99e Initial load
duke
parents:
diff changeset
457 // Provide "phi_inputs" to check if every input to a PhiNode is from the
a61af66fc99e Initial load
duke
parents:
diff changeset
458 // original memory state. This indicates a PhiNode for which should not
a61af66fc99e Initial load
duke
parents:
diff changeset
459 // prevent the load from sinking. For such a block, set_raise_LCA_mark
a61af66fc99e Initial load
duke
parents:
diff changeset
460 // may be overly conservative.
a61af66fc99e Initial load
duke
parents:
diff changeset
461 // Mechanism: count inputs seen for each Phi encountered in worklist_store.
a61af66fc99e Initial load
duke
parents:
diff changeset
462 DEBUG_ONLY(GrowableArray<uint> phi_inputs(area, C->unique(),0,0));
a61af66fc99e Initial load
duke
parents:
diff changeset
463 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
464
a61af66fc99e Initial load
duke
parents:
diff changeset
465 // 'load' uses some memory state; look for users of the same state.
a61af66fc99e Initial load
duke
parents:
diff changeset
466 // Recurse through MergeMem nodes to the stores that use them.
a61af66fc99e Initial load
duke
parents:
diff changeset
467
a61af66fc99e Initial load
duke
parents:
diff changeset
468 // Each of these stores is a possible definition of memory
a61af66fc99e Initial load
duke
parents:
diff changeset
469 // that 'load' needs to use. We need to force 'load'
a61af66fc99e Initial load
duke
parents:
diff changeset
470 // to occur before each such store. When the store is in
a61af66fc99e Initial load
duke
parents:
diff changeset
471 // the same block as 'load', we insert an anti-dependence
a61af66fc99e Initial load
duke
parents:
diff changeset
472 // edge load->store.
a61af66fc99e Initial load
duke
parents:
diff changeset
473
a61af66fc99e Initial load
duke
parents:
diff changeset
474 // The relevant stores "nearby" the load consist of a tree rooted
a61af66fc99e Initial load
duke
parents:
diff changeset
475 // at initial_mem, with internal nodes of type MergeMem.
a61af66fc99e Initial load
duke
parents:
diff changeset
476 // Therefore, the branches visited by the worklist are of this form:
a61af66fc99e Initial load
duke
parents:
diff changeset
477 // initial_mem -> (MergeMem ->)* store
a61af66fc99e Initial load
duke
parents:
diff changeset
478 // The anti-dependence constraints apply only to the fringe of this tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
479
a61af66fc99e Initial load
duke
parents:
diff changeset
480 Node* initial_mem = load->in(MemNode::Memory);
a61af66fc99e Initial load
duke
parents:
diff changeset
481 worklist_store.push(initial_mem);
31
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
482 worklist_visited.push(initial_mem);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
483 worklist_mem.push(NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
484 while (worklist_store.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
485 // Examine a nearby store to see if it might interfere with our load.
a61af66fc99e Initial load
duke
parents:
diff changeset
486 Node* mem = worklist_mem.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
487 Node* store = worklist_store.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
488 uint op = store->Opcode();
a61af66fc99e Initial load
duke
parents:
diff changeset
489
a61af66fc99e Initial load
duke
parents:
diff changeset
490 // MergeMems do not directly have anti-deps.
a61af66fc99e Initial load
duke
parents:
diff changeset
491 // Treat them as internal nodes in a forward tree of memory states,
a61af66fc99e Initial load
duke
parents:
diff changeset
492 // the leaves of which are each a 'possible-def'.
a61af66fc99e Initial load
duke
parents:
diff changeset
493 if (store == initial_mem // root (exclusive) of tree we are searching
a61af66fc99e Initial load
duke
parents:
diff changeset
494 || op == Op_MergeMem // internal node of tree we are searching
a61af66fc99e Initial load
duke
parents:
diff changeset
495 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
496 mem = store; // It's not a possibly interfering store.
31
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
497 if (store == initial_mem)
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
498 initial_mem = NULL; // only process initial memory once
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
499
0
a61af66fc99e Initial load
duke
parents:
diff changeset
500 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
501 store = mem->fast_out(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
502 if (store->is_MergeMem()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
503 // Be sure we don't get into combinatorial problems.
a61af66fc99e Initial load
duke
parents:
diff changeset
504 // (Allow phis to be repeated; they can merge two relevant states.)
31
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
505 uint j = worklist_visited.size();
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
506 for (; j > 0; j--) {
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
507 if (worklist_visited.at(j-1) == store) break;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
508 }
31
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
509 if (j > 0) continue; // already on work list; do not repeat
6152cbb08ce9 6590177: jck60019 test assert(!repeated,"do not walk merges twice")
kvn
parents: 0
diff changeset
510 worklist_visited.push(store);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
511 }
a61af66fc99e Initial load
duke
parents:
diff changeset
512 worklist_mem.push(mem);
a61af66fc99e Initial load
duke
parents:
diff changeset
513 worklist_store.push(store);
a61af66fc99e Initial load
duke
parents:
diff changeset
514 }
a61af66fc99e Initial load
duke
parents:
diff changeset
515 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
516 }
a61af66fc99e Initial load
duke
parents:
diff changeset
517
a61af66fc99e Initial load
duke
parents:
diff changeset
518 if (op == Op_MachProj || op == Op_Catch) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
519 if (store->needs_anti_dependence_check()) continue; // not really a store
a61af66fc99e Initial load
duke
parents:
diff changeset
520
a61af66fc99e Initial load
duke
parents:
diff changeset
521 // Compute the alias index. Loads and stores with different alias
a61af66fc99e Initial load
duke
parents:
diff changeset
522 // indices do not need anti-dependence edges. Wide MemBar's are
a61af66fc99e Initial load
duke
parents:
diff changeset
523 // anti-dependent on everything (except immutable memories).
a61af66fc99e Initial load
duke
parents:
diff changeset
524 const TypePtr* adr_type = store->adr_type();
a61af66fc99e Initial load
duke
parents:
diff changeset
525 if (!C->can_alias(adr_type, load_alias_idx)) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
526
a61af66fc99e Initial load
duke
parents:
diff changeset
527 // Most slow-path runtime calls do NOT modify Java memory, but
a61af66fc99e Initial load
duke
parents:
diff changeset
528 // they can block and so write Raw memory.
a61af66fc99e Initial load
duke
parents:
diff changeset
529 if (store->is_Mach()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
530 MachNode* mstore = store->as_Mach();
a61af66fc99e Initial load
duke
parents:
diff changeset
531 if (load_alias_idx != Compile::AliasIdxRaw) {
a61af66fc99e Initial load
duke
parents:
diff changeset
532 // Check for call into the runtime using the Java calling
a61af66fc99e Initial load
duke
parents:
diff changeset
533 // convention (and from there into a wrapper); it has no
a61af66fc99e Initial load
duke
parents:
diff changeset
534 // _method. Can't do this optimization for Native calls because
a61af66fc99e Initial load
duke
parents:
diff changeset
535 // they CAN write to Java memory.
a61af66fc99e Initial load
duke
parents:
diff changeset
536 if (mstore->ideal_Opcode() == Op_CallStaticJava) {
a61af66fc99e Initial load
duke
parents:
diff changeset
537 assert(mstore->is_MachSafePoint(), "");
a61af66fc99e Initial load
duke
parents:
diff changeset
538 MachSafePointNode* ms = (MachSafePointNode*) mstore;
a61af66fc99e Initial load
duke
parents:
diff changeset
539 assert(ms->is_MachCallJava(), "");
a61af66fc99e Initial load
duke
parents:
diff changeset
540 MachCallJavaNode* mcj = (MachCallJavaNode*) ms;
a61af66fc99e Initial load
duke
parents:
diff changeset
541 if (mcj->_method == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
542 // These runtime calls do not write to Java visible memory
a61af66fc99e Initial load
duke
parents:
diff changeset
543 // (other than Raw) and so do not require anti-dependence edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
544 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
545 }
a61af66fc99e Initial load
duke
parents:
diff changeset
546 }
a61af66fc99e Initial load
duke
parents:
diff changeset
547 // Same for SafePoints: they read/write Raw but only read otherwise.
a61af66fc99e Initial load
duke
parents:
diff changeset
548 // This is basically a workaround for SafePoints only defining control
a61af66fc99e Initial load
duke
parents:
diff changeset
549 // instead of control + memory.
a61af66fc99e Initial load
duke
parents:
diff changeset
550 if (mstore->ideal_Opcode() == Op_SafePoint)
a61af66fc99e Initial load
duke
parents:
diff changeset
551 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
552 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
553 // Some raw memory, such as the load of "top" at an allocation,
a61af66fc99e Initial load
duke
parents:
diff changeset
554 // can be control dependent on the previous safepoint. See
a61af66fc99e Initial load
duke
parents:
diff changeset
555 // comments in GraphKit::allocate_heap() about control input.
a61af66fc99e Initial load
duke
parents:
diff changeset
556 // Inserting an anti-dep between such a safepoint and a use
a61af66fc99e Initial load
duke
parents:
diff changeset
557 // creates a cycle, and will cause a subsequent failure in
a61af66fc99e Initial load
duke
parents:
diff changeset
558 // local scheduling. (BugId 4919904)
a61af66fc99e Initial load
duke
parents:
diff changeset
559 // (%%% How can a control input be a safepoint and not a projection??)
a61af66fc99e Initial load
duke
parents:
diff changeset
560 if (mstore->ideal_Opcode() == Op_SafePoint && load->in(0) == mstore)
a61af66fc99e Initial load
duke
parents:
diff changeset
561 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
562 }
a61af66fc99e Initial load
duke
parents:
diff changeset
563 }
a61af66fc99e Initial load
duke
parents:
diff changeset
564
a61af66fc99e Initial load
duke
parents:
diff changeset
565 // Identify a block that the current load must be above,
a61af66fc99e Initial load
duke
parents:
diff changeset
566 // or else observe that 'store' is all the way up in the
a61af66fc99e Initial load
duke
parents:
diff changeset
567 // earliest legal block for 'load'. In the latter case,
a61af66fc99e Initial load
duke
parents:
diff changeset
568 // immediately insert an anti-dependence edge.
a61af66fc99e Initial load
duke
parents:
diff changeset
569 Block* store_block = _bbs[store->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
570 assert(store_block != NULL, "unused killing projections skipped above");
a61af66fc99e Initial load
duke
parents:
diff changeset
571
a61af66fc99e Initial load
duke
parents:
diff changeset
572 if (store->is_Phi()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
573 // 'load' uses memory which is one (or more) of the Phi's inputs.
a61af66fc99e Initial load
duke
parents:
diff changeset
574 // It must be scheduled not before the Phi, but rather before
a61af66fc99e Initial load
duke
parents:
diff changeset
575 // each of the relevant Phi inputs.
a61af66fc99e Initial load
duke
parents:
diff changeset
576 //
a61af66fc99e Initial load
duke
parents:
diff changeset
577 // Instead of finding the LCA of all inputs to a Phi that match 'mem',
a61af66fc99e Initial load
duke
parents:
diff changeset
578 // we mark each corresponding predecessor block and do a combined
a61af66fc99e Initial load
duke
parents:
diff changeset
579 // hoisting operation later (raise_LCA_above_marks).
a61af66fc99e Initial load
duke
parents:
diff changeset
580 //
a61af66fc99e Initial load
duke
parents:
diff changeset
581 // Do not assert(store_block != early, "Phi merging memory after access")
a61af66fc99e Initial load
duke
parents:
diff changeset
582 // PhiNode may be at start of block 'early' with backedge to 'early'
a61af66fc99e Initial load
duke
parents:
diff changeset
583 DEBUG_ONLY(bool found_match = false);
a61af66fc99e Initial load
duke
parents:
diff changeset
584 for (uint j = PhiNode::Input, jmax = store->req(); j < jmax; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
585 if (store->in(j) == mem) { // Found matching input?
a61af66fc99e Initial load
duke
parents:
diff changeset
586 DEBUG_ONLY(found_match = true);
a61af66fc99e Initial load
duke
parents:
diff changeset
587 Block* pred_block = _bbs[store_block->pred(j)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
588 if (pred_block != early) {
a61af66fc99e Initial load
duke
parents:
diff changeset
589 // If any predecessor of the Phi matches the load's "early block",
a61af66fc99e Initial load
duke
parents:
diff changeset
590 // we do not need a precedence edge between the Phi and 'load'
a61af66fc99e Initial load
duke
parents:
diff changeset
591 // since the load will be forced into a block preceeding the Phi.
a61af66fc99e Initial load
duke
parents:
diff changeset
592 pred_block->set_raise_LCA_mark(load_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
593 assert(!LCA_orig->dominates(pred_block) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
594 early->dominates(pred_block), "early is high enough");
a61af66fc99e Initial load
duke
parents:
diff changeset
595 must_raise_LCA = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
596 }
a61af66fc99e Initial load
duke
parents:
diff changeset
597 }
a61af66fc99e Initial load
duke
parents:
diff changeset
598 }
a61af66fc99e Initial load
duke
parents:
diff changeset
599 assert(found_match, "no worklist bug");
a61af66fc99e Initial load
duke
parents:
diff changeset
600 #ifdef TRACK_PHI_INPUTS
a61af66fc99e Initial load
duke
parents:
diff changeset
601 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
602 // This assert asks about correct handling of PhiNodes, which may not
a61af66fc99e Initial load
duke
parents:
diff changeset
603 // have all input edges directly from 'mem'. See BugId 4621264
a61af66fc99e Initial load
duke
parents:
diff changeset
604 int num_mem_inputs = phi_inputs.at_grow(store->_idx,0) + 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
605 // Increment by exactly one even if there are multiple copies of 'mem'
a61af66fc99e Initial load
duke
parents:
diff changeset
606 // coming into the phi, because we will run this block several times
a61af66fc99e Initial load
duke
parents:
diff changeset
607 // if there are several copies of 'mem'. (That's how DU iterators work.)
a61af66fc99e Initial load
duke
parents:
diff changeset
608 phi_inputs.at_put(store->_idx, num_mem_inputs);
a61af66fc99e Initial load
duke
parents:
diff changeset
609 assert(PhiNode::Input + num_mem_inputs < store->req(),
a61af66fc99e Initial load
duke
parents:
diff changeset
610 "Expect at least one phi input will not be from original memory state");
a61af66fc99e Initial load
duke
parents:
diff changeset
611 #endif //ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
612 #endif //TRACK_PHI_INPUTS
a61af66fc99e Initial load
duke
parents:
diff changeset
613 } else if (store_block != early) {
a61af66fc99e Initial load
duke
parents:
diff changeset
614 // 'store' is between the current LCA and earliest possible block.
a61af66fc99e Initial load
duke
parents:
diff changeset
615 // Label its block, and decide later on how to raise the LCA
a61af66fc99e Initial load
duke
parents:
diff changeset
616 // to include the effect on LCA of this store.
a61af66fc99e Initial load
duke
parents:
diff changeset
617 // If this store's block gets chosen as the raised LCA, we
a61af66fc99e Initial load
duke
parents:
diff changeset
618 // will find him on the non_early_stores list and stick him
a61af66fc99e Initial load
duke
parents:
diff changeset
619 // with a precedence edge.
a61af66fc99e Initial load
duke
parents:
diff changeset
620 // (But, don't bother if LCA is already raised all the way.)
a61af66fc99e Initial load
duke
parents:
diff changeset
621 if (LCA != early) {
a61af66fc99e Initial load
duke
parents:
diff changeset
622 store_block->set_raise_LCA_mark(load_index);
a61af66fc99e Initial load
duke
parents:
diff changeset
623 must_raise_LCA = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
624 non_early_stores.push(store);
a61af66fc99e Initial load
duke
parents:
diff changeset
625 }
a61af66fc99e Initial load
duke
parents:
diff changeset
626 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
627 // Found a possibly-interfering store in the load's 'early' block.
a61af66fc99e Initial load
duke
parents:
diff changeset
628 // This means 'load' cannot sink at all in the dominator tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
629 // Add an anti-dep edge, and squeeze 'load' into the highest block.
a61af66fc99e Initial load
duke
parents:
diff changeset
630 assert(store != load->in(0), "dependence cycle found");
a61af66fc99e Initial load
duke
parents:
diff changeset
631 if (verify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
632 assert(store->find_edge(load) != -1, "missing precedence edge");
a61af66fc99e Initial load
duke
parents:
diff changeset
633 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
634 store->add_prec(load);
a61af66fc99e Initial load
duke
parents:
diff changeset
635 }
a61af66fc99e Initial load
duke
parents:
diff changeset
636 LCA = early;
a61af66fc99e Initial load
duke
parents:
diff changeset
637 // This turns off the process of gathering non_early_stores.
a61af66fc99e Initial load
duke
parents:
diff changeset
638 }
a61af66fc99e Initial load
duke
parents:
diff changeset
639 }
a61af66fc99e Initial load
duke
parents:
diff changeset
640 // (Worklist is now empty; all nearby stores have been visited.)
a61af66fc99e Initial load
duke
parents:
diff changeset
641
a61af66fc99e Initial load
duke
parents:
diff changeset
642 // Finished if 'load' must be scheduled in its 'early' block.
a61af66fc99e Initial load
duke
parents:
diff changeset
643 // If we found any stores there, they have already been given
a61af66fc99e Initial load
duke
parents:
diff changeset
644 // precedence edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
645 if (LCA == early) return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
646
a61af66fc99e Initial load
duke
parents:
diff changeset
647 // We get here only if there are no possibly-interfering stores
a61af66fc99e Initial load
duke
parents:
diff changeset
648 // in the load's 'early' block. Move LCA up above all predecessors
a61af66fc99e Initial load
duke
parents:
diff changeset
649 // which contain stores we have noted.
a61af66fc99e Initial load
duke
parents:
diff changeset
650 //
a61af66fc99e Initial load
duke
parents:
diff changeset
651 // The raised LCA block can be a home to such interfering stores,
a61af66fc99e Initial load
duke
parents:
diff changeset
652 // but its predecessors must not contain any such stores.
a61af66fc99e Initial load
duke
parents:
diff changeset
653 //
a61af66fc99e Initial load
duke
parents:
diff changeset
654 // The raised LCA will be a lower bound for placing the load,
a61af66fc99e Initial load
duke
parents:
diff changeset
655 // preventing the load from sinking past any block containing
a61af66fc99e Initial load
duke
parents:
diff changeset
656 // a store that may invalidate the memory state required by 'load'.
a61af66fc99e Initial load
duke
parents:
diff changeset
657 if (must_raise_LCA)
a61af66fc99e Initial load
duke
parents:
diff changeset
658 LCA = raise_LCA_above_marks(LCA, load->_idx, early, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
659 if (LCA == early) return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
660
a61af66fc99e Initial load
duke
parents:
diff changeset
661 // Insert anti-dependence edges from 'load' to each store
a61af66fc99e Initial load
duke
parents:
diff changeset
662 // in the non-early LCA block.
a61af66fc99e Initial load
duke
parents:
diff changeset
663 // Mine the non_early_stores list for such stores.
a61af66fc99e Initial load
duke
parents:
diff changeset
664 if (LCA->raise_LCA_mark() == load_index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
665 while (non_early_stores.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
666 Node* store = non_early_stores.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
667 Block* store_block = _bbs[store->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
668 if (store_block == LCA) {
a61af66fc99e Initial load
duke
parents:
diff changeset
669 // add anti_dependence from store to load in its own block
a61af66fc99e Initial load
duke
parents:
diff changeset
670 assert(store != load->in(0), "dependence cycle found");
a61af66fc99e Initial load
duke
parents:
diff changeset
671 if (verify) {
a61af66fc99e Initial load
duke
parents:
diff changeset
672 assert(store->find_edge(load) != -1, "missing precedence edge");
a61af66fc99e Initial load
duke
parents:
diff changeset
673 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
674 store->add_prec(load);
a61af66fc99e Initial load
duke
parents:
diff changeset
675 }
a61af66fc99e Initial load
duke
parents:
diff changeset
676 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
677 assert(store_block->raise_LCA_mark() == load_index, "block was marked");
a61af66fc99e Initial load
duke
parents:
diff changeset
678 // Any other stores we found must be either inside the new LCA
a61af66fc99e Initial load
duke
parents:
diff changeset
679 // or else outside the original LCA. In the latter case, they
a61af66fc99e Initial load
duke
parents:
diff changeset
680 // did not interfere with any use of 'load'.
a61af66fc99e Initial load
duke
parents:
diff changeset
681 assert(LCA->dominates(store_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
682 || !LCA_orig->dominates(store_block), "no stray stores");
a61af66fc99e Initial load
duke
parents:
diff changeset
683 }
a61af66fc99e Initial load
duke
parents:
diff changeset
684 }
a61af66fc99e Initial load
duke
parents:
diff changeset
685 }
a61af66fc99e Initial load
duke
parents:
diff changeset
686
a61af66fc99e Initial load
duke
parents:
diff changeset
687 // Return the highest block containing stores; any stores
a61af66fc99e Initial load
duke
parents:
diff changeset
688 // within that block have been given anti-dependence edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
689 return LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
690 }
a61af66fc99e Initial load
duke
parents:
diff changeset
691
a61af66fc99e Initial load
duke
parents:
diff changeset
692 // This class is used to iterate backwards over the nodes in the graph.
a61af66fc99e Initial load
duke
parents:
diff changeset
693
a61af66fc99e Initial load
duke
parents:
diff changeset
694 class Node_Backward_Iterator {
a61af66fc99e Initial load
duke
parents:
diff changeset
695
a61af66fc99e Initial load
duke
parents:
diff changeset
696 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
697 Node_Backward_Iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
698
a61af66fc99e Initial load
duke
parents:
diff changeset
699 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
700 // Constructor for the iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
701 Node_Backward_Iterator(Node *root, VectorSet &visited, Node_List &stack, Block_Array &bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
702
a61af66fc99e Initial load
duke
parents:
diff changeset
703 // Postincrement operator to iterate over the nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
704 Node *next();
a61af66fc99e Initial load
duke
parents:
diff changeset
705
a61af66fc99e Initial load
duke
parents:
diff changeset
706 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
707 VectorSet &_visited;
a61af66fc99e Initial load
duke
parents:
diff changeset
708 Node_List &_stack;
a61af66fc99e Initial load
duke
parents:
diff changeset
709 Block_Array &_bbs;
a61af66fc99e Initial load
duke
parents:
diff changeset
710 };
a61af66fc99e Initial load
duke
parents:
diff changeset
711
a61af66fc99e Initial load
duke
parents:
diff changeset
712 // Constructor for the Node_Backward_Iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
713 Node_Backward_Iterator::Node_Backward_Iterator( Node *root, VectorSet &visited, Node_List &stack, Block_Array &bbs )
a61af66fc99e Initial load
duke
parents:
diff changeset
714 : _visited(visited), _stack(stack), _bbs(bbs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
715 // The stack should contain exactly the root
a61af66fc99e Initial load
duke
parents:
diff changeset
716 stack.clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
717 stack.push(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
718
a61af66fc99e Initial load
duke
parents:
diff changeset
719 // Clear the visited bits
a61af66fc99e Initial load
duke
parents:
diff changeset
720 visited.Clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
721 }
a61af66fc99e Initial load
duke
parents:
diff changeset
722
a61af66fc99e Initial load
duke
parents:
diff changeset
723 // Iterator for the Node_Backward_Iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
724 Node *Node_Backward_Iterator::next() {
a61af66fc99e Initial load
duke
parents:
diff changeset
725
a61af66fc99e Initial load
duke
parents:
diff changeset
726 // If the _stack is empty, then just return NULL: finished.
a61af66fc99e Initial load
duke
parents:
diff changeset
727 if ( !_stack.size() )
a61af66fc99e Initial load
duke
parents:
diff changeset
728 return NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
729
a61af66fc99e Initial load
duke
parents:
diff changeset
730 // '_stack' is emulating a real _stack. The 'visit-all-users' loop has been
a61af66fc99e Initial load
duke
parents:
diff changeset
731 // made stateless, so I do not need to record the index 'i' on my _stack.
a61af66fc99e Initial load
duke
parents:
diff changeset
732 // Instead I visit all users each time, scanning for unvisited users.
a61af66fc99e Initial load
duke
parents:
diff changeset
733 // I visit unvisited not-anti-dependence users first, then anti-dependent
a61af66fc99e Initial load
duke
parents:
diff changeset
734 // children next.
a61af66fc99e Initial load
duke
parents:
diff changeset
735 Node *self = _stack.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
736
a61af66fc99e Initial load
duke
parents:
diff changeset
737 // I cycle here when I am entering a deeper level of recursion.
a61af66fc99e Initial load
duke
parents:
diff changeset
738 // The key variable 'self' was set prior to jumping here.
a61af66fc99e Initial load
duke
parents:
diff changeset
739 while( 1 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
740
a61af66fc99e Initial load
duke
parents:
diff changeset
741 _visited.set(self->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
742
a61af66fc99e Initial load
duke
parents:
diff changeset
743 // Now schedule all uses as late as possible.
a61af66fc99e Initial load
duke
parents:
diff changeset
744 uint src = self->is_Proj() ? self->in(0)->_idx : self->_idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
745 uint src_rpo = _bbs[src]->_rpo;
a61af66fc99e Initial load
duke
parents:
diff changeset
746
a61af66fc99e Initial load
duke
parents:
diff changeset
747 // Schedule all nodes in a post-order visit
a61af66fc99e Initial load
duke
parents:
diff changeset
748 Node *unvisited = NULL; // Unvisited anti-dependent Node, if any
a61af66fc99e Initial load
duke
parents:
diff changeset
749
a61af66fc99e Initial load
duke
parents:
diff changeset
750 // Scan for unvisited nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
751 for (DUIterator_Fast imax, i = self->fast_outs(imax); i < imax; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
752 // For all uses, schedule late
a61af66fc99e Initial load
duke
parents:
diff changeset
753 Node* n = self->fast_out(i); // Use
a61af66fc99e Initial load
duke
parents:
diff changeset
754
a61af66fc99e Initial load
duke
parents:
diff changeset
755 // Skip already visited children
a61af66fc99e Initial load
duke
parents:
diff changeset
756 if ( _visited.test(n->_idx) )
a61af66fc99e Initial load
duke
parents:
diff changeset
757 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
758
a61af66fc99e Initial load
duke
parents:
diff changeset
759 // do not traverse backward control edges
a61af66fc99e Initial load
duke
parents:
diff changeset
760 Node *use = n->is_Proj() ? n->in(0) : n;
a61af66fc99e Initial load
duke
parents:
diff changeset
761 uint use_rpo = _bbs[use->_idx]->_rpo;
a61af66fc99e Initial load
duke
parents:
diff changeset
762
a61af66fc99e Initial load
duke
parents:
diff changeset
763 if ( use_rpo < src_rpo )
a61af66fc99e Initial load
duke
parents:
diff changeset
764 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
765
a61af66fc99e Initial load
duke
parents:
diff changeset
766 // Phi nodes always precede uses in a basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
767 if ( use_rpo == src_rpo && use->is_Phi() )
a61af66fc99e Initial load
duke
parents:
diff changeset
768 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
769
a61af66fc99e Initial load
duke
parents:
diff changeset
770 unvisited = n; // Found unvisited
a61af66fc99e Initial load
duke
parents:
diff changeset
771
a61af66fc99e Initial load
duke
parents:
diff changeset
772 // Check for possible-anti-dependent
a61af66fc99e Initial load
duke
parents:
diff changeset
773 if( !n->needs_anti_dependence_check() )
a61af66fc99e Initial load
duke
parents:
diff changeset
774 break; // Not visited, not anti-dep; schedule it NOW
a61af66fc99e Initial load
duke
parents:
diff changeset
775 }
a61af66fc99e Initial load
duke
parents:
diff changeset
776
a61af66fc99e Initial load
duke
parents:
diff changeset
777 // Did I find an unvisited not-anti-dependent Node?
a61af66fc99e Initial load
duke
parents:
diff changeset
778 if ( !unvisited )
a61af66fc99e Initial load
duke
parents:
diff changeset
779 break; // All done with children; post-visit 'self'
a61af66fc99e Initial load
duke
parents:
diff changeset
780
a61af66fc99e Initial load
duke
parents:
diff changeset
781 // Visit the unvisited Node. Contains the obvious push to
a61af66fc99e Initial load
duke
parents:
diff changeset
782 // indicate I'm entering a deeper level of recursion. I push the
a61af66fc99e Initial load
duke
parents:
diff changeset
783 // old state onto the _stack and set a new state and loop (recurse).
a61af66fc99e Initial load
duke
parents:
diff changeset
784 _stack.push(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
785 self = unvisited;
a61af66fc99e Initial load
duke
parents:
diff changeset
786 } // End recursion loop
a61af66fc99e Initial load
duke
parents:
diff changeset
787
a61af66fc99e Initial load
duke
parents:
diff changeset
788 return self;
a61af66fc99e Initial load
duke
parents:
diff changeset
789 }
a61af66fc99e Initial load
duke
parents:
diff changeset
790
a61af66fc99e Initial load
duke
parents:
diff changeset
791 //------------------------------ComputeLatenciesBackwards----------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
792 // Compute the latency of all the instructions.
a61af66fc99e Initial load
duke
parents:
diff changeset
793 void PhaseCFG::ComputeLatenciesBackwards(VectorSet &visited, Node_List &stack) {
a61af66fc99e Initial load
duke
parents:
diff changeset
794 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
795 if (trace_opto_pipelining())
a61af66fc99e Initial load
duke
parents:
diff changeset
796 tty->print("\n#---- ComputeLatenciesBackwards ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
797 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
798
a61af66fc99e Initial load
duke
parents:
diff changeset
799 Node_Backward_Iterator iter((Node *)_root, visited, stack, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
800 Node *n;
a61af66fc99e Initial load
duke
parents:
diff changeset
801
a61af66fc99e Initial load
duke
parents:
diff changeset
802 // Walk over all the nodes from last to first
a61af66fc99e Initial load
duke
parents:
diff changeset
803 while (n = iter.next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
804 // Set the latency for the definitions of this instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
805 partial_latency_of_defs(n);
a61af66fc99e Initial load
duke
parents:
diff changeset
806 }
a61af66fc99e Initial load
duke
parents:
diff changeset
807 } // end ComputeLatenciesBackwards
a61af66fc99e Initial load
duke
parents:
diff changeset
808
a61af66fc99e Initial load
duke
parents:
diff changeset
809 //------------------------------partial_latency_of_defs------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
810 // Compute the latency impact of this node on all defs. This computes
a61af66fc99e Initial load
duke
parents:
diff changeset
811 // a number that increases as we approach the beginning of the routine.
a61af66fc99e Initial load
duke
parents:
diff changeset
812 void PhaseCFG::partial_latency_of_defs(Node *n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
813 // Set the latency for this instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
814 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
815 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
816 tty->print("# latency_to_inputs: node_latency[%d] = %d for node",
a61af66fc99e Initial load
duke
parents:
diff changeset
817 n->_idx, _node_latency.at_grow(n->_idx));
a61af66fc99e Initial load
duke
parents:
diff changeset
818 dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
819 }
a61af66fc99e Initial load
duke
parents:
diff changeset
820 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
821
a61af66fc99e Initial load
duke
parents:
diff changeset
822 if (n->is_Proj())
a61af66fc99e Initial load
duke
parents:
diff changeset
823 n = n->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
824
a61af66fc99e Initial load
duke
parents:
diff changeset
825 if (n->is_Root())
a61af66fc99e Initial load
duke
parents:
diff changeset
826 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
827
a61af66fc99e Initial load
duke
parents:
diff changeset
828 uint nlen = n->len();
a61af66fc99e Initial load
duke
parents:
diff changeset
829 uint use_latency = _node_latency.at_grow(n->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
830 uint use_pre_order = _bbs[n->_idx]->_pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
831
a61af66fc99e Initial load
duke
parents:
diff changeset
832 for ( uint j=0; j<nlen; j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
833 Node *def = n->in(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
834
a61af66fc99e Initial load
duke
parents:
diff changeset
835 if (!def || def == n)
a61af66fc99e Initial load
duke
parents:
diff changeset
836 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
837
a61af66fc99e Initial load
duke
parents:
diff changeset
838 // Walk backwards thru projections
a61af66fc99e Initial load
duke
parents:
diff changeset
839 if (def->is_Proj())
a61af66fc99e Initial load
duke
parents:
diff changeset
840 def = def->in(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
841
a61af66fc99e Initial load
duke
parents:
diff changeset
842 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
843 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
844 tty->print("# in(%2d): ", j);
a61af66fc99e Initial load
duke
parents:
diff changeset
845 def->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
846 }
a61af66fc99e Initial load
duke
parents:
diff changeset
847 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
848
a61af66fc99e Initial load
duke
parents:
diff changeset
849 // If the defining block is not known, assume it is ok
a61af66fc99e Initial load
duke
parents:
diff changeset
850 Block *def_block = _bbs[def->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
851 uint def_pre_order = def_block ? def_block->_pre_order : 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
852
a61af66fc99e Initial load
duke
parents:
diff changeset
853 if ( (use_pre_order < def_pre_order) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
854 (use_pre_order == def_pre_order && n->is_Phi()) )
a61af66fc99e Initial load
duke
parents:
diff changeset
855 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
856
a61af66fc99e Initial load
duke
parents:
diff changeset
857 uint delta_latency = n->latency(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
858 uint current_latency = delta_latency + use_latency;
a61af66fc99e Initial load
duke
parents:
diff changeset
859
a61af66fc99e Initial load
duke
parents:
diff changeset
860 if (_node_latency.at_grow(def->_idx) < current_latency) {
a61af66fc99e Initial load
duke
parents:
diff changeset
861 _node_latency.at_put_grow(def->_idx, current_latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
862 }
a61af66fc99e Initial load
duke
parents:
diff changeset
863
a61af66fc99e Initial load
duke
parents:
diff changeset
864 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
865 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
866 tty->print_cr("# %d + edge_latency(%d) == %d -> %d, node_latency[%d] = %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
867 use_latency, j, delta_latency, current_latency, def->_idx,
a61af66fc99e Initial load
duke
parents:
diff changeset
868 _node_latency.at_grow(def->_idx));
a61af66fc99e Initial load
duke
parents:
diff changeset
869 }
a61af66fc99e Initial load
duke
parents:
diff changeset
870 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
871 }
a61af66fc99e Initial load
duke
parents:
diff changeset
872 }
a61af66fc99e Initial load
duke
parents:
diff changeset
873
a61af66fc99e Initial load
duke
parents:
diff changeset
874 //------------------------------latency_from_use-------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
875 // Compute the latency of a specific use
a61af66fc99e Initial load
duke
parents:
diff changeset
876 int PhaseCFG::latency_from_use(Node *n, const Node *def, Node *use) {
a61af66fc99e Initial load
duke
parents:
diff changeset
877 // If self-reference, return no latency
a61af66fc99e Initial load
duke
parents:
diff changeset
878 if (use == n || use->is_Root())
a61af66fc99e Initial load
duke
parents:
diff changeset
879 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
880
a61af66fc99e Initial load
duke
parents:
diff changeset
881 uint def_pre_order = _bbs[def->_idx]->_pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
882 uint latency = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
883
a61af66fc99e Initial load
duke
parents:
diff changeset
884 // If the use is not a projection, then it is simple...
a61af66fc99e Initial load
duke
parents:
diff changeset
885 if (!use->is_Proj()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
886 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
887 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
888 tty->print("# out(): ");
a61af66fc99e Initial load
duke
parents:
diff changeset
889 use->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
890 }
a61af66fc99e Initial load
duke
parents:
diff changeset
891 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
892
a61af66fc99e Initial load
duke
parents:
diff changeset
893 uint use_pre_order = _bbs[use->_idx]->_pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
894
a61af66fc99e Initial load
duke
parents:
diff changeset
895 if (use_pre_order < def_pre_order)
a61af66fc99e Initial load
duke
parents:
diff changeset
896 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
897
a61af66fc99e Initial load
duke
parents:
diff changeset
898 if (use_pre_order == def_pre_order && use->is_Phi())
a61af66fc99e Initial load
duke
parents:
diff changeset
899 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
900
a61af66fc99e Initial load
duke
parents:
diff changeset
901 uint nlen = use->len();
a61af66fc99e Initial load
duke
parents:
diff changeset
902 uint nl = _node_latency.at_grow(use->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
903
a61af66fc99e Initial load
duke
parents:
diff changeset
904 for ( uint j=0; j<nlen; j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
905 if (use->in(j) == n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
906 // Change this if we want local latencies
a61af66fc99e Initial load
duke
parents:
diff changeset
907 uint ul = use->latency(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
908 uint l = ul + nl;
a61af66fc99e Initial load
duke
parents:
diff changeset
909 if (latency < l) latency = l;
a61af66fc99e Initial load
duke
parents:
diff changeset
910 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
911 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
912 tty->print_cr("# %d + edge_latency(%d) == %d -> %d, latency = %d",
a61af66fc99e Initial load
duke
parents:
diff changeset
913 nl, j, ul, l, latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
914 }
a61af66fc99e Initial load
duke
parents:
diff changeset
915 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
916 }
a61af66fc99e Initial load
duke
parents:
diff changeset
917 }
a61af66fc99e Initial load
duke
parents:
diff changeset
918 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
919 // This is a projection, just grab the latency of the use(s)
a61af66fc99e Initial load
duke
parents:
diff changeset
920 for (DUIterator_Fast jmax, j = use->fast_outs(jmax); j < jmax; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
921 uint l = latency_from_use(use, def, use->fast_out(j));
a61af66fc99e Initial load
duke
parents:
diff changeset
922 if (latency < l) latency = l;
a61af66fc99e Initial load
duke
parents:
diff changeset
923 }
a61af66fc99e Initial load
duke
parents:
diff changeset
924 }
a61af66fc99e Initial load
duke
parents:
diff changeset
925
a61af66fc99e Initial load
duke
parents:
diff changeset
926 return latency;
a61af66fc99e Initial load
duke
parents:
diff changeset
927 }
a61af66fc99e Initial load
duke
parents:
diff changeset
928
a61af66fc99e Initial load
duke
parents:
diff changeset
929 //------------------------------latency_from_uses------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
930 // Compute the latency of this instruction relative to all of it's uses.
a61af66fc99e Initial load
duke
parents:
diff changeset
931 // This computes a number that increases as we approach the beginning of the
a61af66fc99e Initial load
duke
parents:
diff changeset
932 // routine.
a61af66fc99e Initial load
duke
parents:
diff changeset
933 void PhaseCFG::latency_from_uses(Node *n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
934 // Set the latency for this instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
935 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
936 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
937 tty->print("# latency_from_outputs: node_latency[%d] = %d for node",
a61af66fc99e Initial load
duke
parents:
diff changeset
938 n->_idx, _node_latency.at_grow(n->_idx));
a61af66fc99e Initial load
duke
parents:
diff changeset
939 dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
940 }
a61af66fc99e Initial load
duke
parents:
diff changeset
941 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
942 uint latency=0;
a61af66fc99e Initial load
duke
parents:
diff changeset
943 const Node *def = n->is_Proj() ? n->in(0): n;
a61af66fc99e Initial load
duke
parents:
diff changeset
944
a61af66fc99e Initial load
duke
parents:
diff changeset
945 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
946 uint l = latency_from_use(n, def, n->fast_out(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
947
a61af66fc99e Initial load
duke
parents:
diff changeset
948 if (latency < l) latency = l;
a61af66fc99e Initial load
duke
parents:
diff changeset
949 }
a61af66fc99e Initial load
duke
parents:
diff changeset
950
a61af66fc99e Initial load
duke
parents:
diff changeset
951 _node_latency.at_put_grow(n->_idx, latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
952 }
a61af66fc99e Initial load
duke
parents:
diff changeset
953
a61af66fc99e Initial load
duke
parents:
diff changeset
954 //------------------------------hoist_to_cheaper_block-------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
955 // Pick a block for node self, between early and LCA, that is a cheaper
a61af66fc99e Initial load
duke
parents:
diff changeset
956 // alternative to LCA.
a61af66fc99e Initial load
duke
parents:
diff changeset
957 Block* PhaseCFG::hoist_to_cheaper_block(Block* LCA, Block* early, Node* self) {
a61af66fc99e Initial load
duke
parents:
diff changeset
958 const double delta = 1+PROB_UNLIKELY_MAG(4);
a61af66fc99e Initial load
duke
parents:
diff changeset
959 Block* least = LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
960 double least_freq = least->_freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
961 uint target = _node_latency.at_grow(self->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
962 uint start_latency = _node_latency.at_grow(LCA->_nodes[0]->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
963 uint end_latency = _node_latency.at_grow(LCA->_nodes[LCA->end_idx()]->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
964 bool in_latency = (target <= start_latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
965 const Block* root_block = _bbs[_root->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
966
a61af66fc99e Initial load
duke
parents:
diff changeset
967 // Turn off latency scheduling if scheduling is just plain off
a61af66fc99e Initial load
duke
parents:
diff changeset
968 if (!C->do_scheduling())
a61af66fc99e Initial load
duke
parents:
diff changeset
969 in_latency = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
970
a61af66fc99e Initial load
duke
parents:
diff changeset
971 // Do not hoist (to cover latency) instructions which target a
a61af66fc99e Initial load
duke
parents:
diff changeset
972 // single register. Hoisting stretches the live range of the
a61af66fc99e Initial load
duke
parents:
diff changeset
973 // single register and may force spilling.
a61af66fc99e Initial load
duke
parents:
diff changeset
974 MachNode* mach = self->is_Mach() ? self->as_Mach() : NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
975 if (mach && mach->out_RegMask().is_bound1() && mach->out_RegMask().is_NotEmpty())
a61af66fc99e Initial load
duke
parents:
diff changeset
976 in_latency = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
977
a61af66fc99e Initial load
duke
parents:
diff changeset
978 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
979 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
980 tty->print("# Find cheaper block for latency %d: ",
a61af66fc99e Initial load
duke
parents:
diff changeset
981 _node_latency.at_grow(self->_idx));
a61af66fc99e Initial load
duke
parents:
diff changeset
982 self->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
983 tty->print_cr("# B%d: start latency for [%4d]=%d, end latency for [%4d]=%d, freq=%g",
a61af66fc99e Initial load
duke
parents:
diff changeset
984 LCA->_pre_order,
a61af66fc99e Initial load
duke
parents:
diff changeset
985 LCA->_nodes[0]->_idx,
a61af66fc99e Initial load
duke
parents:
diff changeset
986 start_latency,
a61af66fc99e Initial load
duke
parents:
diff changeset
987 LCA->_nodes[LCA->end_idx()]->_idx,
a61af66fc99e Initial load
duke
parents:
diff changeset
988 end_latency,
a61af66fc99e Initial load
duke
parents:
diff changeset
989 least_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
990 }
a61af66fc99e Initial load
duke
parents:
diff changeset
991 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
992
a61af66fc99e Initial load
duke
parents:
diff changeset
993 // Walk up the dominator tree from LCA (Lowest common ancestor) to
a61af66fc99e Initial load
duke
parents:
diff changeset
994 // the earliest legal location. Capture the least execution frequency.
a61af66fc99e Initial load
duke
parents:
diff changeset
995 while (LCA != early) {
a61af66fc99e Initial load
duke
parents:
diff changeset
996 LCA = LCA->_idom; // Follow up the dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
997
a61af66fc99e Initial load
duke
parents:
diff changeset
998 if (LCA == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
999 // Bailout without retry
a61af66fc99e Initial load
duke
parents:
diff changeset
1000 C->record_method_not_compilable("late schedule failed: LCA == NULL");
a61af66fc99e Initial load
duke
parents:
diff changeset
1001 return least;
a61af66fc99e Initial load
duke
parents:
diff changeset
1002 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1003
a61af66fc99e Initial load
duke
parents:
diff changeset
1004 // Don't hoist machine instructions to the root basic block
a61af66fc99e Initial load
duke
parents:
diff changeset
1005 if (mach && LCA == root_block)
a61af66fc99e Initial load
duke
parents:
diff changeset
1006 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
1007
a61af66fc99e Initial load
duke
parents:
diff changeset
1008 uint start_lat = _node_latency.at_grow(LCA->_nodes[0]->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
1009 uint end_idx = LCA->end_idx();
a61af66fc99e Initial load
duke
parents:
diff changeset
1010 uint end_lat = _node_latency.at_grow(LCA->_nodes[end_idx]->_idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
1011 double LCA_freq = LCA->_freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1012 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1013 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1014 tty->print_cr("# B%d: start latency for [%4d]=%d, end latency for [%4d]=%d, freq=%g",
a61af66fc99e Initial load
duke
parents:
diff changeset
1015 LCA->_pre_order, LCA->_nodes[0]->_idx, start_lat, end_idx, end_lat, LCA_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1016 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1017 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1018 if (LCA_freq < least_freq || // Better Frequency
a61af66fc99e Initial load
duke
parents:
diff changeset
1019 ( !in_latency && // No block containing latency
a61af66fc99e Initial load
duke
parents:
diff changeset
1020 LCA_freq < least_freq * delta && // No worse frequency
a61af66fc99e Initial load
duke
parents:
diff changeset
1021 target >= end_lat && // within latency range
a61af66fc99e Initial load
duke
parents:
diff changeset
1022 !self->is_iteratively_computed() ) // But don't hoist IV increments
a61af66fc99e Initial load
duke
parents:
diff changeset
1023 // because they may end up above other uses of their phi forcing
a61af66fc99e Initial load
duke
parents:
diff changeset
1024 // their result register to be different from their input.
a61af66fc99e Initial load
duke
parents:
diff changeset
1025 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1026 least = LCA; // Found cheaper block
a61af66fc99e Initial load
duke
parents:
diff changeset
1027 least_freq = LCA_freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1028 start_latency = start_lat;
a61af66fc99e Initial load
duke
parents:
diff changeset
1029 end_latency = end_lat;
a61af66fc99e Initial load
duke
parents:
diff changeset
1030 if (target <= start_lat)
a61af66fc99e Initial load
duke
parents:
diff changeset
1031 in_latency = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
1032 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1033 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1034
a61af66fc99e Initial load
duke
parents:
diff changeset
1035 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1036 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1037 tty->print_cr("# Choose block B%d with start latency=%d and freq=%g",
a61af66fc99e Initial load
duke
parents:
diff changeset
1038 least->_pre_order, start_latency, least_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1039 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1040 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1041
a61af66fc99e Initial load
duke
parents:
diff changeset
1042 // See if the latency needs to be updated
a61af66fc99e Initial load
duke
parents:
diff changeset
1043 if (target < end_latency) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1044 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1045 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1046 tty->print_cr("# Change latency for [%4d] from %d to %d", self->_idx, target, end_latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
1047 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1048 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1049 _node_latency.at_put_grow(self->_idx, end_latency);
a61af66fc99e Initial load
duke
parents:
diff changeset
1050 partial_latency_of_defs(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1051 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1052
a61af66fc99e Initial load
duke
parents:
diff changeset
1053 return least;
a61af66fc99e Initial load
duke
parents:
diff changeset
1054 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1055
a61af66fc99e Initial load
duke
parents:
diff changeset
1056
a61af66fc99e Initial load
duke
parents:
diff changeset
1057 //------------------------------schedule_late-----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1058 // Now schedule all codes as LATE as possible. This is the LCA in the
a61af66fc99e Initial load
duke
parents:
diff changeset
1059 // dominator tree of all USES of a value. Pick the block with the least
a61af66fc99e Initial load
duke
parents:
diff changeset
1060 // loop nesting depth that is lowest in the dominator tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
1061 extern const char must_clone[];
a61af66fc99e Initial load
duke
parents:
diff changeset
1062 void PhaseCFG::schedule_late(VectorSet &visited, Node_List &stack) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1063 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1064 if (trace_opto_pipelining())
a61af66fc99e Initial load
duke
parents:
diff changeset
1065 tty->print("\n#---- schedule_late ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1066 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1067
a61af66fc99e Initial load
duke
parents:
diff changeset
1068 Node_Backward_Iterator iter((Node *)_root, visited, stack, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1069 Node *self;
a61af66fc99e Initial load
duke
parents:
diff changeset
1070
a61af66fc99e Initial load
duke
parents:
diff changeset
1071 // Walk over all the nodes from last to first
a61af66fc99e Initial load
duke
parents:
diff changeset
1072 while (self = iter.next()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1073 Block* early = _bbs[self->_idx]; // Earliest legal placement
a61af66fc99e Initial load
duke
parents:
diff changeset
1074
a61af66fc99e Initial load
duke
parents:
diff changeset
1075 if (self->is_top()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1076 // Top node goes in bb #2 with other constants.
a61af66fc99e Initial load
duke
parents:
diff changeset
1077 // It must be special-cased, because it has no out edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
1078 early->add_inst(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1079 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
1080 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1081
a61af66fc99e Initial load
duke
parents:
diff changeset
1082 // No uses, just terminate
a61af66fc99e Initial load
duke
parents:
diff changeset
1083 if (self->outcnt() == 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1084 assert(self->Opcode() == Op_MachProj, "sanity");
a61af66fc99e Initial load
duke
parents:
diff changeset
1085 continue; // Must be a dead machine projection
a61af66fc99e Initial load
duke
parents:
diff changeset
1086 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1087
a61af66fc99e Initial load
duke
parents:
diff changeset
1088 // If node is pinned in the block, then no scheduling can be done.
a61af66fc99e Initial load
duke
parents:
diff changeset
1089 if( self->pinned() ) // Pinned in block?
a61af66fc99e Initial load
duke
parents:
diff changeset
1090 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
1091
a61af66fc99e Initial load
duke
parents:
diff changeset
1092 MachNode* mach = self->is_Mach() ? self->as_Mach() : NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
1093 if (mach) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1094 switch (mach->ideal_Opcode()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1095 case Op_CreateEx:
a61af66fc99e Initial load
duke
parents:
diff changeset
1096 // Don't move exception creation
a61af66fc99e Initial load
duke
parents:
diff changeset
1097 early->add_inst(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1098 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
1099 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
1100 case Op_CheckCastPP:
a61af66fc99e Initial load
duke
parents:
diff changeset
1101 // Don't move CheckCastPP nodes away from their input, if the input
a61af66fc99e Initial load
duke
parents:
diff changeset
1102 // is a rawptr (5071820).
a61af66fc99e Initial load
duke
parents:
diff changeset
1103 Node *def = self->in(1);
a61af66fc99e Initial load
duke
parents:
diff changeset
1104 if (def != NULL && def->bottom_type()->base() == Type::RawPtr) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1105 early->add_inst(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1106 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
1107 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1108 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
1109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1110 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1111
a61af66fc99e Initial load
duke
parents:
diff changeset
1112 // Gather LCA of all uses
a61af66fc99e Initial load
duke
parents:
diff changeset
1113 Block *LCA = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
1114 {
a61af66fc99e Initial load
duke
parents:
diff changeset
1115 for (DUIterator_Fast imax, i = self->fast_outs(imax); i < imax; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1116 // For all uses, find LCA
a61af66fc99e Initial load
duke
parents:
diff changeset
1117 Node* use = self->fast_out(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1118 LCA = raise_LCA_above_use(LCA, use, self, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1119 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1120 } // (Hide defs of imax, i from rest of block.)
a61af66fc99e Initial load
duke
parents:
diff changeset
1121
a61af66fc99e Initial load
duke
parents:
diff changeset
1122 // Place temps in the block of their use. This isn't a
a61af66fc99e Initial load
duke
parents:
diff changeset
1123 // requirement for correctness but it reduces useless
a61af66fc99e Initial load
duke
parents:
diff changeset
1124 // interference between temps and other nodes.
a61af66fc99e Initial load
duke
parents:
diff changeset
1125 if (mach != NULL && mach->is_MachTemp()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1126 _bbs.map(self->_idx, LCA);
a61af66fc99e Initial load
duke
parents:
diff changeset
1127 LCA->add_inst(self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1128 continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
1129 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1130
a61af66fc99e Initial load
duke
parents:
diff changeset
1131 // Check if 'self' could be anti-dependent on memory
a61af66fc99e Initial load
duke
parents:
diff changeset
1132 if (self->needs_anti_dependence_check()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1133 // Hoist LCA above possible-defs and insert anti-dependences to
a61af66fc99e Initial load
duke
parents:
diff changeset
1134 // defs in new LCA block.
a61af66fc99e Initial load
duke
parents:
diff changeset
1135 LCA = insert_anti_dependences(LCA, self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1137
a61af66fc99e Initial load
duke
parents:
diff changeset
1138 if (early->_dom_depth > LCA->_dom_depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1139 // Somehow the LCA has moved above the earliest legal point.
a61af66fc99e Initial load
duke
parents:
diff changeset
1140 // (One way this can happen is via memory_early_block.)
a61af66fc99e Initial load
duke
parents:
diff changeset
1141 if (C->subsume_loads() == true && !C->failing()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1142 // Retry with subsume_loads == false
a61af66fc99e Initial load
duke
parents:
diff changeset
1143 // If this is the first failure, the sentinel string will "stick"
a61af66fc99e Initial load
duke
parents:
diff changeset
1144 // to the Compile object, and the C2Compiler will see it and retry.
a61af66fc99e Initial load
duke
parents:
diff changeset
1145 C->record_failure(C2Compiler::retry_no_subsuming_loads());
a61af66fc99e Initial load
duke
parents:
diff changeset
1146 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1147 // Bailout without retry when (early->_dom_depth > LCA->_dom_depth)
a61af66fc99e Initial load
duke
parents:
diff changeset
1148 C->record_method_not_compilable("late schedule failed: incorrect graph");
a61af66fc99e Initial load
duke
parents:
diff changeset
1149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1150 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
1151 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1152
a61af66fc99e Initial load
duke
parents:
diff changeset
1153 // If there is no opportunity to hoist, then we're done.
a61af66fc99e Initial load
duke
parents:
diff changeset
1154 bool try_to_hoist = (LCA != early);
a61af66fc99e Initial load
duke
parents:
diff changeset
1155
a61af66fc99e Initial load
duke
parents:
diff changeset
1156 // Must clone guys stay next to use; no hoisting allowed.
a61af66fc99e Initial load
duke
parents:
diff changeset
1157 // Also cannot hoist guys that alter memory or are otherwise not
a61af66fc99e Initial load
duke
parents:
diff changeset
1158 // allocatable (hoisting can make a value live longer, leading to
a61af66fc99e Initial load
duke
parents:
diff changeset
1159 // anti and output dependency problems which are normally resolved
a61af66fc99e Initial load
duke
parents:
diff changeset
1160 // by the register allocator giving everyone a different register).
a61af66fc99e Initial load
duke
parents:
diff changeset
1161 if (mach != NULL && must_clone[mach->ideal_Opcode()])
a61af66fc99e Initial load
duke
parents:
diff changeset
1162 try_to_hoist = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
1163
a61af66fc99e Initial load
duke
parents:
diff changeset
1164 Block* late = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
1165 if (try_to_hoist) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1166 // Now find the block with the least execution frequency.
a61af66fc99e Initial load
duke
parents:
diff changeset
1167 // Start at the latest schedule and work up to the earliest schedule
a61af66fc99e Initial load
duke
parents:
diff changeset
1168 // in the dominator tree. Thus the Node will dominate all its uses.
a61af66fc99e Initial load
duke
parents:
diff changeset
1169 late = hoist_to_cheaper_block(LCA, early, self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1170 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1171 // Just use the LCA of the uses.
a61af66fc99e Initial load
duke
parents:
diff changeset
1172 late = LCA;
a61af66fc99e Initial load
duke
parents:
diff changeset
1173 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1174
a61af66fc99e Initial load
duke
parents:
diff changeset
1175 // Put the node into target block
a61af66fc99e Initial load
duke
parents:
diff changeset
1176 schedule_node_into_block(self, late);
a61af66fc99e Initial load
duke
parents:
diff changeset
1177
a61af66fc99e Initial load
duke
parents:
diff changeset
1178 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
1179 if (self->needs_anti_dependence_check()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1180 // since precedence edges are only inserted when we're sure they
a61af66fc99e Initial load
duke
parents:
diff changeset
1181 // are needed make sure that after placement in a block we don't
a61af66fc99e Initial load
duke
parents:
diff changeset
1182 // need any new precedence edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
1183 verify_anti_dependences(late, self);
a61af66fc99e Initial load
duke
parents:
diff changeset
1184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1185 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1186 } // Loop until all nodes have been visited
a61af66fc99e Initial load
duke
parents:
diff changeset
1187
a61af66fc99e Initial load
duke
parents:
diff changeset
1188 } // end ScheduleLate
a61af66fc99e Initial load
duke
parents:
diff changeset
1189
a61af66fc99e Initial load
duke
parents:
diff changeset
1190 //------------------------------GlobalCodeMotion-------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1191 void PhaseCFG::GlobalCodeMotion( Matcher &matcher, uint unique, Node_List &proj_list ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1192 ResourceMark rm;
a61af66fc99e Initial load
duke
parents:
diff changeset
1193
a61af66fc99e Initial load
duke
parents:
diff changeset
1194 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1195 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1196 tty->print("\n---- Start GlobalCodeMotion ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1197 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1198 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1199
a61af66fc99e Initial load
duke
parents:
diff changeset
1200 // Initialize the bbs.map for things on the proj_list
a61af66fc99e Initial load
duke
parents:
diff changeset
1201 uint i;
a61af66fc99e Initial load
duke
parents:
diff changeset
1202 for( i=0; i < proj_list.size(); i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
1203 _bbs.map(proj_list[i]->_idx, NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
1204
a61af66fc99e Initial load
duke
parents:
diff changeset
1205 // Set the basic block for Nodes pinned into blocks
a61af66fc99e Initial load
duke
parents:
diff changeset
1206 Arena *a = Thread::current()->resource_area();
a61af66fc99e Initial load
duke
parents:
diff changeset
1207 VectorSet visited(a);
a61af66fc99e Initial load
duke
parents:
diff changeset
1208 schedule_pinned_nodes( visited );
a61af66fc99e Initial load
duke
parents:
diff changeset
1209
a61af66fc99e Initial load
duke
parents:
diff changeset
1210 // Find the earliest Block any instruction can be placed in. Some
a61af66fc99e Initial load
duke
parents:
diff changeset
1211 // instructions are pinned into Blocks. Unpinned instructions can
a61af66fc99e Initial load
duke
parents:
diff changeset
1212 // appear in last block in which all their inputs occur.
a61af66fc99e Initial load
duke
parents:
diff changeset
1213 visited.Clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
1214 Node_List stack(a);
a61af66fc99e Initial load
duke
parents:
diff changeset
1215 stack.map( (unique >> 1) + 16, NULL); // Pre-grow the list
a61af66fc99e Initial load
duke
parents:
diff changeset
1216 if (!schedule_early(visited, stack)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1217 // Bailout without retry
a61af66fc99e Initial load
duke
parents:
diff changeset
1218 C->record_method_not_compilable("early schedule failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
1219 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
1220 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1221
a61af66fc99e Initial load
duke
parents:
diff changeset
1222 // Build Def-Use edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
1223 proj_list.push(_root); // Add real root as another root
a61af66fc99e Initial load
duke
parents:
diff changeset
1224 proj_list.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
1225
a61af66fc99e Initial load
duke
parents:
diff changeset
1226 // Compute the latency information (via backwards walk) for all the
a61af66fc99e Initial load
duke
parents:
diff changeset
1227 // instructions in the graph
a61af66fc99e Initial load
duke
parents:
diff changeset
1228 GrowableArray<uint> node_latency;
a61af66fc99e Initial load
duke
parents:
diff changeset
1229 _node_latency = node_latency;
a61af66fc99e Initial load
duke
parents:
diff changeset
1230
a61af66fc99e Initial load
duke
parents:
diff changeset
1231 if( C->do_scheduling() )
a61af66fc99e Initial load
duke
parents:
diff changeset
1232 ComputeLatenciesBackwards(visited, stack);
a61af66fc99e Initial load
duke
parents:
diff changeset
1233
a61af66fc99e Initial load
duke
parents:
diff changeset
1234 // Now schedule all codes as LATE as possible. This is the LCA in the
a61af66fc99e Initial load
duke
parents:
diff changeset
1235 // dominator tree of all USES of a value. Pick the block with the least
a61af66fc99e Initial load
duke
parents:
diff changeset
1236 // loop nesting depth that is lowest in the dominator tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
1237 // ( visited.Clear() called in schedule_late()->Node_Backward_Iterator() )
a61af66fc99e Initial load
duke
parents:
diff changeset
1238 schedule_late(visited, stack);
a61af66fc99e Initial load
duke
parents:
diff changeset
1239 if( C->failing() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1240 // schedule_late fails only when graph is incorrect.
a61af66fc99e Initial load
duke
parents:
diff changeset
1241 assert(!VerifyGraphEdges, "verification should have failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
1242 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
1243 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1244
a61af66fc99e Initial load
duke
parents:
diff changeset
1245 unique = C->unique();
a61af66fc99e Initial load
duke
parents:
diff changeset
1246
a61af66fc99e Initial load
duke
parents:
diff changeset
1247 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1248 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1249 tty->print("\n---- Detect implicit null checks ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1250 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1251 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1252
a61af66fc99e Initial load
duke
parents:
diff changeset
1253 // Detect implicit-null-check opportunities. Basically, find NULL checks
a61af66fc99e Initial load
duke
parents:
diff changeset
1254 // with suitable memory ops nearby. Use the memory op to do the NULL check.
a61af66fc99e Initial load
duke
parents:
diff changeset
1255 // I can generate a memory op if there is not one nearby.
a61af66fc99e Initial load
duke
parents:
diff changeset
1256 if (C->is_method_compilation()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1257 // Don't do it for natives, adapters, or runtime stubs
a61af66fc99e Initial load
duke
parents:
diff changeset
1258 int allowed_reasons = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1259 // ...and don't do it when there have been too many traps, globally.
a61af66fc99e Initial load
duke
parents:
diff changeset
1260 for (int reason = (int)Deoptimization::Reason_none+1;
a61af66fc99e Initial load
duke
parents:
diff changeset
1261 reason < Compile::trapHistLength; reason++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1262 assert(reason < BitsPerInt, "recode bit map");
a61af66fc99e Initial load
duke
parents:
diff changeset
1263 if (!C->too_many_traps((Deoptimization::DeoptReason) reason))
a61af66fc99e Initial load
duke
parents:
diff changeset
1264 allowed_reasons |= nth_bit(reason);
a61af66fc99e Initial load
duke
parents:
diff changeset
1265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1266 // By reversing the loop direction we get a very minor gain on mpegaudio.
a61af66fc99e Initial load
duke
parents:
diff changeset
1267 // Feel free to revert to a forward loop for clarity.
a61af66fc99e Initial load
duke
parents:
diff changeset
1268 // for( int i=0; i < (int)matcher._null_check_tests.size(); i+=2 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1269 for( int i= matcher._null_check_tests.size()-2; i>=0; i-=2 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1270 Node *proj = matcher._null_check_tests[i ];
a61af66fc99e Initial load
duke
parents:
diff changeset
1271 Node *val = matcher._null_check_tests[i+1];
a61af66fc99e Initial load
duke
parents:
diff changeset
1272 _bbs[proj->_idx]->implicit_null_check(this, proj, val, allowed_reasons);
a61af66fc99e Initial load
duke
parents:
diff changeset
1273 // The implicit_null_check will only perform the transformation
a61af66fc99e Initial load
duke
parents:
diff changeset
1274 // if the null branch is truly uncommon, *and* it leads to an
a61af66fc99e Initial load
duke
parents:
diff changeset
1275 // uncommon trap. Combined with the too_many_traps guards
a61af66fc99e Initial load
duke
parents:
diff changeset
1276 // above, this prevents SEGV storms reported in 6366351,
a61af66fc99e Initial load
duke
parents:
diff changeset
1277 // by recompiling offending methods without this optimization.
a61af66fc99e Initial load
duke
parents:
diff changeset
1278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1280
a61af66fc99e Initial load
duke
parents:
diff changeset
1281 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1282 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1283 tty->print("\n---- Start Local Scheduling ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1285 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1286
a61af66fc99e Initial load
duke
parents:
diff changeset
1287 // Schedule locally. Right now a simple topological sort.
a61af66fc99e Initial load
duke
parents:
diff changeset
1288 // Later, do a real latency aware scheduler.
a61af66fc99e Initial load
duke
parents:
diff changeset
1289 int *ready_cnt = NEW_RESOURCE_ARRAY(int,C->unique());
a61af66fc99e Initial load
duke
parents:
diff changeset
1290 memset( ready_cnt, -1, C->unique() * sizeof(int) );
a61af66fc99e Initial load
duke
parents:
diff changeset
1291 visited.Clear();
a61af66fc99e Initial load
duke
parents:
diff changeset
1292 for (i = 0; i < _num_blocks; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1293 if (!_blocks[i]->schedule_local(this, matcher, ready_cnt, visited)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1294 if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1295 C->record_method_not_compilable("local schedule failed");
a61af66fc99e Initial load
duke
parents:
diff changeset
1296 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1297 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
1298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1300
a61af66fc99e Initial load
duke
parents:
diff changeset
1301 // If we inserted any instructions between a Call and his CatchNode,
a61af66fc99e Initial load
duke
parents:
diff changeset
1302 // clone the instructions on all paths below the Catch.
a61af66fc99e Initial load
duke
parents:
diff changeset
1303 for( i=0; i < _num_blocks; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
1304 _blocks[i]->call_catch_cleanup(_bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1305
a61af66fc99e Initial load
duke
parents:
diff changeset
1306 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1307 if (trace_opto_pipelining()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1308 tty->print("\n---- After GlobalCodeMotion ----\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1309 for (uint i = 0; i < _num_blocks; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1310 _blocks[i]->dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
1311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1312 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1313 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1314 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1315
a61af66fc99e Initial load
duke
parents:
diff changeset
1316
a61af66fc99e Initial load
duke
parents:
diff changeset
1317 //------------------------------Estimate_Block_Frequency-----------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1318 // Estimate block frequencies based on IfNode probabilities.
a61af66fc99e Initial load
duke
parents:
diff changeset
1319 void PhaseCFG::Estimate_Block_Frequency() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1320 int cnts = C->method() ? C->method()->interpreter_invocation_count() : 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
1321 // Most of our algorithms will die horribly if frequency can become
a61af66fc99e Initial load
duke
parents:
diff changeset
1322 // negative so make sure cnts is a sane value.
a61af66fc99e Initial load
duke
parents:
diff changeset
1323 if( cnts <= 0 ) cnts = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
1324 float f = (float)cnts/(float)FreqCountInvocations;
a61af66fc99e Initial load
duke
parents:
diff changeset
1325
a61af66fc99e Initial load
duke
parents:
diff changeset
1326 // Create the loop tree and calculate loop depth.
a61af66fc99e Initial load
duke
parents:
diff changeset
1327 _root_loop = create_loop_tree();
a61af66fc99e Initial load
duke
parents:
diff changeset
1328 _root_loop->compute_loop_depth(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
1329
a61af66fc99e Initial load
duke
parents:
diff changeset
1330 // Compute block frequency of each block, relative to a single loop entry.
a61af66fc99e Initial load
duke
parents:
diff changeset
1331 _root_loop->compute_freq();
a61af66fc99e Initial load
duke
parents:
diff changeset
1332
a61af66fc99e Initial load
duke
parents:
diff changeset
1333 // Adjust all frequencies to be relative to a single method entry
a61af66fc99e Initial load
duke
parents:
diff changeset
1334 _root_loop->_freq = f * 1.0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1335 _root_loop->scale_freq();
a61af66fc99e Initial load
duke
parents:
diff changeset
1336
a61af66fc99e Initial load
duke
parents:
diff changeset
1337 // force paths ending at uncommon traps to be infrequent
a61af66fc99e Initial load
duke
parents:
diff changeset
1338 Block_List worklist;
a61af66fc99e Initial load
duke
parents:
diff changeset
1339 Block* root_blk = _blocks[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
1340 for (uint i = 0; i < root_blk->num_preds(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1341 Block *pb = _bbs[root_blk->pred(i)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
1342 if (pb->has_uncommon_code()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1343 worklist.push(pb);
a61af66fc99e Initial load
duke
parents:
diff changeset
1344 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1345 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1346 while (worklist.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1347 Block* uct = worklist.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
1348 uct->_freq = PROB_MIN;
a61af66fc99e Initial load
duke
parents:
diff changeset
1349 for (uint i = 0; i < uct->num_preds(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1350 Block *pb = _bbs[uct->pred(i)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
1351 if (pb->_num_succs == 1 && pb->_freq > PROB_MIN) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1352 worklist.push(pb);
a61af66fc99e Initial load
duke
parents:
diff changeset
1353 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1355 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1356
a61af66fc99e Initial load
duke
parents:
diff changeset
1357 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1358 if (PrintCFGBlockFreq) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1359 tty->print_cr("CFG Block Frequencies");
a61af66fc99e Initial load
duke
parents:
diff changeset
1360 _root_loop->dump_tree();
a61af66fc99e Initial load
duke
parents:
diff changeset
1361 if (Verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1362 tty->print_cr("PhaseCFG dump");
a61af66fc99e Initial load
duke
parents:
diff changeset
1363 dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
1364 tty->print_cr("Node dump");
a61af66fc99e Initial load
duke
parents:
diff changeset
1365 _root->dump(99999);
a61af66fc99e Initial load
duke
parents:
diff changeset
1366 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1367 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1368 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1369 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1370
a61af66fc99e Initial load
duke
parents:
diff changeset
1371 //----------------------------create_loop_tree--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1372 // Create a loop tree from the CFG
a61af66fc99e Initial load
duke
parents:
diff changeset
1373 CFGLoop* PhaseCFG::create_loop_tree() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1374
a61af66fc99e Initial load
duke
parents:
diff changeset
1375 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
1376 assert( _blocks[0] == _broot, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
1377 for (uint i = 0; i < _num_blocks; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1378 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
1379 // Check that _loop field are clear...we could clear them if not.
a61af66fc99e Initial load
duke
parents:
diff changeset
1380 assert(b->_loop == NULL, "clear _loop expected");
a61af66fc99e Initial load
duke
parents:
diff changeset
1381 // Sanity check that the RPO numbering is reflected in the _blocks array.
a61af66fc99e Initial load
duke
parents:
diff changeset
1382 // It doesn't have to be for the loop tree to be built, but if it is not,
a61af66fc99e Initial load
duke
parents:
diff changeset
1383 // then the blocks have been reordered since dom graph building...which
a61af66fc99e Initial load
duke
parents:
diff changeset
1384 // may question the RPO numbering
a61af66fc99e Initial load
duke
parents:
diff changeset
1385 assert(b->_rpo == i, "unexpected reverse post order number");
a61af66fc99e Initial load
duke
parents:
diff changeset
1386 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1387 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1388
a61af66fc99e Initial load
duke
parents:
diff changeset
1389 int idct = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1390 CFGLoop* root_loop = new CFGLoop(idct++);
a61af66fc99e Initial load
duke
parents:
diff changeset
1391
a61af66fc99e Initial load
duke
parents:
diff changeset
1392 Block_List worklist;
a61af66fc99e Initial load
duke
parents:
diff changeset
1393
a61af66fc99e Initial load
duke
parents:
diff changeset
1394 // Assign blocks to loops
a61af66fc99e Initial load
duke
parents:
diff changeset
1395 for(uint i = _num_blocks - 1; i > 0; i-- ) { // skip Root block
a61af66fc99e Initial load
duke
parents:
diff changeset
1396 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
1397
a61af66fc99e Initial load
duke
parents:
diff changeset
1398 if (b->head()->is_Loop()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1399 Block* loop_head = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
1400 assert(loop_head->num_preds() - 1 == 2, "loop must have 2 predecessors");
a61af66fc99e Initial load
duke
parents:
diff changeset
1401 Node* tail_n = loop_head->pred(LoopNode::LoopBackControl);
a61af66fc99e Initial load
duke
parents:
diff changeset
1402 Block* tail = _bbs[tail_n->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
1403
a61af66fc99e Initial load
duke
parents:
diff changeset
1404 // Defensively filter out Loop nodes for non-single-entry loops.
a61af66fc99e Initial load
duke
parents:
diff changeset
1405 // For all reasonable loops, the head occurs before the tail in RPO.
a61af66fc99e Initial load
duke
parents:
diff changeset
1406 if (i <= tail->_rpo) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1407
a61af66fc99e Initial load
duke
parents:
diff changeset
1408 // The tail and (recursive) predecessors of the tail
a61af66fc99e Initial load
duke
parents:
diff changeset
1409 // are made members of a new loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1410
a61af66fc99e Initial load
duke
parents:
diff changeset
1411 assert(worklist.size() == 0, "nonempty worklist");
a61af66fc99e Initial load
duke
parents:
diff changeset
1412 CFGLoop* nloop = new CFGLoop(idct++);
a61af66fc99e Initial load
duke
parents:
diff changeset
1413 assert(loop_head->_loop == NULL, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
1414 loop_head->_loop = nloop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1415 // Add to nloop so push_pred() will skip over inner loops
a61af66fc99e Initial load
duke
parents:
diff changeset
1416 nloop->add_member(loop_head);
a61af66fc99e Initial load
duke
parents:
diff changeset
1417 nloop->push_pred(loop_head, LoopNode::LoopBackControl, worklist, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1418
a61af66fc99e Initial load
duke
parents:
diff changeset
1419 while (worklist.size() > 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1420 Block* member = worklist.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
1421 if (member != loop_head) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1422 for (uint j = 1; j < member->num_preds(); j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1423 nloop->push_pred(member, j, worklist, _bbs);
a61af66fc99e Initial load
duke
parents:
diff changeset
1424 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1425 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1426 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1427 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1428 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1429 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1430
a61af66fc99e Initial load
duke
parents:
diff changeset
1431 // Create a member list for each loop consisting
a61af66fc99e Initial load
duke
parents:
diff changeset
1432 // of both blocks and (immediate child) loops.
a61af66fc99e Initial load
duke
parents:
diff changeset
1433 for (uint i = 0; i < _num_blocks; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1434 Block *b = _blocks[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
1435 CFGLoop* lp = b->_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1436 if (lp == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1437 // Not assigned to a loop. Add it to the method's pseudo loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1438 b->_loop = root_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1439 lp = root_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1440 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1441 if (lp == root_loop || b != lp->head()) { // loop heads are already members
a61af66fc99e Initial load
duke
parents:
diff changeset
1442 lp->add_member(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
1443 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1444 if (lp != root_loop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1445 if (lp->parent() == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1446 // Not a nested loop. Make it a child of the method's pseudo loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1447 root_loop->add_nested_loop(lp);
a61af66fc99e Initial load
duke
parents:
diff changeset
1448 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1449 if (b == lp->head()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1450 // Add nested loop to member list of parent loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1451 lp->parent()->add_member(lp);
a61af66fc99e Initial load
duke
parents:
diff changeset
1452 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1453 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1454 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1455
a61af66fc99e Initial load
duke
parents:
diff changeset
1456 return root_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1457 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1458
a61af66fc99e Initial load
duke
parents:
diff changeset
1459 //------------------------------push_pred--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1460 void CFGLoop::push_pred(Block* blk, int i, Block_List& worklist, Block_Array& node_to_blk) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1461 Node* pred_n = blk->pred(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1462 Block* pred = node_to_blk[pred_n->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
1463 CFGLoop *pred_loop = pred->_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1464 if (pred_loop == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1465 // Filter out blocks for non-single-entry loops.
a61af66fc99e Initial load
duke
parents:
diff changeset
1466 // For all reasonable loops, the head occurs before the tail in RPO.
a61af66fc99e Initial load
duke
parents:
diff changeset
1467 if (pred->_rpo > head()->_rpo) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1468 pred->_loop = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
1469 worklist.push(pred);
a61af66fc99e Initial load
duke
parents:
diff changeset
1470 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1471 } else if (pred_loop != this) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1472 // Nested loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1473 while (pred_loop->_parent != NULL && pred_loop->_parent != this) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1474 pred_loop = pred_loop->_parent;
a61af66fc99e Initial load
duke
parents:
diff changeset
1475 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1476 // Make pred's loop be a child
a61af66fc99e Initial load
duke
parents:
diff changeset
1477 if (pred_loop->_parent == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1478 add_nested_loop(pred_loop);
a61af66fc99e Initial load
duke
parents:
diff changeset
1479 // Continue with loop entry predecessor.
a61af66fc99e Initial load
duke
parents:
diff changeset
1480 Block* pred_head = pred_loop->head();
a61af66fc99e Initial load
duke
parents:
diff changeset
1481 assert(pred_head->num_preds() - 1 == 2, "loop must have 2 predecessors");
a61af66fc99e Initial load
duke
parents:
diff changeset
1482 assert(pred_head != head(), "loop head in only one loop");
a61af66fc99e Initial load
duke
parents:
diff changeset
1483 push_pred(pred_head, LoopNode::EntryControl, worklist, node_to_blk);
a61af66fc99e Initial load
duke
parents:
diff changeset
1484 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1485 assert(pred_loop->_parent == this && _parent == NULL, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
1486 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1488 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1489
a61af66fc99e Initial load
duke
parents:
diff changeset
1490 //------------------------------add_nested_loop--------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1491 // Make cl a child of the current loop in the loop tree.
a61af66fc99e Initial load
duke
parents:
diff changeset
1492 void CFGLoop::add_nested_loop(CFGLoop* cl) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1493 assert(_parent == NULL, "no parent yet");
a61af66fc99e Initial load
duke
parents:
diff changeset
1494 assert(cl != this, "not my own parent");
a61af66fc99e Initial load
duke
parents:
diff changeset
1495 cl->_parent = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
1496 CFGLoop* ch = _child;
a61af66fc99e Initial load
duke
parents:
diff changeset
1497 if (ch == NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1498 _child = cl;
a61af66fc99e Initial load
duke
parents:
diff changeset
1499 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1500 while (ch->_sibling != NULL) { ch = ch->_sibling; }
a61af66fc99e Initial load
duke
parents:
diff changeset
1501 ch->_sibling = cl;
a61af66fc99e Initial load
duke
parents:
diff changeset
1502 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1503 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1504
a61af66fc99e Initial load
duke
parents:
diff changeset
1505 //------------------------------compute_loop_depth-----------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1506 // Store the loop depth in each CFGLoop object.
a61af66fc99e Initial load
duke
parents:
diff changeset
1507 // Recursively walk the children to do the same for them.
a61af66fc99e Initial load
duke
parents:
diff changeset
1508 void CFGLoop::compute_loop_depth(int depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1509 _depth = depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
1510 CFGLoop* ch = _child;
a61af66fc99e Initial load
duke
parents:
diff changeset
1511 while (ch != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1512 ch->compute_loop_depth(depth + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
1513 ch = ch->_sibling;
a61af66fc99e Initial load
duke
parents:
diff changeset
1514 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1515 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1516
a61af66fc99e Initial load
duke
parents:
diff changeset
1517 //------------------------------compute_freq-----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1518 // Compute the frequency of each block and loop, relative to a single entry
a61af66fc99e Initial load
duke
parents:
diff changeset
1519 // into the dominating loop head.
a61af66fc99e Initial load
duke
parents:
diff changeset
1520 void CFGLoop::compute_freq() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1521 // Bottom up traversal of loop tree (visit inner loops first.)
a61af66fc99e Initial load
duke
parents:
diff changeset
1522 // Set loop head frequency to 1.0, then transitively
a61af66fc99e Initial load
duke
parents:
diff changeset
1523 // compute frequency for all successors in the loop,
a61af66fc99e Initial load
duke
parents:
diff changeset
1524 // as well as for each exit edge. Inner loops are
a61af66fc99e Initial load
duke
parents:
diff changeset
1525 // treated as single blocks with loop exit targets
a61af66fc99e Initial load
duke
parents:
diff changeset
1526 // as the successor blocks.
a61af66fc99e Initial load
duke
parents:
diff changeset
1527
a61af66fc99e Initial load
duke
parents:
diff changeset
1528 // Nested loops first
a61af66fc99e Initial load
duke
parents:
diff changeset
1529 CFGLoop* ch = _child;
a61af66fc99e Initial load
duke
parents:
diff changeset
1530 while (ch != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1531 ch->compute_freq();
a61af66fc99e Initial load
duke
parents:
diff changeset
1532 ch = ch->_sibling;
a61af66fc99e Initial load
duke
parents:
diff changeset
1533 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1534 assert (_members.length() > 0, "no empty loops");
a61af66fc99e Initial load
duke
parents:
diff changeset
1535 Block* hd = head();
a61af66fc99e Initial load
duke
parents:
diff changeset
1536 hd->_freq = 1.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1537 for (int i = 0; i < _members.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1538 CFGElement* s = _members.at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1539 float freq = s->_freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1540 if (s->is_block()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1541 Block* b = s->as_Block();
a61af66fc99e Initial load
duke
parents:
diff changeset
1542 for (uint j = 0; j < b->_num_succs; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1543 Block* sb = b->_succs[j];
a61af66fc99e Initial load
duke
parents:
diff changeset
1544 update_succ_freq(sb, freq * b->succ_prob(j));
a61af66fc99e Initial load
duke
parents:
diff changeset
1545 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1546 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1547 CFGLoop* lp = s->as_CFGLoop();
a61af66fc99e Initial load
duke
parents:
diff changeset
1548 assert(lp->_parent == this, "immediate child");
a61af66fc99e Initial load
duke
parents:
diff changeset
1549 for (int k = 0; k < lp->_exits.length(); k++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1550 Block* eb = lp->_exits.at(k).get_target();
a61af66fc99e Initial load
duke
parents:
diff changeset
1551 float prob = lp->_exits.at(k).get_prob();
a61af66fc99e Initial load
duke
parents:
diff changeset
1552 update_succ_freq(eb, freq * prob);
a61af66fc99e Initial load
duke
parents:
diff changeset
1553 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1554 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1555 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1556
a61af66fc99e Initial load
duke
parents:
diff changeset
1557 #if 0
a61af66fc99e Initial load
duke
parents:
diff changeset
1558 // Raise frequency of the loop backedge block, in an effort
a61af66fc99e Initial load
duke
parents:
diff changeset
1559 // to keep it empty. Skip the method level "loop".
a61af66fc99e Initial load
duke
parents:
diff changeset
1560 if (_parent != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1561 CFGElement* s = _members.at(_members.length() - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
1562 if (s->is_block()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1563 Block* bk = s->as_Block();
a61af66fc99e Initial load
duke
parents:
diff changeset
1564 if (bk->_num_succs == 1 && bk->_succs[0] == hd) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1565 // almost any value >= 1.0f works
a61af66fc99e Initial load
duke
parents:
diff changeset
1566 // FIXME: raw constant
a61af66fc99e Initial load
duke
parents:
diff changeset
1567 bk->_freq = 1.05f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1568 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1569 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1570 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1571 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
1572
a61af66fc99e Initial load
duke
parents:
diff changeset
1573 // For all loops other than the outer, "method" loop,
a61af66fc99e Initial load
duke
parents:
diff changeset
1574 // sum and normalize the exit probability. The "method" loop
a61af66fc99e Initial load
duke
parents:
diff changeset
1575 // should keep the initial exit probability of 1, so that
a61af66fc99e Initial load
duke
parents:
diff changeset
1576 // inner blocks do not get erroneously scaled.
a61af66fc99e Initial load
duke
parents:
diff changeset
1577 if (_depth != 0) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1578 // Total the exit probabilities for this loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1579 float exits_sum = 0.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1580 for (int i = 0; i < _exits.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1581 exits_sum += _exits.at(i).get_prob();
a61af66fc99e Initial load
duke
parents:
diff changeset
1582 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1583
a61af66fc99e Initial load
duke
parents:
diff changeset
1584 // Normalize the exit probabilities. Until now, the
a61af66fc99e Initial load
duke
parents:
diff changeset
1585 // probabilities estimate the possibility of exit per
a61af66fc99e Initial load
duke
parents:
diff changeset
1586 // a single loop iteration; afterward, they estimate
a61af66fc99e Initial load
duke
parents:
diff changeset
1587 // the probability of exit per loop entry.
a61af66fc99e Initial load
duke
parents:
diff changeset
1588 for (int i = 0; i < _exits.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1589 Block* et = _exits.at(i).get_target();
a61af66fc99e Initial load
duke
parents:
diff changeset
1590 float new_prob = _exits.at(i).get_prob() / exits_sum;
a61af66fc99e Initial load
duke
parents:
diff changeset
1591 BlockProbPair bpp(et, new_prob);
a61af66fc99e Initial load
duke
parents:
diff changeset
1592 _exits.at_put(i, bpp);
a61af66fc99e Initial load
duke
parents:
diff changeset
1593 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1594
a61af66fc99e Initial load
duke
parents:
diff changeset
1595 // Save the total, but guard against unreasoable probability,
a61af66fc99e Initial load
duke
parents:
diff changeset
1596 // as the value is used to estimate the loop trip count.
a61af66fc99e Initial load
duke
parents:
diff changeset
1597 // An infinite trip count would blur relative block
a61af66fc99e Initial load
duke
parents:
diff changeset
1598 // frequencies.
a61af66fc99e Initial load
duke
parents:
diff changeset
1599 if (exits_sum > 1.0f) exits_sum = 1.0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1600 if (exits_sum < PROB_MIN) exits_sum = PROB_MIN;
a61af66fc99e Initial load
duke
parents:
diff changeset
1601 _exit_prob = exits_sum;
a61af66fc99e Initial load
duke
parents:
diff changeset
1602 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1603 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1604
a61af66fc99e Initial load
duke
parents:
diff changeset
1605 //------------------------------succ_prob-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1606 // Determine the probability of reaching successor 'i' from the receiver block.
a61af66fc99e Initial load
duke
parents:
diff changeset
1607 float Block::succ_prob(uint i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1608 int eidx = end_idx();
a61af66fc99e Initial load
duke
parents:
diff changeset
1609 Node *n = _nodes[eidx]; // Get ending Node
a61af66fc99e Initial load
duke
parents:
diff changeset
1610 int op = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : n->Opcode();
a61af66fc99e Initial load
duke
parents:
diff changeset
1611
a61af66fc99e Initial load
duke
parents:
diff changeset
1612 // Switch on branch type
a61af66fc99e Initial load
duke
parents:
diff changeset
1613 switch( op ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1614 case Op_CountedLoopEnd:
a61af66fc99e Initial load
duke
parents:
diff changeset
1615 case Op_If: {
a61af66fc99e Initial load
duke
parents:
diff changeset
1616 assert (i < 2, "just checking");
a61af66fc99e Initial load
duke
parents:
diff changeset
1617 // Conditionals pass on only part of their frequency
a61af66fc99e Initial load
duke
parents:
diff changeset
1618 float prob = n->as_MachIf()->_prob;
a61af66fc99e Initial load
duke
parents:
diff changeset
1619 assert(prob >= 0.0 && prob <= 1.0, "out of range probability");
a61af66fc99e Initial load
duke
parents:
diff changeset
1620 // If succ[i] is the FALSE branch, invert path info
a61af66fc99e Initial load
duke
parents:
diff changeset
1621 if( _nodes[i + eidx + 1]->Opcode() == Op_IfFalse ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1622 return 1.0f - prob; // not taken
a61af66fc99e Initial load
duke
parents:
diff changeset
1623 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1624 return prob; // taken
a61af66fc99e Initial load
duke
parents:
diff changeset
1625 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1626 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1627
a61af66fc99e Initial load
duke
parents:
diff changeset
1628 case Op_Jump:
a61af66fc99e Initial load
duke
parents:
diff changeset
1629 // Divide the frequency between all successors evenly
a61af66fc99e Initial load
duke
parents:
diff changeset
1630 return 1.0f/_num_succs;
a61af66fc99e Initial load
duke
parents:
diff changeset
1631
a61af66fc99e Initial load
duke
parents:
diff changeset
1632 case Op_Catch: {
a61af66fc99e Initial load
duke
parents:
diff changeset
1633 const CatchProjNode *ci = _nodes[i + eidx + 1]->as_CatchProj();
a61af66fc99e Initial load
duke
parents:
diff changeset
1634 if (ci->_con == CatchProjNode::fall_through_index) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1635 // Fall-thru path gets the lion's share.
a61af66fc99e Initial load
duke
parents:
diff changeset
1636 return 1.0f - PROB_UNLIKELY_MAG(5)*_num_succs;
a61af66fc99e Initial load
duke
parents:
diff changeset
1637 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1638 // Presume exceptional paths are equally unlikely
a61af66fc99e Initial load
duke
parents:
diff changeset
1639 return PROB_UNLIKELY_MAG(5);
a61af66fc99e Initial load
duke
parents:
diff changeset
1640 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1641 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1642
a61af66fc99e Initial load
duke
parents:
diff changeset
1643 case Op_Root:
a61af66fc99e Initial load
duke
parents:
diff changeset
1644 case Op_Goto:
a61af66fc99e Initial load
duke
parents:
diff changeset
1645 // Pass frequency straight thru to target
a61af66fc99e Initial load
duke
parents:
diff changeset
1646 return 1.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1647
a61af66fc99e Initial load
duke
parents:
diff changeset
1648 case Op_NeverBranch:
a61af66fc99e Initial load
duke
parents:
diff changeset
1649 return 0.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1650
a61af66fc99e Initial load
duke
parents:
diff changeset
1651 case Op_TailCall:
a61af66fc99e Initial load
duke
parents:
diff changeset
1652 case Op_TailJump:
a61af66fc99e Initial load
duke
parents:
diff changeset
1653 case Op_Return:
a61af66fc99e Initial load
duke
parents:
diff changeset
1654 case Op_Halt:
a61af66fc99e Initial load
duke
parents:
diff changeset
1655 case Op_Rethrow:
a61af66fc99e Initial load
duke
parents:
diff changeset
1656 // Do not push out freq to root block
a61af66fc99e Initial load
duke
parents:
diff changeset
1657 return 0.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1658
a61af66fc99e Initial load
duke
parents:
diff changeset
1659 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
1660 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
1661 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1662
a61af66fc99e Initial load
duke
parents:
diff changeset
1663 return 0.0f;
a61af66fc99e Initial load
duke
parents:
diff changeset
1664 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1665
a61af66fc99e Initial load
duke
parents:
diff changeset
1666 //------------------------------update_succ_freq-------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1667 // Update the appropriate frequency associated with block 'b', a succesor of
a61af66fc99e Initial load
duke
parents:
diff changeset
1668 // a block in this loop.
a61af66fc99e Initial load
duke
parents:
diff changeset
1669 void CFGLoop::update_succ_freq(Block* b, float freq) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1670 if (b->_loop == this) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1671 if (b == head()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1672 // back branch within the loop
a61af66fc99e Initial load
duke
parents:
diff changeset
1673 // Do nothing now, the loop carried frequency will be
a61af66fc99e Initial load
duke
parents:
diff changeset
1674 // adjust later in scale_freq().
a61af66fc99e Initial load
duke
parents:
diff changeset
1675 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1676 // simple branch within the loop
a61af66fc99e Initial load
duke
parents:
diff changeset
1677 b->_freq += freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1678 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1679 } else if (!in_loop_nest(b)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1680 // branch is exit from this loop
a61af66fc99e Initial load
duke
parents:
diff changeset
1681 BlockProbPair bpp(b, freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1682 _exits.append(bpp);
a61af66fc99e Initial load
duke
parents:
diff changeset
1683 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1684 // branch into nested loop
a61af66fc99e Initial load
duke
parents:
diff changeset
1685 CFGLoop* ch = b->_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1686 ch->_freq += freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1687 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1688 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1689
a61af66fc99e Initial load
duke
parents:
diff changeset
1690 //------------------------------in_loop_nest-----------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1691 // Determine if block b is in the receiver's loop nest.
a61af66fc99e Initial load
duke
parents:
diff changeset
1692 bool CFGLoop::in_loop_nest(Block* b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1693 int depth = _depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
1694 CFGLoop* b_loop = b->_loop;
a61af66fc99e Initial load
duke
parents:
diff changeset
1695 int b_depth = b_loop->_depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
1696 if (depth == b_depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1697 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
1698 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1699 while (b_depth > depth) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1700 b_loop = b_loop->_parent;
a61af66fc99e Initial load
duke
parents:
diff changeset
1701 b_depth = b_loop->_depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
1702 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1703 return b_loop == this;
a61af66fc99e Initial load
duke
parents:
diff changeset
1704 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1705
a61af66fc99e Initial load
duke
parents:
diff changeset
1706 //------------------------------scale_freq-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1707 // Scale frequency of loops and blocks by trip counts from outer loops
a61af66fc99e Initial load
duke
parents:
diff changeset
1708 // Do a top down traversal of loop tree (visit outer loops first.)
a61af66fc99e Initial load
duke
parents:
diff changeset
1709 void CFGLoop::scale_freq() {
a61af66fc99e Initial load
duke
parents:
diff changeset
1710 float loop_freq = _freq * trip_count();
a61af66fc99e Initial load
duke
parents:
diff changeset
1711 for (int i = 0; i < _members.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1712 CFGElement* s = _members.at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1713 s->_freq *= loop_freq;
a61af66fc99e Initial load
duke
parents:
diff changeset
1714 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1715 CFGLoop* ch = _child;
a61af66fc99e Initial load
duke
parents:
diff changeset
1716 while (ch != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1717 ch->scale_freq();
a61af66fc99e Initial load
duke
parents:
diff changeset
1718 ch = ch->_sibling;
a61af66fc99e Initial load
duke
parents:
diff changeset
1719 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1720 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1721
a61af66fc99e Initial load
duke
parents:
diff changeset
1722 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
1723 //------------------------------dump_tree--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1724 void CFGLoop::dump_tree() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
1725 dump();
a61af66fc99e Initial load
duke
parents:
diff changeset
1726 if (_child != NULL) _child->dump_tree();
a61af66fc99e Initial load
duke
parents:
diff changeset
1727 if (_sibling != NULL) _sibling->dump_tree();
a61af66fc99e Initial load
duke
parents:
diff changeset
1728 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1729
a61af66fc99e Initial load
duke
parents:
diff changeset
1730 //------------------------------dump-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
1731 void CFGLoop::dump() const {
a61af66fc99e Initial load
duke
parents:
diff changeset
1732 for (int i = 0; i < _depth; i++) tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1733 tty->print("%s: %d trip_count: %6.0f freq: %6.0f\n",
a61af66fc99e Initial load
duke
parents:
diff changeset
1734 _depth == 0 ? "Method" : "Loop", _id, trip_count(), _freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1735 for (int i = 0; i < _depth; i++) tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1736 tty->print(" members:", _id);
a61af66fc99e Initial load
duke
parents:
diff changeset
1737 int k = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1738 for (int i = 0; i < _members.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1739 if (k++ >= 6) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1740 tty->print("\n ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1741 for (int j = 0; j < _depth+1; j++) tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1742 k = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1743 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1744 CFGElement *s = _members.at(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
1745 if (s->is_block()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1746 Block *b = s->as_Block();
a61af66fc99e Initial load
duke
parents:
diff changeset
1747 tty->print(" B%d(%6.3f)", b->_pre_order, b->_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1748 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
1749 CFGLoop* lp = s->as_CFGLoop();
a61af66fc99e Initial load
duke
parents:
diff changeset
1750 tty->print(" L%d(%6.3f)", lp->_id, lp->_freq);
a61af66fc99e Initial load
duke
parents:
diff changeset
1751 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1752 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1753 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1754 for (int i = 0; i < _depth; i++) tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1755 tty->print(" exits: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1756 k = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1757 for (int i = 0; i < _exits.length(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1758 if (k++ >= 7) {
a61af66fc99e Initial load
duke
parents:
diff changeset
1759 tty->print("\n ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1760 for (int j = 0; j < _depth+1; j++) tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
1761 k = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
1762 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1763 Block *blk = _exits.at(i).get_target();
a61af66fc99e Initial load
duke
parents:
diff changeset
1764 float prob = _exits.at(i).get_prob();
a61af66fc99e Initial load
duke
parents:
diff changeset
1765 tty->print(" ->%d@%d%%", blk->_pre_order, (int)(prob*100));
a61af66fc99e Initial load
duke
parents:
diff changeset
1766 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1767 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
1768 }
a61af66fc99e Initial load
duke
parents:
diff changeset
1769 #endif