src/share/vm/opto/gcm.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 1036
523ded093c31
child 1056
19f25e603e7b
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

duke@435 1 /*
xdono@631 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // Portions of code courtesy of Clifford Click
duke@435 26
duke@435 27 // Optimization - Graph Style
duke@435 28
duke@435 29 #include "incls/_precompiled.incl"
duke@435 30 #include "incls/_gcm.cpp.incl"
duke@435 31
kvn@987 32 // To avoid float value underflow
kvn@987 33 #define MIN_BLOCK_FREQUENCY 1.e-35f
kvn@987 34
duke@435 35 //----------------------------schedule_node_into_block-------------------------
duke@435 36 // Insert node n into block b. Look for projections of n and make sure they
duke@435 37 // are in b also.
duke@435 38 void PhaseCFG::schedule_node_into_block( Node *n, Block *b ) {
duke@435 39 // Set basic block of n, Add n to b,
duke@435 40 _bbs.map(n->_idx, b);
duke@435 41 b->add_inst(n);
duke@435 42
duke@435 43 // After Matching, nearly any old Node may have projections trailing it.
duke@435 44 // These are usually machine-dependent flags. In any case, they might
duke@435 45 // float to another block below this one. Move them up.
duke@435 46 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@435 47 Node* use = n->fast_out(i);
duke@435 48 if (use->is_Proj()) {
duke@435 49 Block* buse = _bbs[use->_idx];
duke@435 50 if (buse != b) { // In wrong block?
duke@435 51 if (buse != NULL)
duke@435 52 buse->find_remove(use); // Remove from wrong block
duke@435 53 _bbs.map(use->_idx, b); // Re-insert in this block
duke@435 54 b->add_inst(use);
duke@435 55 }
duke@435 56 }
duke@435 57 }
duke@435 58 }
duke@435 59
kvn@1036 60 //----------------------------replace_block_proj_ctrl-------------------------
kvn@1036 61 // Nodes that have is_block_proj() nodes as their control need to use
kvn@1036 62 // the appropriate Region for their actual block as their control since
kvn@1036 63 // the projection will be in a predecessor block.
kvn@1036 64 void PhaseCFG::replace_block_proj_ctrl( Node *n ) {
kvn@1036 65 const Node *in0 = n->in(0);
kvn@1036 66 assert(in0 != NULL, "Only control-dependent");
kvn@1036 67 const Node *p = in0->is_block_proj();
kvn@1036 68 if (p != NULL && p != n) { // Control from a block projection?
kvn@1036 69 assert(!n->pinned() || n->is_SafePointScalarObject(), "only SafePointScalarObject pinned node is expected here");
kvn@1036 70 // Find trailing Region
kvn@1036 71 Block *pb = _bbs[in0->_idx]; // Block-projection already has basic block
kvn@1036 72 uint j = 0;
kvn@1036 73 if (pb->_num_succs != 1) { // More then 1 successor?
kvn@1036 74 // Search for successor
kvn@1036 75 uint max = pb->_nodes.size();
kvn@1036 76 assert( max > 1, "" );
kvn@1036 77 uint start = max - pb->_num_succs;
kvn@1036 78 // Find which output path belongs to projection
kvn@1036 79 for (j = start; j < max; j++) {
kvn@1036 80 if( pb->_nodes[j] == in0 )
kvn@1036 81 break;
kvn@1036 82 }
kvn@1036 83 assert( j < max, "must find" );
kvn@1036 84 // Change control to match head of successor basic block
kvn@1036 85 j -= start;
kvn@1036 86 }
kvn@1036 87 n->set_req(0, pb->_succs[j]->head());
kvn@1036 88 }
kvn@1036 89 }
kvn@1036 90
duke@435 91
duke@435 92 //------------------------------schedule_pinned_nodes--------------------------
duke@435 93 // Set the basic block for Nodes pinned into blocks
duke@435 94 void PhaseCFG::schedule_pinned_nodes( VectorSet &visited ) {
duke@435 95 // Allocate node stack of size C->unique()+8 to avoid frequent realloc
duke@435 96 GrowableArray <Node *> spstack(C->unique()+8);
duke@435 97 spstack.push(_root);
duke@435 98 while ( spstack.is_nonempty() ) {
duke@435 99 Node *n = spstack.pop();
duke@435 100 if( !visited.test_set(n->_idx) ) { // Test node and flag it as visited
duke@435 101 if( n->pinned() && !_bbs.lookup(n->_idx) ) { // Pinned? Nail it down!
kvn@1036 102 assert( n->in(0), "pinned Node must have Control" );
kvn@1036 103 // Before setting block replace block_proj control edge
kvn@1036 104 replace_block_proj_ctrl(n);
duke@435 105 Node *input = n->in(0);
duke@435 106 while( !input->is_block_start() )
duke@435 107 input = input->in(0);
duke@435 108 Block *b = _bbs[input->_idx]; // Basic block of controlling input
duke@435 109 schedule_node_into_block(n, b);
duke@435 110 }
duke@435 111 for( int i = n->req() - 1; i >= 0; --i ) { // For all inputs
duke@435 112 if( n->in(i) != NULL )
duke@435 113 spstack.push(n->in(i));
duke@435 114 }
duke@435 115 }
duke@435 116 }
duke@435 117 }
duke@435 118
duke@435 119 #ifdef ASSERT
duke@435 120 // Assert that new input b2 is dominated by all previous inputs.
duke@435 121 // Check this by by seeing that it is dominated by b1, the deepest
duke@435 122 // input observed until b2.
duke@435 123 static void assert_dom(Block* b1, Block* b2, Node* n, Block_Array &bbs) {
duke@435 124 if (b1 == NULL) return;
duke@435 125 assert(b1->_dom_depth < b2->_dom_depth, "sanity");
duke@435 126 Block* tmp = b2;
duke@435 127 while (tmp != b1 && tmp != NULL) {
duke@435 128 tmp = tmp->_idom;
duke@435 129 }
duke@435 130 if (tmp != b1) {
duke@435 131 // Detected an unschedulable graph. Print some nice stuff and die.
duke@435 132 tty->print_cr("!!! Unschedulable graph !!!");
duke@435 133 for (uint j=0; j<n->len(); j++) { // For all inputs
duke@435 134 Node* inn = n->in(j); // Get input
duke@435 135 if (inn == NULL) continue; // Ignore NULL, missing inputs
duke@435 136 Block* inb = bbs[inn->_idx];
duke@435 137 tty->print("B%d idom=B%d depth=%2d ",inb->_pre_order,
duke@435 138 inb->_idom ? inb->_idom->_pre_order : 0, inb->_dom_depth);
duke@435 139 inn->dump();
duke@435 140 }
duke@435 141 tty->print("Failing node: ");
duke@435 142 n->dump();
duke@435 143 assert(false, "unscheduable graph");
duke@435 144 }
duke@435 145 }
duke@435 146 #endif
duke@435 147
duke@435 148 static Block* find_deepest_input(Node* n, Block_Array &bbs) {
duke@435 149 // Find the last input dominated by all other inputs.
duke@435 150 Block* deepb = NULL; // Deepest block so far
duke@435 151 int deepb_dom_depth = 0;
duke@435 152 for (uint k = 0; k < n->len(); k++) { // For all inputs
duke@435 153 Node* inn = n->in(k); // Get input
duke@435 154 if (inn == NULL) continue; // Ignore NULL, missing inputs
duke@435 155 Block* inb = bbs[inn->_idx];
duke@435 156 assert(inb != NULL, "must already have scheduled this input");
duke@435 157 if (deepb_dom_depth < (int) inb->_dom_depth) {
duke@435 158 // The new inb must be dominated by the previous deepb.
duke@435 159 // The various inputs must be linearly ordered in the dom
duke@435 160 // tree, or else there will not be a unique deepest block.
duke@435 161 DEBUG_ONLY(assert_dom(deepb, inb, n, bbs));
duke@435 162 deepb = inb; // Save deepest block
duke@435 163 deepb_dom_depth = deepb->_dom_depth;
duke@435 164 }
duke@435 165 }
duke@435 166 assert(deepb != NULL, "must be at least one input to n");
duke@435 167 return deepb;
duke@435 168 }
duke@435 169
duke@435 170
duke@435 171 //------------------------------schedule_early---------------------------------
duke@435 172 // Find the earliest Block any instruction can be placed in. Some instructions
duke@435 173 // are pinned into Blocks. Unpinned instructions can appear in last block in
duke@435 174 // which all their inputs occur.
duke@435 175 bool PhaseCFG::schedule_early(VectorSet &visited, Node_List &roots) {
duke@435 176 // Allocate stack with enough space to avoid frequent realloc
duke@435 177 Node_Stack nstack(roots.Size() + 8); // (unique >> 1) + 24 from Java2D stats
duke@435 178 // roots.push(_root); _root will be processed among C->top() inputs
duke@435 179 roots.push(C->top());
duke@435 180 visited.set(C->top()->_idx);
duke@435 181
duke@435 182 while (roots.size() != 0) {
duke@435 183 // Use local variables nstack_top_n & nstack_top_i to cache values
duke@435 184 // on stack's top.
duke@435 185 Node *nstack_top_n = roots.pop();
duke@435 186 uint nstack_top_i = 0;
duke@435 187 //while_nstack_nonempty:
duke@435 188 while (true) {
duke@435 189 // Get parent node and next input's index from stack's top.
duke@435 190 Node *n = nstack_top_n;
duke@435 191 uint i = nstack_top_i;
duke@435 192
duke@435 193 if (i == 0) {
kvn@1036 194 // Fixup some control. Constants without control get attached
kvn@1036 195 // to root and nodes that use is_block_proj() nodes should be attached
kvn@1036 196 // to the region that starts their block.
duke@435 197 const Node *in0 = n->in(0);
duke@435 198 if (in0 != NULL) { // Control-dependent?
kvn@1036 199 replace_block_proj_ctrl(n);
duke@435 200 } else { // n->in(0) == NULL
duke@435 201 if (n->req() == 1) { // This guy is a constant with NO inputs?
duke@435 202 n->set_req(0, _root);
duke@435 203 }
duke@435 204 }
duke@435 205 }
duke@435 206
duke@435 207 // First, visit all inputs and force them to get a block. If an
duke@435 208 // input is already in a block we quit following inputs (to avoid
duke@435 209 // cycles). Instead we put that Node on a worklist to be handled
duke@435 210 // later (since IT'S inputs may not have a block yet).
duke@435 211 bool done = true; // Assume all n's inputs will be processed
duke@435 212 while (i < n->len()) { // For all inputs
duke@435 213 Node *in = n->in(i); // Get input
duke@435 214 ++i;
duke@435 215 if (in == NULL) continue; // Ignore NULL, missing inputs
duke@435 216 int is_visited = visited.test_set(in->_idx);
duke@435 217 if (!_bbs.lookup(in->_idx)) { // Missing block selection?
duke@435 218 if (is_visited) {
duke@435 219 // assert( !visited.test(in->_idx), "did not schedule early" );
duke@435 220 return false;
duke@435 221 }
duke@435 222 nstack.push(n, i); // Save parent node and next input's index.
duke@435 223 nstack_top_n = in; // Process current input now.
duke@435 224 nstack_top_i = 0;
duke@435 225 done = false; // Not all n's inputs processed.
duke@435 226 break; // continue while_nstack_nonempty;
duke@435 227 } else if (!is_visited) { // Input not yet visited?
duke@435 228 roots.push(in); // Visit this guy later, using worklist
duke@435 229 }
duke@435 230 }
duke@435 231 if (done) {
duke@435 232 // All of n's inputs have been processed, complete post-processing.
duke@435 233
duke@435 234 // Some instructions are pinned into a block. These include Region,
duke@435 235 // Phi, Start, Return, and other control-dependent instructions and
duke@435 236 // any projections which depend on them.
duke@435 237 if (!n->pinned()) {
duke@435 238 // Set earliest legal block.
duke@435 239 _bbs.map(n->_idx, find_deepest_input(n, _bbs));
kvn@1036 240 } else {
kvn@1036 241 assert(_bbs[n->_idx] == _bbs[n->in(0)->_idx], "Pinned Node should be at the same block as its control edge");
duke@435 242 }
duke@435 243
duke@435 244 if (nstack.is_empty()) {
duke@435 245 // Finished all nodes on stack.
duke@435 246 // Process next node on the worklist 'roots'.
duke@435 247 break;
duke@435 248 }
duke@435 249 // Get saved parent node and next input's index.
duke@435 250 nstack_top_n = nstack.node();
duke@435 251 nstack_top_i = nstack.index();
duke@435 252 nstack.pop();
duke@435 253 } // if (done)
duke@435 254 } // while (true)
duke@435 255 } // while (roots.size() != 0)
duke@435 256 return true;
duke@435 257 }
duke@435 258
duke@435 259 //------------------------------dom_lca----------------------------------------
duke@435 260 // Find least common ancestor in dominator tree
duke@435 261 // LCA is a current notion of LCA, to be raised above 'this'.
duke@435 262 // As a convenient boundary condition, return 'this' if LCA is NULL.
duke@435 263 // Find the LCA of those two nodes.
duke@435 264 Block* Block::dom_lca(Block* LCA) {
duke@435 265 if (LCA == NULL || LCA == this) return this;
duke@435 266
duke@435 267 Block* anc = this;
duke@435 268 while (anc->_dom_depth > LCA->_dom_depth)
duke@435 269 anc = anc->_idom; // Walk up till anc is as high as LCA
duke@435 270
duke@435 271 while (LCA->_dom_depth > anc->_dom_depth)
duke@435 272 LCA = LCA->_idom; // Walk up till LCA is as high as anc
duke@435 273
duke@435 274 while (LCA != anc) { // Walk both up till they are the same
duke@435 275 LCA = LCA->_idom;
duke@435 276 anc = anc->_idom;
duke@435 277 }
duke@435 278
duke@435 279 return LCA;
duke@435 280 }
duke@435 281
duke@435 282 //--------------------------raise_LCA_above_use--------------------------------
duke@435 283 // We are placing a definition, and have been given a def->use edge.
duke@435 284 // The definition must dominate the use, so move the LCA upward in the
duke@435 285 // dominator tree to dominate the use. If the use is a phi, adjust
duke@435 286 // the LCA only with the phi input paths which actually use this def.
duke@435 287 static Block* raise_LCA_above_use(Block* LCA, Node* use, Node* def, Block_Array &bbs) {
duke@435 288 Block* buse = bbs[use->_idx];
duke@435 289 if (buse == NULL) return LCA; // Unused killing Projs have no use block
duke@435 290 if (!use->is_Phi()) return buse->dom_lca(LCA);
duke@435 291 uint pmax = use->req(); // Number of Phi inputs
duke@435 292 // Why does not this loop just break after finding the matching input to
duke@435 293 // the Phi? Well...it's like this. I do not have true def-use/use-def
duke@435 294 // chains. Means I cannot distinguish, from the def-use direction, which
duke@435 295 // of many use-defs lead from the same use to the same def. That is, this
duke@435 296 // Phi might have several uses of the same def. Each use appears in a
duke@435 297 // different predecessor block. But when I enter here, I cannot distinguish
duke@435 298 // which use-def edge I should find the predecessor block for. So I find
duke@435 299 // them all. Means I do a little extra work if a Phi uses the same value
duke@435 300 // more than once.
duke@435 301 for (uint j=1; j<pmax; j++) { // For all inputs
duke@435 302 if (use->in(j) == def) { // Found matching input?
duke@435 303 Block* pred = bbs[buse->pred(j)->_idx];
duke@435 304 LCA = pred->dom_lca(LCA);
duke@435 305 }
duke@435 306 }
duke@435 307 return LCA;
duke@435 308 }
duke@435 309
duke@435 310 //----------------------------raise_LCA_above_marks----------------------------
duke@435 311 // Return a new LCA that dominates LCA and any of its marked predecessors.
duke@435 312 // Search all my parents up to 'early' (exclusive), looking for predecessors
duke@435 313 // which are marked with the given index. Return the LCA (in the dom tree)
duke@435 314 // of all marked blocks. If there are none marked, return the original
duke@435 315 // LCA.
duke@435 316 static Block* raise_LCA_above_marks(Block* LCA, node_idx_t mark,
duke@435 317 Block* early, Block_Array &bbs) {
duke@435 318 Block_List worklist;
duke@435 319 worklist.push(LCA);
duke@435 320 while (worklist.size() > 0) {
duke@435 321 Block* mid = worklist.pop();
duke@435 322 if (mid == early) continue; // stop searching here
duke@435 323
duke@435 324 // Test and set the visited bit.
duke@435 325 if (mid->raise_LCA_visited() == mark) continue; // already visited
duke@435 326
duke@435 327 // Don't process the current LCA, otherwise the search may terminate early
duke@435 328 if (mid != LCA && mid->raise_LCA_mark() == mark) {
duke@435 329 // Raise the LCA.
duke@435 330 LCA = mid->dom_lca(LCA);
duke@435 331 if (LCA == early) break; // stop searching everywhere
duke@435 332 assert(early->dominates(LCA), "early is high enough");
duke@435 333 // Resume searching at that point, skipping intermediate levels.
duke@435 334 worklist.push(LCA);
kvn@650 335 if (LCA == mid)
kvn@650 336 continue; // Don't mark as visited to avoid early termination.
duke@435 337 } else {
duke@435 338 // Keep searching through this block's predecessors.
duke@435 339 for (uint j = 1, jmax = mid->num_preds(); j < jmax; j++) {
duke@435 340 Block* mid_parent = bbs[ mid->pred(j)->_idx ];
duke@435 341 worklist.push(mid_parent);
duke@435 342 }
duke@435 343 }
kvn@650 344 mid->set_raise_LCA_visited(mark);
duke@435 345 }
duke@435 346 return LCA;
duke@435 347 }
duke@435 348
duke@435 349 //--------------------------memory_early_block--------------------------------
duke@435 350 // This is a variation of find_deepest_input, the heart of schedule_early.
duke@435 351 // Find the "early" block for a load, if we considered only memory and
duke@435 352 // address inputs, that is, if other data inputs were ignored.
duke@435 353 //
duke@435 354 // Because a subset of edges are considered, the resulting block will
duke@435 355 // be earlier (at a shallower dom_depth) than the true schedule_early
duke@435 356 // point of the node. We compute this earlier block as a more permissive
duke@435 357 // site for anti-dependency insertion, but only if subsume_loads is enabled.
duke@435 358 static Block* memory_early_block(Node* load, Block* early, Block_Array &bbs) {
duke@435 359 Node* base;
duke@435 360 Node* index;
duke@435 361 Node* store = load->in(MemNode::Memory);
duke@435 362 load->as_Mach()->memory_inputs(base, index);
duke@435 363
duke@435 364 assert(base != NodeSentinel && index != NodeSentinel,
duke@435 365 "unexpected base/index inputs");
duke@435 366
duke@435 367 Node* mem_inputs[4];
duke@435 368 int mem_inputs_length = 0;
duke@435 369 if (base != NULL) mem_inputs[mem_inputs_length++] = base;
duke@435 370 if (index != NULL) mem_inputs[mem_inputs_length++] = index;
duke@435 371 if (store != NULL) mem_inputs[mem_inputs_length++] = store;
duke@435 372
duke@435 373 // In the comparision below, add one to account for the control input,
duke@435 374 // which may be null, but always takes up a spot in the in array.
duke@435 375 if (mem_inputs_length + 1 < (int) load->req()) {
duke@435 376 // This "load" has more inputs than just the memory, base and index inputs.
duke@435 377 // For purposes of checking anti-dependences, we need to start
duke@435 378 // from the early block of only the address portion of the instruction,
duke@435 379 // and ignore other blocks that may have factored into the wider
duke@435 380 // schedule_early calculation.
duke@435 381 if (load->in(0) != NULL) mem_inputs[mem_inputs_length++] = load->in(0);
duke@435 382
duke@435 383 Block* deepb = NULL; // Deepest block so far
duke@435 384 int deepb_dom_depth = 0;
duke@435 385 for (int i = 0; i < mem_inputs_length; i++) {
duke@435 386 Block* inb = bbs[mem_inputs[i]->_idx];
duke@435 387 if (deepb_dom_depth < (int) inb->_dom_depth) {
duke@435 388 // The new inb must be dominated by the previous deepb.
duke@435 389 // The various inputs must be linearly ordered in the dom
duke@435 390 // tree, or else there will not be a unique deepest block.
duke@435 391 DEBUG_ONLY(assert_dom(deepb, inb, load, bbs));
duke@435 392 deepb = inb; // Save deepest block
duke@435 393 deepb_dom_depth = deepb->_dom_depth;
duke@435 394 }
duke@435 395 }
duke@435 396 early = deepb;
duke@435 397 }
duke@435 398
duke@435 399 return early;
duke@435 400 }
duke@435 401
duke@435 402 //--------------------------insert_anti_dependences---------------------------
duke@435 403 // A load may need to witness memory that nearby stores can overwrite.
duke@435 404 // For each nearby store, either insert an "anti-dependence" edge
duke@435 405 // from the load to the store, or else move LCA upward to force the
duke@435 406 // load to (eventually) be scheduled in a block above the store.
duke@435 407 //
duke@435 408 // Do not add edges to stores on distinct control-flow paths;
duke@435 409 // only add edges to stores which might interfere.
duke@435 410 //
duke@435 411 // Return the (updated) LCA. There will not be any possibly interfering
duke@435 412 // store between the load's "early block" and the updated LCA.
duke@435 413 // Any stores in the updated LCA will have new precedence edges
duke@435 414 // back to the load. The caller is expected to schedule the load
duke@435 415 // in the LCA, in which case the precedence edges will make LCM
duke@435 416 // preserve anti-dependences. The caller may also hoist the load
duke@435 417 // above the LCA, if it is not the early block.
duke@435 418 Block* PhaseCFG::insert_anti_dependences(Block* LCA, Node* load, bool verify) {
duke@435 419 assert(load->needs_anti_dependence_check(), "must be a load of some sort");
duke@435 420 assert(LCA != NULL, "");
duke@435 421 DEBUG_ONLY(Block* LCA_orig = LCA);
duke@435 422
duke@435 423 // Compute the alias index. Loads and stores with different alias indices
duke@435 424 // do not need anti-dependence edges.
duke@435 425 uint load_alias_idx = C->get_alias_index(load->adr_type());
duke@435 426 #ifdef ASSERT
duke@435 427 if (load_alias_idx == Compile::AliasIdxBot && C->AliasLevel() > 0 &&
duke@435 428 (PrintOpto || VerifyAliases ||
duke@435 429 PrintMiscellaneous && (WizardMode || Verbose))) {
duke@435 430 // Load nodes should not consume all of memory.
duke@435 431 // Reporting a bottom type indicates a bug in adlc.
duke@435 432 // If some particular type of node validly consumes all of memory,
duke@435 433 // sharpen the preceding "if" to exclude it, so we can catch bugs here.
duke@435 434 tty->print_cr("*** Possible Anti-Dependence Bug: Load consumes all of memory.");
duke@435 435 load->dump(2);
duke@435 436 if (VerifyAliases) assert(load_alias_idx != Compile::AliasIdxBot, "");
duke@435 437 }
duke@435 438 #endif
duke@435 439 assert(load_alias_idx || (load->is_Mach() && load->as_Mach()->ideal_Opcode() == Op_StrComp),
duke@435 440 "String compare is only known 'load' that does not conflict with any stores");
duke@435 441
duke@435 442 if (!C->alias_type(load_alias_idx)->is_rewritable()) {
duke@435 443 // It is impossible to spoil this load by putting stores before it,
duke@435 444 // because we know that the stores will never update the value
duke@435 445 // which 'load' must witness.
duke@435 446 return LCA;
duke@435 447 }
duke@435 448
duke@435 449 node_idx_t load_index = load->_idx;
duke@435 450
duke@435 451 // Note the earliest legal placement of 'load', as determined by
duke@435 452 // by the unique point in the dom tree where all memory effects
duke@435 453 // and other inputs are first available. (Computed by schedule_early.)
duke@435 454 // For normal loads, 'early' is the shallowest place (dom graph wise)
duke@435 455 // to look for anti-deps between this load and any store.
duke@435 456 Block* early = _bbs[load_index];
duke@435 457
duke@435 458 // If we are subsuming loads, compute an "early" block that only considers
duke@435 459 // memory or address inputs. This block may be different than the
duke@435 460 // schedule_early block in that it could be at an even shallower depth in the
duke@435 461 // dominator tree, and allow for a broader discovery of anti-dependences.
duke@435 462 if (C->subsume_loads()) {
duke@435 463 early = memory_early_block(load, early, _bbs);
duke@435 464 }
duke@435 465
duke@435 466 ResourceArea *area = Thread::current()->resource_area();
duke@435 467 Node_List worklist_mem(area); // prior memory state to store
duke@435 468 Node_List worklist_store(area); // possible-def to explore
kvn@466 469 Node_List worklist_visited(area); // visited mergemem nodes
duke@435 470 Node_List non_early_stores(area); // all relevant stores outside of early
duke@435 471 bool must_raise_LCA = false;
duke@435 472
duke@435 473 #ifdef TRACK_PHI_INPUTS
duke@435 474 // %%% This extra checking fails because MergeMem nodes are not GVNed.
duke@435 475 // Provide "phi_inputs" to check if every input to a PhiNode is from the
duke@435 476 // original memory state. This indicates a PhiNode for which should not
duke@435 477 // prevent the load from sinking. For such a block, set_raise_LCA_mark
duke@435 478 // may be overly conservative.
duke@435 479 // Mechanism: count inputs seen for each Phi encountered in worklist_store.
duke@435 480 DEBUG_ONLY(GrowableArray<uint> phi_inputs(area, C->unique(),0,0));
duke@435 481 #endif
duke@435 482
duke@435 483 // 'load' uses some memory state; look for users of the same state.
duke@435 484 // Recurse through MergeMem nodes to the stores that use them.
duke@435 485
duke@435 486 // Each of these stores is a possible definition of memory
duke@435 487 // that 'load' needs to use. We need to force 'load'
duke@435 488 // to occur before each such store. When the store is in
duke@435 489 // the same block as 'load', we insert an anti-dependence
duke@435 490 // edge load->store.
duke@435 491
duke@435 492 // The relevant stores "nearby" the load consist of a tree rooted
duke@435 493 // at initial_mem, with internal nodes of type MergeMem.
duke@435 494 // Therefore, the branches visited by the worklist are of this form:
duke@435 495 // initial_mem -> (MergeMem ->)* store
duke@435 496 // The anti-dependence constraints apply only to the fringe of this tree.
duke@435 497
duke@435 498 Node* initial_mem = load->in(MemNode::Memory);
duke@435 499 worklist_store.push(initial_mem);
kvn@466 500 worklist_visited.push(initial_mem);
duke@435 501 worklist_mem.push(NULL);
duke@435 502 while (worklist_store.size() > 0) {
duke@435 503 // Examine a nearby store to see if it might interfere with our load.
duke@435 504 Node* mem = worklist_mem.pop();
duke@435 505 Node* store = worklist_store.pop();
duke@435 506 uint op = store->Opcode();
duke@435 507
duke@435 508 // MergeMems do not directly have anti-deps.
duke@435 509 // Treat them as internal nodes in a forward tree of memory states,
duke@435 510 // the leaves of which are each a 'possible-def'.
duke@435 511 if (store == initial_mem // root (exclusive) of tree we are searching
duke@435 512 || op == Op_MergeMem // internal node of tree we are searching
duke@435 513 ) {
duke@435 514 mem = store; // It's not a possibly interfering store.
kvn@466 515 if (store == initial_mem)
kvn@466 516 initial_mem = NULL; // only process initial memory once
kvn@466 517
duke@435 518 for (DUIterator_Fast imax, i = mem->fast_outs(imax); i < imax; i++) {
duke@435 519 store = mem->fast_out(i);
duke@435 520 if (store->is_MergeMem()) {
duke@435 521 // Be sure we don't get into combinatorial problems.
duke@435 522 // (Allow phis to be repeated; they can merge two relevant states.)
kvn@466 523 uint j = worklist_visited.size();
kvn@466 524 for (; j > 0; j--) {
kvn@466 525 if (worklist_visited.at(j-1) == store) break;
duke@435 526 }
kvn@466 527 if (j > 0) continue; // already on work list; do not repeat
kvn@466 528 worklist_visited.push(store);
duke@435 529 }
duke@435 530 worklist_mem.push(mem);
duke@435 531 worklist_store.push(store);
duke@435 532 }
duke@435 533 continue;
duke@435 534 }
duke@435 535
duke@435 536 if (op == Op_MachProj || op == Op_Catch) continue;
duke@435 537 if (store->needs_anti_dependence_check()) continue; // not really a store
duke@435 538
duke@435 539 // Compute the alias index. Loads and stores with different alias
duke@435 540 // indices do not need anti-dependence edges. Wide MemBar's are
duke@435 541 // anti-dependent on everything (except immutable memories).
duke@435 542 const TypePtr* adr_type = store->adr_type();
duke@435 543 if (!C->can_alias(adr_type, load_alias_idx)) continue;
duke@435 544
duke@435 545 // Most slow-path runtime calls do NOT modify Java memory, but
duke@435 546 // they can block and so write Raw memory.
duke@435 547 if (store->is_Mach()) {
duke@435 548 MachNode* mstore = store->as_Mach();
duke@435 549 if (load_alias_idx != Compile::AliasIdxRaw) {
duke@435 550 // Check for call into the runtime using the Java calling
duke@435 551 // convention (and from there into a wrapper); it has no
duke@435 552 // _method. Can't do this optimization for Native calls because
duke@435 553 // they CAN write to Java memory.
duke@435 554 if (mstore->ideal_Opcode() == Op_CallStaticJava) {
duke@435 555 assert(mstore->is_MachSafePoint(), "");
duke@435 556 MachSafePointNode* ms = (MachSafePointNode*) mstore;
duke@435 557 assert(ms->is_MachCallJava(), "");
duke@435 558 MachCallJavaNode* mcj = (MachCallJavaNode*) ms;
duke@435 559 if (mcj->_method == NULL) {
duke@435 560 // These runtime calls do not write to Java visible memory
duke@435 561 // (other than Raw) and so do not require anti-dependence edges.
duke@435 562 continue;
duke@435 563 }
duke@435 564 }
duke@435 565 // Same for SafePoints: they read/write Raw but only read otherwise.
duke@435 566 // This is basically a workaround for SafePoints only defining control
duke@435 567 // instead of control + memory.
duke@435 568 if (mstore->ideal_Opcode() == Op_SafePoint)
duke@435 569 continue;
duke@435 570 } else {
duke@435 571 // Some raw memory, such as the load of "top" at an allocation,
duke@435 572 // can be control dependent on the previous safepoint. See
duke@435 573 // comments in GraphKit::allocate_heap() about control input.
duke@435 574 // Inserting an anti-dep between such a safepoint and a use
duke@435 575 // creates a cycle, and will cause a subsequent failure in
duke@435 576 // local scheduling. (BugId 4919904)
duke@435 577 // (%%% How can a control input be a safepoint and not a projection??)
duke@435 578 if (mstore->ideal_Opcode() == Op_SafePoint && load->in(0) == mstore)
duke@435 579 continue;
duke@435 580 }
duke@435 581 }
duke@435 582
duke@435 583 // Identify a block that the current load must be above,
duke@435 584 // or else observe that 'store' is all the way up in the
duke@435 585 // earliest legal block for 'load'. In the latter case,
duke@435 586 // immediately insert an anti-dependence edge.
duke@435 587 Block* store_block = _bbs[store->_idx];
duke@435 588 assert(store_block != NULL, "unused killing projections skipped above");
duke@435 589
duke@435 590 if (store->is_Phi()) {
duke@435 591 // 'load' uses memory which is one (or more) of the Phi's inputs.
duke@435 592 // It must be scheduled not before the Phi, but rather before
duke@435 593 // each of the relevant Phi inputs.
duke@435 594 //
duke@435 595 // Instead of finding the LCA of all inputs to a Phi that match 'mem',
duke@435 596 // we mark each corresponding predecessor block and do a combined
duke@435 597 // hoisting operation later (raise_LCA_above_marks).
duke@435 598 //
duke@435 599 // Do not assert(store_block != early, "Phi merging memory after access")
duke@435 600 // PhiNode may be at start of block 'early' with backedge to 'early'
duke@435 601 DEBUG_ONLY(bool found_match = false);
duke@435 602 for (uint j = PhiNode::Input, jmax = store->req(); j < jmax; j++) {
duke@435 603 if (store->in(j) == mem) { // Found matching input?
duke@435 604 DEBUG_ONLY(found_match = true);
duke@435 605 Block* pred_block = _bbs[store_block->pred(j)->_idx];
duke@435 606 if (pred_block != early) {
duke@435 607 // If any predecessor of the Phi matches the load's "early block",
duke@435 608 // we do not need a precedence edge between the Phi and 'load'
twisti@1040 609 // since the load will be forced into a block preceding the Phi.
duke@435 610 pred_block->set_raise_LCA_mark(load_index);
duke@435 611 assert(!LCA_orig->dominates(pred_block) ||
duke@435 612 early->dominates(pred_block), "early is high enough");
duke@435 613 must_raise_LCA = true;
duke@435 614 }
duke@435 615 }
duke@435 616 }
duke@435 617 assert(found_match, "no worklist bug");
duke@435 618 #ifdef TRACK_PHI_INPUTS
duke@435 619 #ifdef ASSERT
duke@435 620 // This assert asks about correct handling of PhiNodes, which may not
duke@435 621 // have all input edges directly from 'mem'. See BugId 4621264
duke@435 622 int num_mem_inputs = phi_inputs.at_grow(store->_idx,0) + 1;
duke@435 623 // Increment by exactly one even if there are multiple copies of 'mem'
duke@435 624 // coming into the phi, because we will run this block several times
duke@435 625 // if there are several copies of 'mem'. (That's how DU iterators work.)
duke@435 626 phi_inputs.at_put(store->_idx, num_mem_inputs);
duke@435 627 assert(PhiNode::Input + num_mem_inputs < store->req(),
duke@435 628 "Expect at least one phi input will not be from original memory state");
duke@435 629 #endif //ASSERT
duke@435 630 #endif //TRACK_PHI_INPUTS
duke@435 631 } else if (store_block != early) {
duke@435 632 // 'store' is between the current LCA and earliest possible block.
duke@435 633 // Label its block, and decide later on how to raise the LCA
duke@435 634 // to include the effect on LCA of this store.
duke@435 635 // If this store's block gets chosen as the raised LCA, we
duke@435 636 // will find him on the non_early_stores list and stick him
duke@435 637 // with a precedence edge.
duke@435 638 // (But, don't bother if LCA is already raised all the way.)
duke@435 639 if (LCA != early) {
duke@435 640 store_block->set_raise_LCA_mark(load_index);
duke@435 641 must_raise_LCA = true;
duke@435 642 non_early_stores.push(store);
duke@435 643 }
duke@435 644 } else {
duke@435 645 // Found a possibly-interfering store in the load's 'early' block.
duke@435 646 // This means 'load' cannot sink at all in the dominator tree.
duke@435 647 // Add an anti-dep edge, and squeeze 'load' into the highest block.
duke@435 648 assert(store != load->in(0), "dependence cycle found");
duke@435 649 if (verify) {
duke@435 650 assert(store->find_edge(load) != -1, "missing precedence edge");
duke@435 651 } else {
duke@435 652 store->add_prec(load);
duke@435 653 }
duke@435 654 LCA = early;
duke@435 655 // This turns off the process of gathering non_early_stores.
duke@435 656 }
duke@435 657 }
duke@435 658 // (Worklist is now empty; all nearby stores have been visited.)
duke@435 659
duke@435 660 // Finished if 'load' must be scheduled in its 'early' block.
duke@435 661 // If we found any stores there, they have already been given
duke@435 662 // precedence edges.
duke@435 663 if (LCA == early) return LCA;
duke@435 664
duke@435 665 // We get here only if there are no possibly-interfering stores
duke@435 666 // in the load's 'early' block. Move LCA up above all predecessors
duke@435 667 // which contain stores we have noted.
duke@435 668 //
duke@435 669 // The raised LCA block can be a home to such interfering stores,
duke@435 670 // but its predecessors must not contain any such stores.
duke@435 671 //
duke@435 672 // The raised LCA will be a lower bound for placing the load,
duke@435 673 // preventing the load from sinking past any block containing
duke@435 674 // a store that may invalidate the memory state required by 'load'.
duke@435 675 if (must_raise_LCA)
duke@435 676 LCA = raise_LCA_above_marks(LCA, load->_idx, early, _bbs);
duke@435 677 if (LCA == early) return LCA;
duke@435 678
duke@435 679 // Insert anti-dependence edges from 'load' to each store
duke@435 680 // in the non-early LCA block.
duke@435 681 // Mine the non_early_stores list for such stores.
duke@435 682 if (LCA->raise_LCA_mark() == load_index) {
duke@435 683 while (non_early_stores.size() > 0) {
duke@435 684 Node* store = non_early_stores.pop();
duke@435 685 Block* store_block = _bbs[store->_idx];
duke@435 686 if (store_block == LCA) {
duke@435 687 // add anti_dependence from store to load in its own block
duke@435 688 assert(store != load->in(0), "dependence cycle found");
duke@435 689 if (verify) {
duke@435 690 assert(store->find_edge(load) != -1, "missing precedence edge");
duke@435 691 } else {
duke@435 692 store->add_prec(load);
duke@435 693 }
duke@435 694 } else {
duke@435 695 assert(store_block->raise_LCA_mark() == load_index, "block was marked");
duke@435 696 // Any other stores we found must be either inside the new LCA
duke@435 697 // or else outside the original LCA. In the latter case, they
duke@435 698 // did not interfere with any use of 'load'.
duke@435 699 assert(LCA->dominates(store_block)
duke@435 700 || !LCA_orig->dominates(store_block), "no stray stores");
duke@435 701 }
duke@435 702 }
duke@435 703 }
duke@435 704
duke@435 705 // Return the highest block containing stores; any stores
duke@435 706 // within that block have been given anti-dependence edges.
duke@435 707 return LCA;
duke@435 708 }
duke@435 709
duke@435 710 // This class is used to iterate backwards over the nodes in the graph.
duke@435 711
duke@435 712 class Node_Backward_Iterator {
duke@435 713
duke@435 714 private:
duke@435 715 Node_Backward_Iterator();
duke@435 716
duke@435 717 public:
duke@435 718 // Constructor for the iterator
duke@435 719 Node_Backward_Iterator(Node *root, VectorSet &visited, Node_List &stack, Block_Array &bbs);
duke@435 720
duke@435 721 // Postincrement operator to iterate over the nodes
duke@435 722 Node *next();
duke@435 723
duke@435 724 private:
duke@435 725 VectorSet &_visited;
duke@435 726 Node_List &_stack;
duke@435 727 Block_Array &_bbs;
duke@435 728 };
duke@435 729
duke@435 730 // Constructor for the Node_Backward_Iterator
duke@435 731 Node_Backward_Iterator::Node_Backward_Iterator( Node *root, VectorSet &visited, Node_List &stack, Block_Array &bbs )
duke@435 732 : _visited(visited), _stack(stack), _bbs(bbs) {
duke@435 733 // The stack should contain exactly the root
duke@435 734 stack.clear();
duke@435 735 stack.push(root);
duke@435 736
duke@435 737 // Clear the visited bits
duke@435 738 visited.Clear();
duke@435 739 }
duke@435 740
duke@435 741 // Iterator for the Node_Backward_Iterator
duke@435 742 Node *Node_Backward_Iterator::next() {
duke@435 743
duke@435 744 // If the _stack is empty, then just return NULL: finished.
duke@435 745 if ( !_stack.size() )
duke@435 746 return NULL;
duke@435 747
duke@435 748 // '_stack' is emulating a real _stack. The 'visit-all-users' loop has been
duke@435 749 // made stateless, so I do not need to record the index 'i' on my _stack.
duke@435 750 // Instead I visit all users each time, scanning for unvisited users.
duke@435 751 // I visit unvisited not-anti-dependence users first, then anti-dependent
duke@435 752 // children next.
duke@435 753 Node *self = _stack.pop();
duke@435 754
duke@435 755 // I cycle here when I am entering a deeper level of recursion.
duke@435 756 // The key variable 'self' was set prior to jumping here.
duke@435 757 while( 1 ) {
duke@435 758
duke@435 759 _visited.set(self->_idx);
duke@435 760
duke@435 761 // Now schedule all uses as late as possible.
duke@435 762 uint src = self->is_Proj() ? self->in(0)->_idx : self->_idx;
duke@435 763 uint src_rpo = _bbs[src]->_rpo;
duke@435 764
duke@435 765 // Schedule all nodes in a post-order visit
duke@435 766 Node *unvisited = NULL; // Unvisited anti-dependent Node, if any
duke@435 767
duke@435 768 // Scan for unvisited nodes
duke@435 769 for (DUIterator_Fast imax, i = self->fast_outs(imax); i < imax; i++) {
duke@435 770 // For all uses, schedule late
duke@435 771 Node* n = self->fast_out(i); // Use
duke@435 772
duke@435 773 // Skip already visited children
duke@435 774 if ( _visited.test(n->_idx) )
duke@435 775 continue;
duke@435 776
duke@435 777 // do not traverse backward control edges
duke@435 778 Node *use = n->is_Proj() ? n->in(0) : n;
duke@435 779 uint use_rpo = _bbs[use->_idx]->_rpo;
duke@435 780
duke@435 781 if ( use_rpo < src_rpo )
duke@435 782 continue;
duke@435 783
duke@435 784 // Phi nodes always precede uses in a basic block
duke@435 785 if ( use_rpo == src_rpo && use->is_Phi() )
duke@435 786 continue;
duke@435 787
duke@435 788 unvisited = n; // Found unvisited
duke@435 789
duke@435 790 // Check for possible-anti-dependent
duke@435 791 if( !n->needs_anti_dependence_check() )
duke@435 792 break; // Not visited, not anti-dep; schedule it NOW
duke@435 793 }
duke@435 794
duke@435 795 // Did I find an unvisited not-anti-dependent Node?
duke@435 796 if ( !unvisited )
duke@435 797 break; // All done with children; post-visit 'self'
duke@435 798
duke@435 799 // Visit the unvisited Node. Contains the obvious push to
duke@435 800 // indicate I'm entering a deeper level of recursion. I push the
duke@435 801 // old state onto the _stack and set a new state and loop (recurse).
duke@435 802 _stack.push(self);
duke@435 803 self = unvisited;
duke@435 804 } // End recursion loop
duke@435 805
duke@435 806 return self;
duke@435 807 }
duke@435 808
duke@435 809 //------------------------------ComputeLatenciesBackwards----------------------
duke@435 810 // Compute the latency of all the instructions.
duke@435 811 void PhaseCFG::ComputeLatenciesBackwards(VectorSet &visited, Node_List &stack) {
duke@435 812 #ifndef PRODUCT
duke@435 813 if (trace_opto_pipelining())
duke@435 814 tty->print("\n#---- ComputeLatenciesBackwards ----\n");
duke@435 815 #endif
duke@435 816
duke@435 817 Node_Backward_Iterator iter((Node *)_root, visited, stack, _bbs);
duke@435 818 Node *n;
duke@435 819
duke@435 820 // Walk over all the nodes from last to first
duke@435 821 while (n = iter.next()) {
duke@435 822 // Set the latency for the definitions of this instruction
duke@435 823 partial_latency_of_defs(n);
duke@435 824 }
duke@435 825 } // end ComputeLatenciesBackwards
duke@435 826
duke@435 827 //------------------------------partial_latency_of_defs------------------------
duke@435 828 // Compute the latency impact of this node on all defs. This computes
duke@435 829 // a number that increases as we approach the beginning of the routine.
duke@435 830 void PhaseCFG::partial_latency_of_defs(Node *n) {
duke@435 831 // Set the latency for this instruction
duke@435 832 #ifndef PRODUCT
duke@435 833 if (trace_opto_pipelining()) {
duke@435 834 tty->print("# latency_to_inputs: node_latency[%d] = %d for node",
duke@435 835 n->_idx, _node_latency.at_grow(n->_idx));
duke@435 836 dump();
duke@435 837 }
duke@435 838 #endif
duke@435 839
duke@435 840 if (n->is_Proj())
duke@435 841 n = n->in(0);
duke@435 842
duke@435 843 if (n->is_Root())
duke@435 844 return;
duke@435 845
duke@435 846 uint nlen = n->len();
duke@435 847 uint use_latency = _node_latency.at_grow(n->_idx);
duke@435 848 uint use_pre_order = _bbs[n->_idx]->_pre_order;
duke@435 849
duke@435 850 for ( uint j=0; j<nlen; j++ ) {
duke@435 851 Node *def = n->in(j);
duke@435 852
duke@435 853 if (!def || def == n)
duke@435 854 continue;
duke@435 855
duke@435 856 // Walk backwards thru projections
duke@435 857 if (def->is_Proj())
duke@435 858 def = def->in(0);
duke@435 859
duke@435 860 #ifndef PRODUCT
duke@435 861 if (trace_opto_pipelining()) {
duke@435 862 tty->print("# in(%2d): ", j);
duke@435 863 def->dump();
duke@435 864 }
duke@435 865 #endif
duke@435 866
duke@435 867 // If the defining block is not known, assume it is ok
duke@435 868 Block *def_block = _bbs[def->_idx];
duke@435 869 uint def_pre_order = def_block ? def_block->_pre_order : 0;
duke@435 870
duke@435 871 if ( (use_pre_order < def_pre_order) ||
duke@435 872 (use_pre_order == def_pre_order && n->is_Phi()) )
duke@435 873 continue;
duke@435 874
duke@435 875 uint delta_latency = n->latency(j);
duke@435 876 uint current_latency = delta_latency + use_latency;
duke@435 877
duke@435 878 if (_node_latency.at_grow(def->_idx) < current_latency) {
duke@435 879 _node_latency.at_put_grow(def->_idx, current_latency);
duke@435 880 }
duke@435 881
duke@435 882 #ifndef PRODUCT
duke@435 883 if (trace_opto_pipelining()) {
duke@435 884 tty->print_cr("# %d + edge_latency(%d) == %d -> %d, node_latency[%d] = %d",
duke@435 885 use_latency, j, delta_latency, current_latency, def->_idx,
duke@435 886 _node_latency.at_grow(def->_idx));
duke@435 887 }
duke@435 888 #endif
duke@435 889 }
duke@435 890 }
duke@435 891
duke@435 892 //------------------------------latency_from_use-------------------------------
duke@435 893 // Compute the latency of a specific use
duke@435 894 int PhaseCFG::latency_from_use(Node *n, const Node *def, Node *use) {
duke@435 895 // If self-reference, return no latency
duke@435 896 if (use == n || use->is_Root())
duke@435 897 return 0;
duke@435 898
duke@435 899 uint def_pre_order = _bbs[def->_idx]->_pre_order;
duke@435 900 uint latency = 0;
duke@435 901
duke@435 902 // If the use is not a projection, then it is simple...
duke@435 903 if (!use->is_Proj()) {
duke@435 904 #ifndef PRODUCT
duke@435 905 if (trace_opto_pipelining()) {
duke@435 906 tty->print("# out(): ");
duke@435 907 use->dump();
duke@435 908 }
duke@435 909 #endif
duke@435 910
duke@435 911 uint use_pre_order = _bbs[use->_idx]->_pre_order;
duke@435 912
duke@435 913 if (use_pre_order < def_pre_order)
duke@435 914 return 0;
duke@435 915
duke@435 916 if (use_pre_order == def_pre_order && use->is_Phi())
duke@435 917 return 0;
duke@435 918
duke@435 919 uint nlen = use->len();
duke@435 920 uint nl = _node_latency.at_grow(use->_idx);
duke@435 921
duke@435 922 for ( uint j=0; j<nlen; j++ ) {
duke@435 923 if (use->in(j) == n) {
duke@435 924 // Change this if we want local latencies
duke@435 925 uint ul = use->latency(j);
duke@435 926 uint l = ul + nl;
duke@435 927 if (latency < l) latency = l;
duke@435 928 #ifndef PRODUCT
duke@435 929 if (trace_opto_pipelining()) {
duke@435 930 tty->print_cr("# %d + edge_latency(%d) == %d -> %d, latency = %d",
duke@435 931 nl, j, ul, l, latency);
duke@435 932 }
duke@435 933 #endif
duke@435 934 }
duke@435 935 }
duke@435 936 } else {
duke@435 937 // This is a projection, just grab the latency of the use(s)
duke@435 938 for (DUIterator_Fast jmax, j = use->fast_outs(jmax); j < jmax; j++) {
duke@435 939 uint l = latency_from_use(use, def, use->fast_out(j));
duke@435 940 if (latency < l) latency = l;
duke@435 941 }
duke@435 942 }
duke@435 943
duke@435 944 return latency;
duke@435 945 }
duke@435 946
duke@435 947 //------------------------------latency_from_uses------------------------------
duke@435 948 // Compute the latency of this instruction relative to all of it's uses.
duke@435 949 // This computes a number that increases as we approach the beginning of the
duke@435 950 // routine.
duke@435 951 void PhaseCFG::latency_from_uses(Node *n) {
duke@435 952 // Set the latency for this instruction
duke@435 953 #ifndef PRODUCT
duke@435 954 if (trace_opto_pipelining()) {
duke@435 955 tty->print("# latency_from_outputs: node_latency[%d] = %d for node",
duke@435 956 n->_idx, _node_latency.at_grow(n->_idx));
duke@435 957 dump();
duke@435 958 }
duke@435 959 #endif
duke@435 960 uint latency=0;
duke@435 961 const Node *def = n->is_Proj() ? n->in(0): n;
duke@435 962
duke@435 963 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
duke@435 964 uint l = latency_from_use(n, def, n->fast_out(i));
duke@435 965
duke@435 966 if (latency < l) latency = l;
duke@435 967 }
duke@435 968
duke@435 969 _node_latency.at_put_grow(n->_idx, latency);
duke@435 970 }
duke@435 971
duke@435 972 //------------------------------hoist_to_cheaper_block-------------------------
duke@435 973 // Pick a block for node self, between early and LCA, that is a cheaper
duke@435 974 // alternative to LCA.
duke@435 975 Block* PhaseCFG::hoist_to_cheaper_block(Block* LCA, Block* early, Node* self) {
duke@435 976 const double delta = 1+PROB_UNLIKELY_MAG(4);
duke@435 977 Block* least = LCA;
duke@435 978 double least_freq = least->_freq;
duke@435 979 uint target = _node_latency.at_grow(self->_idx);
duke@435 980 uint start_latency = _node_latency.at_grow(LCA->_nodes[0]->_idx);
duke@435 981 uint end_latency = _node_latency.at_grow(LCA->_nodes[LCA->end_idx()]->_idx);
duke@435 982 bool in_latency = (target <= start_latency);
duke@435 983 const Block* root_block = _bbs[_root->_idx];
duke@435 984
duke@435 985 // Turn off latency scheduling if scheduling is just plain off
duke@435 986 if (!C->do_scheduling())
duke@435 987 in_latency = true;
duke@435 988
duke@435 989 // Do not hoist (to cover latency) instructions which target a
duke@435 990 // single register. Hoisting stretches the live range of the
duke@435 991 // single register and may force spilling.
duke@435 992 MachNode* mach = self->is_Mach() ? self->as_Mach() : NULL;
duke@435 993 if (mach && mach->out_RegMask().is_bound1() && mach->out_RegMask().is_NotEmpty())
duke@435 994 in_latency = true;
duke@435 995
duke@435 996 #ifndef PRODUCT
duke@435 997 if (trace_opto_pipelining()) {
duke@435 998 tty->print("# Find cheaper block for latency %d: ",
duke@435 999 _node_latency.at_grow(self->_idx));
duke@435 1000 self->dump();
duke@435 1001 tty->print_cr("# B%d: start latency for [%4d]=%d, end latency for [%4d]=%d, freq=%g",
duke@435 1002 LCA->_pre_order,
duke@435 1003 LCA->_nodes[0]->_idx,
duke@435 1004 start_latency,
duke@435 1005 LCA->_nodes[LCA->end_idx()]->_idx,
duke@435 1006 end_latency,
duke@435 1007 least_freq);
duke@435 1008 }
duke@435 1009 #endif
duke@435 1010
duke@435 1011 // Walk up the dominator tree from LCA (Lowest common ancestor) to
duke@435 1012 // the earliest legal location. Capture the least execution frequency.
duke@435 1013 while (LCA != early) {
duke@435 1014 LCA = LCA->_idom; // Follow up the dominator tree
duke@435 1015
duke@435 1016 if (LCA == NULL) {
duke@435 1017 // Bailout without retry
duke@435 1018 C->record_method_not_compilable("late schedule failed: LCA == NULL");
duke@435 1019 return least;
duke@435 1020 }
duke@435 1021
duke@435 1022 // Don't hoist machine instructions to the root basic block
duke@435 1023 if (mach && LCA == root_block)
duke@435 1024 break;
duke@435 1025
duke@435 1026 uint start_lat = _node_latency.at_grow(LCA->_nodes[0]->_idx);
duke@435 1027 uint end_idx = LCA->end_idx();
duke@435 1028 uint end_lat = _node_latency.at_grow(LCA->_nodes[end_idx]->_idx);
duke@435 1029 double LCA_freq = LCA->_freq;
duke@435 1030 #ifndef PRODUCT
duke@435 1031 if (trace_opto_pipelining()) {
duke@435 1032 tty->print_cr("# B%d: start latency for [%4d]=%d, end latency for [%4d]=%d, freq=%g",
duke@435 1033 LCA->_pre_order, LCA->_nodes[0]->_idx, start_lat, end_idx, end_lat, LCA_freq);
duke@435 1034 }
duke@435 1035 #endif
duke@435 1036 if (LCA_freq < least_freq || // Better Frequency
duke@435 1037 ( !in_latency && // No block containing latency
duke@435 1038 LCA_freq < least_freq * delta && // No worse frequency
duke@435 1039 target >= end_lat && // within latency range
duke@435 1040 !self->is_iteratively_computed() ) // But don't hoist IV increments
duke@435 1041 // because they may end up above other uses of their phi forcing
duke@435 1042 // their result register to be different from their input.
duke@435 1043 ) {
duke@435 1044 least = LCA; // Found cheaper block
duke@435 1045 least_freq = LCA_freq;
duke@435 1046 start_latency = start_lat;
duke@435 1047 end_latency = end_lat;
duke@435 1048 if (target <= start_lat)
duke@435 1049 in_latency = true;
duke@435 1050 }
duke@435 1051 }
duke@435 1052
duke@435 1053 #ifndef PRODUCT
duke@435 1054 if (trace_opto_pipelining()) {
duke@435 1055 tty->print_cr("# Choose block B%d with start latency=%d and freq=%g",
duke@435 1056 least->_pre_order, start_latency, least_freq);
duke@435 1057 }
duke@435 1058 #endif
duke@435 1059
duke@435 1060 // See if the latency needs to be updated
duke@435 1061 if (target < end_latency) {
duke@435 1062 #ifndef PRODUCT
duke@435 1063 if (trace_opto_pipelining()) {
duke@435 1064 tty->print_cr("# Change latency for [%4d] from %d to %d", self->_idx, target, end_latency);
duke@435 1065 }
duke@435 1066 #endif
duke@435 1067 _node_latency.at_put_grow(self->_idx, end_latency);
duke@435 1068 partial_latency_of_defs(self);
duke@435 1069 }
duke@435 1070
duke@435 1071 return least;
duke@435 1072 }
duke@435 1073
duke@435 1074
duke@435 1075 //------------------------------schedule_late-----------------------------------
duke@435 1076 // Now schedule all codes as LATE as possible. This is the LCA in the
duke@435 1077 // dominator tree of all USES of a value. Pick the block with the least
duke@435 1078 // loop nesting depth that is lowest in the dominator tree.
duke@435 1079 extern const char must_clone[];
duke@435 1080 void PhaseCFG::schedule_late(VectorSet &visited, Node_List &stack) {
duke@435 1081 #ifndef PRODUCT
duke@435 1082 if (trace_opto_pipelining())
duke@435 1083 tty->print("\n#---- schedule_late ----\n");
duke@435 1084 #endif
duke@435 1085
duke@435 1086 Node_Backward_Iterator iter((Node *)_root, visited, stack, _bbs);
duke@435 1087 Node *self;
duke@435 1088
duke@435 1089 // Walk over all the nodes from last to first
duke@435 1090 while (self = iter.next()) {
duke@435 1091 Block* early = _bbs[self->_idx]; // Earliest legal placement
duke@435 1092
duke@435 1093 if (self->is_top()) {
duke@435 1094 // Top node goes in bb #2 with other constants.
duke@435 1095 // It must be special-cased, because it has no out edges.
duke@435 1096 early->add_inst(self);
duke@435 1097 continue;
duke@435 1098 }
duke@435 1099
duke@435 1100 // No uses, just terminate
duke@435 1101 if (self->outcnt() == 0) {
duke@435 1102 assert(self->Opcode() == Op_MachProj, "sanity");
duke@435 1103 continue; // Must be a dead machine projection
duke@435 1104 }
duke@435 1105
duke@435 1106 // If node is pinned in the block, then no scheduling can be done.
duke@435 1107 if( self->pinned() ) // Pinned in block?
duke@435 1108 continue;
duke@435 1109
duke@435 1110 MachNode* mach = self->is_Mach() ? self->as_Mach() : NULL;
duke@435 1111 if (mach) {
duke@435 1112 switch (mach->ideal_Opcode()) {
duke@435 1113 case Op_CreateEx:
duke@435 1114 // Don't move exception creation
duke@435 1115 early->add_inst(self);
duke@435 1116 continue;
duke@435 1117 break;
duke@435 1118 case Op_CheckCastPP:
duke@435 1119 // Don't move CheckCastPP nodes away from their input, if the input
duke@435 1120 // is a rawptr (5071820).
duke@435 1121 Node *def = self->in(1);
duke@435 1122 if (def != NULL && def->bottom_type()->base() == Type::RawPtr) {
duke@435 1123 early->add_inst(self);
duke@435 1124 continue;
duke@435 1125 }
duke@435 1126 break;
duke@435 1127 }
duke@435 1128 }
duke@435 1129
duke@435 1130 // Gather LCA of all uses
duke@435 1131 Block *LCA = NULL;
duke@435 1132 {
duke@435 1133 for (DUIterator_Fast imax, i = self->fast_outs(imax); i < imax; i++) {
duke@435 1134 // For all uses, find LCA
duke@435 1135 Node* use = self->fast_out(i);
duke@435 1136 LCA = raise_LCA_above_use(LCA, use, self, _bbs);
duke@435 1137 }
duke@435 1138 } // (Hide defs of imax, i from rest of block.)
duke@435 1139
duke@435 1140 // Place temps in the block of their use. This isn't a
duke@435 1141 // requirement for correctness but it reduces useless
duke@435 1142 // interference between temps and other nodes.
duke@435 1143 if (mach != NULL && mach->is_MachTemp()) {
duke@435 1144 _bbs.map(self->_idx, LCA);
duke@435 1145 LCA->add_inst(self);
duke@435 1146 continue;
duke@435 1147 }
duke@435 1148
duke@435 1149 // Check if 'self' could be anti-dependent on memory
duke@435 1150 if (self->needs_anti_dependence_check()) {
duke@435 1151 // Hoist LCA above possible-defs and insert anti-dependences to
duke@435 1152 // defs in new LCA block.
duke@435 1153 LCA = insert_anti_dependences(LCA, self);
duke@435 1154 }
duke@435 1155
duke@435 1156 if (early->_dom_depth > LCA->_dom_depth) {
duke@435 1157 // Somehow the LCA has moved above the earliest legal point.
duke@435 1158 // (One way this can happen is via memory_early_block.)
duke@435 1159 if (C->subsume_loads() == true && !C->failing()) {
duke@435 1160 // Retry with subsume_loads == false
duke@435 1161 // If this is the first failure, the sentinel string will "stick"
duke@435 1162 // to the Compile object, and the C2Compiler will see it and retry.
duke@435 1163 C->record_failure(C2Compiler::retry_no_subsuming_loads());
duke@435 1164 } else {
duke@435 1165 // Bailout without retry when (early->_dom_depth > LCA->_dom_depth)
duke@435 1166 C->record_method_not_compilable("late schedule failed: incorrect graph");
duke@435 1167 }
duke@435 1168 return;
duke@435 1169 }
duke@435 1170
duke@435 1171 // If there is no opportunity to hoist, then we're done.
duke@435 1172 bool try_to_hoist = (LCA != early);
duke@435 1173
duke@435 1174 // Must clone guys stay next to use; no hoisting allowed.
duke@435 1175 // Also cannot hoist guys that alter memory or are otherwise not
duke@435 1176 // allocatable (hoisting can make a value live longer, leading to
duke@435 1177 // anti and output dependency problems which are normally resolved
duke@435 1178 // by the register allocator giving everyone a different register).
duke@435 1179 if (mach != NULL && must_clone[mach->ideal_Opcode()])
duke@435 1180 try_to_hoist = false;
duke@435 1181
duke@435 1182 Block* late = NULL;
duke@435 1183 if (try_to_hoist) {
duke@435 1184 // Now find the block with the least execution frequency.
duke@435 1185 // Start at the latest schedule and work up to the earliest schedule
duke@435 1186 // in the dominator tree. Thus the Node will dominate all its uses.
duke@435 1187 late = hoist_to_cheaper_block(LCA, early, self);
duke@435 1188 } else {
duke@435 1189 // Just use the LCA of the uses.
duke@435 1190 late = LCA;
duke@435 1191 }
duke@435 1192
duke@435 1193 // Put the node into target block
duke@435 1194 schedule_node_into_block(self, late);
duke@435 1195
duke@435 1196 #ifdef ASSERT
duke@435 1197 if (self->needs_anti_dependence_check()) {
duke@435 1198 // since precedence edges are only inserted when we're sure they
duke@435 1199 // are needed make sure that after placement in a block we don't
duke@435 1200 // need any new precedence edges.
duke@435 1201 verify_anti_dependences(late, self);
duke@435 1202 }
duke@435 1203 #endif
duke@435 1204 } // Loop until all nodes have been visited
duke@435 1205
duke@435 1206 } // end ScheduleLate
duke@435 1207
duke@435 1208 //------------------------------GlobalCodeMotion-------------------------------
duke@435 1209 void PhaseCFG::GlobalCodeMotion( Matcher &matcher, uint unique, Node_List &proj_list ) {
duke@435 1210 ResourceMark rm;
duke@435 1211
duke@435 1212 #ifndef PRODUCT
duke@435 1213 if (trace_opto_pipelining()) {
duke@435 1214 tty->print("\n---- Start GlobalCodeMotion ----\n");
duke@435 1215 }
duke@435 1216 #endif
duke@435 1217
duke@435 1218 // Initialize the bbs.map for things on the proj_list
duke@435 1219 uint i;
duke@435 1220 for( i=0; i < proj_list.size(); i++ )
duke@435 1221 _bbs.map(proj_list[i]->_idx, NULL);
duke@435 1222
duke@435 1223 // Set the basic block for Nodes pinned into blocks
duke@435 1224 Arena *a = Thread::current()->resource_area();
duke@435 1225 VectorSet visited(a);
duke@435 1226 schedule_pinned_nodes( visited );
duke@435 1227
duke@435 1228 // Find the earliest Block any instruction can be placed in. Some
duke@435 1229 // instructions are pinned into Blocks. Unpinned instructions can
duke@435 1230 // appear in last block in which all their inputs occur.
duke@435 1231 visited.Clear();
duke@435 1232 Node_List stack(a);
duke@435 1233 stack.map( (unique >> 1) + 16, NULL); // Pre-grow the list
duke@435 1234 if (!schedule_early(visited, stack)) {
duke@435 1235 // Bailout without retry
duke@435 1236 C->record_method_not_compilable("early schedule failed");
duke@435 1237 return;
duke@435 1238 }
duke@435 1239
duke@435 1240 // Build Def-Use edges.
duke@435 1241 proj_list.push(_root); // Add real root as another root
duke@435 1242 proj_list.pop();
duke@435 1243
duke@435 1244 // Compute the latency information (via backwards walk) for all the
duke@435 1245 // instructions in the graph
duke@435 1246 GrowableArray<uint> node_latency;
duke@435 1247 _node_latency = node_latency;
duke@435 1248
duke@435 1249 if( C->do_scheduling() )
duke@435 1250 ComputeLatenciesBackwards(visited, stack);
duke@435 1251
duke@435 1252 // Now schedule all codes as LATE as possible. This is the LCA in the
duke@435 1253 // dominator tree of all USES of a value. Pick the block with the least
duke@435 1254 // loop nesting depth that is lowest in the dominator tree.
duke@435 1255 // ( visited.Clear() called in schedule_late()->Node_Backward_Iterator() )
duke@435 1256 schedule_late(visited, stack);
duke@435 1257 if( C->failing() ) {
duke@435 1258 // schedule_late fails only when graph is incorrect.
duke@435 1259 assert(!VerifyGraphEdges, "verification should have failed");
duke@435 1260 return;
duke@435 1261 }
duke@435 1262
duke@435 1263 unique = C->unique();
duke@435 1264
duke@435 1265 #ifndef PRODUCT
duke@435 1266 if (trace_opto_pipelining()) {
duke@435 1267 tty->print("\n---- Detect implicit null checks ----\n");
duke@435 1268 }
duke@435 1269 #endif
duke@435 1270
duke@435 1271 // Detect implicit-null-check opportunities. Basically, find NULL checks
duke@435 1272 // with suitable memory ops nearby. Use the memory op to do the NULL check.
duke@435 1273 // I can generate a memory op if there is not one nearby.
duke@435 1274 if (C->is_method_compilation()) {
duke@435 1275 // Don't do it for natives, adapters, or runtime stubs
duke@435 1276 int allowed_reasons = 0;
duke@435 1277 // ...and don't do it when there have been too many traps, globally.
duke@435 1278 for (int reason = (int)Deoptimization::Reason_none+1;
duke@435 1279 reason < Compile::trapHistLength; reason++) {
duke@435 1280 assert(reason < BitsPerInt, "recode bit map");
duke@435 1281 if (!C->too_many_traps((Deoptimization::DeoptReason) reason))
duke@435 1282 allowed_reasons |= nth_bit(reason);
duke@435 1283 }
duke@435 1284 // By reversing the loop direction we get a very minor gain on mpegaudio.
duke@435 1285 // Feel free to revert to a forward loop for clarity.
duke@435 1286 // for( int i=0; i < (int)matcher._null_check_tests.size(); i+=2 ) {
duke@435 1287 for( int i= matcher._null_check_tests.size()-2; i>=0; i-=2 ) {
duke@435 1288 Node *proj = matcher._null_check_tests[i ];
duke@435 1289 Node *val = matcher._null_check_tests[i+1];
duke@435 1290 _bbs[proj->_idx]->implicit_null_check(this, proj, val, allowed_reasons);
duke@435 1291 // The implicit_null_check will only perform the transformation
duke@435 1292 // if the null branch is truly uncommon, *and* it leads to an
duke@435 1293 // uncommon trap. Combined with the too_many_traps guards
duke@435 1294 // above, this prevents SEGV storms reported in 6366351,
duke@435 1295 // by recompiling offending methods without this optimization.
duke@435 1296 }
duke@435 1297 }
duke@435 1298
duke@435 1299 #ifndef PRODUCT
duke@435 1300 if (trace_opto_pipelining()) {
duke@435 1301 tty->print("\n---- Start Local Scheduling ----\n");
duke@435 1302 }
duke@435 1303 #endif
duke@435 1304
duke@435 1305 // Schedule locally. Right now a simple topological sort.
duke@435 1306 // Later, do a real latency aware scheduler.
duke@435 1307 int *ready_cnt = NEW_RESOURCE_ARRAY(int,C->unique());
duke@435 1308 memset( ready_cnt, -1, C->unique() * sizeof(int) );
duke@435 1309 visited.Clear();
duke@435 1310 for (i = 0; i < _num_blocks; i++) {
duke@435 1311 if (!_blocks[i]->schedule_local(this, matcher, ready_cnt, visited)) {
duke@435 1312 if (!C->failure_reason_is(C2Compiler::retry_no_subsuming_loads())) {
duke@435 1313 C->record_method_not_compilable("local schedule failed");
duke@435 1314 }
duke@435 1315 return;
duke@435 1316 }
duke@435 1317 }
duke@435 1318
duke@435 1319 // If we inserted any instructions between a Call and his CatchNode,
duke@435 1320 // clone the instructions on all paths below the Catch.
duke@435 1321 for( i=0; i < _num_blocks; i++ )
duke@435 1322 _blocks[i]->call_catch_cleanup(_bbs);
duke@435 1323
duke@435 1324 #ifndef PRODUCT
duke@435 1325 if (trace_opto_pipelining()) {
duke@435 1326 tty->print("\n---- After GlobalCodeMotion ----\n");
duke@435 1327 for (uint i = 0; i < _num_blocks; i++) {
duke@435 1328 _blocks[i]->dump();
duke@435 1329 }
duke@435 1330 }
duke@435 1331 #endif
duke@435 1332 }
duke@435 1333
duke@435 1334
duke@435 1335 //------------------------------Estimate_Block_Frequency-----------------------
duke@435 1336 // Estimate block frequencies based on IfNode probabilities.
duke@435 1337 void PhaseCFG::Estimate_Block_Frequency() {
rasbold@853 1338
rasbold@853 1339 // Force conditional branches leading to uncommon traps to be unlikely,
rasbold@853 1340 // not because we get to the uncommon_trap with less relative frequency,
rasbold@853 1341 // but because an uncommon_trap typically causes a deopt, so we only get
rasbold@853 1342 // there once.
rasbold@853 1343 if (C->do_freq_based_layout()) {
rasbold@853 1344 Block_List worklist;
rasbold@853 1345 Block* root_blk = _blocks[0];
rasbold@853 1346 for (uint i = 1; i < root_blk->num_preds(); i++) {
rasbold@853 1347 Block *pb = _bbs[root_blk->pred(i)->_idx];
rasbold@853 1348 if (pb->has_uncommon_code()) {
rasbold@853 1349 worklist.push(pb);
rasbold@853 1350 }
rasbold@853 1351 }
rasbold@853 1352 while (worklist.size() > 0) {
rasbold@853 1353 Block* uct = worklist.pop();
rasbold@853 1354 if (uct == _broot) continue;
rasbold@853 1355 for (uint i = 1; i < uct->num_preds(); i++) {
rasbold@853 1356 Block *pb = _bbs[uct->pred(i)->_idx];
rasbold@853 1357 if (pb->_num_succs == 1) {
rasbold@853 1358 worklist.push(pb);
rasbold@853 1359 } else if (pb->num_fall_throughs() == 2) {
rasbold@853 1360 pb->update_uncommon_branch(uct);
rasbold@853 1361 }
rasbold@853 1362 }
rasbold@853 1363 }
rasbold@853 1364 }
duke@435 1365
duke@435 1366 // Create the loop tree and calculate loop depth.
duke@435 1367 _root_loop = create_loop_tree();
duke@435 1368 _root_loop->compute_loop_depth(0);
duke@435 1369
duke@435 1370 // Compute block frequency of each block, relative to a single loop entry.
duke@435 1371 _root_loop->compute_freq();
duke@435 1372
duke@435 1373 // Adjust all frequencies to be relative to a single method entry
rasbold@853 1374 _root_loop->_freq = 1.0;
duke@435 1375 _root_loop->scale_freq();
duke@435 1376
duke@435 1377 // force paths ending at uncommon traps to be infrequent
rasbold@853 1378 if (!C->do_freq_based_layout()) {
rasbold@853 1379 Block_List worklist;
rasbold@853 1380 Block* root_blk = _blocks[0];
rasbold@853 1381 for (uint i = 1; i < root_blk->num_preds(); i++) {
rasbold@853 1382 Block *pb = _bbs[root_blk->pred(i)->_idx];
rasbold@853 1383 if (pb->has_uncommon_code()) {
rasbold@853 1384 worklist.push(pb);
rasbold@853 1385 }
duke@435 1386 }
rasbold@853 1387 while (worklist.size() > 0) {
rasbold@853 1388 Block* uct = worklist.pop();
rasbold@853 1389 uct->_freq = PROB_MIN;
rasbold@853 1390 for (uint i = 1; i < uct->num_preds(); i++) {
rasbold@853 1391 Block *pb = _bbs[uct->pred(i)->_idx];
rasbold@853 1392 if (pb->_num_succs == 1 && pb->_freq > PROB_MIN) {
rasbold@853 1393 worklist.push(pb);
rasbold@853 1394 }
duke@435 1395 }
duke@435 1396 }
duke@435 1397 }
duke@435 1398
kvn@987 1399 #ifdef ASSERT
kvn@987 1400 for (uint i = 0; i < _num_blocks; i++ ) {
kvn@987 1401 Block *b = _blocks[i];
twisti@1040 1402 assert(b->_freq >= MIN_BLOCK_FREQUENCY, "Register Allocator requires meaningful block frequency");
kvn@987 1403 }
kvn@987 1404 #endif
kvn@987 1405
duke@435 1406 #ifndef PRODUCT
duke@435 1407 if (PrintCFGBlockFreq) {
duke@435 1408 tty->print_cr("CFG Block Frequencies");
duke@435 1409 _root_loop->dump_tree();
duke@435 1410 if (Verbose) {
duke@435 1411 tty->print_cr("PhaseCFG dump");
duke@435 1412 dump();
duke@435 1413 tty->print_cr("Node dump");
duke@435 1414 _root->dump(99999);
duke@435 1415 }
duke@435 1416 }
duke@435 1417 #endif
duke@435 1418 }
duke@435 1419
duke@435 1420 //----------------------------create_loop_tree--------------------------------
duke@435 1421 // Create a loop tree from the CFG
duke@435 1422 CFGLoop* PhaseCFG::create_loop_tree() {
duke@435 1423
duke@435 1424 #ifdef ASSERT
duke@435 1425 assert( _blocks[0] == _broot, "" );
duke@435 1426 for (uint i = 0; i < _num_blocks; i++ ) {
duke@435 1427 Block *b = _blocks[i];
duke@435 1428 // Check that _loop field are clear...we could clear them if not.
duke@435 1429 assert(b->_loop == NULL, "clear _loop expected");
duke@435 1430 // Sanity check that the RPO numbering is reflected in the _blocks array.
duke@435 1431 // It doesn't have to be for the loop tree to be built, but if it is not,
duke@435 1432 // then the blocks have been reordered since dom graph building...which
duke@435 1433 // may question the RPO numbering
duke@435 1434 assert(b->_rpo == i, "unexpected reverse post order number");
duke@435 1435 }
duke@435 1436 #endif
duke@435 1437
duke@435 1438 int idct = 0;
duke@435 1439 CFGLoop* root_loop = new CFGLoop(idct++);
duke@435 1440
duke@435 1441 Block_List worklist;
duke@435 1442
duke@435 1443 // Assign blocks to loops
duke@435 1444 for(uint i = _num_blocks - 1; i > 0; i-- ) { // skip Root block
duke@435 1445 Block *b = _blocks[i];
duke@435 1446
duke@435 1447 if (b->head()->is_Loop()) {
duke@435 1448 Block* loop_head = b;
duke@435 1449 assert(loop_head->num_preds() - 1 == 2, "loop must have 2 predecessors");
duke@435 1450 Node* tail_n = loop_head->pred(LoopNode::LoopBackControl);
duke@435 1451 Block* tail = _bbs[tail_n->_idx];
duke@435 1452
duke@435 1453 // Defensively filter out Loop nodes for non-single-entry loops.
duke@435 1454 // For all reasonable loops, the head occurs before the tail in RPO.
duke@435 1455 if (i <= tail->_rpo) {
duke@435 1456
duke@435 1457 // The tail and (recursive) predecessors of the tail
duke@435 1458 // are made members of a new loop.
duke@435 1459
duke@435 1460 assert(worklist.size() == 0, "nonempty worklist");
duke@435 1461 CFGLoop* nloop = new CFGLoop(idct++);
duke@435 1462 assert(loop_head->_loop == NULL, "just checking");
duke@435 1463 loop_head->_loop = nloop;
duke@435 1464 // Add to nloop so push_pred() will skip over inner loops
duke@435 1465 nloop->add_member(loop_head);
duke@435 1466 nloop->push_pred(loop_head, LoopNode::LoopBackControl, worklist, _bbs);
duke@435 1467
duke@435 1468 while (worklist.size() > 0) {
duke@435 1469 Block* member = worklist.pop();
duke@435 1470 if (member != loop_head) {
duke@435 1471 for (uint j = 1; j < member->num_preds(); j++) {
duke@435 1472 nloop->push_pred(member, j, worklist, _bbs);
duke@435 1473 }
duke@435 1474 }
duke@435 1475 }
duke@435 1476 }
duke@435 1477 }
duke@435 1478 }
duke@435 1479
duke@435 1480 // Create a member list for each loop consisting
duke@435 1481 // of both blocks and (immediate child) loops.
duke@435 1482 for (uint i = 0; i < _num_blocks; i++) {
duke@435 1483 Block *b = _blocks[i];
duke@435 1484 CFGLoop* lp = b->_loop;
duke@435 1485 if (lp == NULL) {
duke@435 1486 // Not assigned to a loop. Add it to the method's pseudo loop.
duke@435 1487 b->_loop = root_loop;
duke@435 1488 lp = root_loop;
duke@435 1489 }
duke@435 1490 if (lp == root_loop || b != lp->head()) { // loop heads are already members
duke@435 1491 lp->add_member(b);
duke@435 1492 }
duke@435 1493 if (lp != root_loop) {
duke@435 1494 if (lp->parent() == NULL) {
duke@435 1495 // Not a nested loop. Make it a child of the method's pseudo loop.
duke@435 1496 root_loop->add_nested_loop(lp);
duke@435 1497 }
duke@435 1498 if (b == lp->head()) {
duke@435 1499 // Add nested loop to member list of parent loop.
duke@435 1500 lp->parent()->add_member(lp);
duke@435 1501 }
duke@435 1502 }
duke@435 1503 }
duke@435 1504
duke@435 1505 return root_loop;
duke@435 1506 }
duke@435 1507
duke@435 1508 //------------------------------push_pred--------------------------------------
duke@435 1509 void CFGLoop::push_pred(Block* blk, int i, Block_List& worklist, Block_Array& node_to_blk) {
duke@435 1510 Node* pred_n = blk->pred(i);
duke@435 1511 Block* pred = node_to_blk[pred_n->_idx];
duke@435 1512 CFGLoop *pred_loop = pred->_loop;
duke@435 1513 if (pred_loop == NULL) {
duke@435 1514 // Filter out blocks for non-single-entry loops.
duke@435 1515 // For all reasonable loops, the head occurs before the tail in RPO.
duke@435 1516 if (pred->_rpo > head()->_rpo) {
duke@435 1517 pred->_loop = this;
duke@435 1518 worklist.push(pred);
duke@435 1519 }
duke@435 1520 } else if (pred_loop != this) {
duke@435 1521 // Nested loop.
duke@435 1522 while (pred_loop->_parent != NULL && pred_loop->_parent != this) {
duke@435 1523 pred_loop = pred_loop->_parent;
duke@435 1524 }
duke@435 1525 // Make pred's loop be a child
duke@435 1526 if (pred_loop->_parent == NULL) {
duke@435 1527 add_nested_loop(pred_loop);
duke@435 1528 // Continue with loop entry predecessor.
duke@435 1529 Block* pred_head = pred_loop->head();
duke@435 1530 assert(pred_head->num_preds() - 1 == 2, "loop must have 2 predecessors");
duke@435 1531 assert(pred_head != head(), "loop head in only one loop");
duke@435 1532 push_pred(pred_head, LoopNode::EntryControl, worklist, node_to_blk);
duke@435 1533 } else {
duke@435 1534 assert(pred_loop->_parent == this && _parent == NULL, "just checking");
duke@435 1535 }
duke@435 1536 }
duke@435 1537 }
duke@435 1538
duke@435 1539 //------------------------------add_nested_loop--------------------------------
duke@435 1540 // Make cl a child of the current loop in the loop tree.
duke@435 1541 void CFGLoop::add_nested_loop(CFGLoop* cl) {
duke@435 1542 assert(_parent == NULL, "no parent yet");
duke@435 1543 assert(cl != this, "not my own parent");
duke@435 1544 cl->_parent = this;
duke@435 1545 CFGLoop* ch = _child;
duke@435 1546 if (ch == NULL) {
duke@435 1547 _child = cl;
duke@435 1548 } else {
duke@435 1549 while (ch->_sibling != NULL) { ch = ch->_sibling; }
duke@435 1550 ch->_sibling = cl;
duke@435 1551 }
duke@435 1552 }
duke@435 1553
duke@435 1554 //------------------------------compute_loop_depth-----------------------------
duke@435 1555 // Store the loop depth in each CFGLoop object.
duke@435 1556 // Recursively walk the children to do the same for them.
duke@435 1557 void CFGLoop::compute_loop_depth(int depth) {
duke@435 1558 _depth = depth;
duke@435 1559 CFGLoop* ch = _child;
duke@435 1560 while (ch != NULL) {
duke@435 1561 ch->compute_loop_depth(depth + 1);
duke@435 1562 ch = ch->_sibling;
duke@435 1563 }
duke@435 1564 }
duke@435 1565
duke@435 1566 //------------------------------compute_freq-----------------------------------
duke@435 1567 // Compute the frequency of each block and loop, relative to a single entry
duke@435 1568 // into the dominating loop head.
duke@435 1569 void CFGLoop::compute_freq() {
duke@435 1570 // Bottom up traversal of loop tree (visit inner loops first.)
duke@435 1571 // Set loop head frequency to 1.0, then transitively
duke@435 1572 // compute frequency for all successors in the loop,
duke@435 1573 // as well as for each exit edge. Inner loops are
duke@435 1574 // treated as single blocks with loop exit targets
duke@435 1575 // as the successor blocks.
duke@435 1576
duke@435 1577 // Nested loops first
duke@435 1578 CFGLoop* ch = _child;
duke@435 1579 while (ch != NULL) {
duke@435 1580 ch->compute_freq();
duke@435 1581 ch = ch->_sibling;
duke@435 1582 }
duke@435 1583 assert (_members.length() > 0, "no empty loops");
duke@435 1584 Block* hd = head();
duke@435 1585 hd->_freq = 1.0f;
duke@435 1586 for (int i = 0; i < _members.length(); i++) {
duke@435 1587 CFGElement* s = _members.at(i);
duke@435 1588 float freq = s->_freq;
duke@435 1589 if (s->is_block()) {
duke@435 1590 Block* b = s->as_Block();
duke@435 1591 for (uint j = 0; j < b->_num_succs; j++) {
duke@435 1592 Block* sb = b->_succs[j];
duke@435 1593 update_succ_freq(sb, freq * b->succ_prob(j));
duke@435 1594 }
duke@435 1595 } else {
duke@435 1596 CFGLoop* lp = s->as_CFGLoop();
duke@435 1597 assert(lp->_parent == this, "immediate child");
duke@435 1598 for (int k = 0; k < lp->_exits.length(); k++) {
duke@435 1599 Block* eb = lp->_exits.at(k).get_target();
duke@435 1600 float prob = lp->_exits.at(k).get_prob();
duke@435 1601 update_succ_freq(eb, freq * prob);
duke@435 1602 }
duke@435 1603 }
duke@435 1604 }
duke@435 1605
duke@435 1606 // For all loops other than the outer, "method" loop,
duke@435 1607 // sum and normalize the exit probability. The "method" loop
duke@435 1608 // should keep the initial exit probability of 1, so that
duke@435 1609 // inner blocks do not get erroneously scaled.
duke@435 1610 if (_depth != 0) {
duke@435 1611 // Total the exit probabilities for this loop.
duke@435 1612 float exits_sum = 0.0f;
duke@435 1613 for (int i = 0; i < _exits.length(); i++) {
duke@435 1614 exits_sum += _exits.at(i).get_prob();
duke@435 1615 }
duke@435 1616
duke@435 1617 // Normalize the exit probabilities. Until now, the
duke@435 1618 // probabilities estimate the possibility of exit per
duke@435 1619 // a single loop iteration; afterward, they estimate
duke@435 1620 // the probability of exit per loop entry.
duke@435 1621 for (int i = 0; i < _exits.length(); i++) {
duke@435 1622 Block* et = _exits.at(i).get_target();
rasbold@853 1623 float new_prob = 0.0f;
rasbold@853 1624 if (_exits.at(i).get_prob() > 0.0f) {
rasbold@853 1625 new_prob = _exits.at(i).get_prob() / exits_sum;
rasbold@853 1626 }
duke@435 1627 BlockProbPair bpp(et, new_prob);
duke@435 1628 _exits.at_put(i, bpp);
duke@435 1629 }
duke@435 1630
rasbold@853 1631 // Save the total, but guard against unreasonable probability,
duke@435 1632 // as the value is used to estimate the loop trip count.
duke@435 1633 // An infinite trip count would blur relative block
duke@435 1634 // frequencies.
duke@435 1635 if (exits_sum > 1.0f) exits_sum = 1.0;
duke@435 1636 if (exits_sum < PROB_MIN) exits_sum = PROB_MIN;
duke@435 1637 _exit_prob = exits_sum;
duke@435 1638 }
duke@435 1639 }
duke@435 1640
duke@435 1641 //------------------------------succ_prob-------------------------------------
duke@435 1642 // Determine the probability of reaching successor 'i' from the receiver block.
duke@435 1643 float Block::succ_prob(uint i) {
duke@435 1644 int eidx = end_idx();
duke@435 1645 Node *n = _nodes[eidx]; // Get ending Node
rasbold@743 1646
rasbold@743 1647 int op = n->Opcode();
rasbold@743 1648 if (n->is_Mach()) {
rasbold@743 1649 if (n->is_MachNullCheck()) {
rasbold@743 1650 // Can only reach here if called after lcm. The original Op_If is gone,
rasbold@743 1651 // so we attempt to infer the probability from one or both of the
rasbold@743 1652 // successor blocks.
rasbold@743 1653 assert(_num_succs == 2, "expecting 2 successors of a null check");
rasbold@743 1654 // If either successor has only one predecessor, then the
twisti@1040 1655 // probability estimate can be derived using the
rasbold@743 1656 // relative frequency of the successor and this block.
rasbold@743 1657 if (_succs[i]->num_preds() == 2) {
rasbold@743 1658 return _succs[i]->_freq / _freq;
rasbold@743 1659 } else if (_succs[1-i]->num_preds() == 2) {
rasbold@743 1660 return 1 - (_succs[1-i]->_freq / _freq);
rasbold@743 1661 } else {
rasbold@743 1662 // Estimate using both successor frequencies
rasbold@743 1663 float freq = _succs[i]->_freq;
rasbold@743 1664 return freq / (freq + _succs[1-i]->_freq);
rasbold@743 1665 }
rasbold@743 1666 }
rasbold@743 1667 op = n->as_Mach()->ideal_Opcode();
rasbold@743 1668 }
rasbold@743 1669
duke@435 1670
duke@435 1671 // Switch on branch type
duke@435 1672 switch( op ) {
duke@435 1673 case Op_CountedLoopEnd:
duke@435 1674 case Op_If: {
duke@435 1675 assert (i < 2, "just checking");
duke@435 1676 // Conditionals pass on only part of their frequency
duke@435 1677 float prob = n->as_MachIf()->_prob;
duke@435 1678 assert(prob >= 0.0 && prob <= 1.0, "out of range probability");
duke@435 1679 // If succ[i] is the FALSE branch, invert path info
duke@435 1680 if( _nodes[i + eidx + 1]->Opcode() == Op_IfFalse ) {
duke@435 1681 return 1.0f - prob; // not taken
duke@435 1682 } else {
duke@435 1683 return prob; // taken
duke@435 1684 }
duke@435 1685 }
duke@435 1686
duke@435 1687 case Op_Jump:
duke@435 1688 // Divide the frequency between all successors evenly
duke@435 1689 return 1.0f/_num_succs;
duke@435 1690
duke@435 1691 case Op_Catch: {
duke@435 1692 const CatchProjNode *ci = _nodes[i + eidx + 1]->as_CatchProj();
duke@435 1693 if (ci->_con == CatchProjNode::fall_through_index) {
duke@435 1694 // Fall-thru path gets the lion's share.
duke@435 1695 return 1.0f - PROB_UNLIKELY_MAG(5)*_num_succs;
duke@435 1696 } else {
duke@435 1697 // Presume exceptional paths are equally unlikely
duke@435 1698 return PROB_UNLIKELY_MAG(5);
duke@435 1699 }
duke@435 1700 }
duke@435 1701
duke@435 1702 case Op_Root:
duke@435 1703 case Op_Goto:
duke@435 1704 // Pass frequency straight thru to target
duke@435 1705 return 1.0f;
duke@435 1706
duke@435 1707 case Op_NeverBranch:
duke@435 1708 return 0.0f;
duke@435 1709
duke@435 1710 case Op_TailCall:
duke@435 1711 case Op_TailJump:
duke@435 1712 case Op_Return:
duke@435 1713 case Op_Halt:
duke@435 1714 case Op_Rethrow:
duke@435 1715 // Do not push out freq to root block
duke@435 1716 return 0.0f;
duke@435 1717
duke@435 1718 default:
duke@435 1719 ShouldNotReachHere();
duke@435 1720 }
duke@435 1721
duke@435 1722 return 0.0f;
duke@435 1723 }
duke@435 1724
rasbold@853 1725 //------------------------------num_fall_throughs-----------------------------
rasbold@853 1726 // Return the number of fall-through candidates for a block
rasbold@853 1727 int Block::num_fall_throughs() {
rasbold@853 1728 int eidx = end_idx();
rasbold@853 1729 Node *n = _nodes[eidx]; // Get ending Node
rasbold@853 1730
rasbold@853 1731 int op = n->Opcode();
rasbold@853 1732 if (n->is_Mach()) {
rasbold@853 1733 if (n->is_MachNullCheck()) {
rasbold@853 1734 // In theory, either side can fall-thru, for simplicity sake,
rasbold@853 1735 // let's say only the false branch can now.
rasbold@853 1736 return 1;
rasbold@853 1737 }
rasbold@853 1738 op = n->as_Mach()->ideal_Opcode();
rasbold@853 1739 }
rasbold@853 1740
rasbold@853 1741 // Switch on branch type
rasbold@853 1742 switch( op ) {
rasbold@853 1743 case Op_CountedLoopEnd:
rasbold@853 1744 case Op_If:
rasbold@853 1745 return 2;
rasbold@853 1746
rasbold@853 1747 case Op_Root:
rasbold@853 1748 case Op_Goto:
rasbold@853 1749 return 1;
rasbold@853 1750
rasbold@853 1751 case Op_Catch: {
rasbold@853 1752 for (uint i = 0; i < _num_succs; i++) {
rasbold@853 1753 const CatchProjNode *ci = _nodes[i + eidx + 1]->as_CatchProj();
rasbold@853 1754 if (ci->_con == CatchProjNode::fall_through_index) {
rasbold@853 1755 return 1;
rasbold@853 1756 }
rasbold@853 1757 }
rasbold@853 1758 return 0;
rasbold@853 1759 }
rasbold@853 1760
rasbold@853 1761 case Op_Jump:
rasbold@853 1762 case Op_NeverBranch:
rasbold@853 1763 case Op_TailCall:
rasbold@853 1764 case Op_TailJump:
rasbold@853 1765 case Op_Return:
rasbold@853 1766 case Op_Halt:
rasbold@853 1767 case Op_Rethrow:
rasbold@853 1768 return 0;
rasbold@853 1769
rasbold@853 1770 default:
rasbold@853 1771 ShouldNotReachHere();
rasbold@853 1772 }
rasbold@853 1773
rasbold@853 1774 return 0;
rasbold@853 1775 }
rasbold@853 1776
rasbold@853 1777 //------------------------------succ_fall_through-----------------------------
rasbold@853 1778 // Return true if a specific successor could be fall-through target.
rasbold@853 1779 bool Block::succ_fall_through(uint i) {
rasbold@853 1780 int eidx = end_idx();
rasbold@853 1781 Node *n = _nodes[eidx]; // Get ending Node
rasbold@853 1782
rasbold@853 1783 int op = n->Opcode();
rasbold@853 1784 if (n->is_Mach()) {
rasbold@853 1785 if (n->is_MachNullCheck()) {
rasbold@853 1786 // In theory, either side can fall-thru, for simplicity sake,
rasbold@853 1787 // let's say only the false branch can now.
rasbold@853 1788 return _nodes[i + eidx + 1]->Opcode() == Op_IfFalse;
rasbold@853 1789 }
rasbold@853 1790 op = n->as_Mach()->ideal_Opcode();
rasbold@853 1791 }
rasbold@853 1792
rasbold@853 1793 // Switch on branch type
rasbold@853 1794 switch( op ) {
rasbold@853 1795 case Op_CountedLoopEnd:
rasbold@853 1796 case Op_If:
rasbold@853 1797 case Op_Root:
rasbold@853 1798 case Op_Goto:
rasbold@853 1799 return true;
rasbold@853 1800
rasbold@853 1801 case Op_Catch: {
rasbold@853 1802 const CatchProjNode *ci = _nodes[i + eidx + 1]->as_CatchProj();
rasbold@853 1803 return ci->_con == CatchProjNode::fall_through_index;
rasbold@853 1804 }
rasbold@853 1805
rasbold@853 1806 case Op_Jump:
rasbold@853 1807 case Op_NeverBranch:
rasbold@853 1808 case Op_TailCall:
rasbold@853 1809 case Op_TailJump:
rasbold@853 1810 case Op_Return:
rasbold@853 1811 case Op_Halt:
rasbold@853 1812 case Op_Rethrow:
rasbold@853 1813 return false;
rasbold@853 1814
rasbold@853 1815 default:
rasbold@853 1816 ShouldNotReachHere();
rasbold@853 1817 }
rasbold@853 1818
rasbold@853 1819 return false;
rasbold@853 1820 }
rasbold@853 1821
rasbold@853 1822 //------------------------------update_uncommon_branch------------------------
rasbold@853 1823 // Update the probability of a two-branch to be uncommon
rasbold@853 1824 void Block::update_uncommon_branch(Block* ub) {
rasbold@853 1825 int eidx = end_idx();
rasbold@853 1826 Node *n = _nodes[eidx]; // Get ending Node
rasbold@853 1827
rasbold@853 1828 int op = n->as_Mach()->ideal_Opcode();
rasbold@853 1829
rasbold@853 1830 assert(op == Op_CountedLoopEnd || op == Op_If, "must be a If");
rasbold@853 1831 assert(num_fall_throughs() == 2, "must be a two way branch block");
rasbold@853 1832
rasbold@853 1833 // Which successor is ub?
rasbold@853 1834 uint s;
rasbold@853 1835 for (s = 0; s <_num_succs; s++) {
rasbold@853 1836 if (_succs[s] == ub) break;
rasbold@853 1837 }
rasbold@853 1838 assert(s < 2, "uncommon successor must be found");
rasbold@853 1839
rasbold@853 1840 // If ub is the true path, make the proability small, else
rasbold@853 1841 // ub is the false path, and make the probability large
rasbold@853 1842 bool invert = (_nodes[s + eidx + 1]->Opcode() == Op_IfFalse);
rasbold@853 1843
rasbold@853 1844 // Get existing probability
rasbold@853 1845 float p = n->as_MachIf()->_prob;
rasbold@853 1846
rasbold@853 1847 if (invert) p = 1.0 - p;
rasbold@853 1848 if (p > PROB_MIN) {
rasbold@853 1849 p = PROB_MIN;
rasbold@853 1850 }
rasbold@853 1851 if (invert) p = 1.0 - p;
rasbold@853 1852
rasbold@853 1853 n->as_MachIf()->_prob = p;
rasbold@853 1854 }
rasbold@853 1855
duke@435 1856 //------------------------------update_succ_freq-------------------------------
twisti@1040 1857 // Update the appropriate frequency associated with block 'b', a successor of
duke@435 1858 // a block in this loop.
duke@435 1859 void CFGLoop::update_succ_freq(Block* b, float freq) {
duke@435 1860 if (b->_loop == this) {
duke@435 1861 if (b == head()) {
duke@435 1862 // back branch within the loop
duke@435 1863 // Do nothing now, the loop carried frequency will be
duke@435 1864 // adjust later in scale_freq().
duke@435 1865 } else {
duke@435 1866 // simple branch within the loop
duke@435 1867 b->_freq += freq;
duke@435 1868 }
duke@435 1869 } else if (!in_loop_nest(b)) {
duke@435 1870 // branch is exit from this loop
duke@435 1871 BlockProbPair bpp(b, freq);
duke@435 1872 _exits.append(bpp);
duke@435 1873 } else {
duke@435 1874 // branch into nested loop
duke@435 1875 CFGLoop* ch = b->_loop;
duke@435 1876 ch->_freq += freq;
duke@435 1877 }
duke@435 1878 }
duke@435 1879
duke@435 1880 //------------------------------in_loop_nest-----------------------------------
duke@435 1881 // Determine if block b is in the receiver's loop nest.
duke@435 1882 bool CFGLoop::in_loop_nest(Block* b) {
duke@435 1883 int depth = _depth;
duke@435 1884 CFGLoop* b_loop = b->_loop;
duke@435 1885 int b_depth = b_loop->_depth;
duke@435 1886 if (depth == b_depth) {
duke@435 1887 return true;
duke@435 1888 }
duke@435 1889 while (b_depth > depth) {
duke@435 1890 b_loop = b_loop->_parent;
duke@435 1891 b_depth = b_loop->_depth;
duke@435 1892 }
duke@435 1893 return b_loop == this;
duke@435 1894 }
duke@435 1895
duke@435 1896 //------------------------------scale_freq-------------------------------------
duke@435 1897 // Scale frequency of loops and blocks by trip counts from outer loops
duke@435 1898 // Do a top down traversal of loop tree (visit outer loops first.)
duke@435 1899 void CFGLoop::scale_freq() {
duke@435 1900 float loop_freq = _freq * trip_count();
duke@435 1901 for (int i = 0; i < _members.length(); i++) {
duke@435 1902 CFGElement* s = _members.at(i);
kvn@987 1903 float block_freq = s->_freq * loop_freq;
kvn@987 1904 if (block_freq < MIN_BLOCK_FREQUENCY) block_freq = MIN_BLOCK_FREQUENCY;
kvn@987 1905 s->_freq = block_freq;
duke@435 1906 }
duke@435 1907 CFGLoop* ch = _child;
duke@435 1908 while (ch != NULL) {
duke@435 1909 ch->scale_freq();
duke@435 1910 ch = ch->_sibling;
duke@435 1911 }
duke@435 1912 }
duke@435 1913
duke@435 1914 #ifndef PRODUCT
duke@435 1915 //------------------------------dump_tree--------------------------------------
duke@435 1916 void CFGLoop::dump_tree() const {
duke@435 1917 dump();
duke@435 1918 if (_child != NULL) _child->dump_tree();
duke@435 1919 if (_sibling != NULL) _sibling->dump_tree();
duke@435 1920 }
duke@435 1921
duke@435 1922 //------------------------------dump-------------------------------------------
duke@435 1923 void CFGLoop::dump() const {
duke@435 1924 for (int i = 0; i < _depth; i++) tty->print(" ");
duke@435 1925 tty->print("%s: %d trip_count: %6.0f freq: %6.0f\n",
duke@435 1926 _depth == 0 ? "Method" : "Loop", _id, trip_count(), _freq);
duke@435 1927 for (int i = 0; i < _depth; i++) tty->print(" ");
duke@435 1928 tty->print(" members:", _id);
duke@435 1929 int k = 0;
duke@435 1930 for (int i = 0; i < _members.length(); i++) {
duke@435 1931 if (k++ >= 6) {
duke@435 1932 tty->print("\n ");
duke@435 1933 for (int j = 0; j < _depth+1; j++) tty->print(" ");
duke@435 1934 k = 0;
duke@435 1935 }
duke@435 1936 CFGElement *s = _members.at(i);
duke@435 1937 if (s->is_block()) {
duke@435 1938 Block *b = s->as_Block();
duke@435 1939 tty->print(" B%d(%6.3f)", b->_pre_order, b->_freq);
duke@435 1940 } else {
duke@435 1941 CFGLoop* lp = s->as_CFGLoop();
duke@435 1942 tty->print(" L%d(%6.3f)", lp->_id, lp->_freq);
duke@435 1943 }
duke@435 1944 }
duke@435 1945 tty->print("\n");
duke@435 1946 for (int i = 0; i < _depth; i++) tty->print(" ");
duke@435 1947 tty->print(" exits: ");
duke@435 1948 k = 0;
duke@435 1949 for (int i = 0; i < _exits.length(); i++) {
duke@435 1950 if (k++ >= 7) {
duke@435 1951 tty->print("\n ");
duke@435 1952 for (int j = 0; j < _depth+1; j++) tty->print(" ");
duke@435 1953 k = 0;
duke@435 1954 }
duke@435 1955 Block *blk = _exits.at(i).get_target();
duke@435 1956 float prob = _exits.at(i).get_prob();
duke@435 1957 tty->print(" ->%d@%d%%", blk->_pre_order, (int)(prob*100));
duke@435 1958 }
duke@435 1959 tty->print("\n");
duke@435 1960 }
duke@435 1961 #endif

mercurial