annotate src/share/vm/opto/domgraph.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 a61af66fc99e
children 98cb887364d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
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/_domgraph.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 //------------------------------Tarjan-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // A data structure that holds all the information needed to find dominators.
a61af66fc99e Initial load
duke
parents:
diff changeset
34 struct Tarjan {
a61af66fc99e Initial load
duke
parents:
diff changeset
35 Block *_block; // Basic block for this info
a61af66fc99e Initial load
duke
parents:
diff changeset
36
a61af66fc99e Initial load
duke
parents:
diff changeset
37 uint _semi; // Semi-dominators
a61af66fc99e Initial load
duke
parents:
diff changeset
38 uint _size; // Used for faster LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
39 Tarjan *_parent; // Parent in DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
40 Tarjan *_label; // Used for LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
41 Tarjan *_ancestor; // Used for LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
42 Tarjan *_child; // Used for faster LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
43 Tarjan *_dom; // Parent in dominator tree (immediate dom)
a61af66fc99e Initial load
duke
parents:
diff changeset
44 Tarjan *_bucket; // Set of vertices with given semidominator
a61af66fc99e Initial load
duke
parents:
diff changeset
45
a61af66fc99e Initial load
duke
parents:
diff changeset
46 Tarjan *_dom_child; // Child in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
47 Tarjan *_dom_next; // Next in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 // Fast union-find work
a61af66fc99e Initial load
duke
parents:
diff changeset
50 void COMPRESS();
a61af66fc99e Initial load
duke
parents:
diff changeset
51 Tarjan *EVAL(void);
a61af66fc99e Initial load
duke
parents:
diff changeset
52 void LINK( Tarjan *w, Tarjan *tarjan0 );
a61af66fc99e Initial load
duke
parents:
diff changeset
53
a61af66fc99e Initial load
duke
parents:
diff changeset
54 void setdepth( uint size );
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 //------------------------------Dominator--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
59 // Compute the dominator tree of the CFG. The CFG must already have been
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // constructed. This is the Lengauer & Tarjan O(E-alpha(E,V)) algorithm.
a61af66fc99e Initial load
duke
parents:
diff changeset
61 void PhaseCFG::Dominators( ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
62 // Pre-grow the blocks array, prior to the ResourceMark kicking in
a61af66fc99e Initial load
duke
parents:
diff changeset
63 _blocks.map(_num_blocks,0);
a61af66fc99e Initial load
duke
parents:
diff changeset
64
a61af66fc99e Initial load
duke
parents:
diff changeset
65 ResourceMark rm;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // Setup mappings from my Graph to Tarjan's stuff and back
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // Note: Tarjan uses 1-based arrays
a61af66fc99e Initial load
duke
parents:
diff changeset
68 Tarjan *tarjan = NEW_RESOURCE_ARRAY(Tarjan,_num_blocks+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
69
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // Tarjan's algorithm, almost verbatim:
a61af66fc99e Initial load
duke
parents:
diff changeset
71 // Step 1:
a61af66fc99e Initial load
duke
parents:
diff changeset
72 _rpo_ctr = _num_blocks;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 uint dfsnum = DFS( tarjan );
a61af66fc99e Initial load
duke
parents:
diff changeset
74 if( dfsnum-1 != _num_blocks ) {// Check for unreachable loops!
a61af66fc99e Initial load
duke
parents:
diff changeset
75 // If the returned dfsnum does not match the number of blocks, then we
a61af66fc99e Initial load
duke
parents:
diff changeset
76 // must have some unreachable loops. These can be made at any time by
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // IterGVN. They are cleaned up by CCP or the loop opts, but the last
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // IterGVN can always make more that are not cleaned up. Highly unlikely
a61af66fc99e Initial load
duke
parents:
diff changeset
79 // except in ZKM.jar, where endless irreducible loops cause the loop opts
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // to not get run.
a61af66fc99e Initial load
duke
parents:
diff changeset
81 //
a61af66fc99e Initial load
duke
parents:
diff changeset
82 // Having found unreachable loops, we have made a bad RPO _block layout.
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // We can re-run the above DFS pass with the correct number of blocks,
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // and hack the Tarjan algorithm below to be robust in the presence of
a61af66fc99e Initial load
duke
parents:
diff changeset
85 // such dead loops (as was done for the NTarjan code farther below).
a61af66fc99e Initial load
duke
parents:
diff changeset
86 // Since this situation is so unlikely, instead I've decided to bail out.
a61af66fc99e Initial load
duke
parents:
diff changeset
87 // CNC 7/24/2001
a61af66fc99e Initial load
duke
parents:
diff changeset
88 C->record_method_not_compilable("unreachable loop");
a61af66fc99e Initial load
duke
parents:
diff changeset
89 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
90 }
a61af66fc99e Initial load
duke
parents:
diff changeset
91 _blocks._cnt = _num_blocks;
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 // Tarjan is using 1-based arrays, so these are some initialize flags
a61af66fc99e Initial load
duke
parents:
diff changeset
94 tarjan[0]._size = tarjan[0]._semi = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
95 tarjan[0]._label = &tarjan[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 uint i;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 for( i=_num_blocks; i>=2; i-- ) { // For all vertices in DFS order
a61af66fc99e Initial load
duke
parents:
diff changeset
99 Tarjan *w = &tarjan[i]; // Get vertex from DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // Step 2:
a61af66fc99e Initial load
duke
parents:
diff changeset
102 Node *whead = w->_block->head();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 for( uint j=1; j < whead->req(); j++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 Block *b = _bbs[whead->in(j)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
105 Tarjan *vx = &tarjan[b->_pre_order];
a61af66fc99e Initial load
duke
parents:
diff changeset
106 Tarjan *u = vx->EVAL();
a61af66fc99e Initial load
duke
parents:
diff changeset
107 if( u->_semi < w->_semi )
a61af66fc99e Initial load
duke
parents:
diff changeset
108 w->_semi = u->_semi;
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 // w is added to a bucket here, and only here.
a61af66fc99e Initial load
duke
parents:
diff changeset
112 // Thus w is in at most one bucket and the sum of all bucket sizes is O(n).
a61af66fc99e Initial load
duke
parents:
diff changeset
113 // Thus bucket can be a linked list.
a61af66fc99e Initial load
duke
parents:
diff changeset
114 // Thus we do not need a small integer name for each Block.
a61af66fc99e Initial load
duke
parents:
diff changeset
115 w->_bucket = tarjan[w->_semi]._bucket;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 tarjan[w->_semi]._bucket = w;
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 w->_parent->LINK( w, &tarjan[0] );
a61af66fc99e Initial load
duke
parents:
diff changeset
119
a61af66fc99e Initial load
duke
parents:
diff changeset
120 // Step 3:
a61af66fc99e Initial load
duke
parents:
diff changeset
121 for( Tarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 Tarjan *u = vx->EVAL();
a61af66fc99e Initial load
duke
parents:
diff changeset
123 vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;
a61af66fc99e Initial load
duke
parents:
diff changeset
124 }
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // Step 4:
a61af66fc99e Initial load
duke
parents:
diff changeset
128 for( i=2; i <= _num_blocks; i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 Tarjan *w = &tarjan[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
130 if( w->_dom != &tarjan[w->_semi] )
a61af66fc99e Initial load
duke
parents:
diff changeset
131 w->_dom = w->_dom->_dom;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // No immediate dominator for the root
a61af66fc99e Initial load
duke
parents:
diff changeset
135 Tarjan *w = &tarjan[_broot->_pre_order];
a61af66fc99e Initial load
duke
parents:
diff changeset
136 w->_dom = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
137 w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
a61af66fc99e Initial load
duke
parents:
diff changeset
138
a61af66fc99e Initial load
duke
parents:
diff changeset
139 // Convert the dominator tree array into my kind of graph
a61af66fc99e Initial load
duke
parents:
diff changeset
140 for( i=1; i<=_num_blocks;i++){// For all Tarjan vertices
a61af66fc99e Initial load
duke
parents:
diff changeset
141 Tarjan *t = &tarjan[i]; // Handy access
a61af66fc99e Initial load
duke
parents:
diff changeset
142 Tarjan *tdom = t->_dom; // Handy access to immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
143 if( tdom ) { // Root has no immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
144 t->_block->_idom = tdom->_block; // Set immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
145 t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child
a61af66fc99e Initial load
duke
parents:
diff changeset
146 tdom->_dom_child = t; // Make me a child of my parent
a61af66fc99e Initial load
duke
parents:
diff changeset
147 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
148 t->_block->_idom = NULL; // Root
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150 w->setdepth( _num_blocks+1 ); // Set depth in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 }
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 //----------------------------Block_Stack--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
155 class Block_Stack {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
157 struct Block_Descr {
a61af66fc99e Initial load
duke
parents:
diff changeset
158 Block *block; // Block
a61af66fc99e Initial load
duke
parents:
diff changeset
159 int index; // Index of block's successor pushed on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
160 int freq_idx; // Index of block's most frequent successor
a61af66fc99e Initial load
duke
parents:
diff changeset
161 };
a61af66fc99e Initial load
duke
parents:
diff changeset
162 Block_Descr *_stack_top;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 Block_Descr *_stack_max;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 Block_Descr *_stack;
a61af66fc99e Initial load
duke
parents:
diff changeset
165 Tarjan *_tarjan;
a61af66fc99e Initial load
duke
parents:
diff changeset
166 uint most_frequent_successor( Block *b );
a61af66fc99e Initial load
duke
parents:
diff changeset
167 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
168 Block_Stack(Tarjan *tarjan, int size) : _tarjan(tarjan) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 _stack = NEW_RESOURCE_ARRAY(Block_Descr, size);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 _stack_max = _stack + size;
a61af66fc99e Initial load
duke
parents:
diff changeset
171 _stack_top = _stack - 1; // stack is empty
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173 void push(uint pre_order, Block *b) {
a61af66fc99e Initial load
duke
parents:
diff changeset
174 Tarjan *t = &_tarjan[pre_order]; // Fast local access
a61af66fc99e Initial load
duke
parents:
diff changeset
175 b->_pre_order = pre_order; // Flag as visited
a61af66fc99e Initial load
duke
parents:
diff changeset
176 t->_block = b; // Save actual block
a61af66fc99e Initial load
duke
parents:
diff changeset
177 t->_semi = pre_order; // Block to DFS map
a61af66fc99e Initial load
duke
parents:
diff changeset
178 t->_label = t; // DFS to vertex map
a61af66fc99e Initial load
duke
parents:
diff changeset
179 t->_ancestor = NULL; // Fast LINK & EVAL setup
a61af66fc99e Initial load
duke
parents:
diff changeset
180 t->_child = &_tarjan[0]; // Sentenial
a61af66fc99e Initial load
duke
parents:
diff changeset
181 t->_size = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
182 t->_bucket = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
183 if (pre_order == 1)
a61af66fc99e Initial load
duke
parents:
diff changeset
184 t->_parent = NULL; // first block doesn't have parent
a61af66fc99e Initial load
duke
parents:
diff changeset
185 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
186 // Save parent (currernt top block on stack) in DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
187 t->_parent = &_tarjan[_stack_top->block->_pre_order];
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // Now put this block on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
190 ++_stack_top;
a61af66fc99e Initial load
duke
parents:
diff changeset
191 assert(_stack_top < _stack_max, ""); // assert if stack have to grow
a61af66fc99e Initial load
duke
parents:
diff changeset
192 _stack_top->block = b;
a61af66fc99e Initial load
duke
parents:
diff changeset
193 _stack_top->index = -1;
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // Find the index into b->succs[] array of the most frequent successor.
a61af66fc99e Initial load
duke
parents:
diff changeset
195 _stack_top->freq_idx = most_frequent_successor(b); // freq_idx >= 0
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197 Block* pop() { Block* b = _stack_top->block; _stack_top--; return b; }
a61af66fc99e Initial load
duke
parents:
diff changeset
198 bool is_nonempty() { return (_stack_top >= _stack); }
a61af66fc99e Initial load
duke
parents:
diff changeset
199 bool last_successor() { return (_stack_top->index == _stack_top->freq_idx); }
a61af66fc99e Initial load
duke
parents:
diff changeset
200 Block* next_successor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 int i = _stack_top->index;
a61af66fc99e Initial load
duke
parents:
diff changeset
202 i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
203 if (i == _stack_top->freq_idx) i++;
a61af66fc99e Initial load
duke
parents:
diff changeset
204 if (i >= (int)(_stack_top->block->_num_succs)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
205 i = _stack_top->freq_idx; // process most frequent successor last
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207 _stack_top->index = i;
a61af66fc99e Initial load
duke
parents:
diff changeset
208 return _stack_top->block->_succs[ i ];
a61af66fc99e Initial load
duke
parents:
diff changeset
209 }
a61af66fc99e Initial load
duke
parents:
diff changeset
210 };
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 //-------------------------most_frequent_successor-----------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
213 // Find the index into the b->succs[] array of the most frequent successor.
a61af66fc99e Initial load
duke
parents:
diff changeset
214 uint Block_Stack::most_frequent_successor( Block *b ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
215 uint freq_idx = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
216 int eidx = b->end_idx();
a61af66fc99e Initial load
duke
parents:
diff changeset
217 Node *n = b->_nodes[eidx];
a61af66fc99e Initial load
duke
parents:
diff changeset
218 int op = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : n->Opcode();
a61af66fc99e Initial load
duke
parents:
diff changeset
219 switch( op ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
220 case Op_CountedLoopEnd:
a61af66fc99e Initial load
duke
parents:
diff changeset
221 case Op_If: { // Split frequency amongst children
a61af66fc99e Initial load
duke
parents:
diff changeset
222 float prob = n->as_MachIf()->_prob;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 // Is succ[0] the TRUE branch or the FALSE branch?
a61af66fc99e Initial load
duke
parents:
diff changeset
224 if( b->_nodes[eidx+1]->Opcode() == Op_IfFalse )
a61af66fc99e Initial load
duke
parents:
diff changeset
225 prob = 1.0f - prob;
a61af66fc99e Initial load
duke
parents:
diff changeset
226 freq_idx = prob < PROB_FAIR; // freq=1 for succ[0] < 0.5 prob
a61af66fc99e Initial load
duke
parents:
diff changeset
227 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
228 }
a61af66fc99e Initial load
duke
parents:
diff changeset
229 case Op_Catch: // Split frequency amongst children
a61af66fc99e Initial load
duke
parents:
diff changeset
230 for( freq_idx = 0; freq_idx < b->_num_succs; freq_idx++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
231 if( b->_nodes[eidx+1+freq_idx]->as_CatchProj()->_con == CatchProjNode::fall_through_index )
a61af66fc99e Initial load
duke
parents:
diff changeset
232 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
233 // Handle case of no fall-thru (e.g., check-cast MUST throw an exception)
a61af66fc99e Initial load
duke
parents:
diff changeset
234 if( freq_idx == b->_num_succs ) freq_idx = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
235 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
236 // Currently there is no support for finding out the most
a61af66fc99e Initial load
duke
parents:
diff changeset
237 // frequent successor for jumps, so lets just make it the first one
a61af66fc99e Initial load
duke
parents:
diff changeset
238 case Op_Jump:
a61af66fc99e Initial load
duke
parents:
diff changeset
239 case Op_Root:
a61af66fc99e Initial load
duke
parents:
diff changeset
240 case Op_Goto:
a61af66fc99e Initial load
duke
parents:
diff changeset
241 case Op_NeverBranch:
a61af66fc99e Initial load
duke
parents:
diff changeset
242 freq_idx = 0; // fall thru
a61af66fc99e Initial load
duke
parents:
diff changeset
243 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
244 case Op_TailCall:
a61af66fc99e Initial load
duke
parents:
diff changeset
245 case Op_TailJump:
a61af66fc99e Initial load
duke
parents:
diff changeset
246 case Op_Return:
a61af66fc99e Initial load
duke
parents:
diff changeset
247 case Op_Halt:
a61af66fc99e Initial load
duke
parents:
diff changeset
248 case Op_Rethrow:
a61af66fc99e Initial load
duke
parents:
diff changeset
249 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
250 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
251 ShouldNotReachHere();
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253 return freq_idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
254 }
a61af66fc99e Initial load
duke
parents:
diff changeset
255
a61af66fc99e Initial load
duke
parents:
diff changeset
256 //------------------------------DFS--------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
257 // Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup
a61af66fc99e Initial load
duke
parents:
diff changeset
258 // 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.
a61af66fc99e Initial load
duke
parents:
diff changeset
259 uint PhaseCFG::DFS( Tarjan *tarjan ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 Block *b = _broot;
a61af66fc99e Initial load
duke
parents:
diff changeset
261 uint pre_order = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
262 // Allocate stack of size _num_blocks+1 to avoid frequent realloc
a61af66fc99e Initial load
duke
parents:
diff changeset
263 Block_Stack bstack(tarjan, _num_blocks+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
264
a61af66fc99e Initial load
duke
parents:
diff changeset
265 // Push on stack the state for the first block
a61af66fc99e Initial load
duke
parents:
diff changeset
266 bstack.push(pre_order, b);
a61af66fc99e Initial load
duke
parents:
diff changeset
267 ++pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
268
a61af66fc99e Initial load
duke
parents:
diff changeset
269 while (bstack.is_nonempty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
270 if (!bstack.last_successor()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
271 // Walk over all successors in pre-order (DFS).
a61af66fc99e Initial load
duke
parents:
diff changeset
272 Block *s = bstack.next_successor();
a61af66fc99e Initial load
duke
parents:
diff changeset
273 if (s->_pre_order == 0) { // Check for no-pre-order, not-visited
a61af66fc99e Initial load
duke
parents:
diff changeset
274 // Push on stack the state of successor
a61af66fc99e Initial load
duke
parents:
diff changeset
275 bstack.push(pre_order, s);
a61af66fc99e Initial load
duke
parents:
diff changeset
276 ++pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
277 }
a61af66fc99e Initial load
duke
parents:
diff changeset
278 }
a61af66fc99e Initial load
duke
parents:
diff changeset
279 else {
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // Build a reverse post-order in the CFG _blocks array
a61af66fc99e Initial load
duke
parents:
diff changeset
281 Block *stack_top = bstack.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
282 stack_top->_rpo = --_rpo_ctr;
a61af66fc99e Initial load
duke
parents:
diff changeset
283 _blocks.map(stack_top->_rpo, stack_top);
a61af66fc99e Initial load
duke
parents:
diff changeset
284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
285 }
a61af66fc99e Initial load
duke
parents:
diff changeset
286 return pre_order;
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }
a61af66fc99e Initial load
duke
parents:
diff changeset
288
a61af66fc99e Initial load
duke
parents:
diff changeset
289 //------------------------------COMPRESS---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
290 void Tarjan::COMPRESS()
a61af66fc99e Initial load
duke
parents:
diff changeset
291 {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 assert( _ancestor != 0, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if( _ancestor->_ancestor != 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 _ancestor->COMPRESS( );
a61af66fc99e Initial load
duke
parents:
diff changeset
295 if( _ancestor->_label->_semi < _label->_semi )
a61af66fc99e Initial load
duke
parents:
diff changeset
296 _label = _ancestor->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
297 _ancestor = _ancestor->_ancestor;
a61af66fc99e Initial load
duke
parents:
diff changeset
298 }
a61af66fc99e Initial load
duke
parents:
diff changeset
299 }
a61af66fc99e Initial load
duke
parents:
diff changeset
300
a61af66fc99e Initial load
duke
parents:
diff changeset
301 //------------------------------EVAL-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
302 Tarjan *Tarjan::EVAL() {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 if( !_ancestor ) return _label;
a61af66fc99e Initial load
duke
parents:
diff changeset
304 COMPRESS();
a61af66fc99e Initial load
duke
parents:
diff changeset
305 return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
306 }
a61af66fc99e Initial load
duke
parents:
diff changeset
307
a61af66fc99e Initial load
duke
parents:
diff changeset
308 //------------------------------LINK-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
309 void Tarjan::LINK( Tarjan *w, Tarjan *tarjan0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
310 Tarjan *s = w;
a61af66fc99e Initial load
duke
parents:
diff changeset
311 while( w->_label->_semi < s->_child->_label->_semi ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
312 if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 s->_child->_ancestor = s;
a61af66fc99e Initial load
duke
parents:
diff changeset
314 s->_child = s->_child->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
315 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
316 s->_child->_size = s->_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
317 s = s->_ancestor = s->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320 s->_label = w->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
321 _size += w->_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 if( _size < (w->_size << 1) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
323 Tarjan *tmp = s; s = _child; _child = tmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
324 }
a61af66fc99e Initial load
duke
parents:
diff changeset
325 while( s != tarjan0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
326 s->_ancestor = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
327 s = s->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
328 }
a61af66fc99e Initial load
duke
parents:
diff changeset
329 }
a61af66fc99e Initial load
duke
parents:
diff changeset
330
a61af66fc99e Initial load
duke
parents:
diff changeset
331 //------------------------------setdepth---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
332 void Tarjan::setdepth( uint stack_size ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
333 Tarjan **top = NEW_RESOURCE_ARRAY(Tarjan*, stack_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
334 Tarjan **next = top;
a61af66fc99e Initial load
duke
parents:
diff changeset
335 Tarjan **last;
a61af66fc99e Initial load
duke
parents:
diff changeset
336 uint depth = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
337 *top = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
338 ++top;
a61af66fc99e Initial load
duke
parents:
diff changeset
339 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
340 // next level
a61af66fc99e Initial load
duke
parents:
diff changeset
341 ++depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
342 last = top;
a61af66fc99e Initial load
duke
parents:
diff changeset
343 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
344 // Set current depth for all tarjans on this level
a61af66fc99e Initial load
duke
parents:
diff changeset
345 Tarjan *t = *next; // next tarjan from stack
a61af66fc99e Initial load
duke
parents:
diff changeset
346 ++next;
a61af66fc99e Initial load
duke
parents:
diff changeset
347 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
348 t->_block->_dom_depth = depth; // Set depth in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
349 Tarjan *dom_child = t->_dom_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
350 t = t->_dom_next; // next tarjan
a61af66fc99e Initial load
duke
parents:
diff changeset
351 if (dom_child != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
352 *top = dom_child; // save child on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
353 ++top;
a61af66fc99e Initial load
duke
parents:
diff changeset
354 }
a61af66fc99e Initial load
duke
parents:
diff changeset
355 } while (t != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
356 } while (next < last);
a61af66fc99e Initial load
duke
parents:
diff changeset
357 } while (last < top);
a61af66fc99e Initial load
duke
parents:
diff changeset
358 }
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 //*********************** DOMINATORS ON THE SEA OF NODES***********************
a61af66fc99e Initial load
duke
parents:
diff changeset
361 //------------------------------NTarjan----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // A data structure that holds all the information needed to find dominators.
a61af66fc99e Initial load
duke
parents:
diff changeset
363 struct NTarjan {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 Node *_control; // Control node associated with this info
a61af66fc99e Initial load
duke
parents:
diff changeset
365
a61af66fc99e Initial load
duke
parents:
diff changeset
366 uint _semi; // Semi-dominators
a61af66fc99e Initial load
duke
parents:
diff changeset
367 uint _size; // Used for faster LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
368 NTarjan *_parent; // Parent in DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
369 NTarjan *_label; // Used for LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
370 NTarjan *_ancestor; // Used for LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
371 NTarjan *_child; // Used for faster LINK and EVAL
a61af66fc99e Initial load
duke
parents:
diff changeset
372 NTarjan *_dom; // Parent in dominator tree (immediate dom)
a61af66fc99e Initial load
duke
parents:
diff changeset
373 NTarjan *_bucket; // Set of vertices with given semidominator
a61af66fc99e Initial load
duke
parents:
diff changeset
374
a61af66fc99e Initial load
duke
parents:
diff changeset
375 NTarjan *_dom_child; // Child in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
376 NTarjan *_dom_next; // Next in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
377
a61af66fc99e Initial load
duke
parents:
diff changeset
378 // Perform DFS search.
a61af66fc99e Initial load
duke
parents:
diff changeset
379 // Setup 'vertex' as DFS to vertex mapping.
a61af66fc99e Initial load
duke
parents:
diff changeset
380 // Setup 'semi' as vertex to DFS mapping.
a61af66fc99e Initial load
duke
parents:
diff changeset
381 // Set 'parent' to DFS parent.
a61af66fc99e Initial load
duke
parents:
diff changeset
382 static int DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder );
a61af66fc99e Initial load
duke
parents:
diff changeset
383 void setdepth( uint size, uint *dom_depth );
a61af66fc99e Initial load
duke
parents:
diff changeset
384
a61af66fc99e Initial load
duke
parents:
diff changeset
385 // Fast union-find work
a61af66fc99e Initial load
duke
parents:
diff changeset
386 void COMPRESS();
a61af66fc99e Initial load
duke
parents:
diff changeset
387 NTarjan *EVAL(void);
a61af66fc99e Initial load
duke
parents:
diff changeset
388 void LINK( NTarjan *w, NTarjan *ntarjan0 );
a61af66fc99e Initial load
duke
parents:
diff changeset
389 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
390 void dump(int offset) const;
a61af66fc99e Initial load
duke
parents:
diff changeset
391 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
392 };
a61af66fc99e Initial load
duke
parents:
diff changeset
393
a61af66fc99e Initial load
duke
parents:
diff changeset
394 //------------------------------Dominator--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
395 // Compute the dominator tree of the sea of nodes. This version walks all CFG
a61af66fc99e Initial load
duke
parents:
diff changeset
396 // nodes (using the is_CFG() call) and places them in a dominator tree. Thus,
a61af66fc99e Initial load
duke
parents:
diff changeset
397 // it needs a count of the CFG nodes for the mapping table. This is the
a61af66fc99e Initial load
duke
parents:
diff changeset
398 // Lengauer & Tarjan O(E-alpha(E,V)) algorithm.
a61af66fc99e Initial load
duke
parents:
diff changeset
399 void PhaseIdealLoop::Dominators( ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
400 ResourceMark rm;
a61af66fc99e Initial load
duke
parents:
diff changeset
401 // Setup mappings from my Graph to Tarjan's stuff and back
a61af66fc99e Initial load
duke
parents:
diff changeset
402 // Note: Tarjan uses 1-based arrays
a61af66fc99e Initial load
duke
parents:
diff changeset
403 NTarjan *ntarjan = NEW_RESOURCE_ARRAY(NTarjan,C->unique()+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
404 // Initialize _control field for fast reference
a61af66fc99e Initial load
duke
parents:
diff changeset
405 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
406 for( i= C->unique()-1; i>=0; i-- )
a61af66fc99e Initial load
duke
parents:
diff changeset
407 ntarjan[i]._control = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
408
a61af66fc99e Initial load
duke
parents:
diff changeset
409 // Store the DFS order for the main loop
a61af66fc99e Initial load
duke
parents:
diff changeset
410 uint *dfsorder = NEW_RESOURCE_ARRAY(uint,C->unique()+1);
a61af66fc99e Initial load
duke
parents:
diff changeset
411 memset(dfsorder, max_uint, (C->unique()+1) * sizeof(uint));
a61af66fc99e Initial load
duke
parents:
diff changeset
412
a61af66fc99e Initial load
duke
parents:
diff changeset
413 // Tarjan's algorithm, almost verbatim:
a61af66fc99e Initial load
duke
parents:
diff changeset
414 // Step 1:
a61af66fc99e Initial load
duke
parents:
diff changeset
415 VectorSet visited(Thread::current()->resource_area());
a61af66fc99e Initial load
duke
parents:
diff changeset
416 int dfsnum = NTarjan::DFS( ntarjan, visited, this, dfsorder);
a61af66fc99e Initial load
duke
parents:
diff changeset
417
a61af66fc99e Initial load
duke
parents:
diff changeset
418 // Tarjan is using 1-based arrays, so these are some initialize flags
a61af66fc99e Initial load
duke
parents:
diff changeset
419 ntarjan[0]._size = ntarjan[0]._semi = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
420 ntarjan[0]._label = &ntarjan[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
421
a61af66fc99e Initial load
duke
parents:
diff changeset
422 for( i = dfsnum-1; i>1; i-- ) { // For all nodes in reverse DFS order
a61af66fc99e Initial load
duke
parents:
diff changeset
423 NTarjan *w = &ntarjan[i]; // Get Node from DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
424 assert(w->_control != NULL,"bad DFS walk");
a61af66fc99e Initial load
duke
parents:
diff changeset
425
a61af66fc99e Initial load
duke
parents:
diff changeset
426 // Step 2:
a61af66fc99e Initial load
duke
parents:
diff changeset
427 Node *whead = w->_control;
a61af66fc99e Initial load
duke
parents:
diff changeset
428 for( uint j=0; j < whead->req(); j++ ) { // For each predecessor
a61af66fc99e Initial load
duke
parents:
diff changeset
429 if( whead->in(j) == NULL || !whead->in(j)->is_CFG() )
a61af66fc99e Initial load
duke
parents:
diff changeset
430 continue; // Only process control nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
431 uint b = dfsorder[whead->in(j)->_idx];
a61af66fc99e Initial load
duke
parents:
diff changeset
432 if(b == max_uint) continue;
a61af66fc99e Initial load
duke
parents:
diff changeset
433 NTarjan *vx = &ntarjan[b];
a61af66fc99e Initial load
duke
parents:
diff changeset
434 NTarjan *u = vx->EVAL();
a61af66fc99e Initial load
duke
parents:
diff changeset
435 if( u->_semi < w->_semi )
a61af66fc99e Initial load
duke
parents:
diff changeset
436 w->_semi = u->_semi;
a61af66fc99e Initial load
duke
parents:
diff changeset
437 }
a61af66fc99e Initial load
duke
parents:
diff changeset
438
a61af66fc99e Initial load
duke
parents:
diff changeset
439 // w is added to a bucket here, and only here.
a61af66fc99e Initial load
duke
parents:
diff changeset
440 // Thus w is in at most one bucket and the sum of all bucket sizes is O(n).
a61af66fc99e Initial load
duke
parents:
diff changeset
441 // Thus bucket can be a linked list.
a61af66fc99e Initial load
duke
parents:
diff changeset
442 w->_bucket = ntarjan[w->_semi]._bucket;
a61af66fc99e Initial load
duke
parents:
diff changeset
443 ntarjan[w->_semi]._bucket = w;
a61af66fc99e Initial load
duke
parents:
diff changeset
444
a61af66fc99e Initial load
duke
parents:
diff changeset
445 w->_parent->LINK( w, &ntarjan[0] );
a61af66fc99e Initial load
duke
parents:
diff changeset
446
a61af66fc99e Initial load
duke
parents:
diff changeset
447 // Step 3:
a61af66fc99e Initial load
duke
parents:
diff changeset
448 for( NTarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
449 NTarjan *u = vx->EVAL();
a61af66fc99e Initial load
duke
parents:
diff changeset
450 vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;
a61af66fc99e Initial load
duke
parents:
diff changeset
451 }
a61af66fc99e Initial load
duke
parents:
diff changeset
452
a61af66fc99e Initial load
duke
parents:
diff changeset
453 // Cleanup any unreachable loops now. Unreachable loops are loops that
a61af66fc99e Initial load
duke
parents:
diff changeset
454 // flow into the main graph (and hence into ROOT) but are not reachable
a61af66fc99e Initial load
duke
parents:
diff changeset
455 // from above. Such code is dead, but requires a global pass to detect
a61af66fc99e Initial load
duke
parents:
diff changeset
456 // it; this global pass was the 'build_loop_tree' pass run just prior.
a61af66fc99e Initial load
duke
parents:
diff changeset
457 if( whead->is_Region() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
458 for( uint i = 1; i < whead->req(); i++ ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
459 if (!has_node(whead->in(i))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
460 // Kill dead input path
a61af66fc99e Initial load
duke
parents:
diff changeset
461 assert( !visited.test(whead->in(i)->_idx),
a61af66fc99e Initial load
duke
parents:
diff changeset
462 "input with no loop must be dead" );
a61af66fc99e Initial load
duke
parents:
diff changeset
463 _igvn.hash_delete(whead);
a61af66fc99e Initial load
duke
parents:
diff changeset
464 whead->del_req(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
465 _igvn._worklist.push(whead);
a61af66fc99e Initial load
duke
parents:
diff changeset
466 for (DUIterator_Fast jmax, j = whead->fast_outs(jmax); j < jmax; j++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
467 Node* p = whead->fast_out(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
468 if( p->is_Phi() ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
469 _igvn.hash_delete(p);
a61af66fc99e Initial load
duke
parents:
diff changeset
470 p->del_req(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
471 _igvn._worklist.push(p);
a61af66fc99e Initial load
duke
parents:
diff changeset
472 }
a61af66fc99e Initial load
duke
parents:
diff changeset
473 }
a61af66fc99e Initial load
duke
parents:
diff changeset
474 i--; // Rerun same iteration
a61af66fc99e Initial load
duke
parents:
diff changeset
475 } // End of if dead input path
a61af66fc99e Initial load
duke
parents:
diff changeset
476 } // End of for all input paths
a61af66fc99e Initial load
duke
parents:
diff changeset
477 } // End if if whead is a Region
a61af66fc99e Initial load
duke
parents:
diff changeset
478 } // End of for all Nodes in reverse DFS order
a61af66fc99e Initial load
duke
parents:
diff changeset
479
a61af66fc99e Initial load
duke
parents:
diff changeset
480 // Step 4:
a61af66fc99e Initial load
duke
parents:
diff changeset
481 for( i=2; i < dfsnum; i++ ) { // DFS order
a61af66fc99e Initial load
duke
parents:
diff changeset
482 NTarjan *w = &ntarjan[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
483 assert(w->_control != NULL,"Bad DFS walk");
a61af66fc99e Initial load
duke
parents:
diff changeset
484 if( w->_dom != &ntarjan[w->_semi] )
a61af66fc99e Initial load
duke
parents:
diff changeset
485 w->_dom = w->_dom->_dom;
a61af66fc99e Initial load
duke
parents:
diff changeset
486 w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
a61af66fc99e Initial load
duke
parents:
diff changeset
487 }
a61af66fc99e Initial load
duke
parents:
diff changeset
488 // No immediate dominator for the root
a61af66fc99e Initial load
duke
parents:
diff changeset
489 NTarjan *w = &ntarjan[dfsorder[C->root()->_idx]];
a61af66fc99e Initial load
duke
parents:
diff changeset
490 w->_dom = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
491 w->_parent = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
492 w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
a61af66fc99e Initial load
duke
parents:
diff changeset
493
a61af66fc99e Initial load
duke
parents:
diff changeset
494 // Convert the dominator tree array into my kind of graph
a61af66fc99e Initial load
duke
parents:
diff changeset
495 for( i=1; i<dfsnum; i++ ) { // For all Tarjan vertices
a61af66fc99e Initial load
duke
parents:
diff changeset
496 NTarjan *t = &ntarjan[i]; // Handy access
a61af66fc99e Initial load
duke
parents:
diff changeset
497 assert(t->_control != NULL,"Bad DFS walk");
a61af66fc99e Initial load
duke
parents:
diff changeset
498 NTarjan *tdom = t->_dom; // Handy access to immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
499 if( tdom ) { // Root has no immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
500 _idom[t->_control->_idx] = tdom->_control; // Set immediate dominator
a61af66fc99e Initial load
duke
parents:
diff changeset
501 t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child
a61af66fc99e Initial load
duke
parents:
diff changeset
502 tdom->_dom_child = t; // Make me a child of my parent
a61af66fc99e Initial load
duke
parents:
diff changeset
503 } else
a61af66fc99e Initial load
duke
parents:
diff changeset
504 _idom[C->root()->_idx] = NULL; // Root
a61af66fc99e Initial load
duke
parents:
diff changeset
505 }
a61af66fc99e Initial load
duke
parents:
diff changeset
506 w->setdepth( C->unique()+1, _dom_depth ); // Set depth in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
507 // Pick up the 'top' node as well
a61af66fc99e Initial load
duke
parents:
diff changeset
508 _idom [C->top()->_idx] = C->root();
a61af66fc99e Initial load
duke
parents:
diff changeset
509 _dom_depth[C->top()->_idx] = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
510
a61af66fc99e Initial load
duke
parents:
diff changeset
511 // Debug Print of Dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
512 if( PrintDominators ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
513 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
514 w->dump(0);
a61af66fc99e Initial load
duke
parents:
diff changeset
515 #endif
a61af66fc99e Initial load
duke
parents:
diff changeset
516 }
a61af66fc99e Initial load
duke
parents:
diff changeset
517 }
a61af66fc99e Initial load
duke
parents:
diff changeset
518
a61af66fc99e Initial load
duke
parents:
diff changeset
519 //------------------------------DFS--------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
520 // Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup
a61af66fc99e Initial load
duke
parents:
diff changeset
521 // 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.
a61af66fc99e Initial load
duke
parents:
diff changeset
522 int NTarjan::DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder) {
a61af66fc99e Initial load
duke
parents:
diff changeset
523 // Allocate stack of size C->unique()/8 to avoid frequent realloc
a61af66fc99e Initial load
duke
parents:
diff changeset
524 GrowableArray <Node *> dfstack(pil->C->unique() >> 3);
a61af66fc99e Initial load
duke
parents:
diff changeset
525 Node *b = pil->C->root();
a61af66fc99e Initial load
duke
parents:
diff changeset
526 int dfsnum = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
527 dfsorder[b->_idx] = dfsnum; // Cache parent's dfsnum for a later use
a61af66fc99e Initial load
duke
parents:
diff changeset
528 dfstack.push(b);
a61af66fc99e Initial load
duke
parents:
diff changeset
529
a61af66fc99e Initial load
duke
parents:
diff changeset
530 while (dfstack.is_nonempty()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
531 b = dfstack.pop();
a61af66fc99e Initial load
duke
parents:
diff changeset
532 if( !visited.test_set(b->_idx) ) { // Test node and flag it as visited
a61af66fc99e Initial load
duke
parents:
diff changeset
533 NTarjan *w = &ntarjan[dfsnum];
a61af66fc99e Initial load
duke
parents:
diff changeset
534 // Only fully process control nodes
a61af66fc99e Initial load
duke
parents:
diff changeset
535 w->_control = b; // Save actual node
a61af66fc99e Initial load
duke
parents:
diff changeset
536 // Use parent's cached dfsnum to identify "Parent in DFS"
a61af66fc99e Initial load
duke
parents:
diff changeset
537 w->_parent = &ntarjan[dfsorder[b->_idx]];
a61af66fc99e Initial load
duke
parents:
diff changeset
538 dfsorder[b->_idx] = dfsnum; // Save DFS order info
a61af66fc99e Initial load
duke
parents:
diff changeset
539 w->_semi = dfsnum; // Node to DFS map
a61af66fc99e Initial load
duke
parents:
diff changeset
540 w->_label = w; // DFS to vertex map
a61af66fc99e Initial load
duke
parents:
diff changeset
541 w->_ancestor = NULL; // Fast LINK & EVAL setup
a61af66fc99e Initial load
duke
parents:
diff changeset
542 w->_child = &ntarjan[0]; // Sentinal
a61af66fc99e Initial load
duke
parents:
diff changeset
543 w->_size = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
544 w->_bucket = NULL;
a61af66fc99e Initial load
duke
parents:
diff changeset
545
a61af66fc99e Initial load
duke
parents:
diff changeset
546 // Need DEF-USE info for this pass
a61af66fc99e Initial load
duke
parents:
diff changeset
547 for ( int i = b->outcnt(); i-- > 0; ) { // Put on stack backwards
a61af66fc99e Initial load
duke
parents:
diff changeset
548 Node* s = b->raw_out(i); // Get a use
a61af66fc99e Initial load
duke
parents:
diff changeset
549 // CFG nodes only and not dead stuff
a61af66fc99e Initial load
duke
parents:
diff changeset
550 if( s->is_CFG() && pil->has_node(s) && !visited.test(s->_idx) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
551 dfsorder[s->_idx] = dfsnum; // Cache parent's dfsnum for a later use
a61af66fc99e Initial load
duke
parents:
diff changeset
552 dfstack.push(s);
a61af66fc99e Initial load
duke
parents:
diff changeset
553 }
a61af66fc99e Initial load
duke
parents:
diff changeset
554 }
a61af66fc99e Initial load
duke
parents:
diff changeset
555 dfsnum++; // update after parent's dfsnum has been cached.
a61af66fc99e Initial load
duke
parents:
diff changeset
556 }
a61af66fc99e Initial load
duke
parents:
diff changeset
557 }
a61af66fc99e Initial load
duke
parents:
diff changeset
558
a61af66fc99e Initial load
duke
parents:
diff changeset
559 return dfsnum;
a61af66fc99e Initial load
duke
parents:
diff changeset
560 }
a61af66fc99e Initial load
duke
parents:
diff changeset
561
a61af66fc99e Initial load
duke
parents:
diff changeset
562 //------------------------------COMPRESS---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
563 void NTarjan::COMPRESS()
a61af66fc99e Initial load
duke
parents:
diff changeset
564 {
a61af66fc99e Initial load
duke
parents:
diff changeset
565 assert( _ancestor != 0, "" );
a61af66fc99e Initial load
duke
parents:
diff changeset
566 if( _ancestor->_ancestor != 0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
567 _ancestor->COMPRESS( );
a61af66fc99e Initial load
duke
parents:
diff changeset
568 if( _ancestor->_label->_semi < _label->_semi )
a61af66fc99e Initial load
duke
parents:
diff changeset
569 _label = _ancestor->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
570 _ancestor = _ancestor->_ancestor;
a61af66fc99e Initial load
duke
parents:
diff changeset
571 }
a61af66fc99e Initial load
duke
parents:
diff changeset
572 }
a61af66fc99e Initial load
duke
parents:
diff changeset
573
a61af66fc99e Initial load
duke
parents:
diff changeset
574 //------------------------------EVAL-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
575 NTarjan *NTarjan::EVAL() {
a61af66fc99e Initial load
duke
parents:
diff changeset
576 if( !_ancestor ) return _label;
a61af66fc99e Initial load
duke
parents:
diff changeset
577 COMPRESS();
a61af66fc99e Initial load
duke
parents:
diff changeset
578 return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
579 }
a61af66fc99e Initial load
duke
parents:
diff changeset
580
a61af66fc99e Initial load
duke
parents:
diff changeset
581 //------------------------------LINK-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
582 void NTarjan::LINK( NTarjan *w, NTarjan *ntarjan0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
583 NTarjan *s = w;
a61af66fc99e Initial load
duke
parents:
diff changeset
584 while( w->_label->_semi < s->_child->_label->_semi ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
585 if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
586 s->_child->_ancestor = s;
a61af66fc99e Initial load
duke
parents:
diff changeset
587 s->_child = s->_child->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
588 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
589 s->_child->_size = s->_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
590 s = s->_ancestor = s->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
591 }
a61af66fc99e Initial load
duke
parents:
diff changeset
592 }
a61af66fc99e Initial load
duke
parents:
diff changeset
593 s->_label = w->_label;
a61af66fc99e Initial load
duke
parents:
diff changeset
594 _size += w->_size;
a61af66fc99e Initial load
duke
parents:
diff changeset
595 if( _size < (w->_size << 1) ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
596 NTarjan *tmp = s; s = _child; _child = tmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
597 }
a61af66fc99e Initial load
duke
parents:
diff changeset
598 while( s != ntarjan0 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
599 s->_ancestor = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
600 s = s->_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
601 }
a61af66fc99e Initial load
duke
parents:
diff changeset
602 }
a61af66fc99e Initial load
duke
parents:
diff changeset
603
a61af66fc99e Initial load
duke
parents:
diff changeset
604 //------------------------------setdepth---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
605 void NTarjan::setdepth( uint stack_size, uint *dom_depth ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
606 NTarjan **top = NEW_RESOURCE_ARRAY(NTarjan*, stack_size);
a61af66fc99e Initial load
duke
parents:
diff changeset
607 NTarjan **next = top;
a61af66fc99e Initial load
duke
parents:
diff changeset
608 NTarjan **last;
a61af66fc99e Initial load
duke
parents:
diff changeset
609 uint depth = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
610 *top = this;
a61af66fc99e Initial load
duke
parents:
diff changeset
611 ++top;
a61af66fc99e Initial load
duke
parents:
diff changeset
612 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
613 // next level
a61af66fc99e Initial load
duke
parents:
diff changeset
614 ++depth;
a61af66fc99e Initial load
duke
parents:
diff changeset
615 last = top;
a61af66fc99e Initial load
duke
parents:
diff changeset
616 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
617 // Set current depth for all tarjans on this level
a61af66fc99e Initial load
duke
parents:
diff changeset
618 NTarjan *t = *next; // next tarjan from stack
a61af66fc99e Initial load
duke
parents:
diff changeset
619 ++next;
a61af66fc99e Initial load
duke
parents:
diff changeset
620 do {
a61af66fc99e Initial load
duke
parents:
diff changeset
621 dom_depth[t->_control->_idx] = depth; // Set depth in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
622 NTarjan *dom_child = t->_dom_child;
a61af66fc99e Initial load
duke
parents:
diff changeset
623 t = t->_dom_next; // next tarjan
a61af66fc99e Initial load
duke
parents:
diff changeset
624 if (dom_child != NULL) {
a61af66fc99e Initial load
duke
parents:
diff changeset
625 *top = dom_child; // save child on stack
a61af66fc99e Initial load
duke
parents:
diff changeset
626 ++top;
a61af66fc99e Initial load
duke
parents:
diff changeset
627 }
a61af66fc99e Initial load
duke
parents:
diff changeset
628 } while (t != NULL);
a61af66fc99e Initial load
duke
parents:
diff changeset
629 } while (next < last);
a61af66fc99e Initial load
duke
parents:
diff changeset
630 } while (last < top);
a61af66fc99e Initial load
duke
parents:
diff changeset
631 }
a61af66fc99e Initial load
duke
parents:
diff changeset
632
a61af66fc99e Initial load
duke
parents:
diff changeset
633 //------------------------------dump-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
634 #ifndef PRODUCT
a61af66fc99e Initial load
duke
parents:
diff changeset
635 void NTarjan::dump(int offset) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
636 // Dump the data from this node
a61af66fc99e Initial load
duke
parents:
diff changeset
637 int i;
a61af66fc99e Initial load
duke
parents:
diff changeset
638 for(i = offset; i >0; i--) // Use indenting for tree structure
a61af66fc99e Initial load
duke
parents:
diff changeset
639 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
640 tty->print("Dominator Node: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
641 _control->dump(); // Control node for this dom node
a61af66fc99e Initial load
duke
parents:
diff changeset
642 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
643 for(i = offset; i >0; i--) // Use indenting for tree structure
a61af66fc99e Initial load
duke
parents:
diff changeset
644 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
645 tty->print("semi:%d, size:%d\n",_semi, _size);
a61af66fc99e Initial load
duke
parents:
diff changeset
646 for(i = offset; i >0; i--) // Use indenting for tree structure
a61af66fc99e Initial load
duke
parents:
diff changeset
647 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
648 tty->print("DFS Parent: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
649 if(_parent != NULL)
a61af66fc99e Initial load
duke
parents:
diff changeset
650 _parent->_control->dump(); // Parent in DFS
a61af66fc99e Initial load
duke
parents:
diff changeset
651 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
652 for(i = offset; i >0; i--) // Use indenting for tree structure
a61af66fc99e Initial load
duke
parents:
diff changeset
653 tty->print(" ");
a61af66fc99e Initial load
duke
parents:
diff changeset
654 tty->print("Dom Parent: ");
a61af66fc99e Initial load
duke
parents:
diff changeset
655 if(_dom != NULL)
a61af66fc99e Initial load
duke
parents:
diff changeset
656 _dom->_control->dump(); // Parent in Dominator Tree
a61af66fc99e Initial load
duke
parents:
diff changeset
657 tty->print("\n");
a61af66fc99e Initial load
duke
parents:
diff changeset
658
a61af66fc99e Initial load
duke
parents:
diff changeset
659 // Recurse over remaining tree
a61af66fc99e Initial load
duke
parents:
diff changeset
660 if( _dom_child ) _dom_child->dump(offset+2); // Children in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
661 if( _dom_next ) _dom_next ->dump(offset ); // Siblings in dominator tree
a61af66fc99e Initial load
duke
parents:
diff changeset
662
a61af66fc99e Initial load
duke
parents:
diff changeset
663 }
a61af66fc99e Initial load
duke
parents:
diff changeset
664 #endif