src/share/vm/opto/block.hpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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.
kvn@8615 188
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
aoqi@0 196 // Report the alignment required by this block. Must be a power of 2.
aoqi@0 197 // The previous block will insert nops to get this alignment.
aoqi@0 198 uint code_alignment();
aoqi@0 199 uint compute_loop_alignment();
aoqi@0 200
aoqi@0 201 // BLOCK_FREQUENCY is a sentinel to mark uses of constant block frequencies.
aoqi@0 202 // It is currently also used to scale such frequencies relative to
aoqi@0 203 // FreqCountInvocations relative to the old value of 1500.
aoqi@0 204 #define BLOCK_FREQUENCY(f) ((f * (float) 1500) / FreqCountInvocations)
aoqi@0 205
aoqi@0 206 // Register Pressure (estimate) for Splitting heuristic
aoqi@0 207 uint _reg_pressure;
aoqi@0 208 uint _ihrp_index;
aoqi@0 209 uint _freg_pressure;
aoqi@0 210 uint _fhrp_index;
aoqi@0 211
aoqi@0 212 // Mark and visited bits for an LCA calculation in insert_anti_dependences.
aoqi@0 213 // Since they hold unique node indexes, they do not need reinitialization.
aoqi@0 214 node_idx_t _raise_LCA_mark;
aoqi@0 215 void set_raise_LCA_mark(node_idx_t x) { _raise_LCA_mark = x; }
aoqi@0 216 node_idx_t raise_LCA_mark() const { return _raise_LCA_mark; }
aoqi@0 217 node_idx_t _raise_LCA_visited;
aoqi@0 218 void set_raise_LCA_visited(node_idx_t x) { _raise_LCA_visited = x; }
aoqi@0 219 node_idx_t raise_LCA_visited() const { return _raise_LCA_visited; }
aoqi@0 220
aoqi@0 221 // Estimated size in bytes of first instructions in a loop.
aoqi@0 222 uint _first_inst_size;
aoqi@0 223 uint first_inst_size() const { return _first_inst_size; }
aoqi@0 224 void set_first_inst_size(uint s) { _first_inst_size = s; }
aoqi@0 225
aoqi@0 226 // Compute the size of first instructions in this block.
aoqi@0 227 uint compute_first_inst_size(uint& sum_size, uint inst_cnt, PhaseRegAlloc* ra);
aoqi@0 228
aoqi@0 229 // Compute alignment padding if the block needs it.
aoqi@0 230 // Align a loop if loop's padding is less or equal to padding limit
aoqi@0 231 // or the size of first instructions in the loop > padding.
aoqi@0 232 uint alignment_padding(int current_offset) {
aoqi@0 233 int block_alignment = code_alignment();
aoqi@0 234 int max_pad = block_alignment-relocInfo::addr_unit();
aoqi@0 235 if( max_pad > 0 ) {
aoqi@0 236 assert(is_power_of_2(max_pad+relocInfo::addr_unit()), "");
aoqi@0 237 int current_alignment = current_offset & max_pad;
aoqi@0 238 if( current_alignment != 0 ) {
aoqi@0 239 uint padding = (block_alignment-current_alignment) & max_pad;
aoqi@0 240 if( has_loop_alignment() &&
aoqi@0 241 padding > (uint)MaxLoopPad &&
aoqi@0 242 first_inst_size() <= padding ) {
aoqi@0 243 return 0;
aoqi@0 244 }
aoqi@0 245 return padding;
aoqi@0 246 }
aoqi@0 247 }
aoqi@0 248 return 0;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 // Connector blocks. Connector blocks are basic blocks devoid of
aoqi@0 252 // instructions, but may have relevant non-instruction Nodes, such as
aoqi@0 253 // Phis or MergeMems. Such blocks are discovered and marked during the
aoqi@0 254 // RemoveEmpty phase, and elided during Output.
aoqi@0 255 bool _connector;
aoqi@0 256 void set_connector() { _connector = true; }
aoqi@0 257 bool is_connector() const { return _connector; };
aoqi@0 258
aoqi@0 259 // Loop_alignment will be set for blocks which are at the top of loops.
aoqi@0 260 // The block layout pass may rotate loops such that the loop head may not
aoqi@0 261 // be the sequentially first block of the loop encountered in the linear
aoqi@0 262 // list of blocks. If the layout pass is not run, loop alignment is set
aoqi@0 263 // for each block which is the head of a loop.
aoqi@0 264 uint _loop_alignment;
aoqi@0 265 void set_loop_alignment(Block *loop_top) {
aoqi@0 266 uint new_alignment = loop_top->compute_loop_alignment();
aoqi@0 267 if (new_alignment > _loop_alignment) {
aoqi@0 268 _loop_alignment = new_alignment;
aoqi@0 269 }
aoqi@0 270 }
aoqi@0 271 uint loop_alignment() const { return _loop_alignment; }
aoqi@0 272 bool has_loop_alignment() const { return loop_alignment() > 0; }
aoqi@0 273
aoqi@0 274 // Create a new Block with given head Node.
aoqi@0 275 // Creates the (empty) predecessor arrays.
aoqi@0 276 Block( Arena *a, Node *headnode )
aoqi@0 277 : CFGElement(),
aoqi@0 278 _nodes(a),
aoqi@0 279 _succs(a),
aoqi@0 280 _num_succs(0),
aoqi@0 281 _pre_order(0),
aoqi@0 282 _idom(0),
aoqi@0 283 _loop(NULL),
aoqi@0 284 _reg_pressure(0),
aoqi@0 285 _ihrp_index(1),
aoqi@0 286 _freg_pressure(0),
aoqi@0 287 _fhrp_index(1),
aoqi@0 288 _raise_LCA_mark(0),
aoqi@0 289 _raise_LCA_visited(0),
aoqi@0 290 _first_inst_size(999999),
aoqi@0 291 _connector(false),
aoqi@0 292 _loop_alignment(0) {
aoqi@0 293 _nodes.push(headnode);
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 // Index of 'end' Node
aoqi@0 297 uint end_idx() const {
aoqi@0 298 // %%%%% add a proj after every goto
aoqi@0 299 // so (last->is_block_proj() != last) always, then simplify this code
aoqi@0 300 // This will not give correct end_idx for block 0 when it only contains root.
aoqi@0 301 int last_idx = _nodes.size() - 1;
aoqi@0 302 Node *last = _nodes[last_idx];
aoqi@0 303 assert(last->is_block_proj() == last || last->is_block_proj() == _nodes[last_idx - _num_succs], "");
aoqi@0 304 return (last->is_block_proj() == last) ? last_idx : (last_idx - _num_succs);
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 // Basic blocks have a Node which ends them. This Node determines which
aoqi@0 308 // basic block follows this one in the program flow. This Node is either an
aoqi@0 309 // IfNode, a GotoNode, a JmpNode, or a ReturnNode.
aoqi@0 310 Node *end() const { return _nodes[end_idx()]; }
aoqi@0 311
aoqi@0 312 // Add an instruction to an existing block. It must go after the head
aoqi@0 313 // instruction and before the end instruction.
aoqi@0 314 void add_inst( Node *n ) { insert_node(n, end_idx()); }
aoqi@0 315 // Find node in block. Fails if node not in block.
aoqi@0 316 uint find_node( const Node *n ) const;
aoqi@0 317 // Find and remove n from block list
aoqi@0 318 void find_remove( const Node *n );
aoqi@0 319 // Check wether the node is in the block.
aoqi@0 320 bool contains (const Node *n) const;
aoqi@0 321
aoqi@0 322 // Return the empty status of a block
aoqi@0 323 enum { not_empty, empty_with_goto, completely_empty };
aoqi@0 324 int is_Empty() const;
aoqi@0 325
aoqi@0 326 // Forward through connectors
aoqi@0 327 Block* non_connector() {
aoqi@0 328 Block* s = this;
aoqi@0 329 while (s->is_connector()) {
aoqi@0 330 s = s->_succs[0];
aoqi@0 331 }
aoqi@0 332 return s;
aoqi@0 333 }
aoqi@0 334
aoqi@0 335 // Return true if b is a successor of this block
aoqi@0 336 bool has_successor(Block* b) const {
aoqi@0 337 for (uint i = 0; i < _num_succs; i++ ) {
aoqi@0 338 if (non_connector_successor(i) == b) {
aoqi@0 339 return true;
aoqi@0 340 }
aoqi@0 341 }
aoqi@0 342 return false;
aoqi@0 343 }
aoqi@0 344
aoqi@0 345 // Successor block, after forwarding through connectors
aoqi@0 346 Block* non_connector_successor(int i) const {
aoqi@0 347 return _succs[i]->non_connector();
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 // Examine block's code shape to predict if it is not commonly executed.
aoqi@0 351 bool has_uncommon_code() const;
aoqi@0 352
aoqi@0 353 #ifndef PRODUCT
aoqi@0 354 // Debugging print of basic block
aoqi@0 355 void dump_bidx(const Block* orig, outputStream* st = tty) const;
aoqi@0 356 void dump_pred(const PhaseCFG* cfg, Block* orig, outputStream* st = tty) const;
aoqi@0 357 void dump_head(const PhaseCFG* cfg, outputStream* st = tty) const;
aoqi@0 358 void dump() const;
aoqi@0 359 void dump(const PhaseCFG* cfg) const;
aoqi@0 360 #endif
aoqi@0 361 };
aoqi@0 362
aoqi@0 363
aoqi@0 364 //------------------------------PhaseCFG---------------------------------------
aoqi@0 365 // Build an array of Basic Block pointers, one per Node.
aoqi@0 366 class PhaseCFG : public Phase {
aoqi@0 367 friend class VMStructs;
aoqi@0 368 private:
aoqi@0 369
aoqi@0 370 // Root of whole program
aoqi@0 371 RootNode* _root;
aoqi@0 372
aoqi@0 373 // The block containing the root node
aoqi@0 374 Block* _root_block;
aoqi@0 375
aoqi@0 376 // List of basic blocks that are created during CFG creation
aoqi@0 377 Block_List _blocks;
aoqi@0 378
aoqi@0 379 // Count of basic blocks
aoqi@0 380 uint _number_of_blocks;
aoqi@0 381
aoqi@0 382 // Arena for the blocks to be stored in
aoqi@0 383 Arena* _block_arena;
aoqi@0 384
aoqi@0 385 // The matcher for this compilation
aoqi@0 386 Matcher& _matcher;
aoqi@0 387
aoqi@0 388 // Map nodes to owning basic block
aoqi@0 389 Block_Array _node_to_block_mapping;
aoqi@0 390
aoqi@0 391 // Loop from the root
aoqi@0 392 CFGLoop* _root_loop;
aoqi@0 393
aoqi@0 394 // Outmost loop frequency
aoqi@0 395 float _outer_loop_frequency;
aoqi@0 396
aoqi@0 397 // Per node latency estimation, valid only during GCM
aoqi@0 398 GrowableArray<uint>* _node_latency;
aoqi@0 399
aoqi@0 400 // Build a proper looking cfg. Return count of basic blocks
aoqi@0 401 uint build_cfg();
aoqi@0 402
aoqi@0 403 // Build the dominator tree so that we know where we can move instructions
aoqi@0 404 void build_dominator_tree();
aoqi@0 405
aoqi@0 406 // Estimate block frequencies based on IfNode probabilities, so that we know where we want to move instructions
aoqi@0 407 void estimate_block_frequency();
aoqi@0 408
aoqi@0 409 // Global Code Motion. See Click's PLDI95 paper. Place Nodes in specific
aoqi@0 410 // basic blocks; i.e. _node_to_block_mapping now maps _idx for all Nodes to some Block.
aoqi@0 411 // Move nodes to ensure correctness from GVN and also try to move nodes out of loops.
aoqi@0 412 void global_code_motion();
aoqi@0 413
aoqi@0 414 // Schedule Nodes early in their basic blocks.
aoqi@0 415 bool schedule_early(VectorSet &visited, Node_List &roots);
aoqi@0 416
aoqi@0 417 // For each node, find the latest block it can be scheduled into
aoqi@0 418 // and then select the cheapest block between the latest and earliest
aoqi@0 419 // block to place the node.
aoqi@0 420 void schedule_late(VectorSet &visited, Node_List &stack);
aoqi@0 421
aoqi@0 422 // Compute the (backwards) latency of a node from a single use
aoqi@0 423 int latency_from_use(Node *n, const Node *def, Node *use);
aoqi@0 424
aoqi@0 425 // Compute the (backwards) latency of a node from the uses of this instruction
aoqi@0 426 void partial_latency_of_defs(Node *n);
aoqi@0 427
aoqi@0 428 // Compute the instruction global latency with a backwards walk
aoqi@0 429 void compute_latencies_backwards(VectorSet &visited, Node_List &stack);
aoqi@0 430
aoqi@0 431 // Pick a block between early and late that is a cheaper alternative
aoqi@0 432 // to late. Helper for schedule_late.
aoqi@0 433 Block* hoist_to_cheaper_block(Block* LCA, Block* early, Node* self);
aoqi@0 434
aoqi@0 435 bool schedule_local(Block* block, GrowableArray<int>& ready_cnt, VectorSet& next_call);
aoqi@0 436 void set_next_call(Block* block, Node* n, VectorSet& next_call);
aoqi@0 437 void needed_for_next_call(Block* block, Node* this_call, VectorSet& next_call);
aoqi@0 438
aoqi@0 439 // Perform basic-block local scheduling
aoqi@0 440 Node* select(Block* block, Node_List& worklist, GrowableArray<int>& ready_cnt, VectorSet& next_call, uint sched_slot);
aoqi@0 441
aoqi@0 442 // Schedule a call next in the block
aoqi@0 443 uint sched_call(Block* block, uint node_cnt, Node_List& worklist, GrowableArray<int>& ready_cnt, MachCallNode* mcall, VectorSet& next_call);
aoqi@0 444
aoqi@0 445 // Cleanup if any code lands between a Call and his Catch
aoqi@0 446 void call_catch_cleanup(Block* block);
aoqi@0 447
aoqi@0 448 Node* catch_cleanup_find_cloned_def(Block* use_blk, Node* def, Block* def_blk, int n_clone_idx);
aoqi@0 449 void catch_cleanup_inter_block(Node *use, Block *use_blk, Node *def, Block *def_blk, int n_clone_idx);
aoqi@0 450
aoqi@0 451 // Detect implicit-null-check opportunities. Basically, find NULL checks
aoqi@0 452 // with suitable memory ops nearby. Use the memory op to do the NULL check.
aoqi@0 453 // I can generate a memory op if there is not one nearby.
aoqi@0 454 void implicit_null_check(Block* block, Node *proj, Node *val, int allowed_reasons);
aoqi@0 455
aoqi@0 456 // Perform a Depth First Search (DFS).
aoqi@0 457 // Setup 'vertex' as DFS to vertex mapping.
aoqi@0 458 // Setup 'semi' as vertex to DFS mapping.
aoqi@0 459 // Set 'parent' to DFS parent.
aoqi@0 460 uint do_DFS(Tarjan* tarjan, uint rpo_counter);
aoqi@0 461
aoqi@0 462 // Helper function to insert a node into a block
aoqi@0 463 void schedule_node_into_block( Node *n, Block *b );
aoqi@0 464
aoqi@0 465 void replace_block_proj_ctrl( Node *n );
aoqi@0 466
aoqi@0 467 // Set the basic block for pinned Nodes
aoqi@0 468 void schedule_pinned_nodes( VectorSet &visited );
aoqi@0 469
aoqi@0 470 // I'll need a few machine-specific GotoNodes. Clone from this one.
aoqi@0 471 // Used when building the CFG and creating end nodes for blocks.
aoqi@0 472 MachNode* _goto;
aoqi@0 473
aoqi@0 474 Block* insert_anti_dependences(Block* LCA, Node* load, bool verify = false);
kvn@8615 475 void verify_anti_dependences(Block* LCA, Node* load) const {
aoqi@0 476 assert(LCA == get_block_for_node(load), "should already be scheduled");
kvn@8615 477 const_cast<PhaseCFG*>(this)->insert_anti_dependences(LCA, load, true);
aoqi@0 478 }
aoqi@0 479
aoqi@0 480 bool move_to_next(Block* bx, uint b_index);
aoqi@0 481 void move_to_end(Block* bx, uint b_index);
aoqi@0 482
aoqi@0 483 void insert_goto_at(uint block_no, uint succ_no);
aoqi@0 484
aoqi@0 485 // Check for NeverBranch at block end. This needs to become a GOTO to the
aoqi@0 486 // true target. NeverBranch are treated as a conditional branch that always
aoqi@0 487 // goes the same direction for most of the optimizer and are used to give a
aoqi@0 488 // fake exit path to infinite loops. At this late stage they need to turn
aoqi@0 489 // into Goto's so that when you enter the infinite loop you indeed hang.
aoqi@0 490 void convert_NeverBranch_to_Goto(Block *b);
aoqi@0 491
aoqi@0 492 CFGLoop* create_loop_tree();
aoqi@0 493
aoqi@0 494 #ifndef PRODUCT
aoqi@0 495 bool _trace_opto_pipelining; // tracing flag
aoqi@0 496 #endif
aoqi@0 497
aoqi@0 498 public:
aoqi@0 499 PhaseCFG(Arena* arena, RootNode* root, Matcher& matcher);
aoqi@0 500
aoqi@0 501 void set_latency_for_node(Node* node, int latency) {
aoqi@0 502 _node_latency->at_put_grow(node->_idx, latency);
aoqi@0 503 }
aoqi@0 504
aoqi@0 505 uint get_latency_for_node(Node* node) {
aoqi@0 506 return _node_latency->at_grow(node->_idx);
aoqi@0 507 }
aoqi@0 508
aoqi@0 509 // Get the outer most frequency
aoqi@0 510 float get_outer_loop_frequency() const {
aoqi@0 511 return _outer_loop_frequency;
aoqi@0 512 }
aoqi@0 513
aoqi@0 514 // Get the root node of the CFG
aoqi@0 515 RootNode* get_root_node() const {
aoqi@0 516 return _root;
aoqi@0 517 }
aoqi@0 518
aoqi@0 519 // Get the block of the root node
aoqi@0 520 Block* get_root_block() const {
aoqi@0 521 return _root_block;
aoqi@0 522 }
aoqi@0 523
aoqi@0 524 // Add a block at a position and moves the later ones one step
aoqi@0 525 void add_block_at(uint pos, Block* block) {
aoqi@0 526 _blocks.insert(pos, block);
aoqi@0 527 _number_of_blocks++;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 // Adds a block to the top of the block list
aoqi@0 531 void add_block(Block* block) {
aoqi@0 532 _blocks.push(block);
aoqi@0 533 _number_of_blocks++;
aoqi@0 534 }
aoqi@0 535
aoqi@0 536 // Clear the list of blocks
aoqi@0 537 void clear_blocks() {
aoqi@0 538 _blocks.reset();
aoqi@0 539 _number_of_blocks = 0;
aoqi@0 540 }
aoqi@0 541
aoqi@0 542 // Get the block at position pos in _blocks
aoqi@0 543 Block* get_block(uint pos) const {
aoqi@0 544 return _blocks[pos];
aoqi@0 545 }
aoqi@0 546
aoqi@0 547 // Number of blocks
aoqi@0 548 uint number_of_blocks() const {
aoqi@0 549 return _number_of_blocks;
aoqi@0 550 }
aoqi@0 551
aoqi@0 552 // set which block this node should reside in
aoqi@0 553 void map_node_to_block(const Node* node, Block* block) {
aoqi@0 554 _node_to_block_mapping.map(node->_idx, block);
aoqi@0 555 }
aoqi@0 556
aoqi@0 557 // removes the mapping from a node to a block
aoqi@0 558 void unmap_node_from_block(const Node* node) {
aoqi@0 559 _node_to_block_mapping.map(node->_idx, NULL);
aoqi@0 560 }
aoqi@0 561
aoqi@0 562 // get the block in which this node resides
aoqi@0 563 Block* get_block_for_node(const Node* node) const {
aoqi@0 564 return _node_to_block_mapping[node->_idx];
aoqi@0 565 }
aoqi@0 566
aoqi@0 567 // does this node reside in a block; return true
aoqi@0 568 bool has_block(const Node* node) const {
aoqi@0 569 return (_node_to_block_mapping.lookup(node->_idx) != NULL);
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 // Use frequency calculations and code shape to predict if the block
aoqi@0 573 // is uncommon.
aoqi@0 574 bool is_uncommon(const Block* block);
aoqi@0 575
aoqi@0 576 #ifdef ASSERT
aoqi@0 577 Unique_Node_List _raw_oops;
aoqi@0 578 #endif
aoqi@0 579
aoqi@0 580 // Do global code motion by first building dominator tree and estimate block frequency
aoqi@0 581 // Returns true on success
aoqi@0 582 bool do_global_code_motion();
aoqi@0 583
aoqi@0 584 // Compute the (backwards) latency of a node from the uses
aoqi@0 585 void latency_from_uses(Node *n);
aoqi@0 586
aoqi@0 587 // Set loop alignment
aoqi@0 588 void set_loop_alignment();
aoqi@0 589
aoqi@0 590 // Remove empty basic blocks
aoqi@0 591 void remove_empty_blocks();
aoqi@0 592 Block *fixup_trap_based_check(Node *branch, Block *block, int block_pos, Block *bnext);
aoqi@0 593 void fixup_flow();
aoqi@0 594
aoqi@0 595 // Insert a node into a block at index and map the node to the block
aoqi@0 596 void insert(Block *b, uint idx, Node *n) {
aoqi@0 597 b->insert_node(n , idx);
aoqi@0 598 map_node_to_block(n, b);
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 // Check all nodes and postalloc_expand them if necessary.
aoqi@0 602 void postalloc_expand(PhaseRegAlloc* _ra);
aoqi@0 603
aoqi@0 604 #ifndef PRODUCT
aoqi@0 605 bool trace_opto_pipelining() const { return _trace_opto_pipelining; }
aoqi@0 606
aoqi@0 607 // Debugging print of CFG
aoqi@0 608 void dump( ) const; // CFG only
aoqi@0 609 void _dump_cfg( const Node *end, VectorSet &visited ) const;
aoqi@0 610 void verify() const;
aoqi@0 611 void dump_headers();
aoqi@0 612 #else
aoqi@0 613 bool trace_opto_pipelining() const { return false; }
aoqi@0 614 #endif
aoqi@0 615 };
aoqi@0 616
aoqi@0 617
aoqi@0 618 //------------------------------UnionFind--------------------------------------
aoqi@0 619 // Map Block indices to a block-index for a cfg-cover.
aoqi@0 620 // Array lookup in the optimized case.
aoqi@0 621 class UnionFind : public ResourceObj {
aoqi@0 622 uint _cnt, _max;
aoqi@0 623 uint* _indices;
aoqi@0 624 ReallocMark _nesting; // assertion check for reallocations
aoqi@0 625 public:
aoqi@0 626 UnionFind( uint max );
aoqi@0 627 void reset( uint max ); // Reset to identity map for [0..max]
aoqi@0 628
aoqi@0 629 uint lookup( uint nidx ) const {
aoqi@0 630 return _indices[nidx];
aoqi@0 631 }
aoqi@0 632 uint operator[] (uint nidx) const { return lookup(nidx); }
aoqi@0 633
aoqi@0 634 void map( uint from_idx, uint to_idx ) {
aoqi@0 635 assert( from_idx < _cnt, "oob" );
aoqi@0 636 _indices[from_idx] = to_idx;
aoqi@0 637 }
aoqi@0 638 void extend( uint from_idx, uint to_idx );
aoqi@0 639
aoqi@0 640 uint Size() const { return _cnt; }
aoqi@0 641
aoqi@0 642 uint Find( uint idx ) {
aoqi@0 643 assert( idx < 65536, "Must fit into uint");
aoqi@0 644 uint uf_idx = lookup(idx);
aoqi@0 645 return (uf_idx == idx) ? uf_idx : Find_compress(idx);
aoqi@0 646 }
aoqi@0 647 uint Find_compress( uint idx );
aoqi@0 648 uint Find_const( uint idx ) const;
aoqi@0 649 void Union( uint idx1, uint idx2 );
aoqi@0 650
aoqi@0 651 };
aoqi@0 652
aoqi@0 653 //----------------------------BlockProbPair---------------------------
aoqi@0 654 // Ordered pair of Node*.
aoqi@0 655 class BlockProbPair VALUE_OBJ_CLASS_SPEC {
aoqi@0 656 protected:
aoqi@0 657 Block* _target; // block target
aoqi@0 658 float _prob; // probability of edge to block
aoqi@0 659 public:
aoqi@0 660 BlockProbPair() : _target(NULL), _prob(0.0) {}
aoqi@0 661 BlockProbPair(Block* b, float p) : _target(b), _prob(p) {}
aoqi@0 662
aoqi@0 663 Block* get_target() const { return _target; }
aoqi@0 664 float get_prob() const { return _prob; }
aoqi@0 665 };
aoqi@0 666
aoqi@0 667 //------------------------------CFGLoop-------------------------------------------
aoqi@0 668 class CFGLoop : public CFGElement {
aoqi@0 669 friend class VMStructs;
aoqi@0 670 int _id;
aoqi@0 671 int _depth;
aoqi@0 672 CFGLoop *_parent; // root of loop tree is the method level "pseudo" loop, it's parent is null
aoqi@0 673 CFGLoop *_sibling; // null terminated list
aoqi@0 674 CFGLoop *_child; // first child, use child's sibling to visit all immediately nested loops
aoqi@0 675 GrowableArray<CFGElement*> _members; // list of members of loop
aoqi@0 676 GrowableArray<BlockProbPair> _exits; // list of successor blocks and their probabilities
aoqi@0 677 float _exit_prob; // probability any loop exit is taken on a single loop iteration
aoqi@0 678 void update_succ_freq(Block* b, float freq);
aoqi@0 679
aoqi@0 680 public:
aoqi@0 681 CFGLoop(int id) :
aoqi@0 682 CFGElement(),
aoqi@0 683 _id(id),
aoqi@0 684 _depth(0),
aoqi@0 685 _parent(NULL),
aoqi@0 686 _sibling(NULL),
aoqi@0 687 _child(NULL),
aoqi@0 688 _exit_prob(1.0f) {}
aoqi@0 689 CFGLoop* parent() { return _parent; }
aoqi@0 690 void push_pred(Block* blk, int i, Block_List& worklist, PhaseCFG* cfg);
aoqi@0 691 void add_member(CFGElement *s) { _members.push(s); }
aoqi@0 692 void add_nested_loop(CFGLoop* cl);
aoqi@0 693 Block* head() {
aoqi@0 694 assert(_members.at(0)->is_block(), "head must be a block");
aoqi@0 695 Block* hd = _members.at(0)->as_Block();
aoqi@0 696 assert(hd->_loop == this, "just checking");
aoqi@0 697 assert(hd->head()->is_Loop(), "must begin with loop head node");
aoqi@0 698 return hd;
aoqi@0 699 }
aoqi@0 700 Block* backedge_block(); // Return the block on the backedge of the loop (else NULL)
aoqi@0 701 void compute_loop_depth(int depth);
aoqi@0 702 void compute_freq(); // compute frequency with loop assuming head freq 1.0f
aoqi@0 703 void scale_freq(); // scale frequency by loop trip count (including outer loops)
aoqi@0 704 float outer_loop_freq() const; // frequency of outer loop
aoqi@0 705 bool in_loop_nest(Block* b);
aoqi@0 706 float trip_count() const { return 1.0f / _exit_prob; }
aoqi@0 707 virtual bool is_loop() { return true; }
aoqi@0 708 int id() { return _id; }
aoqi@0 709
aoqi@0 710 #ifndef PRODUCT
aoqi@0 711 void dump( ) const;
aoqi@0 712 void dump_tree() const;
aoqi@0 713 #endif
aoqi@0 714 };
aoqi@0 715
aoqi@0 716
aoqi@0 717 //----------------------------------CFGEdge------------------------------------
aoqi@0 718 // A edge between two basic blocks that will be embodied by a branch or a
aoqi@0 719 // fall-through.
aoqi@0 720 class CFGEdge : public ResourceObj {
aoqi@0 721 friend class VMStructs;
aoqi@0 722 private:
aoqi@0 723 Block * _from; // Source basic block
aoqi@0 724 Block * _to; // Destination basic block
aoqi@0 725 float _freq; // Execution frequency (estimate)
aoqi@0 726 int _state;
aoqi@0 727 bool _infrequent;
aoqi@0 728 int _from_pct;
aoqi@0 729 int _to_pct;
aoqi@0 730
aoqi@0 731 // Private accessors
aoqi@0 732 int from_pct() const { return _from_pct; }
aoqi@0 733 int to_pct() const { return _to_pct; }
aoqi@0 734 int from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; }
aoqi@0 735 int to_infrequent() const { return to_pct() < BlockLayoutMinDiamondPercentage; }
aoqi@0 736
aoqi@0 737 public:
aoqi@0 738 enum {
aoqi@0 739 open, // initial edge state; unprocessed
aoqi@0 740 connected, // edge used to connect two traces together
aoqi@0 741 interior // edge is interior to trace (could be backedge)
aoqi@0 742 };
aoqi@0 743
aoqi@0 744 CFGEdge(Block *from, Block *to, float freq, int from_pct, int to_pct) :
aoqi@0 745 _from(from), _to(to), _freq(freq),
aoqi@0 746 _from_pct(from_pct), _to_pct(to_pct), _state(open) {
aoqi@0 747 _infrequent = from_infrequent() || to_infrequent();
aoqi@0 748 }
aoqi@0 749
aoqi@0 750 float freq() const { return _freq; }
aoqi@0 751 Block* from() const { return _from; }
aoqi@0 752 Block* to () const { return _to; }
aoqi@0 753 int infrequent() const { return _infrequent; }
aoqi@0 754 int state() const { return _state; }
aoqi@0 755
aoqi@0 756 void set_state(int state) { _state = state; }
aoqi@0 757
aoqi@0 758 #ifndef PRODUCT
aoqi@0 759 void dump( ) const;
aoqi@0 760 #endif
aoqi@0 761 };
aoqi@0 762
aoqi@0 763
aoqi@0 764 //-----------------------------------Trace-------------------------------------
aoqi@0 765 // An ordered list of basic blocks.
aoqi@0 766 class Trace : public ResourceObj {
aoqi@0 767 private:
aoqi@0 768 uint _id; // Unique Trace id (derived from initial block)
aoqi@0 769 Block ** _next_list; // Array mapping index to next block
aoqi@0 770 Block ** _prev_list; // Array mapping index to previous block
aoqi@0 771 Block * _first; // First block in the trace
aoqi@0 772 Block * _last; // Last block in the trace
aoqi@0 773
aoqi@0 774 // Return the block that follows "b" in the trace.
aoqi@0 775 Block * next(Block *b) const { return _next_list[b->_pre_order]; }
aoqi@0 776 void set_next(Block *b, Block *n) const { _next_list[b->_pre_order] = n; }
aoqi@0 777
aoqi@0 778 // Return the block that precedes "b" in the trace.
aoqi@0 779 Block * prev(Block *b) const { return _prev_list[b->_pre_order]; }
aoqi@0 780 void set_prev(Block *b, Block *p) const { _prev_list[b->_pre_order] = p; }
aoqi@0 781
aoqi@0 782 // We've discovered a loop in this trace. Reset last to be "b", and first as
aoqi@0 783 // the block following "b
aoqi@0 784 void break_loop_after(Block *b) {
aoqi@0 785 _last = b;
aoqi@0 786 _first = next(b);
aoqi@0 787 set_prev(_first, NULL);
aoqi@0 788 set_next(_last, NULL);
aoqi@0 789 }
aoqi@0 790
aoqi@0 791 public:
aoqi@0 792
aoqi@0 793 Trace(Block *b, Block **next_list, Block **prev_list) :
aoqi@0 794 _first(b),
aoqi@0 795 _last(b),
aoqi@0 796 _next_list(next_list),
aoqi@0 797 _prev_list(prev_list),
aoqi@0 798 _id(b->_pre_order) {
aoqi@0 799 set_next(b, NULL);
aoqi@0 800 set_prev(b, NULL);
aoqi@0 801 };
aoqi@0 802
aoqi@0 803 // Return the id number
aoqi@0 804 uint id() const { return _id; }
aoqi@0 805 void set_id(uint id) { _id = id; }
aoqi@0 806
aoqi@0 807 // Return the first block in the trace
aoqi@0 808 Block * first_block() const { return _first; }
aoqi@0 809
aoqi@0 810 // Return the last block in the trace
aoqi@0 811 Block * last_block() const { return _last; }
aoqi@0 812
aoqi@0 813 // Insert a trace in the middle of this one after b
aoqi@0 814 void insert_after(Block *b, Trace *tr) {
aoqi@0 815 set_next(tr->last_block(), next(b));
aoqi@0 816 if (next(b) != NULL) {
aoqi@0 817 set_prev(next(b), tr->last_block());
aoqi@0 818 }
aoqi@0 819
aoqi@0 820 set_next(b, tr->first_block());
aoqi@0 821 set_prev(tr->first_block(), b);
aoqi@0 822
aoqi@0 823 if (b == _last) {
aoqi@0 824 _last = tr->last_block();
aoqi@0 825 }
aoqi@0 826 }
aoqi@0 827
aoqi@0 828 void insert_before(Block *b, Trace *tr) {
aoqi@0 829 Block *p = prev(b);
aoqi@0 830 assert(p != NULL, "use append instead");
aoqi@0 831 insert_after(p, tr);
aoqi@0 832 }
aoqi@0 833
aoqi@0 834 // Append another trace to this one.
aoqi@0 835 void append(Trace *tr) {
aoqi@0 836 insert_after(_last, tr);
aoqi@0 837 }
aoqi@0 838
aoqi@0 839 // Append a block at the end of this trace
aoqi@0 840 void append(Block *b) {
aoqi@0 841 set_next(_last, b);
aoqi@0 842 set_prev(b, _last);
aoqi@0 843 _last = b;
aoqi@0 844 }
aoqi@0 845
aoqi@0 846 // Adjust the the blocks in this trace
aoqi@0 847 void fixup_blocks(PhaseCFG &cfg);
aoqi@0 848 bool backedge(CFGEdge *e);
aoqi@0 849
aoqi@0 850 #ifndef PRODUCT
aoqi@0 851 void dump( ) const;
aoqi@0 852 #endif
aoqi@0 853 };
aoqi@0 854
aoqi@0 855 //------------------------------PhaseBlockLayout-------------------------------
aoqi@0 856 // Rearrange blocks into some canonical order, based on edges and their frequencies
aoqi@0 857 class PhaseBlockLayout : public Phase {
aoqi@0 858 friend class VMStructs;
aoqi@0 859 PhaseCFG &_cfg; // Control flow graph
aoqi@0 860
aoqi@0 861 GrowableArray<CFGEdge *> *edges;
aoqi@0 862 Trace **traces;
aoqi@0 863 Block **next;
aoqi@0 864 Block **prev;
aoqi@0 865 UnionFind *uf;
aoqi@0 866
aoqi@0 867 // Given a block, find its encompassing Trace
aoqi@0 868 Trace * trace(Block *b) {
aoqi@0 869 return traces[uf->Find_compress(b->_pre_order)];
aoqi@0 870 }
aoqi@0 871 public:
aoqi@0 872 PhaseBlockLayout(PhaseCFG &cfg);
aoqi@0 873
aoqi@0 874 void find_edges();
aoqi@0 875 void grow_traces();
aoqi@0 876 void merge_traces(bool loose_connections);
aoqi@0 877 void reorder_traces(int count);
aoqi@0 878 void union_traces(Trace* from, Trace* to);
aoqi@0 879 };
aoqi@0 880
aoqi@0 881 #endif // SHARE_VM_OPTO_BLOCK_HPP

mercurial