src/share/vm/opto/chaitin.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 7994
04ff2f6cd0eb
child 9122
024be04bb151
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

aoqi@0 1 /*
dlong@7598 2 * Copyright (c) 2000, 2015, 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 #include "precompiled.hpp"
aoqi@0 26 #include "compiler/compileLog.hpp"
aoqi@0 27 #include "compiler/oopMap.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "opto/addnode.hpp"
aoqi@0 30 #include "opto/block.hpp"
aoqi@0 31 #include "opto/callnode.hpp"
aoqi@0 32 #include "opto/cfgnode.hpp"
aoqi@0 33 #include "opto/chaitin.hpp"
aoqi@0 34 #include "opto/coalesce.hpp"
aoqi@0 35 #include "opto/connode.hpp"
aoqi@0 36 #include "opto/idealGraphPrinter.hpp"
aoqi@0 37 #include "opto/indexSet.hpp"
aoqi@0 38 #include "opto/machnode.hpp"
aoqi@0 39 #include "opto/memnode.hpp"
aoqi@0 40 #include "opto/opcodes.hpp"
aoqi@0 41 #include "opto/rootnode.hpp"
aoqi@0 42
aoqi@0 43 #ifndef PRODUCT
aoqi@0 44 void LRG::dump() const {
aoqi@0 45 ttyLocker ttyl;
aoqi@0 46 tty->print("%d ",num_regs());
aoqi@0 47 _mask.dump();
aoqi@0 48 if( _msize_valid ) {
aoqi@0 49 if( mask_size() == compute_mask_size() ) tty->print(", #%d ",_mask_size);
aoqi@0 50 else tty->print(", #!!!_%d_vs_%d ",_mask_size,_mask.Size());
aoqi@0 51 } else {
aoqi@0 52 tty->print(", #?(%d) ",_mask.Size());
aoqi@0 53 }
aoqi@0 54
aoqi@0 55 tty->print("EffDeg: ");
aoqi@0 56 if( _degree_valid ) tty->print( "%d ", _eff_degree );
aoqi@0 57 else tty->print("? ");
aoqi@0 58
aoqi@0 59 if( is_multidef() ) {
aoqi@0 60 tty->print("MultiDef ");
aoqi@0 61 if (_defs != NULL) {
aoqi@0 62 tty->print("(");
aoqi@0 63 for (int i = 0; i < _defs->length(); i++) {
aoqi@0 64 tty->print("N%d ", _defs->at(i)->_idx);
aoqi@0 65 }
aoqi@0 66 tty->print(") ");
aoqi@0 67 }
aoqi@0 68 }
aoqi@0 69 else if( _def == 0 ) tty->print("Dead ");
aoqi@0 70 else tty->print("Def: N%d ",_def->_idx);
aoqi@0 71
aoqi@0 72 tty->print("Cost:%4.2g Area:%4.2g Score:%4.2g ",_cost,_area, score());
aoqi@0 73 // Flags
aoqi@0 74 if( _is_oop ) tty->print("Oop ");
aoqi@0 75 if( _is_float ) tty->print("Float ");
aoqi@0 76 if( _is_vector ) tty->print("Vector ");
aoqi@0 77 if( _was_spilled1 ) tty->print("Spilled ");
aoqi@0 78 if( _was_spilled2 ) tty->print("Spilled2 ");
aoqi@0 79 if( _direct_conflict ) tty->print("Direct_conflict ");
aoqi@0 80 if( _fat_proj ) tty->print("Fat ");
aoqi@0 81 if( _was_lo ) tty->print("Lo ");
aoqi@0 82 if( _has_copy ) tty->print("Copy ");
aoqi@0 83 if( _at_risk ) tty->print("Risk ");
aoqi@0 84
aoqi@0 85 if( _must_spill ) tty->print("Must_spill ");
aoqi@0 86 if( _is_bound ) tty->print("Bound ");
aoqi@0 87 if( _msize_valid ) {
aoqi@0 88 if( _degree_valid && lo_degree() ) tty->print("Trivial ");
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 tty->cr();
aoqi@0 92 }
aoqi@0 93 #endif
aoqi@0 94
aoqi@0 95 // Compute score from cost and area. Low score is best to spill.
aoqi@0 96 static double raw_score( double cost, double area ) {
aoqi@0 97 return cost - (area*RegisterCostAreaRatio) * 1.52588e-5;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 double LRG::score() const {
aoqi@0 101 // Scale _area by RegisterCostAreaRatio/64K then subtract from cost.
aoqi@0 102 // Bigger area lowers score, encourages spilling this live range.
aoqi@0 103 // Bigger cost raise score, prevents spilling this live range.
aoqi@0 104 // (Note: 1/65536 is the magic constant below; I dont trust the C optimizer
aoqi@0 105 // to turn a divide by a constant into a multiply by the reciprical).
aoqi@0 106 double score = raw_score( _cost, _area);
aoqi@0 107
aoqi@0 108 // Account for area. Basically, LRGs covering large areas are better
aoqi@0 109 // to spill because more other LRGs get freed up.
aoqi@0 110 if( _area == 0.0 ) // No area? Then no progress to spill
aoqi@0 111 return 1e35;
aoqi@0 112
aoqi@0 113 if( _was_spilled2 ) // If spilled once before, we are unlikely
aoqi@0 114 return score + 1e30; // to make progress again.
aoqi@0 115
aoqi@0 116 if( _cost >= _area*3.0 ) // Tiny area relative to cost
aoqi@0 117 return score + 1e17; // Probably no progress to spill
aoqi@0 118
aoqi@0 119 if( (_cost+_cost) >= _area*3.0 ) // Small area relative to cost
aoqi@0 120 return score + 1e10; // Likely no progress to spill
aoqi@0 121
aoqi@0 122 return score;
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 #define NUMBUCKS 3
aoqi@0 126
aoqi@0 127 // Straight out of Tarjan's union-find algorithm
aoqi@0 128 uint LiveRangeMap::find_compress(uint lrg) {
aoqi@0 129 uint cur = lrg;
aoqi@0 130 uint next = _uf_map.at(cur);
aoqi@0 131 while (next != cur) { // Scan chain of equivalences
aoqi@0 132 assert( next < cur, "always union smaller");
aoqi@0 133 cur = next; // until find a fixed-point
aoqi@0 134 next = _uf_map.at(cur);
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 // Core of union-find algorithm: update chain of
aoqi@0 138 // equivalences to be equal to the root.
aoqi@0 139 while (lrg != next) {
aoqi@0 140 uint tmp = _uf_map.at(lrg);
aoqi@0 141 _uf_map.at_put(lrg, next);
aoqi@0 142 lrg = tmp;
aoqi@0 143 }
aoqi@0 144 return lrg;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 // Reset the Union-Find map to identity
aoqi@0 148 void LiveRangeMap::reset_uf_map(uint max_lrg_id) {
aoqi@0 149 _max_lrg_id= max_lrg_id;
aoqi@0 150 // Force the Union-Find mapping to be at least this large
aoqi@0 151 _uf_map.at_put_grow(_max_lrg_id, 0);
aoqi@0 152 // Initialize it to be the ID mapping.
aoqi@0 153 for (uint i = 0; i < _max_lrg_id; ++i) {
aoqi@0 154 _uf_map.at_put(i, i);
aoqi@0 155 }
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 // Make all Nodes map directly to their final live range; no need for
aoqi@0 159 // the Union-Find mapping after this call.
aoqi@0 160 void LiveRangeMap::compress_uf_map_for_nodes() {
aoqi@0 161 // For all Nodes, compress mapping
aoqi@0 162 uint unique = _names.length();
aoqi@0 163 for (uint i = 0; i < unique; ++i) {
aoqi@0 164 uint lrg = _names.at(i);
aoqi@0 165 uint compressed_lrg = find(lrg);
aoqi@0 166 if (lrg != compressed_lrg) {
aoqi@0 167 _names.at_put(i, compressed_lrg);
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 // Like Find above, but no path compress, so bad asymptotic behavior
aoqi@0 173 uint LiveRangeMap::find_const(uint lrg) const {
aoqi@0 174 if (!lrg) {
aoqi@0 175 return lrg; // Ignore the zero LRG
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 // Off the end? This happens during debugging dumps when you got
aoqi@0 179 // brand new live ranges but have not told the allocator yet.
aoqi@0 180 if (lrg >= _max_lrg_id) {
aoqi@0 181 return lrg;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 uint next = _uf_map.at(lrg);
aoqi@0 185 while (next != lrg) { // Scan chain of equivalences
aoqi@0 186 assert(next < lrg, "always union smaller");
aoqi@0 187 lrg = next; // until find a fixed-point
aoqi@0 188 next = _uf_map.at(lrg);
aoqi@0 189 }
aoqi@0 190 return next;
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 PhaseChaitin::PhaseChaitin(uint unique, PhaseCFG &cfg, Matcher &matcher)
aoqi@0 194 : PhaseRegAlloc(unique, cfg, matcher,
aoqi@0 195 #ifndef PRODUCT
aoqi@0 196 print_chaitin_statistics
aoqi@0 197 #else
aoqi@0 198 NULL
aoqi@0 199 #endif
aoqi@0 200 )
aoqi@0 201 , _lrg_map(Thread::current()->resource_area(), unique)
aoqi@0 202 , _live(0)
aoqi@0 203 , _spilled_once(Thread::current()->resource_area())
aoqi@0 204 , _spilled_twice(Thread::current()->resource_area())
aoqi@0 205 , _lo_degree(0), _lo_stk_degree(0), _hi_degree(0), _simplified(0)
aoqi@0 206 , _oldphi(unique)
aoqi@0 207 #ifndef PRODUCT
aoqi@0 208 , _trace_spilling(TraceSpilling || C->method_has_option("TraceSpilling"))
aoqi@0 209 #endif
aoqi@0 210 {
aoqi@0 211 NOT_PRODUCT( Compile::TracePhase t3("ctorChaitin", &_t_ctorChaitin, TimeCompiler); )
aoqi@0 212
aoqi@0 213 _high_frequency_lrg = MIN2(float(OPTO_LRG_HIGH_FREQ), _cfg.get_outer_loop_frequency());
aoqi@0 214
aoqi@0 215 // Build a list of basic blocks, sorted by frequency
aoqi@0 216 _blks = NEW_RESOURCE_ARRAY(Block *, _cfg.number_of_blocks());
aoqi@0 217 // Experiment with sorting strategies to speed compilation
aoqi@0 218 double cutoff = BLOCK_FREQUENCY(1.0); // Cutoff for high frequency bucket
aoqi@0 219 Block **buckets[NUMBUCKS]; // Array of buckets
aoqi@0 220 uint buckcnt[NUMBUCKS]; // Array of bucket counters
aoqi@0 221 double buckval[NUMBUCKS]; // Array of bucket value cutoffs
aoqi@0 222 for (uint i = 0; i < NUMBUCKS; i++) {
aoqi@0 223 buckets[i] = NEW_RESOURCE_ARRAY(Block *, _cfg.number_of_blocks());
aoqi@0 224 buckcnt[i] = 0;
aoqi@0 225 // Bump by three orders of magnitude each time
aoqi@0 226 cutoff *= 0.001;
aoqi@0 227 buckval[i] = cutoff;
aoqi@0 228 for (uint j = 0; j < _cfg.number_of_blocks(); j++) {
aoqi@0 229 buckets[i][j] = NULL;
aoqi@0 230 }
aoqi@0 231 }
aoqi@0 232 // Sort blocks into buckets
aoqi@0 233 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 234 for (uint j = 0; j < NUMBUCKS; j++) {
aoqi@0 235 if ((j == NUMBUCKS - 1) || (_cfg.get_block(i)->_freq > buckval[j])) {
aoqi@0 236 // Assign block to end of list for appropriate bucket
aoqi@0 237 buckets[j][buckcnt[j]++] = _cfg.get_block(i);
aoqi@0 238 break; // kick out of inner loop
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241 }
aoqi@0 242 // Dump buckets into final block array
aoqi@0 243 uint blkcnt = 0;
aoqi@0 244 for (uint i = 0; i < NUMBUCKS; i++) {
aoqi@0 245 for (uint j = 0; j < buckcnt[i]; j++) {
aoqi@0 246 _blks[blkcnt++] = buckets[i][j];
aoqi@0 247 }
aoqi@0 248 }
aoqi@0 249
aoqi@0 250 assert(blkcnt == _cfg.number_of_blocks(), "Block array not totally filled");
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 // union 2 sets together.
aoqi@0 254 void PhaseChaitin::Union( const Node *src_n, const Node *dst_n ) {
aoqi@0 255 uint src = _lrg_map.find(src_n);
aoqi@0 256 uint dst = _lrg_map.find(dst_n);
aoqi@0 257 assert(src, "");
aoqi@0 258 assert(dst, "");
aoqi@0 259 assert(src < _lrg_map.max_lrg_id(), "oob");
aoqi@0 260 assert(dst < _lrg_map.max_lrg_id(), "oob");
aoqi@0 261 assert(src < dst, "always union smaller");
aoqi@0 262 _lrg_map.uf_map(dst, src);
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 void PhaseChaitin::new_lrg(const Node *x, uint lrg) {
aoqi@0 266 // Make the Node->LRG mapping
aoqi@0 267 _lrg_map.extend(x->_idx,lrg);
aoqi@0 268 // Make the Union-Find mapping an identity function
aoqi@0 269 _lrg_map.uf_extend(lrg, lrg);
aoqi@0 270 }
aoqi@0 271
aoqi@0 272
aoqi@0 273 int PhaseChaitin::clone_projs(Block* b, uint idx, Node* orig, Node* copy, uint& max_lrg_id) {
aoqi@0 274 assert(b->find_node(copy) == (idx - 1), "incorrect insert index for copy kill projections");
aoqi@0 275 DEBUG_ONLY( Block* borig = _cfg.get_block_for_node(orig); )
aoqi@0 276 int found_projs = 0;
aoqi@0 277 uint cnt = orig->outcnt();
aoqi@0 278 for (uint i = 0; i < cnt; i++) {
aoqi@0 279 Node* proj = orig->raw_out(i);
aoqi@0 280 if (proj->is_MachProj()) {
aoqi@0 281 assert(proj->outcnt() == 0, "only kill projections are expected here");
aoqi@0 282 assert(_cfg.get_block_for_node(proj) == borig, "incorrect block for kill projections");
aoqi@0 283 found_projs++;
aoqi@0 284 // Copy kill projections after the cloned node
aoqi@0 285 Node* kills = proj->clone();
aoqi@0 286 kills->set_req(0, copy);
aoqi@0 287 b->insert_node(kills, idx++);
aoqi@0 288 _cfg.map_node_to_block(kills, b);
aoqi@0 289 new_lrg(kills, max_lrg_id++);
aoqi@0 290 }
aoqi@0 291 }
aoqi@0 292 return found_projs;
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 // Renumber the live ranges to compact them. Makes the IFG smaller.
aoqi@0 296 void PhaseChaitin::compact() {
aoqi@0 297 // Current the _uf_map contains a series of short chains which are headed
aoqi@0 298 // by a self-cycle. All the chains run from big numbers to little numbers.
aoqi@0 299 // The Find() call chases the chains & shortens them for the next Find call.
aoqi@0 300 // We are going to change this structure slightly. Numbers above a moving
aoqi@0 301 // wave 'i' are unchanged. Numbers below 'j' point directly to their
aoqi@0 302 // compacted live range with no further chaining. There are no chains or
aoqi@0 303 // cycles below 'i', so the Find call no longer works.
aoqi@0 304 uint j=1;
aoqi@0 305 uint i;
aoqi@0 306 for (i = 1; i < _lrg_map.max_lrg_id(); i++) {
aoqi@0 307 uint lr = _lrg_map.uf_live_range_id(i);
aoqi@0 308 // Ignore unallocated live ranges
aoqi@0 309 if (!lr) {
aoqi@0 310 continue;
aoqi@0 311 }
aoqi@0 312 assert(lr <= i, "");
aoqi@0 313 _lrg_map.uf_map(i, ( lr == i ) ? j++ : _lrg_map.uf_live_range_id(lr));
aoqi@0 314 }
aoqi@0 315 // Now change the Node->LR mapping to reflect the compacted names
aoqi@0 316 uint unique = _lrg_map.size();
aoqi@0 317 for (i = 0; i < unique; i++) {
aoqi@0 318 uint lrg_id = _lrg_map.live_range_id(i);
aoqi@0 319 _lrg_map.map(i, _lrg_map.uf_live_range_id(lrg_id));
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 // Reset the Union-Find mapping
aoqi@0 323 _lrg_map.reset_uf_map(j);
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 void PhaseChaitin::Register_Allocate() {
aoqi@0 327
aoqi@0 328 // Above the OLD FP (and in registers) are the incoming arguments. Stack
aoqi@0 329 // slots in this area are called "arg_slots". Above the NEW FP (and in
aoqi@0 330 // registers) is the outgoing argument area; above that is the spill/temp
aoqi@0 331 // area. These are all "frame_slots". Arg_slots start at the zero
aoqi@0 332 // stack_slots and count up to the known arg_size. Frame_slots start at
aoqi@0 333 // the stack_slot #arg_size and go up. After allocation I map stack
aoqi@0 334 // slots to actual offsets. Stack-slots in the arg_slot area are biased
aoqi@0 335 // by the frame_size; stack-slots in the frame_slot area are biased by 0.
aoqi@0 336
aoqi@0 337 _trip_cnt = 0;
aoqi@0 338 _alternate = 0;
aoqi@0 339 _matcher._allocation_started = true;
aoqi@0 340
aoqi@0 341 ResourceArea split_arena; // Arena for Split local resources
aoqi@0 342 ResourceArea live_arena; // Arena for liveness & IFG info
aoqi@0 343 ResourceMark rm(&live_arena);
aoqi@0 344
aoqi@0 345 // Need live-ness for the IFG; need the IFG for coalescing. If the
aoqi@0 346 // liveness is JUST for coalescing, then I can get some mileage by renaming
aoqi@0 347 // all copy-related live ranges low and then using the max copy-related
aoqi@0 348 // live range as a cut-off for LIVE and the IFG. In other words, I can
aoqi@0 349 // build a subset of LIVE and IFG just for copies.
aoqi@0 350 PhaseLive live(_cfg, _lrg_map.names(), &live_arena);
aoqi@0 351
aoqi@0 352 // Need IFG for coalescing and coloring
aoqi@0 353 PhaseIFG ifg(&live_arena);
aoqi@0 354 _ifg = &ifg;
aoqi@0 355
aoqi@0 356 // Come out of SSA world to the Named world. Assign (virtual) registers to
aoqi@0 357 // Nodes. Use the same register for all inputs and the output of PhiNodes
aoqi@0 358 // - effectively ending SSA form. This requires either coalescing live
aoqi@0 359 // ranges or inserting copies. For the moment, we insert "virtual copies"
aoqi@0 360 // - we pretend there is a copy prior to each Phi in predecessor blocks.
aoqi@0 361 // We will attempt to coalesce such "virtual copies" before we manifest
aoqi@0 362 // them for real.
aoqi@0 363 de_ssa();
aoqi@0 364
aoqi@0 365 #ifdef ASSERT
aoqi@0 366 // Veify the graph before RA.
aoqi@0 367 verify(&live_arena);
aoqi@0 368 #endif
aoqi@0 369
aoqi@0 370 {
aoqi@0 371 NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
aoqi@0 372 _live = NULL; // Mark live as being not available
aoqi@0 373 rm.reset_to_mark(); // Reclaim working storage
aoqi@0 374 IndexSet::reset_memory(C, &live_arena);
aoqi@0 375 ifg.init(_lrg_map.max_lrg_id()); // Empty IFG
aoqi@0 376 gather_lrg_masks( false ); // Collect LRG masks
aoqi@0 377 live.compute(_lrg_map.max_lrg_id()); // Compute liveness
aoqi@0 378 _live = &live; // Mark LIVE as being available
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 // Base pointers are currently "used" by instructions which define new
aoqi@0 382 // derived pointers. This makes base pointers live up to the where the
aoqi@0 383 // derived pointer is made, but not beyond. Really, they need to be live
aoqi@0 384 // across any GC point where the derived value is live. So this code looks
aoqi@0 385 // at all the GC points, and "stretches" the live range of any base pointer
aoqi@0 386 // to the GC point.
aoqi@0 387 if (stretch_base_pointer_live_ranges(&live_arena)) {
aoqi@0 388 NOT_PRODUCT(Compile::TracePhase t3("computeLive (sbplr)", &_t_computeLive, TimeCompiler);)
aoqi@0 389 // Since some live range stretched, I need to recompute live
aoqi@0 390 _live = NULL;
aoqi@0 391 rm.reset_to_mark(); // Reclaim working storage
aoqi@0 392 IndexSet::reset_memory(C, &live_arena);
aoqi@0 393 ifg.init(_lrg_map.max_lrg_id());
aoqi@0 394 gather_lrg_masks(false);
aoqi@0 395 live.compute(_lrg_map.max_lrg_id());
aoqi@0 396 _live = &live;
aoqi@0 397 }
aoqi@0 398 // Create the interference graph using virtual copies
aoqi@0 399 build_ifg_virtual(); // Include stack slots this time
aoqi@0 400
aoqi@0 401 // Aggressive (but pessimistic) copy coalescing.
aoqi@0 402 // This pass works on virtual copies. Any virtual copies which are not
aoqi@0 403 // coalesced get manifested as actual copies
aoqi@0 404 {
aoqi@0 405 // The IFG is/was triangular. I am 'squaring it up' so Union can run
aoqi@0 406 // faster. Union requires a 'for all' operation which is slow on the
aoqi@0 407 // triangular adjacency matrix (quick reminder: the IFG is 'sparse' -
aoqi@0 408 // meaning I can visit all the Nodes neighbors less than a Node in time
aoqi@0 409 // O(# of neighbors), but I have to visit all the Nodes greater than a
aoqi@0 410 // given Node and search them for an instance, i.e., time O(#MaxLRG)).
aoqi@0 411 _ifg->SquareUp();
aoqi@0 412
aoqi@0 413 PhaseAggressiveCoalesce coalesce(*this);
aoqi@0 414 coalesce.coalesce_driver();
aoqi@0 415 // Insert un-coalesced copies. Visit all Phis. Where inputs to a Phi do
aoqi@0 416 // not match the Phi itself, insert a copy.
aoqi@0 417 coalesce.insert_copies(_matcher);
aoqi@0 418 if (C->failing()) {
aoqi@0 419 return;
aoqi@0 420 }
aoqi@0 421 }
aoqi@0 422
aoqi@0 423 // After aggressive coalesce, attempt a first cut at coloring.
aoqi@0 424 // To color, we need the IFG and for that we need LIVE.
aoqi@0 425 {
aoqi@0 426 NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
aoqi@0 427 _live = NULL;
aoqi@0 428 rm.reset_to_mark(); // Reclaim working storage
aoqi@0 429 IndexSet::reset_memory(C, &live_arena);
aoqi@0 430 ifg.init(_lrg_map.max_lrg_id());
aoqi@0 431 gather_lrg_masks( true );
aoqi@0 432 live.compute(_lrg_map.max_lrg_id());
aoqi@0 433 _live = &live;
aoqi@0 434 }
aoqi@0 435
aoqi@0 436 // Build physical interference graph
aoqi@0 437 uint must_spill = 0;
aoqi@0 438 must_spill = build_ifg_physical(&live_arena);
aoqi@0 439 // If we have a guaranteed spill, might as well spill now
aoqi@0 440 if (must_spill) {
aoqi@0 441 if(!_lrg_map.max_lrg_id()) {
aoqi@0 442 return;
aoqi@0 443 }
aoqi@0 444 // Bail out if unique gets too large (ie - unique > MaxNodeLimit)
aoqi@0 445 C->check_node_count(10*must_spill, "out of nodes before split");
aoqi@0 446 if (C->failing()) {
aoqi@0 447 return;
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 uint new_max_lrg_id = Split(_lrg_map.max_lrg_id(), &split_arena); // Split spilling LRG everywhere
aoqi@0 451 _lrg_map.set_max_lrg_id(new_max_lrg_id);
aoqi@0 452 // Bail out if unique gets too large (ie - unique > MaxNodeLimit - 2*NodeLimitFudgeFactor)
aoqi@0 453 // or we failed to split
aoqi@0 454 C->check_node_count(2*NodeLimitFudgeFactor, "out of nodes after physical split");
aoqi@0 455 if (C->failing()) {
aoqi@0 456 return;
aoqi@0 457 }
aoqi@0 458
aoqi@0 459 NOT_PRODUCT(C->verify_graph_edges();)
aoqi@0 460
aoqi@0 461 compact(); // Compact LRGs; return new lower max lrg
aoqi@0 462
aoqi@0 463 {
aoqi@0 464 NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
aoqi@0 465 _live = NULL;
aoqi@0 466 rm.reset_to_mark(); // Reclaim working storage
aoqi@0 467 IndexSet::reset_memory(C, &live_arena);
aoqi@0 468 ifg.init(_lrg_map.max_lrg_id()); // Build a new interference graph
aoqi@0 469 gather_lrg_masks( true ); // Collect intersect mask
aoqi@0 470 live.compute(_lrg_map.max_lrg_id()); // Compute LIVE
aoqi@0 471 _live = &live;
aoqi@0 472 }
aoqi@0 473 build_ifg_physical(&live_arena);
aoqi@0 474 _ifg->SquareUp();
aoqi@0 475 _ifg->Compute_Effective_Degree();
aoqi@0 476 // Only do conservative coalescing if requested
aoqi@0 477 if (OptoCoalesce) {
aoqi@0 478 // Conservative (and pessimistic) copy coalescing of those spills
aoqi@0 479 PhaseConservativeCoalesce coalesce(*this);
aoqi@0 480 // If max live ranges greater than cutoff, don't color the stack.
aoqi@0 481 // This cutoff can be larger than below since it is only done once.
aoqi@0 482 coalesce.coalesce_driver();
aoqi@0 483 }
aoqi@0 484 _lrg_map.compress_uf_map_for_nodes();
aoqi@0 485
aoqi@0 486 #ifdef ASSERT
aoqi@0 487 verify(&live_arena, true);
aoqi@0 488 #endif
aoqi@0 489 } else {
aoqi@0 490 ifg.SquareUp();
aoqi@0 491 ifg.Compute_Effective_Degree();
aoqi@0 492 #ifdef ASSERT
aoqi@0 493 set_was_low();
aoqi@0 494 #endif
aoqi@0 495 }
aoqi@0 496
aoqi@0 497 // Prepare for Simplify & Select
aoqi@0 498 cache_lrg_info(); // Count degree of LRGs
aoqi@0 499
aoqi@0 500 // Simplify the InterFerence Graph by removing LRGs of low degree.
aoqi@0 501 // LRGs of low degree are trivially colorable.
aoqi@0 502 Simplify();
aoqi@0 503
aoqi@0 504 // Select colors by re-inserting LRGs back into the IFG in reverse order.
aoqi@0 505 // Return whether or not something spills.
aoqi@0 506 uint spills = Select( );
aoqi@0 507
aoqi@0 508 // If we spill, split and recycle the entire thing
aoqi@0 509 while( spills ) {
aoqi@0 510 if( _trip_cnt++ > 24 ) {
aoqi@0 511 DEBUG_ONLY( dump_for_spill_split_recycle(); )
aoqi@0 512 if( _trip_cnt > 27 ) {
aoqi@0 513 C->record_method_not_compilable("failed spill-split-recycle sanity check");
aoqi@0 514 return;
aoqi@0 515 }
aoqi@0 516 }
aoqi@0 517
aoqi@0 518 if (!_lrg_map.max_lrg_id()) {
aoqi@0 519 return;
aoqi@0 520 }
aoqi@0 521 uint new_max_lrg_id = Split(_lrg_map.max_lrg_id(), &split_arena); // Split spilling LRG everywhere
aoqi@0 522 _lrg_map.set_max_lrg_id(new_max_lrg_id);
aoqi@0 523 // Bail out if unique gets too large (ie - unique > MaxNodeLimit - 2*NodeLimitFudgeFactor)
aoqi@0 524 C->check_node_count(2 * NodeLimitFudgeFactor, "out of nodes after split");
aoqi@0 525 if (C->failing()) {
aoqi@0 526 return;
aoqi@0 527 }
aoqi@0 528
aoqi@0 529 compact(); // Compact LRGs; return new lower max lrg
aoqi@0 530
aoqi@0 531 // Nuke the live-ness and interference graph and LiveRanGe info
aoqi@0 532 {
aoqi@0 533 NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
aoqi@0 534 _live = NULL;
aoqi@0 535 rm.reset_to_mark(); // Reclaim working storage
aoqi@0 536 IndexSet::reset_memory(C, &live_arena);
aoqi@0 537 ifg.init(_lrg_map.max_lrg_id());
aoqi@0 538
aoqi@0 539 // Create LiveRanGe array.
aoqi@0 540 // Intersect register masks for all USEs and DEFs
aoqi@0 541 gather_lrg_masks(true);
aoqi@0 542 live.compute(_lrg_map.max_lrg_id());
aoqi@0 543 _live = &live;
aoqi@0 544 }
aoqi@0 545 must_spill = build_ifg_physical(&live_arena);
aoqi@0 546 _ifg->SquareUp();
aoqi@0 547 _ifg->Compute_Effective_Degree();
aoqi@0 548
aoqi@0 549 // Only do conservative coalescing if requested
aoqi@0 550 if (OptoCoalesce) {
aoqi@0 551 // Conservative (and pessimistic) copy coalescing
aoqi@0 552 PhaseConservativeCoalesce coalesce(*this);
aoqi@0 553 // Check for few live ranges determines how aggressive coalesce is.
aoqi@0 554 coalesce.coalesce_driver();
aoqi@0 555 }
aoqi@0 556 _lrg_map.compress_uf_map_for_nodes();
aoqi@0 557 #ifdef ASSERT
aoqi@0 558 verify(&live_arena, true);
aoqi@0 559 #endif
aoqi@0 560 cache_lrg_info(); // Count degree of LRGs
aoqi@0 561
aoqi@0 562 // Simplify the InterFerence Graph by removing LRGs of low degree.
aoqi@0 563 // LRGs of low degree are trivially colorable.
aoqi@0 564 Simplify();
aoqi@0 565
aoqi@0 566 // Select colors by re-inserting LRGs back into the IFG in reverse order.
aoqi@0 567 // Return whether or not something spills.
aoqi@0 568 spills = Select();
aoqi@0 569 }
aoqi@0 570
aoqi@0 571 // Count number of Simplify-Select trips per coloring success.
aoqi@0 572 _allocator_attempts += _trip_cnt + 1;
aoqi@0 573 _allocator_successes += 1;
aoqi@0 574
aoqi@0 575 // Peephole remove copies
aoqi@0 576 post_allocate_copy_removal();
aoqi@0 577
iveresov@7564 578 // Merge multidefs if multiple defs representing the same value are used in a single block.
iveresov@7564 579 merge_multidefs();
iveresov@7564 580
aoqi@0 581 #ifdef ASSERT
aoqi@0 582 // Veify the graph after RA.
aoqi@0 583 verify(&live_arena);
aoqi@0 584 #endif
aoqi@0 585
aoqi@0 586 // max_reg is past the largest *register* used.
aoqi@0 587 // Convert that to a frame_slot number.
aoqi@0 588 if (_max_reg <= _matcher._new_SP) {
aoqi@0 589 _framesize = C->out_preserve_stack_slots();
aoqi@0 590 }
aoqi@0 591 else {
aoqi@0 592 _framesize = _max_reg -_matcher._new_SP;
aoqi@0 593 }
aoqi@0 594 assert((int)(_matcher._new_SP+_framesize) >= (int)_matcher._out_arg_limit, "framesize must be large enough");
aoqi@0 595
aoqi@0 596 // This frame must preserve the required fp alignment
aoqi@0 597 _framesize = round_to(_framesize, Matcher::stack_alignment_in_slots());
aoqi@0 598 assert( _framesize >= 0 && _framesize <= 1000000, "sanity check" );
aoqi@0 599 #ifndef PRODUCT
aoqi@0 600 _total_framesize += _framesize;
aoqi@0 601 if ((int)_framesize > _max_framesize) {
aoqi@0 602 _max_framesize = _framesize;
aoqi@0 603 }
aoqi@0 604 #endif
aoqi@0 605
aoqi@0 606 // Convert CISC spills
aoqi@0 607 fixup_spills();
aoqi@0 608
aoqi@0 609 // Log regalloc results
aoqi@0 610 CompileLog* log = Compile::current()->log();
aoqi@0 611 if (log != NULL) {
aoqi@0 612 log->elem("regalloc attempts='%d' success='%d'", _trip_cnt, !C->failing());
aoqi@0 613 }
aoqi@0 614
aoqi@0 615 if (C->failing()) {
aoqi@0 616 return;
aoqi@0 617 }
aoqi@0 618
aoqi@0 619 NOT_PRODUCT(C->verify_graph_edges();)
aoqi@0 620
aoqi@0 621 // Move important info out of the live_arena to longer lasting storage.
aoqi@0 622 alloc_node_regs(_lrg_map.size());
aoqi@0 623 for (uint i=0; i < _lrg_map.size(); i++) {
aoqi@0 624 if (_lrg_map.live_range_id(i)) { // Live range associated with Node?
aoqi@0 625 LRG &lrg = lrgs(_lrg_map.live_range_id(i));
aoqi@0 626 if (!lrg.alive()) {
aoqi@0 627 set_bad(i);
aoqi@0 628 } else if (lrg.num_regs() == 1) {
aoqi@0 629 set1(i, lrg.reg());
aoqi@0 630 } else { // Must be a register-set
aoqi@0 631 if (!lrg._fat_proj) { // Must be aligned adjacent register set
aoqi@0 632 // Live ranges record the highest register in their mask.
aoqi@0 633 // We want the low register for the AD file writer's convenience.
aoqi@0 634 OptoReg::Name hi = lrg.reg(); // Get hi register
aoqi@0 635 OptoReg::Name lo = OptoReg::add(hi, (1-lrg.num_regs())); // Find lo
aoqi@0 636 // We have to use pair [lo,lo+1] even for wide vectors because
aoqi@0 637 // the rest of code generation works only with pairs. It is safe
aoqi@0 638 // since for registers encoding only 'lo' is used.
aoqi@0 639 // Second reg from pair is used in ScheduleAndBundle on SPARC where
aoqi@0 640 // vector max size is 8 which corresponds to registers pair.
aoqi@0 641 // It is also used in BuildOopMaps but oop operations are not
aoqi@0 642 // vectorized.
aoqi@0 643 set2(i, lo);
aoqi@0 644 } else { // Misaligned; extract 2 bits
aoqi@0 645 OptoReg::Name hi = lrg.reg(); // Get hi register
aoqi@0 646 lrg.Remove(hi); // Yank from mask
aoqi@0 647 int lo = lrg.mask().find_first_elem(); // Find lo
aoqi@0 648 set_pair(i, hi, lo);
aoqi@0 649 }
aoqi@0 650 }
aoqi@0 651 if( lrg._is_oop ) _node_oops.set(i);
aoqi@0 652 } else {
aoqi@0 653 set_bad(i);
aoqi@0 654 }
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 // Done!
aoqi@0 658 _live = NULL;
aoqi@0 659 _ifg = NULL;
aoqi@0 660 C->set_indexSet_arena(NULL); // ResourceArea is at end of scope
aoqi@0 661 }
aoqi@0 662
aoqi@0 663 void PhaseChaitin::de_ssa() {
aoqi@0 664 // Set initial Names for all Nodes. Most Nodes get the virtual register
aoqi@0 665 // number. A few get the ZERO live range number. These do not
aoqi@0 666 // get allocated, but instead rely on correct scheduling to ensure that
aoqi@0 667 // only one instance is simultaneously live at a time.
aoqi@0 668 uint lr_counter = 1;
aoqi@0 669 for( uint i = 0; i < _cfg.number_of_blocks(); i++ ) {
aoqi@0 670 Block* block = _cfg.get_block(i);
aoqi@0 671 uint cnt = block->number_of_nodes();
aoqi@0 672
aoqi@0 673 // Handle all the normal Nodes in the block
aoqi@0 674 for( uint j = 0; j < cnt; j++ ) {
aoqi@0 675 Node *n = block->get_node(j);
aoqi@0 676 // Pre-color to the zero live range, or pick virtual register
aoqi@0 677 const RegMask &rm = n->out_RegMask();
aoqi@0 678 _lrg_map.map(n->_idx, rm.is_NotEmpty() ? lr_counter++ : 0);
aoqi@0 679 }
aoqi@0 680 }
aoqi@0 681
aoqi@0 682 // Reset the Union-Find mapping to be identity
aoqi@0 683 _lrg_map.reset_uf_map(lr_counter);
aoqi@0 684 }
aoqi@0 685
aoqi@0 686
aoqi@0 687 // Gather LiveRanGe information, including register masks. Modification of
aoqi@0 688 // cisc spillable in_RegMasks should not be done before AggressiveCoalesce.
aoqi@0 689 void PhaseChaitin::gather_lrg_masks( bool after_aggressive ) {
aoqi@0 690
aoqi@0 691 // Nail down the frame pointer live range
aoqi@0 692 uint fp_lrg = _lrg_map.live_range_id(_cfg.get_root_node()->in(1)->in(TypeFunc::FramePtr));
aoqi@0 693 lrgs(fp_lrg)._cost += 1e12; // Cost is infinite
aoqi@0 694
aoqi@0 695 // For all blocks
aoqi@0 696 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 697 Block* block = _cfg.get_block(i);
aoqi@0 698
aoqi@0 699 // For all instructions
aoqi@0 700 for (uint j = 1; j < block->number_of_nodes(); j++) {
aoqi@0 701 Node* n = block->get_node(j);
aoqi@0 702 uint input_edge_start =1; // Skip control most nodes
aoqi@0 703 if (n->is_Mach()) {
aoqi@0 704 input_edge_start = n->as_Mach()->oper_input_base();
aoqi@0 705 }
aoqi@0 706 uint idx = n->is_Copy();
aoqi@0 707
aoqi@0 708 // Get virtual register number, same as LiveRanGe index
aoqi@0 709 uint vreg = _lrg_map.live_range_id(n);
aoqi@0 710 LRG& lrg = lrgs(vreg);
aoqi@0 711 if (vreg) { // No vreg means un-allocable (e.g. memory)
aoqi@0 712
aoqi@0 713 // Collect has-copy bit
aoqi@0 714 if (idx) {
aoqi@0 715 lrg._has_copy = 1;
aoqi@0 716 uint clidx = _lrg_map.live_range_id(n->in(idx));
aoqi@0 717 LRG& copy_src = lrgs(clidx);
aoqi@0 718 copy_src._has_copy = 1;
aoqi@0 719 }
aoqi@0 720
aoqi@0 721 // Check for float-vs-int live range (used in register-pressure
aoqi@0 722 // calculations)
aoqi@0 723 const Type *n_type = n->bottom_type();
aoqi@0 724 if (n_type->is_floatingpoint()) {
aoqi@0 725 lrg._is_float = 1;
aoqi@0 726 }
aoqi@0 727
aoqi@0 728 // Check for twice prior spilling. Once prior spilling might have
aoqi@0 729 // spilled 'soft', 2nd prior spill should have spilled 'hard' and
aoqi@0 730 // further spilling is unlikely to make progress.
aoqi@0 731 if (_spilled_once.test(n->_idx)) {
aoqi@0 732 lrg._was_spilled1 = 1;
aoqi@0 733 if (_spilled_twice.test(n->_idx)) {
aoqi@0 734 lrg._was_spilled2 = 1;
aoqi@0 735 }
aoqi@0 736 }
aoqi@0 737
aoqi@0 738 #ifndef PRODUCT
aoqi@0 739 if (trace_spilling() && lrg._def != NULL) {
aoqi@0 740 // collect defs for MultiDef printing
aoqi@0 741 if (lrg._defs == NULL) {
aoqi@0 742 lrg._defs = new (_ifg->_arena) GrowableArray<Node*>(_ifg->_arena, 2, 0, NULL);
aoqi@0 743 lrg._defs->append(lrg._def);
aoqi@0 744 }
aoqi@0 745 lrg._defs->append(n);
aoqi@0 746 }
aoqi@0 747 #endif
aoqi@0 748
aoqi@0 749 // Check for a single def LRG; these can spill nicely
aoqi@0 750 // via rematerialization. Flag as NULL for no def found
aoqi@0 751 // yet, or 'n' for single def or -1 for many defs.
aoqi@0 752 lrg._def = lrg._def ? NodeSentinel : n;
aoqi@0 753
aoqi@0 754 // Limit result register mask to acceptable registers
aoqi@0 755 const RegMask &rm = n->out_RegMask();
aoqi@0 756 lrg.AND( rm );
aoqi@0 757
aoqi@0 758 int ireg = n->ideal_reg();
aoqi@0 759 assert( !n->bottom_type()->isa_oop_ptr() || ireg == Op_RegP,
aoqi@0 760 "oops must be in Op_RegP's" );
aoqi@0 761
aoqi@0 762 // Check for vector live range (only if vector register is used).
aoqi@0 763 // On SPARC vector uses RegD which could be misaligned so it is not
aoqi@0 764 // processes as vector in RA.
aoqi@0 765 if (RegMask::is_vector(ireg))
aoqi@0 766 lrg._is_vector = 1;
aoqi@0 767 assert(n_type->isa_vect() == NULL || lrg._is_vector || ireg == Op_RegD || ireg == Op_RegL,
aoqi@0 768 "vector must be in vector registers");
aoqi@0 769
aoqi@0 770 // Check for bound register masks
aoqi@0 771 const RegMask &lrgmask = lrg.mask();
aoqi@0 772 if (lrgmask.is_bound(ireg)) {
aoqi@0 773 lrg._is_bound = 1;
aoqi@0 774 }
aoqi@0 775
aoqi@0 776 // Check for maximum frequency value
aoqi@0 777 if (lrg._maxfreq < block->_freq) {
aoqi@0 778 lrg._maxfreq = block->_freq;
aoqi@0 779 }
aoqi@0 780
aoqi@0 781 // Check for oop-iness, or long/double
aoqi@0 782 // Check for multi-kill projection
aoqi@0 783 switch (ireg) {
aoqi@0 784 case MachProjNode::fat_proj:
aoqi@0 785 // Fat projections have size equal to number of registers killed
aoqi@0 786 lrg.set_num_regs(rm.Size());
aoqi@0 787 lrg.set_reg_pressure(lrg.num_regs());
aoqi@0 788 lrg._fat_proj = 1;
aoqi@0 789 lrg._is_bound = 1;
aoqi@0 790 break;
aoqi@0 791 case Op_RegP:
aoqi@0 792 #ifdef _LP64
aoqi@0 793 lrg.set_num_regs(2); // Size is 2 stack words
aoqi@0 794 #else
aoqi@0 795 lrg.set_num_regs(1); // Size is 1 stack word
aoqi@0 796 #endif
aoqi@0 797 // Register pressure is tracked relative to the maximum values
aoqi@0 798 // suggested for that platform, INTPRESSURE and FLOATPRESSURE,
aoqi@0 799 // and relative to other types which compete for the same regs.
aoqi@0 800 //
aoqi@0 801 // The following table contains suggested values based on the
aoqi@0 802 // architectures as defined in each .ad file.
aoqi@0 803 // INTPRESSURE and FLOATPRESSURE may be tuned differently for
aoqi@0 804 // compile-speed or performance.
aoqi@0 805 // Note1:
aoqi@0 806 // SPARC and SPARCV9 reg_pressures are at 2 instead of 1
aoqi@0 807 // since .ad registers are defined as high and low halves.
aoqi@0 808 // These reg_pressure values remain compatible with the code
aoqi@0 809 // in is_high_pressure() which relates get_invalid_mask_size(),
aoqi@0 810 // Block::_reg_pressure and INTPRESSURE, FLOATPRESSURE.
aoqi@0 811 // Note2:
aoqi@0 812 // SPARC -d32 has 24 registers available for integral values,
aoqi@0 813 // but only 10 of these are safe for 64-bit longs.
aoqi@0 814 // Using set_reg_pressure(2) for both int and long means
aoqi@0 815 // the allocator will believe it can fit 26 longs into
aoqi@0 816 // registers. Using 2 for longs and 1 for ints means the
aoqi@0 817 // allocator will attempt to put 52 integers into registers.
aoqi@0 818 // The settings below limit this problem to methods with
aoqi@0 819 // many long values which are being run on 32-bit SPARC.
aoqi@0 820 //
aoqi@0 821 // ------------------- reg_pressure --------------------
aoqi@0 822 // Each entry is reg_pressure_per_value,number_of_regs
aoqi@0 823 // RegL RegI RegFlags RegF RegD INTPRESSURE FLOATPRESSURE
aoqi@0 824 // IA32 2 1 1 1 1 6 6
aoqi@0 825 // IA64 1 1 1 1 1 50 41
aoqi@0 826 // SPARC 2 2 2 2 2 48 (24) 52 (26)
aoqi@0 827 // SPARCV9 2 2 2 2 2 48 (24) 52 (26)
aoqi@0 828 // AMD64 1 1 1 1 1 14 15
aoqi@0 829 // -----------------------------------------------------
aoqi@0 830 #if defined(SPARC)
aoqi@0 831 lrg.set_reg_pressure(2); // use for v9 as well
aoqi@0 832 #else
aoqi@0 833 lrg.set_reg_pressure(1); // normally one value per register
aoqi@0 834 #endif
aoqi@0 835 if( n_type->isa_oop_ptr() ) {
aoqi@0 836 lrg._is_oop = 1;
aoqi@0 837 }
aoqi@0 838 break;
aoqi@0 839 case Op_RegL: // Check for long or double
aoqi@0 840 case Op_RegD:
aoqi@0 841 lrg.set_num_regs(2);
aoqi@0 842 // Define platform specific register pressure
dlong@7598 843 #if defined(SPARC) || defined(ARM32)
aoqi@0 844 lrg.set_reg_pressure(2);
aoqi@0 845 #elif defined(IA32)
aoqi@0 846 if( ireg == Op_RegL ) {
aoqi@0 847 lrg.set_reg_pressure(2);
aoqi@0 848 } else {
aoqi@0 849 lrg.set_reg_pressure(1);
aoqi@0 850 }
aoqi@0 851 #else
aoqi@0 852 lrg.set_reg_pressure(1); // normally one value per register
aoqi@0 853 #endif
aoqi@0 854 // If this def of a double forces a mis-aligned double,
aoqi@0 855 // flag as '_fat_proj' - really flag as allowing misalignment
aoqi@0 856 // AND changes how we count interferences. A mis-aligned
aoqi@0 857 // double can interfere with TWO aligned pairs, or effectively
aoqi@0 858 // FOUR registers!
aoqi@0 859 if (rm.is_misaligned_pair()) {
aoqi@0 860 lrg._fat_proj = 1;
aoqi@0 861 lrg._is_bound = 1;
aoqi@0 862 }
aoqi@0 863 break;
aoqi@0 864 case Op_RegF:
aoqi@0 865 case Op_RegI:
aoqi@0 866 case Op_RegN:
aoqi@0 867 case Op_RegFlags:
aoqi@0 868 case 0: // not an ideal register
aoqi@0 869 lrg.set_num_regs(1);
aoqi@0 870 #ifdef SPARC
aoqi@0 871 lrg.set_reg_pressure(2);
aoqi@0 872 #else
aoqi@0 873 lrg.set_reg_pressure(1);
aoqi@0 874 #endif
aoqi@0 875 break;
aoqi@0 876 case Op_VecS:
aoqi@0 877 assert(Matcher::vector_size_supported(T_BYTE,4), "sanity");
aoqi@0 878 assert(RegMask::num_registers(Op_VecS) == RegMask::SlotsPerVecS, "sanity");
aoqi@0 879 lrg.set_num_regs(RegMask::SlotsPerVecS);
aoqi@0 880 lrg.set_reg_pressure(1);
aoqi@0 881 break;
aoqi@0 882 case Op_VecD:
aoqi@0 883 assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecD), "sanity");
aoqi@0 884 assert(RegMask::num_registers(Op_VecD) == RegMask::SlotsPerVecD, "sanity");
aoqi@0 885 assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecD), "vector should be aligned");
aoqi@0 886 lrg.set_num_regs(RegMask::SlotsPerVecD);
aoqi@0 887 lrg.set_reg_pressure(1);
aoqi@0 888 break;
aoqi@0 889 case Op_VecX:
aoqi@0 890 assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecX), "sanity");
aoqi@0 891 assert(RegMask::num_registers(Op_VecX) == RegMask::SlotsPerVecX, "sanity");
aoqi@0 892 assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecX), "vector should be aligned");
aoqi@0 893 lrg.set_num_regs(RegMask::SlotsPerVecX);
aoqi@0 894 lrg.set_reg_pressure(1);
aoqi@0 895 break;
aoqi@0 896 case Op_VecY:
aoqi@0 897 assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecY), "sanity");
aoqi@0 898 assert(RegMask::num_registers(Op_VecY) == RegMask::SlotsPerVecY, "sanity");
aoqi@0 899 assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecY), "vector should be aligned");
aoqi@0 900 lrg.set_num_regs(RegMask::SlotsPerVecY);
aoqi@0 901 lrg.set_reg_pressure(1);
aoqi@0 902 break;
aoqi@0 903 default:
aoqi@0 904 ShouldNotReachHere();
aoqi@0 905 }
aoqi@0 906 }
aoqi@0 907
aoqi@0 908 // Now do the same for inputs
aoqi@0 909 uint cnt = n->req();
aoqi@0 910 // Setup for CISC SPILLING
aoqi@0 911 uint inp = (uint)AdlcVMDeps::Not_cisc_spillable;
aoqi@0 912 if( UseCISCSpill && after_aggressive ) {
aoqi@0 913 inp = n->cisc_operand();
aoqi@0 914 if( inp != (uint)AdlcVMDeps::Not_cisc_spillable )
aoqi@0 915 // Convert operand number to edge index number
aoqi@0 916 inp = n->as_Mach()->operand_index(inp);
aoqi@0 917 }
aoqi@0 918 // Prepare register mask for each input
aoqi@0 919 for( uint k = input_edge_start; k < cnt; k++ ) {
aoqi@0 920 uint vreg = _lrg_map.live_range_id(n->in(k));
aoqi@0 921 if (!vreg) {
aoqi@0 922 continue;
aoqi@0 923 }
aoqi@0 924
aoqi@0 925 // If this instruction is CISC Spillable, add the flags
aoqi@0 926 // bit to its appropriate input
aoqi@0 927 if( UseCISCSpill && after_aggressive && inp == k ) {
aoqi@0 928 #ifndef PRODUCT
aoqi@0 929 if( TraceCISCSpill ) {
aoqi@0 930 tty->print(" use_cisc_RegMask: ");
aoqi@0 931 n->dump();
aoqi@0 932 }
aoqi@0 933 #endif
aoqi@0 934 n->as_Mach()->use_cisc_RegMask();
aoqi@0 935 }
aoqi@0 936
aoqi@0 937 LRG &lrg = lrgs(vreg);
aoqi@0 938 // // Testing for floating point code shape
aoqi@0 939 // Node *test = n->in(k);
aoqi@0 940 // if( test->is_Mach() ) {
aoqi@0 941 // MachNode *m = test->as_Mach();
aoqi@0 942 // int op = m->ideal_Opcode();
aoqi@0 943 // if (n->is_Call() && (op == Op_AddF || op == Op_MulF) ) {
aoqi@0 944 // int zzz = 1;
aoqi@0 945 // }
aoqi@0 946 // }
aoqi@0 947
aoqi@0 948 // Limit result register mask to acceptable registers.
aoqi@0 949 // Do not limit registers from uncommon uses before
aoqi@0 950 // AggressiveCoalesce. This effectively pre-virtual-splits
aoqi@0 951 // around uncommon uses of common defs.
aoqi@0 952 const RegMask &rm = n->in_RegMask(k);
aoqi@0 953 if (!after_aggressive && _cfg.get_block_for_node(n->in(k))->_freq > 1000 * block->_freq) {
aoqi@0 954 // Since we are BEFORE aggressive coalesce, leave the register
aoqi@0 955 // mask untrimmed by the call. This encourages more coalescing.
aoqi@0 956 // Later, AFTER aggressive, this live range will have to spill
aoqi@0 957 // but the spiller handles slow-path calls very nicely.
aoqi@0 958 } else {
aoqi@0 959 lrg.AND( rm );
aoqi@0 960 }
aoqi@0 961
aoqi@0 962 // Check for bound register masks
aoqi@0 963 const RegMask &lrgmask = lrg.mask();
aoqi@0 964 int kreg = n->in(k)->ideal_reg();
aoqi@0 965 bool is_vect = RegMask::is_vector(kreg);
aoqi@0 966 assert(n->in(k)->bottom_type()->isa_vect() == NULL ||
aoqi@0 967 is_vect || kreg == Op_RegD || kreg == Op_RegL,
aoqi@0 968 "vector must be in vector registers");
aoqi@0 969 if (lrgmask.is_bound(kreg))
aoqi@0 970 lrg._is_bound = 1;
aoqi@0 971
aoqi@0 972 // If this use of a double forces a mis-aligned double,
aoqi@0 973 // flag as '_fat_proj' - really flag as allowing misalignment
aoqi@0 974 // AND changes how we count interferences. A mis-aligned
aoqi@0 975 // double can interfere with TWO aligned pairs, or effectively
aoqi@0 976 // FOUR registers!
aoqi@0 977 #ifdef ASSERT
aoqi@0 978 if (is_vect) {
aoqi@0 979 assert(lrgmask.is_aligned_sets(lrg.num_regs()), "vector should be aligned");
aoqi@0 980 assert(!lrg._fat_proj, "sanity");
aoqi@0 981 assert(RegMask::num_registers(kreg) == lrg.num_regs(), "sanity");
aoqi@0 982 }
aoqi@0 983 #endif
aoqi@0 984 if (!is_vect && lrg.num_regs() == 2 && !lrg._fat_proj && rm.is_misaligned_pair()) {
aoqi@0 985 lrg._fat_proj = 1;
aoqi@0 986 lrg._is_bound = 1;
aoqi@0 987 }
aoqi@0 988 // if the LRG is an unaligned pair, we will have to spill
aoqi@0 989 // so clear the LRG's register mask if it is not already spilled
aoqi@0 990 if (!is_vect && !n->is_SpillCopy() &&
aoqi@0 991 (lrg._def == NULL || lrg.is_multidef() || !lrg._def->is_SpillCopy()) &&
aoqi@0 992 lrgmask.is_misaligned_pair()) {
aoqi@0 993 lrg.Clear();
aoqi@0 994 }
aoqi@0 995
aoqi@0 996 // Check for maximum frequency value
aoqi@0 997 if (lrg._maxfreq < block->_freq) {
aoqi@0 998 lrg._maxfreq = block->_freq;
aoqi@0 999 }
aoqi@0 1000
aoqi@0 1001 } // End for all allocated inputs
aoqi@0 1002 } // end for all instructions
aoqi@0 1003 } // end for all blocks
aoqi@0 1004
aoqi@0 1005 // Final per-liverange setup
aoqi@0 1006 for (uint i2 = 0; i2 < _lrg_map.max_lrg_id(); i2++) {
aoqi@0 1007 LRG &lrg = lrgs(i2);
aoqi@0 1008 assert(!lrg._is_vector || !lrg._fat_proj, "sanity");
aoqi@0 1009 if (lrg.num_regs() > 1 && !lrg._fat_proj) {
aoqi@0 1010 lrg.clear_to_sets();
aoqi@0 1011 }
aoqi@0 1012 lrg.compute_set_mask_size();
aoqi@0 1013 if (lrg.not_free()) { // Handle case where we lose from the start
aoqi@0 1014 lrg.set_reg(OptoReg::Name(LRG::SPILL_REG));
aoqi@0 1015 lrg._direct_conflict = 1;
aoqi@0 1016 }
aoqi@0 1017 lrg.set_degree(0); // no neighbors in IFG yet
aoqi@0 1018 }
aoqi@0 1019 }
aoqi@0 1020
aoqi@0 1021 // Set the was-lo-degree bit. Conservative coalescing should not change the
aoqi@0 1022 // colorability of the graph. If any live range was of low-degree before
aoqi@0 1023 // coalescing, it should Simplify. This call sets the was-lo-degree bit.
aoqi@0 1024 // The bit is checked in Simplify.
aoqi@0 1025 void PhaseChaitin::set_was_low() {
aoqi@0 1026 #ifdef ASSERT
aoqi@0 1027 for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
aoqi@0 1028 int size = lrgs(i).num_regs();
aoqi@0 1029 uint old_was_lo = lrgs(i)._was_lo;
aoqi@0 1030 lrgs(i)._was_lo = 0;
aoqi@0 1031 if( lrgs(i).lo_degree() ) {
aoqi@0 1032 lrgs(i)._was_lo = 1; // Trivially of low degree
aoqi@0 1033 } else { // Else check the Brigg's assertion
aoqi@0 1034 // Brigg's observation is that the lo-degree neighbors of a
aoqi@0 1035 // hi-degree live range will not interfere with the color choices
aoqi@0 1036 // of said hi-degree live range. The Simplify reverse-stack-coloring
aoqi@0 1037 // order takes care of the details. Hence you do not have to count
aoqi@0 1038 // low-degree neighbors when determining if this guy colors.
aoqi@0 1039 int briggs_degree = 0;
aoqi@0 1040 IndexSet *s = _ifg->neighbors(i);
aoqi@0 1041 IndexSetIterator elements(s);
aoqi@0 1042 uint lidx;
aoqi@0 1043 while((lidx = elements.next()) != 0) {
aoqi@0 1044 if( !lrgs(lidx).lo_degree() )
aoqi@0 1045 briggs_degree += MAX2(size,lrgs(lidx).num_regs());
aoqi@0 1046 }
aoqi@0 1047 if( briggs_degree < lrgs(i).degrees_of_freedom() )
aoqi@0 1048 lrgs(i)._was_lo = 1; // Low degree via the briggs assertion
aoqi@0 1049 }
aoqi@0 1050 assert(old_was_lo <= lrgs(i)._was_lo, "_was_lo may not decrease");
aoqi@0 1051 }
aoqi@0 1052 #endif
aoqi@0 1053 }
aoqi@0 1054
aoqi@0 1055 #define REGISTER_CONSTRAINED 16
aoqi@0 1056
aoqi@0 1057 // Compute cost/area ratio, in case we spill. Build the lo-degree list.
aoqi@0 1058 void PhaseChaitin::cache_lrg_info( ) {
aoqi@0 1059
aoqi@0 1060 for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
aoqi@0 1061 LRG &lrg = lrgs(i);
aoqi@0 1062
aoqi@0 1063 // Check for being of low degree: means we can be trivially colored.
aoqi@0 1064 // Low degree, dead or must-spill guys just get to simplify right away
aoqi@0 1065 if( lrg.lo_degree() ||
aoqi@0 1066 !lrg.alive() ||
aoqi@0 1067 lrg._must_spill ) {
aoqi@0 1068 // Split low degree list into those guys that must get a
aoqi@0 1069 // register and those that can go to register or stack.
aoqi@0 1070 // The idea is LRGs that can go register or stack color first when
aoqi@0 1071 // they have a good chance of getting a register. The register-only
aoqi@0 1072 // lo-degree live ranges always get a register.
aoqi@0 1073 OptoReg::Name hi_reg = lrg.mask().find_last_elem();
aoqi@0 1074 if( OptoReg::is_stack(hi_reg)) { // Can go to stack?
aoqi@0 1075 lrg._next = _lo_stk_degree;
aoqi@0 1076 _lo_stk_degree = i;
aoqi@0 1077 } else {
aoqi@0 1078 lrg._next = _lo_degree;
aoqi@0 1079 _lo_degree = i;
aoqi@0 1080 }
aoqi@0 1081 } else { // Else high degree
aoqi@0 1082 lrgs(_hi_degree)._prev = i;
aoqi@0 1083 lrg._next = _hi_degree;
aoqi@0 1084 lrg._prev = 0;
aoqi@0 1085 _hi_degree = i;
aoqi@0 1086 }
aoqi@0 1087 }
aoqi@0 1088 }
aoqi@0 1089
aoqi@0 1090 // Simplify the IFG by removing LRGs of low degree that have NO copies
aoqi@0 1091 void PhaseChaitin::Pre_Simplify( ) {
aoqi@0 1092
aoqi@0 1093 // Warm up the lo-degree no-copy list
aoqi@0 1094 int lo_no_copy = 0;
aoqi@0 1095 for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
aoqi@0 1096 if ((lrgs(i).lo_degree() && !lrgs(i)._has_copy) ||
aoqi@0 1097 !lrgs(i).alive() ||
aoqi@0 1098 lrgs(i)._must_spill) {
aoqi@0 1099 lrgs(i)._next = lo_no_copy;
aoqi@0 1100 lo_no_copy = i;
aoqi@0 1101 }
aoqi@0 1102 }
aoqi@0 1103
aoqi@0 1104 while( lo_no_copy ) {
aoqi@0 1105 uint lo = lo_no_copy;
aoqi@0 1106 lo_no_copy = lrgs(lo)._next;
aoqi@0 1107 int size = lrgs(lo).num_regs();
aoqi@0 1108
aoqi@0 1109 // Put the simplified guy on the simplified list.
aoqi@0 1110 lrgs(lo)._next = _simplified;
aoqi@0 1111 _simplified = lo;
aoqi@0 1112
aoqi@0 1113 // Yank this guy from the IFG.
aoqi@0 1114 IndexSet *adj = _ifg->remove_node( lo );
aoqi@0 1115
aoqi@0 1116 // If any neighbors' degrees fall below their number of
aoqi@0 1117 // allowed registers, then put that neighbor on the low degree
aoqi@0 1118 // list. Note that 'degree' can only fall and 'numregs' is
aoqi@0 1119 // unchanged by this action. Thus the two are equal at most once,
aoqi@0 1120 // so LRGs hit the lo-degree worklists at most once.
aoqi@0 1121 IndexSetIterator elements(adj);
aoqi@0 1122 uint neighbor;
aoqi@0 1123 while ((neighbor = elements.next()) != 0) {
aoqi@0 1124 LRG *n = &lrgs(neighbor);
aoqi@0 1125 assert( _ifg->effective_degree(neighbor) == n->degree(), "" );
aoqi@0 1126
aoqi@0 1127 // Check for just becoming of-low-degree
aoqi@0 1128 if( n->just_lo_degree() && !n->_has_copy ) {
aoqi@0 1129 assert(!(*_ifg->_yanked)[neighbor],"Cannot move to lo degree twice");
aoqi@0 1130 // Put on lo-degree list
aoqi@0 1131 n->_next = lo_no_copy;
aoqi@0 1132 lo_no_copy = neighbor;
aoqi@0 1133 }
aoqi@0 1134 }
aoqi@0 1135 } // End of while lo-degree no_copy worklist not empty
aoqi@0 1136
aoqi@0 1137 // No more lo-degree no-copy live ranges to simplify
aoqi@0 1138 }
aoqi@0 1139
aoqi@0 1140 // Simplify the IFG by removing LRGs of low degree.
aoqi@0 1141 void PhaseChaitin::Simplify( ) {
aoqi@0 1142
aoqi@0 1143 while( 1 ) { // Repeat till simplified it all
aoqi@0 1144 // May want to explore simplifying lo_degree before _lo_stk_degree.
aoqi@0 1145 // This might result in more spills coloring into registers during
aoqi@0 1146 // Select().
aoqi@0 1147 while( _lo_degree || _lo_stk_degree ) {
aoqi@0 1148 // If possible, pull from lo_stk first
aoqi@0 1149 uint lo;
aoqi@0 1150 if( _lo_degree ) {
aoqi@0 1151 lo = _lo_degree;
aoqi@0 1152 _lo_degree = lrgs(lo)._next;
aoqi@0 1153 } else {
aoqi@0 1154 lo = _lo_stk_degree;
aoqi@0 1155 _lo_stk_degree = lrgs(lo)._next;
aoqi@0 1156 }
aoqi@0 1157
aoqi@0 1158 // Put the simplified guy on the simplified list.
aoqi@0 1159 lrgs(lo)._next = _simplified;
aoqi@0 1160 _simplified = lo;
aoqi@0 1161 // If this guy is "at risk" then mark his current neighbors
aoqi@0 1162 if( lrgs(lo)._at_risk ) {
aoqi@0 1163 IndexSetIterator elements(_ifg->neighbors(lo));
aoqi@0 1164 uint datum;
aoqi@0 1165 while ((datum = elements.next()) != 0) {
aoqi@0 1166 lrgs(datum)._risk_bias = lo;
aoqi@0 1167 }
aoqi@0 1168 }
aoqi@0 1169
aoqi@0 1170 // Yank this guy from the IFG.
aoqi@0 1171 IndexSet *adj = _ifg->remove_node( lo );
aoqi@0 1172
aoqi@0 1173 // If any neighbors' degrees fall below their number of
aoqi@0 1174 // allowed registers, then put that neighbor on the low degree
aoqi@0 1175 // list. Note that 'degree' can only fall and 'numregs' is
aoqi@0 1176 // unchanged by this action. Thus the two are equal at most once,
aoqi@0 1177 // so LRGs hit the lo-degree worklist at most once.
aoqi@0 1178 IndexSetIterator elements(adj);
aoqi@0 1179 uint neighbor;
aoqi@0 1180 while ((neighbor = elements.next()) != 0) {
aoqi@0 1181 LRG *n = &lrgs(neighbor);
aoqi@0 1182 #ifdef ASSERT
aoqi@0 1183 if( VerifyOpto || VerifyRegisterAllocator ) {
aoqi@0 1184 assert( _ifg->effective_degree(neighbor) == n->degree(), "" );
aoqi@0 1185 }
aoqi@0 1186 #endif
aoqi@0 1187
aoqi@0 1188 // Check for just becoming of-low-degree just counting registers.
aoqi@0 1189 // _must_spill live ranges are already on the low degree list.
aoqi@0 1190 if( n->just_lo_degree() && !n->_must_spill ) {
aoqi@0 1191 assert(!(*_ifg->_yanked)[neighbor],"Cannot move to lo degree twice");
aoqi@0 1192 // Pull from hi-degree list
aoqi@0 1193 uint prev = n->_prev;
aoqi@0 1194 uint next = n->_next;
aoqi@0 1195 if( prev ) lrgs(prev)._next = next;
aoqi@0 1196 else _hi_degree = next;
aoqi@0 1197 lrgs(next)._prev = prev;
aoqi@0 1198 n->_next = _lo_degree;
aoqi@0 1199 _lo_degree = neighbor;
aoqi@0 1200 }
aoqi@0 1201 }
aoqi@0 1202 } // End of while lo-degree/lo_stk_degree worklist not empty
aoqi@0 1203
aoqi@0 1204 // Check for got everything: is hi-degree list empty?
aoqi@0 1205 if( !_hi_degree ) break;
aoqi@0 1206
aoqi@0 1207 // Time to pick a potential spill guy
aoqi@0 1208 uint lo_score = _hi_degree;
aoqi@0 1209 double score = lrgs(lo_score).score();
aoqi@0 1210 double area = lrgs(lo_score)._area;
aoqi@0 1211 double cost = lrgs(lo_score)._cost;
aoqi@0 1212 bool bound = lrgs(lo_score)._is_bound;
aoqi@0 1213
aoqi@0 1214 // Find cheapest guy
aoqi@0 1215 debug_only( int lo_no_simplify=0; );
aoqi@0 1216 for( uint i = _hi_degree; i; i = lrgs(i)._next ) {
aoqi@0 1217 assert( !(*_ifg->_yanked)[i], "" );
aoqi@0 1218 // It's just vaguely possible to move hi-degree to lo-degree without
aoqi@0 1219 // going through a just-lo-degree stage: If you remove a double from
aoqi@0 1220 // a float live range it's degree will drop by 2 and you can skip the
aoqi@0 1221 // just-lo-degree stage. It's very rare (shows up after 5000+ methods
aoqi@0 1222 // in -Xcomp of Java2Demo). So just choose this guy to simplify next.
aoqi@0 1223 if( lrgs(i).lo_degree() ) {
aoqi@0 1224 lo_score = i;
aoqi@0 1225 break;
aoqi@0 1226 }
aoqi@0 1227 debug_only( if( lrgs(i)._was_lo ) lo_no_simplify=i; );
aoqi@0 1228 double iscore = lrgs(i).score();
aoqi@0 1229 double iarea = lrgs(i)._area;
aoqi@0 1230 double icost = lrgs(i)._cost;
aoqi@0 1231 bool ibound = lrgs(i)._is_bound;
aoqi@0 1232
aoqi@0 1233 // Compare cost/area of i vs cost/area of lo_score. Smaller cost/area
aoqi@0 1234 // wins. Ties happen because all live ranges in question have spilled
aoqi@0 1235 // a few times before and the spill-score adds a huge number which
aoqi@0 1236 // washes out the low order bits. We are choosing the lesser of 2
aoqi@0 1237 // evils; in this case pick largest area to spill.
aoqi@0 1238 // Ties also happen when live ranges are defined and used only inside
aoqi@0 1239 // one block. In which case their area is 0 and score set to max.
aoqi@0 1240 // In such case choose bound live range over unbound to free registers
aoqi@0 1241 // or with smaller cost to spill.
aoqi@0 1242 if( iscore < score ||
aoqi@0 1243 (iscore == score && iarea > area && lrgs(lo_score)._was_spilled2) ||
aoqi@0 1244 (iscore == score && iarea == area &&
aoqi@0 1245 ( (ibound && !bound) || ibound == bound && (icost < cost) )) ) {
aoqi@0 1246 lo_score = i;
aoqi@0 1247 score = iscore;
aoqi@0 1248 area = iarea;
aoqi@0 1249 cost = icost;
aoqi@0 1250 bound = ibound;
aoqi@0 1251 }
aoqi@0 1252 }
aoqi@0 1253 LRG *lo_lrg = &lrgs(lo_score);
aoqi@0 1254 // The live range we choose for spilling is either hi-degree, or very
aoqi@0 1255 // rarely it can be low-degree. If we choose a hi-degree live range
aoqi@0 1256 // there better not be any lo-degree choices.
aoqi@0 1257 assert( lo_lrg->lo_degree() || !lo_no_simplify, "Live range was lo-degree before coalesce; should simplify" );
aoqi@0 1258
aoqi@0 1259 // Pull from hi-degree list
aoqi@0 1260 uint prev = lo_lrg->_prev;
aoqi@0 1261 uint next = lo_lrg->_next;
aoqi@0 1262 if( prev ) lrgs(prev)._next = next;
aoqi@0 1263 else _hi_degree = next;
aoqi@0 1264 lrgs(next)._prev = prev;
aoqi@0 1265 // Jam him on the lo-degree list, despite his high degree.
aoqi@0 1266 // Maybe he'll get a color, and maybe he'll spill.
aoqi@0 1267 // Only Select() will know.
aoqi@0 1268 lrgs(lo_score)._at_risk = true;
aoqi@0 1269 _lo_degree = lo_score;
aoqi@0 1270 lo_lrg->_next = 0;
aoqi@0 1271
aoqi@0 1272 } // End of while not simplified everything
aoqi@0 1273
aoqi@0 1274 }
aoqi@0 1275
aoqi@0 1276 // Is 'reg' register legal for 'lrg'?
aoqi@0 1277 static bool is_legal_reg(LRG &lrg, OptoReg::Name reg, int chunk) {
aoqi@0 1278 if (reg >= chunk && reg < (chunk + RegMask::CHUNK_SIZE) &&
aoqi@0 1279 lrg.mask().Member(OptoReg::add(reg,-chunk))) {
aoqi@0 1280 // RA uses OptoReg which represent the highest element of a registers set.
aoqi@0 1281 // For example, vectorX (128bit) on x86 uses [XMM,XMMb,XMMc,XMMd] set
aoqi@0 1282 // in which XMMd is used by RA to represent such vectors. A double value
aoqi@0 1283 // uses [XMM,XMMb] pairs and XMMb is used by RA for it.
aoqi@0 1284 // The register mask uses largest bits set of overlapping register sets.
aoqi@0 1285 // On x86 with AVX it uses 8 bits for each XMM registers set.
aoqi@0 1286 //
aoqi@0 1287 // The 'lrg' already has cleared-to-set register mask (done in Select()
aoqi@0 1288 // before calling choose_color()). Passing mask.Member(reg) check above
aoqi@0 1289 // indicates that the size (num_regs) of 'reg' set is less or equal to
aoqi@0 1290 // 'lrg' set size.
aoqi@0 1291 // For set size 1 any register which is member of 'lrg' mask is legal.
aoqi@0 1292 if (lrg.num_regs()==1)
aoqi@0 1293 return true;
aoqi@0 1294 // For larger sets only an aligned register with the same set size is legal.
aoqi@0 1295 int mask = lrg.num_regs()-1;
aoqi@0 1296 if ((reg&mask) == mask)
aoqi@0 1297 return true;
aoqi@0 1298 }
aoqi@0 1299 return false;
aoqi@0 1300 }
aoqi@0 1301
aoqi@0 1302 // Choose a color using the biasing heuristic
aoqi@0 1303 OptoReg::Name PhaseChaitin::bias_color( LRG &lrg, int chunk ) {
aoqi@0 1304
aoqi@0 1305 // Check for "at_risk" LRG's
aoqi@0 1306 uint risk_lrg = _lrg_map.find(lrg._risk_bias);
aoqi@0 1307 if( risk_lrg != 0 ) {
aoqi@0 1308 // Walk the colored neighbors of the "at_risk" candidate
aoqi@0 1309 // Choose a color which is both legal and already taken by a neighbor
aoqi@0 1310 // of the "at_risk" candidate in order to improve the chances of the
aoqi@0 1311 // "at_risk" candidate of coloring
aoqi@0 1312 IndexSetIterator elements(_ifg->neighbors(risk_lrg));
aoqi@0 1313 uint datum;
aoqi@0 1314 while ((datum = elements.next()) != 0) {
aoqi@0 1315 OptoReg::Name reg = lrgs(datum).reg();
aoqi@0 1316 // If this LRG's register is legal for us, choose it
aoqi@0 1317 if (is_legal_reg(lrg, reg, chunk))
aoqi@0 1318 return reg;
aoqi@0 1319 }
aoqi@0 1320 }
aoqi@0 1321
aoqi@0 1322 uint copy_lrg = _lrg_map.find(lrg._copy_bias);
aoqi@0 1323 if( copy_lrg != 0 ) {
aoqi@0 1324 // If he has a color,
aoqi@0 1325 if( !(*(_ifg->_yanked))[copy_lrg] ) {
aoqi@0 1326 OptoReg::Name reg = lrgs(copy_lrg).reg();
aoqi@0 1327 // And it is legal for you,
aoqi@0 1328 if (is_legal_reg(lrg, reg, chunk))
aoqi@0 1329 return reg;
aoqi@0 1330 } else if( chunk == 0 ) {
aoqi@0 1331 // Choose a color which is legal for him
aoqi@0 1332 RegMask tempmask = lrg.mask();
aoqi@0 1333 tempmask.AND(lrgs(copy_lrg).mask());
aoqi@0 1334 tempmask.clear_to_sets(lrg.num_regs());
aoqi@0 1335 OptoReg::Name reg = tempmask.find_first_set(lrg.num_regs());
aoqi@0 1336 if (OptoReg::is_valid(reg))
aoqi@0 1337 return reg;
aoqi@0 1338 }
aoqi@0 1339 }
aoqi@0 1340
aoqi@0 1341 // If no bias info exists, just go with the register selection ordering
aoqi@0 1342 if (lrg._is_vector || lrg.num_regs() == 2) {
aoqi@0 1343 // Find an aligned set
aoqi@0 1344 return OptoReg::add(lrg.mask().find_first_set(lrg.num_regs()),chunk);
aoqi@0 1345 }
aoqi@0 1346
aoqi@0 1347 // CNC - Fun hack. Alternate 1st and 2nd selection. Enables post-allocate
aoqi@0 1348 // copy removal to remove many more copies, by preventing a just-assigned
aoqi@0 1349 // register from being repeatedly assigned.
aoqi@0 1350 OptoReg::Name reg = lrg.mask().find_first_elem();
aoqi@0 1351 if( (++_alternate & 1) && OptoReg::is_valid(reg) ) {
aoqi@0 1352 // This 'Remove; find; Insert' idiom is an expensive way to find the
aoqi@0 1353 // SECOND element in the mask.
aoqi@0 1354 lrg.Remove(reg);
aoqi@0 1355 OptoReg::Name reg2 = lrg.mask().find_first_elem();
aoqi@0 1356 lrg.Insert(reg);
aoqi@0 1357 if( OptoReg::is_reg(reg2))
aoqi@0 1358 reg = reg2;
aoqi@0 1359 }
aoqi@0 1360 return OptoReg::add( reg, chunk );
aoqi@0 1361 }
aoqi@0 1362
aoqi@0 1363 // Choose a color in the current chunk
aoqi@0 1364 OptoReg::Name PhaseChaitin::choose_color( LRG &lrg, int chunk ) {
aoqi@0 1365 assert( C->in_preserve_stack_slots() == 0 || chunk != 0 || lrg._is_bound || lrg.mask().is_bound1() || !lrg.mask().Member(OptoReg::Name(_matcher._old_SP-1)), "must not allocate stack0 (inside preserve area)");
aoqi@0 1366 assert(C->out_preserve_stack_slots() == 0 || chunk != 0 || lrg._is_bound || lrg.mask().is_bound1() || !lrg.mask().Member(OptoReg::Name(_matcher._old_SP+0)), "must not allocate stack0 (inside preserve area)");
aoqi@0 1367
aoqi@0 1368 if( lrg.num_regs() == 1 || // Common Case
aoqi@0 1369 !lrg._fat_proj ) // Aligned+adjacent pairs ok
aoqi@0 1370 // Use a heuristic to "bias" the color choice
aoqi@0 1371 return bias_color(lrg, chunk);
aoqi@0 1372
aoqi@0 1373 assert(!lrg._is_vector, "should be not vector here" );
aoqi@0 1374 assert( lrg.num_regs() >= 2, "dead live ranges do not color" );
aoqi@0 1375
aoqi@0 1376 // Fat-proj case or misaligned double argument.
aoqi@0 1377 assert(lrg.compute_mask_size() == lrg.num_regs() ||
aoqi@0 1378 lrg.num_regs() == 2,"fat projs exactly color" );
aoqi@0 1379 assert( !chunk, "always color in 1st chunk" );
aoqi@0 1380 // Return the highest element in the set.
aoqi@0 1381 return lrg.mask().find_last_elem();
aoqi@0 1382 }
aoqi@0 1383
aoqi@0 1384 // Select colors by re-inserting LRGs back into the IFG. LRGs are re-inserted
aoqi@0 1385 // in reverse order of removal. As long as nothing of hi-degree was yanked,
aoqi@0 1386 // everything going back is guaranteed a color. Select that color. If some
aoqi@0 1387 // hi-degree LRG cannot get a color then we record that we must spill.
aoqi@0 1388 uint PhaseChaitin::Select( ) {
aoqi@0 1389 uint spill_reg = LRG::SPILL_REG;
aoqi@0 1390 _max_reg = OptoReg::Name(0); // Past max register used
aoqi@0 1391 while( _simplified ) {
aoqi@0 1392 // Pull next LRG from the simplified list - in reverse order of removal
aoqi@0 1393 uint lidx = _simplified;
aoqi@0 1394 LRG *lrg = &lrgs(lidx);
aoqi@0 1395 _simplified = lrg->_next;
aoqi@0 1396
aoqi@0 1397
aoqi@0 1398 #ifndef PRODUCT
aoqi@0 1399 if (trace_spilling()) {
aoqi@0 1400 ttyLocker ttyl;
aoqi@0 1401 tty->print_cr("L%d selecting degree %d degrees_of_freedom %d", lidx, lrg->degree(),
aoqi@0 1402 lrg->degrees_of_freedom());
aoqi@0 1403 lrg->dump();
aoqi@0 1404 }
aoqi@0 1405 #endif
aoqi@0 1406
aoqi@0 1407 // Re-insert into the IFG
aoqi@0 1408 _ifg->re_insert(lidx);
aoqi@0 1409 if( !lrg->alive() ) continue;
aoqi@0 1410 // capture allstackedness flag before mask is hacked
aoqi@0 1411 const int is_allstack = lrg->mask().is_AllStack();
aoqi@0 1412
aoqi@0 1413 // Yeah, yeah, yeah, I know, I know. I can refactor this
aoqi@0 1414 // to avoid the GOTO, although the refactored code will not
aoqi@0 1415 // be much clearer. We arrive here IFF we have a stack-based
aoqi@0 1416 // live range that cannot color in the current chunk, and it
aoqi@0 1417 // has to move into the next free stack chunk.
aoqi@0 1418 int chunk = 0; // Current chunk is first chunk
aoqi@0 1419 retry_next_chunk:
aoqi@0 1420
aoqi@0 1421 // Remove neighbor colors
aoqi@0 1422 IndexSet *s = _ifg->neighbors(lidx);
aoqi@0 1423
aoqi@0 1424 debug_only(RegMask orig_mask = lrg->mask();)
aoqi@0 1425 IndexSetIterator elements(s);
aoqi@0 1426 uint neighbor;
aoqi@0 1427 while ((neighbor = elements.next()) != 0) {
aoqi@0 1428 // Note that neighbor might be a spill_reg. In this case, exclusion
aoqi@0 1429 // of its color will be a no-op, since the spill_reg chunk is in outer
aoqi@0 1430 // space. Also, if neighbor is in a different chunk, this exclusion
aoqi@0 1431 // will be a no-op. (Later on, if lrg runs out of possible colors in
aoqi@0 1432 // its chunk, a new chunk of color may be tried, in which case
aoqi@0 1433 // examination of neighbors is started again, at retry_next_chunk.)
aoqi@0 1434 LRG &nlrg = lrgs(neighbor);
aoqi@0 1435 OptoReg::Name nreg = nlrg.reg();
aoqi@0 1436 // Only subtract masks in the same chunk
aoqi@0 1437 if( nreg >= chunk && nreg < chunk + RegMask::CHUNK_SIZE ) {
aoqi@0 1438 #ifndef PRODUCT
aoqi@0 1439 uint size = lrg->mask().Size();
aoqi@0 1440 RegMask rm = lrg->mask();
aoqi@0 1441 #endif
aoqi@0 1442 lrg->SUBTRACT(nlrg.mask());
aoqi@0 1443 #ifndef PRODUCT
aoqi@0 1444 if (trace_spilling() && lrg->mask().Size() != size) {
aoqi@0 1445 ttyLocker ttyl;
aoqi@0 1446 tty->print("L%d ", lidx);
aoqi@0 1447 rm.dump();
aoqi@0 1448 tty->print(" intersected L%d ", neighbor);
aoqi@0 1449 nlrg.mask().dump();
aoqi@0 1450 tty->print(" removed ");
aoqi@0 1451 rm.SUBTRACT(lrg->mask());
aoqi@0 1452 rm.dump();
aoqi@0 1453 tty->print(" leaving ");
aoqi@0 1454 lrg->mask().dump();
aoqi@0 1455 tty->cr();
aoqi@0 1456 }
aoqi@0 1457 #endif
aoqi@0 1458 }
aoqi@0 1459 }
aoqi@0 1460 //assert(is_allstack == lrg->mask().is_AllStack(), "nbrs must not change AllStackedness");
aoqi@0 1461 // Aligned pairs need aligned masks
aoqi@0 1462 assert(!lrg->_is_vector || !lrg->_fat_proj, "sanity");
aoqi@0 1463 if (lrg->num_regs() > 1 && !lrg->_fat_proj) {
aoqi@0 1464 lrg->clear_to_sets();
aoqi@0 1465 }
aoqi@0 1466
aoqi@0 1467 // Check if a color is available and if so pick the color
aoqi@0 1468 OptoReg::Name reg = choose_color( *lrg, chunk );
aoqi@0 1469 #ifdef SPARC
aoqi@0 1470 debug_only(lrg->compute_set_mask_size());
aoqi@0 1471 assert(lrg->num_regs() < 2 || lrg->is_bound() || is_even(reg-1), "allocate all doubles aligned");
aoqi@0 1472 #endif
aoqi@0 1473
aoqi@0 1474 //---------------
aoqi@0 1475 // If we fail to color and the AllStack flag is set, trigger
aoqi@0 1476 // a chunk-rollover event
aoqi@0 1477 if(!OptoReg::is_valid(OptoReg::add(reg,-chunk)) && is_allstack) {
aoqi@0 1478 // Bump register mask up to next stack chunk
aoqi@0 1479 chunk += RegMask::CHUNK_SIZE;
aoqi@0 1480 lrg->Set_All();
aoqi@0 1481
aoqi@0 1482 goto retry_next_chunk;
aoqi@0 1483 }
aoqi@0 1484
aoqi@0 1485 //---------------
aoqi@0 1486 // Did we get a color?
aoqi@0 1487 else if( OptoReg::is_valid(reg)) {
aoqi@0 1488 #ifndef PRODUCT
aoqi@0 1489 RegMask avail_rm = lrg->mask();
aoqi@0 1490 #endif
aoqi@0 1491
aoqi@0 1492 // Record selected register
aoqi@0 1493 lrg->set_reg(reg);
aoqi@0 1494
aoqi@0 1495 if( reg >= _max_reg ) // Compute max register limit
aoqi@0 1496 _max_reg = OptoReg::add(reg,1);
aoqi@0 1497 // Fold reg back into normal space
aoqi@0 1498 reg = OptoReg::add(reg,-chunk);
aoqi@0 1499
aoqi@0 1500 // If the live range is not bound, then we actually had some choices
aoqi@0 1501 // to make. In this case, the mask has more bits in it than the colors
aoqi@0 1502 // chosen. Restrict the mask to just what was picked.
aoqi@0 1503 int n_regs = lrg->num_regs();
aoqi@0 1504 assert(!lrg->_is_vector || !lrg->_fat_proj, "sanity");
aoqi@0 1505 if (n_regs == 1 || !lrg->_fat_proj) {
aoqi@0 1506 assert(!lrg->_is_vector || n_regs <= RegMask::SlotsPerVecY, "sanity");
aoqi@0 1507 lrg->Clear(); // Clear the mask
aoqi@0 1508 lrg->Insert(reg); // Set regmask to match selected reg
aoqi@0 1509 // For vectors and pairs, also insert the low bit of the pair
aoqi@0 1510 for (int i = 1; i < n_regs; i++)
aoqi@0 1511 lrg->Insert(OptoReg::add(reg,-i));
aoqi@0 1512 lrg->set_mask_size(n_regs);
aoqi@0 1513 } else { // Else fatproj
aoqi@0 1514 // mask must be equal to fatproj bits, by definition
aoqi@0 1515 }
aoqi@0 1516 #ifndef PRODUCT
aoqi@0 1517 if (trace_spilling()) {
aoqi@0 1518 ttyLocker ttyl;
aoqi@0 1519 tty->print("L%d selected ", lidx);
aoqi@0 1520 lrg->mask().dump();
aoqi@0 1521 tty->print(" from ");
aoqi@0 1522 avail_rm.dump();
aoqi@0 1523 tty->cr();
aoqi@0 1524 }
aoqi@0 1525 #endif
aoqi@0 1526 // Note that reg is the highest-numbered register in the newly-bound mask.
aoqi@0 1527 } // end color available case
aoqi@0 1528
aoqi@0 1529 //---------------
aoqi@0 1530 // Live range is live and no colors available
aoqi@0 1531 else {
aoqi@0 1532 assert( lrg->alive(), "" );
aoqi@0 1533 assert( !lrg->_fat_proj || lrg->is_multidef() ||
aoqi@0 1534 lrg->_def->outcnt() > 0, "fat_proj cannot spill");
aoqi@0 1535 assert( !orig_mask.is_AllStack(), "All Stack does not spill" );
aoqi@0 1536
aoqi@0 1537 // Assign the special spillreg register
aoqi@0 1538 lrg->set_reg(OptoReg::Name(spill_reg++));
aoqi@0 1539 // Do not empty the regmask; leave mask_size lying around
aoqi@0 1540 // for use during Spilling
aoqi@0 1541 #ifndef PRODUCT
aoqi@0 1542 if( trace_spilling() ) {
aoqi@0 1543 ttyLocker ttyl;
aoqi@0 1544 tty->print("L%d spilling with neighbors: ", lidx);
aoqi@0 1545 s->dump();
aoqi@0 1546 debug_only(tty->print(" original mask: "));
aoqi@0 1547 debug_only(orig_mask.dump());
aoqi@0 1548 dump_lrg(lidx);
aoqi@0 1549 }
aoqi@0 1550 #endif
aoqi@0 1551 } // end spill case
aoqi@0 1552
aoqi@0 1553 }
aoqi@0 1554
aoqi@0 1555 return spill_reg-LRG::SPILL_REG; // Return number of spills
aoqi@0 1556 }
aoqi@0 1557
aoqi@0 1558 // Copy 'was_spilled'-edness from the source Node to the dst Node.
aoqi@0 1559 void PhaseChaitin::copy_was_spilled( Node *src, Node *dst ) {
aoqi@0 1560 if( _spilled_once.test(src->_idx) ) {
aoqi@0 1561 _spilled_once.set(dst->_idx);
aoqi@0 1562 lrgs(_lrg_map.find(dst))._was_spilled1 = 1;
aoqi@0 1563 if( _spilled_twice.test(src->_idx) ) {
aoqi@0 1564 _spilled_twice.set(dst->_idx);
aoqi@0 1565 lrgs(_lrg_map.find(dst))._was_spilled2 = 1;
aoqi@0 1566 }
aoqi@0 1567 }
aoqi@0 1568 }
aoqi@0 1569
aoqi@0 1570 // Set the 'spilled_once' or 'spilled_twice' flag on a node.
aoqi@0 1571 void PhaseChaitin::set_was_spilled( Node *n ) {
aoqi@0 1572 if( _spilled_once.test_set(n->_idx) )
aoqi@0 1573 _spilled_twice.set(n->_idx);
aoqi@0 1574 }
aoqi@0 1575
aoqi@0 1576 // Convert Ideal spill instructions into proper FramePtr + offset Loads and
aoqi@0 1577 // Stores. Use-def chains are NOT preserved, but Node->LRG->reg maps are.
aoqi@0 1578 void PhaseChaitin::fixup_spills() {
aoqi@0 1579 // This function does only cisc spill work.
aoqi@0 1580 if( !UseCISCSpill ) return;
aoqi@0 1581
aoqi@0 1582 NOT_PRODUCT( Compile::TracePhase t3("fixupSpills", &_t_fixupSpills, TimeCompiler); )
aoqi@0 1583
aoqi@0 1584 // Grab the Frame Pointer
aoqi@0 1585 Node *fp = _cfg.get_root_block()->head()->in(1)->in(TypeFunc::FramePtr);
aoqi@0 1586
aoqi@0 1587 // For all blocks
aoqi@0 1588 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 1589 Block* block = _cfg.get_block(i);
aoqi@0 1590
aoqi@0 1591 // For all instructions in block
aoqi@0 1592 uint last_inst = block->end_idx();
aoqi@0 1593 for (uint j = 1; j <= last_inst; j++) {
aoqi@0 1594 Node* n = block->get_node(j);
aoqi@0 1595
aoqi@0 1596 // Dead instruction???
aoqi@0 1597 assert( n->outcnt() != 0 ||// Nothing dead after post alloc
aoqi@0 1598 C->top() == n || // Or the random TOP node
aoqi@0 1599 n->is_Proj(), // Or a fat-proj kill node
aoqi@0 1600 "No dead instructions after post-alloc" );
aoqi@0 1601
aoqi@0 1602 int inp = n->cisc_operand();
aoqi@0 1603 if( inp != AdlcVMDeps::Not_cisc_spillable ) {
aoqi@0 1604 // Convert operand number to edge index number
aoqi@0 1605 MachNode *mach = n->as_Mach();
aoqi@0 1606 inp = mach->operand_index(inp);
aoqi@0 1607 Node *src = n->in(inp); // Value to load or store
aoqi@0 1608 LRG &lrg_cisc = lrgs(_lrg_map.find_const(src));
aoqi@0 1609 OptoReg::Name src_reg = lrg_cisc.reg();
aoqi@0 1610 // Doubles record the HIGH register of an adjacent pair.
aoqi@0 1611 src_reg = OptoReg::add(src_reg,1-lrg_cisc.num_regs());
aoqi@0 1612 if( OptoReg::is_stack(src_reg) ) { // If input is on stack
aoqi@0 1613 // This is a CISC Spill, get stack offset and construct new node
aoqi@0 1614 #ifndef PRODUCT
aoqi@0 1615 if( TraceCISCSpill ) {
aoqi@0 1616 tty->print(" reg-instr: ");
aoqi@0 1617 n->dump();
aoqi@0 1618 }
aoqi@0 1619 #endif
aoqi@0 1620 int stk_offset = reg2offset(src_reg);
aoqi@0 1621 // Bailout if we might exceed node limit when spilling this instruction
aoqi@0 1622 C->check_node_count(0, "out of nodes fixing spills");
aoqi@0 1623 if (C->failing()) return;
aoqi@0 1624 // Transform node
aoqi@0 1625 MachNode *cisc = mach->cisc_version(stk_offset, C)->as_Mach();
aoqi@0 1626 cisc->set_req(inp,fp); // Base register is frame pointer
aoqi@0 1627 if( cisc->oper_input_base() > 1 && mach->oper_input_base() <= 1 ) {
aoqi@0 1628 assert( cisc->oper_input_base() == 2, "Only adding one edge");
aoqi@0 1629 cisc->ins_req(1,src); // Requires a memory edge
aoqi@0 1630 }
aoqi@0 1631 block->map_node(cisc, j); // Insert into basic block
aoqi@0 1632 n->subsume_by(cisc, C); // Correct graph
aoqi@0 1633 //
aoqi@0 1634 ++_used_cisc_instructions;
aoqi@0 1635 #ifndef PRODUCT
aoqi@0 1636 if( TraceCISCSpill ) {
aoqi@0 1637 tty->print(" cisc-instr: ");
aoqi@0 1638 cisc->dump();
aoqi@0 1639 }
aoqi@0 1640 #endif
aoqi@0 1641 } else {
aoqi@0 1642 #ifndef PRODUCT
aoqi@0 1643 if( TraceCISCSpill ) {
aoqi@0 1644 tty->print(" using reg-instr: ");
aoqi@0 1645 n->dump();
aoqi@0 1646 }
aoqi@0 1647 #endif
aoqi@0 1648 ++_unused_cisc_instructions; // input can be on stack
aoqi@0 1649 }
aoqi@0 1650 }
aoqi@0 1651
aoqi@0 1652 } // End of for all instructions
aoqi@0 1653
aoqi@0 1654 } // End of for all blocks
aoqi@0 1655 }
aoqi@0 1656
aoqi@0 1657 // Helper to stretch above; recursively discover the base Node for a
aoqi@0 1658 // given derived Node. Easy for AddP-related machine nodes, but needs
aoqi@0 1659 // to be recursive for derived Phis.
aoqi@0 1660 Node *PhaseChaitin::find_base_for_derived( Node **derived_base_map, Node *derived, uint &maxlrg ) {
aoqi@0 1661 // See if already computed; if so return it
aoqi@0 1662 if( derived_base_map[derived->_idx] )
aoqi@0 1663 return derived_base_map[derived->_idx];
aoqi@0 1664
aoqi@0 1665 // See if this happens to be a base.
aoqi@0 1666 // NOTE: we use TypePtr instead of TypeOopPtr because we can have
aoqi@0 1667 // pointers derived from NULL! These are always along paths that
aoqi@0 1668 // can't happen at run-time but the optimizer cannot deduce it so
aoqi@0 1669 // we have to handle it gracefully.
aoqi@0 1670 assert(!derived->bottom_type()->isa_narrowoop() ||
aoqi@0 1671 derived->bottom_type()->make_ptr()->is_ptr()->_offset == 0, "sanity");
aoqi@0 1672 const TypePtr *tj = derived->bottom_type()->isa_ptr();
aoqi@0 1673 // If its an OOP with a non-zero offset, then it is derived.
aoqi@0 1674 if( tj == NULL || tj->_offset == 0 ) {
aoqi@0 1675 derived_base_map[derived->_idx] = derived;
aoqi@0 1676 return derived;
aoqi@0 1677 }
aoqi@0 1678 // Derived is NULL+offset? Base is NULL!
aoqi@0 1679 if( derived->is_Con() ) {
aoqi@0 1680 Node *base = _matcher.mach_null();
aoqi@0 1681 assert(base != NULL, "sanity");
aoqi@0 1682 if (base->in(0) == NULL) {
aoqi@0 1683 // Initialize it once and make it shared:
aoqi@0 1684 // set control to _root and place it into Start block
aoqi@0 1685 // (where top() node is placed).
aoqi@0 1686 base->init_req(0, _cfg.get_root_node());
aoqi@0 1687 Block *startb = _cfg.get_block_for_node(C->top());
aoqi@0 1688 uint node_pos = startb->find_node(C->top());
aoqi@0 1689 startb->insert_node(base, node_pos);
aoqi@0 1690 _cfg.map_node_to_block(base, startb);
aoqi@0 1691 assert(_lrg_map.live_range_id(base) == 0, "should not have LRG yet");
aoqi@0 1692
aoqi@0 1693 // The loadConP0 might have projection nodes depending on architecture
aoqi@0 1694 // Add the projection nodes to the CFG
aoqi@0 1695 for (DUIterator_Fast imax, i = base->fast_outs(imax); i < imax; i++) {
aoqi@0 1696 Node* use = base->fast_out(i);
aoqi@0 1697 if (use->is_MachProj()) {
aoqi@0 1698 startb->insert_node(use, ++node_pos);
aoqi@0 1699 _cfg.map_node_to_block(use, startb);
aoqi@0 1700 new_lrg(use, maxlrg++);
aoqi@0 1701 }
aoqi@0 1702 }
aoqi@0 1703 }
aoqi@0 1704 if (_lrg_map.live_range_id(base) == 0) {
aoqi@0 1705 new_lrg(base, maxlrg++);
aoqi@0 1706 }
aoqi@0 1707 assert(base->in(0) == _cfg.get_root_node() && _cfg.get_block_for_node(base) == _cfg.get_block_for_node(C->top()), "base NULL should be shared");
aoqi@0 1708 derived_base_map[derived->_idx] = base;
aoqi@0 1709 return base;
aoqi@0 1710 }
aoqi@0 1711
aoqi@0 1712 // Check for AddP-related opcodes
aoqi@0 1713 if (!derived->is_Phi()) {
aoqi@0 1714 assert(derived->as_Mach()->ideal_Opcode() == Op_AddP, err_msg_res("but is: %s", derived->Name()));
aoqi@0 1715 Node *base = derived->in(AddPNode::Base);
aoqi@0 1716 derived_base_map[derived->_idx] = base;
aoqi@0 1717 return base;
aoqi@0 1718 }
aoqi@0 1719
aoqi@0 1720 // Recursively find bases for Phis.
aoqi@0 1721 // First check to see if we can avoid a base Phi here.
aoqi@0 1722 Node *base = find_base_for_derived( derived_base_map, derived->in(1),maxlrg);
aoqi@0 1723 uint i;
aoqi@0 1724 for( i = 2; i < derived->req(); i++ )
aoqi@0 1725 if( base != find_base_for_derived( derived_base_map,derived->in(i),maxlrg))
aoqi@0 1726 break;
aoqi@0 1727 // Went to the end without finding any different bases?
aoqi@0 1728 if( i == derived->req() ) { // No need for a base Phi here
aoqi@0 1729 derived_base_map[derived->_idx] = base;
aoqi@0 1730 return base;
aoqi@0 1731 }
aoqi@0 1732
aoqi@0 1733 // Now we see we need a base-Phi here to merge the bases
aoqi@0 1734 const Type *t = base->bottom_type();
aoqi@0 1735 base = new (C) PhiNode( derived->in(0), t );
aoqi@0 1736 for( i = 1; i < derived->req(); i++ ) {
aoqi@0 1737 base->init_req(i, find_base_for_derived(derived_base_map, derived->in(i), maxlrg));
aoqi@0 1738 t = t->meet(base->in(i)->bottom_type());
aoqi@0 1739 }
aoqi@0 1740 base->as_Phi()->set_type(t);
aoqi@0 1741
aoqi@0 1742 // Search the current block for an existing base-Phi
aoqi@0 1743 Block *b = _cfg.get_block_for_node(derived);
aoqi@0 1744 for( i = 1; i <= b->end_idx(); i++ ) {// Search for matching Phi
aoqi@0 1745 Node *phi = b->get_node(i);
aoqi@0 1746 if( !phi->is_Phi() ) { // Found end of Phis with no match?
aoqi@0 1747 b->insert_node(base, i); // Must insert created Phi here as base
aoqi@0 1748 _cfg.map_node_to_block(base, b);
aoqi@0 1749 new_lrg(base,maxlrg++);
aoqi@0 1750 break;
aoqi@0 1751 }
aoqi@0 1752 // See if Phi matches.
aoqi@0 1753 uint j;
aoqi@0 1754 for( j = 1; j < base->req(); j++ )
aoqi@0 1755 if( phi->in(j) != base->in(j) &&
aoqi@0 1756 !(phi->in(j)->is_Con() && base->in(j)->is_Con()) ) // allow different NULLs
aoqi@0 1757 break;
aoqi@0 1758 if( j == base->req() ) { // All inputs match?
aoqi@0 1759 base = phi; // Then use existing 'phi' and drop 'base'
aoqi@0 1760 break;
aoqi@0 1761 }
aoqi@0 1762 }
aoqi@0 1763
aoqi@0 1764
aoqi@0 1765 // Cache info for later passes
aoqi@0 1766 derived_base_map[derived->_idx] = base;
aoqi@0 1767 return base;
aoqi@0 1768 }
aoqi@0 1769
aoqi@0 1770 // At each Safepoint, insert extra debug edges for each pair of derived value/
aoqi@0 1771 // base pointer that is live across the Safepoint for oopmap building. The
aoqi@0 1772 // edge pairs get added in after sfpt->jvmtail()->oopoff(), but are in the
aoqi@0 1773 // required edge set.
aoqi@0 1774 bool PhaseChaitin::stretch_base_pointer_live_ranges(ResourceArea *a) {
aoqi@0 1775 int must_recompute_live = false;
aoqi@0 1776 uint maxlrg = _lrg_map.max_lrg_id();
aoqi@0 1777 Node **derived_base_map = (Node**)a->Amalloc(sizeof(Node*)*C->unique());
aoqi@0 1778 memset( derived_base_map, 0, sizeof(Node*)*C->unique() );
aoqi@0 1779
aoqi@0 1780 // For all blocks in RPO do...
aoqi@0 1781 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 1782 Block* block = _cfg.get_block(i);
aoqi@0 1783 // Note use of deep-copy constructor. I cannot hammer the original
aoqi@0 1784 // liveout bits, because they are needed by the following coalesce pass.
aoqi@0 1785 IndexSet liveout(_live->live(block));
aoqi@0 1786
aoqi@0 1787 for (uint j = block->end_idx() + 1; j > 1; j--) {
aoqi@0 1788 Node* n = block->get_node(j - 1);
aoqi@0 1789
aoqi@0 1790 // Pre-split compares of loop-phis. Loop-phis form a cycle we would
aoqi@0 1791 // like to see in the same register. Compare uses the loop-phi and so
aoqi@0 1792 // extends its live range BUT cannot be part of the cycle. If this
aoqi@0 1793 // extended live range overlaps with the update of the loop-phi value
aoqi@0 1794 // we need both alive at the same time -- which requires at least 1
aoqi@0 1795 // copy. But because Intel has only 2-address registers we end up with
aoqi@0 1796 // at least 2 copies, one before the loop-phi update instruction and
aoqi@0 1797 // one after. Instead we split the input to the compare just after the
aoqi@0 1798 // phi.
aoqi@0 1799 if( n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_CmpI ) {
aoqi@0 1800 Node *phi = n->in(1);
aoqi@0 1801 if( phi->is_Phi() && phi->as_Phi()->region()->is_Loop() ) {
aoqi@0 1802 Block *phi_block = _cfg.get_block_for_node(phi);
aoqi@0 1803 if (_cfg.get_block_for_node(phi_block->pred(2)) == block) {
aoqi@0 1804 const RegMask *mask = C->matcher()->idealreg2spillmask[Op_RegI];
aoqi@0 1805 Node *spill = new (C) MachSpillCopyNode( phi, *mask, *mask );
aoqi@0 1806 insert_proj( phi_block, 1, spill, maxlrg++ );
aoqi@0 1807 n->set_req(1,spill);
aoqi@0 1808 must_recompute_live = true;
aoqi@0 1809 }
aoqi@0 1810 }
aoqi@0 1811 }
aoqi@0 1812
aoqi@0 1813 // Get value being defined
aoqi@0 1814 uint lidx = _lrg_map.live_range_id(n);
aoqi@0 1815 // Ignore the occasional brand-new live range
aoqi@0 1816 if (lidx && lidx < _lrg_map.max_lrg_id()) {
aoqi@0 1817 // Remove from live-out set
aoqi@0 1818 liveout.remove(lidx);
aoqi@0 1819
aoqi@0 1820 // Copies do not define a new value and so do not interfere.
aoqi@0 1821 // Remove the copies source from the liveout set before interfering.
aoqi@0 1822 uint idx = n->is_Copy();
aoqi@0 1823 if (idx) {
aoqi@0 1824 liveout.remove(_lrg_map.live_range_id(n->in(idx)));
aoqi@0 1825 }
aoqi@0 1826 }
aoqi@0 1827
aoqi@0 1828 // Found a safepoint?
aoqi@0 1829 JVMState *jvms = n->jvms();
aoqi@0 1830 if( jvms ) {
aoqi@0 1831 // Now scan for a live derived pointer
aoqi@0 1832 IndexSetIterator elements(&liveout);
aoqi@0 1833 uint neighbor;
aoqi@0 1834 while ((neighbor = elements.next()) != 0) {
aoqi@0 1835 // Find reaching DEF for base and derived values
aoqi@0 1836 // This works because we are still in SSA during this call.
aoqi@0 1837 Node *derived = lrgs(neighbor)._def;
aoqi@0 1838 const TypePtr *tj = derived->bottom_type()->isa_ptr();
aoqi@0 1839 assert(!derived->bottom_type()->isa_narrowoop() ||
aoqi@0 1840 derived->bottom_type()->make_ptr()->is_ptr()->_offset == 0, "sanity");
aoqi@0 1841 // If its an OOP with a non-zero offset, then it is derived.
aoqi@0 1842 if( tj && tj->_offset != 0 && tj->isa_oop_ptr() ) {
aoqi@0 1843 Node *base = find_base_for_derived(derived_base_map, derived, maxlrg);
aoqi@0 1844 assert(base->_idx < _lrg_map.size(), "");
aoqi@0 1845 // Add reaching DEFs of derived pointer and base pointer as a
aoqi@0 1846 // pair of inputs
aoqi@0 1847 n->add_req(derived);
aoqi@0 1848 n->add_req(base);
aoqi@0 1849
aoqi@0 1850 // See if the base pointer is already live to this point.
aoqi@0 1851 // Since I'm working on the SSA form, live-ness amounts to
aoqi@0 1852 // reaching def's. So if I find the base's live range then
aoqi@0 1853 // I know the base's def reaches here.
aoqi@0 1854 if ((_lrg_map.live_range_id(base) >= _lrg_map.max_lrg_id() || // (Brand new base (hence not live) or
aoqi@0 1855 !liveout.member(_lrg_map.live_range_id(base))) && // not live) AND
aoqi@0 1856 (_lrg_map.live_range_id(base) > 0) && // not a constant
aoqi@0 1857 _cfg.get_block_for_node(base) != block) { // base not def'd in blk)
aoqi@0 1858 // Base pointer is not currently live. Since I stretched
aoqi@0 1859 // the base pointer to here and it crosses basic-block
aoqi@0 1860 // boundaries, the global live info is now incorrect.
aoqi@0 1861 // Recompute live.
aoqi@0 1862 must_recompute_live = true;
aoqi@0 1863 } // End of if base pointer is not live to debug info
aoqi@0 1864 }
aoqi@0 1865 } // End of scan all live data for derived ptrs crossing GC point
aoqi@0 1866 } // End of if found a GC point
aoqi@0 1867
aoqi@0 1868 // Make all inputs live
aoqi@0 1869 if (!n->is_Phi()) { // Phi function uses come from prior block
aoqi@0 1870 for (uint k = 1; k < n->req(); k++) {
aoqi@0 1871 uint lidx = _lrg_map.live_range_id(n->in(k));
aoqi@0 1872 if (lidx < _lrg_map.max_lrg_id()) {
aoqi@0 1873 liveout.insert(lidx);
aoqi@0 1874 }
aoqi@0 1875 }
aoqi@0 1876 }
aoqi@0 1877
aoqi@0 1878 } // End of forall instructions in block
aoqi@0 1879 liveout.clear(); // Free the memory used by liveout.
aoqi@0 1880
aoqi@0 1881 } // End of forall blocks
aoqi@0 1882 _lrg_map.set_max_lrg_id(maxlrg);
aoqi@0 1883
aoqi@0 1884 // If I created a new live range I need to recompute live
aoqi@0 1885 if (maxlrg != _ifg->_maxlrg) {
aoqi@0 1886 must_recompute_live = true;
aoqi@0 1887 }
aoqi@0 1888
aoqi@0 1889 return must_recompute_live != 0;
aoqi@0 1890 }
aoqi@0 1891
aoqi@0 1892 // Extend the node to LRG mapping
aoqi@0 1893
aoqi@0 1894 void PhaseChaitin::add_reference(const Node *node, const Node *old_node) {
aoqi@0 1895 _lrg_map.extend(node->_idx, _lrg_map.live_range_id(old_node));
aoqi@0 1896 }
aoqi@0 1897
aoqi@0 1898 #ifndef PRODUCT
aoqi@0 1899 void PhaseChaitin::dump(const Node *n) const {
aoqi@0 1900 uint r = (n->_idx < _lrg_map.size()) ? _lrg_map.find_const(n) : 0;
aoqi@0 1901 tty->print("L%d",r);
aoqi@0 1902 if (r && n->Opcode() != Op_Phi) {
aoqi@0 1903 if( _node_regs ) { // Got a post-allocation copy of allocation?
aoqi@0 1904 tty->print("[");
aoqi@0 1905 OptoReg::Name second = get_reg_second(n);
aoqi@0 1906 if( OptoReg::is_valid(second) ) {
aoqi@0 1907 if( OptoReg::is_reg(second) )
aoqi@0 1908 tty->print("%s:",Matcher::regName[second]);
aoqi@0 1909 else
aoqi@0 1910 tty->print("%s+%d:",OptoReg::regname(OptoReg::c_frame_pointer), reg2offset_unchecked(second));
aoqi@0 1911 }
aoqi@0 1912 OptoReg::Name first = get_reg_first(n);
aoqi@0 1913 if( OptoReg::is_reg(first) )
aoqi@0 1914 tty->print("%s]",Matcher::regName[first]);
aoqi@0 1915 else
aoqi@0 1916 tty->print("%s+%d]",OptoReg::regname(OptoReg::c_frame_pointer), reg2offset_unchecked(first));
aoqi@0 1917 } else
aoqi@0 1918 n->out_RegMask().dump();
aoqi@0 1919 }
aoqi@0 1920 tty->print("/N%d\t",n->_idx);
aoqi@0 1921 tty->print("%s === ", n->Name());
aoqi@0 1922 uint k;
aoqi@0 1923 for (k = 0; k < n->req(); k++) {
aoqi@0 1924 Node *m = n->in(k);
aoqi@0 1925 if (!m) {
aoqi@0 1926 tty->print("_ ");
aoqi@0 1927 }
aoqi@0 1928 else {
aoqi@0 1929 uint r = (m->_idx < _lrg_map.size()) ? _lrg_map.find_const(m) : 0;
aoqi@0 1930 tty->print("L%d",r);
aoqi@0 1931 // Data MultiNode's can have projections with no real registers.
aoqi@0 1932 // Don't die while dumping them.
aoqi@0 1933 int op = n->Opcode();
aoqi@0 1934 if( r && op != Op_Phi && op != Op_Proj && op != Op_SCMemProj) {
aoqi@0 1935 if( _node_regs ) {
aoqi@0 1936 tty->print("[");
aoqi@0 1937 OptoReg::Name second = get_reg_second(n->in(k));
aoqi@0 1938 if( OptoReg::is_valid(second) ) {
aoqi@0 1939 if( OptoReg::is_reg(second) )
aoqi@0 1940 tty->print("%s:",Matcher::regName[second]);
aoqi@0 1941 else
aoqi@0 1942 tty->print("%s+%d:",OptoReg::regname(OptoReg::c_frame_pointer),
aoqi@0 1943 reg2offset_unchecked(second));
aoqi@0 1944 }
aoqi@0 1945 OptoReg::Name first = get_reg_first(n->in(k));
aoqi@0 1946 if( OptoReg::is_reg(first) )
aoqi@0 1947 tty->print("%s]",Matcher::regName[first]);
aoqi@0 1948 else
aoqi@0 1949 tty->print("%s+%d]",OptoReg::regname(OptoReg::c_frame_pointer),
aoqi@0 1950 reg2offset_unchecked(first));
aoqi@0 1951 } else
aoqi@0 1952 n->in_RegMask(k).dump();
aoqi@0 1953 }
aoqi@0 1954 tty->print("/N%d ",m->_idx);
aoqi@0 1955 }
aoqi@0 1956 }
aoqi@0 1957 if( k < n->len() && n->in(k) ) tty->print("| ");
aoqi@0 1958 for( ; k < n->len(); k++ ) {
aoqi@0 1959 Node *m = n->in(k);
aoqi@0 1960 if(!m) {
aoqi@0 1961 break;
aoqi@0 1962 }
aoqi@0 1963 uint r = (m->_idx < _lrg_map.size()) ? _lrg_map.find_const(m) : 0;
aoqi@0 1964 tty->print("L%d",r);
aoqi@0 1965 tty->print("/N%d ",m->_idx);
aoqi@0 1966 }
aoqi@0 1967 if( n->is_Mach() ) n->as_Mach()->dump_spec(tty);
aoqi@0 1968 else n->dump_spec(tty);
aoqi@0 1969 if( _spilled_once.test(n->_idx ) ) {
aoqi@0 1970 tty->print(" Spill_1");
aoqi@0 1971 if( _spilled_twice.test(n->_idx ) )
aoqi@0 1972 tty->print(" Spill_2");
aoqi@0 1973 }
aoqi@0 1974 tty->print("\n");
aoqi@0 1975 }
aoqi@0 1976
aoqi@0 1977 void PhaseChaitin::dump(const Block *b) const {
aoqi@0 1978 b->dump_head(&_cfg);
aoqi@0 1979
aoqi@0 1980 // For all instructions
aoqi@0 1981 for( uint j = 0; j < b->number_of_nodes(); j++ )
aoqi@0 1982 dump(b->get_node(j));
aoqi@0 1983 // Print live-out info at end of block
aoqi@0 1984 if( _live ) {
aoqi@0 1985 tty->print("Liveout: ");
aoqi@0 1986 IndexSet *live = _live->live(b);
aoqi@0 1987 IndexSetIterator elements(live);
aoqi@0 1988 tty->print("{");
aoqi@0 1989 uint i;
aoqi@0 1990 while ((i = elements.next()) != 0) {
aoqi@0 1991 tty->print("L%d ", _lrg_map.find_const(i));
aoqi@0 1992 }
aoqi@0 1993 tty->print_cr("}");
aoqi@0 1994 }
aoqi@0 1995 tty->print("\n");
aoqi@0 1996 }
aoqi@0 1997
aoqi@0 1998 void PhaseChaitin::dump() const {
aoqi@0 1999 tty->print( "--- Chaitin -- argsize: %d framesize: %d ---\n",
aoqi@0 2000 _matcher._new_SP, _framesize );
aoqi@0 2001
aoqi@0 2002 // For all blocks
aoqi@0 2003 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 2004 dump(_cfg.get_block(i));
aoqi@0 2005 }
aoqi@0 2006 // End of per-block dump
aoqi@0 2007 tty->print("\n");
aoqi@0 2008
aoqi@0 2009 if (!_ifg) {
aoqi@0 2010 tty->print("(No IFG.)\n");
aoqi@0 2011 return;
aoqi@0 2012 }
aoqi@0 2013
aoqi@0 2014 // Dump LRG array
aoqi@0 2015 tty->print("--- Live RanGe Array ---\n");
aoqi@0 2016 for (uint i2 = 1; i2 < _lrg_map.max_lrg_id(); i2++) {
aoqi@0 2017 tty->print("L%d: ",i2);
aoqi@0 2018 if (i2 < _ifg->_maxlrg) {
aoqi@0 2019 lrgs(i2).dump();
aoqi@0 2020 }
aoqi@0 2021 else {
aoqi@0 2022 tty->print_cr("new LRG");
aoqi@0 2023 }
aoqi@0 2024 }
aoqi@0 2025 tty->cr();
aoqi@0 2026
aoqi@0 2027 // Dump lo-degree list
aoqi@0 2028 tty->print("Lo degree: ");
aoqi@0 2029 for(uint i3 = _lo_degree; i3; i3 = lrgs(i3)._next )
aoqi@0 2030 tty->print("L%d ",i3);
aoqi@0 2031 tty->cr();
aoqi@0 2032
aoqi@0 2033 // Dump lo-stk-degree list
aoqi@0 2034 tty->print("Lo stk degree: ");
aoqi@0 2035 for(uint i4 = _lo_stk_degree; i4; i4 = lrgs(i4)._next )
aoqi@0 2036 tty->print("L%d ",i4);
aoqi@0 2037 tty->cr();
aoqi@0 2038
aoqi@0 2039 // Dump lo-degree list
aoqi@0 2040 tty->print("Hi degree: ");
aoqi@0 2041 for(uint i5 = _hi_degree; i5; i5 = lrgs(i5)._next )
aoqi@0 2042 tty->print("L%d ",i5);
aoqi@0 2043 tty->cr();
aoqi@0 2044 }
aoqi@0 2045
aoqi@0 2046 void PhaseChaitin::dump_degree_lists() const {
aoqi@0 2047 // Dump lo-degree list
aoqi@0 2048 tty->print("Lo degree: ");
aoqi@0 2049 for( uint i = _lo_degree; i; i = lrgs(i)._next )
aoqi@0 2050 tty->print("L%d ",i);
aoqi@0 2051 tty->cr();
aoqi@0 2052
aoqi@0 2053 // Dump lo-stk-degree list
aoqi@0 2054 tty->print("Lo stk degree: ");
aoqi@0 2055 for(uint i2 = _lo_stk_degree; i2; i2 = lrgs(i2)._next )
aoqi@0 2056 tty->print("L%d ",i2);
aoqi@0 2057 tty->cr();
aoqi@0 2058
aoqi@0 2059 // Dump lo-degree list
aoqi@0 2060 tty->print("Hi degree: ");
aoqi@0 2061 for(uint i3 = _hi_degree; i3; i3 = lrgs(i3)._next )
aoqi@0 2062 tty->print("L%d ",i3);
aoqi@0 2063 tty->cr();
aoqi@0 2064 }
aoqi@0 2065
aoqi@0 2066 void PhaseChaitin::dump_simplified() const {
aoqi@0 2067 tty->print("Simplified: ");
aoqi@0 2068 for( uint i = _simplified; i; i = lrgs(i)._next )
aoqi@0 2069 tty->print("L%d ",i);
aoqi@0 2070 tty->cr();
aoqi@0 2071 }
aoqi@0 2072
aoqi@0 2073 static char *print_reg( OptoReg::Name reg, const PhaseChaitin *pc, char *buf ) {
aoqi@0 2074 if ((int)reg < 0)
aoqi@0 2075 sprintf(buf, "<OptoReg::%d>", (int)reg);
aoqi@0 2076 else if (OptoReg::is_reg(reg))
aoqi@0 2077 strcpy(buf, Matcher::regName[reg]);
aoqi@0 2078 else
aoqi@0 2079 sprintf(buf,"%s + #%d",OptoReg::regname(OptoReg::c_frame_pointer),
aoqi@0 2080 pc->reg2offset(reg));
aoqi@0 2081 return buf+strlen(buf);
aoqi@0 2082 }
aoqi@0 2083
aoqi@0 2084 // Dump a register name into a buffer. Be intelligent if we get called
aoqi@0 2085 // before allocation is complete.
aoqi@0 2086 char *PhaseChaitin::dump_register( const Node *n, char *buf ) const {
aoqi@0 2087 if( !this ) { // Not got anything?
aoqi@0 2088 sprintf(buf,"N%d",n->_idx); // Then use Node index
aoqi@0 2089 } else if( _node_regs ) {
aoqi@0 2090 // Post allocation, use direct mappings, no LRG info available
aoqi@0 2091 print_reg( get_reg_first(n), this, buf );
aoqi@0 2092 } else {
aoqi@0 2093 uint lidx = _lrg_map.find_const(n); // Grab LRG number
aoqi@0 2094 if( !_ifg ) {
aoqi@0 2095 sprintf(buf,"L%d",lidx); // No register binding yet
aoqi@0 2096 } else if( !lidx ) { // Special, not allocated value
aoqi@0 2097 strcpy(buf,"Special");
aoqi@0 2098 } else {
aoqi@0 2099 if (lrgs(lidx)._is_vector) {
aoqi@0 2100 if (lrgs(lidx).mask().is_bound_set(lrgs(lidx).num_regs()))
aoqi@0 2101 print_reg( lrgs(lidx).reg(), this, buf ); // a bound machine register
aoqi@0 2102 else
aoqi@0 2103 sprintf(buf,"L%d",lidx); // No register binding yet
aoqi@0 2104 } else if( (lrgs(lidx).num_regs() == 1)
aoqi@0 2105 ? lrgs(lidx).mask().is_bound1()
aoqi@0 2106 : lrgs(lidx).mask().is_bound_pair() ) {
aoqi@0 2107 // Hah! We have a bound machine register
aoqi@0 2108 print_reg( lrgs(lidx).reg(), this, buf );
aoqi@0 2109 } else {
aoqi@0 2110 sprintf(buf,"L%d",lidx); // No register binding yet
aoqi@0 2111 }
aoqi@0 2112 }
aoqi@0 2113 }
aoqi@0 2114 return buf+strlen(buf);
aoqi@0 2115 }
aoqi@0 2116
aoqi@0 2117 void PhaseChaitin::dump_for_spill_split_recycle() const {
aoqi@0 2118 if( WizardMode && (PrintCompilation || PrintOpto) ) {
aoqi@0 2119 // Display which live ranges need to be split and the allocator's state
aoqi@0 2120 tty->print_cr("Graph-Coloring Iteration %d will split the following live ranges", _trip_cnt);
aoqi@0 2121 for (uint bidx = 1; bidx < _lrg_map.max_lrg_id(); bidx++) {
aoqi@0 2122 if( lrgs(bidx).alive() && lrgs(bidx).reg() >= LRG::SPILL_REG ) {
aoqi@0 2123 tty->print("L%d: ", bidx);
aoqi@0 2124 lrgs(bidx).dump();
aoqi@0 2125 }
aoqi@0 2126 }
aoqi@0 2127 tty->cr();
aoqi@0 2128 dump();
aoqi@0 2129 }
aoqi@0 2130 }
aoqi@0 2131
aoqi@0 2132 void PhaseChaitin::dump_frame() const {
aoqi@0 2133 const char *fp = OptoReg::regname(OptoReg::c_frame_pointer);
aoqi@0 2134 const TypeTuple *domain = C->tf()->domain();
aoqi@0 2135 const int argcnt = domain->cnt() - TypeFunc::Parms;
aoqi@0 2136
aoqi@0 2137 // Incoming arguments in registers dump
aoqi@0 2138 for( int k = 0; k < argcnt; k++ ) {
aoqi@0 2139 OptoReg::Name parmreg = _matcher._parm_regs[k].first();
aoqi@0 2140 if( OptoReg::is_reg(parmreg)) {
aoqi@0 2141 const char *reg_name = OptoReg::regname(parmreg);
aoqi@0 2142 tty->print("#r%3.3d %s", parmreg, reg_name);
aoqi@0 2143 parmreg = _matcher._parm_regs[k].second();
aoqi@0 2144 if( OptoReg::is_reg(parmreg)) {
aoqi@0 2145 tty->print(":%s", OptoReg::regname(parmreg));
aoqi@0 2146 }
aoqi@0 2147 tty->print(" : parm %d: ", k);
aoqi@0 2148 domain->field_at(k + TypeFunc::Parms)->dump();
aoqi@0 2149 tty->cr();
aoqi@0 2150 }
aoqi@0 2151 }
aoqi@0 2152
aoqi@0 2153 // Check for un-owned padding above incoming args
aoqi@0 2154 OptoReg::Name reg = _matcher._new_SP;
aoqi@0 2155 if( reg > _matcher._in_arg_limit ) {
aoqi@0 2156 reg = OptoReg::add(reg, -1);
aoqi@0 2157 tty->print_cr("#r%3.3d %s+%2d: pad0, owned by CALLER", reg, fp, reg2offset_unchecked(reg));
aoqi@0 2158 }
aoqi@0 2159
aoqi@0 2160 // Incoming argument area dump
aoqi@0 2161 OptoReg::Name begin_in_arg = OptoReg::add(_matcher._old_SP,C->out_preserve_stack_slots());
aoqi@0 2162 while( reg > begin_in_arg ) {
aoqi@0 2163 reg = OptoReg::add(reg, -1);
aoqi@0 2164 tty->print("#r%3.3d %s+%2d: ",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2165 int j;
aoqi@0 2166 for( j = 0; j < argcnt; j++) {
aoqi@0 2167 if( _matcher._parm_regs[j].first() == reg ||
aoqi@0 2168 _matcher._parm_regs[j].second() == reg ) {
aoqi@0 2169 tty->print("parm %d: ",j);
aoqi@0 2170 domain->field_at(j + TypeFunc::Parms)->dump();
aoqi@0 2171 tty->cr();
aoqi@0 2172 break;
aoqi@0 2173 }
aoqi@0 2174 }
aoqi@0 2175 if( j >= argcnt )
aoqi@0 2176 tty->print_cr("HOLE, owned by SELF");
aoqi@0 2177 }
aoqi@0 2178
aoqi@0 2179 // Old outgoing preserve area
aoqi@0 2180 while( reg > _matcher._old_SP ) {
aoqi@0 2181 reg = OptoReg::add(reg, -1);
aoqi@0 2182 tty->print_cr("#r%3.3d %s+%2d: old out preserve",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2183 }
aoqi@0 2184
aoqi@0 2185 // Old SP
aoqi@0 2186 tty->print_cr("# -- Old %s -- Framesize: %d --",fp,
aoqi@0 2187 reg2offset_unchecked(OptoReg::add(_matcher._old_SP,-1)) - reg2offset_unchecked(_matcher._new_SP)+jintSize);
aoqi@0 2188
aoqi@0 2189 // Preserve area dump
aoqi@0 2190 int fixed_slots = C->fixed_slots();
aoqi@0 2191 OptoReg::Name begin_in_preserve = OptoReg::add(_matcher._old_SP, -(int)C->in_preserve_stack_slots());
aoqi@0 2192 OptoReg::Name return_addr = _matcher.return_addr();
aoqi@0 2193
aoqi@0 2194 reg = OptoReg::add(reg, -1);
aoqi@0 2195 while (OptoReg::is_stack(reg)) {
aoqi@0 2196 tty->print("#r%3.3d %s+%2d: ",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2197 if (return_addr == reg) {
aoqi@0 2198 tty->print_cr("return address");
aoqi@0 2199 } else if (reg >= begin_in_preserve) {
aoqi@0 2200 // Preserved slots are present on x86
aoqi@0 2201 if (return_addr == OptoReg::add(reg, VMRegImpl::slots_per_word))
aoqi@0 2202 tty->print_cr("saved fp register");
aoqi@0 2203 else if (return_addr == OptoReg::add(reg, 2*VMRegImpl::slots_per_word) &&
aoqi@0 2204 VerifyStackAtCalls)
aoqi@0 2205 tty->print_cr("0xBADB100D +VerifyStackAtCalls");
aoqi@0 2206 else
aoqi@0 2207 tty->print_cr("in_preserve");
aoqi@0 2208 } else if ((int)OptoReg::reg2stack(reg) < fixed_slots) {
aoqi@0 2209 tty->print_cr("Fixed slot %d", OptoReg::reg2stack(reg));
aoqi@0 2210 } else {
aoqi@0 2211 tty->print_cr("pad2, stack alignment");
aoqi@0 2212 }
aoqi@0 2213 reg = OptoReg::add(reg, -1);
aoqi@0 2214 }
aoqi@0 2215
aoqi@0 2216 // Spill area dump
aoqi@0 2217 reg = OptoReg::add(_matcher._new_SP, _framesize );
aoqi@0 2218 while( reg > _matcher._out_arg_limit ) {
aoqi@0 2219 reg = OptoReg::add(reg, -1);
aoqi@0 2220 tty->print_cr("#r%3.3d %s+%2d: spill",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2221 }
aoqi@0 2222
aoqi@0 2223 // Outgoing argument area dump
aoqi@0 2224 while( reg > OptoReg::add(_matcher._new_SP, C->out_preserve_stack_slots()) ) {
aoqi@0 2225 reg = OptoReg::add(reg, -1);
aoqi@0 2226 tty->print_cr("#r%3.3d %s+%2d: outgoing argument",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2227 }
aoqi@0 2228
aoqi@0 2229 // Outgoing new preserve area
aoqi@0 2230 while( reg > _matcher._new_SP ) {
aoqi@0 2231 reg = OptoReg::add(reg, -1);
aoqi@0 2232 tty->print_cr("#r%3.3d %s+%2d: new out preserve",reg,fp,reg2offset_unchecked(reg));
aoqi@0 2233 }
aoqi@0 2234 tty->print_cr("#");
aoqi@0 2235 }
aoqi@0 2236
aoqi@0 2237 void PhaseChaitin::dump_bb( uint pre_order ) const {
aoqi@0 2238 tty->print_cr("---dump of B%d---",pre_order);
aoqi@0 2239 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 2240 Block* block = _cfg.get_block(i);
aoqi@0 2241 if (block->_pre_order == pre_order) {
aoqi@0 2242 dump(block);
aoqi@0 2243 }
aoqi@0 2244 }
aoqi@0 2245 }
aoqi@0 2246
aoqi@0 2247 void PhaseChaitin::dump_lrg( uint lidx, bool defs_only ) const {
aoqi@0 2248 tty->print_cr("---dump of L%d---",lidx);
aoqi@0 2249
aoqi@0 2250 if (_ifg) {
aoqi@0 2251 if (lidx >= _lrg_map.max_lrg_id()) {
aoqi@0 2252 tty->print("Attempt to print live range index beyond max live range.\n");
aoqi@0 2253 return;
aoqi@0 2254 }
aoqi@0 2255 tty->print("L%d: ",lidx);
aoqi@0 2256 if (lidx < _ifg->_maxlrg) {
aoqi@0 2257 lrgs(lidx).dump();
aoqi@0 2258 } else {
aoqi@0 2259 tty->print_cr("new LRG");
aoqi@0 2260 }
aoqi@0 2261 }
aoqi@0 2262 if( _ifg && lidx < _ifg->_maxlrg) {
aoqi@0 2263 tty->print("Neighbors: %d - ", _ifg->neighbor_cnt(lidx));
aoqi@0 2264 _ifg->neighbors(lidx)->dump();
aoqi@0 2265 tty->cr();
aoqi@0 2266 }
aoqi@0 2267 // For all blocks
aoqi@0 2268 for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
aoqi@0 2269 Block* block = _cfg.get_block(i);
aoqi@0 2270 int dump_once = 0;
aoqi@0 2271
aoqi@0 2272 // For all instructions
aoqi@0 2273 for( uint j = 0; j < block->number_of_nodes(); j++ ) {
aoqi@0 2274 Node *n = block->get_node(j);
aoqi@0 2275 if (_lrg_map.find_const(n) == lidx) {
aoqi@0 2276 if (!dump_once++) {
aoqi@0 2277 tty->cr();
aoqi@0 2278 block->dump_head(&_cfg);
aoqi@0 2279 }
aoqi@0 2280 dump(n);
aoqi@0 2281 continue;
aoqi@0 2282 }
aoqi@0 2283 if (!defs_only) {
aoqi@0 2284 uint cnt = n->req();
aoqi@0 2285 for( uint k = 1; k < cnt; k++ ) {
aoqi@0 2286 Node *m = n->in(k);
aoqi@0 2287 if (!m) {
aoqi@0 2288 continue; // be robust in the dumper
aoqi@0 2289 }
aoqi@0 2290 if (_lrg_map.find_const(m) == lidx) {
aoqi@0 2291 if (!dump_once++) {
aoqi@0 2292 tty->cr();
aoqi@0 2293 block->dump_head(&_cfg);
aoqi@0 2294 }
aoqi@0 2295 dump(n);
aoqi@0 2296 }
aoqi@0 2297 }
aoqi@0 2298 }
aoqi@0 2299 }
aoqi@0 2300 } // End of per-block dump
aoqi@0 2301 tty->cr();
aoqi@0 2302 }
aoqi@0 2303 #endif // not PRODUCT
aoqi@0 2304
aoqi@0 2305 int PhaseChaitin::_final_loads = 0;
aoqi@0 2306 int PhaseChaitin::_final_stores = 0;
aoqi@0 2307 int PhaseChaitin::_final_memoves= 0;
aoqi@0 2308 int PhaseChaitin::_final_copies = 0;
aoqi@0 2309 double PhaseChaitin::_final_load_cost = 0;
aoqi@0 2310 double PhaseChaitin::_final_store_cost = 0;
aoqi@0 2311 double PhaseChaitin::_final_memove_cost= 0;
aoqi@0 2312 double PhaseChaitin::_final_copy_cost = 0;
aoqi@0 2313 int PhaseChaitin::_conserv_coalesce = 0;
aoqi@0 2314 int PhaseChaitin::_conserv_coalesce_pair = 0;
aoqi@0 2315 int PhaseChaitin::_conserv_coalesce_trie = 0;
aoqi@0 2316 int PhaseChaitin::_conserv_coalesce_quad = 0;
aoqi@0 2317 int PhaseChaitin::_post_alloc = 0;
aoqi@0 2318 int PhaseChaitin::_lost_opp_pp_coalesce = 0;
aoqi@0 2319 int PhaseChaitin::_lost_opp_cflow_coalesce = 0;
aoqi@0 2320 int PhaseChaitin::_used_cisc_instructions = 0;
aoqi@0 2321 int PhaseChaitin::_unused_cisc_instructions = 0;
aoqi@0 2322 int PhaseChaitin::_allocator_attempts = 0;
aoqi@0 2323 int PhaseChaitin::_allocator_successes = 0;
aoqi@0 2324
aoqi@0 2325 #ifndef PRODUCT
aoqi@0 2326 uint PhaseChaitin::_high_pressure = 0;
aoqi@0 2327 uint PhaseChaitin::_low_pressure = 0;
aoqi@0 2328
aoqi@0 2329 void PhaseChaitin::print_chaitin_statistics() {
aoqi@0 2330 tty->print_cr("Inserted %d spill loads, %d spill stores, %d mem-mem moves and %d copies.", _final_loads, _final_stores, _final_memoves, _final_copies);
aoqi@0 2331 tty->print_cr("Total load cost= %6.0f, store cost = %6.0f, mem-mem cost = %5.2f, copy cost = %5.0f.", _final_load_cost, _final_store_cost, _final_memove_cost, _final_copy_cost);
aoqi@0 2332 tty->print_cr("Adjusted spill cost = %7.0f.",
aoqi@0 2333 _final_load_cost*4.0 + _final_store_cost * 2.0 +
aoqi@0 2334 _final_copy_cost*1.0 + _final_memove_cost*12.0);
aoqi@0 2335 tty->print("Conservatively coalesced %d copies, %d pairs",
aoqi@0 2336 _conserv_coalesce, _conserv_coalesce_pair);
aoqi@0 2337 if( _conserv_coalesce_trie || _conserv_coalesce_quad )
aoqi@0 2338 tty->print(", %d tries, %d quads", _conserv_coalesce_trie, _conserv_coalesce_quad);
aoqi@0 2339 tty->print_cr(", %d post alloc.", _post_alloc);
aoqi@0 2340 if( _lost_opp_pp_coalesce || _lost_opp_cflow_coalesce )
aoqi@0 2341 tty->print_cr("Lost coalesce opportunity, %d private-private, and %d cflow interfered.",
aoqi@0 2342 _lost_opp_pp_coalesce, _lost_opp_cflow_coalesce );
aoqi@0 2343 if( _used_cisc_instructions || _unused_cisc_instructions )
aoqi@0 2344 tty->print_cr("Used cisc instruction %d, remained in register %d",
aoqi@0 2345 _used_cisc_instructions, _unused_cisc_instructions);
aoqi@0 2346 if( _allocator_successes != 0 )
aoqi@0 2347 tty->print_cr("Average allocation trips %f", (float)_allocator_attempts/(float)_allocator_successes);
aoqi@0 2348 tty->print_cr("High Pressure Blocks = %d, Low Pressure Blocks = %d", _high_pressure, _low_pressure);
aoqi@0 2349 }
aoqi@0 2350 #endif // not PRODUCT

mercurial