src/share/vm/opto/domgraph.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8604
04d83ba48607
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial