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