chaitin.hpp revision 196
/*
* Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
class LoopTree;
class MachCallNode;
class MachSafePointNode;
class Matcher;
class PhaseCFG;
class PhaseLive;
class PhaseRegAlloc;
class PhaseChaitin;
//------------------------------LRG--------------------------------------------
// Live-RanGe structure.
class LRG : public ResourceObj {
public:
double _cost; // 2 for loads/1 for stores times block freq
double _area; // Sum of all simultaneously live values
double score() const; // Compute score from cost and area
double _maxfreq; // Maximum frequency of any def or use
#ifndef PRODUCT
#endif
private:
public:
// Return chosen register for this LRG. Error if the LRG is not bound to
// a single register.
private:
public:
// Degree starts not valid and any change to the IFG neighbor
// set makes it not valid.
// Made a change that hammered degree
// Incrementally modify degree. If it was correct, it should remain correct
// Compute the degree between 2 live ranges
int compute_degree( LRG &l ) const;
private:
public:
void set_mask_size( int size ) {
_mask_size = size;
}
return _mask_size; }
// Get the last mask size computed, even if it does not match the
// count of bits in the current mask.
int get_invalid_mask_size() const { return _mask_size; }
// Number of registers this live range uses when it colors
private:
// except _num_regs is kill count for fat_proj
public:
private:
// Number of physical registers this live range uses when it colors
// Architecture and register-set dependent
public:
void set_reg_pressure(int i) { _reg_pressure = i; }
int reg_pressure() const { return _reg_pressure; }
// How much 'wiggle room' does this live range have?
// How many color choices can it make (scaled by _num_regs)?
// Bound LRGs have ZERO degrees of freedom. We also count
// must_spill as bound.
// Negative degrees-of-freedom; even with no neighbors this
// live range must spill.
bool not_free() const { return degrees_of_freedom() < 0; }
// Is this live range of "low-degree"? Trivially colorable?
// Is this live range just barely "low-degree"? Trivially colorable?
// degrees of freedom.
// If _fat_proj is set, live range does NOT require aligned, adjacent
// registers and has NO interferences.
// If _fat_proj is clear, live range requires num_regs() to be a power of
// 2, and it requires registers to form an aligned, adjacent set.
// Alive if non-zero, dead if zero
#ifndef PRODUCT
void dump( ) const;
#endif
};
//------------------------------LRG_List---------------------------------------
// Map Node indices to Live RanGe indices.
// Array lookup in the optimized case.
class LRG_List : public ResourceObj {
public:
}
}
};
//------------------------------IFG--------------------------------------------
// InterFerence Graph
// An undirected graph implementation. Created with a fixed number of
// vertices. Edges can be added & tested. Vertices can be removed, then
// added back later with all edges intact. Can add edges between one vertex
// and a list of other vertices. Can union vertices (and their edges)
// together. The IFG needs to be really really fast, and also fairly
// abstract! It needs abstraction so I can fiddle with the implementation to
// get even more speed.
// Current implementation: a triangular adjacency list.
// Array of adjacency-lists, indexed by live-range number
// Assertion bit for proper use of Squaring
bool _is_square;
// Live range structure goes here
public:
// Largest live-range number
// Keep track of inserted and deleted Nodes
// Add edge between a and b. Returns true if actually addded.
// Add edge between a and everything in the vector
// Test for edge existance
// Square-up matrix for faster Union
void SquareUp();
// Return number of LRG neighbors
// Union edges of b into a on Squared-up matrix
// Test for edge in Squared-up matrix
// Yank a Node and all connected edges from the IFG. Be prepared to
// re-insert the yanked Node in reverse order of yanking. Return a
// list of neighbors (edges) yanked.
// Reinsert a yanked Node
// Return set of neighbors
#ifndef PRODUCT
// Dump the IFG
void dump() const;
void stats() const;
void verify( const PhaseChaitin * ) const;
#endif
//--------------- Live Range Accessors
// Compute and set effective degree. Might be folded into SquareUp().
void Compute_Effective_Degree();
// Compute effective degree as the sum of neighbors' _sizes.
};
// TEMPORARILY REPLACED WITH COMMAND LINE FLAG
//// !!!!! Magic Constants need to move into ad file
#ifdef SPARC
//#define FLOAT_PRESSURE 30 /* SFLT_REG_mask.Size() - 1 */
//#define INT_PRESSURE 23 /* NOTEMP_I_REG_mask.Size() - 1 */
#else
//#define FLOAT_PRESSURE 6
//#define INT_PRESSURE 6
#endif
//------------------------------Chaitin----------------------------------------
// Briggs-Chaitin style allocation, mostly.
class PhaseChaitin : public PhaseRegAlloc {
int _trip_cnt;
int _alternate;
// Union-find map. Declared as a short for speed.
// Indexed by live-range number, it returns the compacted live-range number
// Reset the Union-Find map to identity
// Remove the need for the Union-Find mapping
void compress_uf_map_for_nodes( );
// Combine the Live Range Indices for these 2 Nodes into a single live
// range. Future requests for any Node in either live range will
// return the live range index for the combined live range.
// Compact live ranges, removing unused ones. Return new maxlrg.
void compact();
// Helper functions for Split()
uint split_DEF( Node *def, Block *b, int loc, uint max, Node **Reachblock, Node **debug_defs, GrowableArray<uint> splits, int slidx );
uint split_USE( Node *def, Block *b, Node *use, uint useidx, uint max, bool def_down, bool cisc_sp, GrowableArray<uint> splits, int slidx );
Node *split_Rematerialize( Node *def, Block *b, uint insidx, uint &maxlrg, GrowableArray<uint> splits, int slidx, uint *lrg2reach, Node **Reachblock, bool walkThru );
// True if lidx is used before any real register is def'd in the block
// Insert the spill at chosen location. Skip over any interveneing Proj's or
// Phis. Skip over a CatchNode and projs, inserting in the fall-through block
// instead. Update high-pressure indices. Create a new live range.
#ifndef PRODUCT
bool _trace_spilling;
#endif
public:
~PhaseChaitin() {}
// Convert a Node into a Live Range Index - a lidx
}
// Do all the real work of allocate
void Register_Allocate();
#ifndef PRODUCT
bool trace_spilling() const { return _trace_spilling; }
#endif
private:
// De-SSA the world. Assign registers to Nodes. Use the same register for
// all inputs to a PhiNode, effectively coalescing live ranges. Insert
// copies as needed.
void de_ssa();
}
return retval;
}
// Add edge between reg and everything in the vector.
// Same as _ifg->add_vector(reg,live) EXCEPT use the RegMask
// information to trim the set of interferences. Return the
// count of edges added.
// Count register pressure for asserts
// Build the interference graph using virtual registers only.
// Used for aggressive coalescing.
void build_ifg_virtual( );
// Build the interference graph using physical registers when available.
// That is, if 2 live ranges are simultaneously alive but in their
// acceptable register sets do not overlap, then they do not interfere.
// Gather LiveRanGe information, including register masks and base pointer/
// derived pointer relationships.
void gather_lrg_masks( bool mod_cisc_masks );
// Force the bases of derived pointers to be alive at GC points.
bool stretch_base_pointer_live_ranges( ResourceArea *a );
// 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.
// 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.
void set_was_low();
// Split live-ranges that must spill due to register conflicts (as opposed
// to capacity spills). Typically these are things def'd in a register
// and used on the stack or vice-versa.
void pre_spill();
// Init LRG caching of degree, numregs. Init lo_degree list.
void cache_lrg_info( );
// Simplify the IFG by removing LRGs of low degree with no copies
void Pre_Simplify();
// Simplify the IFG by removing LRGs of low degree
void Simplify();
// Select colors by re-inserting edges into the IFG.
// Return TRUE if any spills occured.
// Helper function for select which allows biased coloring
// Helper function which implements biasing heuristic
// Split uncolorable live ranges
// Return new number of live ranges
// Copy 'was_spilled'-edness from one Node to another.
// Set the 'spilled_once' or 'spilled_twice' flag on a node.
void set_was_spilled( Node *n );
// Convert ideal spill-nodes into machine loads & stores
// Set C->failing when fixup spills could not complete, node limit exceeded.
void fixup_spills();
// Post-Allocation peephole copy removal
void post_allocate_copy_removal();
int elide_copy( Node *n, int k, Block *current_block, Node_List &value, Node_List ®nd, bool can_change_regs );
int use_prior_register( Node *copy, uint idx, Node *def, Block *current_block, Node_List &value, Node_List ®nd );
// If nreg already contains the same constant as val then eliminate it
// Extend the node to LRG mapping
private:
static int _conserv_coalesce, _conserv_coalesce_pair;
static int _conserv_coalesce_trie, _conserv_coalesce_quad;
static int _post_alloc;
static int _lost_opp_pp_coalesce, _lost_opp_cflow_coalesce;
static int _used_cisc_instructions, _unused_cisc_instructions;
static int _allocator_attempts, _allocator_successes;
#ifndef PRODUCT
void dump() const;
void dump_degree_lists() const;
void dump_simplified() const;
// Verify that base pointers and derived pointers are still sane
void verify_base_ptrs( ResourceArea *a ) const;
void dump_for_spill_split_recycle() const;
public:
void dump_frame() const;
private:
static void print_chaitin_statistics();
#endif
friend class PhaseCoalesce;
friend class PhaseAggressiveCoalesce;
friend class PhaseConservativeCoalesce;
};