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