aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_OPTO_BLOCK_HPP aoqi@0: #define SHARE_VM_OPTO_BLOCK_HPP aoqi@0: aoqi@0: #include "opto/multnode.hpp" aoqi@0: #include "opto/node.hpp" aoqi@0: #include "opto/phase.hpp" aoqi@0: aoqi@0: // Optimization - Graph Style aoqi@0: aoqi@0: class Block; aoqi@0: class CFGLoop; aoqi@0: class MachCallNode; aoqi@0: class Matcher; aoqi@0: class RootNode; aoqi@0: class VectorSet; aoqi@0: struct Tarjan; aoqi@0: aoqi@0: //------------------------------Block_Array------------------------------------ aoqi@0: // Map dense integer indices to Blocks. Uses classic doubling-array trick. aoqi@0: // Abstractly provides an infinite array of Block*'s, initialized to NULL. aoqi@0: // Note that the constructor just zeros things, and since I use Arena aoqi@0: // allocation I do not need a destructor to reclaim storage. aoqi@0: class Block_Array : public ResourceObj { aoqi@0: friend class VMStructs; aoqi@0: uint _size; // allocated size, as opposed to formal limit aoqi@0: debug_only(uint _limit;) // limit to formal domain aoqi@0: Arena *_arena; // Arena to allocate in aoqi@0: protected: aoqi@0: Block **_blocks; aoqi@0: void grow( uint i ); // Grow array node to fit aoqi@0: aoqi@0: public: aoqi@0: Block_Array(Arena *a) : _arena(a), _size(OptoBlockListSize) { aoqi@0: debug_only(_limit=0); aoqi@0: _blocks = NEW_ARENA_ARRAY( a, Block *, OptoBlockListSize ); aoqi@0: for( int i = 0; i < OptoBlockListSize; i++ ) { aoqi@0: _blocks[i] = NULL; aoqi@0: } aoqi@0: } aoqi@0: Block *lookup( uint i ) const // Lookup, or NULL for not mapped aoqi@0: { return (i=Max() ) grow(i); _blocks[i] = n; } aoqi@0: uint Max() const { debug_only(return _limit); return _size; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class Block_List : public Block_Array { aoqi@0: friend class VMStructs; aoqi@0: public: aoqi@0: uint _cnt; aoqi@0: Block_List() : Block_Array(Thread::current()->resource_area()), _cnt(0) {} aoqi@0: void push( Block *b ) { map(_cnt++,b); } aoqi@0: Block *pop() { return _blocks[--_cnt]; } aoqi@0: Block *rpop() { Block *b = _blocks[0]; _blocks[0]=_blocks[--_cnt]; return b;} aoqi@0: void remove( uint i ); aoqi@0: void insert( uint i, Block *n ); aoqi@0: uint size() const { return _cnt; } aoqi@0: void reset() { _cnt = 0; } aoqi@0: void print(); aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class CFGElement : public ResourceObj { aoqi@0: friend class VMStructs; aoqi@0: public: aoqi@0: float _freq; // Execution frequency (estimate) aoqi@0: aoqi@0: CFGElement() : _freq(0.0f) {} aoqi@0: virtual bool is_block() { return false; } aoqi@0: virtual bool is_loop() { return false; } aoqi@0: Block* as_Block() { assert(is_block(), "must be block"); return (Block*)this; } aoqi@0: CFGLoop* as_CFGLoop() { assert(is_loop(), "must be loop"); return (CFGLoop*)this; } aoqi@0: }; aoqi@0: aoqi@0: //------------------------------Block------------------------------------------ aoqi@0: // This class defines a Basic Block. aoqi@0: // Basic blocks are used during the output routines, and are not used during aoqi@0: // any optimization pass. They are created late in the game. aoqi@0: class Block : public CFGElement { aoqi@0: friend class VMStructs; aoqi@0: aoqi@0: private: aoqi@0: // Nodes in this block, in order aoqi@0: Node_List _nodes; aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // Get the node at index 'at_index', if 'at_index' is out of bounds return NULL aoqi@0: Node* get_node(uint at_index) const { aoqi@0: return _nodes[at_index]; aoqi@0: } aoqi@0: aoqi@0: // Get the number of nodes in this block aoqi@0: uint number_of_nodes() const { aoqi@0: return _nodes.size(); aoqi@0: } aoqi@0: aoqi@0: // 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: void map_node(Node* node, uint to_index) { aoqi@0: _nodes.map(to_index, node); aoqi@0: } aoqi@0: aoqi@0: // 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: void insert_node(Node* node, uint at_index) { aoqi@0: _nodes.insert(at_index, node); aoqi@0: } aoqi@0: aoqi@0: // Remove a node at index 'at_index' aoqi@0: void remove_node(uint at_index) { aoqi@0: _nodes.remove(at_index); aoqi@0: } aoqi@0: aoqi@0: // Push a node 'node' onto the node list aoqi@0: void push_node(Node* node) { aoqi@0: _nodes.push(node); aoqi@0: } aoqi@0: aoqi@0: // Pop the last node off the node list aoqi@0: Node* pop_node() { aoqi@0: return _nodes.pop(); aoqi@0: } aoqi@0: aoqi@0: // Basic blocks have a Node which defines Control for all Nodes pinned in aoqi@0: // this block. This Node is a RegionNode. Exception-causing Nodes aoqi@0: // (division, subroutines) and Phi functions are always pinned. Later, aoqi@0: // every Node will get pinned to some block. aoqi@0: Node *head() const { return get_node(0); } aoqi@0: aoqi@0: // CAUTION: num_preds() is ONE based, so that predecessor numbers match aoqi@0: // input edges to Regions and Phis. aoqi@0: uint num_preds() const { return head()->req(); } aoqi@0: Node *pred(uint i) const { return head()->in(i); } aoqi@0: aoqi@0: // Array of successor blocks, same size as projs array aoqi@0: Block_Array _succs; aoqi@0: aoqi@0: // Basic blocks have some number of Nodes which split control to all aoqi@0: // following blocks. These Nodes are always Projections. The field in aoqi@0: // the Projection and the block-ending Node determine which Block follows. aoqi@0: uint _num_succs; aoqi@0: aoqi@0: // Basic blocks also carry all sorts of good old fashioned DFS information aoqi@0: // used to find loops, loop nesting depth, dominators, etc. aoqi@0: uint _pre_order; // Pre-order DFS number aoqi@0: aoqi@0: // Dominator tree aoqi@0: uint _dom_depth; // Depth in dominator tree for fast LCA aoqi@0: Block* _idom; // Immediate dominator block aoqi@0: aoqi@0: CFGLoop *_loop; // Loop to which this block belongs aoqi@0: uint _rpo; // Number in reverse post order walk aoqi@0: aoqi@0: virtual bool is_block() { return true; } aoqi@0: float succ_prob(uint i); // return probability of i'th successor aoqi@0: int num_fall_throughs(); // How many fall-through candidate this block has aoqi@0: void update_uncommon_branch(Block* un); // Lower branch prob to uncommon code aoqi@0: bool succ_fall_through(uint i); // Is successor "i" is a fall-through candidate aoqi@0: Block* lone_fall_through(); // Return lone fall-through Block or null aoqi@0: aoqi@0: Block* dom_lca(Block* that); // Compute LCA in dominator tree. aoqi@0: #ifdef ASSERT aoqi@0: bool dominates(Block* that) { aoqi@0: int dom_diff = this->_dom_depth - that->_dom_depth; aoqi@0: if (dom_diff > 0) return false; aoqi@0: for (; dom_diff < 0; dom_diff++) that = that->_idom; aoqi@0: return this == that; aoqi@0: } aoqi@0: #endif aoqi@0: aoqi@0: // Report the alignment required by this block. Must be a power of 2. aoqi@0: // The previous block will insert nops to get this alignment. aoqi@0: uint code_alignment(); aoqi@0: uint compute_loop_alignment(); aoqi@0: aoqi@0: // BLOCK_FREQUENCY is a sentinel to mark uses of constant block frequencies. aoqi@0: // It is currently also used to scale such frequencies relative to aoqi@0: // FreqCountInvocations relative to the old value of 1500. aoqi@0: #define BLOCK_FREQUENCY(f) ((f * (float) 1500) / FreqCountInvocations) aoqi@0: aoqi@0: // Register Pressure (estimate) for Splitting heuristic aoqi@0: uint _reg_pressure; aoqi@0: uint _ihrp_index; aoqi@0: uint _freg_pressure; aoqi@0: uint _fhrp_index; aoqi@0: aoqi@0: // Mark and visited bits for an LCA calculation in insert_anti_dependences. aoqi@0: // Since they hold unique node indexes, they do not need reinitialization. aoqi@0: node_idx_t _raise_LCA_mark; aoqi@0: void set_raise_LCA_mark(node_idx_t x) { _raise_LCA_mark = x; } aoqi@0: node_idx_t raise_LCA_mark() const { return _raise_LCA_mark; } aoqi@0: node_idx_t _raise_LCA_visited; aoqi@0: void set_raise_LCA_visited(node_idx_t x) { _raise_LCA_visited = x; } aoqi@0: node_idx_t raise_LCA_visited() const { return _raise_LCA_visited; } aoqi@0: aoqi@0: // Estimated size in bytes of first instructions in a loop. aoqi@0: uint _first_inst_size; aoqi@0: uint first_inst_size() const { return _first_inst_size; } aoqi@0: void set_first_inst_size(uint s) { _first_inst_size = s; } aoqi@0: aoqi@0: // Compute the size of first instructions in this block. aoqi@0: uint compute_first_inst_size(uint& sum_size, uint inst_cnt, PhaseRegAlloc* ra); aoqi@0: aoqi@0: // Compute alignment padding if the block needs it. aoqi@0: // Align a loop if loop's padding is less or equal to padding limit aoqi@0: // or the size of first instructions in the loop > padding. aoqi@0: uint alignment_padding(int current_offset) { aoqi@0: int block_alignment = code_alignment(); aoqi@0: int max_pad = block_alignment-relocInfo::addr_unit(); aoqi@0: if( max_pad > 0 ) { aoqi@0: assert(is_power_of_2(max_pad+relocInfo::addr_unit()), ""); aoqi@0: int current_alignment = current_offset & max_pad; aoqi@0: if( current_alignment != 0 ) { aoqi@0: uint padding = (block_alignment-current_alignment) & max_pad; aoqi@0: if( has_loop_alignment() && aoqi@0: padding > (uint)MaxLoopPad && aoqi@0: first_inst_size() <= padding ) { aoqi@0: return 0; aoqi@0: } aoqi@0: return padding; aoqi@0: } aoqi@0: } aoqi@0: return 0; aoqi@0: } aoqi@0: aoqi@0: // Connector blocks. Connector blocks are basic blocks devoid of aoqi@0: // instructions, but may have relevant non-instruction Nodes, such as aoqi@0: // Phis or MergeMems. Such blocks are discovered and marked during the aoqi@0: // RemoveEmpty phase, and elided during Output. aoqi@0: bool _connector; aoqi@0: void set_connector() { _connector = true; } aoqi@0: bool is_connector() const { return _connector; }; aoqi@0: aoqi@0: // Loop_alignment will be set for blocks which are at the top of loops. aoqi@0: // The block layout pass may rotate loops such that the loop head may not aoqi@0: // be the sequentially first block of the loop encountered in the linear aoqi@0: // list of blocks. If the layout pass is not run, loop alignment is set aoqi@0: // for each block which is the head of a loop. aoqi@0: uint _loop_alignment; aoqi@0: void set_loop_alignment(Block *loop_top) { aoqi@0: uint new_alignment = loop_top->compute_loop_alignment(); aoqi@0: if (new_alignment > _loop_alignment) { aoqi@0: _loop_alignment = new_alignment; aoqi@0: } aoqi@0: } aoqi@0: uint loop_alignment() const { return _loop_alignment; } aoqi@0: bool has_loop_alignment() const { return loop_alignment() > 0; } aoqi@0: aoqi@0: // Create a new Block with given head Node. aoqi@0: // Creates the (empty) predecessor arrays. aoqi@0: Block( Arena *a, Node *headnode ) aoqi@0: : CFGElement(), aoqi@0: _nodes(a), aoqi@0: _succs(a), aoqi@0: _num_succs(0), aoqi@0: _pre_order(0), aoqi@0: _idom(0), aoqi@0: _loop(NULL), aoqi@0: _reg_pressure(0), aoqi@0: _ihrp_index(1), aoqi@0: _freg_pressure(0), aoqi@0: _fhrp_index(1), aoqi@0: _raise_LCA_mark(0), aoqi@0: _raise_LCA_visited(0), aoqi@0: _first_inst_size(999999), aoqi@0: _connector(false), aoqi@0: _loop_alignment(0) { aoqi@0: _nodes.push(headnode); aoqi@0: } aoqi@0: aoqi@0: // Index of 'end' Node aoqi@0: uint end_idx() const { aoqi@0: // %%%%% add a proj after every goto aoqi@0: // so (last->is_block_proj() != last) always, then simplify this code aoqi@0: // This will not give correct end_idx for block 0 when it only contains root. aoqi@0: int last_idx = _nodes.size() - 1; aoqi@0: Node *last = _nodes[last_idx]; aoqi@0: assert(last->is_block_proj() == last || last->is_block_proj() == _nodes[last_idx - _num_succs], ""); aoqi@0: return (last->is_block_proj() == last) ? last_idx : (last_idx - _num_succs); aoqi@0: } aoqi@0: aoqi@0: // Basic blocks have a Node which ends them. This Node determines which aoqi@0: // basic block follows this one in the program flow. This Node is either an aoqi@0: // IfNode, a GotoNode, a JmpNode, or a ReturnNode. aoqi@0: Node *end() const { return _nodes[end_idx()]; } aoqi@0: aoqi@0: // Add an instruction to an existing block. It must go after the head aoqi@0: // instruction and before the end instruction. aoqi@0: void add_inst( Node *n ) { insert_node(n, end_idx()); } aoqi@0: // Find node in block. Fails if node not in block. aoqi@0: uint find_node( const Node *n ) const; aoqi@0: // Find and remove n from block list aoqi@0: void find_remove( const Node *n ); aoqi@0: // Check wether the node is in the block. aoqi@0: bool contains (const Node *n) const; aoqi@0: aoqi@0: // Return the empty status of a block aoqi@0: enum { not_empty, empty_with_goto, completely_empty }; aoqi@0: int is_Empty() const; aoqi@0: aoqi@0: // Forward through connectors aoqi@0: Block* non_connector() { aoqi@0: Block* s = this; aoqi@0: while (s->is_connector()) { aoqi@0: s = s->_succs[0]; aoqi@0: } aoqi@0: return s; aoqi@0: } aoqi@0: aoqi@0: // Return true if b is a successor of this block aoqi@0: bool has_successor(Block* b) const { aoqi@0: for (uint i = 0; i < _num_succs; i++ ) { aoqi@0: if (non_connector_successor(i) == b) { aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Successor block, after forwarding through connectors aoqi@0: Block* non_connector_successor(int i) const { aoqi@0: return _succs[i]->non_connector(); aoqi@0: } aoqi@0: aoqi@0: // Examine block's code shape to predict if it is not commonly executed. aoqi@0: bool has_uncommon_code() const; aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: // Debugging print of basic block aoqi@0: void dump_bidx(const Block* orig, outputStream* st = tty) const; aoqi@0: void dump_pred(const PhaseCFG* cfg, Block* orig, outputStream* st = tty) const; aoqi@0: void dump_head(const PhaseCFG* cfg, outputStream* st = tty) const; aoqi@0: void dump() const; aoqi@0: void dump(const PhaseCFG* cfg) const; aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //------------------------------PhaseCFG--------------------------------------- aoqi@0: // Build an array of Basic Block pointers, one per Node. aoqi@0: class PhaseCFG : public Phase { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: aoqi@0: // Root of whole program aoqi@0: RootNode* _root; aoqi@0: aoqi@0: // The block containing the root node aoqi@0: Block* _root_block; aoqi@0: aoqi@0: // List of basic blocks that are created during CFG creation aoqi@0: Block_List _blocks; aoqi@0: aoqi@0: // Count of basic blocks aoqi@0: uint _number_of_blocks; aoqi@0: aoqi@0: // Arena for the blocks to be stored in aoqi@0: Arena* _block_arena; aoqi@0: aoqi@0: // The matcher for this compilation aoqi@0: Matcher& _matcher; aoqi@0: aoqi@0: // Map nodes to owning basic block aoqi@0: Block_Array _node_to_block_mapping; aoqi@0: aoqi@0: // Loop from the root aoqi@0: CFGLoop* _root_loop; aoqi@0: aoqi@0: // Outmost loop frequency aoqi@0: float _outer_loop_frequency; aoqi@0: aoqi@0: // Per node latency estimation, valid only during GCM aoqi@0: GrowableArray* _node_latency; aoqi@0: aoqi@0: // Build a proper looking cfg. Return count of basic blocks aoqi@0: uint build_cfg(); aoqi@0: aoqi@0: // Build the dominator tree so that we know where we can move instructions aoqi@0: void build_dominator_tree(); aoqi@0: aoqi@0: // Estimate block frequencies based on IfNode probabilities, so that we know where we want to move instructions aoqi@0: void estimate_block_frequency(); aoqi@0: aoqi@0: // Global Code Motion. See Click's PLDI95 paper. Place Nodes in specific aoqi@0: // basic blocks; i.e. _node_to_block_mapping now maps _idx for all Nodes to some Block. aoqi@0: // Move nodes to ensure correctness from GVN and also try to move nodes out of loops. aoqi@0: void global_code_motion(); aoqi@0: aoqi@0: // Schedule Nodes early in their basic blocks. aoqi@0: bool schedule_early(VectorSet &visited, Node_List &roots); aoqi@0: aoqi@0: // For each node, find the latest block it can be scheduled into aoqi@0: // and then select the cheapest block between the latest and earliest aoqi@0: // block to place the node. aoqi@0: void schedule_late(VectorSet &visited, Node_List &stack); aoqi@0: aoqi@0: // Compute the (backwards) latency of a node from a single use aoqi@0: int latency_from_use(Node *n, const Node *def, Node *use); aoqi@0: aoqi@0: // Compute the (backwards) latency of a node from the uses of this instruction aoqi@0: void partial_latency_of_defs(Node *n); aoqi@0: aoqi@0: // Compute the instruction global latency with a backwards walk aoqi@0: void compute_latencies_backwards(VectorSet &visited, Node_List &stack); aoqi@0: aoqi@0: // Pick a block between early and late that is a cheaper alternative aoqi@0: // to late. Helper for schedule_late. aoqi@0: Block* hoist_to_cheaper_block(Block* LCA, Block* early, Node* self); aoqi@0: aoqi@0: bool schedule_local(Block* block, GrowableArray& ready_cnt, VectorSet& next_call); aoqi@0: void set_next_call(Block* block, Node* n, VectorSet& next_call); aoqi@0: void needed_for_next_call(Block* block, Node* this_call, VectorSet& next_call); aoqi@0: aoqi@0: // Perform basic-block local scheduling aoqi@0: Node* select(Block* block, Node_List& worklist, GrowableArray& ready_cnt, VectorSet& next_call, uint sched_slot); aoqi@0: aoqi@0: // Schedule a call next in the block aoqi@0: uint sched_call(Block* block, uint node_cnt, Node_List& worklist, GrowableArray& ready_cnt, MachCallNode* mcall, VectorSet& next_call); aoqi@0: aoqi@0: // Cleanup if any code lands between a Call and his Catch aoqi@0: void call_catch_cleanup(Block* block); aoqi@0: aoqi@0: Node* catch_cleanup_find_cloned_def(Block* use_blk, Node* def, Block* def_blk, int n_clone_idx); aoqi@0: void catch_cleanup_inter_block(Node *use, Block *use_blk, Node *def, Block *def_blk, int n_clone_idx); aoqi@0: aoqi@0: // Detect implicit-null-check opportunities. Basically, find NULL checks aoqi@0: // with suitable memory ops nearby. Use the memory op to do the NULL check. aoqi@0: // I can generate a memory op if there is not one nearby. aoqi@0: void implicit_null_check(Block* block, Node *proj, Node *val, int allowed_reasons); aoqi@0: aoqi@0: // Perform a Depth First Search (DFS). aoqi@0: // Setup 'vertex' as DFS to vertex mapping. aoqi@0: // Setup 'semi' as vertex to DFS mapping. aoqi@0: // Set 'parent' to DFS parent. aoqi@0: uint do_DFS(Tarjan* tarjan, uint rpo_counter); aoqi@0: aoqi@0: // Helper function to insert a node into a block aoqi@0: void schedule_node_into_block( Node *n, Block *b ); aoqi@0: aoqi@0: void replace_block_proj_ctrl( Node *n ); aoqi@0: aoqi@0: // Set the basic block for pinned Nodes aoqi@0: void schedule_pinned_nodes( VectorSet &visited ); aoqi@0: aoqi@0: // I'll need a few machine-specific GotoNodes. Clone from this one. aoqi@0: // Used when building the CFG and creating end nodes for blocks. aoqi@0: MachNode* _goto; aoqi@0: aoqi@0: Block* insert_anti_dependences(Block* LCA, Node* load, bool verify = false); aoqi@0: void verify_anti_dependences(Block* LCA, Node* load) { aoqi@0: assert(LCA == get_block_for_node(load), "should already be scheduled"); aoqi@0: insert_anti_dependences(LCA, load, true); aoqi@0: } aoqi@0: aoqi@0: bool move_to_next(Block* bx, uint b_index); aoqi@0: void move_to_end(Block* bx, uint b_index); aoqi@0: aoqi@0: void insert_goto_at(uint block_no, uint succ_no); aoqi@0: aoqi@0: // Check for NeverBranch at block end. This needs to become a GOTO to the aoqi@0: // true target. NeverBranch are treated as a conditional branch that always aoqi@0: // goes the same direction for most of the optimizer and are used to give a aoqi@0: // fake exit path to infinite loops. At this late stage they need to turn aoqi@0: // into Goto's so that when you enter the infinite loop you indeed hang. aoqi@0: void convert_NeverBranch_to_Goto(Block *b); aoqi@0: aoqi@0: CFGLoop* create_loop_tree(); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: bool _trace_opto_pipelining; // tracing flag aoqi@0: #endif aoqi@0: aoqi@0: public: aoqi@0: PhaseCFG(Arena* arena, RootNode* root, Matcher& matcher); aoqi@0: aoqi@0: void set_latency_for_node(Node* node, int latency) { aoqi@0: _node_latency->at_put_grow(node->_idx, latency); aoqi@0: } aoqi@0: aoqi@0: uint get_latency_for_node(Node* node) { aoqi@0: return _node_latency->at_grow(node->_idx); aoqi@0: } aoqi@0: aoqi@0: // Get the outer most frequency aoqi@0: float get_outer_loop_frequency() const { aoqi@0: return _outer_loop_frequency; aoqi@0: } aoqi@0: aoqi@0: // Get the root node of the CFG aoqi@0: RootNode* get_root_node() const { aoqi@0: return _root; aoqi@0: } aoqi@0: aoqi@0: // Get the block of the root node aoqi@0: Block* get_root_block() const { aoqi@0: return _root_block; aoqi@0: } aoqi@0: aoqi@0: // Add a block at a position and moves the later ones one step aoqi@0: void add_block_at(uint pos, Block* block) { aoqi@0: _blocks.insert(pos, block); aoqi@0: _number_of_blocks++; aoqi@0: } aoqi@0: aoqi@0: // Adds a block to the top of the block list aoqi@0: void add_block(Block* block) { aoqi@0: _blocks.push(block); aoqi@0: _number_of_blocks++; aoqi@0: } aoqi@0: aoqi@0: // Clear the list of blocks aoqi@0: void clear_blocks() { aoqi@0: _blocks.reset(); aoqi@0: _number_of_blocks = 0; aoqi@0: } aoqi@0: aoqi@0: // Get the block at position pos in _blocks aoqi@0: Block* get_block(uint pos) const { aoqi@0: return _blocks[pos]; aoqi@0: } aoqi@0: aoqi@0: // Number of blocks aoqi@0: uint number_of_blocks() const { aoqi@0: return _number_of_blocks; aoqi@0: } aoqi@0: aoqi@0: // set which block this node should reside in aoqi@0: void map_node_to_block(const Node* node, Block* block) { aoqi@0: _node_to_block_mapping.map(node->_idx, block); aoqi@0: } aoqi@0: aoqi@0: // removes the mapping from a node to a block aoqi@0: void unmap_node_from_block(const Node* node) { aoqi@0: _node_to_block_mapping.map(node->_idx, NULL); aoqi@0: } aoqi@0: aoqi@0: // get the block in which this node resides aoqi@0: Block* get_block_for_node(const Node* node) const { aoqi@0: return _node_to_block_mapping[node->_idx]; aoqi@0: } aoqi@0: aoqi@0: // does this node reside in a block; return true aoqi@0: bool has_block(const Node* node) const { aoqi@0: return (_node_to_block_mapping.lookup(node->_idx) != NULL); aoqi@0: } aoqi@0: aoqi@0: // Use frequency calculations and code shape to predict if the block aoqi@0: // is uncommon. aoqi@0: bool is_uncommon(const Block* block); aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: Unique_Node_List _raw_oops; aoqi@0: #endif aoqi@0: aoqi@0: // Do global code motion by first building dominator tree and estimate block frequency aoqi@0: // Returns true on success aoqi@0: bool do_global_code_motion(); aoqi@0: aoqi@0: // Compute the (backwards) latency of a node from the uses aoqi@0: void latency_from_uses(Node *n); aoqi@0: aoqi@0: // Set loop alignment aoqi@0: void set_loop_alignment(); aoqi@0: aoqi@0: // Remove empty basic blocks aoqi@0: void remove_empty_blocks(); aoqi@0: Block *fixup_trap_based_check(Node *branch, Block *block, int block_pos, Block *bnext); aoqi@0: void fixup_flow(); aoqi@0: aoqi@0: // Insert a node into a block at index and map the node to the block aoqi@0: void insert(Block *b, uint idx, Node *n) { aoqi@0: b->insert_node(n , idx); aoqi@0: map_node_to_block(n, b); aoqi@0: } aoqi@0: aoqi@0: // Check all nodes and postalloc_expand them if necessary. aoqi@0: void postalloc_expand(PhaseRegAlloc* _ra); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: bool trace_opto_pipelining() const { return _trace_opto_pipelining; } aoqi@0: aoqi@0: // Debugging print of CFG aoqi@0: void dump( ) const; // CFG only aoqi@0: void _dump_cfg( const Node *end, VectorSet &visited ) const; aoqi@0: void verify() const; aoqi@0: void dump_headers(); aoqi@0: #else aoqi@0: bool trace_opto_pipelining() const { return false; } aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //------------------------------UnionFind-------------------------------------- aoqi@0: // Map Block indices to a block-index for a cfg-cover. aoqi@0: // Array lookup in the optimized case. aoqi@0: class UnionFind : public ResourceObj { aoqi@0: uint _cnt, _max; aoqi@0: uint* _indices; aoqi@0: ReallocMark _nesting; // assertion check for reallocations aoqi@0: public: aoqi@0: UnionFind( uint max ); aoqi@0: void reset( uint max ); // Reset to identity map for [0..max] aoqi@0: aoqi@0: uint lookup( uint nidx ) const { aoqi@0: return _indices[nidx]; aoqi@0: } aoqi@0: uint operator[] (uint nidx) const { return lookup(nidx); } aoqi@0: aoqi@0: void map( uint from_idx, uint to_idx ) { aoqi@0: assert( from_idx < _cnt, "oob" ); aoqi@0: _indices[from_idx] = to_idx; aoqi@0: } aoqi@0: void extend( uint from_idx, uint to_idx ); aoqi@0: aoqi@0: uint Size() const { return _cnt; } aoqi@0: aoqi@0: uint Find( uint idx ) { aoqi@0: assert( idx < 65536, "Must fit into uint"); aoqi@0: uint uf_idx = lookup(idx); aoqi@0: return (uf_idx == idx) ? uf_idx : Find_compress(idx); aoqi@0: } aoqi@0: uint Find_compress( uint idx ); aoqi@0: uint Find_const( uint idx ) const; aoqi@0: void Union( uint idx1, uint idx2 ); aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: //----------------------------BlockProbPair--------------------------- aoqi@0: // Ordered pair of Node*. aoqi@0: class BlockProbPair VALUE_OBJ_CLASS_SPEC { aoqi@0: protected: aoqi@0: Block* _target; // block target aoqi@0: float _prob; // probability of edge to block aoqi@0: public: aoqi@0: BlockProbPair() : _target(NULL), _prob(0.0) {} aoqi@0: BlockProbPair(Block* b, float p) : _target(b), _prob(p) {} aoqi@0: aoqi@0: Block* get_target() const { return _target; } aoqi@0: float get_prob() const { return _prob; } aoqi@0: }; aoqi@0: aoqi@0: //------------------------------CFGLoop------------------------------------------- aoqi@0: class CFGLoop : public CFGElement { aoqi@0: friend class VMStructs; aoqi@0: int _id; aoqi@0: int _depth; aoqi@0: CFGLoop *_parent; // root of loop tree is the method level "pseudo" loop, it's parent is null aoqi@0: CFGLoop *_sibling; // null terminated list aoqi@0: CFGLoop *_child; // first child, use child's sibling to visit all immediately nested loops aoqi@0: GrowableArray _members; // list of members of loop aoqi@0: GrowableArray _exits; // list of successor blocks and their probabilities aoqi@0: float _exit_prob; // probability any loop exit is taken on a single loop iteration aoqi@0: void update_succ_freq(Block* b, float freq); aoqi@0: aoqi@0: public: aoqi@0: CFGLoop(int id) : aoqi@0: CFGElement(), aoqi@0: _id(id), aoqi@0: _depth(0), aoqi@0: _parent(NULL), aoqi@0: _sibling(NULL), aoqi@0: _child(NULL), aoqi@0: _exit_prob(1.0f) {} aoqi@0: CFGLoop* parent() { return _parent; } aoqi@0: void push_pred(Block* blk, int i, Block_List& worklist, PhaseCFG* cfg); aoqi@0: void add_member(CFGElement *s) { _members.push(s); } aoqi@0: void add_nested_loop(CFGLoop* cl); aoqi@0: Block* head() { aoqi@0: assert(_members.at(0)->is_block(), "head must be a block"); aoqi@0: Block* hd = _members.at(0)->as_Block(); aoqi@0: assert(hd->_loop == this, "just checking"); aoqi@0: assert(hd->head()->is_Loop(), "must begin with loop head node"); aoqi@0: return hd; aoqi@0: } aoqi@0: Block* backedge_block(); // Return the block on the backedge of the loop (else NULL) aoqi@0: void compute_loop_depth(int depth); aoqi@0: void compute_freq(); // compute frequency with loop assuming head freq 1.0f aoqi@0: void scale_freq(); // scale frequency by loop trip count (including outer loops) aoqi@0: float outer_loop_freq() const; // frequency of outer loop aoqi@0: bool in_loop_nest(Block* b); aoqi@0: float trip_count() const { return 1.0f / _exit_prob; } aoqi@0: virtual bool is_loop() { return true; } aoqi@0: int id() { return _id; } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void dump( ) const; aoqi@0: void dump_tree() const; aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //----------------------------------CFGEdge------------------------------------ aoqi@0: // A edge between two basic blocks that will be embodied by a branch or a aoqi@0: // fall-through. aoqi@0: class CFGEdge : public ResourceObj { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: Block * _from; // Source basic block aoqi@0: Block * _to; // Destination basic block aoqi@0: float _freq; // Execution frequency (estimate) aoqi@0: int _state; aoqi@0: bool _infrequent; aoqi@0: int _from_pct; aoqi@0: int _to_pct; aoqi@0: aoqi@0: // Private accessors aoqi@0: int from_pct() const { return _from_pct; } aoqi@0: int to_pct() const { return _to_pct; } aoqi@0: int from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; } aoqi@0: int to_infrequent() const { return to_pct() < BlockLayoutMinDiamondPercentage; } aoqi@0: aoqi@0: public: aoqi@0: enum { aoqi@0: open, // initial edge state; unprocessed aoqi@0: connected, // edge used to connect two traces together aoqi@0: interior // edge is interior to trace (could be backedge) aoqi@0: }; aoqi@0: aoqi@0: CFGEdge(Block *from, Block *to, float freq, int from_pct, int to_pct) : aoqi@0: _from(from), _to(to), _freq(freq), aoqi@0: _from_pct(from_pct), _to_pct(to_pct), _state(open) { aoqi@0: _infrequent = from_infrequent() || to_infrequent(); aoqi@0: } aoqi@0: aoqi@0: float freq() const { return _freq; } aoqi@0: Block* from() const { return _from; } aoqi@0: Block* to () const { return _to; } aoqi@0: int infrequent() const { return _infrequent; } aoqi@0: int state() const { return _state; } aoqi@0: aoqi@0: void set_state(int state) { _state = state; } aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void dump( ) const; aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //-----------------------------------Trace------------------------------------- aoqi@0: // An ordered list of basic blocks. aoqi@0: class Trace : public ResourceObj { aoqi@0: private: aoqi@0: uint _id; // Unique Trace id (derived from initial block) aoqi@0: Block ** _next_list; // Array mapping index to next block aoqi@0: Block ** _prev_list; // Array mapping index to previous block aoqi@0: Block * _first; // First block in the trace aoqi@0: Block * _last; // Last block in the trace aoqi@0: aoqi@0: // Return the block that follows "b" in the trace. aoqi@0: Block * next(Block *b) const { return _next_list[b->_pre_order]; } aoqi@0: void set_next(Block *b, Block *n) const { _next_list[b->_pre_order] = n; } aoqi@0: aoqi@0: // Return the block that precedes "b" in the trace. aoqi@0: Block * prev(Block *b) const { return _prev_list[b->_pre_order]; } aoqi@0: void set_prev(Block *b, Block *p) const { _prev_list[b->_pre_order] = p; } aoqi@0: aoqi@0: // We've discovered a loop in this trace. Reset last to be "b", and first as aoqi@0: // the block following "b aoqi@0: void break_loop_after(Block *b) { aoqi@0: _last = b; aoqi@0: _first = next(b); aoqi@0: set_prev(_first, NULL); aoqi@0: set_next(_last, NULL); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: Trace(Block *b, Block **next_list, Block **prev_list) : aoqi@0: _first(b), aoqi@0: _last(b), aoqi@0: _next_list(next_list), aoqi@0: _prev_list(prev_list), aoqi@0: _id(b->_pre_order) { aoqi@0: set_next(b, NULL); aoqi@0: set_prev(b, NULL); aoqi@0: }; aoqi@0: aoqi@0: // Return the id number aoqi@0: uint id() const { return _id; } aoqi@0: void set_id(uint id) { _id = id; } aoqi@0: aoqi@0: // Return the first block in the trace aoqi@0: Block * first_block() const { return _first; } aoqi@0: aoqi@0: // Return the last block in the trace aoqi@0: Block * last_block() const { return _last; } aoqi@0: aoqi@0: // Insert a trace in the middle of this one after b aoqi@0: void insert_after(Block *b, Trace *tr) { aoqi@0: set_next(tr->last_block(), next(b)); aoqi@0: if (next(b) != NULL) { aoqi@0: set_prev(next(b), tr->last_block()); aoqi@0: } aoqi@0: aoqi@0: set_next(b, tr->first_block()); aoqi@0: set_prev(tr->first_block(), b); aoqi@0: aoqi@0: if (b == _last) { aoqi@0: _last = tr->last_block(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void insert_before(Block *b, Trace *tr) { aoqi@0: Block *p = prev(b); aoqi@0: assert(p != NULL, "use append instead"); aoqi@0: insert_after(p, tr); aoqi@0: } aoqi@0: aoqi@0: // Append another trace to this one. aoqi@0: void append(Trace *tr) { aoqi@0: insert_after(_last, tr); aoqi@0: } aoqi@0: aoqi@0: // Append a block at the end of this trace aoqi@0: void append(Block *b) { aoqi@0: set_next(_last, b); aoqi@0: set_prev(b, _last); aoqi@0: _last = b; aoqi@0: } aoqi@0: aoqi@0: // Adjust the the blocks in this trace aoqi@0: void fixup_blocks(PhaseCFG &cfg); aoqi@0: bool backedge(CFGEdge *e); aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: void dump( ) const; aoqi@0: #endif aoqi@0: }; aoqi@0: aoqi@0: //------------------------------PhaseBlockLayout------------------------------- aoqi@0: // Rearrange blocks into some canonical order, based on edges and their frequencies aoqi@0: class PhaseBlockLayout : public Phase { aoqi@0: friend class VMStructs; aoqi@0: PhaseCFG &_cfg; // Control flow graph aoqi@0: aoqi@0: GrowableArray *edges; aoqi@0: Trace **traces; aoqi@0: Block **next; aoqi@0: Block **prev; aoqi@0: UnionFind *uf; aoqi@0: aoqi@0: // Given a block, find its encompassing Trace aoqi@0: Trace * trace(Block *b) { aoqi@0: return traces[uf->Find_compress(b->_pre_order)]; aoqi@0: } aoqi@0: public: aoqi@0: PhaseBlockLayout(PhaseCFG &cfg); aoqi@0: aoqi@0: void find_edges(); aoqi@0: void grow_traces(); aoqi@0: void merge_traces(bool loose_connections); aoqi@0: void reorder_traces(int count); aoqi@0: void union_traces(Trace* from, Trace* to); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_OPTO_BLOCK_HPP