src/share/vm/opto/coalesce.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/opto/coalesce.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,789 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "memory/allocation.inline.hpp"
    1.30 +#include "opto/block.hpp"
    1.31 +#include "opto/cfgnode.hpp"
    1.32 +#include "opto/chaitin.hpp"
    1.33 +#include "opto/coalesce.hpp"
    1.34 +#include "opto/connode.hpp"
    1.35 +#include "opto/indexSet.hpp"
    1.36 +#include "opto/machnode.hpp"
    1.37 +#include "opto/matcher.hpp"
    1.38 +#include "opto/regmask.hpp"
    1.39 +
    1.40 +#ifndef PRODUCT
    1.41 +void PhaseCoalesce::dump(Node *n) const {
    1.42 +  // Being a const function means I cannot use 'Find'
    1.43 +  uint r = _phc._lrg_map.find(n);
    1.44 +  tty->print("L%d/N%d ",r,n->_idx);
    1.45 +}
    1.46 +
    1.47 +void PhaseCoalesce::dump() const {
    1.48 +  // I know I have a block layout now, so I can print blocks in a loop
    1.49 +  for( uint i=0; i<_phc._cfg.number_of_blocks(); i++ ) {
    1.50 +    uint j;
    1.51 +    Block* b = _phc._cfg.get_block(i);
    1.52 +    // Print a nice block header
    1.53 +    tty->print("B%d: ",b->_pre_order);
    1.54 +    for( j=1; j<b->num_preds(); j++ )
    1.55 +      tty->print("B%d ", _phc._cfg.get_block_for_node(b->pred(j))->_pre_order);
    1.56 +    tty->print("-> ");
    1.57 +    for( j=0; j<b->_num_succs; j++ )
    1.58 +      tty->print("B%d ",b->_succs[j]->_pre_order);
    1.59 +    tty->print(" IDom: B%d/#%d\n", b->_idom ? b->_idom->_pre_order : 0, b->_dom_depth);
    1.60 +    uint cnt = b->number_of_nodes();
    1.61 +    for( j=0; j<cnt; j++ ) {
    1.62 +      Node *n = b->get_node(j);
    1.63 +      dump( n );
    1.64 +      tty->print("\t%s\t",n->Name());
    1.65 +
    1.66 +      // Dump the inputs
    1.67 +      uint k;                   // Exit value of loop
    1.68 +      for( k=0; k<n->req(); k++ ) // For all required inputs
    1.69 +        if( n->in(k) ) dump( n->in(k) );
    1.70 +        else tty->print("_ ");
    1.71 +      int any_prec = 0;
    1.72 +      for( ; k<n->len(); k++ )          // For all precedence inputs
    1.73 +        if( n->in(k) ) {
    1.74 +          if( !any_prec++ ) tty->print(" |");
    1.75 +          dump( n->in(k) );
    1.76 +        }
    1.77 +
    1.78 +      // Dump node-specific info
    1.79 +      n->dump_spec(tty);
    1.80 +      tty->print("\n");
    1.81 +
    1.82 +    }
    1.83 +    tty->print("\n");
    1.84 +  }
    1.85 +}
    1.86 +#endif
    1.87 +
    1.88 +// Combine the live ranges def'd by these 2 Nodes.  N2 is an input to N1.
    1.89 +void PhaseCoalesce::combine_these_two(Node *n1, Node *n2) {
    1.90 +  uint lr1 = _phc._lrg_map.find(n1);
    1.91 +  uint lr2 = _phc._lrg_map.find(n2);
    1.92 +  if( lr1 != lr2 &&             // Different live ranges already AND
    1.93 +      !_phc._ifg->test_edge_sq( lr1, lr2 ) ) {  // Do not interfere
    1.94 +    LRG *lrg1 = &_phc.lrgs(lr1);
    1.95 +    LRG *lrg2 = &_phc.lrgs(lr2);
    1.96 +    // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK.
    1.97 +
    1.98 +    // Now, why is int->oop OK?  We end up declaring a raw-pointer as an oop
    1.99 +    // and in general that's a bad thing.  However, int->oop conversions only
   1.100 +    // happen at GC points, so the lifetime of the misclassified raw-pointer
   1.101 +    // is from the CheckCastPP (that converts it to an oop) backwards up
   1.102 +    // through a merge point and into the slow-path call, and around the
   1.103 +    // diamond up to the heap-top check and back down into the slow-path call.
   1.104 +    // The misclassified raw pointer is NOT live across the slow-path call,
   1.105 +    // and so does not appear in any GC info, so the fact that it is
   1.106 +    // misclassified is OK.
   1.107 +
   1.108 +    if( (lrg1->_is_oop || !lrg2->_is_oop) && // not an oop->int cast AND
   1.109 +        // Compatible final mask
   1.110 +        lrg1->mask().overlap( lrg2->mask() ) ) {
   1.111 +      // Merge larger into smaller.
   1.112 +      if( lr1 > lr2 ) {
   1.113 +        uint  tmp =  lr1;  lr1 =  lr2;  lr2 =  tmp;
   1.114 +        Node   *n =   n1;   n1 =   n2;   n2 =    n;
   1.115 +        LRG *ltmp = lrg1; lrg1 = lrg2; lrg2 = ltmp;
   1.116 +      }
   1.117 +      // Union lr2 into lr1
   1.118 +      _phc.Union( n1, n2 );
   1.119 +      if (lrg1->_maxfreq < lrg2->_maxfreq)
   1.120 +        lrg1->_maxfreq = lrg2->_maxfreq;
   1.121 +      // Merge in the IFG
   1.122 +      _phc._ifg->Union( lr1, lr2 );
   1.123 +      // Combine register restrictions
   1.124 +      lrg1->AND(lrg2->mask());
   1.125 +    }
   1.126 +  }
   1.127 +}
   1.128 +
   1.129 +// Copy coalescing
   1.130 +void PhaseCoalesce::coalesce_driver() {
   1.131 +  verify();
   1.132 +  // Coalesce from high frequency to low
   1.133 +  for (uint i = 0; i < _phc._cfg.number_of_blocks(); i++) {
   1.134 +    coalesce(_phc._blks[i]);
   1.135 +  }
   1.136 +}
   1.137 +
   1.138 +// I am inserting copies to come out of SSA form.  In the general case, I am
   1.139 +// doing a parallel renaming.  I'm in the Named world now, so I can't do a
   1.140 +// general parallel renaming.  All the copies now use  "names" (live-ranges)
   1.141 +// to carry values instead of the explicit use-def chains.  Suppose I need to
   1.142 +// insert 2 copies into the same block.  They copy L161->L128 and L128->L132.
   1.143 +// If I insert them in the wrong order then L128 will get clobbered before it
   1.144 +// can get used by the second copy.  This cannot happen in the SSA model;
   1.145 +// direct use-def chains get me the right value.  It DOES happen in the named
   1.146 +// model so I have to handle the reordering of copies.
   1.147 +//
   1.148 +// In general, I need to topo-sort the placed copies to avoid conflicts.
   1.149 +// Its possible to have a closed cycle of copies (e.g., recirculating the same
   1.150 +// values around a loop).  In this case I need a temp to break the cycle.
   1.151 +void PhaseAggressiveCoalesce::insert_copy_with_overlap( Block *b, Node *copy, uint dst_name, uint src_name ) {
   1.152 +
   1.153 +  // Scan backwards for the locations of the last use of the dst_name.
   1.154 +  // I am about to clobber the dst_name, so the copy must be inserted
   1.155 +  // after the last use.  Last use is really first-use on a backwards scan.
   1.156 +  uint i = b->end_idx()-1;
   1.157 +  while(1) {
   1.158 +    Node *n = b->get_node(i);
   1.159 +    // Check for end of virtual copies; this is also the end of the
   1.160 +    // parallel renaming effort.
   1.161 +    if (n->_idx < _unique) {
   1.162 +      break;
   1.163 +    }
   1.164 +    uint idx = n->is_Copy();
   1.165 +    assert( idx || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" );
   1.166 +    if (idx && _phc._lrg_map.find(n->in(idx)) == dst_name) {
   1.167 +      break;
   1.168 +    }
   1.169 +    i--;
   1.170 +  }
   1.171 +  uint last_use_idx = i;
   1.172 +
   1.173 +  // Also search for any kill of src_name that exits the block.
   1.174 +  // Since the copy uses src_name, I have to come before any kill.
   1.175 +  uint kill_src_idx = b->end_idx();
   1.176 +  // There can be only 1 kill that exits any block and that is
   1.177 +  // the last kill.  Thus it is the first kill on a backwards scan.
   1.178 +  i = b->end_idx()-1;
   1.179 +  while (1) {
   1.180 +    Node *n = b->get_node(i);
   1.181 +    // Check for end of virtual copies; this is also the end of the
   1.182 +    // parallel renaming effort.
   1.183 +    if (n->_idx < _unique) {
   1.184 +      break;
   1.185 +    }
   1.186 +    assert( n->is_Copy() || n->is_Con() || n->is_MachProj(), "Only copies during parallel renaming" );
   1.187 +    if (_phc._lrg_map.find(n) == src_name) {
   1.188 +      kill_src_idx = i;
   1.189 +      break;
   1.190 +    }
   1.191 +    i--;
   1.192 +  }
   1.193 +  // Need a temp?  Last use of dst comes after the kill of src?
   1.194 +  if (last_use_idx >= kill_src_idx) {
   1.195 +    // Need to break a cycle with a temp
   1.196 +    uint idx = copy->is_Copy();
   1.197 +    Node *tmp = copy->clone();
   1.198 +    uint max_lrg_id = _phc._lrg_map.max_lrg_id();
   1.199 +    _phc.new_lrg(tmp, max_lrg_id);
   1.200 +    _phc._lrg_map.set_max_lrg_id(max_lrg_id + 1);
   1.201 +
   1.202 +    // Insert new temp between copy and source
   1.203 +    tmp ->set_req(idx,copy->in(idx));
   1.204 +    copy->set_req(idx,tmp);
   1.205 +    // Save source in temp early, before source is killed
   1.206 +    b->insert_node(tmp, kill_src_idx);
   1.207 +    _phc._cfg.map_node_to_block(tmp, b);
   1.208 +    last_use_idx++;
   1.209 +  }
   1.210 +
   1.211 +  // Insert just after last use
   1.212 +  b->insert_node(copy, last_use_idx + 1);
   1.213 +}
   1.214 +
   1.215 +void PhaseAggressiveCoalesce::insert_copies( Matcher &matcher ) {
   1.216 +  // We do LRGs compressing and fix a liveout data only here since the other
   1.217 +  // place in Split() is guarded by the assert which we never hit.
   1.218 +  _phc._lrg_map.compress_uf_map_for_nodes();
   1.219 +  // Fix block's liveout data for compressed live ranges.
   1.220 +  for (uint lrg = 1; lrg < _phc._lrg_map.max_lrg_id(); lrg++) {
   1.221 +    uint compressed_lrg = _phc._lrg_map.find(lrg);
   1.222 +    if (lrg != compressed_lrg) {
   1.223 +      for (uint bidx = 0; bidx < _phc._cfg.number_of_blocks(); bidx++) {
   1.224 +        IndexSet *liveout = _phc._live->live(_phc._cfg.get_block(bidx));
   1.225 +        if (liveout->member(lrg)) {
   1.226 +          liveout->remove(lrg);
   1.227 +          liveout->insert(compressed_lrg);
   1.228 +        }
   1.229 +      }
   1.230 +    }
   1.231 +  }
   1.232 +
   1.233 +  // All new nodes added are actual copies to replace virtual copies.
   1.234 +  // Nodes with index less than '_unique' are original, non-virtual Nodes.
   1.235 +  _unique = C->unique();
   1.236 +
   1.237 +  for (uint i = 0; i < _phc._cfg.number_of_blocks(); i++) {
   1.238 +    C->check_node_count(NodeLimitFudgeFactor, "out of nodes in coalesce");
   1.239 +    if (C->failing()) return;
   1.240 +    Block *b = _phc._cfg.get_block(i);
   1.241 +    uint cnt = b->num_preds();  // Number of inputs to the Phi
   1.242 +
   1.243 +    for( uint l = 1; l<b->number_of_nodes(); l++ ) {
   1.244 +      Node *n = b->get_node(l);
   1.245 +
   1.246 +      // Do not use removed-copies, use copied value instead
   1.247 +      uint ncnt = n->req();
   1.248 +      for( uint k = 1; k<ncnt; k++ ) {
   1.249 +        Node *copy = n->in(k);
   1.250 +        uint cidx = copy->is_Copy();
   1.251 +        if( cidx ) {
   1.252 +          Node *def = copy->in(cidx);
   1.253 +          if (_phc._lrg_map.find(copy) == _phc._lrg_map.find(def)) {
   1.254 +            n->set_req(k, def);
   1.255 +          }
   1.256 +        }
   1.257 +      }
   1.258 +
   1.259 +      // Remove any explicit copies that get coalesced.
   1.260 +      uint cidx = n->is_Copy();
   1.261 +      if( cidx ) {
   1.262 +        Node *def = n->in(cidx);
   1.263 +        if (_phc._lrg_map.find(n) == _phc._lrg_map.find(def)) {
   1.264 +          n->replace_by(def);
   1.265 +          n->set_req(cidx,NULL);
   1.266 +          b->remove_node(l);
   1.267 +          l--;
   1.268 +          continue;
   1.269 +        }
   1.270 +      }
   1.271 +
   1.272 +      if (n->is_Phi()) {
   1.273 +        // Get the chosen name for the Phi
   1.274 +        uint phi_name = _phc._lrg_map.find(n);
   1.275 +        // Ignore the pre-allocated specials
   1.276 +        if (!phi_name) {
   1.277 +          continue;
   1.278 +        }
   1.279 +        // Check for mismatch inputs to Phi
   1.280 +        for (uint j = 1; j < cnt; j++) {
   1.281 +          Node *m = n->in(j);
   1.282 +          uint src_name = _phc._lrg_map.find(m);
   1.283 +          if (src_name != phi_name) {
   1.284 +            Block *pred = _phc._cfg.get_block_for_node(b->pred(j));
   1.285 +            Node *copy;
   1.286 +            assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach");
   1.287 +            // Rematerialize constants instead of copying them
   1.288 +            if( m->is_Mach() && m->as_Mach()->is_Con() &&
   1.289 +                m->as_Mach()->rematerialize() ) {
   1.290 +              copy = m->clone();
   1.291 +              // Insert the copy in the predecessor basic block
   1.292 +              pred->add_inst(copy);
   1.293 +              // Copy any flags as well
   1.294 +              _phc.clone_projs(pred, pred->end_idx(), m, copy, _phc._lrg_map);
   1.295 +            } else {
   1.296 +              const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
   1.297 +              copy = new (C) MachSpillCopyNode(m, *rm, *rm);
   1.298 +              // Find a good place to insert.  Kinda tricky, use a subroutine
   1.299 +              insert_copy_with_overlap(pred,copy,phi_name,src_name);
   1.300 +            }
   1.301 +            // Insert the copy in the use-def chain
   1.302 +            n->set_req(j, copy);
   1.303 +            _phc._cfg.map_node_to_block(copy, pred);
   1.304 +            // Extend ("register allocate") the names array for the copy.
   1.305 +            _phc._lrg_map.extend(copy->_idx, phi_name);
   1.306 +          } // End of if Phi names do not match
   1.307 +        } // End of for all inputs to Phi
   1.308 +      } else { // End of if Phi
   1.309 +
   1.310 +        // Now check for 2-address instructions
   1.311 +        uint idx;
   1.312 +        if( n->is_Mach() && (idx=n->as_Mach()->two_adr()) ) {
   1.313 +          // Get the chosen name for the Node
   1.314 +          uint name = _phc._lrg_map.find(n);
   1.315 +          assert (name, "no 2-address specials");
   1.316 +          // Check for name mis-match on the 2-address input
   1.317 +          Node *m = n->in(idx);
   1.318 +          if (_phc._lrg_map.find(m) != name) {
   1.319 +            Node *copy;
   1.320 +            assert(!m->is_Con() || m->is_Mach(), "all Con must be Mach");
   1.321 +            // At this point it is unsafe to extend live ranges (6550579).
   1.322 +            // Rematerialize only constants as we do for Phi above.
   1.323 +            if(m->is_Mach() && m->as_Mach()->is_Con() &&
   1.324 +               m->as_Mach()->rematerialize()) {
   1.325 +              copy = m->clone();
   1.326 +              // Insert the copy in the basic block, just before us
   1.327 +              b->insert_node(copy, l++);
   1.328 +              l += _phc.clone_projs(b, l, m, copy, _phc._lrg_map);
   1.329 +            } else {
   1.330 +              const RegMask *rm = C->matcher()->idealreg2spillmask[m->ideal_reg()];
   1.331 +              copy = new (C) MachSpillCopyNode(m, *rm, *rm);
   1.332 +              // Insert the copy in the basic block, just before us
   1.333 +              b->insert_node(copy, l++);
   1.334 +            }
   1.335 +            // Insert the copy in the use-def chain
   1.336 +            n->set_req(idx, copy);
   1.337 +            // Extend ("register allocate") the names array for the copy.
   1.338 +            _phc._lrg_map.extend(copy->_idx, name);
   1.339 +            _phc._cfg.map_node_to_block(copy, b);
   1.340 +          }
   1.341 +
   1.342 +        } // End of is two-adr
   1.343 +
   1.344 +        // Insert a copy at a debug use for a lrg which has high frequency
   1.345 +        if (b->_freq < OPTO_DEBUG_SPLIT_FREQ || _phc._cfg.is_uncommon(b)) {
   1.346 +          // Walk the debug inputs to the node and check for lrg freq
   1.347 +          JVMState* jvms = n->jvms();
   1.348 +          uint debug_start = jvms ? jvms->debug_start() : 999999;
   1.349 +          uint debug_end   = jvms ? jvms->debug_end()   : 999999;
   1.350 +          for(uint inpidx = debug_start; inpidx < debug_end; inpidx++) {
   1.351 +            // Do not split monitors; they are only needed for debug table
   1.352 +            // entries and need no code.
   1.353 +            if (jvms->is_monitor_use(inpidx)) {
   1.354 +              continue;
   1.355 +            }
   1.356 +            Node *inp = n->in(inpidx);
   1.357 +            uint nidx = _phc._lrg_map.live_range_id(inp);
   1.358 +            LRG &lrg = lrgs(nidx);
   1.359 +
   1.360 +            // If this lrg has a high frequency use/def
   1.361 +            if( lrg._maxfreq >= _phc.high_frequency_lrg() ) {
   1.362 +              // If the live range is also live out of this block (like it
   1.363 +              // would be for a fast/slow idiom), the normal spill mechanism
   1.364 +              // does an excellent job.  If it is not live out of this block
   1.365 +              // (like it would be for debug info to uncommon trap) splitting
   1.366 +              // the live range now allows a better allocation in the high
   1.367 +              // frequency blocks.
   1.368 +              //   Build_IFG_virtual has converted the live sets to
   1.369 +              // live-IN info, not live-OUT info.
   1.370 +              uint k;
   1.371 +              for( k=0; k < b->_num_succs; k++ )
   1.372 +                if( _phc._live->live(b->_succs[k])->member( nidx ) )
   1.373 +                  break;      // Live in to some successor block?
   1.374 +              if( k < b->_num_succs )
   1.375 +                continue;     // Live out; do not pre-split
   1.376 +              // Split the lrg at this use
   1.377 +              const RegMask *rm = C->matcher()->idealreg2spillmask[inp->ideal_reg()];
   1.378 +              Node *copy = new (C) MachSpillCopyNode( inp, *rm, *rm );
   1.379 +              // Insert the copy in the use-def chain
   1.380 +              n->set_req(inpidx, copy );
   1.381 +              // Insert the copy in the basic block, just before us
   1.382 +              b->insert_node(copy,  l++);
   1.383 +              // Extend ("register allocate") the names array for the copy.
   1.384 +              uint max_lrg_id = _phc._lrg_map.max_lrg_id();
   1.385 +              _phc.new_lrg(copy, max_lrg_id);
   1.386 +              _phc._lrg_map.set_max_lrg_id(max_lrg_id + 1);
   1.387 +              _phc._cfg.map_node_to_block(copy, b);
   1.388 +              //tty->print_cr("Split a debug use in Aggressive Coalesce");
   1.389 +            }  // End of if high frequency use/def
   1.390 +          }  // End of for all debug inputs
   1.391 +        }  // End of if low frequency safepoint
   1.392 +
   1.393 +      } // End of if Phi
   1.394 +
   1.395 +    } // End of for all instructions
   1.396 +  } // End of for all blocks
   1.397 +}
   1.398 +
   1.399 +
   1.400 +// Aggressive (but pessimistic) copy coalescing of a single block
   1.401 +
   1.402 +// The following coalesce pass represents a single round of aggressive
   1.403 +// pessimistic coalesce.  "Aggressive" means no attempt to preserve
   1.404 +// colorability when coalescing.  This occasionally means more spills, but
   1.405 +// it also means fewer rounds of coalescing for better code - and that means
   1.406 +// faster compiles.
   1.407 +
   1.408 +// "Pessimistic" means we do not hit the fixed point in one pass (and we are
   1.409 +// reaching for the least fixed point to boot).  This is typically solved
   1.410 +// with a few more rounds of coalescing, but the compiler must run fast.  We
   1.411 +// could optimistically coalescing everything touching PhiNodes together
   1.412 +// into one big live range, then check for self-interference.  Everywhere
   1.413 +// the live range interferes with self it would have to be split.  Finding
   1.414 +// the right split points can be done with some heuristics (based on
   1.415 +// expected frequency of edges in the live range).  In short, it's a real
   1.416 +// research problem and the timeline is too short to allow such research.
   1.417 +// Further thoughts: (1) build the LR in a pass, (2) find self-interference
   1.418 +// in another pass, (3) per each self-conflict, split, (4) split by finding
   1.419 +// the low-cost cut (min-cut) of the LR, (5) edges in the LR are weighted
   1.420 +// according to the GCM algorithm (or just exec freq on CFG edges).
   1.421 +
   1.422 +void PhaseAggressiveCoalesce::coalesce( Block *b ) {
   1.423 +  // Copies are still "virtual" - meaning we have not made them explicitly
   1.424 +  // copies.  Instead, Phi functions of successor blocks have mis-matched
   1.425 +  // live-ranges.  If I fail to coalesce, I'll have to insert a copy to line
   1.426 +  // up the live-ranges.  Check for Phis in successor blocks.
   1.427 +  uint i;
   1.428 +  for( i=0; i<b->_num_succs; i++ ) {
   1.429 +    Block *bs = b->_succs[i];
   1.430 +    // Find index of 'b' in 'bs' predecessors
   1.431 +    uint j=1;
   1.432 +    while (_phc._cfg.get_block_for_node(bs->pred(j)) != b) {
   1.433 +      j++;
   1.434 +    }
   1.435 +
   1.436 +    // Visit all the Phis in successor block
   1.437 +    for( uint k = 1; k<bs->number_of_nodes(); k++ ) {
   1.438 +      Node *n = bs->get_node(k);
   1.439 +      if( !n->is_Phi() ) break;
   1.440 +      combine_these_two( n, n->in(j) );
   1.441 +    }
   1.442 +  } // End of for all successor blocks
   1.443 +
   1.444 +
   1.445 +  // Check _this_ block for 2-address instructions and copies.
   1.446 +  uint cnt = b->end_idx();
   1.447 +  for( i = 1; i<cnt; i++ ) {
   1.448 +    Node *n = b->get_node(i);
   1.449 +    uint idx;
   1.450 +    // 2-address instructions have a virtual Copy matching their input
   1.451 +    // to their output
   1.452 +    if (n->is_Mach() && (idx = n->as_Mach()->two_adr())) {
   1.453 +      MachNode *mach = n->as_Mach();
   1.454 +      combine_these_two(mach, mach->in(idx));
   1.455 +    }
   1.456 +  } // End of for all instructions in block
   1.457 +}
   1.458 +
   1.459 +PhaseConservativeCoalesce::PhaseConservativeCoalesce(PhaseChaitin &chaitin) : PhaseCoalesce(chaitin) {
   1.460 +  _ulr.initialize(_phc._lrg_map.max_lrg_id());
   1.461 +}
   1.462 +
   1.463 +void PhaseConservativeCoalesce::verify() {
   1.464 +#ifdef ASSERT
   1.465 +  _phc.set_was_low();
   1.466 +#endif
   1.467 +}
   1.468 +
   1.469 +void PhaseConservativeCoalesce::union_helper( Node *lr1_node, Node *lr2_node, uint lr1, uint lr2, Node *src_def, Node *dst_copy, Node *src_copy, Block *b, uint bindex ) {
   1.470 +  // Join live ranges.  Merge larger into smaller.  Union lr2 into lr1 in the
   1.471 +  // union-find tree
   1.472 +  _phc.Union( lr1_node, lr2_node );
   1.473 +
   1.474 +  // Single-def live range ONLY if both live ranges are single-def.
   1.475 +  // If both are single def, then src_def powers one live range
   1.476 +  // and def_copy powers the other.  After merging, src_def powers
   1.477 +  // the combined live range.
   1.478 +  lrgs(lr1)._def = (lrgs(lr1).is_multidef() ||
   1.479 +                        lrgs(lr2).is_multidef() )
   1.480 +    ? NodeSentinel : src_def;
   1.481 +  lrgs(lr2)._def = NULL;    // No def for lrg 2
   1.482 +  lrgs(lr2).Clear();        // Force empty mask for LRG 2
   1.483 +  //lrgs(lr2)._size = 0;      // Live-range 2 goes dead
   1.484 +  lrgs(lr1)._is_oop |= lrgs(lr2)._is_oop;
   1.485 +  lrgs(lr2)._is_oop = 0;    // In particular, not an oop for GC info
   1.486 +
   1.487 +  if (lrgs(lr1)._maxfreq < lrgs(lr2)._maxfreq)
   1.488 +    lrgs(lr1)._maxfreq = lrgs(lr2)._maxfreq;
   1.489 +
   1.490 +  // Copy original value instead.  Intermediate copies go dead, and
   1.491 +  // the dst_copy becomes useless.
   1.492 +  int didx = dst_copy->is_Copy();
   1.493 +  dst_copy->set_req( didx, src_def );
   1.494 +  // Add copy to free list
   1.495 +  // _phc.free_spillcopy(b->_nodes[bindex]);
   1.496 +  assert( b->get_node(bindex) == dst_copy, "" );
   1.497 +  dst_copy->replace_by( dst_copy->in(didx) );
   1.498 +  dst_copy->set_req( didx, NULL);
   1.499 +  b->remove_node(bindex);
   1.500 +  if( bindex < b->_ihrp_index ) b->_ihrp_index--;
   1.501 +  if( bindex < b->_fhrp_index ) b->_fhrp_index--;
   1.502 +
   1.503 +  // Stretched lr1; add it to liveness of intermediate blocks
   1.504 +  Block *b2 = _phc._cfg.get_block_for_node(src_copy);
   1.505 +  while( b != b2 ) {
   1.506 +    b = _phc._cfg.get_block_for_node(b->pred(1));
   1.507 +    _phc._live->live(b)->insert(lr1);
   1.508 +  }
   1.509 +}
   1.510 +
   1.511 +// Factored code from copy_copy that computes extra interferences from
   1.512 +// lengthening a live range by double-coalescing.
   1.513 +uint PhaseConservativeCoalesce::compute_separating_interferences(Node *dst_copy, Node *src_copy, Block *b, uint bindex, RegMask &rm, uint reg_degree, uint rm_size, uint lr1, uint lr2 ) {
   1.514 +
   1.515 +  assert(!lrgs(lr1)._fat_proj, "cannot coalesce fat_proj");
   1.516 +  assert(!lrgs(lr2)._fat_proj, "cannot coalesce fat_proj");
   1.517 +  Node *prev_copy = dst_copy->in(dst_copy->is_Copy());
   1.518 +  Block *b2 = b;
   1.519 +  uint bindex2 = bindex;
   1.520 +  while( 1 ) {
   1.521 +    // Find previous instruction
   1.522 +    bindex2--;                  // Chain backwards 1 instruction
   1.523 +    while( bindex2 == 0 ) {     // At block start, find prior block
   1.524 +      assert( b2->num_preds() == 2, "cannot double coalesce across c-flow" );
   1.525 +      b2 = _phc._cfg.get_block_for_node(b2->pred(1));
   1.526 +      bindex2 = b2->end_idx()-1;
   1.527 +    }
   1.528 +    // Get prior instruction
   1.529 +    assert(bindex2 < b2->number_of_nodes(), "index out of bounds");
   1.530 +    Node *x = b2->get_node(bindex2);
   1.531 +    if( x == prev_copy ) {      // Previous copy in copy chain?
   1.532 +      if( prev_copy == src_copy)// Found end of chain and all interferences
   1.533 +        break;                  // So break out of loop
   1.534 +      // Else work back one in copy chain
   1.535 +      prev_copy = prev_copy->in(prev_copy->is_Copy());
   1.536 +    } else {                    // Else collect interferences
   1.537 +      uint lidx = _phc._lrg_map.find(x);
   1.538 +      // Found another def of live-range being stretched?
   1.539 +      if(lidx == lr1) {
   1.540 +        return max_juint;
   1.541 +      }
   1.542 +      if(lidx == lr2) {
   1.543 +        return max_juint;
   1.544 +      }
   1.545 +
   1.546 +      // If we attempt to coalesce across a bound def
   1.547 +      if( lrgs(lidx).is_bound() ) {
   1.548 +        // Do not let the coalesced LRG expect to get the bound color
   1.549 +        rm.SUBTRACT( lrgs(lidx).mask() );
   1.550 +        // Recompute rm_size
   1.551 +        rm_size = rm.Size();
   1.552 +        //if( rm._flags ) rm_size += 1000000;
   1.553 +        if( reg_degree >= rm_size ) return max_juint;
   1.554 +      }
   1.555 +      if( rm.overlap(lrgs(lidx).mask()) ) {
   1.556 +        // Insert lidx into union LRG; returns TRUE if actually inserted
   1.557 +        if( _ulr.insert(lidx) ) {
   1.558 +          // Infinite-stack neighbors do not alter colorability, as they
   1.559 +          // can always color to some other color.
   1.560 +          if( !lrgs(lidx).mask().is_AllStack() ) {
   1.561 +            // If this coalesce will make any new neighbor uncolorable,
   1.562 +            // do not coalesce.
   1.563 +            if( lrgs(lidx).just_lo_degree() )
   1.564 +              return max_juint;
   1.565 +            // Bump our degree
   1.566 +            if( ++reg_degree >= rm_size )
   1.567 +              return max_juint;
   1.568 +          } // End of if not infinite-stack neighbor
   1.569 +        } // End of if actually inserted
   1.570 +      } // End of if live range overlaps
   1.571 +    } // End of else collect interferences for 1 node
   1.572 +  } // End of while forever, scan back for interferences
   1.573 +  return reg_degree;
   1.574 +}
   1.575 +
   1.576 +void PhaseConservativeCoalesce::update_ifg(uint lr1, uint lr2, IndexSet *n_lr1, IndexSet *n_lr2) {
   1.577 +  // Some original neighbors of lr1 might have gone away
   1.578 +  // because the constrained register mask prevented them.
   1.579 +  // Remove lr1 from such neighbors.
   1.580 +  IndexSetIterator one(n_lr1);
   1.581 +  uint neighbor;
   1.582 +  LRG &lrg1 = lrgs(lr1);
   1.583 +  while ((neighbor = one.next()) != 0)
   1.584 +    if( !_ulr.member(neighbor) )
   1.585 +      if( _phc._ifg->neighbors(neighbor)->remove(lr1) )
   1.586 +        lrgs(neighbor).inc_degree( -lrg1.compute_degree(lrgs(neighbor)) );
   1.587 +
   1.588 +
   1.589 +  // lr2 is now called (coalesced into) lr1.
   1.590 +  // Remove lr2 from the IFG.
   1.591 +  IndexSetIterator two(n_lr2);
   1.592 +  LRG &lrg2 = lrgs(lr2);
   1.593 +  while ((neighbor = two.next()) != 0)
   1.594 +    if( _phc._ifg->neighbors(neighbor)->remove(lr2) )
   1.595 +      lrgs(neighbor).inc_degree( -lrg2.compute_degree(lrgs(neighbor)) );
   1.596 +
   1.597 +  // Some neighbors of intermediate copies now interfere with the
   1.598 +  // combined live range.
   1.599 +  IndexSetIterator three(&_ulr);
   1.600 +  while ((neighbor = three.next()) != 0)
   1.601 +    if( _phc._ifg->neighbors(neighbor)->insert(lr1) )
   1.602 +      lrgs(neighbor).inc_degree( lrg1.compute_degree(lrgs(neighbor)) );
   1.603 +}
   1.604 +
   1.605 +static void record_bias( const PhaseIFG *ifg, int lr1, int lr2 ) {
   1.606 +  // Tag copy bias here
   1.607 +  if( !ifg->lrgs(lr1)._copy_bias )
   1.608 +    ifg->lrgs(lr1)._copy_bias = lr2;
   1.609 +  if( !ifg->lrgs(lr2)._copy_bias )
   1.610 +    ifg->lrgs(lr2)._copy_bias = lr1;
   1.611 +}
   1.612 +
   1.613 +// See if I can coalesce a series of multiple copies together.  I need the
   1.614 +// final dest copy and the original src copy.  They can be the same Node.
   1.615 +// Compute the compatible register masks.
   1.616 +bool PhaseConservativeCoalesce::copy_copy(Node *dst_copy, Node *src_copy, Block *b, uint bindex) {
   1.617 +
   1.618 +  if (!dst_copy->is_SpillCopy()) {
   1.619 +    return false;
   1.620 +  }
   1.621 +  if (!src_copy->is_SpillCopy()) {
   1.622 +    return false;
   1.623 +  }
   1.624 +  Node *src_def = src_copy->in(src_copy->is_Copy());
   1.625 +  uint lr1 = _phc._lrg_map.find(dst_copy);
   1.626 +  uint lr2 = _phc._lrg_map.find(src_def);
   1.627 +
   1.628 +  // Same live ranges already?
   1.629 +  if (lr1 == lr2) {
   1.630 +    return false;
   1.631 +  }
   1.632 +
   1.633 +  // Interfere?
   1.634 +  if (_phc._ifg->test_edge_sq(lr1, lr2)) {
   1.635 +    return false;
   1.636 +  }
   1.637 +
   1.638 +  // Not an oop->int cast; oop->oop, int->int, AND int->oop are OK.
   1.639 +  if (!lrgs(lr1)._is_oop && lrgs(lr2)._is_oop) { // not an oop->int cast
   1.640 +    return false;
   1.641 +  }
   1.642 +
   1.643 +  // Coalescing between an aligned live range and a mis-aligned live range?
   1.644 +  // No, no!  Alignment changes how we count degree.
   1.645 +  if (lrgs(lr1)._fat_proj != lrgs(lr2)._fat_proj) {
   1.646 +    return false;
   1.647 +  }
   1.648 +
   1.649 +  // Sort; use smaller live-range number
   1.650 +  Node *lr1_node = dst_copy;
   1.651 +  Node *lr2_node = src_def;
   1.652 +  if (lr1 > lr2) {
   1.653 +    uint tmp = lr1; lr1 = lr2; lr2 = tmp;
   1.654 +    lr1_node = src_def;  lr2_node = dst_copy;
   1.655 +  }
   1.656 +
   1.657 +  // Check for compatibility of the 2 live ranges by
   1.658 +  // intersecting their allowed register sets.
   1.659 +  RegMask rm = lrgs(lr1).mask();
   1.660 +  rm.AND(lrgs(lr2).mask());
   1.661 +  // Number of bits free
   1.662 +  uint rm_size = rm.Size();
   1.663 +
   1.664 +  if (UseFPUForSpilling && rm.is_AllStack() ) {
   1.665 +    // Don't coalesce when frequency difference is large
   1.666 +    Block *dst_b = _phc._cfg.get_block_for_node(dst_copy);
   1.667 +    Block *src_def_b = _phc._cfg.get_block_for_node(src_def);
   1.668 +    if (src_def_b->_freq > 10*dst_b->_freq )
   1.669 +      return false;
   1.670 +  }
   1.671 +
   1.672 +  // If we can use any stack slot, then effective size is infinite
   1.673 +  if( rm.is_AllStack() ) rm_size += 1000000;
   1.674 +  // Incompatible masks, no way to coalesce
   1.675 +  if( rm_size == 0 ) return false;
   1.676 +
   1.677 +  // Another early bail-out test is when we are double-coalescing and the
   1.678 +  // 2 copies are separated by some control flow.
   1.679 +  if( dst_copy != src_copy ) {
   1.680 +    Block *src_b = _phc._cfg.get_block_for_node(src_copy);
   1.681 +    Block *b2 = b;
   1.682 +    while( b2 != src_b ) {
   1.683 +      if( b2->num_preds() > 2 ){// Found merge-point
   1.684 +        _phc._lost_opp_cflow_coalesce++;
   1.685 +        // extra record_bias commented out because Chris believes it is not
   1.686 +        // productive.  Since we can record only 1 bias, we want to choose one
   1.687 +        // that stands a chance of working and this one probably does not.
   1.688 +        //record_bias( _phc._lrgs, lr1, lr2 );
   1.689 +        return false;           // To hard to find all interferences
   1.690 +      }
   1.691 +      b2 = _phc._cfg.get_block_for_node(b2->pred(1));
   1.692 +    }
   1.693 +  }
   1.694 +
   1.695 +  // Union the two interference sets together into '_ulr'
   1.696 +  uint reg_degree = _ulr.lrg_union( lr1, lr2, rm_size, _phc._ifg, rm );
   1.697 +
   1.698 +  if( reg_degree >= rm_size ) {
   1.699 +    record_bias( _phc._ifg, lr1, lr2 );
   1.700 +    return false;
   1.701 +  }
   1.702 +
   1.703 +  // Now I need to compute all the interferences between dst_copy and
   1.704 +  // src_copy.  I'm not willing visit the entire interference graph, so
   1.705 +  // I limit my search to things in dst_copy's block or in a straight
   1.706 +  // line of previous blocks.  I give up at merge points or when I get
   1.707 +  // more interferences than my degree.  I can stop when I find src_copy.
   1.708 +  if( dst_copy != src_copy ) {
   1.709 +    reg_degree = compute_separating_interferences(dst_copy, src_copy, b, bindex, rm, rm_size, reg_degree, lr1, lr2 );
   1.710 +    if( reg_degree == max_juint ) {
   1.711 +      record_bias( _phc._ifg, lr1, lr2 );
   1.712 +      return false;
   1.713 +    }
   1.714 +  } // End of if dst_copy & src_copy are different
   1.715 +
   1.716 +
   1.717 +  // ---- THE COMBINED LRG IS COLORABLE ----
   1.718 +
   1.719 +  // YEAH - Now coalesce this copy away
   1.720 +  assert( lrgs(lr1).num_regs() == lrgs(lr2).num_regs(),   "" );
   1.721 +
   1.722 +  IndexSet *n_lr1 = _phc._ifg->neighbors(lr1);
   1.723 +  IndexSet *n_lr2 = _phc._ifg->neighbors(lr2);
   1.724 +
   1.725 +  // Update the interference graph
   1.726 +  update_ifg(lr1, lr2, n_lr1, n_lr2);
   1.727 +
   1.728 +  _ulr.remove(lr1);
   1.729 +
   1.730 +  // Uncomment the following code to trace Coalescing in great detail.
   1.731 +  //
   1.732 +  //if (false) {
   1.733 +  //  tty->cr();
   1.734 +  //  tty->print_cr("#######################################");
   1.735 +  //  tty->print_cr("union %d and %d", lr1, lr2);
   1.736 +  //  n_lr1->dump();
   1.737 +  //  n_lr2->dump();
   1.738 +  //  tty->print_cr("resulting set is");
   1.739 +  //  _ulr.dump();
   1.740 +  //}
   1.741 +
   1.742 +  // Replace n_lr1 with the new combined live range.  _ulr will use
   1.743 +  // n_lr1's old memory on the next iteration.  n_lr2 is cleared to
   1.744 +  // send its internal memory to the free list.
   1.745 +  _ulr.swap(n_lr1);
   1.746 +  _ulr.clear();
   1.747 +  n_lr2->clear();
   1.748 +
   1.749 +  lrgs(lr1).set_degree( _phc._ifg->effective_degree(lr1) );
   1.750 +  lrgs(lr2).set_degree( 0 );
   1.751 +
   1.752 +  // Join live ranges.  Merge larger into smaller.  Union lr2 into lr1 in the
   1.753 +  // union-find tree
   1.754 +  union_helper( lr1_node, lr2_node, lr1, lr2, src_def, dst_copy, src_copy, b, bindex );
   1.755 +  // Combine register restrictions
   1.756 +  lrgs(lr1).set_mask(rm);
   1.757 +  lrgs(lr1).compute_set_mask_size();
   1.758 +  lrgs(lr1)._cost += lrgs(lr2)._cost;
   1.759 +  lrgs(lr1)._area += lrgs(lr2)._area;
   1.760 +
   1.761 +  // While its uncommon to successfully coalesce live ranges that started out
   1.762 +  // being not-lo-degree, it can happen.  In any case the combined coalesced
   1.763 +  // live range better Simplify nicely.
   1.764 +  lrgs(lr1)._was_lo = 1;
   1.765 +
   1.766 +  // kinda expensive to do all the time
   1.767 +  //tty->print_cr("warning: slow verify happening");
   1.768 +  //_phc._ifg->verify( &_phc );
   1.769 +  return true;
   1.770 +}
   1.771 +
   1.772 +// Conservative (but pessimistic) copy coalescing of a single block
   1.773 +void PhaseConservativeCoalesce::coalesce( Block *b ) {
   1.774 +  // Bail out on infrequent blocks
   1.775 +  if (_phc._cfg.is_uncommon(b)) {
   1.776 +    return;
   1.777 +  }
   1.778 +  // Check this block for copies.
   1.779 +  for( uint i = 1; i<b->end_idx(); i++ ) {
   1.780 +    // Check for actual copies on inputs.  Coalesce a copy into its
   1.781 +    // input if use and copy's input are compatible.
   1.782 +    Node *copy1 = b->get_node(i);
   1.783 +    uint idx1 = copy1->is_Copy();
   1.784 +    if( !idx1 ) continue;       // Not a copy
   1.785 +
   1.786 +    if( copy_copy(copy1,copy1,b,i) ) {
   1.787 +      i--;                      // Retry, same location in block
   1.788 +      PhaseChaitin::_conserv_coalesce++;  // Collect stats on success
   1.789 +      continue;
   1.790 +    }
   1.791 +  }
   1.792 +}

mercurial