Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_OPTO_CHAITIN_HPP |
aoqi@0 | 26 | #define SHARE_VM_OPTO_CHAITIN_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "code/vmreg.hpp" |
aoqi@0 | 29 | #include "libadt/port.hpp" |
aoqi@0 | 30 | #include "memory/resourceArea.hpp" |
aoqi@0 | 31 | #include "opto/connode.hpp" |
aoqi@0 | 32 | #include "opto/live.hpp" |
aoqi@0 | 33 | #include "opto/matcher.hpp" |
aoqi@0 | 34 | #include "opto/phase.hpp" |
aoqi@0 | 35 | #include "opto/regalloc.hpp" |
aoqi@0 | 36 | #include "opto/regmask.hpp" |
aoqi@0 | 37 | |
aoqi@0 | 38 | class LoopTree; |
aoqi@0 | 39 | class MachCallNode; |
aoqi@0 | 40 | class MachSafePointNode; |
aoqi@0 | 41 | class Matcher; |
aoqi@0 | 42 | class PhaseCFG; |
aoqi@0 | 43 | class PhaseLive; |
aoqi@0 | 44 | class PhaseRegAlloc; |
aoqi@0 | 45 | class PhaseChaitin; |
aoqi@0 | 46 | |
aoqi@0 | 47 | #define OPTO_DEBUG_SPLIT_FREQ BLOCK_FREQUENCY(0.001) |
aoqi@0 | 48 | #define OPTO_LRG_HIGH_FREQ BLOCK_FREQUENCY(0.25) |
aoqi@0 | 49 | |
aoqi@0 | 50 | //------------------------------LRG-------------------------------------------- |
aoqi@0 | 51 | // Live-RanGe structure. |
aoqi@0 | 52 | class LRG : public ResourceObj { |
aoqi@0 | 53 | friend class VMStructs; |
aoqi@0 | 54 | public: |
aoqi@0 | 55 | static const uint AllStack_size = 0xFFFFF; // This mask size is used to tell that the mask of this LRG supports stack positions |
aoqi@0 | 56 | enum { SPILL_REG=29999 }; // Register number of a spilled LRG |
aoqi@0 | 57 | |
aoqi@0 | 58 | double _cost; // 2 for loads/1 for stores times block freq |
aoqi@0 | 59 | double _area; // Sum of all simultaneously live values |
aoqi@0 | 60 | double score() const; // Compute score from cost and area |
aoqi@0 | 61 | double _maxfreq; // Maximum frequency of any def or use |
aoqi@0 | 62 | |
aoqi@0 | 63 | Node *_def; // Check for multi-def live ranges |
aoqi@0 | 64 | #ifndef PRODUCT |
aoqi@0 | 65 | GrowableArray<Node*>* _defs; |
aoqi@0 | 66 | #endif |
aoqi@0 | 67 | |
aoqi@0 | 68 | uint _risk_bias; // Index of LRG which we want to avoid color |
aoqi@0 | 69 | uint _copy_bias; // Index of LRG which we want to share color |
aoqi@0 | 70 | |
aoqi@0 | 71 | uint _next; // Index of next LRG in linked list |
aoqi@0 | 72 | uint _prev; // Index of prev LRG in linked list |
aoqi@0 | 73 | private: |
aoqi@0 | 74 | uint _reg; // Chosen register; undefined if mask is plural |
aoqi@0 | 75 | public: |
aoqi@0 | 76 | // Return chosen register for this LRG. Error if the LRG is not bound to |
aoqi@0 | 77 | // a single register. |
aoqi@0 | 78 | OptoReg::Name reg() const { return OptoReg::Name(_reg); } |
aoqi@0 | 79 | void set_reg( OptoReg::Name r ) { _reg = r; } |
aoqi@0 | 80 | |
aoqi@0 | 81 | private: |
aoqi@0 | 82 | uint _eff_degree; // Effective degree: Sum of neighbors _num_regs |
aoqi@0 | 83 | public: |
aoqi@0 | 84 | int degree() const { assert( _degree_valid , "" ); return _eff_degree; } |
aoqi@0 | 85 | // Degree starts not valid and any change to the IFG neighbor |
aoqi@0 | 86 | // set makes it not valid. |
aoqi@0 | 87 | void set_degree( uint degree ) { |
aoqi@0 | 88 | _eff_degree = degree; |
aoqi@0 | 89 | debug_only(_degree_valid = 1;) |
aoqi@0 | 90 | assert(!_mask.is_AllStack() || (_mask.is_AllStack() && lo_degree()), "_eff_degree can't be bigger than AllStack_size - _num_regs if the mask supports stack registers"); |
aoqi@0 | 91 | } |
aoqi@0 | 92 | // Made a change that hammered degree |
aoqi@0 | 93 | void invalid_degree() { debug_only(_degree_valid=0;) } |
aoqi@0 | 94 | // Incrementally modify degree. If it was correct, it should remain correct |
aoqi@0 | 95 | void inc_degree( uint mod ) { |
aoqi@0 | 96 | _eff_degree += mod; |
aoqi@0 | 97 | assert(!_mask.is_AllStack() || (_mask.is_AllStack() && lo_degree()), "_eff_degree can't be bigger than AllStack_size - _num_regs if the mask supports stack registers"); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | // Compute the degree between 2 live ranges |
aoqi@0 | 100 | int compute_degree( LRG &l ) const; |
aoqi@0 | 101 | |
aoqi@0 | 102 | private: |
aoqi@0 | 103 | RegMask _mask; // Allowed registers for this LRG |
aoqi@0 | 104 | uint _mask_size; // cache of _mask.Size(); |
aoqi@0 | 105 | public: |
aoqi@0 | 106 | int compute_mask_size() const { return _mask.is_AllStack() ? AllStack_size : _mask.Size(); } |
aoqi@0 | 107 | void set_mask_size( int size ) { |
aoqi@0 | 108 | assert((size == (int)AllStack_size) || (size == (int)_mask.Size()), ""); |
aoqi@0 | 109 | _mask_size = size; |
aoqi@0 | 110 | #ifdef ASSERT |
aoqi@0 | 111 | _msize_valid=1; |
aoqi@0 | 112 | if (_is_vector) { |
aoqi@0 | 113 | assert(!_fat_proj, "sanity"); |
aoqi@0 | 114 | _mask.verify_sets(_num_regs); |
aoqi@0 | 115 | } else if (_num_regs == 2 && !_fat_proj) { |
aoqi@0 | 116 | _mask.verify_pairs(); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | #endif |
aoqi@0 | 119 | } |
aoqi@0 | 120 | void compute_set_mask_size() { set_mask_size(compute_mask_size()); } |
aoqi@0 | 121 | int mask_size() const { assert( _msize_valid, "mask size not valid" ); |
aoqi@0 | 122 | return _mask_size; } |
aoqi@0 | 123 | // Get the last mask size computed, even if it does not match the |
aoqi@0 | 124 | // count of bits in the current mask. |
aoqi@0 | 125 | int get_invalid_mask_size() const { return _mask_size; } |
aoqi@0 | 126 | const RegMask &mask() const { return _mask; } |
aoqi@0 | 127 | void set_mask( const RegMask &rm ) { _mask = rm; debug_only(_msize_valid=0;)} |
aoqi@0 | 128 | void AND( const RegMask &rm ) { _mask.AND(rm); debug_only(_msize_valid=0;)} |
aoqi@0 | 129 | void SUBTRACT( const RegMask &rm ) { _mask.SUBTRACT(rm); debug_only(_msize_valid=0;)} |
aoqi@0 | 130 | void Clear() { _mask.Clear() ; debug_only(_msize_valid=1); _mask_size = 0; } |
aoqi@0 | 131 | void Set_All() { _mask.Set_All(); debug_only(_msize_valid=1); _mask_size = RegMask::CHUNK_SIZE; } |
aoqi@0 | 132 | void Insert( OptoReg::Name reg ) { _mask.Insert(reg); debug_only(_msize_valid=0;) } |
aoqi@0 | 133 | void Remove( OptoReg::Name reg ) { _mask.Remove(reg); debug_only(_msize_valid=0;) } |
aoqi@0 | 134 | void clear_to_pairs() { _mask.clear_to_pairs(); debug_only(_msize_valid=0;) } |
aoqi@0 | 135 | void clear_to_sets() { _mask.clear_to_sets(_num_regs); debug_only(_msize_valid=0;) } |
aoqi@0 | 136 | |
aoqi@0 | 137 | // Number of registers this live range uses when it colors |
aoqi@0 | 138 | private: |
aoqi@0 | 139 | uint8 _num_regs; // 2 for Longs and Doubles, 1 for all else |
aoqi@0 | 140 | // except _num_regs is kill count for fat_proj |
aoqi@0 | 141 | public: |
aoqi@0 | 142 | int num_regs() const { return _num_regs; } |
aoqi@0 | 143 | void set_num_regs( int reg ) { assert( _num_regs == reg || !_num_regs, "" ); _num_regs = reg; } |
aoqi@0 | 144 | |
aoqi@0 | 145 | private: |
aoqi@0 | 146 | // Number of physical registers this live range uses when it colors |
aoqi@0 | 147 | // Architecture and register-set dependent |
aoqi@0 | 148 | uint8 _reg_pressure; |
aoqi@0 | 149 | public: |
aoqi@0 | 150 | void set_reg_pressure(int i) { _reg_pressure = i; } |
aoqi@0 | 151 | int reg_pressure() const { return _reg_pressure; } |
aoqi@0 | 152 | |
aoqi@0 | 153 | // How much 'wiggle room' does this live range have? |
aoqi@0 | 154 | // How many color choices can it make (scaled by _num_regs)? |
aoqi@0 | 155 | int degrees_of_freedom() const { return mask_size() - _num_regs; } |
aoqi@0 | 156 | // Bound LRGs have ZERO degrees of freedom. We also count |
aoqi@0 | 157 | // must_spill as bound. |
aoqi@0 | 158 | bool is_bound () const { return _is_bound; } |
aoqi@0 | 159 | // Negative degrees-of-freedom; even with no neighbors this |
aoqi@0 | 160 | // live range must spill. |
aoqi@0 | 161 | bool not_free() const { return degrees_of_freedom() < 0; } |
aoqi@0 | 162 | // Is this live range of "low-degree"? Trivially colorable? |
aoqi@0 | 163 | bool lo_degree () const { return degree() <= degrees_of_freedom(); } |
aoqi@0 | 164 | // Is this live range just barely "low-degree"? Trivially colorable? |
aoqi@0 | 165 | bool just_lo_degree () const { return degree() == degrees_of_freedom(); } |
aoqi@0 | 166 | |
aoqi@0 | 167 | uint _is_oop:1, // Live-range holds an oop |
aoqi@0 | 168 | _is_float:1, // True if in float registers |
aoqi@0 | 169 | _is_vector:1, // True if in vector registers |
aoqi@0 | 170 | _was_spilled1:1, // True if prior spilling on def |
aoqi@0 | 171 | _was_spilled2:1, // True if twice prior spilling on def |
aoqi@0 | 172 | _is_bound:1, // live range starts life with no |
aoqi@0 | 173 | // degrees of freedom. |
aoqi@0 | 174 | _direct_conflict:1, // True if def and use registers in conflict |
aoqi@0 | 175 | _must_spill:1, // live range has lost all degrees of freedom |
aoqi@0 | 176 | // If _fat_proj is set, live range does NOT require aligned, adjacent |
aoqi@0 | 177 | // registers and has NO interferences. |
aoqi@0 | 178 | // If _fat_proj is clear, live range requires num_regs() to be a power of |
aoqi@0 | 179 | // 2, and it requires registers to form an aligned, adjacent set. |
aoqi@0 | 180 | _fat_proj:1, // |
aoqi@0 | 181 | _was_lo:1, // Was lo-degree prior to coalesce |
aoqi@0 | 182 | _msize_valid:1, // _mask_size cache valid |
aoqi@0 | 183 | _degree_valid:1, // _degree cache valid |
aoqi@0 | 184 | _has_copy:1, // Adjacent to some copy instruction |
aoqi@0 | 185 | _at_risk:1; // Simplify says this guy is at risk to spill |
aoqi@0 | 186 | |
aoqi@0 | 187 | |
aoqi@0 | 188 | // Alive if non-zero, dead if zero |
aoqi@0 | 189 | bool alive() const { return _def != NULL; } |
aoqi@0 | 190 | bool is_multidef() const { return _def == NodeSentinel; } |
aoqi@0 | 191 | bool is_singledef() const { return _def != NodeSentinel; } |
aoqi@0 | 192 | |
aoqi@0 | 193 | #ifndef PRODUCT |
aoqi@0 | 194 | void dump( ) const; |
aoqi@0 | 195 | #endif |
aoqi@0 | 196 | }; |
aoqi@0 | 197 | |
aoqi@0 | 198 | //------------------------------IFG-------------------------------------------- |
aoqi@0 | 199 | // InterFerence Graph |
aoqi@0 | 200 | // An undirected graph implementation. Created with a fixed number of |
aoqi@0 | 201 | // vertices. Edges can be added & tested. Vertices can be removed, then |
aoqi@0 | 202 | // added back later with all edges intact. Can add edges between one vertex |
aoqi@0 | 203 | // and a list of other vertices. Can union vertices (and their edges) |
aoqi@0 | 204 | // together. The IFG needs to be really really fast, and also fairly |
aoqi@0 | 205 | // abstract! It needs abstraction so I can fiddle with the implementation to |
aoqi@0 | 206 | // get even more speed. |
aoqi@0 | 207 | class PhaseIFG : public Phase { |
aoqi@0 | 208 | friend class VMStructs; |
aoqi@0 | 209 | // Current implementation: a triangular adjacency list. |
aoqi@0 | 210 | |
aoqi@0 | 211 | // Array of adjacency-lists, indexed by live-range number |
aoqi@0 | 212 | IndexSet *_adjs; |
aoqi@0 | 213 | |
aoqi@0 | 214 | // Assertion bit for proper use of Squaring |
aoqi@0 | 215 | bool _is_square; |
aoqi@0 | 216 | |
aoqi@0 | 217 | // Live range structure goes here |
aoqi@0 | 218 | LRG *_lrgs; // Array of LRG structures |
aoqi@0 | 219 | |
aoqi@0 | 220 | public: |
aoqi@0 | 221 | // Largest live-range number |
aoqi@0 | 222 | uint _maxlrg; |
aoqi@0 | 223 | |
aoqi@0 | 224 | Arena *_arena; |
aoqi@0 | 225 | |
aoqi@0 | 226 | // Keep track of inserted and deleted Nodes |
aoqi@0 | 227 | VectorSet *_yanked; |
aoqi@0 | 228 | |
aoqi@0 | 229 | PhaseIFG( Arena *arena ); |
aoqi@0 | 230 | void init( uint maxlrg ); |
aoqi@0 | 231 | |
aoqi@0 | 232 | // Add edge between a and b. Returns true if actually addded. |
aoqi@0 | 233 | int add_edge( uint a, uint b ); |
aoqi@0 | 234 | |
aoqi@0 | 235 | // Add edge between a and everything in the vector |
aoqi@0 | 236 | void add_vector( uint a, IndexSet *vec ); |
aoqi@0 | 237 | |
aoqi@0 | 238 | // Test for edge existance |
aoqi@0 | 239 | int test_edge( uint a, uint b ) const; |
aoqi@0 | 240 | |
aoqi@0 | 241 | // Square-up matrix for faster Union |
aoqi@0 | 242 | void SquareUp(); |
aoqi@0 | 243 | |
aoqi@0 | 244 | // Return number of LRG neighbors |
aoqi@0 | 245 | uint neighbor_cnt( uint a ) const { return _adjs[a].count(); } |
aoqi@0 | 246 | // Union edges of b into a on Squared-up matrix |
aoqi@0 | 247 | void Union( uint a, uint b ); |
aoqi@0 | 248 | // Test for edge in Squared-up matrix |
aoqi@0 | 249 | int test_edge_sq( uint a, uint b ) const; |
aoqi@0 | 250 | // Yank a Node and all connected edges from the IFG. Be prepared to |
aoqi@0 | 251 | // re-insert the yanked Node in reverse order of yanking. Return a |
aoqi@0 | 252 | // list of neighbors (edges) yanked. |
aoqi@0 | 253 | IndexSet *remove_node( uint a ); |
aoqi@0 | 254 | // Reinsert a yanked Node |
aoqi@0 | 255 | void re_insert( uint a ); |
aoqi@0 | 256 | // Return set of neighbors |
aoqi@0 | 257 | IndexSet *neighbors( uint a ) const { return &_adjs[a]; } |
aoqi@0 | 258 | |
aoqi@0 | 259 | #ifndef PRODUCT |
aoqi@0 | 260 | // Dump the IFG |
aoqi@0 | 261 | void dump() const; |
aoqi@0 | 262 | void stats() const; |
aoqi@0 | 263 | void verify( const PhaseChaitin * ) const; |
aoqi@0 | 264 | #endif |
aoqi@0 | 265 | |
aoqi@0 | 266 | //--------------- Live Range Accessors |
aoqi@0 | 267 | LRG &lrgs(uint idx) const { assert(idx < _maxlrg, "oob"); return _lrgs[idx]; } |
aoqi@0 | 268 | |
aoqi@0 | 269 | // Compute and set effective degree. Might be folded into SquareUp(). |
aoqi@0 | 270 | void Compute_Effective_Degree(); |
aoqi@0 | 271 | |
aoqi@0 | 272 | // Compute effective degree as the sum of neighbors' _sizes. |
aoqi@0 | 273 | int effective_degree( uint lidx ) const; |
aoqi@0 | 274 | }; |
aoqi@0 | 275 | |
aoqi@0 | 276 | // The LiveRangeMap class is responsible for storing node to live range id mapping. |
aoqi@0 | 277 | // Each node is mapped to a live range id (a virtual register). Nodes that are |
aoqi@0 | 278 | // not considered for register allocation are given live range id 0. |
aoqi@0 | 279 | class LiveRangeMap VALUE_OBJ_CLASS_SPEC { |
aoqi@0 | 280 | |
aoqi@0 | 281 | private: |
aoqi@0 | 282 | |
aoqi@0 | 283 | uint _max_lrg_id; |
aoqi@0 | 284 | |
aoqi@0 | 285 | // Union-find map. Declared as a short for speed. |
aoqi@0 | 286 | // Indexed by live-range number, it returns the compacted live-range number |
aoqi@0 | 287 | LRG_List _uf_map; |
aoqi@0 | 288 | |
aoqi@0 | 289 | // Map from Nodes to live ranges |
aoqi@0 | 290 | LRG_List _names; |
aoqi@0 | 291 | |
aoqi@0 | 292 | // Straight out of Tarjan's union-find algorithm |
aoqi@0 | 293 | uint find_compress(const Node *node) { |
aoqi@0 | 294 | uint lrg_id = find_compress(_names.at(node->_idx)); |
aoqi@0 | 295 | _names.at_put(node->_idx, lrg_id); |
aoqi@0 | 296 | return lrg_id; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | |
aoqi@0 | 299 | uint find_compress(uint lrg); |
aoqi@0 | 300 | |
aoqi@0 | 301 | public: |
aoqi@0 | 302 | |
aoqi@0 | 303 | const LRG_List& names() { |
aoqi@0 | 304 | return _names; |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | uint max_lrg_id() const { |
aoqi@0 | 308 | return _max_lrg_id; |
aoqi@0 | 309 | } |
aoqi@0 | 310 | |
aoqi@0 | 311 | void set_max_lrg_id(uint max_lrg_id) { |
aoqi@0 | 312 | _max_lrg_id = max_lrg_id; |
aoqi@0 | 313 | } |
aoqi@0 | 314 | |
aoqi@0 | 315 | uint size() const { |
aoqi@0 | 316 | return _names.length(); |
aoqi@0 | 317 | } |
aoqi@0 | 318 | |
aoqi@0 | 319 | uint live_range_id(uint idx) const { |
aoqi@0 | 320 | return _names.at(idx); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | uint live_range_id(const Node *node) const { |
aoqi@0 | 324 | return _names.at(node->_idx); |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | uint uf_live_range_id(uint lrg_id) const { |
aoqi@0 | 328 | return _uf_map.at(lrg_id); |
aoqi@0 | 329 | } |
aoqi@0 | 330 | |
aoqi@0 | 331 | void map(uint idx, uint lrg_id) { |
aoqi@0 | 332 | _names.at_put(idx, lrg_id); |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | void uf_map(uint dst_lrg_id, uint src_lrg_id) { |
aoqi@0 | 336 | _uf_map.at_put(dst_lrg_id, src_lrg_id); |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | void extend(uint idx, uint lrg_id) { |
aoqi@0 | 340 | _names.at_put_grow(idx, lrg_id); |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | void uf_extend(uint dst_lrg_id, uint src_lrg_id) { |
aoqi@0 | 344 | _uf_map.at_put_grow(dst_lrg_id, src_lrg_id); |
aoqi@0 | 345 | } |
aoqi@0 | 346 | |
aoqi@0 | 347 | LiveRangeMap(Arena* arena, uint unique) |
aoqi@0 | 348 | : _names(arena, unique, unique, 0) |
aoqi@0 | 349 | , _uf_map(arena, unique, unique, 0) |
aoqi@0 | 350 | , _max_lrg_id(0) {} |
aoqi@0 | 351 | |
aoqi@0 | 352 | uint find_id( const Node *n ) { |
aoqi@0 | 353 | uint retval = live_range_id(n); |
aoqi@0 | 354 | assert(retval == find(n),"Invalid node to lidx mapping"); |
aoqi@0 | 355 | return retval; |
aoqi@0 | 356 | } |
aoqi@0 | 357 | |
aoqi@0 | 358 | // Reset the Union-Find map to identity |
aoqi@0 | 359 | void reset_uf_map(uint max_lrg_id); |
aoqi@0 | 360 | |
aoqi@0 | 361 | // Make all Nodes map directly to their final live range; no need for |
aoqi@0 | 362 | // the Union-Find mapping after this call. |
aoqi@0 | 363 | void compress_uf_map_for_nodes(); |
aoqi@0 | 364 | |
aoqi@0 | 365 | uint find(uint lidx) { |
aoqi@0 | 366 | uint uf_lidx = _uf_map.at(lidx); |
aoqi@0 | 367 | return (uf_lidx == lidx) ? uf_lidx : find_compress(lidx); |
aoqi@0 | 368 | } |
aoqi@0 | 369 | |
aoqi@0 | 370 | // Convert a Node into a Live Range Index - a lidx |
aoqi@0 | 371 | uint find(const Node *node) { |
aoqi@0 | 372 | uint lidx = live_range_id(node); |
aoqi@0 | 373 | uint uf_lidx = _uf_map.at(lidx); |
aoqi@0 | 374 | return (uf_lidx == lidx) ? uf_lidx : find_compress(node); |
aoqi@0 | 375 | } |
aoqi@0 | 376 | |
aoqi@0 | 377 | // Like Find above, but no path compress, so bad asymptotic behavior |
aoqi@0 | 378 | uint find_const(uint lrg) const; |
aoqi@0 | 379 | |
aoqi@0 | 380 | // Like Find above, but no path compress, so bad asymptotic behavior |
aoqi@0 | 381 | uint find_const(const Node *node) const { |
aoqi@0 | 382 | if(node->_idx >= (uint)_names.length()) { |
aoqi@0 | 383 | return 0; // not mapped, usual for debug dump |
aoqi@0 | 384 | } |
aoqi@0 | 385 | return find_const(_names.at(node->_idx)); |
aoqi@0 | 386 | } |
aoqi@0 | 387 | }; |
aoqi@0 | 388 | |
aoqi@0 | 389 | //------------------------------Chaitin---------------------------------------- |
aoqi@0 | 390 | // Briggs-Chaitin style allocation, mostly. |
aoqi@0 | 391 | class PhaseChaitin : public PhaseRegAlloc { |
aoqi@0 | 392 | friend class VMStructs; |
aoqi@0 | 393 | |
aoqi@0 | 394 | int _trip_cnt; |
aoqi@0 | 395 | int _alternate; |
aoqi@0 | 396 | |
aoqi@0 | 397 | LRG &lrgs(uint idx) const { return _ifg->lrgs(idx); } |
aoqi@0 | 398 | PhaseLive *_live; // Liveness, used in the interference graph |
aoqi@0 | 399 | PhaseIFG *_ifg; // Interference graph (for original chunk) |
aoqi@0 | 400 | Node_List **_lrg_nodes; // Array of node; lists for lrgs which spill |
aoqi@0 | 401 | VectorSet _spilled_once; // Nodes that have been spilled |
aoqi@0 | 402 | VectorSet _spilled_twice; // Nodes that have been spilled twice |
aoqi@0 | 403 | |
aoqi@0 | 404 | // Combine the Live Range Indices for these 2 Nodes into a single live |
aoqi@0 | 405 | // range. Future requests for any Node in either live range will |
aoqi@0 | 406 | // return the live range index for the combined live range. |
aoqi@0 | 407 | void Union( const Node *src, const Node *dst ); |
aoqi@0 | 408 | |
aoqi@0 | 409 | void new_lrg( const Node *x, uint lrg ); |
aoqi@0 | 410 | |
aoqi@0 | 411 | // Compact live ranges, removing unused ones. Return new maxlrg. |
aoqi@0 | 412 | void compact(); |
aoqi@0 | 413 | |
aoqi@0 | 414 | uint _lo_degree; // Head of lo-degree LRGs list |
aoqi@0 | 415 | uint _lo_stk_degree; // Head of lo-stk-degree LRGs list |
aoqi@0 | 416 | uint _hi_degree; // Head of hi-degree LRGs list |
aoqi@0 | 417 | uint _simplified; // Linked list head of simplified LRGs |
aoqi@0 | 418 | |
aoqi@0 | 419 | // Helper functions for Split() |
aoqi@0 | 420 | uint split_DEF( Node *def, Block *b, int loc, uint max, Node **Reachblock, Node **debug_defs, GrowableArray<uint> splits, int slidx ); |
aoqi@0 | 421 | uint split_USE( Node *def, Block *b, Node *use, uint useidx, uint max, bool def_down, bool cisc_sp, GrowableArray<uint> splits, int slidx ); |
aoqi@0 | 422 | |
aoqi@0 | 423 | //------------------------------clone_projs------------------------------------ |
aoqi@0 | 424 | // After cloning some rematerialized instruction, clone any MachProj's that |
aoqi@0 | 425 | // follow it. Example: Intel zero is XOR, kills flags. Sparc FP constants |
aoqi@0 | 426 | // use G3 as an address temp. |
aoqi@0 | 427 | int clone_projs(Block* b, uint idx, Node* orig, Node* copy, uint& max_lrg_id); |
aoqi@0 | 428 | |
aoqi@0 | 429 | int clone_projs(Block* b, uint idx, Node* orig, Node* copy, LiveRangeMap& lrg_map) { |
aoqi@0 | 430 | uint max_lrg_id = lrg_map.max_lrg_id(); |
aoqi@0 | 431 | int found_projs = clone_projs(b, idx, orig, copy, max_lrg_id); |
aoqi@0 | 432 | if (found_projs > 0) { |
aoqi@0 | 433 | // max_lrg_id is updated during call above |
aoqi@0 | 434 | lrg_map.set_max_lrg_id(max_lrg_id); |
aoqi@0 | 435 | } |
aoqi@0 | 436 | return found_projs; |
aoqi@0 | 437 | } |
aoqi@0 | 438 | |
aoqi@0 | 439 | Node *split_Rematerialize(Node *def, Block *b, uint insidx, uint &maxlrg, GrowableArray<uint> splits, |
aoqi@0 | 440 | int slidx, uint *lrg2reach, Node **Reachblock, bool walkThru); |
aoqi@0 | 441 | // True if lidx is used before any real register is def'd in the block |
aoqi@0 | 442 | bool prompt_use( Block *b, uint lidx ); |
aoqi@0 | 443 | Node *get_spillcopy_wide( Node *def, Node *use, uint uidx ); |
aoqi@0 | 444 | // Insert the spill at chosen location. Skip over any intervening Proj's or |
aoqi@0 | 445 | // Phis. Skip over a CatchNode and projs, inserting in the fall-through block |
aoqi@0 | 446 | // instead. Update high-pressure indices. Create a new live range. |
aoqi@0 | 447 | void insert_proj( Block *b, uint i, Node *spill, uint maxlrg ); |
aoqi@0 | 448 | |
aoqi@0 | 449 | bool is_high_pressure( Block *b, LRG *lrg, uint insidx ); |
aoqi@0 | 450 | |
aoqi@0 | 451 | uint _oldphi; // Node index which separates pre-allocation nodes |
aoqi@0 | 452 | |
aoqi@0 | 453 | Block **_blks; // Array of blocks sorted by frequency for coalescing |
aoqi@0 | 454 | |
aoqi@0 | 455 | float _high_frequency_lrg; // Frequency at which LRG will be spilled for debug info |
aoqi@0 | 456 | |
aoqi@0 | 457 | #ifndef PRODUCT |
aoqi@0 | 458 | bool _trace_spilling; |
aoqi@0 | 459 | #endif |
aoqi@0 | 460 | |
aoqi@0 | 461 | public: |
aoqi@0 | 462 | PhaseChaitin( uint unique, PhaseCFG &cfg, Matcher &matcher ); |
aoqi@0 | 463 | ~PhaseChaitin() {} |
aoqi@0 | 464 | |
aoqi@0 | 465 | LiveRangeMap _lrg_map; |
aoqi@0 | 466 | |
aoqi@0 | 467 | // Do all the real work of allocate |
aoqi@0 | 468 | void Register_Allocate(); |
aoqi@0 | 469 | |
aoqi@0 | 470 | float high_frequency_lrg() const { return _high_frequency_lrg; } |
aoqi@0 | 471 | |
aoqi@0 | 472 | #ifndef PRODUCT |
aoqi@0 | 473 | bool trace_spilling() const { return _trace_spilling; } |
aoqi@0 | 474 | #endif |
aoqi@0 | 475 | |
aoqi@0 | 476 | private: |
aoqi@0 | 477 | // De-SSA the world. Assign registers to Nodes. Use the same register for |
aoqi@0 | 478 | // all inputs to a PhiNode, effectively coalescing live ranges. Insert |
aoqi@0 | 479 | // copies as needed. |
aoqi@0 | 480 | void de_ssa(); |
aoqi@0 | 481 | |
aoqi@0 | 482 | // Add edge between reg and everything in the vector. |
aoqi@0 | 483 | // Same as _ifg->add_vector(reg,live) EXCEPT use the RegMask |
aoqi@0 | 484 | // information to trim the set of interferences. Return the |
aoqi@0 | 485 | // count of edges added. |
aoqi@0 | 486 | void interfere_with_live( uint reg, IndexSet *live ); |
aoqi@0 | 487 | // Count register pressure for asserts |
aoqi@0 | 488 | uint count_int_pressure( IndexSet *liveout ); |
aoqi@0 | 489 | uint count_float_pressure( IndexSet *liveout ); |
aoqi@0 | 490 | |
aoqi@0 | 491 | // Build the interference graph using virtual registers only. |
aoqi@0 | 492 | // Used for aggressive coalescing. |
aoqi@0 | 493 | void build_ifg_virtual( ); |
aoqi@0 | 494 | |
aoqi@0 | 495 | // Build the interference graph using physical registers when available. |
aoqi@0 | 496 | // That is, if 2 live ranges are simultaneously alive but in their |
aoqi@0 | 497 | // acceptable register sets do not overlap, then they do not interfere. |
aoqi@0 | 498 | uint build_ifg_physical( ResourceArea *a ); |
aoqi@0 | 499 | |
aoqi@0 | 500 | // Gather LiveRanGe information, including register masks and base pointer/ |
aoqi@0 | 501 | // derived pointer relationships. |
aoqi@0 | 502 | void gather_lrg_masks( bool mod_cisc_masks ); |
aoqi@0 | 503 | |
aoqi@0 | 504 | // Force the bases of derived pointers to be alive at GC points. |
aoqi@0 | 505 | bool stretch_base_pointer_live_ranges( ResourceArea *a ); |
aoqi@0 | 506 | // Helper to stretch above; recursively discover the base Node for |
aoqi@0 | 507 | // a given derived Node. Easy for AddP-related machine nodes, but |
aoqi@0 | 508 | // needs to be recursive for derived Phis. |
aoqi@0 | 509 | Node *find_base_for_derived( Node **derived_base_map, Node *derived, uint &maxlrg ); |
aoqi@0 | 510 | |
aoqi@0 | 511 | // Set the was-lo-degree bit. Conservative coalescing should not change the |
aoqi@0 | 512 | // colorability of the graph. If any live range was of low-degree before |
aoqi@0 | 513 | // coalescing, it should Simplify. This call sets the was-lo-degree bit. |
aoqi@0 | 514 | void set_was_low(); |
aoqi@0 | 515 | |
aoqi@0 | 516 | // Split live-ranges that must spill due to register conflicts (as opposed |
aoqi@0 | 517 | // to capacity spills). Typically these are things def'd in a register |
aoqi@0 | 518 | // and used on the stack or vice-versa. |
aoqi@0 | 519 | void pre_spill(); |
aoqi@0 | 520 | |
aoqi@0 | 521 | // Init LRG caching of degree, numregs. Init lo_degree list. |
aoqi@0 | 522 | void cache_lrg_info( ); |
aoqi@0 | 523 | |
aoqi@0 | 524 | // Simplify the IFG by removing LRGs of low degree with no copies |
aoqi@0 | 525 | void Pre_Simplify(); |
aoqi@0 | 526 | |
aoqi@0 | 527 | // Simplify the IFG by removing LRGs of low degree |
aoqi@0 | 528 | void Simplify(); |
aoqi@0 | 529 | |
aoqi@0 | 530 | // Select colors by re-inserting edges into the IFG. |
aoqi@0 | 531 | // Return TRUE if any spills occurred. |
aoqi@0 | 532 | uint Select( ); |
aoqi@0 | 533 | // Helper function for select which allows biased coloring |
aoqi@0 | 534 | OptoReg::Name choose_color( LRG &lrg, int chunk ); |
aoqi@0 | 535 | // Helper function which implements biasing heuristic |
aoqi@0 | 536 | OptoReg::Name bias_color( LRG &lrg, int chunk ); |
aoqi@0 | 537 | |
aoqi@0 | 538 | // Split uncolorable live ranges |
aoqi@0 | 539 | // Return new number of live ranges |
aoqi@0 | 540 | uint Split(uint maxlrg, ResourceArea* split_arena); |
aoqi@0 | 541 | |
aoqi@0 | 542 | // Copy 'was_spilled'-edness from one Node to another. |
aoqi@0 | 543 | void copy_was_spilled( Node *src, Node *dst ); |
aoqi@0 | 544 | // Set the 'spilled_once' or 'spilled_twice' flag on a node. |
aoqi@0 | 545 | void set_was_spilled( Node *n ); |
aoqi@0 | 546 | |
aoqi@0 | 547 | // Convert ideal spill-nodes into machine loads & stores |
aoqi@0 | 548 | // Set C->failing when fixup spills could not complete, node limit exceeded. |
aoqi@0 | 549 | void fixup_spills(); |
aoqi@0 | 550 | |
aoqi@0 | 551 | // Post-Allocation peephole copy removal |
aoqi@0 | 552 | void post_allocate_copy_removal(); |
aoqi@0 | 553 | Node *skip_copies( Node *c ); |
aoqi@0 | 554 | // Replace the old node with the current live version of that value |
aoqi@0 | 555 | // and yank the old value if it's dead. |
aoqi@0 | 556 | int replace_and_yank_if_dead( Node *old, OptoReg::Name nreg, |
aoqi@0 | 557 | Block *current_block, Node_List& value, Node_List& regnd ) { |
aoqi@0 | 558 | Node* v = regnd[nreg]; |
aoqi@0 | 559 | assert(v->outcnt() != 0, "no dead values"); |
aoqi@0 | 560 | old->replace_by(v); |
aoqi@0 | 561 | return yank_if_dead(old, current_block, &value, ®nd); |
aoqi@0 | 562 | } |
aoqi@0 | 563 | |
aoqi@0 | 564 | int yank_if_dead( Node *old, Block *current_block, Node_List *value, Node_List *regnd ) { |
aoqi@0 | 565 | return yank_if_dead_recurse(old, old, current_block, value, regnd); |
aoqi@0 | 566 | } |
aoqi@0 | 567 | int yank_if_dead_recurse(Node *old, Node *orig_old, Block *current_block, |
aoqi@0 | 568 | Node_List *value, Node_List *regnd); |
aoqi@0 | 569 | int yank( Node *old, Block *current_block, Node_List *value, Node_List *regnd ); |
aoqi@0 | 570 | int elide_copy( Node *n, int k, Block *current_block, Node_List &value, Node_List ®nd, bool can_change_regs ); |
aoqi@0 | 571 | int use_prior_register( Node *copy, uint idx, Node *def, Block *current_block, Node_List &value, Node_List ®nd ); |
aoqi@0 | 572 | bool may_be_copy_of_callee( Node *def ) const; |
aoqi@0 | 573 | |
aoqi@0 | 574 | // If nreg already contains the same constant as val then eliminate it |
aoqi@0 | 575 | bool eliminate_copy_of_constant(Node* val, Node* n, |
aoqi@0 | 576 | Block *current_block, Node_List& value, Node_List ®nd, |
aoqi@0 | 577 | OptoReg::Name nreg, OptoReg::Name nreg2); |
aoqi@0 | 578 | // Extend the node to LRG mapping |
aoqi@0 | 579 | void add_reference( const Node *node, const Node *old_node); |
aoqi@0 | 580 | |
aoqi@0 | 581 | private: |
aoqi@0 | 582 | |
aoqi@0 | 583 | static int _final_loads, _final_stores, _final_copies, _final_memoves; |
aoqi@0 | 584 | static double _final_load_cost, _final_store_cost, _final_copy_cost, _final_memove_cost; |
aoqi@0 | 585 | static int _conserv_coalesce, _conserv_coalesce_pair; |
aoqi@0 | 586 | static int _conserv_coalesce_trie, _conserv_coalesce_quad; |
aoqi@0 | 587 | static int _post_alloc; |
aoqi@0 | 588 | static int _lost_opp_pp_coalesce, _lost_opp_cflow_coalesce; |
aoqi@0 | 589 | static int _used_cisc_instructions, _unused_cisc_instructions; |
aoqi@0 | 590 | static int _allocator_attempts, _allocator_successes; |
aoqi@0 | 591 | |
aoqi@0 | 592 | #ifndef PRODUCT |
aoqi@0 | 593 | static uint _high_pressure, _low_pressure; |
aoqi@0 | 594 | |
aoqi@0 | 595 | void dump() const; |
aoqi@0 | 596 | void dump( const Node *n ) const; |
aoqi@0 | 597 | void dump( const Block * b ) const; |
aoqi@0 | 598 | void dump_degree_lists() const; |
aoqi@0 | 599 | void dump_simplified() const; |
aoqi@0 | 600 | void dump_lrg( uint lidx, bool defs_only) const; |
aoqi@0 | 601 | void dump_lrg( uint lidx) const { |
aoqi@0 | 602 | // dump defs and uses by default |
aoqi@0 | 603 | dump_lrg(lidx, false); |
aoqi@0 | 604 | } |
aoqi@0 | 605 | void dump_bb( uint pre_order ) const; |
aoqi@0 | 606 | |
aoqi@0 | 607 | // Verify that base pointers and derived pointers are still sane |
aoqi@0 | 608 | void verify_base_ptrs( ResourceArea *a ) const; |
aoqi@0 | 609 | |
aoqi@0 | 610 | void verify( ResourceArea *a, bool verify_ifg = false ) const; |
aoqi@0 | 611 | |
aoqi@0 | 612 | void dump_for_spill_split_recycle() const; |
aoqi@0 | 613 | |
aoqi@0 | 614 | public: |
aoqi@0 | 615 | void dump_frame() const; |
aoqi@0 | 616 | char *dump_register( const Node *n, char *buf ) const; |
aoqi@0 | 617 | private: |
aoqi@0 | 618 | static void print_chaitin_statistics(); |
aoqi@0 | 619 | #endif |
aoqi@0 | 620 | friend class PhaseCoalesce; |
aoqi@0 | 621 | friend class PhaseAggressiveCoalesce; |
aoqi@0 | 622 | friend class PhaseConservativeCoalesce; |
aoqi@0 | 623 | }; |
aoqi@0 | 624 | |
aoqi@0 | 625 | #endif // SHARE_VM_OPTO_CHAITIN_HPP |