Path: blob/master/src/hotspot/share/opto/domgraph.cpp
40930 views
/*1* Copyright (c) 1997, 2016, 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 "memory/resourceArea.hpp"28#include "opto/block.hpp"29#include "opto/machnode.hpp"30#include "opto/phaseX.hpp"31#include "opto/rootnode.hpp"3233// Portions of code courtesy of Clifford Click3435// A data structure that holds all the information needed to find dominators.36struct Tarjan {37Block *_block; // Basic block for this info3839uint _semi; // Semi-dominators40uint _size; // Used for faster LINK and EVAL41Tarjan *_parent; // Parent in DFS42Tarjan *_label; // Used for LINK and EVAL43Tarjan *_ancestor; // Used for LINK and EVAL44Tarjan *_child; // Used for faster LINK and EVAL45Tarjan *_dom; // Parent in dominator tree (immediate dom)46Tarjan *_bucket; // Set of vertices with given semidominator4748Tarjan *_dom_child; // Child in dominator tree49Tarjan *_dom_next; // Next in dominator tree5051// Fast union-find work52void COMPRESS();53Tarjan *EVAL(void);54void LINK( Tarjan *w, Tarjan *tarjan0 );5556void setdepth( uint size );5758};5960// Compute the dominator tree of the CFG. The CFG must already have been61// constructed. This is the Lengauer & Tarjan O(E-alpha(E,V)) algorithm.62void PhaseCFG::build_dominator_tree() {63// Pre-grow the blocks array, prior to the ResourceMark kicking in64_blocks.map(number_of_blocks(), 0);6566ResourceMark rm;67// Setup mappings from my Graph to Tarjan's stuff and back68// Note: Tarjan uses 1-based arrays69Tarjan* tarjan = NEW_RESOURCE_ARRAY(Tarjan, number_of_blocks() + 1);7071// Tarjan's algorithm, almost verbatim:72// Step 1:73uint dfsnum = do_DFS(tarjan, number_of_blocks());74if (dfsnum - 1 != number_of_blocks()) { // Check for unreachable loops!75// If the returned dfsnum does not match the number of blocks, then we76// must have some unreachable loops. These can be made at any time by77// IterGVN. They are cleaned up by CCP or the loop opts, but the last78// IterGVN can always make more that are not cleaned up. Highly unlikely79// except in ZKM.jar, where endless irreducible loops cause the loop opts80// to not get run.81//82// Having found unreachable loops, we have made a bad RPO _block layout.83// We can re-run the above DFS pass with the correct number of blocks,84// and hack the Tarjan algorithm below to be robust in the presence of85// such dead loops (as was done for the NTarjan code farther below).86// Since this situation is so unlikely, instead I've decided to bail out.87// CNC 7/24/200188C->record_method_not_compilable("unreachable loop");89return;90}91_blocks._cnt = number_of_blocks();9293// Tarjan is using 1-based arrays, so these are some initialize flags94tarjan[0]._size = tarjan[0]._semi = 0;95tarjan[0]._label = &tarjan[0];9697for (uint i = number_of_blocks(); i >= 2; i--) { // For all vertices in DFS order98Tarjan *w = &tarjan[i]; // Get vertex from DFS99100// Step 2:101Node *whead = w->_block->head();102for (uint j = 1; j < whead->req(); j++) {103Block* b = get_block_for_node(whead->in(j));104Tarjan *vx = &tarjan[b->_pre_order];105Tarjan *u = vx->EVAL();106if( u->_semi < w->_semi )107w->_semi = u->_semi;108}109110// w is added to a bucket here, and only here.111// Thus w is in at most one bucket and the sum of all bucket sizes is O(n).112// Thus bucket can be a linked list.113// Thus we do not need a small integer name for each Block.114w->_bucket = tarjan[w->_semi]._bucket;115tarjan[w->_semi]._bucket = w;116117w->_parent->LINK( w, &tarjan[0] );118119// Step 3:120for( Tarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {121Tarjan *u = vx->EVAL();122vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;123}124}125126// Step 4:127for (uint i = 2; i <= number_of_blocks(); i++) {128Tarjan *w = &tarjan[i];129if( w->_dom != &tarjan[w->_semi] )130w->_dom = w->_dom->_dom;131w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later132}133// No immediate dominator for the root134Tarjan *w = &tarjan[get_root_block()->_pre_order];135w->_dom = NULL;136w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later137138// Convert the dominator tree array into my kind of graph139for(uint i = 1; i <= number_of_blocks(); i++){ // For all Tarjan vertices140Tarjan *t = &tarjan[i]; // Handy access141Tarjan *tdom = t->_dom; // Handy access to immediate dominator142if( tdom ) { // Root has no immediate dominator143t->_block->_idom = tdom->_block; // Set immediate dominator144t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child145tdom->_dom_child = t; // Make me a child of my parent146} else147t->_block->_idom = NULL; // Root148}149w->setdepth(number_of_blocks() + 1); // Set depth in dominator tree150151}152153class Block_Stack {154private:155struct Block_Descr {156Block *block; // Block157int index; // Index of block's successor pushed on stack158int freq_idx; // Index of block's most frequent successor159};160Block_Descr *_stack_top;161Block_Descr *_stack_max;162Block_Descr *_stack;163Tarjan *_tarjan;164uint most_frequent_successor( Block *b );165public:166Block_Stack(Tarjan *tarjan, int size) : _tarjan(tarjan) {167_stack = NEW_RESOURCE_ARRAY(Block_Descr, size);168_stack_max = _stack + size;169_stack_top = _stack - 1; // stack is empty170}171void push(uint pre_order, Block *b) {172Tarjan *t = &_tarjan[pre_order]; // Fast local access173b->_pre_order = pre_order; // Flag as visited174t->_block = b; // Save actual block175t->_semi = pre_order; // Block to DFS map176t->_label = t; // DFS to vertex map177t->_ancestor = NULL; // Fast LINK & EVAL setup178t->_child = &_tarjan[0]; // Sentenial179t->_size = 1;180t->_bucket = NULL;181if (pre_order == 1)182t->_parent = NULL; // first block doesn't have parent183else {184// Save parent (current top block on stack) in DFS185t->_parent = &_tarjan[_stack_top->block->_pre_order];186}187// Now put this block on stack188++_stack_top;189assert(_stack_top < _stack_max, ""); // assert if stack have to grow190_stack_top->block = b;191_stack_top->index = -1;192// Find the index into b->succs[] array of the most frequent successor.193_stack_top->freq_idx = most_frequent_successor(b); // freq_idx >= 0194}195Block* pop() { Block* b = _stack_top->block; _stack_top--; return b; }196bool is_nonempty() { return (_stack_top >= _stack); }197bool last_successor() { return (_stack_top->index == _stack_top->freq_idx); }198Block* next_successor() {199int i = _stack_top->index;200i++;201if (i == _stack_top->freq_idx) i++;202if (i >= (int)(_stack_top->block->_num_succs)) {203i = _stack_top->freq_idx; // process most frequent successor last204}205_stack_top->index = i;206return _stack_top->block->_succs[ i ];207}208};209210// Find the index into the b->succs[] array of the most frequent successor.211uint Block_Stack::most_frequent_successor( Block *b ) {212uint freq_idx = 0;213int eidx = b->end_idx();214Node *n = b->get_node(eidx);215int op = n->is_Mach() ? n->as_Mach()->ideal_Opcode() : n->Opcode();216switch( op ) {217case Op_CountedLoopEnd:218case Op_If: { // Split frequency amongst children219float prob = n->as_MachIf()->_prob;220// Is succ[0] the TRUE branch or the FALSE branch?221if( b->get_node(eidx+1)->Opcode() == Op_IfFalse )222prob = 1.0f - prob;223freq_idx = prob < PROB_FAIR; // freq=1 for succ[0] < 0.5 prob224break;225}226case Op_Catch: // Split frequency amongst children227for( freq_idx = 0; freq_idx < b->_num_succs; freq_idx++ )228if( b->get_node(eidx+1+freq_idx)->as_CatchProj()->_con == CatchProjNode::fall_through_index )229break;230// Handle case of no fall-thru (e.g., check-cast MUST throw an exception)231if( freq_idx == b->_num_succs ) freq_idx = 0;232break;233// Currently there is no support for finding out the most234// frequent successor for jumps, so lets just make it the first one235case Op_Jump:236case Op_Root:237case Op_Goto:238case Op_NeverBranch:239freq_idx = 0; // fall thru240break;241case Op_TailCall:242case Op_TailJump:243case Op_Return:244case Op_Halt:245case Op_Rethrow:246break;247default:248ShouldNotReachHere();249}250return freq_idx;251}252253// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup254// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.255uint PhaseCFG::do_DFS(Tarjan *tarjan, uint rpo_counter) {256Block* root_block = get_root_block();257uint pre_order = 1;258// Allocate stack of size number_of_blocks() + 1 to avoid frequent realloc259Block_Stack bstack(tarjan, number_of_blocks() + 1);260261// Push on stack the state for the first block262bstack.push(pre_order, root_block);263++pre_order;264265while (bstack.is_nonempty()) {266if (!bstack.last_successor()) {267// Walk over all successors in pre-order (DFS).268Block* next_block = bstack.next_successor();269if (next_block->_pre_order == 0) { // Check for no-pre-order, not-visited270// Push on stack the state of successor271bstack.push(pre_order, next_block);272++pre_order;273}274}275else {276// Build a reverse post-order in the CFG _blocks array277Block *stack_top = bstack.pop();278stack_top->_rpo = --rpo_counter;279_blocks.map(stack_top->_rpo, stack_top);280}281}282return pre_order;283}284285void Tarjan::COMPRESS()286{287assert( _ancestor != 0, "" );288if( _ancestor->_ancestor != 0 ) {289_ancestor->COMPRESS( );290if( _ancestor->_label->_semi < _label->_semi )291_label = _ancestor->_label;292_ancestor = _ancestor->_ancestor;293}294}295296Tarjan *Tarjan::EVAL() {297if( !_ancestor ) return _label;298COMPRESS();299return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;300}301302void Tarjan::LINK( Tarjan *w, Tarjan *tarjan0 ) {303Tarjan *s = w;304while( w->_label->_semi < s->_child->_label->_semi ) {305if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {306s->_child->_ancestor = s;307s->_child = s->_child->_child;308} else {309s->_child->_size = s->_size;310s = s->_ancestor = s->_child;311}312}313s->_label = w->_label;314_size += w->_size;315if( _size < (w->_size << 1) ) {316Tarjan *tmp = s; s = _child; _child = tmp;317}318while( s != tarjan0 ) {319s->_ancestor = this;320s = s->_child;321}322}323324void Tarjan::setdepth( uint stack_size ) {325Tarjan **top = NEW_RESOURCE_ARRAY(Tarjan*, stack_size);326Tarjan **next = top;327Tarjan **last;328uint depth = 0;329*top = this;330++top;331do {332// next level333++depth;334last = top;335do {336// Set current depth for all tarjans on this level337Tarjan *t = *next; // next tarjan from stack338++next;339do {340t->_block->_dom_depth = depth; // Set depth in dominator tree341Tarjan *dom_child = t->_dom_child;342t = t->_dom_next; // next tarjan343if (dom_child != NULL) {344*top = dom_child; // save child on stack345++top;346}347} while (t != NULL);348} while (next < last);349} while (last < top);350}351352// Compute dominators on the Sea of Nodes form353// A data structure that holds all the information needed to find dominators.354struct NTarjan {355Node *_control; // Control node associated with this info356357uint _semi; // Semi-dominators358uint _size; // Used for faster LINK and EVAL359NTarjan *_parent; // Parent in DFS360NTarjan *_label; // Used for LINK and EVAL361NTarjan *_ancestor; // Used for LINK and EVAL362NTarjan *_child; // Used for faster LINK and EVAL363NTarjan *_dom; // Parent in dominator tree (immediate dom)364NTarjan *_bucket; // Set of vertices with given semidominator365366NTarjan *_dom_child; // Child in dominator tree367NTarjan *_dom_next; // Next in dominator tree368369// Perform DFS search.370// Setup 'vertex' as DFS to vertex mapping.371// Setup 'semi' as vertex to DFS mapping.372// Set 'parent' to DFS parent.373static int DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder );374void setdepth( uint size, uint *dom_depth );375376// Fast union-find work377void COMPRESS();378NTarjan *EVAL(void);379void LINK( NTarjan *w, NTarjan *ntarjan0 );380#ifndef PRODUCT381void dump(int offset) const;382#endif383};384385// Compute the dominator tree of the sea of nodes. This version walks all CFG386// nodes (using the is_CFG() call) and places them in a dominator tree. Thus,387// it needs a count of the CFG nodes for the mapping table. This is the388// Lengauer & Tarjan O(E-alpha(E,V)) algorithm.389void PhaseIdealLoop::Dominators() {390ResourceMark rm;391// Setup mappings from my Graph to Tarjan's stuff and back392// Note: Tarjan uses 1-based arrays393NTarjan *ntarjan = NEW_RESOURCE_ARRAY(NTarjan,C->unique()+1);394// Initialize _control field for fast reference395int i;396for( i= C->unique()-1; i>=0; i-- )397ntarjan[i]._control = NULL;398399// Store the DFS order for the main loop400const uint fill_value = max_juint;401uint *dfsorder = NEW_RESOURCE_ARRAY(uint,C->unique()+1);402memset(dfsorder, fill_value, (C->unique()+1) * sizeof(uint));403404// Tarjan's algorithm, almost verbatim:405// Step 1:406VectorSet visited;407int dfsnum = NTarjan::DFS( ntarjan, visited, this, dfsorder);408409// Tarjan is using 1-based arrays, so these are some initialize flags410ntarjan[0]._size = ntarjan[0]._semi = 0;411ntarjan[0]._label = &ntarjan[0];412413for( i = dfsnum-1; i>1; i-- ) { // For all nodes in reverse DFS order414NTarjan *w = &ntarjan[i]; // Get Node from DFS415assert(w->_control != NULL,"bad DFS walk");416417// Step 2:418Node *whead = w->_control;419for( uint j=0; j < whead->req(); j++ ) { // For each predecessor420if( whead->in(j) == NULL || !whead->in(j)->is_CFG() )421continue; // Only process control nodes422uint b = dfsorder[whead->in(j)->_idx];423if(b == fill_value) continue;424NTarjan *vx = &ntarjan[b];425NTarjan *u = vx->EVAL();426if( u->_semi < w->_semi )427w->_semi = u->_semi;428}429430// w is added to a bucket here, and only here.431// Thus w is in at most one bucket and the sum of all bucket sizes is O(n).432// Thus bucket can be a linked list.433w->_bucket = ntarjan[w->_semi]._bucket;434ntarjan[w->_semi]._bucket = w;435436w->_parent->LINK( w, &ntarjan[0] );437438// Step 3:439for( NTarjan *vx = w->_parent->_bucket; vx; vx = vx->_bucket ) {440NTarjan *u = vx->EVAL();441vx->_dom = (u->_semi < vx->_semi) ? u : w->_parent;442}443444// Cleanup any unreachable loops now. Unreachable loops are loops that445// flow into the main graph (and hence into ROOT) but are not reachable446// from above. Such code is dead, but requires a global pass to detect447// it; this global pass was the 'build_loop_tree' pass run just prior.448if( !_verify_only && whead->is_Region() ) {449for( uint i = 1; i < whead->req(); i++ ) {450if (!has_node(whead->in(i))) {451// Kill dead input path452assert( !visited.test(whead->in(i)->_idx),453"input with no loop must be dead" );454_igvn.delete_input_of(whead, i);455for (DUIterator_Fast jmax, j = whead->fast_outs(jmax); j < jmax; j++) {456Node* p = whead->fast_out(j);457if( p->is_Phi() ) {458_igvn.delete_input_of(p, i);459}460}461i--; // Rerun same iteration462} // End of if dead input path463} // End of for all input paths464} // End if if whead is a Region465} // End of for all Nodes in reverse DFS order466467// Step 4:468for( i=2; i < dfsnum; i++ ) { // DFS order469NTarjan *w = &ntarjan[i];470assert(w->_control != NULL,"Bad DFS walk");471if( w->_dom != &ntarjan[w->_semi] )472w->_dom = w->_dom->_dom;473w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later474}475// No immediate dominator for the root476NTarjan *w = &ntarjan[dfsorder[C->root()->_idx]];477w->_dom = NULL;478w->_parent = NULL;479w->_dom_next = w->_dom_child = NULL; // Initialize for building tree later480481// Convert the dominator tree array into my kind of graph482for( i=1; i<dfsnum; i++ ) { // For all Tarjan vertices483NTarjan *t = &ntarjan[i]; // Handy access484assert(t->_control != NULL,"Bad DFS walk");485NTarjan *tdom = t->_dom; // Handy access to immediate dominator486if( tdom ) { // Root has no immediate dominator487_idom[t->_control->_idx] = tdom->_control; // Set immediate dominator488t->_dom_next = tdom->_dom_child; // Make me a sibling of parent's child489tdom->_dom_child = t; // Make me a child of my parent490} else491_idom[C->root()->_idx] = NULL; // Root492}493w->setdepth( C->unique()+1, _dom_depth ); // Set depth in dominator tree494// Pick up the 'top' node as well495_idom [C->top()->_idx] = C->root();496_dom_depth[C->top()->_idx] = 1;497498// Debug Print of Dominator tree499if( PrintDominators ) {500#ifndef PRODUCT501w->dump(0);502#endif503}504}505506// Perform DFS search. Setup 'vertex' as DFS to vertex mapping. Setup507// 'semi' as vertex to DFS mapping. Set 'parent' to DFS parent.508int NTarjan::DFS( NTarjan *ntarjan, VectorSet &visited, PhaseIdealLoop *pil, uint *dfsorder) {509// Allocate stack of size C->live_nodes()/8 to avoid frequent realloc510GrowableArray <Node *> dfstack(pil->C->live_nodes() >> 3);511Node *b = pil->C->root();512int dfsnum = 1;513dfsorder[b->_idx] = dfsnum; // Cache parent's dfsnum for a later use514dfstack.push(b);515516while (dfstack.is_nonempty()) {517b = dfstack.pop();518if( !visited.test_set(b->_idx) ) { // Test node and flag it as visited519NTarjan *w = &ntarjan[dfsnum];520// Only fully process control nodes521w->_control = b; // Save actual node522// Use parent's cached dfsnum to identify "Parent in DFS"523w->_parent = &ntarjan[dfsorder[b->_idx]];524dfsorder[b->_idx] = dfsnum; // Save DFS order info525w->_semi = dfsnum; // Node to DFS map526w->_label = w; // DFS to vertex map527w->_ancestor = NULL; // Fast LINK & EVAL setup528w->_child = &ntarjan[0]; // Sentinal529w->_size = 1;530w->_bucket = NULL;531532// Need DEF-USE info for this pass533for ( int i = b->outcnt(); i-- > 0; ) { // Put on stack backwards534Node* s = b->raw_out(i); // Get a use535// CFG nodes only and not dead stuff536if( s->is_CFG() && pil->has_node(s) && !visited.test(s->_idx) ) {537dfsorder[s->_idx] = dfsnum; // Cache parent's dfsnum for a later use538dfstack.push(s);539}540}541dfsnum++; // update after parent's dfsnum has been cached.542}543}544545return dfsnum;546}547548void NTarjan::COMPRESS()549{550assert( _ancestor != 0, "" );551if( _ancestor->_ancestor != 0 ) {552_ancestor->COMPRESS( );553if( _ancestor->_label->_semi < _label->_semi )554_label = _ancestor->_label;555_ancestor = _ancestor->_ancestor;556}557}558559NTarjan *NTarjan::EVAL() {560if( !_ancestor ) return _label;561COMPRESS();562return (_ancestor->_label->_semi >= _label->_semi) ? _label : _ancestor->_label;563}564565void NTarjan::LINK( NTarjan *w, NTarjan *ntarjan0 ) {566NTarjan *s = w;567while( w->_label->_semi < s->_child->_label->_semi ) {568if( s->_size + s->_child->_child->_size >= (s->_child->_size << 1) ) {569s->_child->_ancestor = s;570s->_child = s->_child->_child;571} else {572s->_child->_size = s->_size;573s = s->_ancestor = s->_child;574}575}576s->_label = w->_label;577_size += w->_size;578if( _size < (w->_size << 1) ) {579NTarjan *tmp = s; s = _child; _child = tmp;580}581while( s != ntarjan0 ) {582s->_ancestor = this;583s = s->_child;584}585}586587void NTarjan::setdepth( uint stack_size, uint *dom_depth ) {588NTarjan **top = NEW_RESOURCE_ARRAY(NTarjan*, stack_size);589NTarjan **next = top;590NTarjan **last;591uint depth = 0;592*top = this;593++top;594do {595// next level596++depth;597last = top;598do {599// Set current depth for all tarjans on this level600NTarjan *t = *next; // next tarjan from stack601++next;602do {603dom_depth[t->_control->_idx] = depth; // Set depth in dominator tree604NTarjan *dom_child = t->_dom_child;605t = t->_dom_next; // next tarjan606if (dom_child != NULL) {607*top = dom_child; // save child on stack608++top;609}610} while (t != NULL);611} while (next < last);612} while (last < top);613}614615#ifndef PRODUCT616void NTarjan::dump(int offset) const {617// Dump the data from this node618int i;619for(i = offset; i >0; i--) // Use indenting for tree structure620tty->print(" ");621tty->print("Dominator Node: ");622_control->dump(); // Control node for this dom node623tty->print("\n");624for(i = offset; i >0; i--) // Use indenting for tree structure625tty->print(" ");626tty->print("semi:%d, size:%d\n",_semi, _size);627for(i = offset; i >0; i--) // Use indenting for tree structure628tty->print(" ");629tty->print("DFS Parent: ");630if(_parent != NULL)631_parent->_control->dump(); // Parent in DFS632tty->print("\n");633for(i = offset; i >0; i--) // Use indenting for tree structure634tty->print(" ");635tty->print("Dom Parent: ");636if(_dom != NULL)637_dom->_control->dump(); // Parent in Dominator Tree638tty->print("\n");639640// Recurse over remaining tree641if( _dom_child ) _dom_child->dump(offset+2); // Children in dominator tree642if( _dom_next ) _dom_next ->dump(offset ); // Siblings in dominator tree643644}645#endif646647648