chaitin.cpp 84.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "precompiled.hpp"
#include "compiler/compileLog.hpp"
#include "compiler/oopMap.hpp"
#include "memory/allocation.inline.hpp"
#include "opto/addnode.hpp"
#include "opto/block.hpp"
#include "opto/callnode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/chaitin.hpp"
#include "opto/coalesce.hpp"
#include "opto/connode.hpp"
#include "opto/idealGraphPrinter.hpp"
#include "opto/indexSet.hpp"
#include "opto/machnode.hpp"
#include "opto/memnode.hpp"
#include "opto/opcodes.hpp"
#include "opto/rootnode.hpp"
D
duke 已提交
42 43

#ifndef PRODUCT
44
void LRG::dump() const {
D
duke 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
  ttyLocker ttyl;
  tty->print("%d ",num_regs());
  _mask.dump();
  if( _msize_valid ) {
    if( mask_size() == compute_mask_size() ) tty->print(", #%d ",_mask_size);
    else tty->print(", #!!!_%d_vs_%d ",_mask_size,_mask.Size());
  } else {
    tty->print(", #?(%d) ",_mask.Size());
  }

  tty->print("EffDeg: ");
  if( _degree_valid ) tty->print( "%d ", _eff_degree );
  else tty->print("? ");

59
  if( is_multidef() ) {
D
duke 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    tty->print("MultiDef ");
    if (_defs != NULL) {
      tty->print("(");
      for (int i = 0; i < _defs->length(); i++) {
        tty->print("N%d ", _defs->at(i)->_idx);
      }
      tty->print(") ");
    }
  }
  else if( _def == 0 ) tty->print("Dead ");
  else tty->print("Def: N%d ",_def->_idx);

  tty->print("Cost:%4.2g Area:%4.2g Score:%4.2g ",_cost,_area, score());
  // Flags
  if( _is_oop ) tty->print("Oop ");
  if( _is_float ) tty->print("Float ");
76
  if( _is_vector ) tty->print("Vector ");
D
duke 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  if( _was_spilled1 ) tty->print("Spilled ");
  if( _was_spilled2 ) tty->print("Spilled2 ");
  if( _direct_conflict ) tty->print("Direct_conflict ");
  if( _fat_proj ) tty->print("Fat ");
  if( _was_lo ) tty->print("Lo ");
  if( _has_copy ) tty->print("Copy ");
  if( _at_risk ) tty->print("Risk ");

  if( _must_spill ) tty->print("Must_spill ");
  if( _is_bound ) tty->print("Bound ");
  if( _msize_valid ) {
    if( _degree_valid && lo_degree() ) tty->print("Trivial ");
  }

  tty->cr();
}
#endif

// Compute score from cost and area.  Low score is best to spill.
static double raw_score( double cost, double area ) {
  return cost - (area*RegisterCostAreaRatio) * 1.52588e-5;
}

double LRG::score() const {
  // Scale _area by RegisterCostAreaRatio/64K then subtract from cost.
  // Bigger area lowers score, encourages spilling this live range.
  // Bigger cost raise score, prevents spilling this live range.
  // (Note: 1/65536 is the magic constant below; I dont trust the C optimizer
  // to turn a divide by a constant into a multiply by the reciprical).
  double score = raw_score( _cost, _area);

  // Account for area.  Basically, LRGs covering large areas are better
  // to spill because more other LRGs get freed up.
  if( _area == 0.0 )            // No area?  Then no progress to spill
    return 1e35;

  if( _was_spilled2 )           // If spilled once before, we are unlikely
    return score + 1e30;        // to make progress again.

  if( _cost >= _area*3.0 )      // Tiny area relative to cost
    return score + 1e17;        // Probably no progress to spill

  if( (_cost+_cost) >= _area*3.0 ) // Small area relative to cost
    return score + 1e10;        // Likely no progress to spill

  return score;
}

#define NUMBUCKS 3

127 128 129
// Straight out of Tarjan's union-find algorithm
uint LiveRangeMap::find_compress(uint lrg) {
  uint cur = lrg;
130
  uint next = _uf_map.at(cur);
131 132 133
  while (next != cur) { // Scan chain of equivalences
    assert( next < cur, "always union smaller");
    cur = next; // until find a fixed-point
134
    next = _uf_map.at(cur);
135 136 137 138 139
  }

  // Core of union-find algorithm: update chain of
  // equivalences to be equal to the root.
  while (lrg != next) {
140 141
    uint tmp = _uf_map.at(lrg);
    _uf_map.at_put(lrg, next);
142 143 144 145 146 147 148 149 150
    lrg = tmp;
  }
  return lrg;
}

// Reset the Union-Find map to identity
void LiveRangeMap::reset_uf_map(uint max_lrg_id) {
  _max_lrg_id= max_lrg_id;
  // Force the Union-Find mapping to be at least this large
151
  _uf_map.at_put_grow(_max_lrg_id, 0);
152 153
  // Initialize it to be the ID mapping.
  for (uint i = 0; i < _max_lrg_id; ++i) {
154
    _uf_map.at_put(i, i);
155 156 157 158 159 160 161
  }
}

// Make all Nodes map directly to their final live range; no need for
// the Union-Find mapping after this call.
void LiveRangeMap::compress_uf_map_for_nodes() {
  // For all Nodes, compress mapping
162
  uint unique = _names.length();
163
  for (uint i = 0; i < unique; ++i) {
164
    uint lrg = _names.at(i);
165 166
    uint compressed_lrg = find(lrg);
    if (lrg != compressed_lrg) {
167
      _names.at_put(i, compressed_lrg);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    }
  }
}

// Like Find above, but no path compress, so bad asymptotic behavior
uint LiveRangeMap::find_const(uint lrg) const {
  if (!lrg) {
    return lrg; // Ignore the zero LRG
  }

  // Off the end?  This happens during debugging dumps when you got
  // brand new live ranges but have not told the allocator yet.
  if (lrg >= _max_lrg_id) {
    return lrg;
  }

184
  uint next = _uf_map.at(lrg);
185 186 187
  while (next != lrg) { // Scan chain of equivalences
    assert(next < lrg, "always union smaller");
    lrg = next; // until find a fixed-point
188
    next = _uf_map.at(lrg);
189 190 191 192
  }
  return next;
}

D
duke 已提交
193 194 195 196 197 198 199
PhaseChaitin::PhaseChaitin(uint unique, PhaseCFG &cfg, Matcher &matcher)
  : PhaseRegAlloc(unique, cfg, matcher,
#ifndef PRODUCT
       print_chaitin_statistics
#else
       NULL
#endif
200
       )
201
  , _lrg_map(Thread::current()->resource_area(), unique)
202 203 204 205 206
  , _live(0)
  , _spilled_once(Thread::current()->resource_area())
  , _spilled_twice(Thread::current()->resource_area())
  , _lo_degree(0), _lo_stk_degree(0), _hi_degree(0), _simplified(0)
  , _oldphi(unique)
D
duke 已提交
207 208 209 210 211
#ifndef PRODUCT
  , _trace_spilling(TraceSpilling || C->method_has_option("TraceSpilling"))
#endif
{
  NOT_PRODUCT( Compile::TracePhase t3("ctorChaitin", &_t_ctorChaitin, TimeCompiler); )
212

213
  _high_frequency_lrg = MIN2(float(OPTO_LRG_HIGH_FREQ), _cfg.get_outer_loop_frequency());
214

D
duke 已提交
215
  // Build a list of basic blocks, sorted by frequency
216
  _blks = NEW_RESOURCE_ARRAY(Block *, _cfg.number_of_blocks());
D
duke 已提交
217 218 219 220 221
  // Experiment with sorting strategies to speed compilation
  double  cutoff = BLOCK_FREQUENCY(1.0); // Cutoff for high frequency bucket
  Block **buckets[NUMBUCKS];             // Array of buckets
  uint    buckcnt[NUMBUCKS];             // Array of bucket counters
  double  buckval[NUMBUCKS];             // Array of bucket value cutoffs
222
  for (uint i = 0; i < NUMBUCKS; i++) {
223
    buckets[i] = NEW_RESOURCE_ARRAY(Block *, _cfg.number_of_blocks());
D
duke 已提交
224 225 226 227
    buckcnt[i] = 0;
    // Bump by three orders of magnitude each time
    cutoff *= 0.001;
    buckval[i] = cutoff;
228
    for (uint j = 0; j < _cfg.number_of_blocks(); j++) {
D
duke 已提交
229 230 231 232
      buckets[i][j] = NULL;
    }
  }
  // Sort blocks into buckets
233
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
234
    for (uint j = 0; j < NUMBUCKS; j++) {
235
      if ((j == NUMBUCKS - 1) || (_cfg.get_block(i)->_freq > buckval[j])) {
D
duke 已提交
236
        // Assign block to end of list for appropriate bucket
237
        buckets[j][buckcnt[j]++] = _cfg.get_block(i);
238
        break; // kick out of inner loop
D
duke 已提交
239 240 241 242 243
      }
    }
  }
  // Dump buckets into final block array
  uint blkcnt = 0;
244 245
  for (uint i = 0; i < NUMBUCKS; i++) {
    for (uint j = 0; j < buckcnt[i]; j++) {
D
duke 已提交
246 247 248 249
      _blks[blkcnt++] = buckets[i][j];
    }
  }

250
  assert(blkcnt == _cfg.number_of_blocks(), "Block array not totally filled");
D
duke 已提交
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
// union 2 sets together.
void PhaseChaitin::Union( const Node *src_n, const Node *dst_n ) {
  uint src = _lrg_map.find(src_n);
  uint dst = _lrg_map.find(dst_n);
  assert(src, "");
  assert(dst, "");
  assert(src < _lrg_map.max_lrg_id(), "oob");
  assert(dst < _lrg_map.max_lrg_id(), "oob");
  assert(src < dst, "always union smaller");
  _lrg_map.uf_map(dst, src);
}

void PhaseChaitin::new_lrg(const Node *x, uint lrg) {
  // Make the Node->LRG mapping
  _lrg_map.extend(x->_idx,lrg);
  // Make the Union-Find mapping an identity function
  _lrg_map.uf_extend(lrg, lrg);
}


273 274 275 276 277 278 279 280 281 282 283 284 285 286
int PhaseChaitin::clone_projs(Block* b, uint idx, Node* orig, Node* copy, uint& max_lrg_id) {
  assert(b->find_node(copy) == (idx - 1), "incorrect insert index for copy kill projections");
  DEBUG_ONLY( Block* borig = _cfg.get_block_for_node(orig); )
  int found_projs = 0;
  uint cnt = orig->outcnt();
  for (uint i = 0; i < cnt; i++) {
    Node* proj = orig->raw_out(i);
    if (proj->is_MachProj()) {
      assert(proj->outcnt() == 0, "only kill projections are expected here");
      assert(_cfg.get_block_for_node(proj) == borig, "incorrect block for kill projections");
      found_projs++;
      // Copy kill projections after the cloned node
      Node* kills = proj->clone();
      kills->set_req(0, copy);
287
      b->insert_node(kills, idx++);
288 289 290
      _cfg.map_node_to_block(kills, b);
      new_lrg(kills, max_lrg_id++);
    }
291
  }
292
  return found_projs;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
}

// Renumber the live ranges to compact them.  Makes the IFG smaller.
void PhaseChaitin::compact() {
  // Current the _uf_map contains a series of short chains which are headed
  // by a self-cycle.  All the chains run from big numbers to little numbers.
  // The Find() call chases the chains & shortens them for the next Find call.
  // We are going to change this structure slightly.  Numbers above a moving
  // wave 'i' are unchanged.  Numbers below 'j' point directly to their
  // compacted live range with no further chaining.  There are no chains or
  // cycles below 'i', so the Find call no longer works.
  uint j=1;
  uint i;
  for (i = 1; i < _lrg_map.max_lrg_id(); i++) {
    uint lr = _lrg_map.uf_live_range_id(i);
    // Ignore unallocated live ranges
    if (!lr) {
      continue;
    }
    assert(lr <= i, "");
    _lrg_map.uf_map(i, ( lr == i ) ? j++ : _lrg_map.uf_live_range_id(lr));
  }
  // Now change the Node->LR mapping to reflect the compacted names
  uint unique = _lrg_map.size();
  for (i = 0; i < unique; i++) {
    uint lrg_id = _lrg_map.live_range_id(i);
    _lrg_map.map(i, _lrg_map.uf_live_range_id(lrg_id));
  }

  // Reset the Union-Find mapping
  _lrg_map.reset_uf_map(j);
}

D
duke 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
void PhaseChaitin::Register_Allocate() {

  // Above the OLD FP (and in registers) are the incoming arguments.  Stack
  // slots in this area are called "arg_slots".  Above the NEW FP (and in
  // registers) is the outgoing argument area; above that is the spill/temp
  // area.  These are all "frame_slots".  Arg_slots start at the zero
  // stack_slots and count up to the known arg_size.  Frame_slots start at
  // the stack_slot #arg_size and go up.  After allocation I map stack
  // slots to actual offsets.  Stack-slots in the arg_slot area are biased
  // by the frame_size; stack-slots in the frame_slot area are biased by 0.

  _trip_cnt = 0;
  _alternate = 0;
  _matcher._allocation_started = true;

341
  ResourceArea split_arena;     // Arena for Split local resources
D
duke 已提交
342 343 344 345 346 347 348 349
  ResourceArea live_arena;      // Arena for liveness & IFG info
  ResourceMark rm(&live_arena);

  // Need live-ness for the IFG; need the IFG for coalescing.  If the
  // liveness is JUST for coalescing, then I can get some mileage by renaming
  // all copy-related live ranges low and then using the max copy-related
  // live range as a cut-off for LIVE and the IFG.  In other words, I can
  // build a subset of LIVE and IFG just for copies.
350
  PhaseLive live(_cfg, _lrg_map.names(), &live_arena);
D
duke 已提交
351 352

  // Need IFG for coalescing and coloring
353
  PhaseIFG ifg(&live_arena);
D
duke 已提交
354 355 356 357 358 359 360 361 362 363 364
  _ifg = &ifg;

  // Come out of SSA world to the Named world.  Assign (virtual) registers to
  // Nodes.  Use the same register for all inputs and the output of PhiNodes
  // - effectively ending SSA form.  This requires either coalescing live
  // ranges or inserting copies.  For the moment, we insert "virtual copies"
  // - we pretend there is a copy prior to each Phi in predecessor blocks.
  // We will attempt to coalesce such "virtual copies" before we manifest
  // them for real.
  de_ssa();

365 366 367 368 369
#ifdef ASSERT
  // Veify the graph before RA.
  verify(&live_arena);
#endif

D
duke 已提交
370 371 372 373 374
  {
    NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
    _live = NULL;                 // Mark live as being not available
    rm.reset_to_mark();           // Reclaim working storage
    IndexSet::reset_memory(C, &live_arena);
375
    ifg.init(_lrg_map.max_lrg_id()); // Empty IFG
D
duke 已提交
376
    gather_lrg_masks( false );    // Collect LRG masks
377
    live.compute(_lrg_map.max_lrg_id()); // Compute liveness
D
duke 已提交
378 379 380 381 382 383 384 385 386
    _live = &live;                // Mark LIVE as being available
  }

  // Base pointers are currently "used" by instructions which define new
  // derived pointers.  This makes base pointers live up to the where the
  // derived pointer is made, but not beyond.  Really, they need to be live
  // across any GC point where the derived value is live.  So this code looks
  // at all the GC points, and "stretches" the live range of any base pointer
  // to the GC point.
387 388
  if (stretch_base_pointer_live_ranges(&live_arena)) {
    NOT_PRODUCT(Compile::TracePhase t3("computeLive (sbplr)", &_t_computeLive, TimeCompiler);)
D
duke 已提交
389 390 391 392
    // Since some live range stretched, I need to recompute live
    _live = NULL;
    rm.reset_to_mark();         // Reclaim working storage
    IndexSet::reset_memory(C, &live_arena);
393 394 395
    ifg.init(_lrg_map.max_lrg_id());
    gather_lrg_masks(false);
    live.compute(_lrg_map.max_lrg_id());
D
duke 已提交
396 397 398
    _live = &live;
  }
  // Create the interference graph using virtual copies
399
  build_ifg_virtual();  // Include stack slots this time
D
duke 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412

  // Aggressive (but pessimistic) copy coalescing.
  // This pass works on virtual copies.  Any virtual copies which are not
  // coalesced get manifested as actual copies
  {
    // The IFG is/was triangular.  I am 'squaring it up' so Union can run
    // faster.  Union requires a 'for all' operation which is slow on the
    // triangular adjacency matrix (quick reminder: the IFG is 'sparse' -
    // meaning I can visit all the Nodes neighbors less than a Node in time
    // O(# of neighbors), but I have to visit all the Nodes greater than a
    // given Node and search them for an instance, i.e., time O(#MaxLRG)).
    _ifg->SquareUp();

413 414
    PhaseAggressiveCoalesce coalesce(*this);
    coalesce.coalesce_driver();
D
duke 已提交
415 416 417
    // Insert un-coalesced copies.  Visit all Phis.  Where inputs to a Phi do
    // not match the Phi itself, insert a copy.
    coalesce.insert_copies(_matcher);
418 419 420
    if (C->failing()) {
      return;
    }
D
duke 已提交
421 422 423 424 425 426 427 428 429
  }

  // After aggressive coalesce, attempt a first cut at coloring.
  // To color, we need the IFG and for that we need LIVE.
  {
    NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
    _live = NULL;
    rm.reset_to_mark();           // Reclaim working storage
    IndexSet::reset_memory(C, &live_arena);
430
    ifg.init(_lrg_map.max_lrg_id());
D
duke 已提交
431
    gather_lrg_masks( true );
432
    live.compute(_lrg_map.max_lrg_id());
D
duke 已提交
433 434 435 436 437
    _live = &live;
  }

  // Build physical interference graph
  uint must_spill = 0;
438
  must_spill = build_ifg_physical(&live_arena);
D
duke 已提交
439
  // If we have a guaranteed spill, might as well spill now
440 441 442 443
  if (must_spill) {
    if(!_lrg_map.max_lrg_id()) {
      return;
    }
D
duke 已提交
444 445
    // Bail out if unique gets too large (ie - unique > MaxNodeLimit)
    C->check_node_count(10*must_spill, "out of nodes before split");
446 447 448 449 450 451
    if (C->failing()) {
      return;
    }

    uint new_max_lrg_id = Split(_lrg_map.max_lrg_id(), &split_arena);  // Split spilling LRG everywhere
    _lrg_map.set_max_lrg_id(new_max_lrg_id);
D
duke 已提交
452 453 454
    // Bail out if unique gets too large (ie - unique > MaxNodeLimit - 2*NodeLimitFudgeFactor)
    // or we failed to split
    C->check_node_count(2*NodeLimitFudgeFactor, "out of nodes after physical split");
455 456 457
    if (C->failing()) {
      return;
    }
D
duke 已提交
458

459
    NOT_PRODUCT(C->verify_graph_edges();)
D
duke 已提交
460 461 462 463 464 465 466 467

    compact();                  // Compact LRGs; return new lower max lrg

    {
      NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
      _live = NULL;
      rm.reset_to_mark();         // Reclaim working storage
      IndexSet::reset_memory(C, &live_arena);
468
      ifg.init(_lrg_map.max_lrg_id()); // Build a new interference graph
D
duke 已提交
469
      gather_lrg_masks( true );   // Collect intersect mask
470
      live.compute(_lrg_map.max_lrg_id()); // Compute LIVE
D
duke 已提交
471 472
      _live = &live;
    }
473
    build_ifg_physical(&live_arena);
D
duke 已提交
474 475 476
    _ifg->SquareUp();
    _ifg->Compute_Effective_Degree();
    // Only do conservative coalescing if requested
477
    if (OptoCoalesce) {
D
duke 已提交
478
      // Conservative (and pessimistic) copy coalescing of those spills
479
      PhaseConservativeCoalesce coalesce(*this);
D
duke 已提交
480 481
      // If max live ranges greater than cutoff, don't color the stack.
      // This cutoff can be larger than below since it is only done once.
482
      coalesce.coalesce_driver();
D
duke 已提交
483
    }
484
    _lrg_map.compress_uf_map_for_nodes();
D
duke 已提交
485 486

#ifdef ASSERT
487
    verify(&live_arena, true);
D
duke 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
#endif
  } else {
    ifg.SquareUp();
    ifg.Compute_Effective_Degree();
#ifdef ASSERT
    set_was_low();
#endif
  }

  // Prepare for Simplify & Select
  cache_lrg_info();           // Count degree of LRGs

  // Simplify the InterFerence Graph by removing LRGs of low degree.
  // LRGs of low degree are trivially colorable.
  Simplify();

  // Select colors by re-inserting LRGs back into the IFG in reverse order.
  // Return whether or not something spills.
  uint spills = Select( );

  // If we spill, split and recycle the entire thing
  while( spills ) {
    if( _trip_cnt++ > 24 ) {
      DEBUG_ONLY( dump_for_spill_split_recycle(); )
      if( _trip_cnt > 27 ) {
        C->record_method_not_compilable("failed spill-split-recycle sanity check");
        return;
      }
    }

518 519 520 521 522
    if (!_lrg_map.max_lrg_id()) {
      return;
    }
    uint new_max_lrg_id = Split(_lrg_map.max_lrg_id(), &split_arena);  // Split spilling LRG everywhere
    _lrg_map.set_max_lrg_id(new_max_lrg_id);
D
duke 已提交
523
    // Bail out if unique gets too large (ie - unique > MaxNodeLimit - 2*NodeLimitFudgeFactor)
524 525 526 527
    C->check_node_count(2 * NodeLimitFudgeFactor, "out of nodes after split");
    if (C->failing()) {
      return;
    }
D
duke 已提交
528

529
    compact(); // Compact LRGs; return new lower max lrg
D
duke 已提交
530 531 532 533 534 535 536

    // Nuke the live-ness and interference graph and LiveRanGe info
    {
      NOT_PRODUCT( Compile::TracePhase t3("computeLive", &_t_computeLive, TimeCompiler); )
      _live = NULL;
      rm.reset_to_mark();         // Reclaim working storage
      IndexSet::reset_memory(C, &live_arena);
537
      ifg.init(_lrg_map.max_lrg_id());
D
duke 已提交
538 539 540

      // Create LiveRanGe array.
      // Intersect register masks for all USEs and DEFs
541 542
      gather_lrg_masks(true);
      live.compute(_lrg_map.max_lrg_id());
D
duke 已提交
543 544
      _live = &live;
    }
545
    must_spill = build_ifg_physical(&live_arena);
D
duke 已提交
546 547 548 549
    _ifg->SquareUp();
    _ifg->Compute_Effective_Degree();

    // Only do conservative coalescing if requested
550
    if (OptoCoalesce) {
D
duke 已提交
551
      // Conservative (and pessimistic) copy coalescing
552
      PhaseConservativeCoalesce coalesce(*this);
D
duke 已提交
553
      // Check for few live ranges determines how aggressive coalesce is.
554
      coalesce.coalesce_driver();
D
duke 已提交
555
    }
556
    _lrg_map.compress_uf_map_for_nodes();
D
duke 已提交
557
#ifdef ASSERT
558
    verify(&live_arena, true);
D
duke 已提交
559 560 561 562 563 564 565 566 567
#endif
    cache_lrg_info();           // Count degree of LRGs

    // Simplify the InterFerence Graph by removing LRGs of low degree.
    // LRGs of low degree are trivially colorable.
    Simplify();

    // Select colors by re-inserting LRGs back into the IFG in reverse order.
    // Return whether or not something spills.
568
    spills = Select();
D
duke 已提交
569 570 571 572 573 574 575 576 577
  }

  // Count number of Simplify-Select trips per coloring success.
  _allocator_attempts += _trip_cnt + 1;
  _allocator_successes += 1;

  // Peephole remove copies
  post_allocate_copy_removal();

578 579 580 581 582
#ifdef ASSERT
  // Veify the graph after RA.
  verify(&live_arena);
#endif

D
duke 已提交
583 584
  // max_reg is past the largest *register* used.
  // Convert that to a frame_slot number.
585
  if (_max_reg <= _matcher._new_SP) {
D
duke 已提交
586
    _framesize = C->out_preserve_stack_slots();
587 588 589 590
  }
  else {
    _framesize = _max_reg -_matcher._new_SP;
  }
D
duke 已提交
591 592 593
  assert((int)(_matcher._new_SP+_framesize) >= (int)_matcher._out_arg_limit, "framesize must be large enough");

  // This frame must preserve the required fp alignment
594
  _framesize = round_to(_framesize, Matcher::stack_alignment_in_slots());
D
duke 已提交
595 596 597
  assert( _framesize >= 0 && _framesize <= 1000000, "sanity check" );
#ifndef PRODUCT
  _total_framesize += _framesize;
598
  if ((int)_framesize > _max_framesize) {
D
duke 已提交
599
    _max_framesize = _framesize;
600
  }
D
duke 已提交
601 602 603 604 605 606 607 608 609 610 611
#endif

  // Convert CISC spills
  fixup_spills();

  // Log regalloc results
  CompileLog* log = Compile::current()->log();
  if (log != NULL) {
    log->elem("regalloc attempts='%d' success='%d'", _trip_cnt, !C->failing());
  }

612 613 614
  if (C->failing()) {
    return;
  }
D
duke 已提交
615

616
  NOT_PRODUCT(C->verify_graph_edges();)
D
duke 已提交
617 618

  // Move important info out of the live_arena to longer lasting storage.
619 620 621 622
  alloc_node_regs(_lrg_map.size());
  for (uint i=0; i < _lrg_map.size(); i++) {
    if (_lrg_map.live_range_id(i)) { // Live range associated with Node?
      LRG &lrg = lrgs(_lrg_map.live_range_id(i));
623
      if (!lrg.alive()) {
624
        set_bad(i);
625
      } else if (lrg.num_regs() == 1) {
626 627 628
        set1(i, lrg.reg());
      } else {                  // Must be a register-set
        if (!lrg._fat_proj) {   // Must be aligned adjacent register set
D
duke 已提交
629 630
          // Live ranges record the highest register in their mask.
          // We want the low register for the AD file writer's convenience.
631 632 633 634 635 636 637 638 639 640
          OptoReg::Name hi = lrg.reg(); // Get hi register
          OptoReg::Name lo = OptoReg::add(hi, (1-lrg.num_regs())); // Find lo
          // We have to use pair [lo,lo+1] even for wide vectors because
          // the rest of code generation works only with pairs. It is safe
          // since for registers encoding only 'lo' is used.
          // Second reg from pair is used in ScheduleAndBundle on SPARC where
          // vector max size is 8 which corresponds to registers pair.
          // It is also used in BuildOopMaps but oop operations are not
          // vectorized.
          set2(i, lo);
D
duke 已提交
641 642 643 644
        } else {                // Misaligned; extract 2 bits
          OptoReg::Name hi = lrg.reg(); // Get hi register
          lrg.Remove(hi);       // Yank from mask
          int lo = lrg.mask().find_first_elem(); // Find lo
645
          set_pair(i, hi, lo);
D
duke 已提交
646 647 648 649
        }
      }
      if( lrg._is_oop ) _node_oops.set(i);
    } else {
650
      set_bad(i);
D
duke 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    }
  }

  // Done!
  _live = NULL;
  _ifg = NULL;
  C->set_indexSet_arena(NULL);  // ResourceArea is at end of scope
}

void PhaseChaitin::de_ssa() {
  // Set initial Names for all Nodes.  Most Nodes get the virtual register
  // number.  A few get the ZERO live range number.  These do not
  // get allocated, but instead rely on correct scheduling to ensure that
  // only one instance is simultaneously live at a time.
  uint lr_counter = 1;
666 667
  for( uint i = 0; i < _cfg.number_of_blocks(); i++ ) {
    Block* block = _cfg.get_block(i);
668
    uint cnt = block->number_of_nodes();
D
duke 已提交
669 670 671

    // Handle all the normal Nodes in the block
    for( uint j = 0; j < cnt; j++ ) {
672
      Node *n = block->get_node(j);
D
duke 已提交
673 674
      // Pre-color to the zero live range, or pick virtual register
      const RegMask &rm = n->out_RegMask();
675
      _lrg_map.map(n->_idx, rm.is_NotEmpty() ? lr_counter++ : 0);
D
duke 已提交
676 677
    }
  }
678

D
duke 已提交
679
  // Reset the Union-Find mapping to be identity
680
  _lrg_map.reset_uf_map(lr_counter);
D
duke 已提交
681 682 683 684 685 686 687 688
}


// Gather LiveRanGe information, including register masks.  Modification of
// cisc spillable in_RegMasks should not be done before AggressiveCoalesce.
void PhaseChaitin::gather_lrg_masks( bool after_aggressive ) {

  // Nail down the frame pointer live range
689
  uint fp_lrg = _lrg_map.live_range_id(_cfg.get_root_node()->in(1)->in(TypeFunc::FramePtr));
D
duke 已提交
690 691 692
  lrgs(fp_lrg)._cost += 1e12;   // Cost is infinite

  // For all blocks
693 694
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    Block* block = _cfg.get_block(i);
D
duke 已提交
695 696

    // For all instructions
697 698
    for (uint j = 1; j < block->number_of_nodes(); j++) {
      Node* n = block->get_node(j);
D
duke 已提交
699
      uint input_edge_start =1; // Skip control most nodes
700 701 702
      if (n->is_Mach()) {
        input_edge_start = n->as_Mach()->oper_input_base();
      }
D
duke 已提交
703 704 705
      uint idx = n->is_Copy();

      // Get virtual register number, same as LiveRanGe index
706
      uint vreg = _lrg_map.live_range_id(n);
707 708
      LRG& lrg = lrgs(vreg);
      if (vreg) {              // No vreg means un-allocable (e.g. memory)
D
duke 已提交
709 710

        // Collect has-copy bit
711
        if (idx) {
D
duke 已提交
712
          lrg._has_copy = 1;
713
          uint clidx = _lrg_map.live_range_id(n->in(idx));
714
          LRG& copy_src = lrgs(clidx);
D
duke 已提交
715 716 717 718 719 720
          copy_src._has_copy = 1;
        }

        // Check for float-vs-int live range (used in register-pressure
        // calculations)
        const Type *n_type = n->bottom_type();
721
        if (n_type->is_floatingpoint()) {
D
duke 已提交
722
          lrg._is_float = 1;
723
        }
D
duke 已提交
724 725 726 727

        // Check for twice prior spilling.  Once prior spilling might have
        // spilled 'soft', 2nd prior spill should have spilled 'hard' and
        // further spilling is unlikely to make progress.
728
        if (_spilled_once.test(n->_idx)) {
D
duke 已提交
729
          lrg._was_spilled1 = 1;
730
          if (_spilled_twice.test(n->_idx)) {
D
duke 已提交
731
            lrg._was_spilled2 = 1;
732
          }
D
duke 已提交
733 734 735 736 737 738
        }

#ifndef PRODUCT
        if (trace_spilling() && lrg._def != NULL) {
          // collect defs for MultiDef printing
          if (lrg._defs == NULL) {
739
            lrg._defs = new (_ifg->_arena) GrowableArray<Node*>(_ifg->_arena, 2, 0, NULL);
D
duke 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753
            lrg._defs->append(lrg._def);
          }
          lrg._defs->append(n);
        }
#endif

        // Check for a single def LRG; these can spill nicely
        // via rematerialization.  Flag as NULL for no def found
        // yet, or 'n' for single def or -1 for many defs.
        lrg._def = lrg._def ? NodeSentinel : n;

        // Limit result register mask to acceptable registers
        const RegMask &rm = n->out_RegMask();
        lrg.AND( rm );
754 755 756 757 758 759 760 761 762 763 764 765 766

        int ireg = n->ideal_reg();
        assert( !n->bottom_type()->isa_oop_ptr() || ireg == Op_RegP,
                "oops must be in Op_RegP's" );

        // Check for vector live range (only if vector register is used).
        // On SPARC vector uses RegD which could be misaligned so it is not
        // processes as vector in RA.
        if (RegMask::is_vector(ireg))
          lrg._is_vector = 1;
        assert(n_type->isa_vect() == NULL || lrg._is_vector || ireg == Op_RegD,
               "vector must be in vector registers");

D
duke 已提交
767 768
        // Check for bound register masks
        const RegMask &lrgmask = lrg.mask();
769
        if (lrgmask.is_bound(ireg)) {
D
duke 已提交
770
          lrg._is_bound = 1;
771
        }
D
duke 已提交
772 773

        // Check for maximum frequency value
774 775 776
        if (lrg._maxfreq < block->_freq) {
          lrg._maxfreq = block->_freq;
        }
D
duke 已提交
777 778 779

        // Check for oop-iness, or long/double
        // Check for multi-kill projection
780
        switch (ireg) {
D
duke 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
        case MachProjNode::fat_proj:
          // Fat projections have size equal to number of registers killed
          lrg.set_num_regs(rm.Size());
          lrg.set_reg_pressure(lrg.num_regs());
          lrg._fat_proj = 1;
          lrg._is_bound = 1;
          break;
        case Op_RegP:
#ifdef _LP64
          lrg.set_num_regs(2);  // Size is 2 stack words
#else
          lrg.set_num_regs(1);  // Size is 1 stack word
#endif
          // Register pressure is tracked relative to the maximum values
          // suggested for that platform, INTPRESSURE and FLOATPRESSURE,
          // and relative to other types which compete for the same regs.
          //
          // The following table contains suggested values based on the
          // architectures as defined in each .ad file.
          // INTPRESSURE and FLOATPRESSURE may be tuned differently for
          // compile-speed or performance.
          // Note1:
          // SPARC and SPARCV9 reg_pressures are at 2 instead of 1
          // since .ad registers are defined as high and low halves.
          // These reg_pressure values remain compatible with the code
          // in is_high_pressure() which relates get_invalid_mask_size(),
          // Block::_reg_pressure and INTPRESSURE, FLOATPRESSURE.
          // Note2:
          // SPARC -d32 has 24 registers available for integral values,
          // but only 10 of these are safe for 64-bit longs.
          // Using set_reg_pressure(2) for both int and long means
          // the allocator will believe it can fit 26 longs into
          // registers.  Using 2 for longs and 1 for ints means the
          // allocator will attempt to put 52 integers into registers.
          // The settings below limit this problem to methods with
          // many long values which are being run on 32-bit SPARC.
          //
          // ------------------- reg_pressure --------------------
          // Each entry is reg_pressure_per_value,number_of_regs
          //         RegL  RegI  RegFlags   RegF RegD    INTPRESSURE  FLOATPRESSURE
          // IA32     2     1     1          1    1          6           6
          // IA64     1     1     1          1    1         50          41
          // SPARC    2     2     2          2    2         48 (24)     52 (26)
          // SPARCV9  2     2     2          2    2         48 (24)     52 (26)
          // AMD64    1     1     1          1    1         14          15
          // -----------------------------------------------------
#if defined(SPARC)
          lrg.set_reg_pressure(2);  // use for v9 as well
#else
          lrg.set_reg_pressure(1);  // normally one value per register
#endif
          if( n_type->isa_oop_ptr() ) {
            lrg._is_oop = 1;
          }
          break;
        case Op_RegL:           // Check for long or double
        case Op_RegD:
          lrg.set_num_regs(2);
          // Define platform specific register pressure
840
#if defined(SPARC) || defined(ARM)
D
duke 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
          lrg.set_reg_pressure(2);
#elif defined(IA32)
          if( ireg == Op_RegL ) {
            lrg.set_reg_pressure(2);
          } else {
            lrg.set_reg_pressure(1);
          }
#else
          lrg.set_reg_pressure(1);  // normally one value per register
#endif
          // If this def of a double forces a mis-aligned double,
          // flag as '_fat_proj' - really flag as allowing misalignment
          // AND changes how we count interferences.  A mis-aligned
          // double can interfere with TWO aligned pairs, or effectively
          // FOUR registers!
856
          if (rm.is_misaligned_pair()) {
D
duke 已提交
857 858 859 860 861 862
            lrg._fat_proj = 1;
            lrg._is_bound = 1;
          }
          break;
        case Op_RegF:
        case Op_RegI:
863
        case Op_RegN:
D
duke 已提交
864 865 866 867 868 869 870 871 872
        case Op_RegFlags:
        case 0:                 // not an ideal register
          lrg.set_num_regs(1);
#ifdef SPARC
          lrg.set_reg_pressure(2);
#else
          lrg.set_reg_pressure(1);
#endif
          break;
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
        case Op_VecS:
          assert(Matcher::vector_size_supported(T_BYTE,4), "sanity");
          assert(RegMask::num_registers(Op_VecS) == RegMask::SlotsPerVecS, "sanity");
          lrg.set_num_regs(RegMask::SlotsPerVecS);
          lrg.set_reg_pressure(1);
          break;
        case Op_VecD:
          assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecD), "sanity");
          assert(RegMask::num_registers(Op_VecD) == RegMask::SlotsPerVecD, "sanity");
          assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecD), "vector should be aligned");
          lrg.set_num_regs(RegMask::SlotsPerVecD);
          lrg.set_reg_pressure(1);
          break;
        case Op_VecX:
          assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecX), "sanity");
          assert(RegMask::num_registers(Op_VecX) == RegMask::SlotsPerVecX, "sanity");
          assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecX), "vector should be aligned");
          lrg.set_num_regs(RegMask::SlotsPerVecX);
          lrg.set_reg_pressure(1);
          break;
        case Op_VecY:
          assert(Matcher::vector_size_supported(T_FLOAT,RegMask::SlotsPerVecY), "sanity");
          assert(RegMask::num_registers(Op_VecY) == RegMask::SlotsPerVecY, "sanity");
          assert(lrgmask.is_aligned_sets(RegMask::SlotsPerVecY), "vector should be aligned");
          lrg.set_num_regs(RegMask::SlotsPerVecY);
          lrg.set_reg_pressure(1);
          break;
D
duke 已提交
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
        default:
          ShouldNotReachHere();
        }
      }

      // Now do the same for inputs
      uint cnt = n->req();
      // Setup for CISC SPILLING
      uint inp = (uint)AdlcVMDeps::Not_cisc_spillable;
      if( UseCISCSpill && after_aggressive ) {
        inp = n->cisc_operand();
        if( inp != (uint)AdlcVMDeps::Not_cisc_spillable )
          // Convert operand number to edge index number
          inp = n->as_Mach()->operand_index(inp);
      }
      // Prepare register mask for each input
      for( uint k = input_edge_start; k < cnt; k++ ) {
917 918 919 920
        uint vreg = _lrg_map.live_range_id(n->in(k));
        if (!vreg) {
          continue;
        }
D
duke 已提交
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

        // If this instruction is CISC Spillable, add the flags
        // bit to its appropriate input
        if( UseCISCSpill && after_aggressive && inp == k ) {
#ifndef PRODUCT
          if( TraceCISCSpill ) {
            tty->print("  use_cisc_RegMask: ");
            n->dump();
          }
#endif
          n->as_Mach()->use_cisc_RegMask();
        }

        LRG &lrg = lrgs(vreg);
        // // Testing for floating point code shape
        // Node *test = n->in(k);
        // if( test->is_Mach() ) {
        //   MachNode *m = test->as_Mach();
        //   int  op = m->ideal_Opcode();
        //   if (n->is_Call() && (op == Op_AddF || op == Op_MulF) ) {
        //     int zzz = 1;
        //   }
        // }

        // Limit result register mask to acceptable registers.
        // Do not limit registers from uncommon uses before
        // AggressiveCoalesce.  This effectively pre-virtual-splits
        // around uncommon uses of common defs.
        const RegMask &rm = n->in_RegMask(k);
950
        if (!after_aggressive && _cfg.get_block_for_node(n->in(k))->_freq > 1000 * block->_freq) {
D
duke 已提交
951 952 953 954 955 956 957
          // Since we are BEFORE aggressive coalesce, leave the register
          // mask untrimmed by the call.  This encourages more coalescing.
          // Later, AFTER aggressive, this live range will have to spill
          // but the spiller handles slow-path calls very nicely.
        } else {
          lrg.AND( rm );
        }
958

D
duke 已提交
959 960
        // Check for bound register masks
        const RegMask &lrgmask = lrg.mask();
961 962 963 964 965 966
        int kreg = n->in(k)->ideal_reg();
        bool is_vect = RegMask::is_vector(kreg);
        assert(n->in(k)->bottom_type()->isa_vect() == NULL ||
               is_vect || kreg == Op_RegD,
               "vector must be in vector registers");
        if (lrgmask.is_bound(kreg))
D
duke 已提交
967
          lrg._is_bound = 1;
968

D
duke 已提交
969 970 971 972 973
        // If this use of a double forces a mis-aligned double,
        // flag as '_fat_proj' - really flag as allowing misalignment
        // AND changes how we count interferences.  A mis-aligned
        // double can interfere with TWO aligned pairs, or effectively
        // FOUR registers!
974 975 976 977 978 979 980 981
#ifdef ASSERT
        if (is_vect) {
          assert(lrgmask.is_aligned_sets(lrg.num_regs()), "vector should be aligned");
          assert(!lrg._fat_proj, "sanity");
          assert(RegMask::num_registers(kreg) == lrg.num_regs(), "sanity");
        }
#endif
        if (!is_vect && lrg.num_regs() == 2 && !lrg._fat_proj && rm.is_misaligned_pair()) {
D
duke 已提交
982 983 984 985 986
          lrg._fat_proj = 1;
          lrg._is_bound = 1;
        }
        // if the LRG is an unaligned pair, we will have to spill
        // so clear the LRG's register mask if it is not already spilled
987 988 989
        if (!is_vect && !n->is_SpillCopy() &&
            (lrg._def == NULL || lrg.is_multidef() || !lrg._def->is_SpillCopy()) &&
            lrgmask.is_misaligned_pair()) {
D
duke 已提交
990 991 992 993
          lrg.Clear();
        }

        // Check for maximum frequency value
994 995 996
        if (lrg._maxfreq < block->_freq) {
          lrg._maxfreq = block->_freq;
        }
D
duke 已提交
997 998 999 1000 1001 1002

      } // End for all allocated inputs
    } // end for all instructions
  } // end for all blocks

  // Final per-liverange setup
1003
  for (uint i2 = 0; i2 < _lrg_map.max_lrg_id(); i2++) {
D
duke 已提交
1004
    LRG &lrg = lrgs(i2);
1005 1006 1007 1008
    assert(!lrg._is_vector || !lrg._fat_proj, "sanity");
    if (lrg.num_regs() > 1 && !lrg._fat_proj) {
      lrg.clear_to_sets();
    }
D
duke 已提交
1009
    lrg.compute_set_mask_size();
1010
    if (lrg.not_free()) {      // Handle case where we lose from the start
D
duke 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
      lrg.set_reg(OptoReg::Name(LRG::SPILL_REG));
      lrg._direct_conflict = 1;
    }
    lrg.set_degree(0);          // no neighbors in IFG yet
  }
}

// Set the was-lo-degree bit.  Conservative coalescing should not change the
// colorability of the graph.  If any live range was of low-degree before
// coalescing, it should Simplify.  This call sets the was-lo-degree bit.
// The bit is checked in Simplify.
void PhaseChaitin::set_was_low() {
#ifdef ASSERT
1024
  for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
D
duke 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    int size = lrgs(i).num_regs();
    uint old_was_lo = lrgs(i)._was_lo;
    lrgs(i)._was_lo = 0;
    if( lrgs(i).lo_degree() ) {
      lrgs(i)._was_lo = 1;      // Trivially of low degree
    } else {                    // Else check the Brigg's assertion
      // Brigg's observation is that the lo-degree neighbors of a
      // hi-degree live range will not interfere with the color choices
      // of said hi-degree live range.  The Simplify reverse-stack-coloring
      // order takes care of the details.  Hence you do not have to count
      // low-degree neighbors when determining if this guy colors.
      int briggs_degree = 0;
      IndexSet *s = _ifg->neighbors(i);
      IndexSetIterator elements(s);
      uint lidx;
      while((lidx = elements.next()) != 0) {
        if( !lrgs(lidx).lo_degree() )
          briggs_degree += MAX2(size,lrgs(lidx).num_regs());
      }
      if( briggs_degree < lrgs(i).degrees_of_freedom() )
        lrgs(i)._was_lo = 1;    // Low degree via the briggs assertion
    }
    assert(old_was_lo <= lrgs(i)._was_lo, "_was_lo may not decrease");
  }
#endif
}

#define REGISTER_CONSTRAINED 16

// Compute cost/area ratio, in case we spill.  Build the lo-degree list.
void PhaseChaitin::cache_lrg_info( ) {

1057
  for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
D
duke 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
    LRG &lrg = lrgs(i);

    // Check for being of low degree: means we can be trivially colored.
    // Low degree, dead or must-spill guys just get to simplify right away
    if( lrg.lo_degree() ||
       !lrg.alive() ||
        lrg._must_spill ) {
      // Split low degree list into those guys that must get a
      // register and those that can go to register or stack.
      // The idea is LRGs that can go register or stack color first when
      // they have a good chance of getting a register.  The register-only
      // lo-degree live ranges always get a register.
      OptoReg::Name hi_reg = lrg.mask().find_last_elem();
      if( OptoReg::is_stack(hi_reg)) { // Can go to stack?
        lrg._next = _lo_stk_degree;
        _lo_stk_degree = i;
      } else {
        lrg._next = _lo_degree;
        _lo_degree = i;
      }
    } else {                    // Else high degree
      lrgs(_hi_degree)._prev = i;
      lrg._next = _hi_degree;
      lrg._prev = 0;
      _hi_degree = i;
    }
  }
}

// Simplify the IFG by removing LRGs of low degree that have NO copies
void PhaseChaitin::Pre_Simplify( ) {

  // Warm up the lo-degree no-copy list
  int lo_no_copy = 0;
1092 1093
  for (uint i = 1; i < _lrg_map.max_lrg_id(); i++) {
    if ((lrgs(i).lo_degree() && !lrgs(i)._has_copy) ||
D
duke 已提交
1094
        !lrgs(i).alive() ||
1095
        lrgs(i)._must_spill) {
D
duke 已提交
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
      lrgs(i)._next = lo_no_copy;
      lo_no_copy = i;
    }
  }

  while( lo_no_copy ) {
    uint lo = lo_no_copy;
    lo_no_copy = lrgs(lo)._next;
    int size = lrgs(lo).num_regs();

    // Put the simplified guy on the simplified list.
    lrgs(lo)._next = _simplified;
    _simplified = lo;

    // Yank this guy from the IFG.
    IndexSet *adj = _ifg->remove_node( lo );

    // If any neighbors' degrees fall below their number of
    // allowed registers, then put that neighbor on the low degree
    // list.  Note that 'degree' can only fall and 'numregs' is
    // unchanged by this action.  Thus the two are equal at most once,
    // so LRGs hit the lo-degree worklists at most once.
    IndexSetIterator elements(adj);
    uint neighbor;
    while ((neighbor = elements.next()) != 0) {
      LRG *n = &lrgs(neighbor);
      assert( _ifg->effective_degree(neighbor) == n->degree(), "" );

      // Check for just becoming of-low-degree
      if( n->just_lo_degree() && !n->_has_copy ) {
        assert(!(*_ifg->_yanked)[neighbor],"Cannot move to lo degree twice");
        // Put on lo-degree list
        n->_next = lo_no_copy;
        lo_no_copy = neighbor;
      }
    }
  } // End of while lo-degree no_copy worklist not empty

  // No more lo-degree no-copy live ranges to simplify
}

// Simplify the IFG by removing LRGs of low degree.
void PhaseChaitin::Simplify( ) {

  while( 1 ) {                  // Repeat till simplified it all
    // May want to explore simplifying lo_degree before _lo_stk_degree.
    // This might result in more spills coloring into registers during
    // Select().
    while( _lo_degree || _lo_stk_degree ) {
      // If possible, pull from lo_stk first
      uint lo;
      if( _lo_degree ) {
        lo = _lo_degree;
        _lo_degree = lrgs(lo)._next;
      } else {
        lo = _lo_stk_degree;
        _lo_stk_degree = lrgs(lo)._next;
      }

      // Put the simplified guy on the simplified list.
      lrgs(lo)._next = _simplified;
      _simplified = lo;
      // If this guy is "at risk" then mark his current neighbors
      if( lrgs(lo)._at_risk ) {
        IndexSetIterator elements(_ifg->neighbors(lo));
        uint datum;
        while ((datum = elements.next()) != 0) {
          lrgs(datum)._risk_bias = lo;
        }
      }

      // Yank this guy from the IFG.
      IndexSet *adj = _ifg->remove_node( lo );

      // If any neighbors' degrees fall below their number of
      // allowed registers, then put that neighbor on the low degree
      // list.  Note that 'degree' can only fall and 'numregs' is
      // unchanged by this action.  Thus the two are equal at most once,
      // so LRGs hit the lo-degree worklist at most once.
      IndexSetIterator elements(adj);
      uint neighbor;
      while ((neighbor = elements.next()) != 0) {
        LRG *n = &lrgs(neighbor);
#ifdef ASSERT
1180
        if( VerifyOpto || VerifyRegisterAllocator ) {
D
duke 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
          assert( _ifg->effective_degree(neighbor) == n->degree(), "" );
        }
#endif

        // Check for just becoming of-low-degree just counting registers.
        // _must_spill live ranges are already on the low degree list.
        if( n->just_lo_degree() && !n->_must_spill ) {
          assert(!(*_ifg->_yanked)[neighbor],"Cannot move to lo degree twice");
          // Pull from hi-degree list
          uint prev = n->_prev;
          uint next = n->_next;
          if( prev ) lrgs(prev)._next = next;
          else _hi_degree = next;
          lrgs(next)._prev = prev;
          n->_next = _lo_degree;
          _lo_degree = neighbor;
        }
      }
    } // End of while lo-degree/lo_stk_degree worklist not empty

    // Check for got everything: is hi-degree list empty?
    if( !_hi_degree ) break;

    // Time to pick a potential spill guy
    uint lo_score = _hi_degree;
    double score = lrgs(lo_score).score();
    double area = lrgs(lo_score)._area;
1208 1209
    double cost = lrgs(lo_score)._cost;
    bool bound = lrgs(lo_score)._is_bound;
D
duke 已提交
1210 1211 1212

    // Find cheapest guy
    debug_only( int lo_no_simplify=0; );
1213
    for( uint i = _hi_degree; i; i = lrgs(i)._next ) {
D
duke 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
      assert( !(*_ifg->_yanked)[i], "" );
      // It's just vaguely possible to move hi-degree to lo-degree without
      // going through a just-lo-degree stage: If you remove a double from
      // a float live range it's degree will drop by 2 and you can skip the
      // just-lo-degree stage.  It's very rare (shows up after 5000+ methods
      // in -Xcomp of Java2Demo).  So just choose this guy to simplify next.
      if( lrgs(i).lo_degree() ) {
        lo_score = i;
        break;
      }
      debug_only( if( lrgs(i)._was_lo ) lo_no_simplify=i; );
      double iscore = lrgs(i).score();
      double iarea = lrgs(i)._area;
1227 1228
      double icost = lrgs(i)._cost;
      bool ibound = lrgs(i)._is_bound;
D
duke 已提交
1229 1230 1231 1232 1233 1234

      // Compare cost/area of i vs cost/area of lo_score.  Smaller cost/area
      // wins.  Ties happen because all live ranges in question have spilled
      // a few times before and the spill-score adds a huge number which
      // washes out the low order bits.  We are choosing the lesser of 2
      // evils; in this case pick largest area to spill.
1235 1236 1237 1238
      // Ties also happen when live ranges are defined and used only inside
      // one block. In which case their area is 0 and score set to max.
      // In such case choose bound live range over unbound to free registers
      // or with smaller cost to spill.
D
duke 已提交
1239
      if( iscore < score ||
1240 1241 1242
          (iscore == score && iarea > area && lrgs(lo_score)._was_spilled2) ||
          (iscore == score && iarea == area &&
           ( (ibound && !bound) || ibound == bound && (icost < cost) )) ) {
D
duke 已提交
1243 1244 1245
        lo_score = i;
        score = iscore;
        area = iarea;
1246 1247
        cost = icost;
        bound = ibound;
D
duke 已提交
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
      }
    }
    LRG *lo_lrg = &lrgs(lo_score);
    // The live range we choose for spilling is either hi-degree, or very
    // rarely it can be low-degree.  If we choose a hi-degree live range
    // there better not be any lo-degree choices.
    assert( lo_lrg->lo_degree() || !lo_no_simplify, "Live range was lo-degree before coalesce; should simplify" );

    // Pull from hi-degree list
    uint prev = lo_lrg->_prev;
    uint next = lo_lrg->_next;
    if( prev ) lrgs(prev)._next = next;
    else _hi_degree = next;
    lrgs(next)._prev = prev;
    // Jam him on the lo-degree list, despite his high degree.
    // Maybe he'll get a color, and maybe he'll spill.
    // Only Select() will know.
    lrgs(lo_score)._at_risk = true;
    _lo_degree = lo_score;
    lo_lrg->_next = 0;

  } // End of while not simplified everything

}

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
// Is 'reg' register legal for 'lrg'?
static bool is_legal_reg(LRG &lrg, OptoReg::Name reg, int chunk) {
  if (reg >= chunk && reg < (chunk + RegMask::CHUNK_SIZE) &&
      lrg.mask().Member(OptoReg::add(reg,-chunk))) {
    // RA uses OptoReg which represent the highest element of a registers set.
    // For example, vectorX (128bit) on x86 uses [XMM,XMMb,XMMc,XMMd] set
    // in which XMMd is used by RA to represent such vectors. A double value
    // uses [XMM,XMMb] pairs and XMMb is used by RA for it.
    // The register mask uses largest bits set of overlapping register sets.
    // On x86 with AVX it uses 8 bits for each XMM registers set.
    //
    // The 'lrg' already has cleared-to-set register mask (done in Select()
    // before calling choose_color()). Passing mask.Member(reg) check above
    // indicates that the size (num_regs) of 'reg' set is less or equal to
    // 'lrg' set size.
    // For set size 1 any register which is member of 'lrg' mask is legal.
    if (lrg.num_regs()==1)
      return true;
    // For larger sets only an aligned register with the same set size is legal.
    int mask = lrg.num_regs()-1;
    if ((reg&mask) == mask)
      return true;
  }
  return false;
}

D
duke 已提交
1299 1300 1301 1302
// Choose a color using the biasing heuristic
OptoReg::Name PhaseChaitin::bias_color( LRG &lrg, int chunk ) {

  // Check for "at_risk" LRG's
1303
  uint risk_lrg = _lrg_map.find(lrg._risk_bias);
D
duke 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  if( risk_lrg != 0 ) {
    // Walk the colored neighbors of the "at_risk" candidate
    // Choose a color which is both legal and already taken by a neighbor
    // of the "at_risk" candidate in order to improve the chances of the
    // "at_risk" candidate of coloring
    IndexSetIterator elements(_ifg->neighbors(risk_lrg));
    uint datum;
    while ((datum = elements.next()) != 0) {
      OptoReg::Name reg = lrgs(datum).reg();
      // If this LRG's register is legal for us, choose it
1314
      if (is_legal_reg(lrg, reg, chunk))
D
duke 已提交
1315 1316 1317 1318
        return reg;
    }
  }

1319
  uint copy_lrg = _lrg_map.find(lrg._copy_bias);
D
duke 已提交
1320 1321 1322 1323 1324
  if( copy_lrg != 0 ) {
    // If he has a color,
    if( !(*(_ifg->_yanked))[copy_lrg] ) {
      OptoReg::Name reg = lrgs(copy_lrg).reg();
      //  And it is legal for you,
1325
      if (is_legal_reg(lrg, reg, chunk))
D
duke 已提交
1326 1327 1328 1329 1330
        return reg;
    } else if( chunk == 0 ) {
      // Choose a color which is legal for him
      RegMask tempmask = lrg.mask();
      tempmask.AND(lrgs(copy_lrg).mask());
1331 1332 1333
      tempmask.clear_to_sets(lrg.num_regs());
      OptoReg::Name reg = tempmask.find_first_set(lrg.num_regs());
      if (OptoReg::is_valid(reg))
D
duke 已提交
1334 1335 1336 1337 1338
        return reg;
    }
  }

  // If no bias info exists, just go with the register selection ordering
1339 1340 1341
  if (lrg._is_vector || lrg.num_regs() == 2) {
    // Find an aligned set
    return OptoReg::add(lrg.mask().find_first_set(lrg.num_regs()),chunk);
D
duke 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  }

  // CNC - Fun hack.  Alternate 1st and 2nd selection.  Enables post-allocate
  // copy removal to remove many more copies, by preventing a just-assigned
  // register from being repeatedly assigned.
  OptoReg::Name reg = lrg.mask().find_first_elem();
  if( (++_alternate & 1) && OptoReg::is_valid(reg) ) {
    // This 'Remove; find; Insert' idiom is an expensive way to find the
    // SECOND element in the mask.
    lrg.Remove(reg);
    OptoReg::Name reg2 = lrg.mask().find_first_elem();
    lrg.Insert(reg);
    if( OptoReg::is_reg(reg2))
      reg = reg2;
  }
  return OptoReg::add( reg, chunk );
}

// Choose a color in the current chunk
OptoReg::Name PhaseChaitin::choose_color( LRG &lrg, int chunk ) {
  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)");
  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)");

  if( lrg.num_regs() == 1 ||    // Common Case
      !lrg._fat_proj )          // Aligned+adjacent pairs ok
    // Use a heuristic to "bias" the color choice
    return bias_color(lrg, chunk);

1370
  assert(!lrg._is_vector, "should be not vector here" );
D
duke 已提交
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
  assert( lrg.num_regs() >= 2, "dead live ranges do not color" );

  // Fat-proj case or misaligned double argument.
  assert(lrg.compute_mask_size() == lrg.num_regs() ||
         lrg.num_regs() == 2,"fat projs exactly color" );
  assert( !chunk, "always color in 1st chunk" );
  // Return the highest element in the set.
  return lrg.mask().find_last_elem();
}

// Select colors by re-inserting LRGs back into the IFG.  LRGs are re-inserted
// in reverse order of removal.  As long as nothing of hi-degree was yanked,
// everything going back is guaranteed a color.  Select that color.  If some
// hi-degree LRG cannot get a color then we record that we must spill.
uint PhaseChaitin::Select( ) {
  uint spill_reg = LRG::SPILL_REG;
  _max_reg = OptoReg::Name(0);  // Past max register used
  while( _simplified ) {
    // Pull next LRG from the simplified list - in reverse order of removal
    uint lidx = _simplified;
    LRG *lrg = &lrgs(lidx);
    _simplified = lrg->_next;


#ifndef PRODUCT
    if (trace_spilling()) {
      ttyLocker ttyl;
      tty->print_cr("L%d selecting degree %d degrees_of_freedom %d", lidx, lrg->degree(),
                    lrg->degrees_of_freedom());
      lrg->dump();
    }
#endif

    // Re-insert into the IFG
    _ifg->re_insert(lidx);
    if( !lrg->alive() ) continue;
    // capture allstackedness flag before mask is hacked
    const int is_allstack = lrg->mask().is_AllStack();

    // Yeah, yeah, yeah, I know, I know.  I can refactor this
    // to avoid the GOTO, although the refactored code will not
    // be much clearer.  We arrive here IFF we have a stack-based
    // live range that cannot color in the current chunk, and it
    // has to move into the next free stack chunk.
    int chunk = 0;              // Current chunk is first chunk
    retry_next_chunk:

    // Remove neighbor colors
    IndexSet *s = _ifg->neighbors(lidx);

    debug_only(RegMask orig_mask = lrg->mask();)
    IndexSetIterator elements(s);
    uint neighbor;
    while ((neighbor = elements.next()) != 0) {
      // Note that neighbor might be a spill_reg.  In this case, exclusion
      // of its color will be a no-op, since the spill_reg chunk is in outer
      // space.  Also, if neighbor is in a different chunk, this exclusion
      // will be a no-op.  (Later on, if lrg runs out of possible colors in
      // its chunk, a new chunk of color may be tried, in which case
      // examination of neighbors is started again, at retry_next_chunk.)
      LRG &nlrg = lrgs(neighbor);
      OptoReg::Name nreg = nlrg.reg();
      // Only subtract masks in the same chunk
      if( nreg >= chunk && nreg < chunk + RegMask::CHUNK_SIZE ) {
#ifndef PRODUCT
        uint size = lrg->mask().Size();
        RegMask rm = lrg->mask();
#endif
        lrg->SUBTRACT(nlrg.mask());
#ifndef PRODUCT
        if (trace_spilling() && lrg->mask().Size() != size) {
          ttyLocker ttyl;
          tty->print("L%d ", lidx);
          rm.dump();
          tty->print(" intersected L%d ", neighbor);
          nlrg.mask().dump();
          tty->print(" removed ");
          rm.SUBTRACT(lrg->mask());
          rm.dump();
          tty->print(" leaving ");
          lrg->mask().dump();
          tty->cr();
        }
#endif
      }
    }
    //assert(is_allstack == lrg->mask().is_AllStack(), "nbrs must not change AllStackedness");
    // Aligned pairs need aligned masks
1459 1460 1461 1462
    assert(!lrg->_is_vector || !lrg->_fat_proj, "sanity");
    if (lrg->num_regs() > 1 && !lrg->_fat_proj) {
      lrg->clear_to_sets();
    }
D
duke 已提交
1463 1464 1465 1466 1467

    // Check if a color is available and if so pick the color
    OptoReg::Name reg = choose_color( *lrg, chunk );
#ifdef SPARC
    debug_only(lrg->compute_set_mask_size());
1468
    assert(lrg->num_regs() < 2 || lrg->is_bound() || is_even(reg-1), "allocate all doubles aligned");
D
duke 已提交
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
#endif

    //---------------
    // If we fail to color and the AllStack flag is set, trigger
    // a chunk-rollover event
    if(!OptoReg::is_valid(OptoReg::add(reg,-chunk)) && is_allstack) {
      // Bump register mask up to next stack chunk
      chunk += RegMask::CHUNK_SIZE;
      lrg->Set_All();

      goto retry_next_chunk;
    }

    //---------------
    // Did we get a color?
    else if( OptoReg::is_valid(reg)) {
#ifndef PRODUCT
      RegMask avail_rm = lrg->mask();
#endif

      // Record selected register
      lrg->set_reg(reg);

      if( reg >= _max_reg )     // Compute max register limit
        _max_reg = OptoReg::add(reg,1);
      // Fold reg back into normal space
      reg = OptoReg::add(reg,-chunk);

      // If the live range is not bound, then we actually had some choices
      // to make.  In this case, the mask has more bits in it than the colors
T
twisti 已提交
1499
      // chosen.  Restrict the mask to just what was picked.
1500 1501 1502 1503
      int n_regs = lrg->num_regs();
      assert(!lrg->_is_vector || !lrg->_fat_proj, "sanity");
      if (n_regs == 1 || !lrg->_fat_proj) {
        assert(!lrg->_is_vector || n_regs <= RegMask::SlotsPerVecY, "sanity");
D
duke 已提交
1504 1505
        lrg->Clear();           // Clear the mask
        lrg->Insert(reg);       // Set regmask to match selected reg
1506 1507 1508 1509
        // For vectors and pairs, also insert the low bit of the pair
        for (int i = 1; i < n_regs; i++)
          lrg->Insert(OptoReg::add(reg,-i));
        lrg->set_mask_size(n_regs);
D
duke 已提交
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
      } else {                  // Else fatproj
        // mask must be equal to fatproj bits, by definition
      }
#ifndef PRODUCT
      if (trace_spilling()) {
        ttyLocker ttyl;
        tty->print("L%d selected ", lidx);
        lrg->mask().dump();
        tty->print(" from ");
        avail_rm.dump();
        tty->cr();
      }
#endif
      // Note that reg is the highest-numbered register in the newly-bound mask.
    } // end color available case

    //---------------
    // Live range is live and no colors available
    else {
      assert( lrg->alive(), "" );
1530
      assert( !lrg->_fat_proj || lrg->is_multidef() ||
D
duke 已提交
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
              lrg->_def->outcnt() > 0, "fat_proj cannot spill");
      assert( !orig_mask.is_AllStack(), "All Stack does not spill" );

      // Assign the special spillreg register
      lrg->set_reg(OptoReg::Name(spill_reg++));
      // Do not empty the regmask; leave mask_size lying around
      // for use during Spilling
#ifndef PRODUCT
      if( trace_spilling() ) {
        ttyLocker ttyl;
        tty->print("L%d spilling with neighbors: ", lidx);
        s->dump();
        debug_only(tty->print(" original mask: "));
        debug_only(orig_mask.dump());
        dump_lrg(lidx);
      }
#endif
    } // end spill case

  }

  return spill_reg-LRG::SPILL_REG;      // Return number of spills
}

// Copy 'was_spilled'-edness from the source Node to the dst Node.
void PhaseChaitin::copy_was_spilled( Node *src, Node *dst ) {
  if( _spilled_once.test(src->_idx) ) {
    _spilled_once.set(dst->_idx);
1559
    lrgs(_lrg_map.find(dst))._was_spilled1 = 1;
D
duke 已提交
1560 1561
    if( _spilled_twice.test(src->_idx) ) {
      _spilled_twice.set(dst->_idx);
1562
      lrgs(_lrg_map.find(dst))._was_spilled2 = 1;
D
duke 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
    }
  }
}

// Set the 'spilled_once' or 'spilled_twice' flag on a node.
void PhaseChaitin::set_was_spilled( Node *n ) {
  if( _spilled_once.test_set(n->_idx) )
    _spilled_twice.set(n->_idx);
}

// Convert Ideal spill instructions into proper FramePtr + offset Loads and
// Stores.  Use-def chains are NOT preserved, but Node->LRG->reg maps are.
void PhaseChaitin::fixup_spills() {
  // This function does only cisc spill work.
  if( !UseCISCSpill ) return;

  NOT_PRODUCT( Compile::TracePhase t3("fixupSpills", &_t_fixupSpills, TimeCompiler); )

  // Grab the Frame Pointer
1582
  Node *fp = _cfg.get_root_block()->head()->in(1)->in(TypeFunc::FramePtr);
D
duke 已提交
1583 1584

  // For all blocks
1585 1586
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    Block* block = _cfg.get_block(i);
D
duke 已提交
1587 1588

    // For all instructions in block
1589 1590
    uint last_inst = block->end_idx();
    for (uint j = 1; j <= last_inst; j++) {
1591
      Node* n = block->get_node(j);
D
duke 已提交
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604

      // Dead instruction???
      assert( n->outcnt() != 0 ||// Nothing dead after post alloc
              C->top() == n ||  // Or the random TOP node
              n->is_Proj(),     // Or a fat-proj kill node
              "No dead instructions after post-alloc" );

      int inp = n->cisc_operand();
      if( inp != AdlcVMDeps::Not_cisc_spillable ) {
        // Convert operand number to edge index number
        MachNode *mach = n->as_Mach();
        inp = mach->operand_index(inp);
        Node *src = n->in(inp);   // Value to load or store
1605
        LRG &lrg_cisc = lrgs(_lrg_map.find_const(src));
D
duke 已提交
1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627
        OptoReg::Name src_reg = lrg_cisc.reg();
        // Doubles record the HIGH register of an adjacent pair.
        src_reg = OptoReg::add(src_reg,1-lrg_cisc.num_regs());
        if( OptoReg::is_stack(src_reg) ) { // If input is on stack
          // This is a CISC Spill, get stack offset and construct new node
#ifndef PRODUCT
          if( TraceCISCSpill ) {
            tty->print("    reg-instr:  ");
            n->dump();
          }
#endif
          int stk_offset = reg2offset(src_reg);
          // Bailout if we might exceed node limit when spilling this instruction
          C->check_node_count(0, "out of nodes fixing spills");
          if (C->failing())  return;
          // Transform node
          MachNode *cisc = mach->cisc_version(stk_offset, C)->as_Mach();
          cisc->set_req(inp,fp);          // Base register is frame pointer
          if( cisc->oper_input_base() > 1 && mach->oper_input_base() <= 1 ) {
            assert( cisc->oper_input_base() == 2, "Only adding one edge");
            cisc->ins_req(1,src);         // Requires a memory edge
          }
1628
          block->map_node(cisc, j);          // Insert into basic block
1629
          n->subsume_by(cisc, C); // Correct graph
D
duke 已提交
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
          //
          ++_used_cisc_instructions;
#ifndef PRODUCT
          if( TraceCISCSpill ) {
            tty->print("    cisc-instr: ");
            cisc->dump();
          }
#endif
        } else {
#ifndef PRODUCT
          if( TraceCISCSpill ) {
            tty->print("    using reg-instr: ");
            n->dump();
          }
#endif
          ++_unused_cisc_instructions;    // input can be on stack
        }
      }

    } // End of for all instructions

  } // End of for all blocks
}

// Helper to stretch above; recursively discover the base Node for a
// given derived Node.  Easy for AddP-related machine nodes, but needs
// to be recursive for derived Phis.
Node *PhaseChaitin::find_base_for_derived( Node **derived_base_map, Node *derived, uint &maxlrg ) {
  // See if already computed; if so return it
  if( derived_base_map[derived->_idx] )
    return derived_base_map[derived->_idx];

  // See if this happens to be a base.
  // NOTE: we use TypePtr instead of TypeOopPtr because we can have
  // pointers derived from NULL!  These are always along paths that
  // can't happen at run-time but the optimizer cannot deduce it so
  // we have to handle it gracefully.
1667 1668
  assert(!derived->bottom_type()->isa_narrowoop() ||
          derived->bottom_type()->make_ptr()->is_ptr()->_offset == 0, "sanity");
D
duke 已提交
1669 1670
  const TypePtr *tj = derived->bottom_type()->isa_ptr();
  // If its an OOP with a non-zero offset, then it is derived.
1671
  if( tj == NULL || tj->_offset == 0 ) {
D
duke 已提交
1672 1673 1674 1675 1676
    derived_base_map[derived->_idx] = derived;
    return derived;
  }
  // Derived is NULL+offset?  Base is NULL!
  if( derived->is_Con() ) {
1677 1678 1679 1680 1681 1682
    Node *base = _matcher.mach_null();
    assert(base != NULL, "sanity");
    if (base->in(0) == NULL) {
      // Initialize it once and make it shared:
      // set control to _root and place it into Start block
      // (where top() node is placed).
1683
      base->init_req(0, _cfg.get_root_node());
1684
      Block *startb = _cfg.get_block_for_node(C->top());
1685
      startb->insert_node(base, startb->find_node(C->top()));
1686
      _cfg.map_node_to_block(base, startb);
1687
      assert(_lrg_map.live_range_id(base) == 0, "should not have LRG yet");
1688
    }
1689
    if (_lrg_map.live_range_id(base) == 0) {
1690 1691
      new_lrg(base, maxlrg++);
    }
1692
    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");
D
duke 已提交
1693 1694 1695 1696 1697
    derived_base_map[derived->_idx] = base;
    return base;
  }

  // Check for AddP-related opcodes
1698
  if (!derived->is_Phi()) {
1699
    assert(derived->as_Mach()->ideal_Opcode() == Op_AddP, err_msg_res("but is: %s", derived->Name()));
D
duke 已提交
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
    Node *base = derived->in(AddPNode::Base);
    derived_base_map[derived->_idx] = base;
    return base;
  }

  // Recursively find bases for Phis.
  // First check to see if we can avoid a base Phi here.
  Node *base = find_base_for_derived( derived_base_map, derived->in(1),maxlrg);
  uint i;
  for( i = 2; i < derived->req(); i++ )
    if( base != find_base_for_derived( derived_base_map,derived->in(i),maxlrg))
      break;
  // Went to the end without finding any different bases?
  if( i == derived->req() ) {   // No need for a base Phi here
    derived_base_map[derived->_idx] = base;
    return base;
  }

  // Now we see we need a base-Phi here to merge the bases
1719
  const Type *t = base->bottom_type();
1720
  base = new (C) PhiNode( derived->in(0), t );
1721
  for( i = 1; i < derived->req(); i++ ) {
D
duke 已提交
1722
    base->init_req(i, find_base_for_derived(derived_base_map, derived->in(i), maxlrg));
1723 1724 1725
    t = t->meet(base->in(i)->bottom_type());
  }
  base->as_Phi()->set_type(t);
D
duke 已提交
1726 1727

  // Search the current block for an existing base-Phi
1728
  Block *b = _cfg.get_block_for_node(derived);
D
duke 已提交
1729
  for( i = 1; i <= b->end_idx(); i++ ) {// Search for matching Phi
1730
    Node *phi = b->get_node(i);
D
duke 已提交
1731
    if( !phi->is_Phi() ) {      // Found end of Phis with no match?
1732
      b->insert_node(base,  i); // Must insert created Phi here as base
1733
      _cfg.map_node_to_block(base, b);
D
duke 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
      new_lrg(base,maxlrg++);
      break;
    }
    // See if Phi matches.
    uint j;
    for( j = 1; j < base->req(); j++ )
      if( phi->in(j) != base->in(j) &&
          !(phi->in(j)->is_Con() && base->in(j)->is_Con()) ) // allow different NULLs
        break;
    if( j == base->req() ) {    // All inputs match?
      base = phi;               // Then use existing 'phi' and drop 'base'
      break;
    }
  }


  // Cache info for later passes
  derived_base_map[derived->_idx] = base;
  return base;
}

// At each Safepoint, insert extra debug edges for each pair of derived value/
// base pointer that is live across the Safepoint for oopmap building.  The
// edge pairs get added in after sfpt->jvmtail()->oopoff(), but are in the
// required edge set.
1759
bool PhaseChaitin::stretch_base_pointer_live_ranges(ResourceArea *a) {
D
duke 已提交
1760
  int must_recompute_live = false;
1761
  uint maxlrg = _lrg_map.max_lrg_id();
D
duke 已提交
1762 1763 1764 1765
  Node **derived_base_map = (Node**)a->Amalloc(sizeof(Node*)*C->unique());
  memset( derived_base_map, 0, sizeof(Node*)*C->unique() );

  // For all blocks in RPO do...
1766 1767
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    Block* block = _cfg.get_block(i);
D
duke 已提交
1768 1769
    // Note use of deep-copy constructor.  I cannot hammer the original
    // liveout bits, because they are needed by the following coalesce pass.
1770
    IndexSet liveout(_live->live(block));
D
duke 已提交
1771

1772
    for (uint j = block->end_idx() + 1; j > 1; j--) {
1773
      Node* n = block->get_node(j - 1);
D
duke 已提交
1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786

      // Pre-split compares of loop-phis.  Loop-phis form a cycle we would
      // like to see in the same register.  Compare uses the loop-phi and so
      // extends its live range BUT cannot be part of the cycle.  If this
      // extended live range overlaps with the update of the loop-phi value
      // we need both alive at the same time -- which requires at least 1
      // copy.  But because Intel has only 2-address registers we end up with
      // at least 2 copies, one before the loop-phi update instruction and
      // one after.  Instead we split the input to the compare just after the
      // phi.
      if( n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_CmpI ) {
        Node *phi = n->in(1);
        if( phi->is_Phi() && phi->as_Phi()->region()->is_Loop() ) {
1787
          Block *phi_block = _cfg.get_block_for_node(phi);
1788
          if (_cfg.get_block_for_node(phi_block->pred(2)) == block) {
D
duke 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
            const RegMask *mask = C->matcher()->idealreg2spillmask[Op_RegI];
            Node *spill = new (C) MachSpillCopyNode( phi, *mask, *mask );
            insert_proj( phi_block, 1, spill, maxlrg++ );
            n->set_req(1,spill);
            must_recompute_live = true;
          }
        }
      }

      // Get value being defined
1799 1800 1801
      uint lidx = _lrg_map.live_range_id(n);
      // Ignore the occasional brand-new live range
      if (lidx && lidx < _lrg_map.max_lrg_id()) {
D
duke 已提交
1802 1803 1804 1805 1806 1807
        // Remove from live-out set
        liveout.remove(lidx);

        // Copies do not define a new value and so do not interfere.
        // Remove the copies source from the liveout set before interfering.
        uint idx = n->is_Copy();
1808 1809 1810
        if (idx) {
          liveout.remove(_lrg_map.live_range_id(n->in(idx)));
        }
D
duke 已提交
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
      }

      // Found a safepoint?
      JVMState *jvms = n->jvms();
      if( jvms ) {
        // Now scan for a live derived pointer
        IndexSetIterator elements(&liveout);
        uint neighbor;
        while ((neighbor = elements.next()) != 0) {
          // Find reaching DEF for base and derived values
          // This works because we are still in SSA during this call.
          Node *derived = lrgs(neighbor)._def;
          const TypePtr *tj = derived->bottom_type()->isa_ptr();
1824 1825
          assert(!derived->bottom_type()->isa_narrowoop() ||
                  derived->bottom_type()->make_ptr()->is_ptr()->_offset == 0, "sanity");
D
duke 已提交
1826 1827
          // If its an OOP with a non-zero offset, then it is derived.
          if( tj && tj->_offset != 0 && tj->isa_oop_ptr() ) {
1828 1829
            Node *base = find_base_for_derived(derived_base_map, derived, maxlrg);
            assert(base->_idx < _lrg_map.size(), "");
D
duke 已提交
1830 1831
            // Add reaching DEFs of derived pointer and base pointer as a
            // pair of inputs
1832 1833
            n->add_req(derived);
            n->add_req(base);
D
duke 已提交
1834 1835 1836 1837 1838

            // See if the base pointer is already live to this point.
            // Since I'm working on the SSA form, live-ness amounts to
            // reaching def's.  So if I find the base's live range then
            // I know the base's def reaches here.
1839 1840 1841
            if ((_lrg_map.live_range_id(base) >= _lrg_map.max_lrg_id() || // (Brand new base (hence not live) or
                 !liveout.member(_lrg_map.live_range_id(base))) && // not live) AND
                 (_lrg_map.live_range_id(base) > 0) && // not a constant
1842
                 _cfg.get_block_for_node(base) != block) { // base not def'd in blk)
D
duke 已提交
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
              // Base pointer is not currently live.  Since I stretched
              // the base pointer to here and it crosses basic-block
              // boundaries, the global live info is now incorrect.
              // Recompute live.
              must_recompute_live = true;
            } // End of if base pointer is not live to debug info
          }
        } // End of scan all live data for derived ptrs crossing GC point
      } // End of if found a GC point

      // Make all inputs live
1854 1855 1856 1857 1858 1859
      if (!n->is_Phi()) {      // Phi function uses come from prior block
        for (uint k = 1; k < n->req(); k++) {
          uint lidx = _lrg_map.live_range_id(n->in(k));
          if (lidx < _lrg_map.max_lrg_id()) {
            liveout.insert(lidx);
          }
D
duke 已提交
1860 1861 1862 1863 1864 1865 1866
        }
      }

    } // End of forall instructions in block
    liveout.clear();  // Free the memory used by liveout.

  } // End of forall blocks
1867
  _lrg_map.set_max_lrg_id(maxlrg);
D
duke 已提交
1868 1869

  // If I created a new live range I need to recompute live
1870
  if (maxlrg != _ifg->_maxlrg) {
D
duke 已提交
1871
    must_recompute_live = true;
1872
  }
D
duke 已提交
1873 1874 1875 1876 1877

  return must_recompute_live != 0;
}

// Extend the node to LRG mapping
1878 1879 1880

void PhaseChaitin::add_reference(const Node *node, const Node *old_node) {
  _lrg_map.extend(node->_idx, _lrg_map.live_range_id(old_node));
D
duke 已提交
1881 1882 1883
}

#ifndef PRODUCT
1884 1885
void PhaseChaitin::dump(const Node *n) const {
  uint r = (n->_idx < _lrg_map.size()) ? _lrg_map.find_const(n) : 0;
D
duke 已提交
1886
  tty->print("L%d",r);
1887
  if (r && n->Opcode() != Op_Phi) {
D
duke 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
    if( _node_regs ) {          // Got a post-allocation copy of allocation?
      tty->print("[");
      OptoReg::Name second = get_reg_second(n);
      if( OptoReg::is_valid(second) ) {
        if( OptoReg::is_reg(second) )
          tty->print("%s:",Matcher::regName[second]);
        else
          tty->print("%s+%d:",OptoReg::regname(OptoReg::c_frame_pointer), reg2offset_unchecked(second));
      }
      OptoReg::Name first = get_reg_first(n);
      if( OptoReg::is_reg(first) )
        tty->print("%s]",Matcher::regName[first]);
      else
         tty->print("%s+%d]",OptoReg::regname(OptoReg::c_frame_pointer), reg2offset_unchecked(first));
    } else
    n->out_RegMask().dump();
  }
  tty->print("/N%d\t",n->_idx);
  tty->print("%s === ", n->Name());
  uint k;
1908
  for (k = 0; k < n->req(); k++) {
D
duke 已提交
1909
    Node *m = n->in(k);
1910 1911 1912
    if (!m) {
      tty->print("_ ");
    }
D
duke 已提交
1913
    else {
1914
      uint r = (m->_idx < _lrg_map.size()) ? _lrg_map.find_const(m) : 0;
D
duke 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
      tty->print("L%d",r);
      // Data MultiNode's can have projections with no real registers.
      // Don't die while dumping them.
      int op = n->Opcode();
      if( r && op != Op_Phi && op != Op_Proj && op != Op_SCMemProj) {
        if( _node_regs ) {
          tty->print("[");
          OptoReg::Name second = get_reg_second(n->in(k));
          if( OptoReg::is_valid(second) ) {
            if( OptoReg::is_reg(second) )
              tty->print("%s:",Matcher::regName[second]);
            else
              tty->print("%s+%d:",OptoReg::regname(OptoReg::c_frame_pointer),
                         reg2offset_unchecked(second));
          }
          OptoReg::Name first = get_reg_first(n->in(k));
          if( OptoReg::is_reg(first) )
            tty->print("%s]",Matcher::regName[first]);
          else
            tty->print("%s+%d]",OptoReg::regname(OptoReg::c_frame_pointer),
                       reg2offset_unchecked(first));
        } else
          n->in_RegMask(k).dump();
      }
      tty->print("/N%d ",m->_idx);
    }
  }
  if( k < n->len() && n->in(k) ) tty->print("| ");
  for( ; k < n->len(); k++ ) {
    Node *m = n->in(k);
1945 1946 1947 1948
    if(!m) {
      break;
    }
    uint r = (m->_idx < _lrg_map.size()) ? _lrg_map.find_const(m) : 0;
D
duke 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
    tty->print("L%d",r);
    tty->print("/N%d ",m->_idx);
  }
  if( n->is_Mach() ) n->as_Mach()->dump_spec(tty);
  else n->dump_spec(tty);
  if( _spilled_once.test(n->_idx ) ) {
    tty->print(" Spill_1");
    if( _spilled_twice.test(n->_idx ) )
      tty->print(" Spill_2");
  }
  tty->print("\n");
}

1962 1963
void PhaseChaitin::dump(const Block *b) const {
  b->dump_head(&_cfg);
D
duke 已提交
1964 1965

  // For all instructions
1966 1967
  for( uint j = 0; j < b->number_of_nodes(); j++ )
    dump(b->get_node(j));
D
duke 已提交
1968 1969 1970 1971 1972 1973 1974 1975
  // Print live-out info at end of block
  if( _live ) {
    tty->print("Liveout: ");
    IndexSet *live = _live->live(b);
    IndexSetIterator elements(live);
    tty->print("{");
    uint i;
    while ((i = elements.next()) != 0) {
1976
      tty->print("L%d ", _lrg_map.find_const(i));
D
duke 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
    }
    tty->print_cr("}");
  }
  tty->print("\n");
}

void PhaseChaitin::dump() const {
  tty->print( "--- Chaitin -- argsize: %d  framesize: %d ---\n",
              _matcher._new_SP, _framesize );

  // For all blocks
1988 1989 1990
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    dump(_cfg.get_block(i));
  }
D
duke 已提交
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
  // End of per-block dump
  tty->print("\n");

  if (!_ifg) {
    tty->print("(No IFG.)\n");
    return;
  }

  // Dump LRG array
  tty->print("--- Live RanGe Array ---\n");
2001
  for (uint i2 = 1; i2 < _lrg_map.max_lrg_id(); i2++) {
D
duke 已提交
2002
    tty->print("L%d: ",i2);
2003 2004 2005 2006 2007 2008
    if (i2 < _ifg->_maxlrg) {
      lrgs(i2).dump();
    }
    else {
      tty->print_cr("new LRG");
    }
D
duke 已提交
2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
  }
  tty->print_cr("");

  // Dump lo-degree list
  tty->print("Lo degree: ");
  for(uint i3 = _lo_degree; i3; i3 = lrgs(i3)._next )
    tty->print("L%d ",i3);
  tty->print_cr("");

  // Dump lo-stk-degree list
  tty->print("Lo stk degree: ");
  for(uint i4 = _lo_stk_degree; i4; i4 = lrgs(i4)._next )
    tty->print("L%d ",i4);
  tty->print_cr("");

  // Dump lo-degree list
  tty->print("Hi degree: ");
  for(uint i5 = _hi_degree; i5; i5 = lrgs(i5)._next )
    tty->print("L%d ",i5);
  tty->print_cr("");
}

void PhaseChaitin::dump_degree_lists() const {
  // Dump lo-degree list
  tty->print("Lo degree: ");
  for( uint i = _lo_degree; i; i = lrgs(i)._next )
    tty->print("L%d ",i);
  tty->print_cr("");

  // Dump lo-stk-degree list
  tty->print("Lo stk degree: ");
  for(uint i2 = _lo_stk_degree; i2; i2 = lrgs(i2)._next )
    tty->print("L%d ",i2);
  tty->print_cr("");

  // Dump lo-degree list
  tty->print("Hi degree: ");
  for(uint i3 = _hi_degree; i3; i3 = lrgs(i3)._next )
    tty->print("L%d ",i3);
  tty->print_cr("");
}

void PhaseChaitin::dump_simplified() const {
  tty->print("Simplified: ");
  for( uint i = _simplified; i; i = lrgs(i)._next )
    tty->print("L%d ",i);
  tty->print_cr("");
}

static char *print_reg( OptoReg::Name reg, const PhaseChaitin *pc, char *buf ) {
  if ((int)reg < 0)
    sprintf(buf, "<OptoReg::%d>", (int)reg);
  else if (OptoReg::is_reg(reg))
    strcpy(buf, Matcher::regName[reg]);
  else
    sprintf(buf,"%s + #%d",OptoReg::regname(OptoReg::c_frame_pointer),
            pc->reg2offset(reg));
  return buf+strlen(buf);
}

// Dump a register name into a buffer.  Be intelligent if we get called
// before allocation is complete.
char *PhaseChaitin::dump_register( const Node *n, char *buf  ) const {
  if( !this ) {                 // Not got anything?
    sprintf(buf,"N%d",n->_idx); // Then use Node index
  } else if( _node_regs ) {
    // Post allocation, use direct mappings, no LRG info available
    print_reg( get_reg_first(n), this, buf );
  } else {
2078
    uint lidx = _lrg_map.find_const(n); // Grab LRG number
D
duke 已提交
2079 2080 2081 2082
    if( !_ifg ) {
      sprintf(buf,"L%d",lidx);  // No register binding yet
    } else if( !lidx ) {        // Special, not allocated value
      strcpy(buf,"Special");
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
    } else {
      if (lrgs(lidx)._is_vector) {
        if (lrgs(lidx).mask().is_bound_set(lrgs(lidx).num_regs()))
          print_reg( lrgs(lidx).reg(), this, buf ); // a bound machine register
        else
          sprintf(buf,"L%d",lidx); // No register binding yet
      } else if( (lrgs(lidx).num_regs() == 1)
                 ? lrgs(lidx).mask().is_bound1()
                 : lrgs(lidx).mask().is_bound_pair() ) {
        // Hah!  We have a bound machine register
        print_reg( lrgs(lidx).reg(), this, buf );
      } else {
        sprintf(buf,"L%d",lidx); // No register binding yet
      }
D
duke 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105
    }
  }
  return buf+strlen(buf);
}

void PhaseChaitin::dump_for_spill_split_recycle() const {
  if( WizardMode && (PrintCompilation || PrintOpto) ) {
    // Display which live ranges need to be split and the allocator's state
    tty->print_cr("Graph-Coloring Iteration %d will split the following live ranges", _trip_cnt);
2106
    for (uint bidx = 1; bidx < _lrg_map.max_lrg_id(); bidx++) {
D
duke 已提交
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
      if( lrgs(bidx).alive() && lrgs(bidx).reg() >= LRG::SPILL_REG ) {
        tty->print("L%d: ", bidx);
        lrgs(bidx).dump();
      }
    }
    tty->cr();
    dump();
  }
}

void PhaseChaitin::dump_frame() const {
  const char *fp = OptoReg::regname(OptoReg::c_frame_pointer);
  const TypeTuple *domain = C->tf()->domain();
  const int        argcnt = domain->cnt() - TypeFunc::Parms;

  // Incoming arguments in registers dump
  for( int k = 0; k < argcnt; k++ ) {
    OptoReg::Name parmreg = _matcher._parm_regs[k].first();
    if( OptoReg::is_reg(parmreg))  {
      const char *reg_name = OptoReg::regname(parmreg);
      tty->print("#r%3.3d %s", parmreg, reg_name);
      parmreg = _matcher._parm_regs[k].second();
      if( OptoReg::is_reg(parmreg))  {
        tty->print(":%s", OptoReg::regname(parmreg));
      }
      tty->print("   : parm %d: ", k);
      domain->field_at(k + TypeFunc::Parms)->dump();
      tty->print_cr("");
    }
  }

  // Check for un-owned padding above incoming args
  OptoReg::Name reg = _matcher._new_SP;
  if( reg > _matcher._in_arg_limit ) {
    reg = OptoReg::add(reg, -1);
    tty->print_cr("#r%3.3d %s+%2d: pad0, owned by CALLER", reg, fp, reg2offset_unchecked(reg));
  }

  // Incoming argument area dump
  OptoReg::Name begin_in_arg = OptoReg::add(_matcher._old_SP,C->out_preserve_stack_slots());
  while( reg > begin_in_arg ) {
    reg = OptoReg::add(reg, -1);
    tty->print("#r%3.3d %s+%2d: ",reg,fp,reg2offset_unchecked(reg));
    int j;
    for( j = 0; j < argcnt; j++) {
      if( _matcher._parm_regs[j].first() == reg ||
          _matcher._parm_regs[j].second() == reg ) {
        tty->print("parm %d: ",j);
        domain->field_at(j + TypeFunc::Parms)->dump();
        tty->print_cr("");
        break;
      }
    }
    if( j >= argcnt )
      tty->print_cr("HOLE, owned by SELF");
  }

  // Old outgoing preserve area
  while( reg > _matcher._old_SP ) {
    reg = OptoReg::add(reg, -1);
    tty->print_cr("#r%3.3d %s+%2d: old out preserve",reg,fp,reg2offset_unchecked(reg));
  }

  // Old SP
  tty->print_cr("# -- Old %s -- Framesize: %d --",fp,
    reg2offset_unchecked(OptoReg::add(_matcher._old_SP,-1)) - reg2offset_unchecked(_matcher._new_SP)+jintSize);

  // Preserve area dump
K
kvn 已提交
2175 2176 2177 2178
  int fixed_slots = C->fixed_slots();
  OptoReg::Name begin_in_preserve = OptoReg::add(_matcher._old_SP, -(int)C->in_preserve_stack_slots());
  OptoReg::Name return_addr = _matcher.return_addr();

D
duke 已提交
2179
  reg = OptoReg::add(reg, -1);
K
kvn 已提交
2180
  while (OptoReg::is_stack(reg)) {
D
duke 已提交
2181
    tty->print("#r%3.3d %s+%2d: ",reg,fp,reg2offset_unchecked(reg));
K
kvn 已提交
2182
    if (return_addr == reg) {
D
duke 已提交
2183
      tty->print_cr("return address");
K
kvn 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
    } else if (reg >= begin_in_preserve) {
      // Preserved slots are present on x86
      if (return_addr == OptoReg::add(reg, VMRegImpl::slots_per_word))
        tty->print_cr("saved fp register");
      else if (return_addr == OptoReg::add(reg, 2*VMRegImpl::slots_per_word) &&
               VerifyStackAtCalls)
        tty->print_cr("0xBADB100D   +VerifyStackAtCalls");
      else
        tty->print_cr("in_preserve");
    } else if ((int)OptoReg::reg2stack(reg) < fixed_slots) {
D
duke 已提交
2194
      tty->print_cr("Fixed slot %d", OptoReg::reg2stack(reg));
K
kvn 已提交
2195 2196 2197
    } else {
      tty->print_cr("pad2, stack alignment");
    }
D
duke 已提交
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
    reg = OptoReg::add(reg, -1);
  }

  // Spill area dump
  reg = OptoReg::add(_matcher._new_SP, _framesize );
  while( reg > _matcher._out_arg_limit ) {
    reg = OptoReg::add(reg, -1);
    tty->print_cr("#r%3.3d %s+%2d: spill",reg,fp,reg2offset_unchecked(reg));
  }

  // Outgoing argument area dump
  while( reg > OptoReg::add(_matcher._new_SP, C->out_preserve_stack_slots()) ) {
    reg = OptoReg::add(reg, -1);
    tty->print_cr("#r%3.3d %s+%2d: outgoing argument",reg,fp,reg2offset_unchecked(reg));
  }

  // Outgoing new preserve area
  while( reg > _matcher._new_SP ) {
    reg = OptoReg::add(reg, -1);
    tty->print_cr("#r%3.3d %s+%2d: new out preserve",reg,fp,reg2offset_unchecked(reg));
  }
  tty->print_cr("#");
}

void PhaseChaitin::dump_bb( uint pre_order ) const {
  tty->print_cr("---dump of B%d---",pre_order);
2224 2225 2226 2227 2228
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    Block* block = _cfg.get_block(i);
    if (block->_pre_order == pre_order) {
      dump(block);
    }
D
duke 已提交
2229 2230 2231
  }
}

2232
void PhaseChaitin::dump_lrg( uint lidx, bool defs_only ) const {
D
duke 已提交
2233 2234
  tty->print_cr("---dump of L%d---",lidx);

2235 2236
  if (_ifg) {
    if (lidx >= _lrg_map.max_lrg_id()) {
D
duke 已提交
2237 2238 2239 2240
      tty->print("Attempt to print live range index beyond max live range.\n");
      return;
    }
    tty->print("L%d: ",lidx);
2241 2242 2243 2244 2245
    if (lidx < _ifg->_maxlrg) {
      lrgs(lidx).dump();
    } else {
      tty->print_cr("new LRG");
    }
D
duke 已提交
2246
  }
2247 2248
  if( _ifg && lidx < _ifg->_maxlrg) {
    tty->print("Neighbors: %d - ", _ifg->neighbor_cnt(lidx));
D
duke 已提交
2249 2250 2251 2252
    _ifg->neighbors(lidx)->dump();
    tty->cr();
  }
  // For all blocks
2253 2254
  for (uint i = 0; i < _cfg.number_of_blocks(); i++) {
    Block* block = _cfg.get_block(i);
D
duke 已提交
2255 2256 2257
    int dump_once = 0;

    // For all instructions
2258 2259
    for( uint j = 0; j < block->number_of_nodes(); j++ ) {
      Node *n = block->get_node(j);
2260 2261
      if (_lrg_map.find_const(n) == lidx) {
        if (!dump_once++) {
D
duke 已提交
2262
          tty->cr();
2263
          block->dump_head(&_cfg);
D
duke 已提交
2264 2265 2266 2267
        }
        dump(n);
        continue;
      }
2268 2269 2270 2271
      if (!defs_only) {
        uint cnt = n->req();
        for( uint k = 1; k < cnt; k++ ) {
          Node *m = n->in(k);
2272 2273 2274 2275 2276
          if (!m)  {
            continue;  // be robust in the dumper
          }
          if (_lrg_map.find_const(m) == lidx) {
            if (!dump_once++) {
2277
              tty->cr();
2278
              block->dump_head(&_cfg);
2279 2280
            }
            dump(n);
D
duke 已提交
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335
          }
        }
      }
    }
  } // End of per-block dump
  tty->cr();
}
#endif // not PRODUCT

int PhaseChaitin::_final_loads  = 0;
int PhaseChaitin::_final_stores = 0;
int PhaseChaitin::_final_memoves= 0;
int PhaseChaitin::_final_copies = 0;
double PhaseChaitin::_final_load_cost  = 0;
double PhaseChaitin::_final_store_cost = 0;
double PhaseChaitin::_final_memove_cost= 0;
double PhaseChaitin::_final_copy_cost  = 0;
int PhaseChaitin::_conserv_coalesce = 0;
int PhaseChaitin::_conserv_coalesce_pair = 0;
int PhaseChaitin::_conserv_coalesce_trie = 0;
int PhaseChaitin::_conserv_coalesce_quad = 0;
int PhaseChaitin::_post_alloc = 0;
int PhaseChaitin::_lost_opp_pp_coalesce = 0;
int PhaseChaitin::_lost_opp_cflow_coalesce = 0;
int PhaseChaitin::_used_cisc_instructions   = 0;
int PhaseChaitin::_unused_cisc_instructions = 0;
int PhaseChaitin::_allocator_attempts       = 0;
int PhaseChaitin::_allocator_successes      = 0;

#ifndef PRODUCT
uint PhaseChaitin::_high_pressure           = 0;
uint PhaseChaitin::_low_pressure            = 0;

void PhaseChaitin::print_chaitin_statistics() {
  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);
  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);
  tty->print_cr("Adjusted spill cost = %7.0f.",
                _final_load_cost*4.0 + _final_store_cost  * 2.0 +
                _final_copy_cost*1.0 + _final_memove_cost*12.0);
  tty->print("Conservatively coalesced %d copies, %d pairs",
                _conserv_coalesce, _conserv_coalesce_pair);
  if( _conserv_coalesce_trie || _conserv_coalesce_quad )
    tty->print(", %d tries, %d quads", _conserv_coalesce_trie, _conserv_coalesce_quad);
  tty->print_cr(", %d post alloc.", _post_alloc);
  if( _lost_opp_pp_coalesce || _lost_opp_cflow_coalesce )
    tty->print_cr("Lost coalesce opportunity, %d private-private, and %d cflow interfered.",
                  _lost_opp_pp_coalesce, _lost_opp_cflow_coalesce );
  if( _used_cisc_instructions || _unused_cisc_instructions )
    tty->print_cr("Used cisc instruction  %d,  remained in register %d",
                   _used_cisc_instructions, _unused_cisc_instructions);
  if( _allocator_successes != 0 )
    tty->print_cr("Average allocation trips %f", (float)_allocator_attempts/(float)_allocator_successes);
  tty->print_cr("High Pressure Blocks = %d, Low Pressure Blocks = %d", _high_pressure, _low_pressure);
}
#endif // not PRODUCT