src/share/vm/opto/gcm.cpp

Sat, 24 Oct 2020 16:43:47 +0800

author
aoqi
date
Sat, 24 Oct 2020 16:43:47 +0800
changeset 10015
eb7ce841ccec
parent 9931
fd44df5e3bc3
parent 9953
8a8f679915aa
permissions
-rw-r--r--

Merge

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

mercurial