annotate src/share/vm/opto/block.hpp @ 642:ec59443af135

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