annotate src/hotspot/share/opto/block.cpp @ 54048:744dc9c33676

8217417: Decorator name typo: C2_TIGHLY_COUPLED_ALLOC Summary: Fixed typo in decorator name, variables, and comments. Reviewed-by: tschatzl
author kbarrett
date Mon, 11 Mar 2019 02:05:07 -0400
parents 71c04702a3d5
children e46fe26d7f77
rev   line source
duke@1 1 /*
jprovino@37248 2 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
stefank@7397 26 #include "libadt/vectset.hpp"
stefank@7397 27 #include "memory/allocation.inline.hpp"
jprovino@37248 28 #include "memory/resourceArea.hpp"
neliasso@33451 29 #include "compiler/compilerDirectives.hpp"
stefank@7397 30 #include "opto/block.hpp"
stefank@7397 31 #include "opto/cfgnode.hpp"
stefank@7397 32 #include "opto/chaitin.hpp"
stefank@7397 33 #include "opto/loopnode.hpp"
stefank@7397 34 #include "opto/machnode.hpp"
stefank@7397 35 #include "opto/matcher.hpp"
stefank@7397 36 #include "opto/opcodes.hpp"
stefank@7397 37 #include "opto/rootnode.hpp"
stefank@7397 38 #include "utilities/copy.hpp"
stefank@7397 39
duke@1 40 void Block_Array::grow( uint i ) {
duke@1 41 assert(i >= Max(), "must be an overflow");
duke@1 42 debug_only(_limit = i+1);
duke@1 43 if( i < _size ) return;
duke@1 44 if( !_size ) {
duke@1 45 _size = 1;
duke@1 46 _blocks = (Block**)_arena->Amalloc( _size * sizeof(Block*) );
duke@1 47 _blocks[0] = NULL;
duke@1 48 }
duke@1 49 uint old = _size;
duke@1 50 while( i >= _size ) _size <<= 1; // Double to fit
duke@1 51 _blocks = (Block**)_arena->Arealloc( _blocks, old*sizeof(Block*),_size*sizeof(Block*));
duke@1 52 Copy::zero_to_bytes( &_blocks[old], (_size-old)*sizeof(Block*) );
duke@1 53 }
duke@1 54
duke@1 55 void Block_List::remove(uint i) {
duke@1 56 assert(i < _cnt, "index out of bounds");
duke@1 57 Copy::conjoint_words_to_lower((HeapWord*)&_blocks[i+1], (HeapWord*)&_blocks[i], ((_cnt-i-1)*sizeof(Block*)));
duke@1 58 pop(); // shrink list by one block
duke@1 59 }
duke@1 60
duke@1 61 void Block_List::insert(uint i, Block *b) {
duke@1 62 push(b); // grow list by one block
duke@1 63 Copy::conjoint_words_to_higher((HeapWord*)&_blocks[i], (HeapWord*)&_blocks[i+1], ((_cnt-i-1)*sizeof(Block*)));
duke@1 64 _blocks[i] = b;
duke@1 65 }
duke@1 66
rasbold@1498 67 #ifndef PRODUCT
rasbold@1498 68 void Block_List::print() {
rasbold@1498 69 for (uint i=0; i < size(); i++) {
rasbold@1498 70 tty->print("B%d ", _blocks[i]->_pre_order);
rasbold@1498 71 }
rasbold@1498 72 tty->print("size = %d\n", size());
rasbold@1498 73 }
rasbold@1498 74 #endif
duke@1 75
roland@38046 76 uint Block::code_alignment() const {
duke@1 77 // Check for Root block
kvn@10264 78 if (_pre_order == 0) return CodeEntryAlignment;
duke@1 79 // Check for Start block
kvn@10264 80 if (_pre_order == 1) return InteriorEntryAlignment;
duke@1 81 // Check for loop alignment
kvn@10264 82 if (has_loop_alignment()) return loop_alignment();
rasbold@1498 83
kvn@10264 84 return relocInfo::addr_unit(); // no particular alignment
rasbold@1498 85 }
rasbold@1498 86
rasbold@1498 87 uint Block::compute_loop_alignment() {
duke@1 88 Node *h = head();
kvn@10264 89 int unit_sz = relocInfo::addr_unit();
kvn@10264 90 if (h->is_Loop() && h->as_Loop()->is_inner_loop()) {
duke@1 91 // Pre- and post-loops have low trip count so do not bother with
duke@1 92 // NOPs for align loop head. The constants are hidden from tuning
duke@1 93 // but only because my "divide by 4" heuristic surely gets nearly
duke@1 94 // all possible gain (a "do not align at all" heuristic has a
duke@1 95 // chance of getting a really tiny gain).
kvn@10264 96 if (h->is_CountedLoop() && (h->as_CountedLoop()->is_pre_loop() ||
kvn@10264 97 h->as_CountedLoop()->is_post_loop())) {
kvn@10264 98 return (OptoLoopAlignment > 4*unit_sz) ? (OptoLoopAlignment>>2) : unit_sz;
kvn@10264 99 }
duke@1 100 // Loops with low backedge frequency should not be aligned.
duke@1 101 Node *n = h->in(LoopNode::LoopBackControl)->in(0);
kvn@10264 102 if (n->is_MachIf() && n->as_MachIf()->_prob < 0.01) {
kvn@10264 103 return unit_sz; // Loop does not loop, more often than not!
duke@1 104 }
duke@1 105 return OptoLoopAlignment; // Otherwise align loop head
duke@1 106 }
rasbold@1498 107
kvn@10264 108 return unit_sz; // no particular alignment
duke@1 109 }
duke@1 110
duke@1 111 // Compute the size of first 'inst_cnt' instructions in this block.
duke@1 112 // Return the number of instructions left to compute if the block has
rasbold@1498 113 // less then 'inst_cnt' instructions. Stop, and return 0 if sum_size
rasbold@1498 114 // exceeds OptoLoopAlignment.
duke@1 115 uint Block::compute_first_inst_size(uint& sum_size, uint inst_cnt,
duke@1 116 PhaseRegAlloc* ra) {
adlertz@19717 117 uint last_inst = number_of_nodes();
duke@1 118 for( uint j = 0; j < last_inst && inst_cnt > 0; j++ ) {
adlertz@19717 119 uint inst_size = get_node(j)->size(ra);
duke@1 120 if( inst_size > 0 ) {
duke@1 121 inst_cnt--;
duke@1 122 uint sz = sum_size + inst_size;
duke@1 123 if( sz <= (uint)OptoLoopAlignment ) {
duke@1 124 // Compute size of instructions which fit into fetch buffer only
duke@1 125 // since all inst_cnt instructions will not fit even if we align them.
duke@1 126 sum_size = sz;
duke@1 127 } else {
duke@1 128 return 0;
duke@1 129 }
duke@1 130 }
duke@1 131 }
duke@1 132 return inst_cnt;
duke@1 133 }
duke@1 134
duke@1 135 uint Block::find_node( const Node *n ) const {
adlertz@19717 136 for( uint i = 0; i < number_of_nodes(); i++ ) {
adlertz@19717 137 if( get_node(i) == n )
duke@1 138 return i;
duke@1 139 }
duke@1 140 ShouldNotReachHere();
duke@1 141 return 0;
duke@1 142 }
duke@1 143
duke@1 144 // Find and remove n from block list
duke@1 145 void Block::find_remove( const Node *n ) {
adlertz@19717 146 remove_node(find_node(n));
duke@1 147 }
duke@1 148
goetz@22844 149 bool Block::contains(const Node *n) const {
goetz@22844 150 return _nodes.contains(n);
goetz@22844 151 }
goetz@22844 152
duke@1 153 // Return empty status of a block. Empty blocks contain only the head, other
duke@1 154 // ideal nodes, and an optional trailing goto.
duke@1 155 int Block::is_Empty() const {
duke@1 156
duke@1 157 // Root or start block is not considered empty
duke@1 158 if (head()->is_Root() || head()->is_Start()) {
duke@1 159 return not_empty;
duke@1 160 }
duke@1 161
duke@1 162 int success_result = completely_empty;
adlertz@19717 163 int end_idx = number_of_nodes() - 1;
duke@1 164
duke@1 165 // Check for ending goto
adlertz@19717 166 if ((end_idx > 0) && (get_node(end_idx)->is_MachGoto())) {
duke@1 167 success_result = empty_with_goto;
duke@1 168 end_idx--;
duke@1 169 }
duke@1 170
duke@1 171 // Unreachable blocks are considered empty
duke@1 172 if (num_preds() <= 1) {
duke@1 173 return success_result;
duke@1 174 }
duke@1 175
duke@1 176 // Ideal nodes are allowable in empty blocks: skip them Only MachNodes
duke@1 177 // turn directly into code, because only MachNodes have non-trivial
duke@1 178 // emit() functions.
adlertz@19717 179 while ((end_idx > 0) && !get_node(end_idx)->is_Mach()) {
duke@1 180 end_idx--;
duke@1 181 }
duke@1 182
duke@1 183 // No room for any interesting instructions?
duke@1 184 if (end_idx == 0) {
duke@1 185 return success_result;
duke@1 186 }
duke@1 187
duke@1 188 return not_empty;
duke@1 189 }
duke@1 190
twisti@2131 191 // Return true if the block's code implies that it is likely to be
duke@1 192 // executed infrequently. Check to see if the block ends in a Halt or
duke@1 193 // a low probability call.
duke@1 194 bool Block::has_uncommon_code() const {
duke@1 195 Node* en = end();
duke@1 196
kvn@10255 197 if (en->is_MachGoto())
duke@1 198 en = en->in(0);
duke@1 199 if (en->is_Catch())
duke@1 200 en = en->in(0);
kvn@10255 201 if (en->is_MachProj() && en->in(0)->is_MachCall()) {
duke@1 202 MachCallNode* call = en->in(0)->as_MachCall();
duke@1 203 if (call->cnt() != COUNT_UNKNOWN && call->cnt() <= PROB_UNLIKELY_MAG(4)) {
duke@1 204 // This is true for slow-path stubs like new_{instance,array},
duke@1 205 // slow_arraycopy, complete_monitor_locking, uncommon_trap.
duke@1 206 // The magic number corresponds to the probability of an uncommon_trap,
duke@1 207 // even though it is a count not a probability.
duke@1 208 return true;
duke@1 209 }
duke@1 210 }
duke@1 211
duke@1 212 int op = en->is_Mach() ? en->as_Mach()->ideal_Opcode() : en->Opcode();
duke@1 213 return op == Op_Halt;
duke@1 214 }
duke@1 215
duke@1 216 // True if block is low enough frequency or guarded by a test which
duke@1 217 // mostly does not go here.
adlertz@19721 218 bool PhaseCFG::is_uncommon(const Block* block) {
duke@1 219 // Initial blocks must never be moved, so are never uncommon.
adlertz@19721 220 if (block->head()->is_Root() || block->head()->is_Start()) return false;
duke@1 221
duke@1 222 // Check for way-low freq
adlertz@19721 223 if(block->_freq < BLOCK_FREQUENCY(0.00001f) ) return true;
duke@1 224
duke@1 225 // Look for code shape indicating uncommon_trap or slow path
adlertz@19721 226 if (block->has_uncommon_code()) return true;
duke@1 227
duke@1 228 const float epsilon = 0.05f;
duke@1 229 const float guard_factor = PROB_UNLIKELY_MAG(4) / (1.f - epsilon);
duke@1 230 uint uncommon_preds = 0;
duke@1 231 uint freq_preds = 0;
duke@1 232 uint uncommon_for_freq_preds = 0;
duke@1 233
adlertz@19721 234 for( uint i=1; i< block->num_preds(); i++ ) {
adlertz@19721 235 Block* guard = get_block_for_node(block->pred(i));
duke@1 236 // Check to see if this block follows its guard 1 time out of 10000
duke@1 237 // or less.
duke@1 238 //
duke@1 239 // See list of magnitude-4 unlikely probabilities in cfgnode.hpp which
duke@1 240 // we intend to be "uncommon", such as slow-path TLE allocation,
duke@1 241 // predicted call failure, and uncommon trap triggers.
duke@1 242 //
duke@1 243 // Use an epsilon value of 5% to allow for variability in frequency
duke@1 244 // predictions and floating point calculations. The net effect is
duke@1 245 // that guard_factor is set to 9500.
duke@1 246 //
duke@1 247 // Ignore low-frequency blocks.
duke@1 248 // The next check is (guard->_freq < 1.e-5 * 9500.).
duke@1 249 if(guard->_freq*BLOCK_FREQUENCY(guard_factor) < BLOCK_FREQUENCY(0.00001f)) {
duke@1 250 uncommon_preds++;
duke@1 251 } else {
duke@1 252 freq_preds++;
adlertz@19721 253 if(block->_freq < guard->_freq * guard_factor ) {
duke@1 254 uncommon_for_freq_preds++;
duke@1 255 }
duke@1 256 }
duke@1 257 }
adlertz@19721 258 if( block->num_preds() > 1 &&
duke@1 259 // The block is uncommon if all preds are uncommon or
adlertz@19721 260 (uncommon_preds == (block->num_preds()-1) ||
duke@1 261 // it is uncommon for all frequent preds.
duke@1 262 uncommon_for_freq_preds == freq_preds) ) {
duke@1 263 return true;
duke@1 264 }
duke@1 265 return false;
duke@1 266 }
duke@1 267
duke@1 268 #ifndef PRODUCT
kvn@10264 269 void Block::dump_bidx(const Block* orig, outputStream* st) const {
kvn@10264 270 if (_pre_order) st->print("B%d",_pre_order);
kvn@10264 271 else st->print("N%d", head()->_idx);
duke@1 272
duke@1 273 if (Verbose && orig != this) {
duke@1 274 // Dump the original block's idx
kvn@10264 275 st->print(" (");
kvn@10264 276 orig->dump_bidx(orig, st);
kvn@10264 277 st->print(")");
duke@1 278 }
duke@1 279 }
duke@1 280
adlertz@19279 281 void Block::dump_pred(const PhaseCFG* cfg, Block* orig, outputStream* st) const {
duke@1 282 if (is_connector()) {
duke@1 283 for (uint i=1; i<num_preds(); i++) {
adlertz@19279 284 Block *p = cfg->get_block_for_node(pred(i));
adlertz@19279 285 p->dump_pred(cfg, orig, st);
duke@1 286 }
duke@1 287 } else {
kvn@10264 288 dump_bidx(orig, st);
kvn@10264 289 st->print(" ");
duke@1 290 }
duke@1 291 }
duke@1 292
adlertz@19279 293 void Block::dump_head(const PhaseCFG* cfg, outputStream* st) const {
duke@1 294 // Print the basic block
kvn@10264 295 dump_bidx(this, st);
kvn@10264 296 st->print(": #\t");
duke@1 297
duke@1 298 // Print the incoming CFG edges and the outgoing CFG edges
duke@1 299 for( uint i=0; i<_num_succs; i++ ) {
kvn@10264 300 non_connector_successor(i)->dump_bidx(_succs[i], st);
kvn@10264 301 st->print(" ");
duke@1 302 }
kvn@10264 303 st->print("<- ");
duke@1 304 if( head()->is_block_start() ) {
duke@1 305 for (uint i=1; i<num_preds(); i++) {
duke@1 306 Node *s = pred(i);
adlertz@19279 307 if (cfg != NULL) {
adlertz@19279 308 Block *p = cfg->get_block_for_node(s);
adlertz@19279 309 p->dump_pred(cfg, p, st);
duke@1 310 } else {
duke@1 311 while (!s->is_block_start())
duke@1 312 s = s->in(0);
kvn@10264 313 st->print("N%d ", s->_idx );
duke@1 314 }
duke@1 315 }
adlertz@19279 316 } else {
kvn@10264 317 st->print("BLOCK HEAD IS JUNK ");
adlertz@19279 318 }
duke@1 319
duke@1 320 // Print loop, if any
duke@1 321 const Block *bhead = this; // Head of self-loop
duke@1 322 Node *bh = bhead->head();
adlertz@19279 323
adlertz@19279 324 if ((cfg != NULL) && bh->is_Loop() && !head()->is_Root()) {
duke@1 325 LoopNode *loop = bh->as_Loop();
adlertz@19279 326 const Block *bx = cfg->get_block_for_node(loop->in(LoopNode::LoopBackControl));
duke@1 327 while (bx->is_connector()) {
adlertz@19279 328 bx = cfg->get_block_for_node(bx->pred(1));
duke@1 329 }
kvn@10264 330 st->print("\tLoop: B%d-B%d ", bhead->_pre_order, bx->_pre_order);
duke@1 331 // Dump any loop-specific bits, especially for CountedLoops.
kvn@10264 332 loop->dump_spec(st);
rasbold@1498 333 } else if (has_loop_alignment()) {
kvn@10264 334 st->print(" top-of-loop");
duke@1 335 }
kvn@10264 336 st->print(" Freq: %g",_freq);
duke@1 337 if( Verbose || WizardMode ) {
kvn@10264 338 st->print(" IDom: %d/#%d", _idom ? _idom->_pre_order : 0, _dom_depth);
kvn@10264 339 st->print(" RegPressure: %d",_reg_pressure);
kvn@10264 340 st->print(" IHRP Index: %d",_ihrp_index);
kvn@10264 341 st->print(" FRegPressure: %d",_freg_pressure);
kvn@10264 342 st->print(" FHRP Index: %d",_fhrp_index);
duke@1 343 }
drchase@24424 344 st->cr();
duke@1 345 }
duke@1 346
adlertz@19279 347 void Block::dump() const {
adlertz@19279 348 dump(NULL);
adlertz@19279 349 }
duke@1 350
adlertz@19279 351 void Block::dump(const PhaseCFG* cfg) const {
adlertz@19279 352 dump_head(cfg);
adlertz@19717 353 for (uint i=0; i< number_of_nodes(); i++) {
adlertz@19717 354 get_node(i)->dump();
adlertz@19279 355 }
duke@1 356 tty->print("\n");
duke@1 357 }
duke@1 358 #endif
duke@1 359
adlertz@19279 360 PhaseCFG::PhaseCFG(Arena* arena, RootNode* root, Matcher& matcher)
adlertz@19279 361 : Phase(CFG)
tschatzl@51333 362 , _root(root)
adlertz@19279 363 , _block_arena(arena)
mcberg@33065 364 , _regalloc(NULL)
mcberg@33065 365 , _scheduling_for_pressure(false)
adlertz@19330 366 , _matcher(matcher)
adlertz@19279 367 , _node_to_block_mapping(arena)
adlertz@19279 368 , _node_latency(NULL)
duke@1 369 #ifndef PRODUCT
neliasso@33451 370 , _trace_opto_pipelining(C->directive()->TraceOptoPipeliningOption)
duke@1 371 #endif
kvn@3186 372 #ifdef ASSERT
adlertz@19279 373 , _raw_oops(arena)
kvn@3186 374 #endif
duke@1 375 {
duke@1 376 ResourceMark rm;
duke@1 377 // I'll need a few machine-specific GotoNodes. Make an Ideal GotoNode,
duke@1 378 // then Match it into a machine-specific Node. Then clone the machine
duke@1 379 // Node on demand.
thartmann@24923 380 Node *x = new GotoNode(NULL);
duke@1 381 x->init_req(0, x);
adlertz@19279 382 _goto = matcher.match_tree(x);
duke@1 383 assert(_goto != NULL, "");
duke@1 384 _goto->set_req(0,_goto);
duke@1 385
duke@1 386 // Build the CFG in Reverse Post Order
adlertz@19330 387 _number_of_blocks = build_cfg();
adlertz@19330 388 _root_block = get_block_for_node(_root);
duke@1 389 }
duke@1 390
duke@1 391 // Build a proper looking CFG. Make every block begin with either a StartNode
duke@1 392 // or a RegionNode. Make every block end with either a Goto, If or Return.
duke@1 393 // The RootNode both starts and ends it's own block. Do this with a recursive
duke@1 394 // backwards walk over the control edges.
duke@1 395 uint PhaseCFG::build_cfg() {
duke@1 396 Arena *a = Thread::current()->resource_area();
duke@1 397 VectorSet visited(a);
duke@1 398
duke@1 399 // Allocate stack with enough space to avoid frequent realloc
kvn@32202 400 Node_Stack nstack(a, C->live_nodes() >> 1);
duke@1 401 nstack.push(_root, 0);
duke@1 402 uint sum = 0; // Counter for blocks
duke@1 403
duke@1 404 while (nstack.is_nonempty()) {
duke@1 405 // node and in's index from stack's top
duke@1 406 // 'np' is _root (see above) or RegionNode, StartNode: we push on stack
duke@1 407 // only nodes which point to the start of basic block (see below).
duke@1 408 Node *np = nstack.node();
duke@1 409 // idx > 0, except for the first node (_root) pushed on stack
duke@1 410 // at the beginning when idx == 0.
duke@1 411 // We will use the condition (idx == 0) later to end the build.
duke@1 412 uint idx = nstack.index();
duke@1 413 Node *proj = np->in(idx);
duke@1 414 const Node *x = proj->is_block_proj();
duke@1 415 // Does the block end with a proper block-ending Node? One of Return,
duke@1 416 // If or Goto? (This check should be done for visited nodes also).
duke@1 417 if (x == NULL) { // Does not end right...
duke@1 418 Node *g = _goto->clone(); // Force it to end in a Goto
duke@1 419 g->set_req(0, proj);
duke@1 420 np->set_req(idx, g);
duke@1 421 x = proj = g;
duke@1 422 }
duke@1 423 if (!visited.test_set(x->_idx)) { // Visit this block once
duke@1 424 // Skip any control-pinned middle'in stuff
duke@1 425 Node *p = proj;
duke@1 426 do {
duke@1 427 proj = p; // Update pointer to last Control
duke@1 428 p = p->in(0); // Move control forward
duke@1 429 } while( !p->is_block_proj() &&
duke@1 430 !p->is_block_start() );
duke@1 431 // Make the block begin with one of Region or StartNode.
duke@1 432 if( !p->is_block_start() ) {
thartmann@24923 433 RegionNode *r = new RegionNode( 2 );
duke@1 434 r->init_req(1, p); // Insert RegionNode in the way
duke@1 435 proj->set_req(0, r); // Insert RegionNode in the way
duke@1 436 p = r;
duke@1 437 }
duke@1 438 // 'p' now points to the start of this basic block
duke@1 439
duke@1 440 // Put self in array of basic blocks
adlertz@19279 441 Block *bb = new (_block_arena) Block(_block_arena, p);
adlertz@19279 442 map_node_to_block(p, bb);
adlertz@19279 443 map_node_to_block(x, bb);
kvn@10264 444 if( x != p ) { // Only for root is x == p
adlertz@19717 445 bb->push_node((Node*)x);
kvn@10264 446 }
duke@1 447 // Now handle predecessors
duke@1 448 ++sum; // Count 1 for self block
duke@1 449 uint cnt = bb->num_preds();
duke@1 450 for (int i = (cnt - 1); i > 0; i-- ) { // For all predecessors
duke@1 451 Node *prevproj = p->in(i); // Get prior input
duke@1 452 assert( !prevproj->is_Con(), "dead input not removed" );
duke@1 453 // Check to see if p->in(i) is a "control-dependent" CFG edge -
duke@1 454 // i.e., it splits at the source (via an IF or SWITCH) and merges
duke@1 455 // at the destination (via a many-input Region).
duke@1 456 // This breaks critical edges. The RegionNode to start the block
duke@1 457 // will be added when <p,i> is pulled off the node stack
duke@1 458 if ( cnt > 2 ) { // Merging many things?
duke@1 459 assert( prevproj== bb->pred(i),"");
duke@1 460 if(prevproj->is_block_proj() != prevproj) { // Control-dependent edge?
duke@1 461 // Force a block on the control-dependent edge
duke@1 462 Node *g = _goto->clone(); // Force it to end in a Goto
duke@1 463 g->set_req(0,prevproj);
duke@1 464 p->set_req(i,g);
duke@1 465 }
duke@1 466 }
duke@1 467 nstack.push(p, i); // 'p' is RegionNode or StartNode
duke@1 468 }
duke@1 469 } else { // Post-processing visited nodes
duke@1 470 nstack.pop(); // remove node from stack
duke@1 471 // Check if it the fist node pushed on stack at the beginning.
duke@1 472 if (idx == 0) break; // end of the build
duke@1 473 // Find predecessor basic block
adlertz@19279 474 Block *pb = get_block_for_node(x);
duke@1 475 // Insert into nodes array, if not already there
adlertz@19279 476 if (!has_block(proj)) {
duke@1 477 assert( x != proj, "" );
duke@1 478 // Map basic block of projection
adlertz@19279 479 map_node_to_block(proj, pb);
adlertz@19717 480 pb->push_node(proj);
duke@1 481 }
duke@1 482 // Insert self as a child of my predecessor block
adlertz@19279 483 pb->_succs.map(pb->_num_succs++, get_block_for_node(np));
adlertz@19717 484 assert( pb->get_node(pb->number_of_nodes() - pb->_num_succs)->is_block_proj(),
duke@1 485 "too many control users, not a CFG?" );
duke@1 486 }
duke@1 487 }
duke@1 488 // Return number of basic blocks for all children and self
duke@1 489 return sum;
duke@1 490 }
duke@1 491
duke@1 492 // Inserts a goto & corresponding basic block between
duke@1 493 // block[block_no] and its succ_no'th successor block
duke@1 494 void PhaseCFG::insert_goto_at(uint block_no, uint succ_no) {
duke@1 495 // get block with block_no
adlertz@19330 496 assert(block_no < number_of_blocks(), "illegal block number");
adlertz@19330 497 Block* in = get_block(block_no);
duke@1 498 // get successor block succ_no
duke@1 499 assert(succ_no < in->_num_succs, "illegal successor number");
duke@1 500 Block* out = in->_succs[succ_no];
rasbold@1070 501 // Compute frequency of the new block. Do this before inserting
rasbold@1070 502 // new block in case succ_prob() needs to infer the probability from
rasbold@1070 503 // surrounding blocks.
rasbold@1070 504 float freq = in->_freq * in->succ_prob(succ_no);
duke@1 505 // get ProjNode corresponding to the succ_no'th successor of the in block
adlertz@19717 506 ProjNode* proj = in->get_node(in->number_of_nodes() - in->_num_succs + succ_no)->as_Proj();
duke@1 507 // create region for basic block
thartmann@24923 508 RegionNode* region = new RegionNode(2);
duke@1 509 region->init_req(1, proj);
duke@1 510 // setup corresponding basic block
adlertz@19279 511 Block* block = new (_block_arena) Block(_block_arena, region);
adlertz@19279 512 map_node_to_block(region, block);
duke@1 513 C->regalloc()->set_bad(region->_idx);
duke@1 514 // add a goto node
duke@1 515 Node* gto = _goto->clone(); // get a new goto node
duke@1 516 gto->set_req(0, region);
duke@1 517 // add it to the basic block
adlertz@19717 518 block->push_node(gto);
adlertz@19279 519 map_node_to_block(gto, block);
duke@1 520 C->regalloc()->set_bad(gto->_idx);
duke@1 521 // hook up successor block
duke@1 522 block->_succs.map(block->_num_succs++, out);
duke@1 523 // remap successor's predecessors if necessary
duke@1 524 for (uint i = 1; i < out->num_preds(); i++) {
duke@1 525 if (out->pred(i) == proj) out->head()->set_req(i, gto);
duke@1 526 }
duke@1 527 // remap predecessor's successor to new block
duke@1 528 in->_succs.map(succ_no, block);
rasbold@1070 529 // Set the frequency of the new block
rasbold@1070 530 block->_freq = freq;
duke@1 531 // add new basic block to basic block list
adlertz@19330 532 add_block_at(block_no + 1, block);
duke@1 533 }
duke@1 534
duke@1 535 // Does this block end in a multiway branch that cannot have the default case
duke@1 536 // flipped for another case?
goetz@22856 537 static bool no_flip_branch(Block *b) {
adlertz@19717 538 int branch_idx = b->number_of_nodes() - b->_num_succs-1;
goetz@22856 539 if (branch_idx < 1) {
goetz@22856 540 return false;
goetz@22856 541 }
goetz@22856 542 Node *branch = b->get_node(branch_idx);
goetz@22856 543 if (branch->is_Catch()) {
rasbold@1498 544 return true;
goetz@22856 545 }
goetz@22856 546 if (branch->is_Mach()) {
goetz@22856 547 if (branch->is_MachNullCheck()) {
rasbold@1498 548 return true;
goetz@22856 549 }
goetz@22856 550 int iop = branch->as_Mach()->ideal_Opcode();
goetz@22856 551 if (iop == Op_FastLock || iop == Op_FastUnlock) {
duke@1 552 return true;
goetz@22856 553 }
goetz@22856 554 // Don't flip if branch has an implicit check.
goetz@22856 555 if (branch->as_Mach()->is_TrapBasedCheckNode()) {
goetz@22856 556 return true;
goetz@22856 557 }
duke@1 558 }
duke@1 559 return false;
duke@1 560 }
duke@1 561
duke@1 562 // Check for NeverBranch at block end. This needs to become a GOTO to the
duke@1 563 // true target. NeverBranch are treated as a conditional branch that always
duke@1 564 // goes the same direction for most of the optimizer and are used to give a
duke@1 565 // fake exit path to infinite loops. At this late stage they need to turn
duke@1 566 // into Goto's so that when you enter the infinite loop you indeed hang.
duke@1 567 void PhaseCFG::convert_NeverBranch_to_Goto(Block *b) {
duke@1 568 // Find true target
duke@1 569 int end_idx = b->end_idx();
adlertz@19717 570 int idx = b->get_node(end_idx+1)->as_Proj()->_con;
duke@1 571 Block *succ = b->_succs[idx];
duke@1 572 Node* gto = _goto->clone(); // get a new goto node
duke@1 573 gto->set_req(0, b->head());
adlertz@19717 574 Node *bp = b->get_node(end_idx);
adlertz@19717 575 b->map_node(gto, end_idx); // Slam over NeverBranch
adlertz@19279 576 map_node_to_block(gto, b);
duke@1 577 C->regalloc()->set_bad(gto->_idx);
adlertz@19717 578 b->pop_node(); // Yank projections
adlertz@19717 579 b->pop_node(); // Yank projections
duke@1 580 b->_succs.map(0,succ); // Map only successor
duke@1 581 b->_num_succs = 1;
duke@1 582 // remap successor's predecessors if necessary
duke@1 583 uint j;
duke@1 584 for( j = 1; j < succ->num_preds(); j++)
duke@1 585 if( succ->pred(j)->in(0) == bp )
duke@1 586 succ->head()->set_req(j, gto);
duke@1 587 // Kill alternate exit path
duke@1 588 Block *dead = b->_succs[1-idx];
duke@1 589 for( j = 1; j < dead->num_preds(); j++)
duke@1 590 if( dead->pred(j)->in(0) == bp )
duke@1 591 break;
duke@1 592 // Scan through block, yanking dead path from
duke@1 593 // all regions and phis.
duke@1 594 dead->head()->del_req(j);
adlertz@19717 595 for( int k = 1; dead->get_node(k)->is_Phi(); k++ )
adlertz@19717 596 dead->get_node(k)->del_req(j);
duke@1 597 }
duke@1 598
duke@1 599 // Helper function to move block bx to the slot following b_index. Return
duke@1 600 // true if the move is successful, otherwise false
rasbold@1498 601 bool PhaseCFG::move_to_next(Block* bx, uint b_index) {
duke@1 602 if (bx == NULL) return false;
duke@1 603
duke@1 604 // Return false if bx is already scheduled.
duke@1 605 uint bx_index = bx->_pre_order;
adlertz@19330 606 if ((bx_index <= b_index) && (get_block(bx_index) == bx)) {
duke@1 607 return false;
duke@1 608 }
duke@1 609
duke@1 610 // Find the current index of block bx on the block list
duke@1 611 bx_index = b_index + 1;
adlertz@19330 612 while (bx_index < number_of_blocks() && get_block(bx_index) != bx) {
adlertz@19330 613 bx_index++;
adlertz@19330 614 }
adlertz@19330 615 assert(get_block(bx_index) == bx, "block not found");
duke@1 616
duke@1 617 // If the previous block conditionally falls into bx, return false,
duke@1 618 // because moving bx will create an extra jump.
duke@1 619 for(uint k = 1; k < bx->num_preds(); k++ ) {
adlertz@19279 620 Block* pred = get_block_for_node(bx->pred(k));
adlertz@19330 621 if (pred == get_block(bx_index - 1)) {
duke@1 622 if (pred->_num_succs != 1) {
duke@1 623 return false;
duke@1 624 }
duke@1 625 }
duke@1 626 }
duke@1 627
duke@1 628 // Reinsert bx just past block 'b'
duke@1 629 _blocks.remove(bx_index);
duke@1 630 _blocks.insert(b_index + 1, bx);
duke@1 631 return true;
duke@1 632 }
duke@1 633
duke@1 634 // Move empty and uncommon blocks to the end.
rasbold@1498 635 void PhaseCFG::move_to_end(Block *b, uint i) {
duke@1 636 int e = b->is_Empty();
duke@1 637 if (e != Block::not_empty) {
duke@1 638 if (e == Block::empty_with_goto) {
duke@1 639 // Remove the goto, but leave the block.
adlertz@19717 640 b->pop_node();
duke@1 641 }
duke@1 642 // Mark this block as a connector block, which will cause it to be
duke@1 643 // ignored in certain functions such as non_connector_successor().
duke@1 644 b->set_connector();
duke@1 645 }
duke@1 646 // Move the empty block to the end, and don't recheck.
duke@1 647 _blocks.remove(i);
duke@1 648 _blocks.push(b);
duke@1 649 }
duke@1 650
rasbold@1498 651 // Set loop alignment for every block
rasbold@1498 652 void PhaseCFG::set_loop_alignment() {
adlertz@19330 653 uint last = number_of_blocks();
adlertz@19330 654 assert(get_block(0) == get_root_block(), "");
rasbold@1498 655
adlertz@19330 656 for (uint i = 1; i < last; i++) {
adlertz@19330 657 Block* block = get_block(i);
adlertz@19330 658 if (block->head()->is_Loop()) {
adlertz@19330 659 block->set_loop_alignment(block);
rasbold@1498 660 }
rasbold@1498 661 }
rasbold@1498 662 }
rasbold@1498 663
rasbold@1498 664 // Make empty basic blocks to be "connector" blocks, Move uncommon blocks
rasbold@1498 665 // to the end.
adlertz@19330 666 void PhaseCFG::remove_empty_blocks() {
duke@1 667 // Move uncommon blocks to the end
adlertz@19330 668 uint last = number_of_blocks();
adlertz@19330 669 assert(get_block(0) == get_root_block(), "");
rasbold@1498 670
rasbold@1498 671 for (uint i = 1; i < last; i++) {
adlertz@19330 672 Block* block = get_block(i);
adlertz@19330 673 if (block->is_connector()) {
adlertz@19330 674 break;
adlertz@19330 675 }
duke@1 676
duke@1 677 // Check for NeverBranch at block end. This needs to become a GOTO to the
duke@1 678 // true target. NeverBranch are treated as a conditional branch that
duke@1 679 // always goes the same direction for most of the optimizer and are used
duke@1 680 // to give a fake exit path to infinite loops. At this late stage they
duke@1 681 // need to turn into Goto's so that when you enter the infinite loop you
duke@1 682 // indeed hang.
adlertz@19717 683 if (block->get_node(block->end_idx())->Opcode() == Op_NeverBranch) {
adlertz@19330 684 convert_NeverBranch_to_Goto(block);
adlertz@19330 685 }
duke@1 686
duke@1 687 // Look for uncommon blocks and move to end.
rasbold@1498 688 if (!C->do_freq_based_layout()) {
adlertz@19721 689 if (is_uncommon(block)) {
adlertz@19330 690 move_to_end(block, i);
rasbold@1498 691 last--; // No longer check for being uncommon!
adlertz@19330 692 if (no_flip_branch(block)) { // Fall-thru case must follow?
adlertz@19330 693 // Find the fall-thru block
adlertz@19330 694 block = get_block(i);
adlertz@19330 695 move_to_end(block, i);
rasbold@1498 696 last--;
rasbold@1498 697 }
adlertz@19330 698 // backup block counter post-increment
adlertz@19330 699 i--;
duke@1 700 }
duke@1 701 }
duke@1 702 }
duke@1 703
rasbold@1498 704 // Move empty blocks to the end
adlertz@19330 705 last = number_of_blocks();
rasbold@1498 706 for (uint i = 1; i < last; i++) {
adlertz@19330 707 Block* block = get_block(i);
adlertz@19330 708 if (block->is_Empty() != Block::not_empty) {
adlertz@19330 709 move_to_end(block, i);
rasbold@1498 710 last--;
rasbold@1498 711 i--;
duke@1 712 }
duke@1 713 } // End of for all blocks
rasbold@1498 714 }
duke@1 715
goetz@22856 716 Block *PhaseCFG::fixup_trap_based_check(Node *branch, Block *block, int block_pos, Block *bnext) {
goetz@22856 717 // Trap based checks must fall through to the successor with
goetz@22856 718 // PROB_ALWAYS.
goetz@22856 719 // They should be an If with 2 successors.
goetz@22856 720 assert(branch->is_MachIf(), "must be If");
goetz@22856 721 assert(block->_num_succs == 2, "must have 2 successors");
goetz@22856 722
goetz@22856 723 // Get the If node and the projection for the first successor.
goetz@22856 724 MachIfNode *iff = block->get_node(block->number_of_nodes()-3)->as_MachIf();
goetz@22856 725 ProjNode *proj0 = block->get_node(block->number_of_nodes()-2)->as_Proj();
goetz@22856 726 ProjNode *proj1 = block->get_node(block->number_of_nodes()-1)->as_Proj();
goetz@22856 727 ProjNode *projt = (proj0->Opcode() == Op_IfTrue) ? proj0 : proj1;
goetz@22856 728 ProjNode *projf = (proj0->Opcode() == Op_IfFalse) ? proj0 : proj1;
goetz@22856 729
goetz@22856 730 // Assert that proj0 and succs[0] match up. Similarly for proj1 and succs[1].
goetz@22856 731 assert(proj0->raw_out(0) == block->_succs[0]->head(), "Mismatch successor 0");
goetz@22856 732 assert(proj1->raw_out(0) == block->_succs[1]->head(), "Mismatch successor 1");
goetz@22856 733
goetz@22856 734 ProjNode *proj_always;
goetz@22856 735 ProjNode *proj_never;
goetz@22856 736 // We must negate the branch if the implicit check doesn't follow
goetz@22856 737 // the branch's TRUE path. Then, the new TRUE branch target will
goetz@22856 738 // be the old FALSE branch target.
goetz@22856 739 if (iff->_prob <= 2*PROB_NEVER) { // There are small rounding errors.
goetz@22856 740 proj_never = projt;
goetz@22856 741 proj_always = projf;
goetz@22856 742 } else {
goetz@22856 743 // We must negate the branch if the trap doesn't follow the
goetz@22856 744 // branch's TRUE path. Then, the new TRUE branch target will
goetz@22856 745 // be the old FALSE branch target.
goetz@22856 746 proj_never = projf;
goetz@22856 747 proj_always = projt;
goetz@22856 748 iff->negate();
goetz@22856 749 }
goetz@22856 750 assert(iff->_prob <= 2*PROB_NEVER, "Trap based checks are expected to trap never!");
goetz@22856 751 // Map the successors properly
goetz@22856 752 block->_succs.map(0, get_block_for_node(proj_never ->raw_out(0))); // The target of the trap.
goetz@22856 753 block->_succs.map(1, get_block_for_node(proj_always->raw_out(0))); // The fall through target.
goetz@22856 754
goetz@22866 755 if (block->get_node(block->number_of_nodes() - block->_num_succs + 1) != proj_always) {
goetz@22866 756 block->map_node(proj_never, block->number_of_nodes() - block->_num_succs + 0);
goetz@22866 757 block->map_node(proj_always, block->number_of_nodes() - block->_num_succs + 1);
goetz@22866 758 }
goetz@22866 759
goetz@22856 760 // Place the fall through block after this block.
goetz@22856 761 Block *bs1 = block->non_connector_successor(1);
goetz@22856 762 if (bs1 != bnext && move_to_next(bs1, block_pos)) {
goetz@22856 763 bnext = bs1;
goetz@22856 764 }
goetz@22856 765 // If the fall through block still is not the next block, insert a goto.
goetz@22856 766 if (bs1 != bnext) {
goetz@22856 767 insert_goto_at(block_pos, 1);
goetz@22856 768 }
goetz@22856 769 return bnext;
goetz@22856 770 }
goetz@22856 771
rasbold@1498 772 // Fix up the final control flow for basic blocks.
rasbold@1498 773 void PhaseCFG::fixup_flow() {
duke@1 774 // Fixup final control flow for the blocks. Remove jump-to-next
goetz@22844 775 // block. If neither arm of an IF follows the conditional branch, we
duke@1 776 // have to add a second jump after the conditional. We place the
duke@1 777 // TRUE branch target in succs[0] for both GOTOs and IFs.
adlertz@19330 778 for (uint i = 0; i < number_of_blocks(); i++) {
adlertz@19330 779 Block* block = get_block(i);
adlertz@19330 780 block->_pre_order = i; // turn pre-order into block-index
duke@1 781
duke@1 782 // Connector blocks need no further processing.
adlertz@19330 783 if (block->is_connector()) {
adlertz@19330 784 assert((i+1) == number_of_blocks() || get_block(i + 1)->is_connector(), "All connector blocks should sink to the end");
duke@1 785 continue;
duke@1 786 }
adlertz@19330 787 assert(block->is_Empty() != Block::completely_empty, "Empty blocks should be connectors");
duke@1 788
adlertz@19330 789 Block* bnext = (i < number_of_blocks() - 1) ? get_block(i + 1) : NULL;
adlertz@19330 790 Block* bs0 = block->non_connector_successor(0);
duke@1 791
duke@1 792 // Check for multi-way branches where I cannot negate the test to
duke@1 793 // exchange the true and false targets.
adlertz@19330 794 if (no_flip_branch(block)) {
goetz@22856 795 // Find fall through case - if must fall into its target.
goetz@22856 796 // Get the index of the branch's first successor.
adlertz@19717 797 int branch_idx = block->number_of_nodes() - block->_num_succs;
goetz@22856 798
goetz@22856 799 // The branch is 1 before the branch's first successor.
goetz@22856 800 Node *branch = block->get_node(branch_idx-1);
goetz@22856 801
goetz@22856 802 // Handle no-flip branches which have implicit checks and which require
goetz@22856 803 // special block ordering and individual semantics of the 'fall through
goetz@22856 804 // case'.
goetz@22856 805 if ((TrapBasedNullChecks || TrapBasedRangeChecks) &&
goetz@22856 806 branch->is_Mach() && branch->as_Mach()->is_TrapBasedCheckNode()) {
goetz@22856 807 bnext = fixup_trap_based_check(branch, block, i, bnext);
goetz@22856 808 } else {
goetz@22856 809 // Else, default handling for no-flip branches
goetz@22856 810 for (uint j2 = 0; j2 < block->_num_succs; j2++) {
goetz@22856 811 const ProjNode* p = block->get_node(branch_idx + j2)->as_Proj();
goetz@22856 812 if (p->_con == 0) {
goetz@22856 813 // successor j2 is fall through case
goetz@22856 814 if (block->non_connector_successor(j2) != bnext) {
goetz@22856 815 // but it is not the next block => insert a goto
goetz@22856 816 insert_goto_at(i, j2);
goetz@22856 817 }
goetz@22856 818 // Put taken branch in slot 0
goetz@22856 819 if (j2 == 0 && block->_num_succs == 2) {
goetz@22856 820 // Flip targets in succs map
goetz@22856 821 Block *tbs0 = block->_succs[0];
goetz@22856 822 Block *tbs1 = block->_succs[1];
goetz@22856 823 block->_succs.map(0, tbs1);
goetz@22856 824 block->_succs.map(1, tbs0);
goetz@22856 825 }
goetz@22856 826 break;
duke@1 827 }
duke@1 828 }
duke@1 829 }
adlertz@19330 830
duke@1 831 // Remove all CatchProjs
adlertz@19330 832 for (uint j = 0; j < block->_num_succs; j++) {
adlertz@19717 833 block->pop_node();
adlertz@19330 834 }
duke@1 835
adlertz@19330 836 } else if (block->_num_succs == 1) {
duke@1 837 // Block ends in a Goto?
duke@1 838 if (bnext == bs0) {
duke@1 839 // We fall into next block; remove the Goto
adlertz@19717 840 block->pop_node();
duke@1 841 }
duke@1 842
adlertz@19330 843 } else if(block->_num_succs == 2) { // Block ends in a If?
duke@1 844 // Get opcode of 1st projection (matches _succs[0])
duke@1 845 // Note: Since this basic block has 2 exits, the last 2 nodes must
duke@1 846 // be projections (in any order), the 3rd last node must be
duke@1 847 // the IfNode (we have excluded other 2-way exits such as
duke@1 848 // CatchNodes already).
adlertz@19717 849 MachNode* iff = block->get_node(block->number_of_nodes() - 3)->as_Mach();
adlertz@19717 850 ProjNode* proj0 = block->get_node(block->number_of_nodes() - 2)->as_Proj();
adlertz@19717 851 ProjNode* proj1 = block->get_node(block->number_of_nodes() - 1)->as_Proj();
duke@1 852
duke@1 853 // Assert that proj0 and succs[0] match up. Similarly for proj1 and succs[1].
adlertz@19330 854 assert(proj0->raw_out(0) == block->_succs[0]->head(), "Mismatch successor 0");
adlertz@19330 855 assert(proj1->raw_out(0) == block->_succs[1]->head(), "Mismatch successor 1");
duke@1 856
adlertz@19330 857 Block* bs1 = block->non_connector_successor(1);
duke@1 858
duke@1 859 // Check for neither successor block following the current
duke@1 860 // block ending in a conditional. If so, move one of the
duke@1 861 // successors after the current one, provided that the
duke@1 862 // successor was previously unscheduled, but moveable
duke@1 863 // (i.e., all paths to it involve a branch).
adlertz@19330 864 if (!C->do_freq_based_layout() && bnext != bs0 && bnext != bs1) {
duke@1 865 // Choose the more common successor based on the probability
duke@1 866 // of the conditional branch.
adlertz@19330 867 Block* bx = bs0;
adlertz@19330 868 Block* by = bs1;
duke@1 869
duke@1 870 // _prob is the probability of taking the true path. Make
duke@1 871 // p the probability of taking successor #1.
duke@1 872 float p = iff->as_MachIf()->_prob;
adlertz@19330 873 if (proj0->Opcode() == Op_IfTrue) {
duke@1 874 p = 1.0 - p;
duke@1 875 }
duke@1 876
duke@1 877 // Prefer successor #1 if p > 0.5
duke@1 878 if (p > PROB_FAIR) {
duke@1 879 bx = bs1;
duke@1 880 by = bs0;
duke@1 881 }
duke@1 882
duke@1 883 // Attempt the more common successor first
rasbold@1498 884 if (move_to_next(bx, i)) {
duke@1 885 bnext = bx;
rasbold@1498 886 } else if (move_to_next(by, i)) {
duke@1 887 bnext = by;
duke@1 888 }
duke@1 889 }
duke@1 890
duke@1 891 // Check for conditional branching the wrong way. Negate
duke@1 892 // conditional, if needed, so it falls into the following block
duke@1 893 // and branches to the not-following block.
duke@1 894
duke@1 895 // Check for the next block being in succs[0]. We are going to branch
duke@1 896 // to succs[0], so we want the fall-thru case as the next block in
duke@1 897 // succs[1].
duke@1 898 if (bnext == bs0) {
duke@1 899 // Fall-thru case in succs[0], so flip targets in succs map
adlertz@19330 900 Block* tbs0 = block->_succs[0];
adlertz@19330 901 Block* tbs1 = block->_succs[1];
adlertz@19330 902 block->_succs.map(0, tbs1);
adlertz@19330 903 block->_succs.map(1, tbs0);
duke@1 904 // Flip projection for each target
adlertz@19330 905 ProjNode* tmp = proj0;
adlertz@19330 906 proj0 = proj1;
adlertz@19330 907 proj1 = tmp;
duke@1 908
adlertz@19330 909 } else if(bnext != bs1) {
rasbold@1498 910 // Need a double-branch
duke@1 911 // The existing conditional branch need not change.
duke@1 912 // Add a unconditional branch to the false target.
duke@1 913 // Alas, it must appear in its own block and adding a
duke@1 914 // block this late in the game is complicated. Sigh.
duke@1 915 insert_goto_at(i, 1);
duke@1 916 }
duke@1 917
duke@1 918 // Make sure we TRUE branch to the target
adlertz@19330 919 if (proj0->Opcode() == Op_IfFalse) {
kvn@10266 920 iff->as_MachIf()->negate();
rasbold@1498 921 }
duke@1 922
adlertz@19717 923 block->pop_node(); // Remove IfFalse & IfTrue projections
adlertz@19717 924 block->pop_node();
duke@1 925
duke@1 926 } else {
duke@1 927 // Multi-exit block, e.g. a switch statement
duke@1 928 // But we don't need to do anything here
duke@1 929 }
duke@1 930 } // End of for all blocks
duke@1 931 }
duke@1 932
duke@1 933
goetz@22844 934 // postalloc_expand: Expand nodes after register allocation.
goetz@22844 935 //
goetz@22844 936 // postalloc_expand has to be called after register allocation, just
goetz@22844 937 // before output (i.e. scheduling). It only gets called if
goetz@22844 938 // Matcher::require_postalloc_expand is true.
goetz@22844 939 //
goetz@22844 940 // Background:
goetz@22844 941 //
goetz@22844 942 // Nodes that are expandend (one compound node requiring several
goetz@22844 943 // assembler instructions to be implemented split into two or more
goetz@22844 944 // non-compound nodes) after register allocation are not as nice as
goetz@22844 945 // the ones expanded before register allocation - they don't
goetz@22844 946 // participate in optimizations as global code motion. But after
goetz@22844 947 // register allocation we can expand nodes that use registers which
goetz@22844 948 // are not spillable or registers that are not allocated, because the
goetz@22844 949 // old compound node is simply replaced (in its location in the basic
goetz@22844 950 // block) by a new subgraph which does not contain compound nodes any
goetz@22844 951 // more. The scheduler called during output can later on process these
goetz@22844 952 // non-compound nodes.
goetz@22844 953 //
goetz@22844 954 // Implementation:
goetz@22844 955 //
goetz@22844 956 // Nodes requiring postalloc expand are specified in the ad file by using
goetz@22844 957 // a postalloc_expand statement instead of ins_encode. A postalloc_expand
goetz@22844 958 // contains a single call to an encoding, as does an ins_encode
goetz@22844 959 // statement. Instead of an emit() function a postalloc_expand() function
goetz@22844 960 // is generated that doesn't emit assembler but creates a new
goetz@22844 961 // subgraph. The code below calls this postalloc_expand function for each
goetz@22844 962 // node with the appropriate attribute. This function returns the new
goetz@22844 963 // nodes generated in an array passed in the call. The old node,
goetz@22844 964 // potential MachTemps before and potential Projs after it then get
goetz@22844 965 // disconnected and replaced by the new nodes. The instruction
goetz@22844 966 // generating the result has to be the last one in the array. In
goetz@22844 967 // general it is assumed that Projs after the node expanded are
goetz@22844 968 // kills. These kills are not required any more after expanding as
goetz@22844 969 // there are now explicitly visible def-use chains and the Projs are
goetz@22844 970 // removed. This does not hold for calls: They do not only have
goetz@22844 971 // kill-Projs but also Projs defining values. Therefore Projs after
goetz@22844 972 // the node expanded are removed for all but for calls. If a node is
goetz@22844 973 // to be reused, it must be added to the nodes list returned, and it
goetz@22844 974 // will be added again.
goetz@22844 975 //
goetz@22844 976 // Implementing the postalloc_expand function for a node in an enc_class
goetz@22844 977 // is rather tedious. It requires knowledge about many node details, as
goetz@22844 978 // the nodes and the subgraph must be hand crafted. To simplify this,
goetz@22844 979 // adlc generates some utility variables into the postalloc_expand function,
goetz@22844 980 // e.g., holding the operands as specified by the postalloc_expand encoding
goetz@22844 981 // specification, e.g.:
goetz@22844 982 // * unsigned idx_<par_name> holding the index of the node in the ins
goetz@22844 983 // * Node *n_<par_name> holding the node loaded from the ins
goetz@22844 984 // * MachOpnd *op_<par_name> holding the corresponding operand
goetz@22844 985 //
goetz@22844 986 // The ordering of operands can not be determined by looking at a
goetz@22844 987 // rule. Especially if a match rule matches several different trees,
goetz@22844 988 // several nodes are generated from one instruct specification with
goetz@22844 989 // different operand orderings. In this case the adlc generated
goetz@22844 990 // variables are the only way to access the ins and operands
goetz@22844 991 // deterministically.
goetz@22844 992 //
goetz@22844 993 // If assigning a register to a node that contains an oop, don't
goetz@22844 994 // forget to call ra_->set_oop() for the node.
goetz@22844 995 void PhaseCFG::postalloc_expand(PhaseRegAlloc* _ra) {
goetz@22844 996 GrowableArray <Node *> new_nodes(32); // Array with new nodes filled by postalloc_expand function of node.
goetz@22844 997 GrowableArray <Node *> remove(32);
goetz@22844 998 GrowableArray <Node *> succs(32);
goetz@22844 999 unsigned int max_idx = C->unique(); // Remember to distinguish new from old nodes.
goetz@22844 1000 DEBUG_ONLY(bool foundNode = false);
goetz@22844 1001
goetz@22844 1002 // for all blocks
goetz@22844 1003 for (uint i = 0; i < number_of_blocks(); i++) {
goetz@22844 1004 Block *b = _blocks[i];
goetz@22844 1005 // For all instructions in the current block.
goetz@22844 1006 for (uint j = 0; j < b->number_of_nodes(); j++) {
goetz@22844 1007 Node *n = b->get_node(j);
goetz@22844 1008 if (n->is_Mach() && n->as_Mach()->requires_postalloc_expand()) {
goetz@22844 1009 #ifdef ASSERT
goetz@22844 1010 if (TracePostallocExpand) {
goetz@22844 1011 if (!foundNode) {
goetz@22844 1012 foundNode = true;
goetz@22844 1013 tty->print("POSTALLOC EXPANDING %d %s\n", C->compile_id(),
goetz@22844 1014 C->method() ? C->method()->name()->as_utf8() : C->stub_name());
goetz@22844 1015 }
goetz@22844 1016 tty->print(" postalloc expanding "); n->dump();
goetz@22844 1017 if (Verbose) {
goetz@22844 1018 tty->print(" with ins:\n");
goetz@22844 1019 for (uint k = 0; k < n->len(); ++k) {
goetz@22844 1020 if (n->in(k)) { tty->print(" "); n->in(k)->dump(); }
goetz@22844 1021 }
goetz@22844 1022 }
goetz@22844 1023 }
goetz@22844 1024 #endif
goetz@22844 1025 new_nodes.clear();
goetz@22844 1026 // Collect nodes that have to be removed from the block later on.
goetz@22844 1027 uint req = n->req();
goetz@22844 1028 remove.clear();
goetz@22844 1029 for (uint k = 0; k < req; ++k) {
goetz@22844 1030 if (n->in(k) && n->in(k)->is_MachTemp()) {
goetz@22844 1031 remove.push(n->in(k)); // MachTemps which are inputs to the old node have to be removed.
goetz@22844 1032 n->in(k)->del_req(0);
goetz@22844 1033 j--;
goetz@22844 1034 }
goetz@22844 1035 }
goetz@22844 1036
goetz@22844 1037 // Check whether we can allocate enough nodes. We set a fix limit for
goetz@22844 1038 // the size of postalloc expands with this.
goetz@22844 1039 uint unique_limit = C->unique() + 40;
goetz@22844 1040 if (unique_limit >= _ra->node_regs_max_index()) {
goetz@22844 1041 Compile::current()->record_failure("out of nodes in postalloc expand");
goetz@22844 1042 return;
goetz@22844 1043 }
goetz@22844 1044
goetz@22844 1045 // Emit (i.e. generate new nodes).
goetz@22844 1046 n->as_Mach()->postalloc_expand(&new_nodes, _ra);
goetz@22844 1047
goetz@22844 1048 assert(C->unique() < unique_limit, "You allocated too many nodes in your postalloc expand.");
goetz@22844 1049
goetz@22844 1050 // Disconnect the inputs of the old node.
goetz@22844 1051 //
goetz@22844 1052 // We reuse MachSpillCopy nodes. If we need to expand them, there
goetz@22844 1053 // are many, so reusing pays off. If reused, the node already
goetz@22844 1054 // has the new ins. n must be the last node on new_nodes list.
goetz@22844 1055 if (!n->is_MachSpillCopy()) {
goetz@22844 1056 for (int k = req - 1; k >= 0; --k) {
goetz@22844 1057 n->del_req(k);
goetz@22844 1058 }
goetz@22844 1059 }
goetz@22844 1060
goetz@22844 1061 #ifdef ASSERT
goetz@22844 1062 // Check that all nodes have proper operands.
goetz@22844 1063 for (int k = 0; k < new_nodes.length(); ++k) {
goetz@22844 1064 if (new_nodes.at(k)->_idx < max_idx || !new_nodes.at(k)->is_Mach()) continue; // old node, Proj ...
goetz@22844 1065 MachNode *m = new_nodes.at(k)->as_Mach();
goetz@22844 1066 for (unsigned int l = 0; l < m->num_opnds(); ++l) {
goetz@22844 1067 if (MachOper::notAnOper(m->_opnds[l])) {
goetz@22844 1068 outputStream *os = tty;
goetz@22844 1069 os->print("Node %s ", m->Name());
goetz@22844 1070 os->print("has invalid opnd %d: %p\n", l, m->_opnds[l]);
goetz@22844 1071 assert(0, "Invalid operands, see inline trace in hs_err_pid file.");
goetz@22844 1072 }
goetz@22844 1073 }
goetz@22844 1074 }
goetz@22844 1075 #endif
goetz@22844 1076
goetz@22844 1077 // Collect succs of old node in remove (for projections) and in succs (for
goetz@22844 1078 // all other nodes) do _not_ collect projections in remove (but in succs)
goetz@22844 1079 // in case the node is a call. We need the projections for calls as they are
goetz@22844 1080 // associated with registes (i.e. they are defs).
goetz@22844 1081 succs.clear();
goetz@22844 1082 for (DUIterator k = n->outs(); n->has_out(k); k++) {
goetz@22844 1083 if (n->out(k)->is_Proj() && !n->is_MachCall() && !n->is_MachBranch()) {
goetz@22844 1084 remove.push(n->out(k));
goetz@22844 1085 } else {
goetz@22844 1086 succs.push(n->out(k));
goetz@22844 1087 }
goetz@22844 1088 }
goetz@22844 1089 // Replace old node n as input of its succs by last of the new nodes.
goetz@22844 1090 for (int k = 0; k < succs.length(); ++k) {
goetz@22844 1091 Node *succ = succs.at(k);
goetz@22844 1092 for (uint l = 0; l < succ->req(); ++l) {
goetz@22844 1093 if (succ->in(l) == n) {
goetz@22844 1094 succ->set_req(l, new_nodes.at(new_nodes.length() - 1));
goetz@22844 1095 }
goetz@22844 1096 }
goetz@22844 1097 for (uint l = succ->req(); l < succ->len(); ++l) {
goetz@22844 1098 if (succ->in(l) == n) {
goetz@22844 1099 succ->set_prec(l, new_nodes.at(new_nodes.length() - 1));
goetz@22844 1100 }
goetz@22844 1101 }
goetz@22844 1102 }
goetz@22844 1103
goetz@22844 1104 // Index of old node in block.
goetz@22844 1105 uint index = b->find_node(n);
goetz@22844 1106 // Insert new nodes into block and map them in nodes->blocks array
goetz@22844 1107 // and remember last node in n2.
goetz@22844 1108 Node *n2 = NULL;
goetz@22844 1109 for (int k = 0; k < new_nodes.length(); ++k) {
goetz@22844 1110 n2 = new_nodes.at(k);
goetz@22844 1111 b->insert_node(n2, ++index);
goetz@22844 1112 map_node_to_block(n2, b);
goetz@22844 1113 }
goetz@22844 1114
goetz@22844 1115 // Add old node n to remove and remove them all from block.
goetz@22844 1116 remove.push(n);
goetz@22844 1117 j--;
goetz@22844 1118 #ifdef ASSERT
goetz@22844 1119 if (TracePostallocExpand && Verbose) {
goetz@22844 1120 tty->print(" removing:\n");
goetz@22844 1121 for (int k = 0; k < remove.length(); ++k) {
goetz@22844 1122 tty->print(" "); remove.at(k)->dump();
goetz@22844 1123 }
goetz@22844 1124 tty->print(" inserting:\n");
goetz@22844 1125 for (int k = 0; k < new_nodes.length(); ++k) {
goetz@22844 1126 tty->print(" "); new_nodes.at(k)->dump();
goetz@22844 1127 }
goetz@22844 1128 }
goetz@22844 1129 #endif
goetz@22844 1130 for (int k = 0; k < remove.length(); ++k) {
goetz@22844 1131 if (b->contains(remove.at(k))) {
goetz@22844 1132 b->find_remove(remove.at(k));
goetz@22844 1133 } else {
goetz@22844 1134 assert(remove.at(k)->is_Proj() && (remove.at(k)->in(0)->is_MachBranch()), "");
goetz@22844 1135 }
goetz@22844 1136 }
goetz@22844 1137 // If anything has been inserted (n2 != NULL), continue after last node inserted.
goetz@22844 1138 // This does not always work. Some postalloc expands don't insert any nodes, if they
goetz@22844 1139 // do optimizations (e.g., max(x,x)). In this case we decrement j accordingly.
goetz@22844 1140 j = n2 ? b->find_node(n2) : j;
goetz@22844 1141 }
goetz@22844 1142 }
goetz@22844 1143 }
goetz@22844 1144
goetz@22844 1145 #ifdef ASSERT
goetz@22844 1146 if (foundNode) {
goetz@22844 1147 tty->print("FINISHED %d %s\n", C->compile_id(),
goetz@22844 1148 C->method() ? C->method()->name()->as_utf8() : C->stub_name());
goetz@22844 1149 tty->flush();
goetz@22844 1150 }
goetz@22844 1151 #endif
goetz@22844 1152 }
goetz@22844 1153
goetz@22844 1154
goetz@22844 1155 //------------------------------dump-------------------------------------------
duke@1 1156 #ifndef PRODUCT
duke@1 1157 void PhaseCFG::_dump_cfg( const Node *end, VectorSet &visited ) const {
duke@1 1158 const Node *x = end->is_block_proj();
duke@1 1159 assert( x, "not a CFG" );
duke@1 1160
duke@1 1161 // Do not visit this block again
duke@1 1162 if( visited.test_set(x->_idx) ) return;
duke@1 1163
duke@1 1164 // Skip through this block
duke@1 1165 const Node *p = x;
duke@1 1166 do {
duke@1 1167 p = p->in(0); // Move control forward
duke@1 1168 assert( !p->is_block_proj() || p->is_Root(), "not a CFG" );
duke@1 1169 } while( !p->is_block_start() );
duke@1 1170
duke@1 1171 // Recursively visit
adlertz@19279 1172 for (uint i = 1; i < p->req(); i++) {
adlertz@19279 1173 _dump_cfg(p->in(i), visited);
adlertz@19279 1174 }
duke@1 1175
duke@1 1176 // Dump the block
adlertz@19279 1177 get_block_for_node(p)->dump(this);
duke@1 1178 }
duke@1 1179
duke@1 1180 void PhaseCFG::dump( ) const {
adlertz@19330 1181 tty->print("\n--- CFG --- %d BBs\n", number_of_blocks());
adlertz@19279 1182 if (_blocks.size()) { // Did we do basic-block layout?
adlertz@19330 1183 for (uint i = 0; i < number_of_blocks(); i++) {
adlertz@19330 1184 const Block* block = get_block(i);
adlertz@19330 1185 block->dump(this);
adlertz@19279 1186 }
duke@1 1187 } else { // Else do it with a DFS
adlertz@19279 1188 VectorSet visited(_block_arena);
duke@1 1189 _dump_cfg(_root,visited);
duke@1 1190 }
duke@1 1191 }
duke@1 1192
duke@1 1193 void PhaseCFG::dump_headers() {
adlertz@19330 1194 for (uint i = 0; i < number_of_blocks(); i++) {
adlertz@19330 1195 Block* block = get_block(i);
adlertz@19330 1196 if (block != NULL) {
adlertz@19330 1197 block->dump_head(this);
adlertz@19279 1198 }
duke@1 1199 }
duke@1 1200 }
duke@1 1201
adlertz@19330 1202 void PhaseCFG::verify() const {
kvn@2030 1203 #ifdef ASSERT
duke@1 1204 // Verify sane CFG
adlertz@19330 1205 for (uint i = 0; i < number_of_blocks(); i++) {
adlertz@19330 1206 Block* block = get_block(i);
adlertz@19717 1207 uint cnt = block->number_of_nodes();
duke@1 1208 uint j;
kvn@11191 1209 for (j = 0; j < cnt; j++) {
adlertz@19717 1210 Node *n = block->get_node(j);
adlertz@19330 1211 assert(get_block_for_node(n) == block, "");
adlertz@19330 1212 if (j >= 1 && n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_CreateEx) {
adlertz@19717 1213 assert(j == 1 || block->get_node(j-1)->is_Phi(), "CreateEx must be first instruction in block");
duke@1 1214 }
kvn@40862 1215 if (n->needs_anti_dependence_check()) {
kvn@40862 1216 verify_anti_dependences(block, n);
kvn@40862 1217 }
kvn@11191 1218 for (uint k = 0; k < n->req(); k++) {
kvn@2030 1219 Node *def = n->in(k);
kvn@11191 1220 if (def && def != n) {
adlertz@19279 1221 assert(get_block_for_node(def) || def->is_Con(), "must have block; constants for debug info ok");
kvn@2030 1222 // Verify that instructions in the block is in correct order.
kvn@2030 1223 // Uses must follow their definition if they are at the same block.
kvn@2030 1224 // Mostly done to check that MachSpillCopy nodes are placed correctly
kvn@2030 1225 // when CreateEx node is moved in build_ifg_physical().
adlertz@19330 1226 if (get_block_for_node(def) == block && !(block->head()->is_Loop() && n->is_Phi()) &&
kvn@2030 1227 // See (+++) comment in reg_split.cpp
kvn@11191 1228 !(n->jvms() != NULL && n->jvms()->is_monitor_use(k))) {
kvn@3593 1229 bool is_loop = false;
kvn@3593 1230 if (n->is_Phi()) {
kvn@11191 1231 for (uint l = 1; l < def->req(); l++) {
kvn@3593 1232 if (n == def->in(l)) {
kvn@3593 1233 is_loop = true;
kvn@3593 1234 break; // Some kind of loop
kvn@3593 1235 }
kvn@3593 1236 }
kvn@3593 1237 }
adlertz@19330 1238 assert(is_loop || block->find_node(def) < j, "uses must follow definitions");
kvn@2127 1239 }
duke@1 1240 }
duke@1 1241 }
duke@1 1242 }
duke@1 1243
adlertz@19330 1244 j = block->end_idx();
adlertz@19717 1245 Node* bp = (Node*)block->get_node(block->number_of_nodes() - 1)->is_block_proj();
adlertz@19330 1246 assert(bp, "last instruction must be a block proj");
adlertz@19717 1247 assert(bp == block->get_node(j), "wrong number of successors for this block");
kvn@11191 1248 if (bp->is_Catch()) {
adlertz@19717 1249 while (block->get_node(--j)->is_MachProj()) {
adlertz@19330 1250 ;
adlertz@19330 1251 }
adlertz@19717 1252 assert(block->get_node(j)->is_MachCall(), "CatchProj must follow call");
kvn@11191 1253 } else if (bp->is_Mach() && bp->as_Mach()->ideal_Opcode() == Op_If) {
adlertz@19330 1254 assert(block->_num_succs == 2, "Conditional branch must have two targets");
duke@1 1255 }
duke@1 1256 }
kvn@2030 1257 #endif
duke@1 1258 }
duke@1 1259 #endif
duke@1 1260
duke@1 1261 UnionFind::UnionFind( uint max ) : _cnt(max), _max(max), _indices(NEW_RESOURCE_ARRAY(uint,max)) {
duke@1 1262 Copy::zero_to_bytes( _indices, sizeof(uint)*max );
duke@1 1263 }
duke@1 1264
duke@1 1265 void UnionFind::extend( uint from_idx, uint to_idx ) {
duke@1 1266 _nesting.check();
duke@1 1267 if( from_idx >= _max ) {
duke@1 1268 uint size = 16;
duke@1 1269 while( size <= from_idx ) size <<=1;
duke@1 1270 _indices = REALLOC_RESOURCE_ARRAY( uint, _indices, _max, size );
duke@1 1271 _max = size;
duke@1 1272 }
duke@1 1273 while( _cnt <= from_idx ) _indices[_cnt++] = 0;
duke@1 1274 _indices[from_idx] = to_idx;
duke@1 1275 }
duke@1 1276
duke@1 1277 void UnionFind::reset( uint max ) {
duke@1 1278 // Force the Union-Find mapping to be at least this large
duke@1 1279 extend(max,0);
duke@1 1280 // Initialize to be the ID mapping.
rasbold@1498 1281 for( uint i=0; i<max; i++ ) map(i,i);
duke@1 1282 }
duke@1 1283
duke@1 1284 // Straight out of Tarjan's union-find algorithm
duke@1 1285 uint UnionFind::Find_compress( uint idx ) {
duke@1 1286 uint cur = idx;
duke@1 1287 uint next = lookup(cur);
duke@1 1288 while( next != cur ) { // Scan chain of equivalences
duke@1 1289 assert( next < cur, "always union smaller" );
duke@1 1290 cur = next; // until find a fixed-point
duke@1 1291 next = lookup(cur);
duke@1 1292 }
duke@1 1293 // Core of union-find algorithm: update chain of
duke@1 1294 // equivalences to be equal to the root.
duke@1 1295 while( idx != next ) {
duke@1 1296 uint tmp = lookup(idx);
duke@1 1297 map(idx, next);
duke@1 1298 idx = tmp;
duke@1 1299 }
duke@1 1300 return idx;
duke@1 1301 }
duke@1 1302
duke@1 1303 // Like Find above, but no path compress, so bad asymptotic behavior
duke@1 1304 uint UnionFind::Find_const( uint idx ) const {
duke@1 1305 if( idx == 0 ) return idx; // Ignore the zero idx
duke@1 1306 // Off the end? This can happen during debugging dumps
duke@1 1307 // when data structures have not finished being updated.
duke@1 1308 if( idx >= _max ) return idx;
duke@1 1309 uint next = lookup(idx);
duke@1 1310 while( next != idx ) { // Scan chain of equivalences
duke@1 1311 idx = next; // until find a fixed-point
duke@1 1312 next = lookup(idx);
duke@1 1313 }
duke@1 1314 return next;
duke@1 1315 }
duke@1 1316
duke@1 1317 // union 2 sets together.
duke@1 1318 void UnionFind::Union( uint idx1, uint idx2 ) {
duke@1 1319 uint src = Find(idx1);
duke@1 1320 uint dst = Find(idx2);
duke@1 1321 assert( src, "" );
duke@1 1322 assert( dst, "" );
duke@1 1323 assert( src < _max, "oob" );
duke@1 1324 assert( dst < _max, "oob" );
duke@1 1325 assert( src < dst, "always union smaller" );
duke@1 1326 map(dst,src);
duke@1 1327 }
rasbold@1498 1328
rasbold@1498 1329 #ifndef PRODUCT
rasbold@1498 1330 void Trace::dump( ) const {
rasbold@1498 1331 tty->print_cr("Trace (freq %f)", first_block()->_freq);
rasbold@1498 1332 for (Block *b = first_block(); b != NULL; b = next(b)) {
rasbold@1498 1333 tty->print(" B%d", b->_pre_order);
rasbold@1498 1334 if (b->head()->is_Loop()) {
rasbold@1498 1335 tty->print(" (L%d)", b->compute_loop_alignment());
rasbold@1498 1336 }
rasbold@1498 1337 if (b->has_loop_alignment()) {
rasbold@1498 1338 tty->print(" (T%d)", b->code_alignment());
rasbold@1498 1339 }
rasbold@1498 1340 }
rasbold@1498 1341 tty->cr();
rasbold@1498 1342 }
rasbold@1498 1343
rasbold@1498 1344 void CFGEdge::dump( ) const {
rasbold@1498 1345 tty->print(" B%d --> B%d Freq: %f out:%3d%% in:%3d%% State: ",
rasbold@1498 1346 from()->_pre_order, to()->_pre_order, freq(), _from_pct, _to_pct);
rasbold@1498 1347 switch(state()) {
rasbold@1498 1348 case connected:
rasbold@1498 1349 tty->print("connected");
rasbold@1498 1350 break;
rasbold@1498 1351 case open:
rasbold@1498 1352 tty->print("open");
rasbold@1498 1353 break;
rasbold@1498 1354 case interior:
rasbold@1498 1355 tty->print("interior");
rasbold@1498 1356 break;
rasbold@1498 1357 }
rasbold@1498 1358 if (infrequent()) {
rasbold@1498 1359 tty->print(" infrequent");
rasbold@1498 1360 }
rasbold@1498 1361 tty->cr();
rasbold@1498 1362 }
rasbold@1498 1363 #endif
rasbold@1498 1364
rasbold@1498 1365 // Comparison function for edges
rasbold@1498 1366 static int edge_order(CFGEdge **e0, CFGEdge **e1) {
rasbold@1498 1367 float freq0 = (*e0)->freq();
rasbold@1498 1368 float freq1 = (*e1)->freq();
rasbold@1498 1369 if (freq0 != freq1) {
rasbold@1498 1370 return freq0 > freq1 ? -1 : 1;
rasbold@1498 1371 }
rasbold@1498 1372
rasbold@1498 1373 int dist0 = (*e0)->to()->_rpo - (*e0)->from()->_rpo;
rasbold@1498 1374 int dist1 = (*e1)->to()->_rpo - (*e1)->from()->_rpo;
rasbold@1498 1375
rasbold@1498 1376 return dist1 - dist0;
rasbold@1498 1377 }
rasbold@1498 1378
rasbold@1498 1379 // Comparison function for edges
kvn@10537 1380 extern "C" int trace_frequency_order(const void *p0, const void *p1) {
rasbold@1498 1381 Trace *tr0 = *(Trace **) p0;
rasbold@1498 1382 Trace *tr1 = *(Trace **) p1;
rasbold@1498 1383 Block *b0 = tr0->first_block();
rasbold@1498 1384 Block *b1 = tr1->first_block();
rasbold@1498 1385
rasbold@1498 1386 // The trace of connector blocks goes at the end;
rasbold@1498 1387 // we only expect one such trace
rasbold@1498 1388 if (b0->is_connector() != b1->is_connector()) {
rasbold@1498 1389 return b1->is_connector() ? -1 : 1;
rasbold@1498 1390 }
rasbold@1498 1391
rasbold@1498 1392 // Pull more frequently executed blocks to the beginning
rasbold@1498 1393 float freq0 = b0->_freq;
rasbold@1498 1394 float freq1 = b1->_freq;
rasbold@1498 1395 if (freq0 != freq1) {
rasbold@1498 1396 return freq0 > freq1 ? -1 : 1;
rasbold@1498 1397 }
rasbold@1498 1398
rasbold@1498 1399 int diff = tr0->first_block()->_rpo - tr1->first_block()->_rpo;
rasbold@1498 1400
rasbold@1498 1401 return diff;
rasbold@1498 1402 }
rasbold@1498 1403
rasbold@1498 1404 // Find edges of interest, i.e, those which can fall through. Presumes that
rasbold@1498 1405 // edges which don't fall through are of low frequency and can be generally
rasbold@1498 1406 // ignored. Initialize the list of traces.
adlertz@19330 1407 void PhaseBlockLayout::find_edges() {
rasbold@1498 1408 // Walk the blocks, creating edges and Traces
rasbold@1498 1409 uint i;
rasbold@1498 1410 Trace *tr = NULL;
adlertz@19330 1411 for (i = 0; i < _cfg.number_of_blocks(); i++) {
adlertz@19330 1412 Block* b = _cfg.get_block(i);
rasbold@1498 1413 tr = new Trace(b, next, prev);
rasbold@1498 1414 traces[tr->id()] = tr;
rasbold@1498 1415
rasbold@1498 1416 // All connector blocks should be at the end of the list
rasbold@1498 1417 if (b->is_connector()) break;
rasbold@1498 1418
rasbold@1498 1419 // If this block and the next one have a one-to-one successor
rasbold@1498 1420 // predecessor relationship, simply append the next block
rasbold@1498 1421 int nfallthru = b->num_fall_throughs();
rasbold@1498 1422 while (nfallthru == 1 &&
rasbold@1498 1423 b->succ_fall_through(0)) {
rasbold@1498 1424 Block *n = b->_succs[0];
rasbold@1498 1425
rasbold@1498 1426 // Skip over single-entry connector blocks, we don't want to
rasbold@1498 1427 // add them to the trace.
rasbold@1498 1428 while (n->is_connector() && n->num_preds() == 1) {
rasbold@1498 1429 n = n->_succs[0];
rasbold@1498 1430 }
rasbold@1498 1431
rasbold@1498 1432 // We see a merge point, so stop search for the next block
rasbold@1498 1433 if (n->num_preds() != 1) break;
rasbold@1498 1434
rasbold@1498 1435 i++;
goetz@38658 1436 assert(n == _cfg.get_block(i), "expecting next block");
rasbold@1498 1437 tr->append(n);
rasbold@1498 1438 uf->map(n->_pre_order, tr->id());
rasbold@1498 1439 traces[n->_pre_order] = NULL;
rasbold@1498 1440 nfallthru = b->num_fall_throughs();
rasbold@1498 1441 b = n;
rasbold@1498 1442 }
rasbold@1498 1443
rasbold@1498 1444 if (nfallthru > 0) {
rasbold@1498 1445 // Create a CFGEdge for each outgoing
rasbold@1498 1446 // edge that could be a fall-through.
rasbold@1498 1447 for (uint j = 0; j < b->_num_succs; j++ ) {
rasbold@1498 1448 if (b->succ_fall_through(j)) {
rasbold@1498 1449 Block *target = b->non_connector_successor(j);
rasbold@1498 1450 float freq = b->_freq * b->succ_prob(j);
rasbold@1498 1451 int from_pct = (int) ((100 * freq) / b->_freq);
rasbold@1498 1452 int to_pct = (int) ((100 * freq) / target->_freq);
rasbold@1498 1453 edges->append(new CFGEdge(b, target, freq, from_pct, to_pct));
rasbold@1498 1454 }
rasbold@1498 1455 }
rasbold@1498 1456 }
rasbold@1498 1457 }
rasbold@1498 1458
rasbold@1498 1459 // Group connector blocks into one trace
adlertz@19330 1460 for (i++; i < _cfg.number_of_blocks(); i++) {
adlertz@19330 1461 Block *b = _cfg.get_block(i);
rasbold@1498 1462 assert(b->is_connector(), "connector blocks at the end");
rasbold@1498 1463 tr->append(b);
rasbold@1498 1464 uf->map(b->_pre_order, tr->id());
rasbold@1498 1465 traces[b->_pre_order] = NULL;
rasbold@1498 1466 }
rasbold@1498 1467 }
rasbold@1498 1468
rasbold@1498 1469 // Union two traces together in uf, and null out the trace in the list
adlertz@19330 1470 void PhaseBlockLayout::union_traces(Trace* updated_trace, Trace* old_trace) {
rasbold@1498 1471 uint old_id = old_trace->id();
rasbold@1498 1472 uint updated_id = updated_trace->id();
rasbold@1498 1473
rasbold@1498 1474 uint lo_id = updated_id;
rasbold@1498 1475 uint hi_id = old_id;
rasbold@1498 1476
rasbold@1498 1477 // If from is greater than to, swap values to meet
rasbold@1498 1478 // UnionFind guarantee.
rasbold@1498 1479 if (updated_id > old_id) {
rasbold@1498 1480 lo_id = old_id;
rasbold@1498 1481 hi_id = updated_id;
rasbold@1498 1482
rasbold@1498 1483 // Fix up the trace ids
rasbold@1498 1484 traces[lo_id] = traces[updated_id];
rasbold@1498 1485 updated_trace->set_id(lo_id);
rasbold@1498 1486 }
rasbold@1498 1487
rasbold@1498 1488 // Union the lower with the higher and remove the pointer
rasbold@1498 1489 // to the higher.
rasbold@1498 1490 uf->Union(lo_id, hi_id);
rasbold@1498 1491 traces[hi_id] = NULL;
rasbold@1498 1492 }
rasbold@1498 1493
rasbold@1498 1494 // Append traces together via the most frequently executed edges
adlertz@19330 1495 void PhaseBlockLayout::grow_traces() {
rasbold@1498 1496 // Order the edges, and drive the growth of Traces via the most
rasbold@1498 1497 // frequently executed edges.
rasbold@1498 1498 edges->sort(edge_order);
rasbold@1498 1499 for (int i = 0; i < edges->length(); i++) {
rasbold@1498 1500 CFGEdge *e = edges->at(i);
rasbold@1498 1501
rasbold@1498 1502 if (e->state() != CFGEdge::open) continue;
rasbold@1498 1503
rasbold@1498 1504 Block *src_block = e->from();
rasbold@1498 1505 Block *targ_block = e->to();
rasbold@1498 1506
rasbold@1498 1507 // Don't grow traces along backedges?
rasbold@1498 1508 if (!BlockLayoutRotateLoops) {
rasbold@1498 1509 if (targ_block->_rpo <= src_block->_rpo) {
rasbold@1498 1510 targ_block->set_loop_alignment(targ_block);
rasbold@1498 1511 continue;
rasbold@1498 1512 }
rasbold@1498 1513 }
rasbold@1498 1514
rasbold@1498 1515 Trace *src_trace = trace(src_block);
rasbold@1498 1516 Trace *targ_trace = trace(targ_block);
rasbold@1498 1517
rasbold@1498 1518 // If the edge in question can join two traces at their ends,
rasbold@1498 1519 // append one trace to the other.
rasbold@1498 1520 if (src_trace->last_block() == src_block) {
rasbold@1498 1521 if (src_trace == targ_trace) {
rasbold@1498 1522 e->set_state(CFGEdge::interior);
rasbold@1498 1523 if (targ_trace->backedge(e)) {
rasbold@1498 1524 // Reset i to catch any newly eligible edge
rasbold@1498 1525 // (Or we could remember the first "open" edge, and reset there)
rasbold@1498 1526 i = 0;
rasbold@1498 1527 }
rasbold@1498 1528 } else if (targ_trace->first_block() == targ_block) {
rasbold@1498 1529 e->set_state(CFGEdge::connected);
rasbold@1498 1530 src_trace->append(targ_trace);
rasbold@1498 1531 union_traces(src_trace, targ_trace);
rasbold@1498 1532 }
rasbold@1498 1533 }
rasbold@1498 1534 }
rasbold@1498 1535 }
rasbold@1498 1536
rasbold@1498 1537 // Embed one trace into another, if the fork or join points are sufficiently
rasbold@1498 1538 // balanced.
adlertz@19330 1539 void PhaseBlockLayout::merge_traces(bool fall_thru_only) {
rasbold@1498 1540 // Walk the edge list a another time, looking at unprocessed edges.
rasbold@1498 1541 // Fold in diamonds
rasbold@1498 1542 for (int i = 0; i < edges->length(); i++) {
rasbold@1498 1543 CFGEdge *e = edges->at(i);
rasbold@1498 1544
rasbold@1498 1545 if (e->state() != CFGEdge::open) continue;
rasbold@1498 1546 if (fall_thru_only) {
rasbold@1498 1547 if (e->infrequent()) continue;
rasbold@1498 1548 }
rasbold@1498 1549
rasbold@1498 1550 Block *src_block = e->from();
rasbold@1498 1551 Trace *src_trace = trace(src_block);
rasbold@1498 1552 bool src_at_tail = src_trace->last_block() == src_block;
rasbold@1498 1553
rasbold@1498 1554 Block *targ_block = e->to();
rasbold@1498 1555 Trace *targ_trace = trace(targ_block);
rasbold@1498 1556 bool targ_at_start = targ_trace->first_block() == targ_block;
rasbold@1498 1557
rasbold@1498 1558 if (src_trace == targ_trace) {
rasbold@1498 1559 // This may be a loop, but we can't do much about it.
rasbold@1498 1560 e->set_state(CFGEdge::interior);
rasbold@1498 1561 continue;
rasbold@1498 1562 }
rasbold@1498 1563
rasbold@1498 1564 if (fall_thru_only) {
rasbold@1498 1565 // If the edge links the middle of two traces, we can't do anything.
rasbold@1498 1566 // Mark the edge and continue.
rasbold@1498 1567 if (!src_at_tail & !targ_at_start) {
rasbold@1498 1568 continue;
rasbold@1498 1569 }
rasbold@1498 1570
rasbold@1498 1571 // Don't grow traces along backedges?
rasbold@1498 1572 if (!BlockLayoutRotateLoops && (targ_block->_rpo <= src_block->_rpo)) {
rasbold@1498 1573 continue;
rasbold@1498 1574 }
rasbold@1498 1575
rasbold@1498 1576 // If both ends of the edge are available, why didn't we handle it earlier?
rasbold@1498 1577 assert(src_at_tail ^ targ_at_start, "Should have caught this edge earlier.");
rasbold@1498 1578
rasbold@1498 1579 if (targ_at_start) {
rasbold@1498 1580 // Insert the "targ" trace in the "src" trace if the insertion point
rasbold@1498 1581 // is a two way branch.
rasbold@1498 1582 // Better profitability check possible, but may not be worth it.
rasbold@1498 1583 // Someday, see if the this "fork" has an associated "join";
rasbold@1498 1584 // then make a policy on merging this trace at the fork or join.
rasbold@1498 1585 // For example, other things being equal, it may be better to place this
rasbold@1498 1586 // trace at the join point if the "src" trace ends in a two-way, but
rasbold@1498 1587 // the insertion point is one-way.
rasbold@1498 1588 assert(src_block->num_fall_throughs() == 2, "unexpected diamond");
rasbold@1498 1589 e->set_state(CFGEdge::connected);
rasbold@1498 1590 src_trace->insert_after(src_block, targ_trace);
rasbold@1498 1591 union_traces(src_trace, targ_trace);
rasbold@1498 1592 } else if (src_at_tail) {
adlertz@19330 1593 if (src_trace != trace(_cfg.get_root_block())) {
rasbold@1498 1594 e->set_state(CFGEdge::connected);
rasbold@1498 1595 targ_trace->insert_before(targ_block, src_trace);
rasbold@1498 1596 union_traces(targ_trace, src_trace);
rasbold@1498 1597 }
rasbold@1498 1598 }
rasbold@1498 1599 } else if (e->state() == CFGEdge::open) {
rasbold@1498 1600 // Append traces, even without a fall-thru connection.
twisti@2131 1601 // But leave root entry at the beginning of the block list.
adlertz@19330 1602 if (targ_trace != trace(_cfg.get_root_block())) {
rasbold@1498 1603 e->set_state(CFGEdge::connected);
rasbold@1498 1604 src_trace->append(targ_trace);
rasbold@1498 1605 union_traces(src_trace, targ_trace);
rasbold@1498 1606 }
rasbold@1498 1607 }
rasbold@1498 1608 }
rasbold@1498 1609 }
rasbold@1498 1610
rasbold@1498 1611 // Order the sequence of the traces in some desirable way, and fixup the
rasbold@1498 1612 // jumps at the end of each block.
adlertz@19330 1613 void PhaseBlockLayout::reorder_traces(int count) {
rasbold@1498 1614 ResourceArea *area = Thread::current()->resource_area();
rasbold@1498 1615 Trace ** new_traces = NEW_ARENA_ARRAY(area, Trace *, count);
rasbold@1498 1616 Block_List worklist;
rasbold@1498 1617 int new_count = 0;
rasbold@1498 1618
rasbold@1498 1619 // Compact the traces.
rasbold@1498 1620 for (int i = 0; i < count; i++) {
rasbold@1498 1621 Trace *tr = traces[i];
rasbold@1498 1622 if (tr != NULL) {
rasbold@1498 1623 new_traces[new_count++] = tr;
rasbold@1498 1624 }
rasbold@1498 1625 }
rasbold@1498 1626
rasbold@1498 1627 // The entry block should be first on the new trace list.
adlertz@19330 1628 Trace *tr = trace(_cfg.get_root_block());
rasbold@1498 1629 assert(tr == new_traces[0], "entry trace misplaced");
rasbold@1498 1630
rasbold@1498 1631 // Sort the new trace list by frequency
rasbold@1498 1632 qsort(new_traces + 1, new_count - 1, sizeof(new_traces[0]), trace_frequency_order);
rasbold@1498 1633
rasbold@1498 1634 // Patch up the successor blocks
adlertz@19330 1635 _cfg.clear_blocks();
rasbold@1498 1636 for (int i = 0; i < new_count; i++) {
rasbold@1498 1637 Trace *tr = new_traces[i];
rasbold@1498 1638 if (tr != NULL) {
rasbold@1498 1639 tr->fixup_blocks(_cfg);
rasbold@1498 1640 }
rasbold@1498 1641 }
rasbold@1498 1642 }
rasbold@1498 1643
rasbold@1498 1644 // Order basic blocks based on frequency
adlertz@19330 1645 PhaseBlockLayout::PhaseBlockLayout(PhaseCFG &cfg)
adlertz@19330 1646 : Phase(BlockLayout)
adlertz@19330 1647 , _cfg(cfg) {
rasbold@1498 1648 ResourceMark rm;
rasbold@1498 1649 ResourceArea *area = Thread::current()->resource_area();
rasbold@1498 1650
rasbold@1498 1651 // List of traces
adlertz@19330 1652 int size = _cfg.number_of_blocks() + 1;
rasbold@1498 1653 traces = NEW_ARENA_ARRAY(area, Trace *, size);
rasbold@1498 1654 memset(traces, 0, size*sizeof(Trace*));
rasbold@1498 1655 next = NEW_ARENA_ARRAY(area, Block *, size);
rasbold@1498 1656 memset(next, 0, size*sizeof(Block *));
rasbold@1498 1657 prev = NEW_ARENA_ARRAY(area, Block *, size);
rasbold@1498 1658 memset(prev , 0, size*sizeof(Block *));
rasbold@1498 1659
rasbold@1498 1660 // List of edges
rasbold@1498 1661 edges = new GrowableArray<CFGEdge*>;
rasbold@1498 1662
rasbold@1498 1663 // Mapping block index --> block_trace
rasbold@1498 1664 uf = new UnionFind(size);
rasbold@1498 1665 uf->reset(size);
rasbold@1498 1666
rasbold@1498 1667 // Find edges and create traces.
rasbold@1498 1668 find_edges();
rasbold@1498 1669
rasbold@1498 1670 // Grow traces at their ends via most frequent edges.
rasbold@1498 1671 grow_traces();
rasbold@1498 1672
rasbold@1498 1673 // Merge one trace into another, but only at fall-through points.
rasbold@1498 1674 // This may make diamonds and other related shapes in a trace.
rasbold@1498 1675 merge_traces(true);
rasbold@1498 1676
rasbold@1498 1677 // Run merge again, allowing two traces to be catenated, even if
rasbold@1498 1678 // one does not fall through into the other. This appends loosely
rasbold@1498 1679 // related traces to be near each other.
rasbold@1498 1680 merge_traces(false);
rasbold@1498 1681
rasbold@1498 1682 // Re-order all the remaining traces by frequency
rasbold@1498 1683 reorder_traces(size);
rasbold@1498 1684
adlertz@19330 1685 assert(_cfg.number_of_blocks() >= (uint) (size - 1), "number of blocks can not shrink");
rasbold@1498 1686 }
rasbold@1498 1687
rasbold@1498 1688
rasbold@1498 1689 // Edge e completes a loop in a trace. If the target block is head of the
rasbold@1498 1690 // loop, rotate the loop block so that the loop ends in a conditional branch.
rasbold@1498 1691 bool Trace::backedge(CFGEdge *e) {
rasbold@1498 1692 bool loop_rotated = false;
rasbold@1498 1693 Block *src_block = e->from();
rasbold@1498 1694 Block *targ_block = e->to();
rasbold@1498 1695
rasbold@1498 1696 assert(last_block() == src_block, "loop discovery at back branch");
rasbold@1498 1697 if (first_block() == targ_block) {
rasbold@1498 1698 if (BlockLayoutRotateLoops && last_block()->num_fall_throughs() < 2) {
rasbold@1498 1699 // Find the last block in the trace that has a conditional
rasbold@1498 1700 // branch.
rasbold@1498 1701 Block *b;
rasbold@1498 1702 for (b = last_block(); b != NULL; b = prev(b)) {
rasbold@1498 1703 if (b->num_fall_throughs() == 2) {
rasbold@1498 1704 break;
rasbold@1498 1705 }
rasbold@1498 1706 }
rasbold@1498 1707
rasbold@1498 1708 if (b != last_block() && b != NULL) {
rasbold@1498 1709 loop_rotated = true;
rasbold@1498 1710
rasbold@1498 1711 // Rotate the loop by doing two-part linked-list surgery.
rasbold@1498 1712 append(first_block());
rasbold@1498 1713 break_loop_after(b);
rasbold@1498 1714 }
rasbold@1498 1715 }
rasbold@1498 1716
rasbold@1498 1717 // Backbranch to the top of a trace
twisti@2131 1718 // Scroll forward through the trace from the targ_block. If we find
rasbold@1498 1719 // a loop head before another loop top, use the the loop head alignment.
rasbold@1498 1720 for (Block *b = targ_block; b != NULL; b = next(b)) {
rasbold@1498 1721 if (b->has_loop_alignment()) {
rasbold@1498 1722 break;
rasbold@1498 1723 }
rasbold@1498 1724 if (b->head()->is_Loop()) {
rasbold@1498 1725 targ_block = b;
rasbold@1498 1726 break;
rasbold@1498 1727 }
rasbold@1498 1728 }
rasbold@1498 1729
rasbold@1498 1730 first_block()->set_loop_alignment(targ_block);
rasbold@1498 1731
rasbold@1498 1732 } else {
roland@38046 1733 // That loop may already have a loop top (we're reaching it again
roland@38046 1734 // through the backedge of an outer loop)
roland@38046 1735 Block* b = prev(targ_block);
roland@38046 1736 bool has_top = targ_block->head()->is_Loop() && b->has_loop_alignment() && !b->head()->is_Loop();
roland@38046 1737 if (!has_top) {
roland@38046 1738 // Backbranch into the middle of a trace
roland@38046 1739 targ_block->set_loop_alignment(targ_block);
roland@38046 1740 }
rasbold@1498 1741 }
rasbold@1498 1742
rasbold@1498 1743 return loop_rotated;
rasbold@1498 1744 }
rasbold@1498 1745
rasbold@1498 1746 // push blocks onto the CFG list
rasbold@1498 1747 // ensure that blocks have the correct two-way branch sense
rasbold@1498 1748 void Trace::fixup_blocks(PhaseCFG &cfg) {
rasbold@1498 1749 Block *last = last_block();
rasbold@1498 1750 for (Block *b = first_block(); b != NULL; b = next(b)) {
adlertz@19330 1751 cfg.add_block(b);
rasbold@1498 1752 if (!b->is_connector()) {
rasbold@1498 1753 int nfallthru = b->num_fall_throughs();
rasbold@1498 1754 if (b != last) {
rasbold@1498 1755 if (nfallthru == 2) {
rasbold@1498 1756 // Ensure that the sense of the branch is correct
rasbold@1498 1757 Block *bnext = next(b);
rasbold@1498 1758 Block *bs0 = b->non_connector_successor(0);
rasbold@1498 1759
adlertz@19717 1760 MachNode *iff = b->get_node(b->number_of_nodes() - 3)->as_Mach();
adlertz@19717 1761 ProjNode *proj0 = b->get_node(b->number_of_nodes() - 2)->as_Proj();
adlertz@19717 1762 ProjNode *proj1 = b->get_node(b->number_of_nodes() - 1)->as_Proj();
rasbold@1498 1763
rasbold@1498 1764 if (bnext == bs0) {
rasbold@1498 1765 // Fall-thru case in succs[0], should be in succs[1]
rasbold@1498 1766
rasbold@1498 1767 // Flip targets in _succs map
rasbold@1498 1768 Block *tbs0 = b->_succs[0];
rasbold@1498 1769 Block *tbs1 = b->_succs[1];
rasbold@1498 1770 b->_succs.map( 0, tbs1 );
rasbold@1498 1771 b->_succs.map( 1, tbs0 );
rasbold@1498 1772
rasbold@1498 1773 // Flip projections to match targets
adlertz@19717 1774 b->map_node(proj1, b->number_of_nodes() - 2);
adlertz@19717 1775 b->map_node(proj0, b->number_of_nodes() - 1);
rasbold@1498 1776 }
rasbold@1498 1777 }
rasbold@1498 1778 }
rasbold@1498 1779 }
rasbold@1498 1780 }
rasbold@1498 1781 }