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