annotate src/share/vm/opto/block.cpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 756b58154237
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 // Optimization - Graph Style
duke@0 26
duke@0 27 #include "incls/_precompiled.incl"
duke@0 28 #include "incls/_block.cpp.incl"
duke@0 29
duke@0 30
duke@0 31 //-----------------------------------------------------------------------------
duke@0 32 void Block_Array::grow( uint i ) {
duke@0 33 assert(i >= Max(), "must be an overflow");
duke@0 34 debug_only(_limit = i+1);
duke@0 35 if( i < _size ) return;
duke@0 36 if( !_size ) {
duke@0 37 _size = 1;
duke@0 38 _blocks = (Block**)_arena->Amalloc( _size * sizeof(Block*) );
duke@0 39 _blocks[0] = NULL;
duke@0 40 }
duke@0 41 uint old = _size;
duke@0 42 while( i >= _size ) _size <<= 1; // Double to fit
duke@0 43 _blocks = (Block**)_arena->Arealloc( _blocks, old*sizeof(Block*),_size*sizeof(Block*));
duke@0 44 Copy::zero_to_bytes( &_blocks[old], (_size-old)*sizeof(Block*) );
duke@0 45 }
duke@0 46
duke@0 47 //=============================================================================
duke@0 48 void Block_List::remove(uint i) {
duke@0 49 assert(i < _cnt, "index out of bounds");
duke@0 50 Copy::conjoint_words_to_lower((HeapWord*)&_blocks[i+1], (HeapWord*)&_blocks[i], ((_cnt-i-1)*sizeof(Block*)));
duke@0 51 pop(); // shrink list by one block
duke@0 52 }
duke@0 53
duke@0 54 void Block_List::insert(uint i, Block *b) {
duke@0 55 push(b); // grow list by one block
duke@0 56 Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*)));
duke@0 57 _blocks[i] = b;
duke@0 58 }
duke@0 59
duke@0 60
duke@0 61 //=============================================================================
duke@0 62
duke@0 63 uint Block::code_alignment() {
duke@0 64 // Check for Root block
duke@0 65 if( _pre_order == 0 ) return CodeEntryAlignment;
duke@0 66 // Check for Start block
duke@0 67 if( _pre_order == 1 ) return InteriorEntryAlignment;
duke@0 68 // Check for loop alignment
duke@0 69 Node *h = head();
duke@0 70 if( h->is_Loop() && h->as_Loop()->is_inner_loop() ) {
duke@0 71 // Pre- and post-loops have low trip count so do not bother with
duke@0 72 // NOPs for align loop head. The constants are hidden from tuning
duke@0 73 // but only because my "divide by 4" heuristic surely gets nearly
duke@0 74 // all possible gain (a "do not align at all" heuristic has a
duke@0 75 // chance of getting a really tiny gain).
duke@0 76 if( h->is_CountedLoop() && (h->as_CountedLoop()->is_pre_loop() ||
duke@0 77 h->as_CountedLoop()->is_post_loop()) )
duke@0 78 return (OptoLoopAlignment > 4) ? (OptoLoopAlignment>>2) : 1;
duke@0 79 // Loops with low backedge frequency should not be aligned.
duke@0 80 Node *n = h->in(LoopNode::LoopBackControl)->in(0);
duke@0 81 if( n->is_MachIf() && n->as_MachIf()->_prob < 0.01 ) {
duke@0 82 return 1; // Loop does not loop, more often than not!
duke@0 83 }
duke@0 84 return OptoLoopAlignment; // Otherwise align loop head
duke@0 85 }
duke@0 86 return 1; // no particular alignment
duke@0 87 }
duke@0 88
duke@0 89 //-----------------------------------------------------------------------------
duke@0 90 // Compute the size of first 'inst_cnt' instructions in this block.
duke@0 91 // Return the number of instructions left to compute if the block has
duke@0 92 // less then 'inst_cnt' instructions.
duke@0 93 uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt,
duke@0 94 PhaseRegAlloc* ra) {
duke@0 95 uint last_inst = _nodes.size();
duke@0 96 for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) {
duke@0 97 uint inst_size = _nodes[j]->size(ra);
duke@0 98 if( inst_size > 0 ) {
duke@0 99 inst_cnt--;
duke@0 100 uint sz = sum_size + inst_size;
duke@0 101 if( sz <= (uint)OptoLoopAlignment ) {
duke@0 102 // Compute size of instructions which fit into fetch buffer only
duke@0 103 // since all inst_cnt instructions will not fit even if we align them.
duke@0 104 sum_size = sz;
duke@0 105 } else {
duke@0 106 return 0;
duke@0 107 }
duke@0 108 }
duke@0 109 }
duke@0 110 return inst_cnt;
duke@0 111 }
duke@0 112
duke@0 113 //-----------------------------------------------------------------------------
duke@0 114 uint Block::find_node( const Node *n ) const {
duke@0 115 for( uint i = 0; i < _nodes.size(); i++ ) {
duke@0 116 if( _nodes[i] == n )
duke@0 117 return i;
duke@0 118 }
duke@0 119 ShouldNotReachHere();
duke@0 120 return 0;
duke@0 121 }
duke@0 122
duke@0 123 // Find and remove n from block list
duke@0 124 void Block::find_remove( const Node *n ) {
duke@0 125 _nodes.remove(find_node(n));
duke@0 126 }
duke@0 127
duke@0 128 //------------------------------is_Empty---------------------------------------
duke@0 129 // Return empty status of a block. Empty blocks contain only the head, other
duke@0 130 // ideal nodes, and an optional trailing goto.
duke@0 131 int Block::is_Empty() const {
duke@0 132
duke@0 133 // Root or start block is not considered empty
duke@0 134 if (head()->is_Root() || head()->is_Start()) {
duke@0 135 return not_empty;
duke@0 136 }
duke@0 137
duke@0 138 int success_result = completely_empty;
duke@0 139 int end_idx = _nodes.size()-1;
duke@0 140
duke@0 141 // Check for ending goto
duke@0 142 if ((end_idx > 0) && (_nodes[end_idx]->is_Goto())) {
duke@0 143 success_result = empty_with_goto;
duke@0 144 end_idx--;
duke@0 145 }
duke@0 146
duke@0 147 // Unreachable blocks are considered empty
duke@0 148 if (num_preds() <= 1) {
duke@0 149 return success_result;
duke@0 150 }
duke@0 151
duke@0 152 // Ideal nodes are allowable in empty blocks: skip them Only MachNodes
duke@0 153 // turn directly into code, because only MachNodes have non-trivial
duke@0 154 // emit() functions.
duke@0 155 while ((end_idx > 0) && !_nodes[end_idx]->is_Mach()) {
duke@0 156 end_idx--;
duke@0 157 }
duke@0 158
duke@0 159 // No room for any interesting instructions?
duke@0 160 if (end_idx == 0) {
duke@0 161 return success_result;
duke@0 162 }
duke@0 163
duke@0 164 return not_empty;
duke@0 165 }
duke@0 166
duke@0 167 //------------------------------has_uncommon_code------------------------------
duke@0 168 // Return true if the block's code implies that it is not likely to be
duke@0 169 // executed infrequently. Check to see if the block ends in a Halt or
duke@0 170 // a low probability call.
duke@0 171 bool Block::has_uncommon_code() const {
duke@0 172 Node* en = end();
duke@0 173
duke@0 174 if (en->is_Goto())
duke@0 175 en = en->in(0);
duke@0 176 if (en->is_Catch())
duke@0 177 en = en->in(0);
duke@0 178 if (en->is_Proj() && en->in(0)->is_MachCall()) {
duke@0 179 MachCallNode* call = en->in(0)->as_MachCall();
duke@0 180 if (call->cnt() != COUNT_UNKNOWN && call->cnt() <= PROB_UNLIKELY_MAG(4)) {
duke@0 181 // This is true for slow-path stubs like new_{instance,array},
duke@0 182 // slow_arraycopy, complete_monitor_locking, uncommon_trap.
duke@0 183 // The magic number corresponds to the probability of an uncommon_trap,
duke@0 184 // even though it is a count not a probability.
duke@0 185 return true;
duke@0 186 }
duke@0 187 }
duke@0 188
duke@0 189 int op = en->is_Mach() ? en->as_Mach()->ideal_Opcode() : en->Opcode();
duke@0 190 return op == Op_Halt;
duke@0 191 }
duke@0 192
duke@0 193 //------------------------------is_uncommon------------------------------------
duke@0 194 // True if block is low enough frequency or guarded by a test which
duke@0 195 // mostly does not go here.
duke@0 196 bool Block::is_uncommon( Block_Array &bbs ) const {
duke@0 197 // Initial blocks must never be moved, so are never uncommon.
duke@0 198 if (head()->is_Root() || head()->is_Start()) return false;
duke@0 199
duke@0 200 // Check for way-low freq
duke@0 201 if( _freq < BLOCK_FREQUENCY(0.00001f) ) return true;
duke@0 202
duke@0 203 // Look for code shape indicating uncommon_trap or slow path
duke@0 204 if (has_uncommon_code()) return true;
duke@0 205
duke@0 206 const float epsilon = 0.05f;
duke@0 207 const float guard_factor = PROB_UNLIKELY_MAG(4) / (1.f - epsilon);
duke@0 208 uint uncommon_preds = 0;
duke@0 209 uint freq_preds = 0;
duke@0 210 uint uncommon_for_freq_preds = 0;
duke@0 211
duke@0 212 for( uint i=1; i<num_preds(); i++ ) {
duke@0 213 Block* guard = bbs[pred(i)->_idx];
duke@0 214 // Check to see if this block follows its guard 1 time out of 10000
duke@0 215 // or less.
duke@0 216 //
duke@0 217 // See list of magnitude-4 unlikely probabilities in cfgnode.hpp which
duke@0 218 // we intend to be "uncommon", such as slow-path TLE allocation,
duke@0 219 // predicted call failure, and uncommon trap triggers.
duke@0 220 //
duke@0 221 // Use an epsilon value of 5% to allow for variability in frequency
duke@0 222 // predictions and floating point calculations. The net effect is
duke@0 223 // that guard_factor is set to 9500.
duke@0 224 //
duke@0 225 // Ignore low-frequency blocks.
duke@0 226 // The next check is (guard->_freq < 1.e-5 * 9500.).
duke@0 227 if(guard->_freq*BLOCK_FREQUENCY(guard_factor) < BLOCK_FREQUENCY(0.00001f)) {
duke@0 228 uncommon_preds++;
duke@0 229 } else {
duke@0 230 freq_preds++;
duke@0 231 if( _freq < guard->_freq * guard_factor ) {
duke@0 232 uncommon_for_freq_preds++;
duke@0 233 }
duke@0 234 }
duke@0 235 }
duke@0 236 if( num_preds() > 1 &&
duke@0 237 // The block is uncommon if all preds are uncommon or
duke@0 238 (uncommon_preds == (num_preds()-1) ||
duke@0 239 // it is uncommon for all frequent preds.
duke@0 240 uncommon_for_freq_preds == freq_preds) ) {
duke@0 241 return true;
duke@0 242 }
duke@0 243 return false;
duke@0 244 }
duke@0 245
duke@0 246 //------------------------------dump-------------------------------------------
duke@0 247 #ifndef PRODUCT
duke@0 248 void Block::dump_bidx(const Block* orig) const {
duke@0 249 if (_pre_order) tty->print("B%d",_pre_order);
duke@0 250 else tty->print("N%d", head()->_idx);
duke@0 251
duke@0 252 if (Verbose && orig != this) {
duke@0 253 // Dump the original block's idx
duke@0 254 tty->print(" (");
duke@0 255 orig->dump_bidx(orig);
duke@0 256 tty->print(")");
duke@0 257 }
duke@0 258 }
duke@0 259
duke@0 260 void Block::dump_pred(const Block_Array *bbs, Block* orig) const {
duke@0 261 if (is_connector()) {
duke@0 262 for (uint i=1; i<num_preds(); i++) {
duke@0 263 Block *p = ((*bbs)[pred(i)->_idx]);
duke@0 264 p->dump_pred(bbs, orig);
duke@0 265 }
duke@0 266 } else {
duke@0 267 dump_bidx(orig);
duke@0 268 tty->print(" ");
duke@0 269 }
duke@0 270 }
duke@0 271
duke@0 272 void Block::dump_head( const Block_Array *bbs ) const {
duke@0 273 // Print the basic block
duke@0 274 dump_bidx(this);
duke@0 275 tty->print(": #\t");
duke@0 276
duke@0 277 // Print the incoming CFG edges and the outgoing CFG edges
duke@0 278 for( uint i=0; i<_num_succs; i++ ) {
duke@0 279 non_connector_successor(i)->dump_bidx(_succs[i]);
duke@0 280 tty->print(" ");
duke@0 281 }
duke@0 282 tty->print("<- ");
duke@0 283 if( head()->is_block_start() ) {
duke@0 284 for (uint i=1; i<num_preds(); i++) {
duke@0 285 Node *s = pred(i);
duke@0 286 if (bbs) {
duke@0 287 Block *p = (*bbs)[s->_idx];
duke@0 288 p->dump_pred(bbs, p);
duke@0 289 } else {
duke@0 290 while (!s->is_block_start())
duke@0 291 s = s->in(0);
duke@0 292 tty->print("N%d ", s->_idx );
duke@0 293 }
duke@0 294 }
duke@0 295 } else
duke@0 296 tty->print("BLOCK HEAD IS JUNK ");
duke@0 297
duke@0 298 // Print loop, if any
duke@0 299 const Block *bhead = this; // Head of self-loop
duke@0 300 Node *bh = bhead->head();
duke@0 301 if( bbs && bh->is_Loop() && !head()->is_Root() ) {
duke@0 302 LoopNode *loop = bh->as_Loop();
duke@0 303 const Block *bx = (*bbs)[loop->in(LoopNode::LoopBackControl)->_idx];
duke@0 304 while (bx->is_connector()) {
duke@0 305 bx = (*bbs)[bx->pred(1)->_idx];
duke@0 306 }
duke@0 307 tty->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order);
duke@0 308 // Dump any loop-specific bits, especially for CountedLoops.
duke@0 309 loop->dump_spec(tty);
duke@0 310 }
duke@0 311 tty->print(" Freq: %g",_freq);
duke@0 312 if( Verbose || WizardMode ) {
duke@0 313 tty->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth);
duke@0 314 tty->print(" RegPressure: %d",_reg_pressure);
duke@0 315 tty->print(" IHRP Index: %d",_ihrp_index);
duke@0 316 tty->print(" FRegPressure: %d",_freg_pressure);
duke@0 317 tty->print(" FHRP Index: %d",_fhrp_index);
duke@0 318 }
duke@0 319 tty->print_cr("");
duke@0 320 }
duke@0 321
duke@0 322 void Block::dump() const { dump(0); }
duke@0 323
duke@0 324 void Block::dump( const Block_Array *bbs ) const {
duke@0 325 dump_head(bbs);
duke@0 326 uint cnt = _nodes.size();
duke@0 327 for( uint i=0; i<cnt; i++ )
duke@0 328 _nodes[i]->dump();
duke@0 329 tty->print("\n");
duke@0 330 }
duke@0 331 #endif
duke@0 332
duke@0 333 //=============================================================================
duke@0 334 //------------------------------PhaseCFG---------------------------------------
duke@0 335 PhaseCFG::PhaseCFG( Arena *a, RootNode *r, Matcher &m ) :
duke@0 336 Phase(CFG),
duke@0 337 _bbs(a),
duke@0 338 _root(r)
duke@0 339 #ifndef PRODUCT
duke@0 340 , _trace_opto_pipelining(TraceOptoPipelining || C->method_has_option("TraceOptoPipelining"))
duke@0 341 #endif
duke@0 342 {
duke@0 343 ResourceMark rm;
duke@0 344 // I'll need a few machine-specific GotoNodes. Make an Ideal GotoNode,
duke@0 345 // then Match it into a machine-specific Node. Then clone the machine
duke@0 346 // Node on demand.
duke@0 347 Node *x = new (C, 1) GotoNode(NULL);
duke@0 348 x->init_req(0, x);
duke@0 349 _goto = m.match_tree(x);
duke@0 350 assert(_goto != NULL, "");
duke@0 351 _goto->set_req(0,_goto);
duke@0 352
duke@0 353 // Build the CFG in Reverse Post Order
duke@0 354 _num_blocks = build_cfg();
duke@0 355 _broot = _bbs[_root->_idx];
duke@0 356 }
duke@0 357
duke@0 358 //------------------------------build_cfg--------------------------------------
duke@0 359 // Build a proper looking CFG. Make every block begin with either a StartNode
duke@0 360 // or a RegionNode. Make every block end with either a Goto, If or Return.
duke@0 361 // The RootNode both starts and ends it's own block. Do this with a recursive
duke@0 362 // backwards walk over the control edges.
duke@0 363 uint PhaseCFG::build_cfg() {
duke@0 364 Arena *a = Thread::current()->resource_area();
duke@0 365 VectorSet visited(a);
duke@0 366
duke@0 367 // Allocate stack with enough space to avoid frequent realloc
duke@0 368 Node_Stack nstack(a, C->unique() >> 1);
duke@0 369 nstack.push(_root, 0);
duke@0 370 uint sum = 0; // Counter for blocks
duke@0 371
duke@0 372 while (nstack.is_nonempty()) {
duke@0 373 // node and in's index from stack's top
duke@0 374 // 'np' is _root (see above) or RegionNode, StartNode: we push on stack
duke@0 375 // only nodes which point to the start of basic block (see below).
duke@0 376 Node *np = nstack.node();
duke@0 377 // idx > 0, except for the first node (_root) pushed on stack
duke@0 378 // at the beginning when idx == 0.
duke@0 379 // We will use the condition (idx == 0) later to end the build.
duke@0 380 uint idx = nstack.index();
duke@0 381 Node *proj = np->in(idx);
duke@0 382 const Node *x = proj->is_block_proj();
duke@0 383 // Does the block end with a proper block-ending Node? One of Return,
duke@0 384 // If or Goto? (This check should be done for visited nodes also).
duke@0 385 if (x == NULL) { // Does not end right...
duke@0 386 Node *g = _goto->clone(); // Force it to end in a Goto
duke@0 387 g->set_req(0, proj);
duke@0 388 np->set_req(idx, g);
duke@0 389 x = proj = g;
duke@0 390 }
duke@0 391 if (!visited.test_set(x->_idx)) { // Visit this block once
duke@0 392 // Skip any control-pinned middle'in stuff
duke@0 393 Node *p = proj;
duke@0 394 do {
duke@0 395 proj = p; // Update pointer to last Control
duke@0 396 p = p->in(0); // Move control forward
duke@0 397 } while( !p->is_block_proj() &&
duke@0 398 !p->is_block_start() );
duke@0 399 // Make the block begin with one of Region or StartNode.
duke@0 400 if( !p->is_block_start() ) {
duke@0 401 RegionNode *r = new (C, 2) RegionNode( 2 );
duke@0 402 r->init_req(1, p); // Insert RegionNode in the way
duke@0 403 proj->set_req(0, r); // Insert RegionNode in the way
duke@0 404 p = r;
duke@0 405 }
duke@0 406 // 'p' now points to the start of this basic block
duke@0 407
duke@0 408 // Put self in array of basic blocks
duke@0 409 Block *bb = new (_bbs._arena) Block(_bbs._arena,p);
duke@0 410 _bbs.map(p->_idx,bb);
duke@0 411 _bbs.map(x->_idx,bb);
duke@0 412 if( x != p ) // Only for root is x == p
duke@0 413 bb->_nodes.push((Node*)x);
duke@0 414
duke@0 415 // Now handle predecessors
duke@0 416 ++sum; // Count 1 for self block
duke@0 417 uint cnt = bb->num_preds();
duke@0 418 for (int i = (cnt - 1); i > 0; i-- ) { // For all predecessors
duke@0 419 Node *prevproj = p->in(i); // Get prior input
duke@0 420 assert( !prevproj->is_Con(), "dead input not removed" );
duke@0 421 // Check to see if p->in(i) is a "control-dependent" CFG edge -
duke@0 422 // i.e., it splits at the source (via an IF or SWITCH) and merges
duke@0 423 // at the destination (via a many-input Region).
duke@0 424 // This breaks critical edges. The RegionNode to start the block
duke@0 425 // will be added when <p,i> is pulled off the node stack
duke@0 426 if ( cnt > 2 ) { // Merging many things?
duke@0 427 assert( prevproj== bb->pred(i),"");
duke@0 428 if(prevproj->is_block_proj() != prevproj) { // Control-dependent edge?
duke@0 429 // Force a block on the control-dependent edge
duke@0 430 Node *g = _goto->clone(); // Force it to end in a Goto
duke@0 431 g->set_req(0,prevproj);
duke@0 432 p->set_req(i,g);
duke@0 433 }
duke@0 434 }
duke@0 435 nstack.push(p, i); // 'p' is RegionNode or StartNode
duke@0 436 }
duke@0 437 } else { // Post-processing visited nodes
duke@0 438 nstack.pop(); // remove node from stack
duke@0 439 // Check if it the fist node pushed on stack at the beginning.
duke@0 440 if (idx == 0) break; // end of the build
duke@0 441 // Find predecessor basic block
duke@0 442 Block *pb = _bbs[x->_idx];
duke@0 443 // Insert into nodes array, if not already there
duke@0 444 if( !_bbs.lookup(proj->_idx) ) {
duke@0 445 assert( x != proj, "" );
duke@0 446 // Map basic block of projection
duke@0 447 _bbs.map(proj->_idx,pb);
duke@0 448 pb->_nodes.push(proj);
duke@0 449 }
duke@0 450 // Insert self as a child of my predecessor block
duke@0 451 pb->_succs.map(pb->_num_succs++, _bbs[np->_idx]);
duke@0 452 assert( pb->_nodes[ pb->_nodes.size() - pb->_num_succs ]->is_block_proj(),
duke@0 453 "too many control users, not a CFG?" );
duke@0 454 }
duke@0 455 }
duke@0 456 // Return number of basic blocks for all children and self
duke@0 457 return sum;
duke@0 458 }
duke@0 459
duke@0 460 //------------------------------insert_goto_at---------------------------------
duke@0 461 // Inserts a goto & corresponding basic block between
duke@0 462 // block[block_no] and its succ_no'th successor block
duke@0 463 void PhaseCFG::insert_goto_at(uint block_no, uint succ_no) {
duke@0 464 // get block with block_no
duke@0 465 assert(block_no < _num_blocks, "illegal block number");
duke@0 466 Block* in = _blocks[block_no];
duke@0 467 // get successor block succ_no
duke@0 468 assert(succ_no < in->_num_succs, "illegal successor number");
duke@0 469 Block* out = in->_succs[succ_no];
duke@0 470 // get ProjNode corresponding to the succ_no'th successor of the in block
duke@0 471 ProjNode* proj = in->_nodes[in->_nodes.size() - in->_num_succs + succ_no]->as_Proj();
duke@0 472 // create region for basic block
duke@0 473 RegionNode* region = new (C, 2) RegionNode(2);
duke@0 474 region->init_req(1, proj);
duke@0 475 // setup corresponding basic block
duke@0 476 Block* block = new (_bbs._arena) Block(_bbs._arena, region);
duke@0 477 _bbs.map(region->_idx, block);
duke@0 478 C->regalloc()->set_bad(region->_idx);
duke@0 479 // add a goto node
duke@0 480 Node* gto = _goto->clone(); // get a new goto node
duke@0 481 gto->set_req(0, region);
duke@0 482 // add it to the basic block
duke@0 483 block->_nodes.push(gto);
duke@0 484 _bbs.map(gto->_idx, block);
duke@0 485 C->regalloc()->set_bad(gto->_idx);
duke@0 486 // hook up successor block
duke@0 487 block->_succs.map(block->_num_succs++, out);
duke@0 488 // remap successor's predecessors if necessary
duke@0 489 for (uint i = 1; i < out->num_preds(); i++) {
duke@0 490 if (out->pred(i) == proj) out->head()->set_req(i, gto);
duke@0 491 }
duke@0 492 // remap predecessor's successor to new block
duke@0 493 in->_succs.map(succ_no, block);
duke@0 494 // add new basic block to basic block list
duke@0 495 _blocks.insert(block_no + 1, block);
duke@0 496 _num_blocks++;
duke@0 497 }
duke@0 498
duke@0 499 //------------------------------no_flip_branch---------------------------------
duke@0 500 // Does this block end in a multiway branch that cannot have the default case
duke@0 501 // flipped for another case?
duke@0 502 static bool no_flip_branch( Block *b ) {
duke@0 503 int branch_idx = b->_nodes.size() - b->_num_succs-1;
duke@0 504 if( branch_idx < 1 ) return false;
duke@0 505 Node *bra = b->_nodes[branch_idx];
duke@0 506 if( bra->is_Catch() ) return true;
duke@0 507 if( bra->is_Mach() ) {
duke@0 508 if( bra->is_MachNullCheck() ) return true;
duke@0 509 int iop = bra->as_Mach()->ideal_Opcode();
duke@0 510 if( iop == Op_FastLock || iop == Op_FastUnlock )
duke@0 511 return true;
duke@0 512 }
duke@0 513 return false;
duke@0 514 }
duke@0 515
duke@0 516 //------------------------------convert_NeverBranch_to_Goto--------------------
duke@0 517 // Check for NeverBranch at block end. This needs to become a GOTO to the
duke@0 518 // true target. NeverBranch are treated as a conditional branch that always
duke@0 519 // goes the same direction for most of the optimizer and are used to give a
duke@0 520 // fake exit path to infinite loops. At this late stage they need to turn
duke@0 521 // into Goto's so that when you enter the infinite loop you indeed hang.
duke@0 522 void PhaseCFG::convert_NeverBranch_to_Goto(Block *b) {
duke@0 523 // Find true target
duke@0 524 int end_idx = b->end_idx();
duke@0 525 int idx = b->_nodes[end_idx+1]->as_Proj()->_con;
duke@0 526 Block *succ = b->_succs[idx];
duke@0 527 Node* gto = _goto->clone(); // get a new goto node
duke@0 528 gto->set_req(0, b->head());
duke@0 529 Node *bp = b->_nodes[end_idx];
duke@0 530 b->_nodes.map(end_idx,gto); // Slam over NeverBranch
duke@0 531 _bbs.map(gto->_idx, b);
duke@0 532 C->regalloc()->set_bad(gto->_idx);
duke@0 533 b->_nodes.pop(); // Yank projections
duke@0 534 b->_nodes.pop(); // Yank projections
duke@0 535 b->_succs.map(0,succ); // Map only successor
duke@0 536 b->_num_succs = 1;
duke@0 537 // remap successor's predecessors if necessary
duke@0 538 uint j;
duke@0 539 for( j = 1; j < succ->num_preds(); j++)
duke@0 540 if( succ->pred(j)->in(0) == bp )
duke@0 541 succ->head()->set_req(j, gto);
duke@0 542 // Kill alternate exit path
duke@0 543 Block *dead = b->_succs[1-idx];
duke@0 544 for( j = 1; j < dead->num_preds(); j++)
duke@0 545 if( dead->pred(j)->in(0) == bp )
duke@0 546 break;
duke@0 547 // Scan through block, yanking dead path from
duke@0 548 // all regions and phis.
duke@0 549 dead->head()->del_req(j);
duke@0 550 for( int k = 1; dead->_nodes[k]->is_Phi(); k++ )
duke@0 551 dead->_nodes[k]->del_req(j);
duke@0 552 }
duke@0 553
duke@0 554 //------------------------------MoveToNext-------------------------------------
duke@0 555 // Helper function to move block bx to the slot following b_index. Return
duke@0 556 // true if the move is successful, otherwise false
duke@0 557 bool PhaseCFG::MoveToNext(Block* bx, uint b_index) {
duke@0 558 if (bx == NULL) return false;
duke@0 559
duke@0 560 // Return false if bx is already scheduled.
duke@0 561 uint bx_index = bx->_pre_order;
duke@0 562 if ((bx_index <= b_index) && (_blocks[bx_index] == bx)) {
duke@0 563 return false;
duke@0 564 }
duke@0 565
duke@0 566 // Find the current index of block bx on the block list
duke@0 567 bx_index = b_index + 1;
duke@0 568 while( bx_index < _num_blocks && _blocks[bx_index] != bx ) bx_index++;
duke@0 569 assert(_blocks[bx_index] == bx, "block not found");
duke@0 570
duke@0 571 // If the previous block conditionally falls into bx, return false,
duke@0 572 // because moving bx will create an extra jump.
duke@0 573 for(uint k = 1; k < bx->num_preds(); k++ ) {
duke@0 574 Block* pred = _bbs[bx->pred(k)->_idx];
duke@0 575 if (pred == _blocks[bx_index-1]) {
duke@0 576 if (pred->_num_succs != 1) {
duke@0 577 return false;
duke@0 578 }
duke@0 579 }
duke@0 580 }
duke@0 581
duke@0 582 // Reinsert bx just past block 'b'
duke@0 583 _blocks.remove(bx_index);
duke@0 584 _blocks.insert(b_index + 1, bx);
duke@0 585 return true;
duke@0 586 }
duke@0 587
duke@0 588 //------------------------------MoveToEnd--------------------------------------
duke@0 589 // Move empty and uncommon blocks to the end.
duke@0 590 void PhaseCFG::MoveToEnd(Block *b, uint i) {
duke@0 591 int e = b->is_Empty();
duke@0 592 if (e != Block::not_empty) {
duke@0 593 if (e == Block::empty_with_goto) {
duke@0 594 // Remove the goto, but leave the block.
duke@0 595 b->_nodes.pop();
duke@0 596 }
duke@0 597 // Mark this block as a connector block, which will cause it to be
duke@0 598 // ignored in certain functions such as non_connector_successor().
duke@0 599 b->set_connector();
duke@0 600 }
duke@0 601 // Move the empty block to the end, and don't recheck.
duke@0 602 _blocks.remove(i);
duke@0 603 _blocks.push(b);
duke@0 604 }
duke@0 605
duke@0 606 //------------------------------RemoveEmpty------------------------------------
duke@0 607 // Remove empty basic blocks and useless branches.
duke@0 608 void PhaseCFG::RemoveEmpty() {
duke@0 609 // Move uncommon blocks to the end
duke@0 610 uint last = _num_blocks;
duke@0 611 uint i;
duke@0 612 assert( _blocks[0] == _broot, "" );
duke@0 613 for( i = 1; i < last; i++ ) {
duke@0 614 Block *b = _blocks[i];
duke@0 615
duke@0 616 // Check for NeverBranch at block end. This needs to become a GOTO to the
duke@0 617 // true target. NeverBranch are treated as a conditional branch that
duke@0 618 // always goes the same direction for most of the optimizer and are used
duke@0 619 // to give a fake exit path to infinite loops. At this late stage they
duke@0 620 // need to turn into Goto's so that when you enter the infinite loop you
duke@0 621 // indeed hang.
duke@0 622 if( b->_nodes[b->end_idx()]->Opcode() == Op_NeverBranch )
duke@0 623 convert_NeverBranch_to_Goto(b);
duke@0 624
duke@0 625 // Look for uncommon blocks and move to end.
duke@0 626 if( b->is_uncommon(_bbs) ) {
duke@0 627 MoveToEnd(b, i);
duke@0 628 last--; // No longer check for being uncommon!
duke@0 629 if( no_flip_branch(b) ) { // Fall-thru case must follow?
duke@0 630 b = _blocks[i]; // Find the fall-thru block
duke@0 631 MoveToEnd(b, i);
duke@0 632 last--;
duke@0 633 }
duke@0 634 i--; // backup block counter post-increment
duke@0 635 }
duke@0 636 }
duke@0 637
duke@0 638 // Remove empty blocks
duke@0 639 uint j1;
duke@0 640 last = _num_blocks;
duke@0 641 for( i=0; i < last; i++ ) {
duke@0 642 Block *b = _blocks[i];
duke@0 643 if (i > 0) {
duke@0 644 if (b->is_Empty() != Block::not_empty) {
duke@0 645 MoveToEnd(b, i);
duke@0 646 last--;
duke@0 647 i--;
duke@0 648 }
duke@0 649 }
duke@0 650 } // End of for all blocks
duke@0 651
duke@0 652 // Fixup final control flow for the blocks. Remove jump-to-next
duke@0 653 // block. If neither arm of a IF follows the conditional branch, we
duke@0 654 // have to add a second jump after the conditional. We place the
duke@0 655 // TRUE branch target in succs[0] for both GOTOs and IFs.
duke@0 656 for( i=0; i < _num_blocks; i++ ) {
duke@0 657 Block *b = _blocks[i];
duke@0 658 b->_pre_order = i; // turn pre-order into block-index
duke@0 659
duke@0 660 // Connector blocks need no further processing.
duke@0 661 if (b->is_connector()) {
duke@0 662 assert((i+1) == _num_blocks || _blocks[i+1]->is_connector(),
duke@0 663 "All connector blocks should sink to the end");
duke@0 664 continue;
duke@0 665 }
duke@0 666 assert(b->is_Empty() != Block::completely_empty,
duke@0 667 "Empty blocks should be connectors");
duke@0 668
duke@0 669 Block *bnext = (i < _num_blocks-1) ? _blocks[i+1] : NULL;
duke@0 670 Block *bs0 = b->non_connector_successor(0);
duke@0 671
duke@0 672 // Check for multi-way branches where I cannot negate the test to
duke@0 673 // exchange the true and false targets.
duke@0 674 if( no_flip_branch( b ) ) {
duke@0 675 // Find fall through case - if must fall into its target
duke@0 676 int branch_idx = b->_nodes.size() - b->_num_succs;
duke@0 677 for (uint j2 = 0; j2 < b->_num_succs; j2++) {
duke@0 678 const ProjNode* p = b->_nodes[branch_idx + j2]->as_Proj();
duke@0 679 if (p->_con == 0) {
duke@0 680 // successor j2 is fall through case
duke@0 681 if (b->non_connector_successor(j2) != bnext) {
duke@0 682 // but it is not the next block => insert a goto
duke@0 683 insert_goto_at(i, j2);
duke@0 684 }
duke@0 685 // Put taken branch in slot 0
duke@0 686 if( j2 == 0 && b->_num_succs == 2) {
duke@0 687 // Flip targets in succs map
duke@0 688 Block *tbs0 = b->_succs[0];
duke@0 689 Block *tbs1 = b->_succs[1];
duke@0 690 b->_succs.map( 0, tbs1 );
duke@0 691 b->_succs.map( 1, tbs0 );
duke@0 692 }
duke@0 693 break;
duke@0 694 }
duke@0 695 }
duke@0 696 // Remove all CatchProjs
duke@0 697 for (j1 = 0; j1 < b->_num_succs; j1++) b->_nodes.pop();
duke@0 698
duke@0 699 } else if (b->_num_succs == 1) {
duke@0 700 // Block ends in a Goto?
duke@0 701 if (bnext == bs0) {
duke@0 702 // We fall into next block; remove the Goto
duke@0 703 b->_nodes.pop();
duke@0 704 }
duke@0 705
duke@0 706 } else if( b->_num_succs == 2 ) { // Block ends in a If?
duke@0 707 // Get opcode of 1st projection (matches _succs[0])
duke@0 708 // Note: Since this basic block has 2 exits, the last 2 nodes must
duke@0 709 // be projections (in any order), the 3rd last node must be
duke@0 710 // the IfNode (we have excluded other 2-way exits such as
duke@0 711 // CatchNodes already).
duke@0 712 MachNode *iff = b->_nodes[b->_nodes.size()-3]->as_Mach();
duke@0 713 ProjNode *proj0 = b->_nodes[b->_nodes.size()-2]->as_Proj();
duke@0 714 ProjNode *proj1 = b->_nodes[b->_nodes.size()-1]->as_Proj();
duke@0 715
duke@0 716 // Assert that proj0 and succs[0] match up. Similarly for proj1 and succs[1].
duke@0 717 assert(proj0->raw_out(0) == b->_succs[0]->head(), "Mismatch successor 0");
duke@0 718 assert(proj1->raw_out(0) == b->_succs[1]->head(), "Mismatch successor 1");
duke@0 719
duke@0 720 Block *bs1 = b->non_connector_successor(1);
duke@0 721
duke@0 722 // Check for neither successor block following the current
duke@0 723 // block ending in a conditional. If so, move one of the
duke@0 724 // successors after the current one, provided that the
duke@0 725 // successor was previously unscheduled, but moveable
duke@0 726 // (i.e., all paths to it involve a branch).
duke@0 727 if( bnext != bs0 && bnext != bs1 ) {
duke@0 728
duke@0 729 // Choose the more common successor based on the probability
duke@0 730 // of the conditional branch.
duke@0 731 Block *bx = bs0;
duke@0 732 Block *by = bs1;
duke@0 733
duke@0 734 // _prob is the probability of taking the true path. Make
duke@0 735 // p the probability of taking successor #1.
duke@0 736 float p = iff->as_MachIf()->_prob;
duke@0 737 if( proj0->Opcode() == Op_IfTrue ) {
duke@0 738 p = 1.0 - p;
duke@0 739 }
duke@0 740
duke@0 741 // Prefer successor #1 if p > 0.5
duke@0 742 if (p > PROB_FAIR) {
duke@0 743 bx = bs1;
duke@0 744 by = bs0;
duke@0 745 }
duke@0 746
duke@0 747 // Attempt the more common successor first
duke@0 748 if (MoveToNext(bx, i)) {
duke@0 749 bnext = bx;
duke@0 750 } else if (MoveToNext(by, i)) {
duke@0 751 bnext = by;
duke@0 752 }
duke@0 753 }
duke@0 754
duke@0 755 // Check for conditional branching the wrong way. Negate
duke@0 756 // conditional, if needed, so it falls into the following block
duke@0 757 // and branches to the not-following block.
duke@0 758
duke@0 759 // Check for the next block being in succs[0]. We are going to branch
duke@0 760 // to succs[0], so we want the fall-thru case as the next block in
duke@0 761 // succs[1].
duke@0 762 if (bnext == bs0) {
duke@0 763 // Fall-thru case in succs[0], so flip targets in succs map
duke@0 764 Block *tbs0 = b->_succs[0];
duke@0 765 Block *tbs1 = b->_succs[1];
duke@0 766 b->_succs.map( 0, tbs1 );
duke@0 767 b->_succs.map( 1, tbs0 );
duke@0 768 // Flip projection for each target
duke@0 769 { ProjNode *tmp = proj0; proj0 = proj1; proj1 = tmp; }
duke@0 770
duke@0 771 } else if( bnext == bs1 ) { // Fall-thru is already in succs[1]
duke@0 772
duke@0 773 } else { // Else need a double-branch
duke@0 774
duke@0 775 // The existing conditional branch need not change.
duke@0 776 // Add a unconditional branch to the false target.
duke@0 777 // Alas, it must appear in its own block and adding a
duke@0 778 // block this late in the game is complicated. Sigh.
duke@0 779 insert_goto_at(i, 1);
duke@0 780 }
duke@0 781
duke@0 782 // Make sure we TRUE branch to the target
duke@0 783 if( proj0->Opcode() == Op_IfFalse )
duke@0 784 iff->negate();
duke@0 785
duke@0 786 b->_nodes.pop(); // Remove IfFalse & IfTrue projections
duke@0 787 b->_nodes.pop();
duke@0 788
duke@0 789 } else {
duke@0 790 // Multi-exit block, e.g. a switch statement
duke@0 791 // But we don't need to do anything here
duke@0 792 }
duke@0 793
duke@0 794 } // End of for all blocks
duke@0 795
duke@0 796 }
duke@0 797
duke@0 798
duke@0 799 //------------------------------dump-------------------------------------------
duke@0 800 #ifndef PRODUCT
duke@0 801 void PhaseCFG::_dump_cfg( const Node *end, VectorSet &visited ) const {
duke@0 802 const Node *x = end->is_block_proj();
duke@0 803 assert( x, "not a CFG" );
duke@0 804
duke@0 805 // Do not visit this block again
duke@0 806 if( visited.test_set(x->_idx) ) return;
duke@0 807
duke@0 808 // Skip through this block
duke@0 809 const Node *p = x;
duke@0 810 do {
duke@0 811 p = p->in(0); // Move control forward
duke@0 812 assert( !p->is_block_proj() || p->is_Root(), "not a CFG" );
duke@0 813 } while( !p->is_block_start() );
duke@0 814
duke@0 815 // Recursively visit
duke@0 816 for( uint i=1; i<p->req(); i++ )
duke@0 817 _dump_cfg(p->in(i),visited);
duke@0 818
duke@0 819 // Dump the block
duke@0 820 _bbs[p->_idx]->dump(&_bbs);
duke@0 821 }
duke@0 822
duke@0 823 void PhaseCFG::dump( ) const {
duke@0 824 tty->print("\n--- CFG --- %d BBs\n",_num_blocks);
duke@0 825 if( _blocks.size() ) { // Did we do basic-block layout?
duke@0 826 for( uint i=0; i<_num_blocks; i++ )
duke@0 827 _blocks[i]->dump(&_bbs);
duke@0 828 } else { // Else do it with a DFS
duke@0 829 VectorSet visited(_bbs._arena);
duke@0 830 _dump_cfg(_root,visited);
duke@0 831 }
duke@0 832 }
duke@0 833
duke@0 834 void PhaseCFG::dump_headers() {
duke@0 835 for( uint i = 0; i < _num_blocks; i++ ) {
duke@0 836 if( _blocks[i] == NULL ) continue;
duke@0 837 _blocks[i]->dump_head(&_bbs);
duke@0 838 }
duke@0 839 }
duke@0 840
duke@0 841 void PhaseCFG::verify( ) const {
duke@0 842 // Verify sane CFG
duke@0 843 for( uint i = 0; i < _num_blocks; i++ ) {
duke@0 844 Block *b = _blocks[i];
duke@0 845 uint cnt = b->_nodes.size();
duke@0 846 uint j;
duke@0 847 for( j = 0; j < cnt; j++ ) {
duke@0 848 Node *n = b->_nodes[j];
duke@0 849 assert( _bbs[n->_idx] == b, "" );
duke@0 850 if( j >= 1 && n->is_Mach() &&
duke@0 851 n->as_Mach()->ideal_Opcode() == Op_CreateEx ) {
duke@0 852 assert( j == 1 || b->_nodes[j-1]->is_Phi(),
duke@0 853 "CreateEx must be first instruction in block" );
duke@0 854 }
duke@0 855 for( uint k = 0; k < n->req(); k++ ) {
duke@0 856 Node *use = n->in(k);
duke@0 857 if( use && use != n ) {
duke@0 858 assert( _bbs[use->_idx] || use->is_Con(),
duke@0 859 "must have block; constants for debug info ok" );
duke@0 860 }
duke@0 861 }
duke@0 862 }
duke@0 863
duke@0 864 j = b->end_idx();
duke@0 865 Node *bp = (Node*)b->_nodes[b->_nodes.size()-1]->is_block_proj();
duke@0 866 assert( bp, "last instruction must be a block proj" );
duke@0 867 assert( bp == b->_nodes[j], "wrong number of successors for this block" );
duke@0 868 if( bp->is_Catch() ) {
duke@0 869 while( b->_nodes[--j]->Opcode() == Op_MachProj ) ;
duke@0 870 assert( b->_nodes[j]->is_Call(), "CatchProj must follow call" );
duke@0 871 }
duke@0 872 else if( bp->is_Mach() && bp->as_Mach()->ideal_Opcode() == Op_If ) {
duke@0 873 assert( b->_num_succs == 2, "Conditional branch must have two targets");
duke@0 874 }
duke@0 875 }
duke@0 876 }
duke@0 877 #endif
duke@0 878
duke@0 879 //=============================================================================
duke@0 880 //------------------------------UnionFind--------------------------------------
duke@0 881 UnionFind::UnionFind( uint max ) : _cnt(max), _max(max), _indices(NEW_RESOURCE_ARRAY(uint,max)) {
duke@0 882 Copy::zero_to_bytes( _indices, sizeof(uint)*max );
duke@0 883 }
duke@0 884
duke@0 885 void UnionFind::extend( uint from_idx, uint to_idx ) {
duke@0 886 _nesting.check();
duke@0 887 if( from_idx >= _max ) {
duke@0 888 uint size = 16;
duke@0 889 while( size <= from_idx ) size <<=1;
duke@0 890 _indices = REALLOC_RESOURCE_ARRAY( uint, _indices, _max, size );
duke@0 891 _max = size;
duke@0 892 }
duke@0 893 while( _cnt <= from_idx ) _indices[_cnt++] = 0;
duke@0 894 _indices[from_idx] = to_idx;
duke@0 895 }
duke@0 896
duke@0 897 void UnionFind::reset( uint max ) {
duke@0 898 assert( max <= max_uint, "Must fit within uint" );
duke@0 899 // Force the Union-Find mapping to be at least this large
duke@0 900 extend(max,0);
duke@0 901 // Initialize to be the ID mapping.
duke@0 902 for( uint i=0; i<_max; i++ ) map(i,i);
duke@0 903 }
duke@0 904
duke@0 905 //------------------------------Find_compress----------------------------------
duke@0 906 // Straight out of Tarjan's union-find algorithm
duke@0 907 uint UnionFind::Find_compress( uint idx ) {
duke@0 908 uint cur = idx;
duke@0 909 uint next = lookup(cur);
duke@0 910 while( next != cur ) { // Scan chain of equivalences
duke@0 911 assert( next < cur, "always union smaller" );
duke@0 912 cur = next; // until find a fixed-point
duke@0 913 next = lookup(cur);
duke@0 914 }
duke@0 915 // Core of union-find algorithm: update chain of
duke@0 916 // equivalences to be equal to the root.
duke@0 917 while( idx != next ) {
duke@0 918 uint tmp = lookup(idx);
duke@0 919 map(idx, next);
duke@0 920 idx = tmp;
duke@0 921 }
duke@0 922 return idx;
duke@0 923 }
duke@0 924
duke@0 925 //------------------------------Find_const-------------------------------------
duke@0 926 // Like Find above, but no path compress, so bad asymptotic behavior
duke@0 927 uint UnionFind::Find_const( uint idx ) const {
duke@0 928 if( idx == 0 ) return idx; // Ignore the zero idx
duke@0 929 // Off the end? This can happen during debugging dumps
duke@0 930 // when data structures have not finished being updated.
duke@0 931 if( idx >= _max ) return idx;
duke@0 932 uint next = lookup(idx);
duke@0 933 while( next != idx ) { // Scan chain of equivalences
duke@0 934 assert( next < idx, "always union smaller" );
duke@0 935 idx = next; // until find a fixed-point
duke@0 936 next = lookup(idx);
duke@0 937 }
duke@0 938 return next;
duke@0 939 }
duke@0 940
duke@0 941 //------------------------------Union------------------------------------------
duke@0 942 // union 2 sets together.
duke@0 943 void UnionFind::Union( uint idx1, uint idx2 ) {
duke@0 944 uint src = Find(idx1);
duke@0 945 uint dst = Find(idx2);
duke@0 946 assert( src, "" );
duke@0 947 assert( dst, "" );
duke@0 948 assert( src < _max, "oob" );
duke@0 949 assert( dst < _max, "oob" );
duke@0 950 assert( src < dst, "always union smaller" );
duke@0 951 map(dst,src);
duke@0 952 }