Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/domgraph.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "libadt/vectset.hpp"26#include "memory/allocation.hpp"27#include "opto/block.hpp"28#include "opto/machnode.hpp"29#include "opto/phaseX.hpp"30#include "opto/rootnode.hpp"3132// Portions of code courtesy of Clifford Click3334// A data structure that holds all the information needed to find dominators.35struct Tarjan {36Block *_block; // Basic block for this info3738uint _semi; // Semi-dominators39uint _size; // Used for faster LINK and EVAL40Tarjan *_parent; // Parent in DFS41Tarjan *_label; // Used for LINK and EVAL42Tarjan *_ancestor; // Used for LINK and EVAL43Tarjan *_child; // Used for faster LINK and EVAL44Tarjan *_dom; // Parent in dominator tree (immediate dom)45Tarjan *_bucket; // Set of vertices with given semidominator4647Tarjan *_dom_child; // Child in dominator tree48Tarjan *_dom_next; // Next in dominator tree4950// Fast union-find work51void COMPRESS();52Tarjan *EVAL(void);53void LINK( Tarjan *w, Tarjan *tarjan0 );5455void setdepth( uint size );5657};5859// Compute the dominator tree of the CFG. The CFG must already have been60// constructed. This is the Lengauer & Tarjan O(E-alpha(E,V)) algorithm.61void PhaseCFG::build_dominator_tree() {62// Pre-grow the blocks array, prior to the ResourceMark kicking in63_blocks.map(number_of_blocks(), 0);6465ResourceMark rm;66// Setup mappings from my Graph to Tarjan's stuff and back67// Note: Tarjan uses 1-based arrays68Tarjan* tarjan = NEW_RESOURCE_ARRAY(Tarjan, number_of_blocks() + 1);6970// Tarjan's algorithm, almost verbatim:71// Step 1:72uint dfsnum = do_DFS(tarjan, number_of_blocks());73if (dfsnum - 1 != number_of_blocks()) { // Check for unreachable loops!74// If the returned dfsnum does not match the number of blocks, then we75// must have some unreachable loops. These can be made at any time by76// IterGVN. They are cleaned up by CCP or the loop opts, but the last77// IterGVN can always make more that are not cleaned up. Highly unlikely78// except in ZKM.jar, where endless irreducible loops cause the loop opts79// to not get run.80//81// Having found unreachable loops, we have made a bad RPO _block layout.82// We can re-run the above DFS pass with the correct number of blocks,83// and hack the Tarjan algorithm below to be robust in the presence of84// such dead loops (as was done for the NTarjan code farther below).85// Since this situation is so unlikely, instead I've decided to bail out.86// CNC 7/24/200187C->record_method_not_compilable("unreachable loop");88return;89}90_blocks._cnt = number_of_blocks();9192// Tarjan is using 1-based arrays, so these are some initialize flags93tarjan[0]._size = tarjan[0]._semi = 0;94tarjan[0]._label = &tarjan[0];9596for (uint i = number_of_blocks(); i >= 2; i--) { // For all vertices in DFS order97Tarjan *w = &tarjan[i]; // Get vertex from DFS9899// Step 2:100Node *whead = w->_block->head();101for (uint j = 1; j < whead->req(); j++) {102Block* b = get_block_for_node(whead->in(j));103Tarjan *vx = &tarjan[b->_pre_order];104Tarjan *u = vx->EVAL();105if( u->_semi < w->_semi )106w->_semi = u->_semi;107}108109// w is added to a bucket here, and only here.110// Thus w is in at most one bucket and the sum of all bucket sizes is O(n).111// Thus bucket can be a linked list.112// Thus we do not need a small integer name for each Block.113w->_bucket = tarjan[w->_semi]._bucket;114tarjan[w->_semi]._bucket = w;115116w->_parent->LINK( w, &tarjan[0] );117118// Step 3:119for( Tarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {120Tarjan *u = vx->EVAL();121vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;122}123}124125// Step 4:126for (uint i = 2; i <= number_of_blocks(); i++) {127Tarjan *w = &tarjan[i];128if( w->_dom != &tarjan[w->_semi] )129w->_dom = w->_dom->_dom;130w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later131}132// No immediate dominator for the root133Tarjan *w = &tarjan[get_root_block()->_pre_order];134w->_dom = NULL;135w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later136137// Convert the dominator tree array into my kind of graph138for(uint i = 1; i <= number_of_blocks(); i++){ // For all Tarjan vertices139Tarjan *t = &tarjan[i]; // Handy access140Tarjan *tdom = t->_dom; // Handy access to immediate dominator141if( tdom ) { // Root has no immediate dominator142t->_block->_idom = tdom->_block; // Set immediate dominator143t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child144tdom->_dom_child = t; // Make me a child of my parent145} else146t->_block->_idom = NULL; // Root147}148w->setdepth(number_of_blocks() + 1); // Set depth in dominator tree149150}151152class Block_Stack {153private:154struct Block_Descr {155Block *block; // Block156int index; // Index of block's successor pushed on stack157int freq_idx; // Index of block's most frequent successor158};159Block_Descr *_stack_top;160Block_Descr *_stack_max;161Block_Descr *_stack;162Tarjan *_tarjan;163uint most_frequent_successor( Block *b );164public:165Block_Stack(Tarjan *tarjan, int size) : _tarjan(tarjan) {166_stack = NEW_RESOURCE_ARRAY(Block_Descr, size);167_stack_max = _stack + size;168_stack_top = _stack - 1; // stack is empty169}170void push(uint pre_order, Block *b) {171Tarjan *t = &_tarjan[pre_order]; // Fast local access172b->_pre_order = pre_order; // Flag as visited173t->_block = b; // Save actual block174t->_semi = pre_order; // Block to DFS map175t->_label = t; // DFS to vertex map176t->_ancestor = NULL; // Fast LINK & EVAL setup177t->_child = &_tarjan[0]; // Sentenial178t->_size = 1;179t->_bucket = NULL;180if (pre_order == 1)181t->_parent = NULL; // first block doesn't have parent182else {183// Save parent (current top block on stack) in DFS184t->_parent = &_tarjan[_stack_top->block->_pre_order];185}186// Now put this block on stack187++_stack_top;188assert(_stack_top < _stack_max, ""); // assert if stack have to grow189_stack_top->block = b;190_stack_top->index = -1;191// Find the index into b->succs[] array of the most frequent successor.192_stack_top->freq_idx = most_frequent_successor(b); // freq_idx >= 0193}194Block* pop() { Block* b = _stack_top->block; _stack_top--; return b; }195bool is_nonempty() { return (_stack_top >= _stack); }196bool last_successor() { return (_stack_top->index == _stack_top->freq_idx); }197Block* next_successor() {198int i = _stack_top->index;199i++;200if (i == _stack_top->freq_idx) i++;201if (i >= (int)(_stack_top->block->_num_succs)) {202i = _stack_top->freq_idx; // process most frequent successor last203}204_stack_top->index = i;205return _stack_top->block->_succs[ i ];206}207};208209// Find the index into the b->succs[] array of the most frequent successor.210uint Block_Stack::most_frequent_successor( Block *b ) {211uint freq_idx = 0;212int eidx = b->end_idx();213Node *n = b->get_node(eidx);214int op = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : n->Opcode();215switch( op ) {216case Op_CountedLoopEnd:217case Op_If: { // Split frequency amongst children218float prob = n->as_MachIf()->_prob;219// Is succ[0] the TRUE branch or the FALSE branch?220if( b->get_node(eidx+1)->Opcode() == Op_IfFalse )221prob = 1.0f - prob;222freq_idx = prob < PROB_FAIR; // freq=1 for succ[0] < 0.5 prob223break;224}225case Op_Catch: // Split frequency amongst children226for( freq_idx = 0; freq_idx < b->_num_succs; freq_idx++ )227if( b->get_node(eidx+1+freq_idx)->as_CatchProj()->_con == CatchProjNode::fall_through_index )228break;229// Handle case of no fall-thru (e.g., check-cast MUST throw an exception)230if( freq_idx == b->_num_succs ) freq_idx = 0;231break;232// Currently there is no support for finding out the most233// frequent successor for jumps, so lets just make it the first one234case Op_Jump:235case Op_Root:236case Op_Goto:237case Op_NeverBranch:238freq_idx = 0; // fall thru239break;240case Op_TailCall:241case Op_TailJump:242case Op_Return:243case Op_Halt:244case Op_Rethrow:245break;246default:247ShouldNotReachHere();248}249return freq_idx;250}251252// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup253// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.254uint PhaseCFG::do_DFS(Tarjan *tarjan, uint rpo_counter) {255Block* root_block = get_root_block();256uint pre_order = 1;257// Allocate stack of size number_of_blocks() + 1 to avoid frequent realloc258Block_Stack bstack(tarjan, number_of_blocks() + 1);259260// Push on stack the state for the first block261bstack.push(pre_order, root_block);262++pre_order;263264while (bstack.is_nonempty()) {265if (!bstack.last_successor()) {266// Walk over all successors in pre-order (DFS).267Block* next_block = bstack.next_successor();268if (next_block->_pre_order == 0) { // Check for no-pre-order, not-visited269// Push on stack the state of successor270bstack.push(pre_order, next_block);271++pre_order;272}273}274else {275// Build a reverse post-order in the CFG _blocks array276Block *stack_top = bstack.pop();277stack_top->_rpo = --rpo_counter;278_blocks.map(stack_top->_rpo, stack_top);279}280}281return pre_order;282}283284void Tarjan::COMPRESS()285{286assert( _ancestor != 0, "" );287if( _ancestor->_ancestor != 0 ) {288_ancestor->COMPRESS( );289if( _ancestor->_label->_semi < _label->_semi )290_label = _ancestor->_label;291_ancestor = _ancestor->_ancestor;292}293}294295Tarjan *Tarjan::EVAL() {296if( !_ancestor ) return _label;297COMPRESS();298return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;299}300301void Tarjan::LINK( Tarjan *w, Tarjan *tarjan0 ) {302Tarjan *s = w;303while( w->_label->_semi < s->_child->_label->_semi ) {304if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {305s->_child->_ancestor = s;306s->_child = s->_child->_child;307} else {308s->_child->_size = s->_size;309s = s->_ancestor = s->_child;310}311}312s->_label = w->_label;313_size += w->_size;314if( _size < (w->_size << 1) ) {315Tarjan *tmp = s; s = _child; _child = tmp;316}317while( s != tarjan0 ) {318s->_ancestor = this;319s = s->_child;320}321}322323void Tarjan::setdepth( uint stack_size ) {324Tarjan **top = NEW_RESOURCE_ARRAY(Tarjan*, stack_size);325Tarjan **next = top;326Tarjan **last;327uint depth = 0;328*top = this;329++top;330do {331// next level332++depth;333last = top;334do {335// Set current depth for all tarjans on this level336Tarjan *t = *next; // next tarjan from stack337++next;338do {339t->_block->_dom_depth = depth; // Set depth in dominator tree340Tarjan *dom_child = t->_dom_child;341t = t->_dom_next; // next tarjan342if (dom_child != NULL) {343*top = dom_child; // save child on stack344++top;345}346} while (t != NULL);347} while (next < last);348} while (last < top);349}350351// Compute dominators on the Sea of Nodes form352// A data structure that holds all the information needed to find dominators.353struct NTarjan {354Node *_control; // Control node associated with this info355356uint _semi; // Semi-dominators357uint _size; // Used for faster LINK and EVAL358NTarjan *_parent; // Parent in DFS359NTarjan *_label; // Used for LINK and EVAL360NTarjan *_ancestor; // Used for LINK and EVAL361NTarjan *_child; // Used for faster LINK and EVAL362NTarjan *_dom; // Parent in dominator tree (immediate dom)363NTarjan *_bucket; // Set of vertices with given semidominator364365NTarjan *_dom_child; // Child in dominator tree366NTarjan *_dom_next; // Next in dominator tree367368// Perform DFS search.369// Setup 'vertex' as DFS to vertex mapping.370// Setup 'semi' as vertex to DFS mapping.371// Set 'parent' to DFS parent.372static int DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder );373void setdepth( uint size, uint *dom_depth );374375// Fast union-find work376void COMPRESS();377NTarjan *EVAL(void);378void LINK( NTarjan *w, NTarjan *ntarjan0 );379#ifndef PRODUCT380void dump(int offset) const;381#endif382};383384// Compute the dominator tree of the sea of nodes. This version walks all CFG385// nodes (using the is_CFG() call) and places them in a dominator tree. Thus,386// it needs a count of the CFG nodes for the mapping table. This is the387// Lengauer & Tarjan O(E-alpha(E,V)) algorithm.388void PhaseIdealLoop::Dominators() {389ResourceMark rm;390// Setup mappings from my Graph to Tarjan's stuff and back391// Note: Tarjan uses 1-based arrays392NTarjan *ntarjan = NEW_RESOURCE_ARRAY(NTarjan,C->unique()+1);393// Initialize _control field for fast reference394int i;395for( i= C->unique()-1; i>=0; i-- )396ntarjan[i]._control = NULL;397398// Store the DFS order for the main loop399uint *dfsorder = NEW_RESOURCE_ARRAY(uint,C->unique()+1);400memset(dfsorder, max_uint, (C->unique()+1) * sizeof(uint));401402// Tarjan's algorithm, almost verbatim:403// Step 1:404VectorSet visited(Thread::current()->resource_area());405int dfsnum = NTarjan::DFS( ntarjan, visited, this, dfsorder);406407// Tarjan is using 1-based arrays, so these are some initialize flags408ntarjan[0]._size = ntarjan[0]._semi = 0;409ntarjan[0]._label = &ntarjan[0];410411for( i = dfsnum-1; i>1; i-- ) { // For all nodes in reverse DFS order412NTarjan *w = &ntarjan[i]; // Get Node from DFS413assert(w->_control != NULL,"bad DFS walk");414415// Step 2:416Node *whead = w->_control;417for( uint j=0; j < whead->req(); j++ ) { // For each predecessor418if( whead->in(j) == NULL || !whead->in(j)->is_CFG() )419continue; // Only process control nodes420uint b = dfsorder[whead->in(j)->_idx];421if(b == max_uint) continue;422NTarjan *vx = &ntarjan[b];423NTarjan *u = vx->EVAL();424if( u->_semi < w->_semi )425w->_semi = u->_semi;426}427428// w is added to a bucket here, and only here.429// Thus w is in at most one bucket and the sum of all bucket sizes is O(n).430// Thus bucket can be a linked list.431w->_bucket = ntarjan[w->_semi]._bucket;432ntarjan[w->_semi]._bucket = w;433434w->_parent->LINK( w, &ntarjan[0] );435436// Step 3:437for( NTarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {438NTarjan *u = vx->EVAL();439vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;440}441442// Cleanup any unreachable loops now. Unreachable loops are loops that443// flow into the main graph (and hence into ROOT) but are not reachable444// from above. Such code is dead, but requires a global pass to detect445// it; this global pass was the 'build_loop_tree' pass run just prior.446if( !_verify_only && whead->is_Region() ) {447for( uint i = 1; i < whead->req(); i++ ) {448if (!has_node(whead->in(i))) {449// Kill dead input path450assert( !visited.test(whead->in(i)->_idx),451"input with no loop must be dead" );452_igvn.delete_input_of(whead, i);453for (DUIterator_Fast jmax, j = whead->fast_outs(jmax); j < jmax; j++) {454Node* p = whead->fast_out(j);455if( p->is_Phi() ) {456_igvn.delete_input_of(p, i);457}458}459i--; // Rerun same iteration460} // End of if dead input path461} // End of for all input paths462} // End if if whead is a Region463} // End of for all Nodes in reverse DFS order464465// Step 4:466for( i=2; i < dfsnum; i++ ) { // DFS order467NTarjan *w = &ntarjan[i];468assert(w->_control != NULL,"Bad DFS walk");469if( w->_dom != &ntarjan[w->_semi] )470w->_dom = w->_dom->_dom;471w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later472}473// No immediate dominator for the root474NTarjan *w = &ntarjan[dfsorder[C->root()->_idx]];475w->_dom = NULL;476w->_parent = NULL;477w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later478479// Convert the dominator tree array into my kind of graph480for( i=1; i<dfsnum; i++ ) { // For all Tarjan vertices481NTarjan *t = &ntarjan[i]; // Handy access482assert(t->_control != NULL,"Bad DFS walk");483NTarjan *tdom = t->_dom; // Handy access to immediate dominator484if( tdom ) { // Root has no immediate dominator485_idom[t->_control->_idx] = tdom->_control; // Set immediate dominator486t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child487tdom->_dom_child = t; // Make me a child of my parent488} else489_idom[C->root()->_idx] = NULL; // Root490}491w->setdepth( C->unique()+1, _dom_depth ); // Set depth in dominator tree492// Pick up the 'top' node as well493_idom [C->top()->_idx] = C->root();494_dom_depth[C->top()->_idx] = 1;495496// Debug Print of Dominator tree497if( PrintDominators ) {498#ifndef PRODUCT499w->dump(0);500#endif501}502}503504// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup505// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.506int NTarjan::DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder) {507// Allocate stack of size C->live_nodes()/8 to avoid frequent realloc508GrowableArray <Node *> dfstack(pil->C->live_nodes() >> 3);509Node *b = pil->C->root();510int dfsnum = 1;511dfsorder[b->_idx] = dfsnum; // Cache parent's dfsnum for a later use512dfstack.push(b);513514while (dfstack.is_nonempty()) {515b = dfstack.pop();516if( !visited.test_set(b->_idx) ) { // Test node and flag it as visited517NTarjan *w = &ntarjan[dfsnum];518// Only fully process control nodes519w->_control = b; // Save actual node520// Use parent's cached dfsnum to identify "Parent in DFS"521w->_parent = &ntarjan[dfsorder[b->_idx]];522dfsorder[b->_idx] = dfsnum; // Save DFS order info523w->_semi = dfsnum; // Node to DFS map524w->_label = w; // DFS to vertex map525w->_ancestor = NULL; // Fast LINK & EVAL setup526w->_child = &ntarjan[0]; // Sentinal527w->_size = 1;528w->_bucket = NULL;529530// Need DEF-USE info for this pass531for ( int i = b->outcnt(); i-- > 0; ) { // Put on stack backwards532Node* s = b->raw_out(i); // Get a use533// CFG nodes only and not dead stuff534if( s->is_CFG() && pil->has_node(s) && !visited.test(s->_idx) ) {535dfsorder[s->_idx] = dfsnum; // Cache parent's dfsnum for a later use536dfstack.push(s);537}538}539dfsnum++; // update after parent's dfsnum has been cached.540}541}542543return dfsnum;544}545546void NTarjan::COMPRESS()547{548assert( _ancestor != 0, "" );549if( _ancestor->_ancestor != 0 ) {550_ancestor->COMPRESS( );551if( _ancestor->_label->_semi < _label->_semi )552_label = _ancestor->_label;553_ancestor = _ancestor->_ancestor;554}555}556557NTarjan *NTarjan::EVAL() {558if( !_ancestor ) return _label;559COMPRESS();560return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;561}562563void NTarjan::LINK( NTarjan *w, NTarjan *ntarjan0 ) {564NTarjan *s = w;565while( w->_label->_semi < s->_child->_label->_semi ) {566if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {567s->_child->_ancestor = s;568s->_child = s->_child->_child;569} else {570s->_child->_size = s->_size;571s = s->_ancestor = s->_child;572}573}574s->_label = w->_label;575_size += w->_size;576if( _size < (w->_size << 1) ) {577NTarjan *tmp = s; s = _child; _child = tmp;578}579while( s != ntarjan0 ) {580s->_ancestor = this;581s = s->_child;582}583}584585void NTarjan::setdepth( uint stack_size, uint *dom_depth ) {586NTarjan **top = NEW_RESOURCE_ARRAY(NTarjan*, stack_size);587NTarjan **next = top;588NTarjan **last;589uint depth = 0;590*top = this;591++top;592do {593// next level594++depth;595last = top;596do {597// Set current depth for all tarjans on this level598NTarjan *t = *next; // next tarjan from stack599++next;600do {601dom_depth[t->_control->_idx] = depth; // Set depth in dominator tree602NTarjan *dom_child = t->_dom_child;603t = t->_dom_next; // next tarjan604if (dom_child != NULL) {605*top = dom_child; // save child on stack606++top;607}608} while (t != NULL);609} while (next < last);610} while (last < top);611}612613#ifndef PRODUCT614void NTarjan::dump(int offset) const {615// Dump the data from this node616int i;617for(i = offset; i >0; i--) // Use indenting for tree structure618tty->print(" ");619tty->print("Dominator Node: ");620_control->dump(); // Control node for this dom node621tty->print("\n");622for(i = offset; i >0; i--) // Use indenting for tree structure623tty->print(" ");624tty->print("semi:%d, size:%d\n",_semi, _size);625for(i = offset; i >0; i--) // Use indenting for tree structure626tty->print(" ");627tty->print("DFS Parent: ");628if(_parent != NULL)629_parent->_control->dump(); // Parent in DFS630tty->print("\n");631for(i = offset; i >0; i--) // Use indenting for tree structure632tty->print(" ");633tty->print("Dom Parent: ");634if(_dom != NULL)635_dom->_control->dump(); // Parent in Dominator Tree636tty->print("\n");637638// Recurse over remaining tree639if( _dom_child ) _dom_child->dump(offset+2); // Children in dominator tree640if( _dom_next ) _dom_next ->dump(offset ); // Siblings in dominator tree641642}643#endif644645646