hotspot/src/share/vm/opto/block.hpp
author kvn
Tue, 03 Aug 2010 15:55:03 -0700
changeset 6180 53c1bf468c81
parent 5547 f4b087cbb361
child 7397 5b173b4ca846
permissions -rw-r--r--
6973963: SEGV in ciBlock::start_bci() with EA Summary: Added more checks into ResourceObj and growableArray to verify correctness of allocation type. Reviewed-by: never, coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
diff changeset
     2
 * Copyright (c) 1997, 2009, 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: 3261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 3261
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: 3261
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
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// Optimization - Graph Style
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
class Block;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
class CFGLoop;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
class MachCallNode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
class Matcher;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
class RootNode;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
class VectorSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
struct Tarjan;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//------------------------------Block_Array------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// Map dense integer indices to Blocks.  Uses classic doubling-array trick.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// Abstractly provides an infinite array of Block*'s, initialized to NULL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// Note that the constructor just zeros things, and since I use Arena
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// allocation I do not need a destructor to reclaim storage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
class Block_Array : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  uint _size;                   // allocated size, as opposed to formal limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  debug_only(uint _limit;)      // limit to formal domain
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  Block **_blocks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  void grow( uint i );          // Grow array node to fit
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  Arena *_arena;                // Arena to allocate in
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  Block_Array(Arena *a) : _arena(a), _size(OptoBlockListSize) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    debug_only(_limit=0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    _blocks = NEW_ARENA_ARRAY( a, Block *, OptoBlockListSize );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    for( int i = 0; i < OptoBlockListSize; i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
      _blocks[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  Block *lookup( uint i ) const // Lookup, or NULL for not mapped
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  { return (i<Max()) ? _blocks[i] : (Block*)NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  Block *operator[] ( uint i ) const // Lookup, or assert for not mapped
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  { assert( i < Max(), "oob" ); return _blocks[i]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  // Extend the mapping: index i maps to Block *n.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  void map( uint i, Block *n ) { if( i>=Max() ) grow(i); _blocks[i] = n; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  uint Max() const { debug_only(return _limit); return _size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
class Block_List : public Block_Array {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  uint _cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  Block_List() : Block_Array(Thread::current()->resource_area()), _cnt(0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  void push( Block *b ) { map(_cnt++,b); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  Block *pop() { return _blocks[--_cnt]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  Block *rpop() { Block *b = _blocks[0]; _blocks[0]=_blocks[--_cnt]; return b;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  void remove( uint i );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  void insert( uint i, Block *n );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  uint size() const { return _cnt; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  void reset() { _cnt = 0; }
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
    78
  void print();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
class CFGElement : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  float _freq; // Execution frequency (estimate)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  CFGElement() : _freq(0.0f) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  virtual bool is_block() { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  virtual bool is_loop()  { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  Block*   as_Block() { assert(is_block(), "must be block"); return (Block*)this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  CFGLoop* as_CFGLoop()  { assert(is_loop(),  "must be loop");  return (CFGLoop*)this;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
//------------------------------Block------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
// This class defines a Basic Block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
// Basic blocks are used during the output routines, and are not used during
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
// any optimization pass.  They are created late in the game.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
class Block : public CFGElement {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // Nodes in this block, in order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  Node_List _nodes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // Basic blocks have a Node which defines Control for all Nodes pinned in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // this block.  This Node is a RegionNode.  Exception-causing Nodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // (division, subroutines) and Phi functions are always pinned.  Later,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  // every Node will get pinned to some block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  Node *head() const { return _nodes[0]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  // CAUTION: num_preds() is ONE based, so that predecessor numbers match
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // input edges to Regions and Phis.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  uint num_preds() const { return head()->req(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  Node *pred(uint i) const { return head()->in(i); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  // Array of successor blocks, same size as projs array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  Block_Array _succs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  // Basic blocks have some number of Nodes which split control to all
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  // following blocks.  These Nodes are always Projections.  The field in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // the Projection and the block-ending Node determine which Block follows.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  uint _num_succs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // Basic blocks also carry all sorts of good old fashioned DFS information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  // used to find loops, loop nesting depth, dominators, etc.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  uint _pre_order;              // Pre-order DFS number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // Dominator tree
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  uint _dom_depth;              // Depth in dominator tree for fast LCA
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  Block* _idom;                 // Immediate dominator block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  CFGLoop *_loop;               // Loop to which this block belongs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  uint _rpo;                    // Number in reverse post order walk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  virtual bool is_block() { return true; }
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   133
  float succ_prob(uint i);      // return probability of i'th successor
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   134
  int num_fall_throughs();      // How many fall-through candidate this block has
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   135
  void update_uncommon_branch(Block* un); // Lower branch prob to uncommon code
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   136
  bool succ_fall_through(uint i); // Is successor "i" is a fall-through candidate
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   137
  Block* lone_fall_through();   // Return lone fall-through Block or null
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  Block* dom_lca(Block* that);  // Compute LCA in dominator tree.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  bool dominates(Block* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    int dom_diff = this->_dom_depth - that->_dom_depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    if (dom_diff > 0)  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    for (; dom_diff < 0; dom_diff++)  that = that->_idom;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    return this == that;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  // Report the alignment required by this block.  Must be a power of 2.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // The previous block will insert nops to get this alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  uint code_alignment();
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   152
  uint compute_loop_alignment();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  // BLOCK_FREQUENCY is a sentinel to mark uses of constant block frequencies.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // It is currently also used to scale such frequencies relative to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  // FreqCountInvocations relative to the old value of 1500.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
#define BLOCK_FREQUENCY(f) ((f * (float) 1500) / FreqCountInvocations)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // Register Pressure (estimate) for Splitting heuristic
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  uint _reg_pressure;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  uint _ihrp_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  uint _freg_pressure;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  uint _fhrp_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  // Mark and visited bits for an LCA calculation in insert_anti_dependences.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Since they hold unique node indexes, they do not need reinitialization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  node_idx_t _raise_LCA_mark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  void    set_raise_LCA_mark(node_idx_t x)    { _raise_LCA_mark = x; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  node_idx_t  raise_LCA_mark() const          { return _raise_LCA_mark; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  node_idx_t _raise_LCA_visited;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  void    set_raise_LCA_visited(node_idx_t x) { _raise_LCA_visited = x; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  node_idx_t  raise_LCA_visited() const       { return _raise_LCA_visited; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
  // Estimated size in bytes of first instructions in a loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  uint _first_inst_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  uint first_inst_size() const     { return _first_inst_size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  void set_first_inst_size(uint s) { _first_inst_size = s; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  // Compute the size of first instructions in this block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  uint compute_first_inst_size(uint& sum_size, uint inst_cnt, PhaseRegAlloc* ra);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  // Compute alignment padding if the block needs it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // Align a loop if loop's padding is less or equal to padding limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  // or the size of first instructions in the loop > padding.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  uint alignment_padding(int current_offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    int block_alignment = code_alignment();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    int max_pad = block_alignment-relocInfo::addr_unit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    if( max_pad > 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      assert(is_power_of_2(max_pad+relocInfo::addr_unit()), "");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      int current_alignment = current_offset & max_pad;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
      if( current_alignment != 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
        uint padding = (block_alignment-current_alignment) & max_pad;
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   193
        if( has_loop_alignment() &&
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   194
            padding > (uint)MaxLoopPad &&
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   195
            first_inst_size() <= padding ) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   196
          return 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
        }
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   198
        return padding;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // Connector blocks. Connector blocks are basic blocks devoid of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  // instructions, but may have relevant non-instruction Nodes, such as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  // Phis or MergeMems. Such blocks are discovered and marked during the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  // RemoveEmpty phase, and elided during Output.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  bool _connector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  void set_connector() { _connector = true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  bool is_connector() const { return _connector; };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   212
  // Loop_alignment will be set for blocks which are at the top of loops.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   213
  // The block layout pass may rotate loops such that the loop head may not
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   214
  // be the sequentially first block of the loop encountered in the linear
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   215
  // list of blocks.  If the layout pass is not run, loop alignment is set
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   216
  // for each block which is the head of a loop.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   217
  uint _loop_alignment;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   218
  void set_loop_alignment(Block *loop_top) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   219
    uint new_alignment = loop_top->compute_loop_alignment();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   220
    if (new_alignment > _loop_alignment) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   221
      _loop_alignment = new_alignment;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   222
    }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   223
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   224
  uint loop_alignment() const { return _loop_alignment; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   225
  bool has_loop_alignment() const { return loop_alignment() > 0; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   226
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // Create a new Block with given head Node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  // Creates the (empty) predecessor arrays.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  Block( Arena *a, Node *headnode )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    : CFGElement(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
      _nodes(a),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      _succs(a),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      _num_succs(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
      _pre_order(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      _idom(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      _loop(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      _reg_pressure(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      _ihrp_index(1),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      _freg_pressure(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      _fhrp_index(1),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      _raise_LCA_mark(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      _raise_LCA_visited(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      _first_inst_size(999999),
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   244
      _connector(false),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   245
      _loop_alignment(0) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    _nodes.push(headnode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  // Index of 'end' Node
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  uint end_idx() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    // %%%%% add a proj after every goto
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    // so (last->is_block_proj() != last) always, then simplify this code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    // This will not give correct end_idx for block 0 when it only contains root.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    int last_idx = _nodes.size() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    Node *last  = _nodes[last_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
    assert(last->is_block_proj() == last || last->is_block_proj() == _nodes[last_idx - _num_succs], "");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    return (last->is_block_proj() == last) ? last_idx : (last_idx - _num_succs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  // Basic blocks have a Node which ends them.  This Node determines which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  // basic block follows this one in the program flow.  This Node is either an
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  // IfNode, a GotoNode, a JmpNode, or a ReturnNode.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  Node *end() const { return _nodes[end_idx()]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  // Add an instruction to an existing block.  It must go after the head
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  // instruction and before the end instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  void add_inst( Node *n ) { _nodes.insert(end_idx(),n); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  // Find node in block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  uint find_node( const Node *n ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  // Find and remove n from block list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  void find_remove( const Node *n );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  // Schedule a call next in the block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  uint sched_call(Matcher &matcher, Block_Array &bbs, uint node_cnt, Node_List &worklist, int *ready_cnt, MachCallNode *mcall, VectorSet &next_call);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  // Perform basic-block local scheduling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  Node *select(PhaseCFG *cfg, Node_List &worklist, int *ready_cnt, VectorSet &next_call, uint sched_slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  void set_next_call( Node *n, VectorSet &next_call, Block_Array &bbs );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  void needed_for_next_call(Node *this_call, VectorSet &next_call, Block_Array &bbs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  bool schedule_local(PhaseCFG *cfg, Matcher &m, int *ready_cnt, VectorSet &next_call);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  // Cleanup if any code lands between a Call and his Catch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  void call_catch_cleanup(Block_Array &bbs);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  // Detect implicit-null-check opportunities.  Basically, find NULL checks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  // with suitable memory ops nearby.  Use the memory op to do the NULL check.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  // I can generate a memory op if there is not one nearby.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  void implicit_null_check(PhaseCFG *cfg, Node *proj, Node *val, int allowed_reasons);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  // Return the empty status of a block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  enum { not_empty, empty_with_goto, completely_empty };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  int is_Empty() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  // Forward through connectors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  Block* non_connector() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    Block* s = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    while (s->is_connector()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
      s = s->_succs[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    return s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   301
  // Return true if b is a successor of this block
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   302
  bool has_successor(Block* b) const {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   303
    for (uint i = 0; i < _num_succs; i++ ) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   304
      if (non_connector_successor(i) == b) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   305
        return true;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   306
      }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   307
    }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   308
    return false;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   309
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   310
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  // Successor block, after forwarding through connectors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  Block* non_connector_successor(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
    return _succs[i]->non_connector();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  // Examine block's code shape to predict if it is not commonly executed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  bool has_uncommon_code() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  // Use frequency calculations and code shape to predict if the block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  // is uncommon.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  bool is_uncommon( Block_Array &bbs ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  // Debugging print of basic block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  void dump_bidx(const Block* orig) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  void dump_pred(const Block_Array *bbs, Block* orig) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  void dump_head( const Block_Array *bbs ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  void dump( ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  void dump( const Block_Array *bbs ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
//------------------------------PhaseCFG---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
// Build an array of Basic Block pointers, one per Node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
class PhaseCFG : public Phase {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  // Build a proper looking cfg.  Return count of basic blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  uint build_cfg();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
  // Perform DFS search.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  // Setup 'vertex' as DFS to vertex mapping.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
  // Setup 'semi' as vertex to DFS mapping.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  // Set 'parent' to DFS parent.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  uint DFS( Tarjan *tarjan );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  // Helper function to insert a node into a block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  void schedule_node_into_block( Node *n, Block *b );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
2130
f935aa562118 6811267: Fix for 6809798 broke linux build
kvn
parents: 2127
diff changeset
   350
  void replace_block_proj_ctrl( Node *n );
2127
268ea58ed775 6809798: SafePointScalarObject node placed into incorrect block during GCM
kvn
parents: 1623
diff changeset
   351
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  // Set the basic block for pinned Nodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  void schedule_pinned_nodes( VectorSet &visited );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  // I'll need a few machine-specific GotoNodes.  Clone from this one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  MachNode *_goto;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  Block* insert_anti_dependences(Block* LCA, Node* load, bool verify = false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  void verify_anti_dependences(Block* LCA, Node* load) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    assert(LCA == _bbs[load->_idx], "should already be scheduled");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    insert_anti_dependences(LCA, load, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  PhaseCFG( Arena *a, RootNode *r, Matcher &m );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  uint _num_blocks;             // Count of basic blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  Block_List _blocks;           // List of basic blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  RootNode *_root;              // Root of whole program
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
  Block_Array _bbs;             // Map Nodes to owning Basic Block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
  Block *_broot;                // Basic block of root
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  uint _rpo_ctr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  CFGLoop* _root_loop;
2340
cb47f8209cd8 6810845: Performance regression in mpegaudio on x64
kvn
parents: 2131
diff changeset
   374
  float _outer_loop_freq;       // Outmost loop frequency
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  // Per node latency estimation, valid only during GCM
6180
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   377
  GrowableArray<uint> *_node_latency;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  bool _trace_opto_pipelining;  // tracing flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
3186
11ba3d09bd0e 6840775: Multiple JVM crashes seen with 1.6.0_10 through 1.6.0_14
kvn
parents: 2340
diff changeset
   383
#ifdef ASSERT
11ba3d09bd0e 6840775: Multiple JVM crashes seen with 1.6.0_10 through 1.6.0_14
kvn
parents: 2340
diff changeset
   384
  Unique_Node_List _raw_oops;
11ba3d09bd0e 6840775: Multiple JVM crashes seen with 1.6.0_10 through 1.6.0_14
kvn
parents: 2340
diff changeset
   385
#endif
11ba3d09bd0e 6840775: Multiple JVM crashes seen with 1.6.0_10 through 1.6.0_14
kvn
parents: 2340
diff changeset
   386
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  // Build dominators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  void Dominators();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  // Estimate block frequencies based on IfNode probabilities
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  void Estimate_Block_Frequency();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  // Global Code Motion.  See Click's PLDI95 paper.  Place Nodes in specific
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  // basic blocks; i.e. _bbs now maps _idx for all Nodes to some Block.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  void GlobalCodeMotion( Matcher &m, uint unique, Node_List &proj_list );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  // Compute the (backwards) latency of a node from the uses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
  void latency_from_uses(Node *n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  // Compute the (backwards) latency of a node from a single use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
  int latency_from_use(Node *n, const Node *def, Node *use);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  // Compute the (backwards) latency of a node from the uses of this instruction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  void partial_latency_of_defs(Node *n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  // Schedule Nodes early in their basic blocks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  bool schedule_early(VectorSet &visited, Node_List &roots);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  // For each node, find the latest block it can be scheduled into
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
  // and then select the cheapest block between the latest and earliest
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  // block to place the node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  void schedule_late(VectorSet &visited, Node_List &stack);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
  // Pick a block between early and late that is a cheaper alternative
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
  // to late. Helper for schedule_late.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  Block* hoist_to_cheaper_block(Block* LCA, Block* early, Node* self);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  // Compute the instruction global latency with a backwards walk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  void ComputeLatenciesBackwards(VectorSet &visited, Node_List &stack);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   421
  // Set loop alignment
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   422
  void set_loop_alignment();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   423
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  // Remove empty basic blocks
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   425
  void remove_empty();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   426
  void fixup_flow();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   427
  bool move_to_next(Block* bx, uint b_index);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   428
  void move_to_end(Block* bx, uint b_index);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   429
  void insert_goto_at(uint block_no, uint succ_no);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  // Check for NeverBranch at block end.  This needs to become a GOTO to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  // true target.  NeverBranch are treated as a conditional branch that always
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  // goes the same direction for most of the optimizer and are used to give a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
  // fake exit path to infinite loops.  At this late stage they need to turn
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  // into Goto's so that when you enter the infinite loop you indeed hang.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  void convert_NeverBranch_to_Goto(Block *b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  CFGLoop* create_loop_tree();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
  // Insert a node into a block, and update the _bbs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  void insert( Block *b, uint idx, Node *n ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
    b->_nodes.insert( idx, n );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
    _bbs.map( n->_idx, b );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  bool trace_opto_pipelining() const { return _trace_opto_pipelining; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  // Debugging print of CFG
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  void dump( ) const;           // CFG only
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  void _dump_cfg( const Node *end, VectorSet &visited  ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  void verify() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  void dump_headers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
  bool trace_opto_pipelining() const { return false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   460
//------------------------------UnionFind--------------------------------------
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
// Map Block indices to a block-index for a cfg-cover.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
// Array lookup in the optimized case.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
class UnionFind : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  uint _cnt, _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  uint* _indices;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
  ReallocMark _nesting;  // assertion check for reallocations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  UnionFind( uint max );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  void reset( uint max );  // Reset to identity map for [0..max]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  uint lookup( uint nidx ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
    return _indices[nidx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
  uint operator[] (uint nidx) const { return lookup(nidx); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  void map( uint from_idx, uint to_idx ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    assert( from_idx < _cnt, "oob" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
    _indices[from_idx] = to_idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
  void extend( uint from_idx, uint to_idx );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
  uint Size() const { return _cnt; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  uint Find( uint idx ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
    assert( idx < 65536, "Must fit into uint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    uint uf_idx = lookup(idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    return (uf_idx == idx) ? uf_idx : Find_compress(idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  uint Find_compress( uint idx );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  uint Find_const( uint idx ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  void Union( uint idx1, uint idx2 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
//----------------------------BlockProbPair---------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
// Ordered pair of Node*.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
class BlockProbPair VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  Block* _target;      // block target
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  float  _prob;        // probability of edge to block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  BlockProbPair() : _target(NULL), _prob(0.0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  BlockProbPair(Block* b, float p) : _target(b), _prob(p) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  Block* get_target() const { return _target; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  float get_prob() const { return _prob; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
//------------------------------CFGLoop-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
class CFGLoop : public CFGElement {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  int _id;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  int _depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  CFGLoop *_parent;      // root of loop tree is the method level "pseudo" loop, it's parent is null
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  CFGLoop *_sibling;     // null terminated list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  CFGLoop *_child;       // first child, use child's sibling to visit all immediately nested loops
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  GrowableArray<CFGElement*> _members; // list of members of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  GrowableArray<BlockProbPair> _exits; // list of successor blocks and their probabilities
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  float _exit_prob;       // probability any loop exit is taken on a single loop iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  void update_succ_freq(Block* b, float freq);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  CFGLoop(int id) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
    CFGElement(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
    _id(id),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    _depth(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
    _parent(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    _sibling(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    _child(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    _exit_prob(1.0f) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
  CFGLoop* parent() { return _parent; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
  void push_pred(Block* blk, int i, Block_List& worklist, Block_Array& node_to_blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  void add_member(CFGElement *s) { _members.push(s); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  void add_nested_loop(CFGLoop* cl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  Block* head() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    assert(_members.at(0)->is_block(), "head must be a block");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
    Block* hd = _members.at(0)->as_Block();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
    assert(hd->_loop == this, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    assert(hd->head()->is_Loop(), "must begin with loop head node");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
    return hd;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  Block* backedge_block(); // Return the block on the backedge of the loop (else NULL)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  void compute_loop_depth(int depth);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  void compute_freq(); // compute frequency with loop assuming head freq 1.0f
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  void scale_freq();   // scale frequency by loop trip count (including outer loops)
2340
cb47f8209cd8 6810845: Performance regression in mpegaudio on x64
kvn
parents: 2131
diff changeset
   545
  float outer_loop_freq() const; // frequency of outer loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  bool in_loop_nest(Block* b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
  float trip_count() const { return 1.0f / _exit_prob; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
  virtual bool is_loop()  { return true; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  int id() { return _id; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
  void dump( ) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
  void dump_tree() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
};
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   556
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   557
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   558
//----------------------------------CFGEdge------------------------------------
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   559
// A edge between two basic blocks that will be embodied by a branch or a
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   560
// fall-through.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   561
class CFGEdge : public ResourceObj {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   562
 private:
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   563
  Block * _from;        // Source basic block
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   564
  Block * _to;          // Destination basic block
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   565
  float _freq;          // Execution frequency (estimate)
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   566
  int   _state;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   567
  bool  _infrequent;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   568
  int   _from_pct;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   569
  int   _to_pct;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   570
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   571
  // Private accessors
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   572
  int  from_pct() const { return _from_pct; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   573
  int  to_pct()   const { return _to_pct;   }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   574
  int  from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   575
  int  to_infrequent()   const { return to_pct()   < BlockLayoutMinDiamondPercentage; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   576
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   577
 public:
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   578
  enum {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   579
    open,               // initial edge state; unprocessed
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   580
    connected,          // edge used to connect two traces together
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   581
    interior            // edge is interior to trace (could be backedge)
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   582
  };
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   583
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   584
  CFGEdge(Block *from, Block *to, float freq, int from_pct, int to_pct) :
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   585
    _from(from), _to(to), _freq(freq),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   586
    _from_pct(from_pct), _to_pct(to_pct), _state(open) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   587
    _infrequent = from_infrequent() || to_infrequent();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   588
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   589
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   590
  float  freq() const { return _freq; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   591
  Block* from() const { return _from; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   592
  Block* to  () const { return _to;   }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   593
  int  infrequent() const { return _infrequent; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   594
  int state() const { return _state; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   595
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   596
  void set_state(int state) { _state = state; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   597
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   598
#ifndef PRODUCT
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   599
  void dump( ) const;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   600
#endif
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   601
};
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   602
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   603
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   604
//-----------------------------------Trace-------------------------------------
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   605
// An ordered list of basic blocks.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   606
class Trace : public ResourceObj {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   607
 private:
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   608
  uint _id;             // Unique Trace id (derived from initial block)
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   609
  Block ** _next_list;  // Array mapping index to next block
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   610
  Block ** _prev_list;  // Array mapping index to previous block
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   611
  Block * _first;       // First block in the trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   612
  Block * _last;        // Last block in the trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   613
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   614
  // Return the block that follows "b" in the trace.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   615
  Block * next(Block *b) const { return _next_list[b->_pre_order]; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   616
  void set_next(Block *b, Block *n) const { _next_list[b->_pre_order] = n; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   617
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2130
diff changeset
   618
  // Return the block that precedes "b" in the trace.
1498
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   619
  Block * prev(Block *b) const { return _prev_list[b->_pre_order]; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   620
  void set_prev(Block *b, Block *p) const { _prev_list[b->_pre_order] = p; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   621
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   622
  // We've discovered a loop in this trace. Reset last to be "b", and first as
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   623
  // the block following "b
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   624
  void break_loop_after(Block *b) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   625
    _last = b;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   626
    _first = next(b);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   627
    set_prev(_first, NULL);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   628
    set_next(_last, NULL);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   629
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   630
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   631
 public:
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   632
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   633
  Trace(Block *b, Block **next_list, Block **prev_list) :
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   634
    _first(b),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   635
    _last(b),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   636
    _next_list(next_list),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   637
    _prev_list(prev_list),
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   638
    _id(b->_pre_order) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   639
    set_next(b, NULL);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   640
    set_prev(b, NULL);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   641
  };
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   642
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   643
  // Return the id number
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   644
  uint id() const { return _id; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   645
  void set_id(uint id) { _id = id; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   646
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   647
  // Return the first block in the trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   648
  Block * first_block() const { return _first; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   649
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   650
  // Return the last block in the trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   651
  Block * last_block() const { return _last; }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   652
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   653
  // Insert a trace in the middle of this one after b
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   654
  void insert_after(Block *b, Trace *tr) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   655
    set_next(tr->last_block(), next(b));
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   656
    if (next(b) != NULL) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   657
      set_prev(next(b), tr->last_block());
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   658
    }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   659
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   660
    set_next(b, tr->first_block());
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   661
    set_prev(tr->first_block(), b);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   662
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   663
    if (b == _last) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   664
      _last = tr->last_block();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   665
    }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   666
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   667
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   668
  void insert_before(Block *b, Trace *tr) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   669
    Block *p = prev(b);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   670
    assert(p != NULL, "use append instead");
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   671
    insert_after(p, tr);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   672
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   673
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   674
  // Append another trace to this one.
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   675
  void append(Trace *tr) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   676
    insert_after(_last, tr);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   677
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   678
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   679
  // Append a block at the end of this trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   680
  void append(Block *b) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   681
    set_next(_last, b);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   682
    set_prev(b, _last);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   683
    _last = b;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   684
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   685
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   686
  // Adjust the the blocks in this trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   687
  void fixup_blocks(PhaseCFG &cfg);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   688
  bool backedge(CFGEdge *e);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   689
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   690
#ifndef PRODUCT
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   691
  void dump( ) const;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   692
#endif
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   693
};
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   694
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   695
//------------------------------PhaseBlockLayout-------------------------------
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   696
// Rearrange blocks into some canonical order, based on edges and their frequencies
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   697
class PhaseBlockLayout : public Phase {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   698
  PhaseCFG &_cfg;               // Control flow graph
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   699
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   700
  GrowableArray<CFGEdge *> *edges;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   701
  Trace **traces;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   702
  Block **next;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   703
  Block **prev;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   704
  UnionFind *uf;
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   705
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   706
  // Given a block, find its encompassing Trace
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   707
  Trace * trace(Block *b) {
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   708
    return traces[uf->Find_compress(b->_pre_order)];
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   709
  }
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   710
 public:
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   711
  PhaseBlockLayout(PhaseCFG &cfg);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   712
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   713
  void find_edges();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   714
  void grow_traces();
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   715
  void merge_traces(bool loose_connections);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   716
  void reorder_traces(int count);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   717
  void union_traces(Trace* from, Trace* to);
346bf226078e 6743900: frequency based block layout
rasbold
parents: 1
diff changeset
   718
};