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