duke@435: /* duke@435: * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // Optimization - Graph Style duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_block.cpp.incl" duke@435: duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: void Block_Array::grow( uint i ) { duke@435: assert(i >= Max(), "must be an overflow"); duke@435: debug_only(_limit = i+1); duke@435: if( i < _size ) return; duke@435: if( !_size ) { duke@435: _size = 1; duke@435: _blocks = (Block**)_arena->Amalloc( _size * sizeof(Block*) ); duke@435: _blocks[0] = NULL; duke@435: } duke@435: uint old = _size; duke@435: while( i >= _size ) _size <<= 1; // Double to fit duke@435: _blocks = (Block**)_arena->Arealloc( _blocks, old*sizeof(Block*),_size*sizeof(Block*)); duke@435: Copy::zero_to_bytes( &_blocks[old], (_size-old)*sizeof(Block*) ); duke@435: } duke@435: duke@435: //============================================================================= duke@435: void Block_List::remove(uint i) { duke@435: assert(i < _cnt, "index out of bounds"); duke@435: Copy::conjoint_words_to_lower((HeapWord*)&_blocks[i+1], (HeapWord*)&_blocks[i], ((_cnt-i-1)*sizeof(Block*))); duke@435: pop(); // shrink list by one block duke@435: } duke@435: duke@435: void Block_List::insert(uint i, Block *b) { duke@435: push(b); // grow list by one block duke@435: Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*))); duke@435: _blocks[i] = b; duke@435: } duke@435: duke@435: duke@435: //============================================================================= duke@435: duke@435: uint Block::code_alignment() { duke@435: // Check for Root block duke@435: if( _pre_order == 0 ) return CodeEntryAlignment; duke@435: // Check for Start block duke@435: if( _pre_order == 1 ) return InteriorEntryAlignment; duke@435: // Check for loop alignment duke@435: Node *h = head(); duke@435: if( h->is_Loop() && h->as_Loop()->is_inner_loop() ) { duke@435: // Pre- and post-loops have low trip count so do not bother with duke@435: // NOPs for align loop head. The constants are hidden from tuning duke@435: // but only because my "divide by 4" heuristic surely gets nearly duke@435: // all possible gain (a "do not align at all" heuristic has a duke@435: // chance of getting a really tiny gain). duke@435: if( h->is_CountedLoop() && (h->as_CountedLoop()->is_pre_loop() || duke@435: h->as_CountedLoop()->is_post_loop()) ) duke@435: return (OptoLoopAlignment > 4) ? (OptoLoopAlignment>>2) : 1; duke@435: // Loops with low backedge frequency should not be aligned. duke@435: Node *n = h->in(LoopNode::LoopBackControl)->in(0); duke@435: if( n->is_MachIf() && n->as_MachIf()->_prob < 0.01 ) { duke@435: return 1; // Loop does not loop, more often than not! duke@435: } duke@435: return OptoLoopAlignment; // Otherwise align loop head duke@435: } duke@435: return 1; // no particular alignment duke@435: } duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: // Compute the size of first 'inst_cnt' instructions in this block. duke@435: // Return the number of instructions left to compute if the block has duke@435: // less then 'inst_cnt' instructions. duke@435: uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt, duke@435: PhaseRegAlloc* ra) { duke@435: uint last_inst = _nodes.size(); duke@435: for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) { duke@435: uint inst_size = _nodes[j]->size(ra); duke@435: if( inst_size > 0 ) { duke@435: inst_cnt--; duke@435: uint sz = sum_size + inst_size; duke@435: if( sz <= (uint)OptoLoopAlignment ) { duke@435: // Compute size of instructions which fit into fetch buffer only duke@435: // since all inst_cnt instructions will not fit even if we align them. duke@435: sum_size = sz; duke@435: } else { duke@435: return 0; duke@435: } duke@435: } duke@435: } duke@435: return inst_cnt; duke@435: } duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: uint Block::find_node( const Node *n ) const { duke@435: for( uint i = 0; i < _nodes.size(); i++ ) { duke@435: if( _nodes[i] == n ) duke@435: return i; duke@435: } duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: } duke@435: duke@435: // Find and remove n from block list duke@435: void Block::find_remove( const Node *n ) { duke@435: _nodes.remove(find_node(n)); duke@435: } duke@435: duke@435: //------------------------------is_Empty--------------------------------------- duke@435: // Return empty status of a block. Empty blocks contain only the head, other duke@435: // ideal nodes, and an optional trailing goto. duke@435: int Block::is_Empty() const { duke@435: duke@435: // Root or start block is not considered empty duke@435: if (head()->is_Root() || head()->is_Start()) { duke@435: return not_empty; duke@435: } duke@435: duke@435: int success_result = completely_empty; duke@435: int end_idx = _nodes.size()-1; duke@435: duke@435: // Check for ending goto duke@435: if ((end_idx > 0) && (_nodes[end_idx]->is_Goto())) { duke@435: success_result = empty_with_goto; duke@435: end_idx--; duke@435: } duke@435: duke@435: // Unreachable blocks are considered empty duke@435: if (num_preds() <= 1) { duke@435: return success_result; duke@435: } duke@435: duke@435: // Ideal nodes are allowable in empty blocks: skip them Only MachNodes duke@435: // turn directly into code, because only MachNodes have non-trivial duke@435: // emit() functions. duke@435: while ((end_idx > 0) && !_nodes[end_idx]->is_Mach()) { duke@435: end_idx--; duke@435: } duke@435: duke@435: // No room for any interesting instructions? duke@435: if (end_idx == 0) { duke@435: return success_result; duke@435: } duke@435: duke@435: return not_empty; duke@435: } duke@435: duke@435: //------------------------------has_uncommon_code------------------------------ duke@435: // Return true if the block's code implies that it is not likely to be duke@435: // executed infrequently. Check to see if the block ends in a Halt or duke@435: // a low probability call. duke@435: bool Block::has_uncommon_code() const { duke@435: Node* en = end(); duke@435: duke@435: if (en->is_Goto()) duke@435: en = en->in(0); duke@435: if (en->is_Catch()) duke@435: en = en->in(0); duke@435: if (en->is_Proj() && en->in(0)->is_MachCall()) { duke@435: MachCallNode* call = en->in(0)->as_MachCall(); duke@435: if (call->cnt() != COUNT_UNKNOWN && call->cnt() <= PROB_UNLIKELY_MAG(4)) { duke@435: // This is true for slow-path stubs like new_{instance,array}, duke@435: // slow_arraycopy, complete_monitor_locking, uncommon_trap. duke@435: // The magic number corresponds to the probability of an uncommon_trap, duke@435: // even though it is a count not a probability. duke@435: return true; duke@435: } duke@435: } duke@435: duke@435: int op = en->is_Mach() ? en->as_Mach()->ideal_Opcode() : en->Opcode(); duke@435: return op == Op_Halt; duke@435: } duke@435: duke@435: //------------------------------is_uncommon------------------------------------ duke@435: // True if block is low enough frequency or guarded by a test which duke@435: // mostly does not go here. duke@435: bool Block::is_uncommon( Block_Array &bbs ) const { duke@435: // Initial blocks must never be moved, so are never uncommon. duke@435: if (head()->is_Root() || head()->is_Start()) return false; duke@435: duke@435: // Check for way-low freq duke@435: if( _freq < BLOCK_FREQUENCY(0.00001f) ) return true; duke@435: duke@435: // Look for code shape indicating uncommon_trap or slow path duke@435: if (has_uncommon_code()) return true; duke@435: duke@435: const float epsilon = 0.05f; duke@435: const float guard_factor = PROB_UNLIKELY_MAG(4) / (1.f - epsilon); duke@435: uint uncommon_preds = 0; duke@435: uint freq_preds = 0; duke@435: uint uncommon_for_freq_preds = 0; duke@435: duke@435: for( uint i=1; i_idx]; duke@435: // Check to see if this block follows its guard 1 time out of 10000 duke@435: // or less. duke@435: // duke@435: // See list of magnitude-4 unlikely probabilities in cfgnode.hpp which duke@435: // we intend to be "uncommon", such as slow-path TLE allocation, duke@435: // predicted call failure, and uncommon trap triggers. duke@435: // duke@435: // Use an epsilon value of 5% to allow for variability in frequency duke@435: // predictions and floating point calculations. The net effect is duke@435: // that guard_factor is set to 9500. duke@435: // duke@435: // Ignore low-frequency blocks. duke@435: // The next check is (guard->_freq < 1.e-5 * 9500.). duke@435: if(guard->_freq*BLOCK_FREQUENCY(guard_factor) < BLOCK_FREQUENCY(0.00001f)) { duke@435: uncommon_preds++; duke@435: } else { duke@435: freq_preds++; duke@435: if( _freq < guard->_freq * guard_factor ) { duke@435: uncommon_for_freq_preds++; duke@435: } duke@435: } duke@435: } duke@435: if( num_preds() > 1 && duke@435: // The block is uncommon if all preds are uncommon or duke@435: (uncommon_preds == (num_preds()-1) || duke@435: // it is uncommon for all frequent preds. duke@435: uncommon_for_freq_preds == freq_preds) ) { duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: //------------------------------dump------------------------------------------- duke@435: #ifndef PRODUCT duke@435: void Block::dump_bidx(const Block* orig) const { duke@435: if (_pre_order) tty->print("B%d",_pre_order); duke@435: else tty->print("N%d", head()->_idx); duke@435: duke@435: if (Verbose && orig != this) { duke@435: // Dump the original block's idx duke@435: tty->print(" ("); duke@435: orig->dump_bidx(orig); duke@435: tty->print(")"); duke@435: } duke@435: } duke@435: duke@435: void Block::dump_pred(const Block_Array *bbs, Block* orig) const { duke@435: if (is_connector()) { duke@435: for (uint i=1; i_idx]); duke@435: p->dump_pred(bbs, orig); duke@435: } duke@435: } else { duke@435: dump_bidx(orig); duke@435: tty->print(" "); duke@435: } duke@435: } duke@435: duke@435: void Block::dump_head( const Block_Array *bbs ) const { duke@435: // Print the basic block duke@435: dump_bidx(this); duke@435: tty->print(": #\t"); duke@435: duke@435: // Print the incoming CFG edges and the outgoing CFG edges duke@435: for( uint i=0; i<_num_succs; i++ ) { duke@435: non_connector_successor(i)->dump_bidx(_succs[i]); duke@435: tty->print(" "); duke@435: } duke@435: tty->print("<- "); duke@435: if( head()->is_block_start() ) { duke@435: for (uint i=1; i_idx]; duke@435: p->dump_pred(bbs, p); duke@435: } else { duke@435: while (!s->is_block_start()) duke@435: s = s->in(0); duke@435: tty->print("N%d ", s->_idx ); duke@435: } duke@435: } duke@435: } else duke@435: tty->print("BLOCK HEAD IS JUNK "); duke@435: duke@435: // Print loop, if any duke@435: const Block *bhead = this; // Head of self-loop duke@435: Node *bh = bhead->head(); duke@435: if( bbs && bh->is_Loop() && !head()->is_Root() ) { duke@435: LoopNode *loop = bh->as_Loop(); duke@435: const Block *bx = (*bbs)[loop->in(LoopNode::LoopBackControl)->_idx]; duke@435: while (bx->is_connector()) { duke@435: bx = (*bbs)[bx->pred(1)->_idx]; duke@435: } duke@435: tty->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order); duke@435: // Dump any loop-specific bits, especially for CountedLoops. duke@435: loop->dump_spec(tty); duke@435: } duke@435: tty->print(" Freq: %g",_freq); duke@435: if( Verbose || WizardMode ) { duke@435: tty->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth); duke@435: tty->print(" RegPressure: %d",_reg_pressure); duke@435: tty->print(" IHRP Index: %d",_ihrp_index); duke@435: tty->print(" FRegPressure: %d",_freg_pressure); duke@435: tty->print(" FHRP Index: %d",_fhrp_index); duke@435: } duke@435: tty->print_cr(""); duke@435: } duke@435: duke@435: void Block::dump() const { dump(0); } duke@435: duke@435: void Block::dump( const Block_Array *bbs ) const { duke@435: dump_head(bbs); duke@435: uint cnt = _nodes.size(); duke@435: for( uint i=0; idump(); duke@435: tty->print("\n"); duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: //------------------------------PhaseCFG--------------------------------------- duke@435: PhaseCFG::PhaseCFG( Arena *a, RootNode *r, Matcher &m ) : duke@435: Phase(CFG), duke@435: _bbs(a), duke@435: _root(r) duke@435: #ifndef PRODUCT duke@435: , _trace_opto_pipelining(TraceOptoPipelining || C->method_has_option("TraceOptoPipelining")) duke@435: #endif duke@435: { duke@435: ResourceMark rm; duke@435: // I'll need a few machine-specific GotoNodes. Make an Ideal GotoNode, duke@435: // then Match it into a machine-specific Node. Then clone the machine duke@435: // Node on demand. duke@435: Node *x = new (C, 1) GotoNode(NULL); duke@435: x->init_req(0, x); duke@435: _goto = m.match_tree(x); duke@435: assert(_goto != NULL, ""); duke@435: _goto->set_req(0,_goto); duke@435: duke@435: // Build the CFG in Reverse Post Order duke@435: _num_blocks = build_cfg(); duke@435: _broot = _bbs[_root->_idx]; duke@435: } duke@435: duke@435: //------------------------------build_cfg-------------------------------------- duke@435: // Build a proper looking CFG. Make every block begin with either a StartNode duke@435: // or a RegionNode. Make every block end with either a Goto, If or Return. duke@435: // The RootNode both starts and ends it's own block. Do this with a recursive duke@435: // backwards walk over the control edges. duke@435: uint PhaseCFG::build_cfg() { duke@435: Arena *a = Thread::current()->resource_area(); duke@435: VectorSet visited(a); duke@435: duke@435: // Allocate stack with enough space to avoid frequent realloc duke@435: Node_Stack nstack(a, C->unique() >> 1); duke@435: nstack.push(_root, 0); duke@435: uint sum = 0; // Counter for blocks duke@435: duke@435: while (nstack.is_nonempty()) { duke@435: // node and in's index from stack's top duke@435: // 'np' is _root (see above) or RegionNode, StartNode: we push on stack duke@435: // only nodes which point to the start of basic block (see below). duke@435: Node *np = nstack.node(); duke@435: // idx > 0, except for the first node (_root) pushed on stack duke@435: // at the beginning when idx == 0. duke@435: // We will use the condition (idx == 0) later to end the build. duke@435: uint idx = nstack.index(); duke@435: Node *proj = np->in(idx); duke@435: const Node *x = proj->is_block_proj(); duke@435: // Does the block end with a proper block-ending Node? One of Return, duke@435: // If or Goto? (This check should be done for visited nodes also). duke@435: if (x == NULL) { // Does not end right... duke@435: Node *g = _goto->clone(); // Force it to end in a Goto duke@435: g->set_req(0, proj); duke@435: np->set_req(idx, g); duke@435: x = proj = g; duke@435: } duke@435: if (!visited.test_set(x->_idx)) { // Visit this block once duke@435: // Skip any control-pinned middle'in stuff duke@435: Node *p = proj; duke@435: do { duke@435: proj = p; // Update pointer to last Control duke@435: p = p->in(0); // Move control forward duke@435: } while( !p->is_block_proj() && duke@435: !p->is_block_start() ); duke@435: // Make the block begin with one of Region or StartNode. duke@435: if( !p->is_block_start() ) { duke@435: RegionNode *r = new (C, 2) RegionNode( 2 ); duke@435: r->init_req(1, p); // Insert RegionNode in the way duke@435: proj->set_req(0, r); // Insert RegionNode in the way duke@435: p = r; duke@435: } duke@435: // 'p' now points to the start of this basic block duke@435: duke@435: // Put self in array of basic blocks duke@435: Block *bb = new (_bbs._arena) Block(_bbs._arena,p); duke@435: _bbs.map(p->_idx,bb); duke@435: _bbs.map(x->_idx,bb); duke@435: if( x != p ) // Only for root is x == p duke@435: bb->_nodes.push((Node*)x); duke@435: duke@435: // Now handle predecessors duke@435: ++sum; // Count 1 for self block duke@435: uint cnt = bb->num_preds(); duke@435: for (int i = (cnt - 1); i > 0; i-- ) { // For all predecessors duke@435: Node *prevproj = p->in(i); // Get prior input duke@435: assert( !prevproj->is_Con(), "dead input not removed" ); duke@435: // Check to see if p->in(i) is a "control-dependent" CFG edge - duke@435: // i.e., it splits at the source (via an IF or SWITCH) and merges duke@435: // at the destination (via a many-input Region). duke@435: // This breaks critical edges. The RegionNode to start the block duke@435: // will be added when is pulled off the node stack duke@435: if ( cnt > 2 ) { // Merging many things? duke@435: assert( prevproj== bb->pred(i),""); duke@435: if(prevproj->is_block_proj() != prevproj) { // Control-dependent edge? duke@435: // Force a block on the control-dependent edge duke@435: Node *g = _goto->clone(); // Force it to end in a Goto duke@435: g->set_req(0,prevproj); duke@435: p->set_req(i,g); duke@435: } duke@435: } duke@435: nstack.push(p, i); // 'p' is RegionNode or StartNode duke@435: } duke@435: } else { // Post-processing visited nodes duke@435: nstack.pop(); // remove node from stack duke@435: // Check if it the fist node pushed on stack at the beginning. duke@435: if (idx == 0) break; // end of the build duke@435: // Find predecessor basic block duke@435: Block *pb = _bbs[x->_idx]; duke@435: // Insert into nodes array, if not already there duke@435: if( !_bbs.lookup(proj->_idx) ) { duke@435: assert( x != proj, "" ); duke@435: // Map basic block of projection duke@435: _bbs.map(proj->_idx,pb); duke@435: pb->_nodes.push(proj); duke@435: } duke@435: // Insert self as a child of my predecessor block duke@435: pb->_succs.map(pb->_num_succs++, _bbs[np->_idx]); duke@435: assert( pb->_nodes[ pb->_nodes.size() - pb->_num_succs ]->is_block_proj(), duke@435: "too many control users, not a CFG?" ); duke@435: } duke@435: } duke@435: // Return number of basic blocks for all children and self duke@435: return sum; duke@435: } duke@435: duke@435: //------------------------------insert_goto_at--------------------------------- duke@435: // Inserts a goto & corresponding basic block between duke@435: // block[block_no] and its succ_no'th successor block duke@435: void PhaseCFG::insert_goto_at(uint block_no, uint succ_no) { duke@435: // get block with block_no duke@435: assert(block_no < _num_blocks, "illegal block number"); duke@435: Block* in = _blocks[block_no]; duke@435: // get successor block succ_no duke@435: assert(succ_no < in->_num_succs, "illegal successor number"); duke@435: Block* out = in->_succs[succ_no]; duke@435: // get ProjNode corresponding to the succ_no'th successor of the in block duke@435: ProjNode* proj = in->_nodes[in->_nodes.size() - in->_num_succs + succ_no]->as_Proj(); duke@435: // create region for basic block duke@435: RegionNode* region = new (C, 2) RegionNode(2); duke@435: region->init_req(1, proj); duke@435: // setup corresponding basic block duke@435: Block* block = new (_bbs._arena) Block(_bbs._arena, region); duke@435: _bbs.map(region->_idx, block); duke@435: C->regalloc()->set_bad(region->_idx); duke@435: // add a goto node duke@435: Node* gto = _goto->clone(); // get a new goto node duke@435: gto->set_req(0, region); duke@435: // add it to the basic block duke@435: block->_nodes.push(gto); duke@435: _bbs.map(gto->_idx, block); duke@435: C->regalloc()->set_bad(gto->_idx); duke@435: // hook up successor block duke@435: block->_succs.map(block->_num_succs++, out); duke@435: // remap successor's predecessors if necessary duke@435: for (uint i = 1; i < out->num_preds(); i++) { duke@435: if (out->pred(i) == proj) out->head()->set_req(i, gto); duke@435: } duke@435: // remap predecessor's successor to new block duke@435: in->_succs.map(succ_no, block); duke@435: // add new basic block to basic block list duke@435: _blocks.insert(block_no + 1, block); duke@435: _num_blocks++; duke@435: } duke@435: duke@435: //------------------------------no_flip_branch--------------------------------- duke@435: // Does this block end in a multiway branch that cannot have the default case duke@435: // flipped for another case? duke@435: static bool no_flip_branch( Block *b ) { duke@435: int branch_idx = b->_nodes.size() - b->_num_succs-1; duke@435: if( branch_idx < 1 ) return false; duke@435: Node *bra = b->_nodes[branch_idx]; duke@435: if( bra->is_Catch() ) return true; duke@435: if( bra->is_Mach() ) { duke@435: if( bra->is_MachNullCheck() ) return true; duke@435: int iop = bra->as_Mach()->ideal_Opcode(); duke@435: if( iop == Op_FastLock || iop == Op_FastUnlock ) duke@435: return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: //------------------------------convert_NeverBranch_to_Goto-------------------- duke@435: // Check for NeverBranch at block end. This needs to become a GOTO to the duke@435: // true target. NeverBranch are treated as a conditional branch that always duke@435: // goes the same direction for most of the optimizer and are used to give a duke@435: // fake exit path to infinite loops. At this late stage they need to turn duke@435: // into Goto's so that when you enter the infinite loop you indeed hang. duke@435: void PhaseCFG::convert_NeverBranch_to_Goto(Block *b) { duke@435: // Find true target duke@435: int end_idx = b->end_idx(); duke@435: int idx = b->_nodes[end_idx+1]->as_Proj()->_con; duke@435: Block *succ = b->_succs[idx]; duke@435: Node* gto = _goto->clone(); // get a new goto node duke@435: gto->set_req(0, b->head()); duke@435: Node *bp = b->_nodes[end_idx]; duke@435: b->_nodes.map(end_idx,gto); // Slam over NeverBranch duke@435: _bbs.map(gto->_idx, b); duke@435: C->regalloc()->set_bad(gto->_idx); duke@435: b->_nodes.pop(); // Yank projections duke@435: b->_nodes.pop(); // Yank projections duke@435: b->_succs.map(0,succ); // Map only successor duke@435: b->_num_succs = 1; duke@435: // remap successor's predecessors if necessary duke@435: uint j; duke@435: for( j = 1; j < succ->num_preds(); j++) duke@435: if( succ->pred(j)->in(0) == bp ) duke@435: succ->head()->set_req(j, gto); duke@435: // Kill alternate exit path duke@435: Block *dead = b->_succs[1-idx]; duke@435: for( j = 1; j < dead->num_preds(); j++) duke@435: if( dead->pred(j)->in(0) == bp ) duke@435: break; duke@435: // Scan through block, yanking dead path from duke@435: // all regions and phis. duke@435: dead->head()->del_req(j); duke@435: for( int k = 1; dead->_nodes[k]->is_Phi(); k++ ) duke@435: dead->_nodes[k]->del_req(j); duke@435: } duke@435: duke@435: //------------------------------MoveToNext------------------------------------- duke@435: // Helper function to move block bx to the slot following b_index. Return duke@435: // true if the move is successful, otherwise false duke@435: bool PhaseCFG::MoveToNext(Block* bx, uint b_index) { duke@435: if (bx == NULL) return false; duke@435: duke@435: // Return false if bx is already scheduled. duke@435: uint bx_index = bx->_pre_order; duke@435: if ((bx_index <= b_index) && (_blocks[bx_index] == bx)) { duke@435: return false; duke@435: } duke@435: duke@435: // Find the current index of block bx on the block list duke@435: bx_index = b_index + 1; duke@435: while( bx_index < _num_blocks && _blocks[bx_index] != bx ) bx_index++; duke@435: assert(_blocks[bx_index] == bx, "block not found"); duke@435: duke@435: // If the previous block conditionally falls into bx, return false, duke@435: // because moving bx will create an extra jump. duke@435: for(uint k = 1; k < bx->num_preds(); k++ ) { duke@435: Block* pred = _bbs[bx->pred(k)->_idx]; duke@435: if (pred == _blocks[bx_index-1]) { duke@435: if (pred->_num_succs != 1) { duke@435: return false; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Reinsert bx just past block 'b' duke@435: _blocks.remove(bx_index); duke@435: _blocks.insert(b_index + 1, bx); duke@435: return true; duke@435: } duke@435: duke@435: //------------------------------MoveToEnd-------------------------------------- duke@435: // Move empty and uncommon blocks to the end. duke@435: void PhaseCFG::MoveToEnd(Block *b, uint i) { duke@435: int e = b->is_Empty(); duke@435: if (e != Block::not_empty) { duke@435: if (e == Block::empty_with_goto) { duke@435: // Remove the goto, but leave the block. duke@435: b->_nodes.pop(); duke@435: } duke@435: // Mark this block as a connector block, which will cause it to be duke@435: // ignored in certain functions such as non_connector_successor(). duke@435: b->set_connector(); duke@435: } duke@435: // Move the empty block to the end, and don't recheck. duke@435: _blocks.remove(i); duke@435: _blocks.push(b); duke@435: } duke@435: duke@435: //------------------------------RemoveEmpty------------------------------------ duke@435: // Remove empty basic blocks and useless branches. duke@435: void PhaseCFG::RemoveEmpty() { duke@435: // Move uncommon blocks to the end duke@435: uint last = _num_blocks; duke@435: uint i; duke@435: assert( _blocks[0] == _broot, "" ); duke@435: for( i = 1; i < last; i++ ) { duke@435: Block *b = _blocks[i]; duke@435: duke@435: // Check for NeverBranch at block end. This needs to become a GOTO to the duke@435: // true target. NeverBranch are treated as a conditional branch that duke@435: // always goes the same direction for most of the optimizer and are used duke@435: // to give a fake exit path to infinite loops. At this late stage they duke@435: // need to turn into Goto's so that when you enter the infinite loop you duke@435: // indeed hang. duke@435: if( b->_nodes[b->end_idx()]->Opcode() == Op_NeverBranch ) duke@435: convert_NeverBranch_to_Goto(b); duke@435: duke@435: // Look for uncommon blocks and move to end. duke@435: if( b->is_uncommon(_bbs) ) { duke@435: MoveToEnd(b, i); duke@435: last--; // No longer check for being uncommon! duke@435: if( no_flip_branch(b) ) { // Fall-thru case must follow? duke@435: b = _blocks[i]; // Find the fall-thru block duke@435: MoveToEnd(b, i); duke@435: last--; duke@435: } duke@435: i--; // backup block counter post-increment duke@435: } duke@435: } duke@435: duke@435: // Remove empty blocks duke@435: uint j1; duke@435: last = _num_blocks; duke@435: for( i=0; i < last; i++ ) { duke@435: Block *b = _blocks[i]; duke@435: if (i > 0) { duke@435: if (b->is_Empty() != Block::not_empty) { duke@435: MoveToEnd(b, i); duke@435: last--; duke@435: i--; duke@435: } duke@435: } duke@435: } // End of for all blocks duke@435: duke@435: // Fixup final control flow for the blocks. Remove jump-to-next duke@435: // block. If neither arm of a IF follows the conditional branch, we duke@435: // have to add a second jump after the conditional. We place the duke@435: // TRUE branch target in succs[0] for both GOTOs and IFs. duke@435: for( i=0; i < _num_blocks; i++ ) { duke@435: Block *b = _blocks[i]; duke@435: b->_pre_order = i; // turn pre-order into block-index duke@435: duke@435: // Connector blocks need no further processing. duke@435: if (b->is_connector()) { duke@435: assert((i+1) == _num_blocks || _blocks[i+1]->is_connector(), duke@435: "All connector blocks should sink to the end"); duke@435: continue; duke@435: } duke@435: assert(b->is_Empty() != Block::completely_empty, duke@435: "Empty blocks should be connectors"); duke@435: duke@435: Block *bnext = (i < _num_blocks-1) ? _blocks[i+1] : NULL; duke@435: Block *bs0 = b->non_connector_successor(0); duke@435: duke@435: // Check for multi-way branches where I cannot negate the test to duke@435: // exchange the true and false targets. duke@435: if( no_flip_branch( b ) ) { duke@435: // Find fall through case - if must fall into its target duke@435: int branch_idx = b->_nodes.size() - b->_num_succs; duke@435: for (uint j2 = 0; j2 < b->_num_succs; j2++) { duke@435: const ProjNode* p = b->_nodes[branch_idx + j2]->as_Proj(); duke@435: if (p->_con == 0) { duke@435: // successor j2 is fall through case duke@435: if (b->non_connector_successor(j2) != bnext) { duke@435: // but it is not the next block => insert a goto duke@435: insert_goto_at(i, j2); duke@435: } duke@435: // Put taken branch in slot 0 duke@435: if( j2 == 0 && b->_num_succs == 2) { duke@435: // Flip targets in succs map duke@435: Block *tbs0 = b->_succs[0]; duke@435: Block *tbs1 = b->_succs[1]; duke@435: b->_succs.map( 0, tbs1 ); duke@435: b->_succs.map( 1, tbs0 ); duke@435: } duke@435: break; duke@435: } duke@435: } duke@435: // Remove all CatchProjs duke@435: for (j1 = 0; j1 < b->_num_succs; j1++) b->_nodes.pop(); duke@435: duke@435: } else if (b->_num_succs == 1) { duke@435: // Block ends in a Goto? duke@435: if (bnext == bs0) { duke@435: // We fall into next block; remove the Goto duke@435: b->_nodes.pop(); duke@435: } duke@435: duke@435: } else if( b->_num_succs == 2 ) { // Block ends in a If? duke@435: // Get opcode of 1st projection (matches _succs[0]) duke@435: // Note: Since this basic block has 2 exits, the last 2 nodes must duke@435: // be projections (in any order), the 3rd last node must be duke@435: // the IfNode (we have excluded other 2-way exits such as duke@435: // CatchNodes already). duke@435: MachNode *iff = b->_nodes[b->_nodes.size()-3]->as_Mach(); duke@435: ProjNode *proj0 = b->_nodes[b->_nodes.size()-2]->as_Proj(); duke@435: ProjNode *proj1 = b->_nodes[b->_nodes.size()-1]->as_Proj(); duke@435: duke@435: // Assert that proj0 and succs[0] match up. Similarly for proj1 and succs[1]. duke@435: assert(proj0->raw_out(0) == b->_succs[0]->head(), "Mismatch successor 0"); duke@435: assert(proj1->raw_out(0) == b->_succs[1]->head(), "Mismatch successor 1"); duke@435: duke@435: Block *bs1 = b->non_connector_successor(1); duke@435: duke@435: // Check for neither successor block following the current duke@435: // block ending in a conditional. If so, move one of the duke@435: // successors after the current one, provided that the duke@435: // successor was previously unscheduled, but moveable duke@435: // (i.e., all paths to it involve a branch). duke@435: if( bnext != bs0 && bnext != bs1 ) { duke@435: duke@435: // Choose the more common successor based on the probability duke@435: // of the conditional branch. duke@435: Block *bx = bs0; duke@435: Block *by = bs1; duke@435: duke@435: // _prob is the probability of taking the true path. Make duke@435: // p the probability of taking successor #1. duke@435: float p = iff->as_MachIf()->_prob; duke@435: if( proj0->Opcode() == Op_IfTrue ) { duke@435: p = 1.0 - p; duke@435: } duke@435: duke@435: // Prefer successor #1 if p > 0.5 duke@435: if (p > PROB_FAIR) { duke@435: bx = bs1; duke@435: by = bs0; duke@435: } duke@435: duke@435: // Attempt the more common successor first duke@435: if (MoveToNext(bx, i)) { duke@435: bnext = bx; duke@435: } else if (MoveToNext(by, i)) { duke@435: bnext = by; duke@435: } duke@435: } duke@435: duke@435: // Check for conditional branching the wrong way. Negate duke@435: // conditional, if needed, so it falls into the following block duke@435: // and branches to the not-following block. duke@435: duke@435: // Check for the next block being in succs[0]. We are going to branch duke@435: // to succs[0], so we want the fall-thru case as the next block in duke@435: // succs[1]. duke@435: if (bnext == bs0) { duke@435: // Fall-thru case in succs[0], so flip targets in succs map duke@435: Block *tbs0 = b->_succs[0]; duke@435: Block *tbs1 = b->_succs[1]; duke@435: b->_succs.map( 0, tbs1 ); duke@435: b->_succs.map( 1, tbs0 ); duke@435: // Flip projection for each target duke@435: { ProjNode *tmp = proj0; proj0 = proj1; proj1 = tmp; } duke@435: duke@435: } else if( bnext == bs1 ) { // Fall-thru is already in succs[1] duke@435: duke@435: } else { // Else need a double-branch duke@435: duke@435: // The existing conditional branch need not change. duke@435: // Add a unconditional branch to the false target. duke@435: // Alas, it must appear in its own block and adding a duke@435: // block this late in the game is complicated. Sigh. duke@435: insert_goto_at(i, 1); duke@435: } duke@435: duke@435: // Make sure we TRUE branch to the target duke@435: if( proj0->Opcode() == Op_IfFalse ) duke@435: iff->negate(); duke@435: duke@435: b->_nodes.pop(); // Remove IfFalse & IfTrue projections duke@435: b->_nodes.pop(); duke@435: duke@435: } else { duke@435: // Multi-exit block, e.g. a switch statement duke@435: // But we don't need to do anything here duke@435: } duke@435: duke@435: } // End of for all blocks duke@435: duke@435: } duke@435: duke@435: duke@435: //------------------------------dump------------------------------------------- duke@435: #ifndef PRODUCT duke@435: void PhaseCFG::_dump_cfg( const Node *end, VectorSet &visited ) const { duke@435: const Node *x = end->is_block_proj(); duke@435: assert( x, "not a CFG" ); duke@435: duke@435: // Do not visit this block again duke@435: if( visited.test_set(x->_idx) ) return; duke@435: duke@435: // Skip through this block duke@435: const Node *p = x; duke@435: do { duke@435: p = p->in(0); // Move control forward duke@435: assert( !p->is_block_proj() || p->is_Root(), "not a CFG" ); duke@435: } while( !p->is_block_start() ); duke@435: duke@435: // Recursively visit duke@435: for( uint i=1; ireq(); i++ ) duke@435: _dump_cfg(p->in(i),visited); duke@435: duke@435: // Dump the block duke@435: _bbs[p->_idx]->dump(&_bbs); duke@435: } duke@435: duke@435: void PhaseCFG::dump( ) const { duke@435: tty->print("\n--- CFG --- %d BBs\n",_num_blocks); duke@435: if( _blocks.size() ) { // Did we do basic-block layout? duke@435: for( uint i=0; i<_num_blocks; i++ ) duke@435: _blocks[i]->dump(&_bbs); duke@435: } else { // Else do it with a DFS duke@435: VectorSet visited(_bbs._arena); duke@435: _dump_cfg(_root,visited); duke@435: } duke@435: } duke@435: duke@435: void PhaseCFG::dump_headers() { duke@435: for( uint i = 0; i < _num_blocks; i++ ) { duke@435: if( _blocks[i] == NULL ) continue; duke@435: _blocks[i]->dump_head(&_bbs); duke@435: } duke@435: } duke@435: duke@435: void PhaseCFG::verify( ) const { duke@435: // Verify sane CFG duke@435: for( uint i = 0; i < _num_blocks; i++ ) { duke@435: Block *b = _blocks[i]; duke@435: uint cnt = b->_nodes.size(); duke@435: uint j; duke@435: for( j = 0; j < cnt; j++ ) { duke@435: Node *n = b->_nodes[j]; duke@435: assert( _bbs[n->_idx] == b, "" ); duke@435: if( j >= 1 && n->is_Mach() && duke@435: n->as_Mach()->ideal_Opcode() == Op_CreateEx ) { duke@435: assert( j == 1 || b->_nodes[j-1]->is_Phi(), duke@435: "CreateEx must be first instruction in block" ); duke@435: } duke@435: for( uint k = 0; k < n->req(); k++ ) { duke@435: Node *use = n->in(k); duke@435: if( use && use != n ) { duke@435: assert( _bbs[use->_idx] || use->is_Con(), duke@435: "must have block; constants for debug info ok" ); duke@435: } duke@435: } duke@435: } duke@435: duke@435: j = b->end_idx(); duke@435: Node *bp = (Node*)b->_nodes[b->_nodes.size()-1]->is_block_proj(); duke@435: assert( bp, "last instruction must be a block proj" ); duke@435: assert( bp == b->_nodes[j], "wrong number of successors for this block" ); duke@435: if( bp->is_Catch() ) { duke@435: while( b->_nodes[--j]->Opcode() == Op_MachProj ) ; duke@435: assert( b->_nodes[j]->is_Call(), "CatchProj must follow call" ); duke@435: } duke@435: else if( bp->is_Mach() && bp->as_Mach()->ideal_Opcode() == Op_If ) { duke@435: assert( b->_num_succs == 2, "Conditional branch must have two targets"); duke@435: } duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: //============================================================================= duke@435: //------------------------------UnionFind-------------------------------------- duke@435: UnionFind::UnionFind( uint max ) : _cnt(max), _max(max), _indices(NEW_RESOURCE_ARRAY(uint,max)) { duke@435: Copy::zero_to_bytes( _indices, sizeof(uint)*max ); duke@435: } duke@435: duke@435: void UnionFind::extend( uint from_idx, uint to_idx ) { duke@435: _nesting.check(); duke@435: if( from_idx >= _max ) { duke@435: uint size = 16; duke@435: while( size <= from_idx ) size <<=1; duke@435: _indices = REALLOC_RESOURCE_ARRAY( uint, _indices, _max, size ); duke@435: _max = size; duke@435: } duke@435: while( _cnt <= from_idx ) _indices[_cnt++] = 0; duke@435: _indices[from_idx] = to_idx; duke@435: } duke@435: duke@435: void UnionFind::reset( uint max ) { duke@435: assert( max <= max_uint, "Must fit within uint" ); duke@435: // Force the Union-Find mapping to be at least this large duke@435: extend(max,0); duke@435: // Initialize to be the ID mapping. duke@435: for( uint i=0; i<_max; i++ ) map(i,i); duke@435: } duke@435: duke@435: //------------------------------Find_compress---------------------------------- duke@435: // Straight out of Tarjan's union-find algorithm duke@435: uint UnionFind::Find_compress( uint idx ) { duke@435: uint cur = idx; duke@435: uint next = lookup(cur); duke@435: while( next != cur ) { // Scan chain of equivalences duke@435: assert( next < cur, "always union smaller" ); duke@435: cur = next; // until find a fixed-point duke@435: next = lookup(cur); duke@435: } duke@435: // Core of union-find algorithm: update chain of duke@435: // equivalences to be equal to the root. duke@435: while( idx != next ) { duke@435: uint tmp = lookup(idx); duke@435: map(idx, next); duke@435: idx = tmp; duke@435: } duke@435: return idx; duke@435: } duke@435: duke@435: //------------------------------Find_const------------------------------------- duke@435: // Like Find above, but no path compress, so bad asymptotic behavior duke@435: uint UnionFind::Find_const( uint idx ) const { duke@435: if( idx == 0 ) return idx; // Ignore the zero idx duke@435: // Off the end? This can happen during debugging dumps duke@435: // when data structures have not finished being updated. duke@435: if( idx >= _max ) return idx; duke@435: uint next = lookup(idx); duke@435: while( next != idx ) { // Scan chain of equivalences duke@435: assert( next < idx, "always union smaller" ); duke@435: idx = next; // until find a fixed-point duke@435: next = lookup(idx); duke@435: } duke@435: return next; duke@435: } duke@435: duke@435: //------------------------------Union------------------------------------------ duke@435: // union 2 sets together. duke@435: void UnionFind::Union( uint idx1, uint idx2 ) { duke@435: uint src = Find(idx1); duke@435: uint dst = Find(idx2); duke@435: assert( src, "" ); duke@435: assert( dst, "" ); duke@435: assert( src < _max, "oob" ); duke@435: assert( dst < _max, "oob" ); duke@435: assert( src < dst, "always union smaller" ); duke@435: map(dst,src); duke@435: }