0N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "libadt/vectset.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A#include "opto/block.hpp"
1879N/A#include "opto/machnode.hpp"
1879N/A#include "opto/phaseX.hpp"
1879N/A#include "opto/rootnode.hpp"
1879N/A
0N/A// Portions of code courtesy of Clifford Click
0N/A
0N/A// Optimization - Graph Style
0N/A
0N/A//------------------------------Tarjan-----------------------------------------
0N/A// A data structure that holds all the information needed to find dominators.
0N/Astruct Tarjan {
0N/A Block *_block; // Basic block for this info
0N/A
0N/A uint _semi; // Semi-dominators
0N/A uint _size; // Used for faster LINK and EVAL
0N/A Tarjan *_parent; // Parent in DFS
0N/A Tarjan *_label; // Used for LINK and EVAL
0N/A Tarjan *_ancestor; // Used for LINK and EVAL
0N/A Tarjan *_child; // Used for faster LINK and EVAL
0N/A Tarjan *_dom; // Parent in dominator tree (immediate dom)
0N/A Tarjan *_bucket; // Set of vertices with given semidominator
0N/A
0N/A Tarjan *_dom_child; // Child in dominator tree
0N/A Tarjan *_dom_next; // Next in dominator tree
0N/A
0N/A // Fast union-find work
0N/A void COMPRESS();
0N/A Tarjan *EVAL(void);
0N/A void LINK( Tarjan *w, Tarjan *tarjan0 );
0N/A
0N/A void setdepth( uint size );
0N/A
0N/A};
0N/A
0N/A//------------------------------Dominator--------------------------------------
0N/A// Compute the dominator tree of the CFG. The CFG must already have been
0N/A// constructed. This is the Lengauer & Tarjan O(E-alpha(E,V)) algorithm.
0N/Avoid PhaseCFG::Dominators( ) {
0N/A // Pre-grow the blocks array, prior to the ResourceMark kicking in
0N/A _blocks.map(_num_blocks,0);
0N/A
0N/A ResourceMark rm;
0N/A // Setup mappings from my Graph to Tarjan's stuff and back
0N/A // Note: Tarjan uses 1-based arrays
0N/A Tarjan *tarjan = NEW_RESOURCE_ARRAY(Tarjan,_num_blocks+1);
0N/A
0N/A // Tarjan's algorithm, almost verbatim:
0N/A // Step 1:
0N/A _rpo_ctr = _num_blocks;
0N/A uint dfsnum = DFS( tarjan );
0N/A if( dfsnum-1 != _num_blocks ) {// Check for unreachable loops!
0N/A // If the returned dfsnum does not match the number of blocks, then we
0N/A // must have some unreachable loops. These can be made at any time by
0N/A // IterGVN. They are cleaned up by CCP or the loop opts, but the last
0N/A // IterGVN can always make more that are not cleaned up. Highly unlikely
0N/A // except in ZKM.jar, where endless irreducible loops cause the loop opts
0N/A // to not get run.
0N/A //
0N/A // Having found unreachable loops, we have made a bad RPO _block layout.
0N/A // We can re-run the above DFS pass with the correct number of blocks,
0N/A // and hack the Tarjan algorithm below to be robust in the presence of
0N/A // such dead loops (as was done for the NTarjan code farther below).
0N/A // Since this situation is so unlikely, instead I've decided to bail out.
0N/A // CNC 7/24/2001
0N/A C->record_method_not_compilable("unreachable loop");
0N/A return;
0N/A }
0N/A _blocks._cnt = _num_blocks;
0N/A
0N/A // Tarjan is using 1-based arrays, so these are some initialize flags
0N/A tarjan[0]._size = tarjan[0]._semi = 0;
0N/A tarjan[0]._label = &tarjan[0];
0N/A
0N/A uint i;
0N/A for( i=_num_blocks; i>=2; i-- ) { // For all vertices in DFS order
0N/A Tarjan *w = &tarjan[i]; // Get vertex from DFS
0N/A
0N/A // Step 2:
0N/A Node *whead = w->_block->head();
0N/A for( uint j=1; j < whead->req(); j++ ) {
0N/A Block *b = _bbs[whead->in(j)->_idx];
0N/A Tarjan *vx = &tarjan[b->_pre_order];
0N/A Tarjan *u = vx->EVAL();
0N/A if( u->_semi < w->_semi )
0N/A w->_semi = u->_semi;
0N/A }
0N/A
0N/A // w is added to a bucket here, and only here.
0N/A // Thus w is in at most one bucket and the sum of all bucket sizes is O(n).
0N/A // Thus bucket can be a linked list.
0N/A // Thus we do not need a small integer name for each Block.
0N/A w->_bucket = tarjan[w->_semi]._bucket;
0N/A tarjan[w->_semi]._bucket = w;
0N/A
0N/A w->_parent->LINK( w, &tarjan[0] );
0N/A
0N/A // Step 3:
0N/A for( Tarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {
0N/A Tarjan *u = vx->EVAL();
0N/A vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;
0N/A }
0N/A }
0N/A
0N/A // Step 4:
0N/A for( i=2; i <= _num_blocks; i++ ) {
0N/A Tarjan *w = &tarjan[i];
0N/A if( w->_dom != &tarjan[w->_semi] )
0N/A w->_dom = w->_dom->_dom;
0N/A w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
0N/A }
0N/A // No immediate dominator for the root
0N/A Tarjan *w = &tarjan[_broot->_pre_order];
0N/A w->_dom = NULL;
0N/A w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
0N/A
0N/A // Convert the dominator tree array into my kind of graph
0N/A for( i=1; i<=_num_blocks;i++){// For all Tarjan vertices
0N/A Tarjan *t = &tarjan[i]; // Handy access
0N/A Tarjan *tdom = t->_dom; // Handy access to immediate dominator
0N/A if( tdom ) { // Root has no immediate dominator
0N/A t->_block->_idom = tdom->_block; // Set immediate dominator
0N/A t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child
0N/A tdom->_dom_child = t; // Make me a child of my parent
0N/A } else
0N/A t->_block->_idom = NULL; // Root
0N/A }
0N/A w->setdepth( _num_blocks+1 ); // Set depth in dominator tree
0N/A
0N/A}
0N/A
0N/A//----------------------------Block_Stack--------------------------------------
0N/Aclass Block_Stack {
0N/A private:
0N/A struct Block_Descr {
0N/A Block *block; // Block
0N/A int index; // Index of block's successor pushed on stack
0N/A int freq_idx; // Index of block's most frequent successor
0N/A };
0N/A Block_Descr *_stack_top;
0N/A Block_Descr *_stack_max;
0N/A Block_Descr *_stack;
0N/A Tarjan *_tarjan;
0N/A uint most_frequent_successor( Block *b );
0N/A public:
0N/A Block_Stack(Tarjan *tarjan, int size) : _tarjan(tarjan) {
0N/A _stack = NEW_RESOURCE_ARRAY(Block_Descr, size);
0N/A _stack_max = _stack + size;
0N/A _stack_top = _stack - 1; // stack is empty
0N/A }
0N/A void push(uint pre_order, Block *b) {
0N/A Tarjan *t = &_tarjan[pre_order]; // Fast local access
0N/A b->_pre_order = pre_order; // Flag as visited
0N/A t->_block = b; // Save actual block
0N/A t->_semi = pre_order; // Block to DFS map
0N/A t->_label = t; // DFS to vertex map
0N/A t->_ancestor = NULL; // Fast LINK & EVAL setup
0N/A t->_child = &_tarjan[0]; // Sentenial
0N/A t->_size = 1;
0N/A t->_bucket = NULL;
0N/A if (pre_order == 1)
0N/A t->_parent = NULL; // first block doesn't have parent
0N/A else {
605N/A // Save parent (current top block on stack) in DFS
0N/A t->_parent = &_tarjan[_stack_top->block->_pre_order];
0N/A }
0N/A // Now put this block on stack
0N/A ++_stack_top;
0N/A assert(_stack_top < _stack_max, ""); // assert if stack have to grow
0N/A _stack_top->block = b;
0N/A _stack_top->index = -1;
0N/A // Find the index into b->succs[] array of the most frequent successor.
0N/A _stack_top->freq_idx = most_frequent_successor(b); // freq_idx >= 0
0N/A }
0N/A Block* pop() { Block* b = _stack_top->block; _stack_top--; return b; }
0N/A bool is_nonempty() { return (_stack_top >= _stack); }
0N/A bool last_successor() { return (_stack_top->index == _stack_top->freq_idx); }
0N/A Block* next_successor() {
0N/A int i = _stack_top->index;
0N/A i++;
0N/A if (i == _stack_top->freq_idx) i++;
0N/A if (i >= (int)(_stack_top->block->_num_succs)) {
0N/A i = _stack_top->freq_idx; // process most frequent successor last
0N/A }
0N/A _stack_top->index = i;
0N/A return _stack_top->block->_succs[ i ];
0N/A }
0N/A};
0N/A
0N/A//-------------------------most_frequent_successor-----------------------------
0N/A// Find the index into the b->succs[] array of the most frequent successor.
0N/Auint Block_Stack::most_frequent_successor( Block *b ) {
0N/A uint freq_idx = 0;
0N/A int eidx = b->end_idx();
0N/A Node *n = b->_nodes[eidx];
0N/A int op = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : n->Opcode();
0N/A switch( op ) {
0N/A case Op_CountedLoopEnd:
0N/A case Op_If: { // Split frequency amongst children
0N/A float prob = n->as_MachIf()->_prob;
0N/A // Is succ[0] the TRUE branch or the FALSE branch?
0N/A if( b->_nodes[eidx+1]->Opcode() == Op_IfFalse )
0N/A prob = 1.0f - prob;
0N/A freq_idx = prob < PROB_FAIR; // freq=1 for succ[0] < 0.5 prob
0N/A break;
0N/A }
0N/A case Op_Catch: // Split frequency amongst children
0N/A for( freq_idx = 0; freq_idx < b->_num_succs; freq_idx++ )
0N/A if( b->_nodes[eidx+1+freq_idx]->as_CatchProj()->_con == CatchProjNode::fall_through_index )
0N/A break;
0N/A // Handle case of no fall-thru (e.g., check-cast MUST throw an exception)
0N/A if( freq_idx == b->_num_succs ) freq_idx = 0;
0N/A break;
0N/A // Currently there is no support for finding out the most
0N/A // frequent successor for jumps, so lets just make it the first one
0N/A case Op_Jump:
0N/A case Op_Root:
0N/A case Op_Goto:
0N/A case Op_NeverBranch:
0N/A freq_idx = 0; // fall thru
0N/A break;
0N/A case Op_TailCall:
0N/A case Op_TailJump:
0N/A case Op_Return:
0N/A case Op_Halt:
0N/A case Op_Rethrow:
0N/A break;
0N/A default:
0N/A ShouldNotReachHere();
0N/A }
0N/A return freq_idx;
0N/A}
0N/A
0N/A//------------------------------DFS--------------------------------------------
0N/A// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup
0N/A// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.
0N/Auint PhaseCFG::DFS( Tarjan *tarjan ) {
0N/A Block *b = _broot;
0N/A uint pre_order = 1;
0N/A // Allocate stack of size _num_blocks+1 to avoid frequent realloc
0N/A Block_Stack bstack(tarjan, _num_blocks+1);
0N/A
0N/A // Push on stack the state for the first block
0N/A bstack.push(pre_order, b);
0N/A ++pre_order;
0N/A
0N/A while (bstack.is_nonempty()) {
0N/A if (!bstack.last_successor()) {
0N/A // Walk over all successors in pre-order (DFS).
0N/A Block *s = bstack.next_successor();
0N/A if (s->_pre_order == 0) { // Check for no-pre-order, not-visited
0N/A // Push on stack the state of successor
0N/A bstack.push(pre_order, s);
0N/A ++pre_order;
0N/A }
0N/A }
0N/A else {
0N/A // Build a reverse post-order in the CFG _blocks array
0N/A Block *stack_top = bstack.pop();
0N/A stack_top->_rpo = --_rpo_ctr;
0N/A _blocks.map(stack_top->_rpo, stack_top);
0N/A }
0N/A }
0N/A return pre_order;
0N/A}
0N/A
0N/A//------------------------------COMPRESS---------------------------------------
0N/Avoid Tarjan::COMPRESS()
0N/A{
0N/A assert( _ancestor != 0, "" );
0N/A if( _ancestor->_ancestor != 0 ) {
0N/A _ancestor->COMPRESS( );
0N/A if( _ancestor->_label->_semi < _label->_semi )
0N/A _label = _ancestor->_label;
0N/A _ancestor = _ancestor->_ancestor;
0N/A }
0N/A}
0N/A
0N/A//------------------------------EVAL-------------------------------------------
0N/ATarjan *Tarjan::EVAL() {
0N/A if( !_ancestor ) return _label;
0N/A COMPRESS();
0N/A return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;
0N/A}
0N/A
0N/A//------------------------------LINK-------------------------------------------
0N/Avoid Tarjan::LINK( Tarjan *w, Tarjan *tarjan0 ) {
0N/A Tarjan *s = w;
0N/A while( w->_label->_semi < s->_child->_label->_semi ) {
0N/A if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {
0N/A s->_child->_ancestor = s;
0N/A s->_child = s->_child->_child;
0N/A } else {
0N/A s->_child->_size = s->_size;
0N/A s = s->_ancestor = s->_child;
0N/A }
0N/A }
0N/A s->_label = w->_label;
0N/A _size += w->_size;
0N/A if( _size < (w->_size << 1) ) {
0N/A Tarjan *tmp = s; s = _child; _child = tmp;
0N/A }
0N/A while( s != tarjan0 ) {
0N/A s->_ancestor = this;
0N/A s = s->_child;
0N/A }
0N/A}
0N/A
0N/A//------------------------------setdepth---------------------------------------
0N/Avoid Tarjan::setdepth( uint stack_size ) {
0N/A Tarjan **top = NEW_RESOURCE_ARRAY(Tarjan*, stack_size);
0N/A Tarjan **next = top;
0N/A Tarjan **last;
0N/A uint depth = 0;
0N/A *top = this;
0N/A ++top;
0N/A do {
0N/A // next level
0N/A ++depth;
0N/A last = top;
0N/A do {
0N/A // Set current depth for all tarjans on this level
0N/A Tarjan *t = *next; // next tarjan from stack
0N/A ++next;
0N/A do {
0N/A t->_block->_dom_depth = depth; // Set depth in dominator tree
0N/A Tarjan *dom_child = t->_dom_child;
0N/A t = t->_dom_next; // next tarjan
0N/A if (dom_child != NULL) {
0N/A *top = dom_child; // save child on stack
0N/A ++top;
0N/A }
0N/A } while (t != NULL);
0N/A } while (next < last);
0N/A } while (last < top);
0N/A}
0N/A
0N/A//*********************** DOMINATORS ON THE SEA OF NODES***********************
0N/A//------------------------------NTarjan----------------------------------------
0N/A// A data structure that holds all the information needed to find dominators.
0N/Astruct NTarjan {
0N/A Node *_control; // Control node associated with this info
0N/A
0N/A uint _semi; // Semi-dominators
0N/A uint _size; // Used for faster LINK and EVAL
0N/A NTarjan *_parent; // Parent in DFS
0N/A NTarjan *_label; // Used for LINK and EVAL
0N/A NTarjan *_ancestor; // Used for LINK and EVAL
0N/A NTarjan *_child; // Used for faster LINK and EVAL
0N/A NTarjan *_dom; // Parent in dominator tree (immediate dom)
0N/A NTarjan *_bucket; // Set of vertices with given semidominator
0N/A
0N/A NTarjan *_dom_child; // Child in dominator tree
0N/A NTarjan *_dom_next; // Next in dominator tree
0N/A
0N/A // Perform DFS search.
0N/A // Setup 'vertex' as DFS to vertex mapping.
0N/A // Setup 'semi' as vertex to DFS mapping.
0N/A // Set 'parent' to DFS parent.
0N/A static int DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder );
0N/A void setdepth( uint size, uint *dom_depth );
0N/A
0N/A // Fast union-find work
0N/A void COMPRESS();
0N/A NTarjan *EVAL(void);
0N/A void LINK( NTarjan *w, NTarjan *ntarjan0 );
0N/A#ifndef PRODUCT
0N/A void dump(int offset) const;
0N/A#endif
0N/A};
0N/A
0N/A//------------------------------Dominator--------------------------------------
0N/A// Compute the dominator tree of the sea of nodes. This version walks all CFG
0N/A// nodes (using the is_CFG() call) and places them in a dominator tree. Thus,
0N/A// it needs a count of the CFG nodes for the mapping table. This is the
0N/A// Lengauer & Tarjan O(E-alpha(E,V)) algorithm.
921N/Avoid PhaseIdealLoop::Dominators() {
0N/A ResourceMark rm;
0N/A // Setup mappings from my Graph to Tarjan's stuff and back
0N/A // Note: Tarjan uses 1-based arrays
0N/A NTarjan *ntarjan = NEW_RESOURCE_ARRAY(NTarjan,C->unique()+1);
0N/A // Initialize _control field for fast reference
0N/A int i;
0N/A for( i= C->unique()-1; i>=0; i-- )
0N/A ntarjan[i]._control = NULL;
0N/A
0N/A // Store the DFS order for the main loop
0N/A uint *dfsorder = NEW_RESOURCE_ARRAY(uint,C->unique()+1);
0N/A memset(dfsorder, max_uint, (C->unique()+1) * sizeof(uint));
0N/A
0N/A // Tarjan's algorithm, almost verbatim:
0N/A // Step 1:
0N/A VectorSet visited(Thread::current()->resource_area());
0N/A int dfsnum = NTarjan::DFS( ntarjan, visited, this, dfsorder);
0N/A
0N/A // Tarjan is using 1-based arrays, so these are some initialize flags
0N/A ntarjan[0]._size = ntarjan[0]._semi = 0;
0N/A ntarjan[0]._label = &ntarjan[0];
0N/A
0N/A for( i = dfsnum-1; i>1; i-- ) { // For all nodes in reverse DFS order
0N/A NTarjan *w = &ntarjan[i]; // Get Node from DFS
0N/A assert(w->_control != NULL,"bad DFS walk");
0N/A
0N/A // Step 2:
0N/A Node *whead = w->_control;
0N/A for( uint j=0; j < whead->req(); j++ ) { // For each predecessor
0N/A if( whead->in(j) == NULL || !whead->in(j)->is_CFG() )
0N/A continue; // Only process control nodes
0N/A uint b = dfsorder[whead->in(j)->_idx];
0N/A if(b == max_uint) continue;
0N/A NTarjan *vx = &ntarjan[b];
0N/A NTarjan *u = vx->EVAL();
0N/A if( u->_semi < w->_semi )
0N/A w->_semi = u->_semi;
0N/A }
0N/A
0N/A // w is added to a bucket here, and only here.
0N/A // Thus w is in at most one bucket and the sum of all bucket sizes is O(n).
0N/A // Thus bucket can be a linked list.
0N/A w->_bucket = ntarjan[w->_semi]._bucket;
0N/A ntarjan[w->_semi]._bucket = w;
0N/A
0N/A w->_parent->LINK( w, &ntarjan[0] );
0N/A
0N/A // Step 3:
0N/A for( NTarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {
0N/A NTarjan *u = vx->EVAL();
0N/A vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;
0N/A }
0N/A
0N/A // Cleanup any unreachable loops now. Unreachable loops are loops that
0N/A // flow into the main graph (and hence into ROOT) but are not reachable
0N/A // from above. Such code is dead, but requires a global pass to detect
0N/A // it; this global pass was the 'build_loop_tree' pass run just prior.
921N/A if( !_verify_only && whead->is_Region() ) {
0N/A for( uint i = 1; i < whead->req(); i++ ) {
0N/A if (!has_node(whead->in(i))) {
0N/A // Kill dead input path
0N/A assert( !visited.test(whead->in(i)->_idx),
0N/A "input with no loop must be dead" );
3811N/A _igvn.delete_input_of(whead, i);
0N/A for (DUIterator_Fast jmax, j = whead->fast_outs(jmax); j < jmax; j++) {
0N/A Node* p = whead->fast_out(j);
0N/A if( p->is_Phi() ) {
3811N/A _igvn.delete_input_of(p, i);
0N/A }
0N/A }
0N/A i--; // Rerun same iteration
0N/A } // End of if dead input path
0N/A } // End of for all input paths
0N/A } // End if if whead is a Region
0N/A } // End of for all Nodes in reverse DFS order
0N/A
0N/A // Step 4:
0N/A for( i=2; i < dfsnum; i++ ) { // DFS order
0N/A NTarjan *w = &ntarjan[i];
0N/A assert(w->_control != NULL,"Bad DFS walk");
0N/A if( w->_dom != &ntarjan[w->_semi] )
0N/A w->_dom = w->_dom->_dom;
0N/A w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
0N/A }
0N/A // No immediate dominator for the root
0N/A NTarjan *w = &ntarjan[dfsorder[C->root()->_idx]];
0N/A w->_dom = NULL;
0N/A w->_parent = NULL;
0N/A w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later
0N/A
0N/A // Convert the dominator tree array into my kind of graph
0N/A for( i=1; i<dfsnum; i++ ) { // For all Tarjan vertices
0N/A NTarjan *t = &ntarjan[i]; // Handy access
0N/A assert(t->_control != NULL,"Bad DFS walk");
0N/A NTarjan *tdom = t->_dom; // Handy access to immediate dominator
0N/A if( tdom ) { // Root has no immediate dominator
0N/A _idom[t->_control->_idx] = tdom->_control; // Set immediate dominator
0N/A t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child
0N/A tdom->_dom_child = t; // Make me a child of my parent
0N/A } else
0N/A _idom[C->root()->_idx] = NULL; // Root
0N/A }
0N/A w->setdepth( C->unique()+1, _dom_depth ); // Set depth in dominator tree
0N/A // Pick up the 'top' node as well
0N/A _idom [C->top()->_idx] = C->root();
0N/A _dom_depth[C->top()->_idx] = 1;
0N/A
0N/A // Debug Print of Dominator tree
0N/A if( PrintDominators ) {
0N/A#ifndef PRODUCT
0N/A w->dump(0);
0N/A#endif
0N/A }
0N/A}
0N/A
0N/A//------------------------------DFS--------------------------------------------
0N/A// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup
0N/A// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.
0N/Aint NTarjan::DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder) {
0N/A // Allocate stack of size C->unique()/8 to avoid frequent realloc
0N/A GrowableArray <Node *> dfstack(pil->C->unique() >> 3);
0N/A Node *b = pil->C->root();
0N/A int dfsnum = 1;
0N/A dfsorder[b->_idx] = dfsnum; // Cache parent's dfsnum for a later use
0N/A dfstack.push(b);
0N/A
0N/A while (dfstack.is_nonempty()) {
0N/A b = dfstack.pop();
0N/A if( !visited.test_set(b->_idx) ) { // Test node and flag it as visited
0N/A NTarjan *w = &ntarjan[dfsnum];
0N/A // Only fully process control nodes
0N/A w->_control = b; // Save actual node
0N/A // Use parent's cached dfsnum to identify "Parent in DFS"
0N/A w->_parent = &ntarjan[dfsorder[b->_idx]];
0N/A dfsorder[b->_idx] = dfsnum; // Save DFS order info
0N/A w->_semi = dfsnum; // Node to DFS map
0N/A w->_label = w; // DFS to vertex map
0N/A w->_ancestor = NULL; // Fast LINK & EVAL setup
0N/A w->_child = &ntarjan[0]; // Sentinal
0N/A w->_size = 1;
0N/A w->_bucket = NULL;
0N/A
0N/A // Need DEF-USE info for this pass
0N/A for ( int i = b->outcnt(); i-- > 0; ) { // Put on stack backwards
0N/A Node* s = b->raw_out(i); // Get a use
0N/A // CFG nodes only and not dead stuff
0N/A if( s->is_CFG() && pil->has_node(s) && !visited.test(s->_idx) ) {
0N/A dfsorder[s->_idx] = dfsnum; // Cache parent's dfsnum for a later use
0N/A dfstack.push(s);
0N/A }
0N/A }
0N/A dfsnum++; // update after parent's dfsnum has been cached.
0N/A }
0N/A }
0N/A
0N/A return dfsnum;
0N/A}
0N/A
0N/A//------------------------------COMPRESS---------------------------------------
0N/Avoid NTarjan::COMPRESS()
0N/A{
0N/A assert( _ancestor != 0, "" );
0N/A if( _ancestor->_ancestor != 0 ) {
0N/A _ancestor->COMPRESS( );
0N/A if( _ancestor->_label->_semi < _label->_semi )
0N/A _label = _ancestor->_label;
0N/A _ancestor = _ancestor->_ancestor;
0N/A }
0N/A}
0N/A
0N/A//------------------------------EVAL-------------------------------------------
0N/ANTarjan *NTarjan::EVAL() {
0N/A if( !_ancestor ) return _label;
0N/A COMPRESS();
0N/A return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;
0N/A}
0N/A
0N/A//------------------------------LINK-------------------------------------------
0N/Avoid NTarjan::LINK( NTarjan *w, NTarjan *ntarjan0 ) {
0N/A NTarjan *s = w;
0N/A while( w->_label->_semi < s->_child->_label->_semi ) {
0N/A if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {
0N/A s->_child->_ancestor = s;
0N/A s->_child = s->_child->_child;
0N/A } else {
0N/A s->_child->_size = s->_size;
0N/A s = s->_ancestor = s->_child;
0N/A }
0N/A }
0N/A s->_label = w->_label;
0N/A _size += w->_size;
0N/A if( _size < (w->_size << 1) ) {
0N/A NTarjan *tmp = s; s = _child; _child = tmp;
0N/A }
0N/A while( s != ntarjan0 ) {
0N/A s->_ancestor = this;
0N/A s = s->_child;
0N/A }
0N/A}
0N/A
0N/A//------------------------------setdepth---------------------------------------
0N/Avoid NTarjan::setdepth( uint stack_size, uint *dom_depth ) {
0N/A NTarjan **top = NEW_RESOURCE_ARRAY(NTarjan*, stack_size);
0N/A NTarjan **next = top;
0N/A NTarjan **last;
0N/A uint depth = 0;
0N/A *top = this;
0N/A ++top;
0N/A do {
0N/A // next level
0N/A ++depth;
0N/A last = top;
0N/A do {
0N/A // Set current depth for all tarjans on this level
0N/A NTarjan *t = *next; // next tarjan from stack
0N/A ++next;
0N/A do {
0N/A dom_depth[t->_control->_idx] = depth; // Set depth in dominator tree
0N/A NTarjan *dom_child = t->_dom_child;
0N/A t = t->_dom_next; // next tarjan
0N/A if (dom_child != NULL) {
0N/A *top = dom_child; // save child on stack
0N/A ++top;
0N/A }
0N/A } while (t != NULL);
0N/A } while (next < last);
0N/A } while (last < top);
0N/A}
0N/A
0N/A//------------------------------dump-------------------------------------------
0N/A#ifndef PRODUCT
0N/Avoid NTarjan::dump(int offset) const {
0N/A // Dump the data from this node
0N/A int i;
0N/A for(i = offset; i >0; i--) // Use indenting for tree structure
0N/A tty->print(" ");
0N/A tty->print("Dominator Node: ");
0N/A _control->dump(); // Control node for this dom node
0N/A tty->print("\n");
0N/A for(i = offset; i >0; i--) // Use indenting for tree structure
0N/A tty->print(" ");
0N/A tty->print("semi:%d, size:%d\n",_semi, _size);
0N/A for(i = offset; i >0; i--) // Use indenting for tree structure
0N/A tty->print(" ");
0N/A tty->print("DFS Parent: ");
0N/A if(_parent != NULL)
0N/A _parent->_control->dump(); // Parent in DFS
0N/A tty->print("\n");
0N/A for(i = offset; i >0; i--) // Use indenting for tree structure
0N/A tty->print(" ");
0N/A tty->print("Dom Parent: ");
0N/A if(_dom != NULL)
0N/A _dom->_control->dump(); // Parent in Dominator Tree
0N/A tty->print("\n");
0N/A
0N/A // Recurse over remaining tree
0N/A if( _dom_child ) _dom_child->dump(offset+2); // Children in dominator tree
0N/A if( _dom_next ) _dom_next ->dump(offset ); // Siblings in dominator tree
0N/A
0N/A}
0N/A#endif