comparison src/share/vm/opto/phaseX.hpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children a8880a78d355
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 1997-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 class Compile;
26 class ConINode;
27 class ConLNode;
28 class Node;
29 class Type;
30 class PhaseTransform;
31 class PhaseGVN;
32 class PhaseIterGVN;
33 class PhaseCCP;
34 class PhasePeephole;
35 class 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.
42 class NodeHash : public StackObj {
43 protected:
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
51 public:
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.
110 class 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;
118 public:
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
133 class PhaseRemoveUseless : public Phase {
134 protected:
135 Unique_Node_List _useful; // Nodes reachable from root
136 // list is allocated from current resource area
137 public:
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.
149 class PhaseTransform : public Phase {
150 protected:
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
170 public:
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
179 public:
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
330 class PhaseValues : public PhaseTransform {
331 protected:
332 NodeHash _table; // Hash table for value-numbering
333
334 public:
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) { _table.remove_useless_nodes(useful); }
349
350 virtual ConNode* uncached_makecon(const Type* t); // override from PhaseTransform
351
352 virtual const Type* saturate(const Type* new_type, const Type* old_type,
353 const Type* limit_type) const
354 { return new_type; }
355
356 #ifndef PRODUCT
357 uint _count_new_values; // For profiling, count new values produced
358 void inc_new_values() { ++_count_new_values; }
359 void clear_new_values() { _count_new_values = 0; }
360 uint made_new_values() const { return _count_new_values; }
361 #endif
362 };
363
364
365 //------------------------------PhaseGVN---------------------------------------
366 // Phase for performing local, pessimistic GVN-style optimizations.
367 class PhaseGVN : public PhaseValues {
368 public:
369 PhaseGVN( Arena *arena, uint est_max_size ) : PhaseValues( arena, est_max_size ) {}
370 PhaseGVN( PhaseGVN *gvn ) : PhaseValues( gvn ) {}
371 PhaseGVN( PhaseGVN *gvn, const char *dummy ) : PhaseValues( gvn, dummy ) {}
372
373 // Return a node which computes the same function as this node, but
374 // in a faster or cheaper fashion.
375 Node *transform( Node *n );
376 Node *transform_no_reclaim( Node *n );
377
378 // Check for a simple dead loop when a data node references itself.
379 DEBUG_ONLY(void dead_loop_check(Node *n);)
380 };
381
382 //------------------------------PhaseIterGVN-----------------------------------
383 // Phase for iteratively performing local, pessimistic GVN-style optimizations.
384 // and ideal transformations on the graph.
385 class PhaseIterGVN : public PhaseGVN {
386 // Idealize old Node 'n' with respect to its inputs and its value
387 virtual Node *transform_old( Node *a_node );
388 protected:
389
390 // Idealize new Node 'n' with respect to its inputs and its value
391 virtual Node *transform( Node *a_node );
392
393 // Warm up hash table, type table and initial worklist
394 void init_worklist( Node *a_root );
395
396 virtual const Type* saturate(const Type* new_type, const Type* old_type,
397 const Type* limit_type) const;
398 // Usually returns new_type. Returns old_type if new_type is only a slight
399 // improvement, such that it would take many (>>10) steps to reach 2**32.
400
401 public:
402 PhaseIterGVN( PhaseIterGVN *igvn ); // Used by CCP constructor
403 PhaseIterGVN( PhaseGVN *gvn ); // Used after Parser
404 PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ); // Used after +VerifyOpto
405
406 virtual PhaseIterGVN *is_IterGVN() { return this; }
407
408 Unique_Node_List _worklist; // Iterative worklist
409
410 // Given def-use info and an initial worklist, apply Node::Ideal,
411 // Node::Value, Node::Identity, hash-based value numbering, Node::Ideal_DU
412 // and dominator info to a fixed point.
413 void optimize();
414
415 // Register a new node with the iter GVN pass without transforming it.
416 // Used when we need to restructure a Region/Phi area and all the Regions
417 // and Phis need to complete this one big transform before any other
418 // transforms can be triggered on the region.
419 // Optional 'orig' is an earlier version of this node.
420 // It is significant only for debugging and profiling.
421 Node* register_new_node_with_optimizer(Node* n, Node* orig = NULL);
422
423 // Kill a globally dead Node. It is allowed to have uses which are
424 // assumed dead and left 'in limbo'.
425 void remove_globally_dead_node( Node *dead );
426
427 // Kill all inputs to a dead node, recursively making more dead nodes.
428 // The Node must be dead locally, i.e., have no uses.
429 void remove_dead_node( Node *dead ) {
430 assert(dead->outcnt() == 0 && !dead->is_top(), "node must be dead");
431 remove_globally_dead_node(dead);
432 }
433
434 // Subsume users of node 'old' into node 'nn'
435 // If no Def-Use info existed for 'nn' it will after call.
436 void subsume_node( Node *old, Node *nn );
437
438 // Add users of 'n' to worklist
439 void add_users_to_worklist0( Node *n );
440 void add_users_to_worklist ( Node *n );
441
442 #ifndef PRODUCT
443 protected:
444 // Sub-quadratic implementation of VerifyIterativeGVN.
445 unsigned long _verify_counter;
446 unsigned long _verify_full_passes;
447 enum { _verify_window_size = 30 };
448 Node* _verify_window[_verify_window_size];
449 void verify_step(Node* n);
450 #endif
451 };
452
453 //------------------------------PhaseCCP---------------------------------------
454 // Phase for performing global Conditional Constant Propagation.
455 // Should be replaced with combined CCP & GVN someday.
456 class PhaseCCP : public PhaseIterGVN {
457 // Non-recursive. Use analysis to transform single Node.
458 virtual Node *transform_once( Node *n );
459
460 public:
461 PhaseCCP( PhaseIterGVN *igvn ); // Compute conditional constants
462 NOT_PRODUCT( ~PhaseCCP(); )
463
464 // Worklist algorithm identifies constants
465 void analyze();
466 // Recursive traversal of program. Used analysis to modify program.
467 virtual Node *transform( Node *n );
468 // Do any transformation after analysis
469 void do_transform();
470
471 virtual const Type* saturate(const Type* new_type, const Type* old_type,
472 const Type* limit_type) const;
473 // Returns new_type->widen(old_type), which increments the widen bits until
474 // giving up with TypeInt::INT or TypeLong::LONG.
475 // Result is clipped to limit_type if necessary.
476
477 #ifndef PRODUCT
478 static uint _total_invokes; // For profiling, count invocations
479 void inc_invokes() { ++PhaseCCP::_total_invokes; }
480
481 static uint _total_constants; // For profiling, count constants found
482 uint _count_constants;
483 void clear_constants() { _count_constants = 0; }
484 void inc_constants() { ++_count_constants; }
485 uint count_constants() const { return _count_constants; }
486
487 static void print_statistics();
488 #endif
489 };
490
491
492 //------------------------------PhasePeephole----------------------------------
493 // Phase for performing peephole optimizations on register allocated basic blocks.
494 class PhasePeephole : public PhaseTransform {
495 PhaseRegAlloc *_regalloc;
496 PhaseCFG &_cfg;
497 // Recursive traversal of program. Pure function is unused in this phase
498 virtual Node *transform( Node *n );
499
500 public:
501 PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg );
502 NOT_PRODUCT( ~PhasePeephole(); )
503
504 // Do any transformation after analysis
505 void do_transform();
506
507 #ifndef PRODUCT
508 static uint _total_peepholes; // For profiling, count peephole rules applied
509 uint _count_peepholes;
510 void clear_peepholes() { _count_peepholes = 0; }
511 void inc_peepholes() { ++_count_peepholes; }
512 uint count_peepholes() const { return _count_peepholes; }
513
514 static void print_statistics();
515 #endif
516 };