src/share/vm/opto/chaitin.hpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 1907
c18cbe5936b8
child 2358
361783318e7e
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

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

mercurial