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