phaseX.hpp revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 1997, 2009, 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 */
24
25class Compile;
26class ConINode;
27class ConLNode;
28class Node;
29class Type;
30class PhaseTransform;
31class   PhaseGVN;
32class     PhaseIterGVN;
33class       PhaseCCP;
34class   PhasePeephole;
35class   PhaseRegAlloc;
36
37
38//-----------------------------------------------------------------------------
39// Expandable closed hash-table of nodes, initialized to NULL.
40// Note that the constructor just zeros things
41// Storage is reclaimed when the Arena's lifetime is over.
42class NodeHash : public StackObj {
43protected:
44  Arena *_a;                    // Arena to allocate in
45  uint   _max;                  // Size of table (power of 2)
46  uint   _inserts;              // For grow and debug, count of hash_inserts
47  uint   _insert_limit;         // 'grow' when _inserts reaches _insert_limit
48  Node **_table;                // Hash table of Node pointers
49  Node  *_sentinel;             // Replaces deleted entries in hash table
50
51public:
52  NodeHash(uint est_max_size);
53  NodeHash(Arena *arena, uint est_max_size);
54  NodeHash(NodeHash *use_this_state);
55#ifdef ASSERT
56  ~NodeHash();                  // Unlock all nodes upon destruction of table.
57  void operator=(const NodeHash&); // Unlock all nodes upon replacement of table.
58#endif
59  Node  *hash_find(const Node*);// Find an equivalent version in hash table
60  Node  *hash_find_insert(Node*);// If not in table insert else return found node
61  void   hash_insert(Node*);    // Insert into hash table
62  bool   hash_delete(const Node*);// Replace with _sentinel in hash table
63  void   check_grow() {
64    _inserts++;
65    if( _inserts == _insert_limit ) { grow(); }
66    assert( _inserts <= _insert_limit, "hash table overflow");
67    assert( _inserts < _max, "hash table overflow" );
68  }
69  static uint round_up(uint);   // Round up to nearest power of 2
70  void   grow();                // Grow _table to next power of 2 and rehash
71  // Return 75% of _max, rounded up.
72  uint   insert_limit() const { return _max - (_max>>2); }
73
74  void   clear();               // Set all entries to NULL, keep storage.
75  // Size of hash table
76  uint   size()         const { return _max; }
77  // Return Node* at index in table
78  Node  *at(uint table_index) {
79    assert(table_index < _max, "Must be within table");
80    return _table[table_index];
81  }
82
83  void   remove_useless_nodes(VectorSet &useful); // replace with sentinel
84
85  Node  *sentinel() { return _sentinel; }
86
87#ifndef PRODUCT
88  Node  *find_index(uint idx);  // For debugging
89  void   dump();                // For debugging, dump statistics
90#endif
91  uint   _grows;                // For debugging, count of table grow()s
92  uint   _look_probes;          // For debugging, count of hash probes
93  uint   _lookup_hits;          // For debugging, count of hash_finds
94  uint   _lookup_misses;        // For debugging, count of hash_finds
95  uint   _insert_probes;        // For debugging, count of hash probes
96  uint   _delete_probes;        // For debugging, count of hash probes for deletes
97  uint   _delete_hits;          // For debugging, count of hash probes for deletes
98  uint   _delete_misses;        // For debugging, count of hash probes for deletes
99  uint   _total_inserts;        // For debugging, total inserts into hash table
100  uint   _total_insert_probes;  // For debugging, total probes while inserting
101};
102
103
104//-----------------------------------------------------------------------------
105// Map dense integer indices to Types.  Uses classic doubling-array trick.
106// Abstractly provides an infinite array of Type*'s, initialized to NULL.
107// Note that the constructor just zeros things, and since I use Arena
108// allocation I do not need a destructor to reclaim storage.
109// Despite the general name, this class is customized for use by PhaseTransform.
110class Type_Array : public StackObj {
111  Arena *_a;                    // Arena to allocate in
112  uint   _max;
113  const Type **_types;
114  void grow( uint i );          // Grow array node to fit
115  const Type *operator[] ( uint i ) const // Lookup, or NULL for not mapped
116  { return (i<_max) ? _types[i] : (Type*)NULL; }
117  friend class PhaseTransform;
118public:
119  Type_Array(Arena *a) : _a(a), _max(0), _types(0) {}
120  Type_Array(Type_Array *ta) : _a(ta->_a), _max(ta->_max), _types(ta->_types) { }
121  const Type *fast_lookup(uint i) const{assert(i<_max,"oob");return _types[i];}
122  // Extend the mapping: index i maps to Type *n.
123  void map( uint i, const Type *n ) { if( i>=_max ) grow(i); _types[i] = n; }
124  uint Size() const { return _max; }
125#ifndef PRODUCT
126  void dump() const;
127#endif
128};
129
130
131//------------------------------PhaseRemoveUseless-----------------------------
132// Remove useless nodes from GVN hash-table, worklist, and graph
133class PhaseRemoveUseless : public Phase {
134protected:
135  Unique_Node_List _useful;   // Nodes reachable from root
136                              // list is allocated from current resource area
137public:
138  PhaseRemoveUseless( PhaseGVN *gvn, Unique_Node_List *worklist );
139
140  Unique_Node_List *get_useful() { return &_useful; }
141};
142
143
144//------------------------------PhaseTransform---------------------------------
145// Phases that analyze, then transform.  Constructing the Phase object does any
146// global or slow analysis.  The results are cached later for a fast
147// transformation pass.  When the Phase object is deleted the cached analysis
148// results are deleted.
149class PhaseTransform : public Phase {
150protected:
151  Arena*     _arena;
152  Node_Array _nodes;           // Map old node indices to new nodes.
153  Type_Array _types;           // Map old node indices to Types.
154
155  // ConNode caches:
156  enum { _icon_min = -1 * HeapWordSize,
157         _icon_max = 16 * HeapWordSize,
158         _lcon_min = _icon_min,
159         _lcon_max = _icon_max,
160         _zcon_max = (uint)T_CONFLICT
161  };
162  ConINode* _icons[_icon_max - _icon_min + 1];   // cached jint constant nodes
163  ConLNode* _lcons[_lcon_max - _lcon_min + 1];   // cached jlong constant nodes
164  ConNode*  _zcons[_zcon_max + 1];               // cached is_zero_type nodes
165  void init_con_caches();
166
167  // Support both int and long caches because either might be an intptr_t,
168  // so they show up frequently in address computations.
169
170public:
171  PhaseTransform( PhaseNumber pnum );
172  PhaseTransform( Arena *arena, PhaseNumber pnum );
173  PhaseTransform( PhaseTransform *phase, PhaseNumber pnum );
174
175  Arena*      arena()   { return _arena; }
176  Type_Array& types()   { return _types; }
177  // _nodes is used in varying ways by subclasses, which define local accessors
178
179public:
180  // Get a previously recorded type for the node n.
181  // This type must already have been recorded.
182  // If you want the type of a very new (untransformed) node,
183  // you must use type_or_null, and test the result for NULL.
184  const Type* type(const Node* n) const {
185    const Type* t = _types.fast_lookup(n->_idx);
186    assert(t != NULL, "must set before get");
187    return t;
188  }
189  // Get a previously recorded type for the node n,
190  // or else return NULL if there is none.
191  const Type* type_or_null(const Node* n) const {
192    return _types.fast_lookup(n->_idx);
193  }
194  // Record a type for a node.
195  void    set_type(const Node* n, const Type *t) {
196    assert(t != NULL, "type must not be null");
197    _types.map(n->_idx, t);
198  }
199  // Record an initial type for a node, the node's bottom type.
200  void    set_type_bottom(const Node* n) {
201    // Use this for initialization when bottom_type() (or better) is not handy.
202    // Usually the initialization shoudl be to n->Value(this) instead,
203    // or a hand-optimized value like Type::MEMORY or Type::CONTROL.
204    assert(_types[n->_idx] == NULL, "must set the initial type just once");
205    _types.map(n->_idx, n->bottom_type());
206  }
207  // Make sure the types array is big enough to record a size for the node n.
208  // (In product builds, we never want to do range checks on the types array!)
209  void ensure_type_or_null(const Node* n) {
210    if (n->_idx >= _types.Size())
211      _types.map(n->_idx, NULL);   // Grow the types array as needed.
212  }
213
214  // Utility functions:
215  const TypeInt*  find_int_type( Node* n);
216  const TypeLong* find_long_type(Node* n);
217  jint  find_int_con( Node* n, jint  value_if_unknown) {
218    const TypeInt* t = find_int_type(n);
219    return (t != NULL && t->is_con()) ? t->get_con() : value_if_unknown;
220  }
221  jlong find_long_con(Node* n, jlong value_if_unknown) {
222    const TypeLong* t = find_long_type(n);
223    return (t != NULL && t->is_con()) ? t->get_con() : value_if_unknown;
224  }
225
226  // Make an idealized constant, i.e., one of ConINode, ConPNode, ConFNode, etc.
227  // Same as transform(ConNode::make(t)).
228  ConNode* makecon(const Type* t);
229  virtual ConNode* uncached_makecon(const Type* t)  // override in PhaseValues
230  { ShouldNotCallThis(); return NULL; }
231
232  // Fast int or long constant.  Same as TypeInt::make(i) or TypeLong::make(l).
233  ConINode* intcon(jint i);
234  ConLNode* longcon(jlong l);
235
236  // Fast zero or null constant.  Same as makecon(Type::get_zero_type(bt)).
237  ConNode* zerocon(BasicType bt);
238
239  // Return a node which computes the same function as this node, but
240  // in a faster or cheaper fashion.
241  virtual Node *transform( Node *n ) = 0;
242
243  // Return whether two Nodes are equivalent.
244  // Must not be recursive, since the recursive version is built from this.
245  // For pessimistic optimizations this is simply pointer equivalence.
246  bool eqv(const Node* n1, const Node* n2) const { return n1 == n2; }
247
248  // Return whether two Nodes are equivalent, after stripping casting.
249  bool eqv_uncast(const Node* n1, const Node* n2) const {
250    return eqv(n1->uncast(), n2->uncast());
251  }
252
253  // For pessimistic passes, the return type must monotonically narrow.
254  // For optimistic  passes, the return type must monotonically widen.
255  // It is possible to get into a "death march" in either type of pass,
256  // where the types are continually moving but it will take 2**31 or
257  // more steps to converge.  This doesn't happen on most normal loops.
258  //
259  // Here is an example of a deadly loop for an optimistic pass, along
260  // with a partial trace of inferred types:
261  //    x = phi(0,x'); L: x' = x+1; if (x' >= 0) goto L;
262  //    0                 1                join([0..max], 1)
263  //    [0..1]            [1..2]           join([0..max], [1..2])
264  //    [0..2]            [1..3]           join([0..max], [1..3])
265  //      ... ... ...
266  //    [0..max]          [min]u[1..max]   join([0..max], [min..max])
267  //    [0..max] ==> fixpoint
268  // We would have proven, the hard way, that the iteration space is all
269  // non-negative ints, with the loop terminating due to 32-bit overflow.
270  //
271  // Here is the corresponding example for a pessimistic pass:
272  //    x = phi(0,x'); L: x' = x-1; if (x' >= 0) goto L;
273  //    int               int              join([0..max], int)
274  //    [0..max]          [-1..max-1]      join([0..max], [-1..max-1])
275  //    [0..max-1]        [-1..max-2]      join([0..max], [-1..max-2])
276  //      ... ... ...
277  //    [0..1]            [-1..0]          join([0..max], [-1..0])
278  //    0                 -1               join([0..max], -1)
279  //    0 == fixpoint
280  // We would have proven, the hard way, that the iteration space is {0}.
281  // (Usually, other optimizations will make the "if (x >= 0)" fold up
282  // before we get into trouble.  But not always.)
283  //
284  // It's a pleasant thing to observe that the pessimistic pass
285  // will make short work of the optimistic pass's deadly loop,
286  // and vice versa.  That is a good example of the complementary
287  // purposes of the CCP (optimistic) vs. GVN (pessimistic) phases.
288  //
289  // In any case, only widen or narrow a few times before going to the
290  // correct flavor of top or bottom.
291  //
292  // This call only needs to be made once as the data flows around any
293  // given cycle.  We do it at Phis, and nowhere else.
294  // The types presented are the new type of a phi (computed by PhiNode::Value)
295  // and the previously computed type, last time the phi was visited.
296  //
297  // The third argument is upper limit for the saturated value,
298  // if the phase wishes to widen the new_type.
299  // If the phase is narrowing, the old type provides a lower limit.
300  // Caller guarantees that old_type and new_type are no higher than limit_type.
301  virtual const Type* saturate(const Type* new_type, const Type* old_type,
302                               const Type* limit_type) const
303  { ShouldNotCallThis(); return NULL; }
304
305#ifndef PRODUCT
306  void dump_old2new_map() const;
307  void dump_new( uint new_lidx ) const;
308  void dump_types() const;
309  void dump_nodes_and_types(const Node *root, uint depth, bool only_ctrl = true);
310  void dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited);
311
312  uint   _count_progress;       // For profiling, count transforms that make progress
313  void   set_progress()        { ++_count_progress; assert( allow_progress(),"No progress allowed during verification"); }
314  void   clear_progress()      { _count_progress = 0; }
315  uint   made_progress() const { return _count_progress; }
316
317  uint   _count_transforms;     // For profiling, count transforms performed
318  void   set_transforms()      { ++_count_transforms; }
319  void   clear_transforms()    { _count_transforms = 0; }
320  uint   made_transforms() const{ return _count_transforms; }
321
322  bool   _allow_progress;      // progress not allowed during verification pass
323  void   set_allow_progress(bool allow) { _allow_progress = allow; }
324  bool   allow_progress()               { return _allow_progress; }
325#endif
326};
327
328//------------------------------PhaseValues------------------------------------
329// Phase infrastructure to support values
330class PhaseValues : public PhaseTransform {
331protected:
332  NodeHash  _table;             // Hash table for value-numbering
333
334public:
335  PhaseValues( Arena *arena, uint est_max_size );
336  PhaseValues( PhaseValues *pt );
337  PhaseValues( PhaseValues *ptv, const char *dummy );
338  NOT_PRODUCT( ~PhaseValues(); )
339  virtual PhaseIterGVN *is_IterGVN() { return 0; }
340
341  // Some Ideal and other transforms delete --> modify --> insert values
342  bool   hash_delete(Node *n)     { return _table.hash_delete(n); }
343  void   hash_insert(Node *n)     { _table.hash_insert(n); }
344  Node  *hash_find_insert(Node *n){ return _table.hash_find_insert(n); }
345  Node  *hash_find(const Node *n) { return _table.hash_find(n); }
346
347  // Used after parsing to eliminate values that are no longer in program
348  void   remove_useless_nodes(VectorSet &useful) {
349    _table.remove_useless_nodes(useful);
350    // this may invalidate cached cons so reset the cache
351    init_con_caches();
352  }
353
354  virtual ConNode* uncached_makecon(const Type* t);  // override from PhaseTransform
355
356  virtual const Type* saturate(const Type* new_type, const Type* old_type,
357                               const Type* limit_type) const
358  { return new_type; }
359
360#ifndef PRODUCT
361  uint   _count_new_values;     // For profiling, count new values produced
362  void    inc_new_values()        { ++_count_new_values; }
363  void    clear_new_values()      { _count_new_values = 0; }
364  uint    made_new_values() const { return _count_new_values; }
365#endif
366};
367
368
369//------------------------------PhaseGVN---------------------------------------
370// Phase for performing local, pessimistic GVN-style optimizations.
371class PhaseGVN : public PhaseValues {
372public:
373  PhaseGVN( Arena *arena, uint est_max_size ) : PhaseValues( arena, est_max_size ) {}
374  PhaseGVN( PhaseGVN *gvn ) : PhaseValues( gvn ) {}
375  PhaseGVN( PhaseGVN *gvn, const char *dummy ) : PhaseValues( gvn, dummy ) {}
376
377  // Return a node which computes the same function as this node, but
378  // in a faster or cheaper fashion.
379  Node  *transform( Node *n );
380  Node  *transform_no_reclaim( Node *n );
381
382  // Check for a simple dead loop when a data node references itself.
383  DEBUG_ONLY(void dead_loop_check(Node *n);)
384};
385
386//------------------------------PhaseIterGVN-----------------------------------
387// Phase for iteratively performing local, pessimistic GVN-style optimizations.
388// and ideal transformations on the graph.
389class PhaseIterGVN : public PhaseGVN {
390 private:
391  bool _delay_transform;  // When true simply register the node when calling transform
392                          // instead of actually optimizing it
393
394  // Idealize old Node 'n' with respect to its inputs and its value
395  virtual Node *transform_old( Node *a_node );
396protected:
397
398  // Idealize new Node 'n' with respect to its inputs and its value
399  virtual Node *transform( Node *a_node );
400
401  // Warm up hash table, type table and initial worklist
402  void init_worklist( Node *a_root );
403
404  virtual const Type* saturate(const Type* new_type, const Type* old_type,
405                               const Type* limit_type) const;
406  // Usually returns new_type.  Returns old_type if new_type is only a slight
407  // improvement, such that it would take many (>>10) steps to reach 2**32.
408
409public:
410  PhaseIterGVN( PhaseIterGVN *igvn ); // Used by CCP constructor
411  PhaseIterGVN( PhaseGVN *gvn ); // Used after Parser
412  PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ); // Used after +VerifyOpto
413
414  virtual PhaseIterGVN *is_IterGVN() { return this; }
415
416  Unique_Node_List _worklist;       // Iterative worklist
417
418  // Given def-use info and an initial worklist, apply Node::Ideal,
419  // Node::Value, Node::Identity, hash-based value numbering, Node::Ideal_DU
420  // and dominator info to a fixed point.
421  void optimize();
422
423  // Register a new node with the iter GVN pass without transforming it.
424  // Used when we need to restructure a Region/Phi area and all the Regions
425  // and Phis need to complete this one big transform before any other
426  // transforms can be triggered on the region.
427  // Optional 'orig' is an earlier version of this node.
428  // It is significant only for debugging and profiling.
429  Node* register_new_node_with_optimizer(Node* n, Node* orig = NULL);
430
431  // Kill a globally dead Node.   It is allowed to have uses which are
432  // assumed dead and left 'in limbo'.
433  void remove_globally_dead_node( Node *dead );
434
435  // Kill all inputs to a dead node, recursively making more dead nodes.
436  // The Node must be dead locally, i.e., have no uses.
437  void remove_dead_node( Node *dead ) {
438    assert(dead->outcnt() == 0 && !dead->is_top(), "node must be dead");
439    remove_globally_dead_node(dead);
440  }
441
442  // Subsume users of node 'old' into node 'nn'
443  // If no Def-Use info existed for 'nn' it will after call.
444  void subsume_node( Node *old, Node *nn );
445
446  // Add users of 'n' to worklist
447  void add_users_to_worklist0( Node *n );
448  void add_users_to_worklist ( Node *n );
449
450  // Replace old node with new one.
451  void replace_node( Node *old, Node *nn ) {
452    add_users_to_worklist(old);
453    hash_delete(old);
454    subsume_node(old, nn);
455  }
456
457  bool delay_transform() const { return _delay_transform; }
458
459  void set_delay_transform(bool delay) {
460    _delay_transform = delay;
461  }
462
463#ifndef PRODUCT
464protected:
465  // Sub-quadratic implementation of VerifyIterativeGVN.
466  unsigned long _verify_counter;
467  unsigned long _verify_full_passes;
468  enum { _verify_window_size = 30 };
469  Node* _verify_window[_verify_window_size];
470  void verify_step(Node* n);
471#endif
472};
473
474//------------------------------PhaseCCP---------------------------------------
475// Phase for performing global Conditional Constant Propagation.
476// Should be replaced with combined CCP & GVN someday.
477class PhaseCCP : public PhaseIterGVN {
478  // Non-recursive.  Use analysis to transform single Node.
479  virtual Node *transform_once( Node *n );
480
481public:
482  PhaseCCP( PhaseIterGVN *igvn ); // Compute conditional constants
483  NOT_PRODUCT( ~PhaseCCP(); )
484
485  // Worklist algorithm identifies constants
486  void analyze();
487  // Recursive traversal of program.  Used analysis to modify program.
488  virtual Node *transform( Node *n );
489  // Do any transformation after analysis
490  void          do_transform();
491
492  virtual const Type* saturate(const Type* new_type, const Type* old_type,
493                               const Type* limit_type) const;
494  // Returns new_type->widen(old_type), which increments the widen bits until
495  // giving up with TypeInt::INT or TypeLong::LONG.
496  // Result is clipped to limit_type if necessary.
497
498#ifndef PRODUCT
499  static uint _total_invokes;    // For profiling, count invocations
500  void    inc_invokes()          { ++PhaseCCP::_total_invokes; }
501
502  static uint _total_constants;  // For profiling, count constants found
503  uint   _count_constants;
504  void    clear_constants()      { _count_constants = 0; }
505  void    inc_constants()        { ++_count_constants; }
506  uint    count_constants() const { return _count_constants; }
507
508  static void print_statistics();
509#endif
510};
511
512
513//------------------------------PhasePeephole----------------------------------
514// Phase for performing peephole optimizations on register allocated basic blocks.
515class PhasePeephole : public PhaseTransform {
516  PhaseRegAlloc *_regalloc;
517  PhaseCFG     &_cfg;
518  // Recursive traversal of program.  Pure function is unused in this phase
519  virtual Node *transform( Node *n );
520
521public:
522  PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg );
523  NOT_PRODUCT( ~PhasePeephole(); )
524
525  // Do any transformation after analysis
526  void          do_transform();
527
528#ifndef PRODUCT
529  static uint _total_peepholes;  // For profiling, count peephole rules applied
530  uint   _count_peepholes;
531  void    clear_peepholes()      { _count_peepholes = 0; }
532  void    inc_peepholes()        { ++_count_peepholes; }
533  uint    count_peepholes() const { return _count_peepholes; }
534
535  static void print_statistics();
536#endif
537};
538