src/share/vm/opto/gcm.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8604
04d83ba48607
child 9448
73d689add964
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial