Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/block.hpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_OPTO_BLOCK_HPP25#define SHARE_VM_OPTO_BLOCK_HPP2627#include "opto/multnode.hpp"28#include "opto/node.hpp"29#include "opto/phase.hpp"3031// Optimization - Graph Style3233class Block;34class CFGLoop;35class MachCallNode;36class Matcher;37class RootNode;38class VectorSet;39struct Tarjan;4041//------------------------------Block_Array------------------------------------42// Map dense integer indices to Blocks. Uses classic doubling-array trick.43// Abstractly provides an infinite array of Block*'s, initialized to NULL.44// Note that the constructor just zeros things, and since I use Arena45// allocation I do not need a destructor to reclaim storage.46class Block_Array : public ResourceObj {47friend class VMStructs;48uint _size; // allocated size, as opposed to formal limit49debug_only(uint _limit;) // limit to formal domain50Arena *_arena; // Arena to allocate in51protected:52Block **_blocks;53void grow( uint i ); // Grow array node to fit5455public:56Block_Array(Arena *a) : _arena(a), _size(OptoBlockListSize) {57debug_only(_limit=0);58_blocks = NEW_ARENA_ARRAY( a, Block *, OptoBlockListSize );59for( int i = 0; i < OptoBlockListSize; i++ ) {60_blocks[i] = NULL;61}62}63Block *lookup( uint i ) const // Lookup, or NULL for not mapped64{ return (i<Max()) ? _blocks[i] : (Block*)NULL; }65Block *operator[] ( uint i ) const // Lookup, or assert for not mapped66{ assert( i < Max(), "oob" ); return _blocks[i]; }67// Extend the mapping: index i maps to Block *n.68void map( uint i, Block *n ) { if( i>=Max() ) grow(i); _blocks[i] = n; }69uint Max() const { debug_only(return _limit); return _size; }70};717273class Block_List : public Block_Array {74friend class VMStructs;75public:76uint _cnt;77Block_List() : Block_Array(Thread::current()->resource_area()), _cnt(0) {}78void push( Block *b ) { map(_cnt++,b); }79Block *pop() { return _blocks[--_cnt]; }80Block *rpop() { Block *b = _blocks[0]; _blocks[0]=_blocks[--_cnt]; return b;}81void remove( uint i );82void insert( uint i, Block *n );83uint size() const { return _cnt; }84void reset() { _cnt = 0; }85void print();86};878889class CFGElement : public ResourceObj {90friend class VMStructs;91public:92float _freq; // Execution frequency (estimate)9394CFGElement() : _freq(0.0f) {}95virtual bool is_block() { return false; }96virtual bool is_loop() { return false; }97Block* as_Block() { assert(is_block(), "must be block"); return (Block*)this; }98CFGLoop* as_CFGLoop() { assert(is_loop(), "must be loop"); return (CFGLoop*)this; }99};100101//------------------------------Block------------------------------------------102// This class defines a Basic Block.103// Basic blocks are used during the output routines, and are not used during104// any optimization pass. They are created late in the game.105class Block : public CFGElement {106friend class VMStructs;107108private:109// Nodes in this block, in order110Node_List _nodes;111112public:113114// Get the node at index 'at_index', if 'at_index' is out of bounds return NULL115Node* get_node(uint at_index) const {116return _nodes[at_index];117}118119// Get the number of nodes in this block120uint number_of_nodes() const {121return _nodes.size();122}123124// 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 increased125void map_node(Node* node, uint to_index) {126_nodes.map(to_index, node);127}128129// 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 crash130void insert_node(Node* node, uint at_index) {131_nodes.insert(at_index, node);132}133134// Remove a node at index 'at_index'135void remove_node(uint at_index) {136_nodes.remove(at_index);137}138139// Push a node 'node' onto the node list140void push_node(Node* node) {141_nodes.push(node);142}143144// Pop the last node off the node list145Node* pop_node() {146return _nodes.pop();147}148149// Basic blocks have a Node which defines Control for all Nodes pinned in150// this block. This Node is a RegionNode. Exception-causing Nodes151// (division, subroutines) and Phi functions are always pinned. Later,152// every Node will get pinned to some block.153Node *head() const { return get_node(0); }154155// CAUTION: num_preds() is ONE based, so that predecessor numbers match156// input edges to Regions and Phis.157uint num_preds() const { return head()->req(); }158Node *pred(uint i) const { return head()->in(i); }159160// Array of successor blocks, same size as projs array161Block_Array _succs;162163// Basic blocks have some number of Nodes which split control to all164// following blocks. These Nodes are always Projections. The field in165// the Projection and the block-ending Node determine which Block follows.166uint _num_succs;167168// Basic blocks also carry all sorts of good old fashioned DFS information169// used to find loops, loop nesting depth, dominators, etc.170uint _pre_order; // Pre-order DFS number171172// Dominator tree173uint _dom_depth; // Depth in dominator tree for fast LCA174Block* _idom; // Immediate dominator block175176CFGLoop *_loop; // Loop to which this block belongs177uint _rpo; // Number in reverse post order walk178179virtual bool is_block() { return true; }180float succ_prob(uint i); // return probability of i'th successor181int num_fall_throughs(); // How many fall-through candidate this block has182void update_uncommon_branch(Block* un); // Lower branch prob to uncommon code183bool succ_fall_through(uint i); // Is successor "i" is a fall-through candidate184Block* lone_fall_through(); // Return lone fall-through Block or null185186Block* dom_lca(Block* that); // Compute LCA in dominator tree.187188bool dominates(Block* that) {189int dom_diff = this->_dom_depth - that->_dom_depth;190if (dom_diff > 0) return false;191for (; dom_diff < 0; dom_diff++) that = that->_idom;192return this == that;193}194195// Report the alignment required by this block. Must be a power of 2.196// The previous block will insert nops to get this alignment.197uint code_alignment();198uint compute_loop_alignment();199200// BLOCK_FREQUENCY is a sentinel to mark uses of constant block frequencies.201// It is currently also used to scale such frequencies relative to202// FreqCountInvocations relative to the old value of 1500.203#define BLOCK_FREQUENCY(f) ((f * (float) 1500) / FreqCountInvocations)204205// Register Pressure (estimate) for Splitting heuristic206uint _reg_pressure;207uint _ihrp_index;208uint _freg_pressure;209uint _fhrp_index;210211// Mark and visited bits for an LCA calculation in insert_anti_dependences.212// Since they hold unique node indexes, they do not need reinitialization.213node_idx_t _raise_LCA_mark;214void set_raise_LCA_mark(node_idx_t x) { _raise_LCA_mark = x; }215node_idx_t raise_LCA_mark() const { return _raise_LCA_mark; }216node_idx_t _raise_LCA_visited;217void set_raise_LCA_visited(node_idx_t x) { _raise_LCA_visited = x; }218node_idx_t raise_LCA_visited() const { return _raise_LCA_visited; }219220// Estimated size in bytes of first instructions in a loop.221uint _first_inst_size;222uint first_inst_size() const { return _first_inst_size; }223void set_first_inst_size(uint s) { _first_inst_size = s; }224225// Compute the size of first instructions in this block.226uint compute_first_inst_size(uint& sum_size, uint inst_cnt, PhaseRegAlloc* ra);227228// Compute alignment padding if the block needs it.229// Align a loop if loop's padding is less or equal to padding limit230// or the size of first instructions in the loop > padding.231uint alignment_padding(int current_offset) {232int block_alignment = code_alignment();233int max_pad = block_alignment-relocInfo::addr_unit();234if( max_pad > 0 ) {235assert(is_power_of_2(max_pad+relocInfo::addr_unit()), "");236int current_alignment = current_offset & max_pad;237if( current_alignment != 0 ) {238uint padding = (block_alignment-current_alignment) & max_pad;239if( has_loop_alignment() &&240padding > (uint)MaxLoopPad &&241first_inst_size() <= padding ) {242return 0;243}244return padding;245}246}247return 0;248}249250// Connector blocks. Connector blocks are basic blocks devoid of251// instructions, but may have relevant non-instruction Nodes, such as252// Phis or MergeMems. Such blocks are discovered and marked during the253// RemoveEmpty phase, and elided during Output.254bool _connector;255void set_connector() { _connector = true; }256bool is_connector() const { return _connector; };257258// Loop_alignment will be set for blocks which are at the top of loops.259// The block layout pass may rotate loops such that the loop head may not260// be the sequentially first block of the loop encountered in the linear261// list of blocks. If the layout pass is not run, loop alignment is set262// for each block which is the head of a loop.263uint _loop_alignment;264void set_loop_alignment(Block *loop_top) {265uint new_alignment = loop_top->compute_loop_alignment();266if (new_alignment > _loop_alignment) {267_loop_alignment = new_alignment;268}269}270uint loop_alignment() const { return _loop_alignment; }271bool has_loop_alignment() const { return loop_alignment() > 0; }272273// Create a new Block with given head Node.274// Creates the (empty) predecessor arrays.275Block( Arena *a, Node *headnode )276: CFGElement(),277_nodes(a),278_succs(a),279_num_succs(0),280_pre_order(0),281_idom(0),282_loop(NULL),283_reg_pressure(0),284_ihrp_index(1),285_freg_pressure(0),286_fhrp_index(1),287_raise_LCA_mark(0),288_raise_LCA_visited(0),289_first_inst_size(999999),290_connector(false),291_loop_alignment(0) {292_nodes.push(headnode);293}294295// Index of 'end' Node296uint end_idx() const {297// %%%%% add a proj after every goto298// so (last->is_block_proj() != last) always, then simplify this code299// This will not give correct end_idx for block 0 when it only contains root.300int last_idx = _nodes.size() - 1;301Node *last = _nodes[last_idx];302assert(last->is_block_proj() == last || last->is_block_proj() == _nodes[last_idx - _num_succs], "");303return (last->is_block_proj() == last) ? last_idx : (last_idx - _num_succs);304}305306// Basic blocks have a Node which ends them. This Node determines which307// basic block follows this one in the program flow. This Node is either an308// IfNode, a GotoNode, a JmpNode, or a ReturnNode.309Node *end() const { return _nodes[end_idx()]; }310311// Add an instruction to an existing block. It must go after the head312// instruction and before the end instruction.313void add_inst( Node *n ) { insert_node(n, end_idx()); }314// Find node in block. Fails if node not in block.315uint find_node( const Node *n ) const;316// Find and remove n from block list317void find_remove( const Node *n );318// Check wether the node is in the block.319bool contains (const Node *n) const;320321// Return the empty status of a block322enum { not_empty, empty_with_goto, completely_empty };323int is_Empty() const;324325// Forward through connectors326Block* non_connector() {327Block* s = this;328while (s->is_connector()) {329s = s->_succs[0];330}331return s;332}333334// Return true if b is a successor of this block335bool has_successor(Block* b) const {336for (uint i = 0; i < _num_succs; i++ ) {337if (non_connector_successor(i) == b) {338return true;339}340}341return false;342}343344// Successor block, after forwarding through connectors345Block* non_connector_successor(int i) const {346return _succs[i]->non_connector();347}348349// Examine block's code shape to predict if it is not commonly executed.350bool has_uncommon_code() const;351352#ifndef PRODUCT353// Debugging print of basic block354void dump_bidx(const Block* orig, outputStream* st = tty) const;355void dump_pred(const PhaseCFG* cfg, Block* orig, outputStream* st = tty) const;356void dump_head(const PhaseCFG* cfg, outputStream* st = tty) const;357void dump() const;358void dump(const PhaseCFG* cfg) const;359#endif360};361362363//------------------------------PhaseCFG---------------------------------------364// Build an array of Basic Block pointers, one per Node.365class PhaseCFG : public Phase {366friend class VMStructs;367private:368369// Root of whole program370RootNode* _root;371372// The block containing the root node373Block* _root_block;374375// List of basic blocks that are created during CFG creation376Block_List _blocks;377378// Count of basic blocks379uint _number_of_blocks;380381// Arena for the blocks to be stored in382Arena* _block_arena;383384// The matcher for this compilation385Matcher& _matcher;386387// Map nodes to owning basic block388Block_Array _node_to_block_mapping;389390// Loop from the root391CFGLoop* _root_loop;392393// Outmost loop frequency394float _outer_loop_frequency;395396// Per node latency estimation, valid only during GCM397GrowableArray<uint>* _node_latency;398399// Build a proper looking cfg. Return count of basic blocks400uint build_cfg();401402// Build the dominator tree so that we know where we can move instructions403void build_dominator_tree();404405// Estimate block frequencies based on IfNode probabilities, so that we know where we want to move instructions406void estimate_block_frequency();407408// Global Code Motion. See Click's PLDI95 paper. Place Nodes in specific409// basic blocks; i.e. _node_to_block_mapping now maps _idx for all Nodes to some Block.410// Move nodes to ensure correctness from GVN and also try to move nodes out of loops.411void global_code_motion();412413// Schedule Nodes early in their basic blocks.414bool schedule_early(VectorSet &visited, Node_List &roots);415416// For each node, find the latest block it can be scheduled into417// and then select the cheapest block between the latest and earliest418// block to place the node.419void schedule_late(VectorSet &visited, Node_List &stack);420421// Compute the (backwards) latency of a node from a single use422int latency_from_use(Node *n, const Node *def, Node *use);423424// Compute the (backwards) latency of a node from the uses of this instruction425void partial_latency_of_defs(Node *n);426427// Compute the instruction global latency with a backwards walk428void compute_latencies_backwards(VectorSet &visited, Node_List &stack);429430// Pick a block between early and late that is a cheaper alternative431// to late. Helper for schedule_late.432Block* hoist_to_cheaper_block(Block* LCA, Block* early, Node* self);433434bool schedule_local(Block* block, GrowableArray<int>& ready_cnt, VectorSet& next_call);435void set_next_call(Block* block, Node* n, VectorSet& next_call);436void needed_for_next_call(Block* block, Node* this_call, VectorSet& next_call);437438// Perform basic-block local scheduling439Node* select(Block* block, Node_List& worklist, GrowableArray<int>& ready_cnt, VectorSet& next_call, uint sched_slot);440441// Schedule a call next in the block442uint sched_call(Block* block, uint node_cnt, Node_List& worklist, GrowableArray<int>& ready_cnt, MachCallNode* mcall, VectorSet& next_call);443444// Cleanup if any code lands between a Call and his Catch445void call_catch_cleanup(Block* block);446447Node* catch_cleanup_find_cloned_def(Block* use_blk, Node* def, Block* def_blk, int n_clone_idx);448void catch_cleanup_inter_block(Node *use, Block *use_blk, Node *def, Block *def_blk, int n_clone_idx);449450// Detect implicit-null-check opportunities. Basically, find NULL checks451// with suitable memory ops nearby. Use the memory op to do the NULL check.452// I can generate a memory op if there is not one nearby.453void implicit_null_check(Block* block, Node *proj, Node *val, int allowed_reasons);454455// Perform a Depth First Search (DFS).456// Setup 'vertex' as DFS to vertex mapping.457// Setup 'semi' as vertex to DFS mapping.458// Set 'parent' to DFS parent.459uint do_DFS(Tarjan* tarjan, uint rpo_counter);460461// Helper function to insert a node into a block462void schedule_node_into_block( Node *n, Block *b );463464void replace_block_proj_ctrl( Node *n );465466// Set the basic block for pinned Nodes467void schedule_pinned_nodes( VectorSet &visited );468469// I'll need a few machine-specific GotoNodes. Clone from this one.470// Used when building the CFG and creating end nodes for blocks.471MachNode* _goto;472473Block* insert_anti_dependences(Block* LCA, Node* load, bool verify = false);474void verify_anti_dependences(Block* LCA, Node* load) const {475assert(LCA == get_block_for_node(load), "should already be scheduled");476const_cast<PhaseCFG*>(this)->insert_anti_dependences(LCA, load, true);477}478479bool move_to_next(Block* bx, uint b_index);480void move_to_end(Block* bx, uint b_index);481482void insert_goto_at(uint block_no, uint succ_no);483484// Check for NeverBranch at block end. This needs to become a GOTO to the485// true target. NeverBranch are treated as a conditional branch that always486// goes the same direction for most of the optimizer and are used to give a487// fake exit path to infinite loops. At this late stage they need to turn488// into Goto's so that when you enter the infinite loop you indeed hang.489void convert_NeverBranch_to_Goto(Block *b);490491CFGLoop* create_loop_tree();492493#ifndef PRODUCT494bool _trace_opto_pipelining; // tracing flag495#endif496497public:498PhaseCFG(Arena* arena, RootNode* root, Matcher& matcher);499500void set_latency_for_node(Node* node, int latency) {501_node_latency->at_put_grow(node->_idx, latency);502}503504uint get_latency_for_node(Node* node) {505return _node_latency->at_grow(node->_idx);506}507508// Get the outer most frequency509float get_outer_loop_frequency() const {510return _outer_loop_frequency;511}512513// Get the root node of the CFG514RootNode* get_root_node() const {515return _root;516}517518// Get the block of the root node519Block* get_root_block() const {520return _root_block;521}522523// Add a block at a position and moves the later ones one step524void add_block_at(uint pos, Block* block) {525_blocks.insert(pos, block);526_number_of_blocks++;527}528529// Adds a block to the top of the block list530void add_block(Block* block) {531_blocks.push(block);532_number_of_blocks++;533}534535// Clear the list of blocks536void clear_blocks() {537_blocks.reset();538_number_of_blocks = 0;539}540541// Get the block at position pos in _blocks542Block* get_block(uint pos) const {543return _blocks[pos];544}545546// Number of blocks547uint number_of_blocks() const {548return _number_of_blocks;549}550551// set which block this node should reside in552void map_node_to_block(const Node* node, Block* block) {553_node_to_block_mapping.map(node->_idx, block);554}555556// removes the mapping from a node to a block557void unmap_node_from_block(const Node* node) {558_node_to_block_mapping.map(node->_idx, NULL);559}560561// get the block in which this node resides562Block* get_block_for_node(const Node* node) const {563return _node_to_block_mapping[node->_idx];564}565566// does this node reside in a block; return true567bool has_block(const Node* node) const {568return (_node_to_block_mapping.lookup(node->_idx) != NULL);569}570571// Use frequency calculations and code shape to predict if the block572// is uncommon.573bool is_uncommon(const Block* block);574575#ifdef ASSERT576Unique_Node_List _raw_oops;577#endif578579// Do global code motion by first building dominator tree and estimate block frequency580// Returns true on success581bool do_global_code_motion();582583// Compute the (backwards) latency of a node from the uses584void latency_from_uses(Node *n);585586// Set loop alignment587void set_loop_alignment();588589// Remove empty basic blocks590void remove_empty_blocks();591Block *fixup_trap_based_check(Node *branch, Block *block, int block_pos, Block *bnext);592void fixup_flow();593594// Insert a node into a block at index and map the node to the block595void insert(Block *b, uint idx, Node *n) {596b->insert_node(n , idx);597map_node_to_block(n, b);598}599600// Check all nodes and postalloc_expand them if necessary.601void postalloc_expand(PhaseRegAlloc* _ra);602603#ifndef PRODUCT604bool trace_opto_pipelining() const { return _trace_opto_pipelining; }605606// Debugging print of CFG607void dump( ) const; // CFG only608void _dump_cfg( const Node *end, VectorSet &visited ) const;609void verify() const;610void dump_headers();611#else612bool trace_opto_pipelining() const { return false; }613#endif614};615616617//------------------------------UnionFind--------------------------------------618// Map Block indices to a block-index for a cfg-cover.619// Array lookup in the optimized case.620class UnionFind : public ResourceObj {621uint _cnt, _max;622uint* _indices;623ReallocMark _nesting; // assertion check for reallocations624public:625UnionFind( uint max );626void reset( uint max ); // Reset to identity map for [0..max]627628uint lookup( uint nidx ) const {629return _indices[nidx];630}631uint operator[] (uint nidx) const { return lookup(nidx); }632633void map( uint from_idx, uint to_idx ) {634assert( from_idx < _cnt, "oob" );635_indices[from_idx] = to_idx;636}637void extend( uint from_idx, uint to_idx );638639uint Size() const { return _cnt; }640641uint Find( uint idx ) {642assert( idx < 65536, "Must fit into uint");643uint uf_idx = lookup(idx);644return (uf_idx == idx) ? uf_idx : Find_compress(idx);645}646uint Find_compress( uint idx );647uint Find_const( uint idx ) const;648void Union( uint idx1, uint idx2 );649650};651652//----------------------------BlockProbPair---------------------------653// Ordered pair of Node*.654class BlockProbPair VALUE_OBJ_CLASS_SPEC {655protected:656Block* _target; // block target657float _prob; // probability of edge to block658public:659BlockProbPair() : _target(NULL), _prob(0.0) {}660BlockProbPair(Block* b, float p) : _target(b), _prob(p) {}661662Block* get_target() const { return _target; }663float get_prob() const { return _prob; }664};665666//------------------------------CFGLoop-------------------------------------------667class CFGLoop : public CFGElement {668friend class VMStructs;669int _id;670int _depth;671CFGLoop *_parent; // root of loop tree is the method level "pseudo" loop, it's parent is null672CFGLoop *_sibling; // null terminated list673CFGLoop *_child; // first child, use child's sibling to visit all immediately nested loops674GrowableArray<CFGElement*> _members; // list of members of loop675GrowableArray<BlockProbPair> _exits; // list of successor blocks and their probabilities676float _exit_prob; // probability any loop exit is taken on a single loop iteration677void update_succ_freq(Block* b, float freq);678679public:680CFGLoop(int id) :681CFGElement(),682_id(id),683_depth(0),684_parent(NULL),685_sibling(NULL),686_child(NULL),687_exit_prob(1.0f) {}688CFGLoop* parent() { return _parent; }689void push_pred(Block* blk, int i, Block_List& worklist, PhaseCFG* cfg);690void add_member(CFGElement *s) { _members.push(s); }691void add_nested_loop(CFGLoop* cl);692Block* head() {693assert(_members.at(0)->is_block(), "head must be a block");694Block* hd = _members.at(0)->as_Block();695assert(hd->_loop == this, "just checking");696assert(hd->head()->is_Loop(), "must begin with loop head node");697return hd;698}699Block* backedge_block(); // Return the block on the backedge of the loop (else NULL)700void compute_loop_depth(int depth);701void compute_freq(); // compute frequency with loop assuming head freq 1.0f702void scale_freq(); // scale frequency by loop trip count (including outer loops)703float outer_loop_freq() const; // frequency of outer loop704bool in_loop_nest(Block* b);705float trip_count() const { return 1.0f / _exit_prob; }706virtual bool is_loop() { return true; }707int id() { return _id; }708709#ifndef PRODUCT710void dump( ) const;711void dump_tree() const;712#endif713};714715716//----------------------------------CFGEdge------------------------------------717// A edge between two basic blocks that will be embodied by a branch or a718// fall-through.719class CFGEdge : public ResourceObj {720friend class VMStructs;721private:722Block * _from; // Source basic block723Block * _to; // Destination basic block724float _freq; // Execution frequency (estimate)725int _state;726bool _infrequent;727int _from_pct;728int _to_pct;729730// Private accessors731int from_pct() const { return _from_pct; }732int to_pct() const { return _to_pct; }733int from_infrequent() const { return from_pct() < BlockLayoutMinDiamondPercentage; }734int to_infrequent() const { return to_pct() < BlockLayoutMinDiamondPercentage; }735736public:737enum {738open, // initial edge state; unprocessed739connected, // edge used to connect two traces together740interior // edge is interior to trace (could be backedge)741};742743CFGEdge(Block *from, Block *to, float freq, int from_pct, int to_pct) :744_from(from), _to(to), _freq(freq),745_from_pct(from_pct), _to_pct(to_pct), _state(open) {746_infrequent = from_infrequent() || to_infrequent();747}748749float freq() const { return _freq; }750Block* from() const { return _from; }751Block* to () const { return _to; }752int infrequent() const { return _infrequent; }753int state() const { return _state; }754755void set_state(int state) { _state = state; }756757#ifndef PRODUCT758void dump( ) const;759#endif760};761762763//-----------------------------------Trace-------------------------------------764// An ordered list of basic blocks.765class Trace : public ResourceObj {766private:767uint _id; // Unique Trace id (derived from initial block)768Block ** _next_list; // Array mapping index to next block769Block ** _prev_list; // Array mapping index to previous block770Block * _first; // First block in the trace771Block * _last; // Last block in the trace772773// Return the block that follows "b" in the trace.774Block * next(Block *b) const { return _next_list[b->_pre_order]; }775void set_next(Block *b, Block *n) const { _next_list[b->_pre_order] = n; }776777// Return the block that precedes "b" in the trace.778Block * prev(Block *b) const { return _prev_list[b->_pre_order]; }779void set_prev(Block *b, Block *p) const { _prev_list[b->_pre_order] = p; }780781// We've discovered a loop in this trace. Reset last to be "b", and first as782// the block following "b783void break_loop_after(Block *b) {784_last = b;785_first = next(b);786set_prev(_first, NULL);787set_next(_last, NULL);788}789790public:791792Trace(Block *b, Block **next_list, Block **prev_list) :793_first(b),794_last(b),795_next_list(next_list),796_prev_list(prev_list),797_id(b->_pre_order) {798set_next(b, NULL);799set_prev(b, NULL);800};801802// Return the id number803uint id() const { return _id; }804void set_id(uint id) { _id = id; }805806// Return the first block in the trace807Block * first_block() const { return _first; }808809// Return the last block in the trace810Block * last_block() const { return _last; }811812// Insert a trace in the middle of this one after b813void insert_after(Block *b, Trace *tr) {814set_next(tr->last_block(), next(b));815if (next(b) != NULL) {816set_prev(next(b), tr->last_block());817}818819set_next(b, tr->first_block());820set_prev(tr->first_block(), b);821822if (b == _last) {823_last = tr->last_block();824}825}826827void insert_before(Block *b, Trace *tr) {828Block *p = prev(b);829assert(p != NULL, "use append instead");830insert_after(p, tr);831}832833// Append another trace to this one.834void append(Trace *tr) {835insert_after(_last, tr);836}837838// Append a block at the end of this trace839void append(Block *b) {840set_next(_last, b);841set_prev(b, _last);842_last = b;843}844845// Adjust the the blocks in this trace846void fixup_blocks(PhaseCFG &cfg);847bool backedge(CFGEdge *e);848849#ifndef PRODUCT850void dump( ) const;851#endif852};853854//------------------------------PhaseBlockLayout-------------------------------855// Rearrange blocks into some canonical order, based on edges and their frequencies856class PhaseBlockLayout : public Phase {857friend class VMStructs;858PhaseCFG &_cfg; // Control flow graph859860GrowableArray<CFGEdge *> *edges;861Trace **traces;862Block **next;863Block **prev;864UnionFind *uf;865866// Given a block, find its encompassing Trace867Trace * trace(Block *b) {868return traces[uf->Find_compress(b->_pre_order)];869}870public:871PhaseBlockLayout(PhaseCFG &cfg);872873void find_edges();874void grow_traces();875void merge_traces(bool loose_connections);876void reorder_traces(int count);877void union_traces(Trace* from, Trace* to);878};879880#endif // SHARE_VM_OPTO_BLOCK_HPP881882883