src/share/vm/opto/domgraph.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 435
a61af66fc99e
child 1356
046932b72aa2
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

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

mercurial