src/share/vm/opto/block.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial