1 /*
2 * Copyright (c) 1997, 2018, 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
25 #include "precompiled.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "opto/block.hpp"
31 #include "opto/callnode.hpp"
32 #include "opto/castnode.hpp"
33 #include "opto/cfgnode.hpp"
34 #include "opto/idealGraphPrinter.hpp"
35 #include "opto/loopnode.hpp"
36 #include "opto/machnode.hpp"
37 #include "opto/opcodes.hpp"
38 #include "opto/phaseX.hpp"
39 #include "opto/regalloc.hpp"
40 #include "opto/rootnode.hpp"
41 #include "utilities/macros.hpp"
42
43 //=============================================================================
44 #define NODE_HASH_MINIMUM_SIZE 255
45 //------------------------------NodeHash---------------------------------------
NodeHash(uint est_max_size)46 NodeHash::NodeHash(uint est_max_size) :
47 _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
48 _a(Thread::current()->resource_area()),
49 _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), // (Node**)_a->Amalloc(_max * sizeof(Node*)) ),
50 _inserts(0), _insert_limit( insert_limit() )
51 #ifndef PRODUCT
52 ,_look_probes(0), _lookup_hits(0), _lookup_misses(0),
53 _delete_probes(0), _delete_hits(0), _delete_misses(0),
54 _total_insert_probes(0), _total_inserts(0),
55 _insert_probes(0), _grows(0)
56 #endif
57 {
58 // _sentinel must be in the current node space
59 _sentinel = new ProjNode(NULL, TypeFunc::Control);
60 memset(_table,0,sizeof(Node*)*_max);
61 }
62
63 //------------------------------NodeHash---------------------------------------
NodeHash(Arena * arena,uint est_max_size)64 NodeHash::NodeHash(Arena *arena, uint est_max_size) :
65 _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
66 _a(arena),
67 _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ),
68 _inserts(0), _insert_limit( insert_limit() )
69 #ifndef PRODUCT
70 ,_look_probes(0), _lookup_hits(0), _lookup_misses(0),
71 _delete_probes(0), _delete_hits(0), _delete_misses(0),
72 _total_insert_probes(0), _total_inserts(0),
73 _insert_probes(0), _grows(0)
74 #endif
75 {
76 // _sentinel must be in the current node space
77 _sentinel = new ProjNode(NULL, TypeFunc::Control);
78 memset(_table,0,sizeof(Node*)*_max);
79 }
80
81 //------------------------------NodeHash---------------------------------------
NodeHash(NodeHash * nh)82 NodeHash::NodeHash(NodeHash *nh) {
83 debug_only(_table = (Node**)badAddress); // interact correctly w/ operator=
84 // just copy in all the fields
85 *this = *nh;
86 // nh->_sentinel must be in the current node space
87 }
88
replace_with(NodeHash * nh)89 void NodeHash::replace_with(NodeHash *nh) {
90 debug_only(_table = (Node**)badAddress); // interact correctly w/ operator=
91 // just copy in all the fields
92 *this = *nh;
93 // nh->_sentinel must be in the current node space
94 }
95
96 //------------------------------hash_find--------------------------------------
97 // Find in hash table
hash_find(const Node * n)98 Node *NodeHash::hash_find( const Node *n ) {
99 // ((Node*)n)->set_hash( n->hash() );
100 uint hash = n->hash();
101 if (hash == Node::NO_HASH) {
102 NOT_PRODUCT( _lookup_misses++ );
103 return NULL;
104 }
105 uint key = hash & (_max-1);
106 uint stride = key | 0x01;
107 NOT_PRODUCT( _look_probes++ );
108 Node *k = _table[key]; // Get hashed value
109 if( !k ) { // ?Miss?
110 NOT_PRODUCT( _lookup_misses++ );
111 return NULL; // Miss!
112 }
113
114 int op = n->Opcode();
115 uint req = n->req();
116 while( 1 ) { // While probing hash table
117 if( k->req() == req && // Same count of inputs
118 k->Opcode() == op ) { // Same Opcode
119 for( uint i=0; i<req; i++ )
120 if( n->in(i)!=k->in(i)) // Different inputs?
121 goto collision; // "goto" is a speed hack...
122 if( n->cmp(*k) ) { // Check for any special bits
123 NOT_PRODUCT( _lookup_hits++ );
124 return k; // Hit!
125 }
126 }
127 collision:
128 NOT_PRODUCT( _look_probes++ );
129 key = (key + stride/*7*/) & (_max-1); // Stride through table with relative prime
130 k = _table[key]; // Get hashed value
131 if( !k ) { // ?Miss?
132 NOT_PRODUCT( _lookup_misses++ );
133 return NULL; // Miss!
134 }
135 }
136 ShouldNotReachHere();
137 return NULL;
138 }
139
140 //------------------------------hash_find_insert-------------------------------
141 // Find in hash table, insert if not already present
142 // Used to preserve unique entries in hash table
hash_find_insert(Node * n)143 Node *NodeHash::hash_find_insert( Node *n ) {
144 // n->set_hash( );
145 uint hash = n->hash();
146 if (hash == Node::NO_HASH) {
147 NOT_PRODUCT( _lookup_misses++ );
148 return NULL;
149 }
150 uint key = hash & (_max-1);
151 uint stride = key | 0x01; // stride must be relatively prime to table siz
152 uint first_sentinel = 0; // replace a sentinel if seen.
153 NOT_PRODUCT( _look_probes++ );
154 Node *k = _table[key]; // Get hashed value
155 if( !k ) { // ?Miss?
156 NOT_PRODUCT( _lookup_misses++ );
157 _table[key] = n; // Insert into table!
158 debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
159 check_grow(); // Grow table if insert hit limit
160 return NULL; // Miss!
161 }
162 else if( k == _sentinel ) {
163 first_sentinel = key; // Can insert here
164 }
165
166 int op = n->Opcode();
167 uint req = n->req();
168 while( 1 ) { // While probing hash table
169 if( k->req() == req && // Same count of inputs
170 k->Opcode() == op ) { // Same Opcode
171 for( uint i=0; i<req; i++ )
172 if( n->in(i)!=k->in(i)) // Different inputs?
173 goto collision; // "goto" is a speed hack...
174 if( n->cmp(*k) ) { // Check for any special bits
175 NOT_PRODUCT( _lookup_hits++ );
176 return k; // Hit!
177 }
178 }
179 collision:
180 NOT_PRODUCT( _look_probes++ );
181 key = (key + stride) & (_max-1); // Stride through table w/ relative prime
182 k = _table[key]; // Get hashed value
183 if( !k ) { // ?Miss?
184 NOT_PRODUCT( _lookup_misses++ );
185 key = (first_sentinel == 0) ? key : first_sentinel; // ?saw sentinel?
186 _table[key] = n; // Insert into table!
187 debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
188 check_grow(); // Grow table if insert hit limit
189 return NULL; // Miss!
190 }
191 else if( first_sentinel == 0 && k == _sentinel ) {
192 first_sentinel = key; // Can insert here
193 }
194
195 }
196 ShouldNotReachHere();
197 return NULL;
198 }
199
200 //------------------------------hash_insert------------------------------------
201 // Insert into hash table
hash_insert(Node * n)202 void NodeHash::hash_insert( Node *n ) {
203 // // "conflict" comments -- print nodes that conflict
204 // bool conflict = false;
205 // n->set_hash();
206 uint hash = n->hash();
207 if (hash == Node::NO_HASH) {
208 return;
209 }
210 check_grow();
211 uint key = hash & (_max-1);
212 uint stride = key | 0x01;
213
214 while( 1 ) { // While probing hash table
215 NOT_PRODUCT( _insert_probes++ );
216 Node *k = _table[key]; // Get hashed value
217 if( !k || (k == _sentinel) ) break; // Found a slot
218 assert( k != n, "already inserted" );
219 // if( PrintCompilation && PrintOptoStatistics && Verbose ) { tty->print(" conflict: "); k->dump(); conflict = true; }
220 key = (key + stride) & (_max-1); // Stride through table w/ relative prime
221 }
222 _table[key] = n; // Insert into table!
223 debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
224 // if( conflict ) { n->dump(); }
225 }
226
227 //------------------------------hash_delete------------------------------------
228 // Replace in hash table with sentinel
hash_delete(const Node * n)229 bool NodeHash::hash_delete( const Node *n ) {
230 Node *k;
231 uint hash = n->hash();
232 if (hash == Node::NO_HASH) {
233 NOT_PRODUCT( _delete_misses++ );
234 return false;
235 }
236 uint key = hash & (_max-1);
237 uint stride = key | 0x01;
238 debug_only( uint counter = 0; );
239 for( ; /* (k != NULL) && (k != _sentinel) */; ) {
240 debug_only( counter++ );
241 NOT_PRODUCT( _delete_probes++ );
242 k = _table[key]; // Get hashed value
243 if( !k ) { // Miss?
244 NOT_PRODUCT( _delete_misses++ );
245 #ifdef ASSERT
246 if( VerifyOpto ) {
247 for( uint i=0; i < _max; i++ )
248 assert( _table[i] != n, "changed edges with rehashing" );
249 }
250 #endif
251 return false; // Miss! Not in chain
252 }
253 else if( n == k ) {
254 NOT_PRODUCT( _delete_hits++ );
255 _table[key] = _sentinel; // Hit! Label as deleted entry
256 debug_only(((Node*)n)->exit_hash_lock()); // Unlock the node upon removal from table.
257 return true;
258 }
259 else {
260 // collision: move through table with prime offset
261 key = (key + stride/*7*/) & (_max-1);
262 assert( counter <= _insert_limit, "Cycle in hash-table");
263 }
264 }
265 ShouldNotReachHere();
266 return false;
267 }
268
269 //------------------------------round_up---------------------------------------
270 // Round up to nearest power of 2
round_up(uint x)271 uint NodeHash::round_up( uint x ) {
272 x += (x>>2); // Add 25% slop
273 if( x <16 ) return 16; // Small stuff
274 uint i=16;
275 while( i < x ) i <<= 1; // Double to fit
276 return i; // Return hash table size
277 }
278
279 //------------------------------grow-------------------------------------------
280 // Grow _table to next power of 2 and insert old entries
grow()281 void NodeHash::grow() {
282 // Record old state
283 uint old_max = _max;
284 Node **old_table = _table;
285 // Construct new table with twice the space
286 #ifndef PRODUCT
287 _grows++;
288 _total_inserts += _inserts;
289 _total_insert_probes += _insert_probes;
290 _insert_probes = 0;
291 #endif
292 _inserts = 0;
293 _max = _max << 1;
294 _table = NEW_ARENA_ARRAY( _a , Node* , _max ); // (Node**)_a->Amalloc( _max * sizeof(Node*) );
295 memset(_table,0,sizeof(Node*)*_max);
296 _insert_limit = insert_limit();
297 // Insert old entries into the new table
298 for( uint i = 0; i < old_max; i++ ) {
299 Node *m = *old_table++;
300 if( !m || m == _sentinel ) continue;
301 debug_only(m->exit_hash_lock()); // Unlock the node upon removal from old table.
302 hash_insert(m);
303 }
304 }
305
306 //------------------------------clear------------------------------------------
307 // Clear all entries in _table to NULL but keep storage
clear()308 void NodeHash::clear() {
309 #ifdef ASSERT
310 // Unlock all nodes upon removal from table.
311 for (uint i = 0; i < _max; i++) {
312 Node* n = _table[i];
313 if (!n || n == _sentinel) continue;
314 n->exit_hash_lock();
315 }
316 #endif
317
318 memset( _table, 0, _max * sizeof(Node*) );
319 }
320
321 //-----------------------remove_useless_nodes----------------------------------
322 // Remove useless nodes from value table,
323 // implementation does not depend on hash function
remove_useless_nodes(VectorSet & useful)324 void NodeHash::remove_useless_nodes(VectorSet &useful) {
325
326 // Dead nodes in the hash table inherited from GVN should not replace
327 // existing nodes, remove dead nodes.
328 uint max = size();
329 Node *sentinel_node = sentinel();
330 for( uint i = 0; i < max; ++i ) {
331 Node *n = at(i);
332 if(n != NULL && n != sentinel_node && !useful.test(n->_idx)) {
333 debug_only(n->exit_hash_lock()); // Unlock the node when removed
334 _table[i] = sentinel_node; // Replace with placeholder
335 }
336 }
337 }
338
339
check_no_speculative_types()340 void NodeHash::check_no_speculative_types() {
341 #ifdef ASSERT
342 uint max = size();
343 Unique_Node_List live_nodes;
344 Compile::current()->identify_useful_nodes(live_nodes);
345 Node *sentinel_node = sentinel();
346 for (uint i = 0; i < max; ++i) {
347 Node *n = at(i);
348 if (n != NULL &&
349 n != sentinel_node &&
350 n->is_Type() &&
351 live_nodes.member(n)) {
352 TypeNode* tn = n->as_Type();
353 const Type* t = tn->type();
354 const Type* t_no_spec = t->remove_speculative();
355 assert(t == t_no_spec, "dead node in hash table or missed node during speculative cleanup");
356 }
357 }
358 #endif
359 }
360
361 #ifndef PRODUCT
362 //------------------------------dump-------------------------------------------
363 // Dump statistics for the hash table
dump()364 void NodeHash::dump() {
365 _total_inserts += _inserts;
366 _total_insert_probes += _insert_probes;
367 if (PrintCompilation && PrintOptoStatistics && Verbose && (_inserts > 0)) {
368 if (WizardMode) {
369 for (uint i=0; i<_max; i++) {
370 if (_table[i])
371 tty->print("%d/%d/%d ",i,_table[i]->hash()&(_max-1),_table[i]->_idx);
372 }
373 }
374 tty->print("\nGVN Hash stats: %d grows to %d max_size\n", _grows, _max);
375 tty->print(" %d/%d (%8.1f%% full)\n", _inserts, _max, (double)_inserts/_max*100.0);
376 tty->print(" %dp/(%dh+%dm) (%8.2f probes/lookup)\n", _look_probes, _lookup_hits, _lookup_misses, (double)_look_probes/(_lookup_hits+_lookup_misses));
377 tty->print(" %dp/%di (%8.2f probes/insert)\n", _total_insert_probes, _total_inserts, (double)_total_insert_probes/_total_inserts);
378 // sentinels increase lookup cost, but not insert cost
379 assert((_lookup_misses+_lookup_hits)*4+100 >= _look_probes, "bad hash function");
380 assert( _inserts+(_inserts>>3) < _max, "table too full" );
381 assert( _inserts*3+100 >= _insert_probes, "bad hash function" );
382 }
383 }
384
find_index(uint idx)385 Node *NodeHash::find_index(uint idx) { // For debugging
386 // Find an entry by its index value
387 for( uint i = 0; i < _max; i++ ) {
388 Node *m = _table[i];
389 if( !m || m == _sentinel ) continue;
390 if( m->_idx == (uint)idx ) return m;
391 }
392 return NULL;
393 }
394 #endif
395
396 #ifdef ASSERT
~NodeHash()397 NodeHash::~NodeHash() {
398 // Unlock all nodes upon destruction of table.
399 if (_table != (Node**)badAddress) clear();
400 }
401
operator =(const NodeHash & nh)402 void NodeHash::operator=(const NodeHash& nh) {
403 // Unlock all nodes upon replacement of table.
404 if (&nh == this) return;
405 if (_table != (Node**)badAddress) clear();
406 memcpy((void*)this, (void*)&nh, sizeof(*this));
407 // Do not increment hash_lock counts again.
408 // Instead, be sure we never again use the source table.
409 ((NodeHash*)&nh)->_table = (Node**)badAddress;
410 }
411
412
413 #endif
414
415
416 //=============================================================================
417 //------------------------------PhaseRemoveUseless-----------------------------
418 // 1) Use a breadthfirst walk to collect useful nodes reachable from root.
PhaseRemoveUseless(PhaseGVN * gvn,Unique_Node_List * worklist,PhaseNumber phase_num)419 PhaseRemoveUseless::PhaseRemoveUseless(PhaseGVN *gvn, Unique_Node_List *worklist, PhaseNumber phase_num) : Phase(phase_num),
420 _useful(Thread::current()->resource_area()) {
421
422 // Implementation requires 'UseLoopSafepoints == true' and an edge from root
423 // to each SafePointNode at a backward branch. Inserted in add_safepoint().
424 if( !UseLoopSafepoints || !OptoRemoveUseless ) return;
425
426 // Identify nodes that are reachable from below, useful.
427 C->identify_useful_nodes(_useful);
428 // Update dead node list
429 C->update_dead_node_list(_useful);
430
431 // Remove all useless nodes from PhaseValues' recorded types
432 // Must be done before disconnecting nodes to preserve hash-table-invariant
433 gvn->remove_useless_nodes(_useful.member_set());
434
435 // Remove all useless nodes from future worklist
436 worklist->remove_useless_nodes(_useful.member_set());
437
438 // Disconnect 'useless' nodes that are adjacent to useful nodes
439 C->remove_useless_nodes(_useful);
440 }
441
442 //=============================================================================
443 //------------------------------PhaseRenumberLive------------------------------
444 // First, remove useless nodes (equivalent to identifying live nodes).
445 // Then, renumber live nodes.
446 //
447 // The set of live nodes is returned by PhaseRemoveUseless in the _useful structure.
448 // If the number of live nodes is 'x' (where 'x' == _useful.size()), then the
449 // PhaseRenumberLive updates the node ID of each node (the _idx field) with a unique
450 // value in the range [0, x).
451 //
452 // At the end of the PhaseRenumberLive phase, the compiler's count of unique nodes is
453 // updated to 'x' and the list of dead nodes is reset (as there are no dead nodes).
454 //
455 // The PhaseRenumberLive phase updates two data structures with the new node IDs.
456 // (1) The worklist is used by the PhaseIterGVN phase to identify nodes that must be
457 // processed. A new worklist (with the updated node IDs) is returned in 'new_worklist'.
458 // (2) Type information (the field PhaseGVN::_types) maps type information to each
459 // node ID. The mapping is updated to use the new node IDs as well. Updated type
460 // information is returned in PhaseGVN::_types.
461 //
462 // The PhaseRenumberLive phase does not preserve the order of elements in the worklist.
463 //
464 // Other data structures used by the compiler are not updated. The hash table for value
465 // numbering (the field PhaseGVN::_table) is not updated because computing the hash
466 // values is not based on node IDs. The field PhaseGVN::_nodes is not updated either
467 // because it is empty wherever PhaseRenumberLive is used.
PhaseRenumberLive(PhaseGVN * gvn,Unique_Node_List * worklist,Unique_Node_List * new_worklist,PhaseNumber phase_num)468 PhaseRenumberLive::PhaseRenumberLive(PhaseGVN* gvn,
469 Unique_Node_List* worklist, Unique_Node_List* new_worklist,
470 PhaseNumber phase_num) :
471 PhaseRemoveUseless(gvn, worklist, Remove_Useless_And_Renumber_Live),
472 _new_type_array(C->comp_arena()),
473 _old2new_map(C->unique(), C->unique(), -1),
474 _delayed(Thread::current()->resource_area()),
475 _is_pass_finished(false),
476 _live_node_count(C->live_nodes())
477 {
478 assert(RenumberLiveNodes, "RenumberLiveNodes must be set to true for node renumbering to take place");
479 assert(C->live_nodes() == _useful.size(), "the number of live nodes must match the number of useful nodes");
480 assert(gvn->nodes_size() == 0, "GVN must not contain any nodes at this point");
481 assert(_delayed.size() == 0, "should be empty");
482
483 uint worklist_size = worklist->size();
484
485 // Iterate over the set of live nodes.
486 for (uint current_idx = 0; current_idx < _useful.size(); current_idx++) {
487 Node* n = _useful.at(current_idx);
488
489 bool in_worklist = false;
490 if (worklist->member(n)) {
491 in_worklist = true;
492 }
493
494 const Type* type = gvn->type_or_null(n);
495 _new_type_array.map(current_idx, type);
496
497 assert(_old2new_map.at(n->_idx) == -1, "already seen");
498 _old2new_map.at_put(n->_idx, current_idx);
499
500 n->set_idx(current_idx); // Update node ID.
501
502 if (in_worklist) {
503 new_worklist->push(n);
504 }
505
506 if (update_embedded_ids(n) < 0) {
507 _delayed.push(n); // has embedded IDs; handle later
508 }
509 }
510
511 assert(worklist_size == new_worklist->size(), "the new worklist must have the same size as the original worklist");
512 assert(_live_node_count == _useful.size(), "all live nodes must be processed");
513
514 _is_pass_finished = true; // pass finished; safe to process delayed updates
515
516 while (_delayed.size() > 0) {
517 Node* n = _delayed.pop();
518 int no_of_updates = update_embedded_ids(n);
519 assert(no_of_updates > 0, "should be updated");
520 }
521
522 // Replace the compiler's type information with the updated type information.
523 gvn->replace_types(_new_type_array);
524
525 // Update the unique node count of the compilation to the number of currently live nodes.
526 C->set_unique(_live_node_count);
527
528 // Set the dead node count to 0 and reset dead node list.
529 C->reset_dead_node_list();
530 }
531
new_index(int old_idx)532 int PhaseRenumberLive::new_index(int old_idx) {
533 assert(_is_pass_finished, "not finished");
534 if (_old2new_map.at(old_idx) == -1) { // absent
535 // Allocate a placeholder to preserve uniqueness
536 _old2new_map.at_put(old_idx, _live_node_count);
537 _live_node_count++;
538 }
539 return _old2new_map.at(old_idx);
540 }
541
update_embedded_ids(Node * n)542 int PhaseRenumberLive::update_embedded_ids(Node* n) {
543 int no_of_updates = 0;
544 if (n->is_Phi()) {
545 PhiNode* phi = n->as_Phi();
546 if (phi->_inst_id != -1) {
547 if (!_is_pass_finished) {
548 return -1; // delay
549 }
550 int new_idx = new_index(phi->_inst_id);
551 assert(new_idx != -1, "");
552 phi->_inst_id = new_idx;
553 no_of_updates++;
554 }
555 if (phi->_inst_mem_id != -1) {
556 if (!_is_pass_finished) {
557 return -1; // delay
558 }
559 int new_idx = new_index(phi->_inst_mem_id);
560 assert(new_idx != -1, "");
561 phi->_inst_mem_id = new_idx;
562 no_of_updates++;
563 }
564 }
565
566 const Type* type = _new_type_array.fast_lookup(n->_idx);
567 if (type != NULL && type->isa_oopptr() && type->is_oopptr()->is_known_instance()) {
568 if (!_is_pass_finished) {
569 return -1; // delay
570 }
571 int old_idx = type->is_oopptr()->instance_id();
572 int new_idx = new_index(old_idx);
573 const Type* new_type = type->is_oopptr()->with_instance_id(new_idx);
574 _new_type_array.map(n->_idx, new_type);
575 no_of_updates++;
576 }
577
578 return no_of_updates;
579 }
580
581 //=============================================================================
582 //------------------------------PhaseTransform---------------------------------
PhaseTransform(PhaseNumber pnum)583 PhaseTransform::PhaseTransform( PhaseNumber pnum ) : Phase(pnum),
584 _arena(Thread::current()->resource_area()),
585 _nodes(_arena),
586 _types(_arena)
587 {
588 init_con_caches();
589 #ifndef PRODUCT
590 clear_progress();
591 clear_transforms();
592 set_allow_progress(true);
593 #endif
594 // Force allocation for currently existing nodes
595 _types.map(C->unique(), NULL);
596 }
597
598 //------------------------------PhaseTransform---------------------------------
PhaseTransform(Arena * arena,PhaseNumber pnum)599 PhaseTransform::PhaseTransform( Arena *arena, PhaseNumber pnum ) : Phase(pnum),
600 _arena(arena),
601 _nodes(arena),
602 _types(arena)
603 {
604 init_con_caches();
605 #ifndef PRODUCT
606 clear_progress();
607 clear_transforms();
608 set_allow_progress(true);
609 #endif
610 // Force allocation for currently existing nodes
611 _types.map(C->unique(), NULL);
612 }
613
614 //------------------------------PhaseTransform---------------------------------
615 // Initialize with previously generated type information
PhaseTransform(PhaseTransform * pt,PhaseNumber pnum)616 PhaseTransform::PhaseTransform( PhaseTransform *pt, PhaseNumber pnum ) : Phase(pnum),
617 _arena(pt->_arena),
618 _nodes(pt->_nodes),
619 _types(pt->_types)
620 {
621 init_con_caches();
622 #ifndef PRODUCT
623 clear_progress();
624 clear_transforms();
625 set_allow_progress(true);
626 #endif
627 }
628
init_con_caches()629 void PhaseTransform::init_con_caches() {
630 memset(_icons,0,sizeof(_icons));
631 memset(_lcons,0,sizeof(_lcons));
632 memset(_zcons,0,sizeof(_zcons));
633 }
634
635
636 //--------------------------------find_int_type--------------------------------
find_int_type(Node * n)637 const TypeInt* PhaseTransform::find_int_type(Node* n) {
638 if (n == NULL) return NULL;
639 // Call type_or_null(n) to determine node's type since we might be in
640 // parse phase and call n->Value() may return wrong type.
641 // (For example, a phi node at the beginning of loop parsing is not ready.)
642 const Type* t = type_or_null(n);
643 if (t == NULL) return NULL;
644 return t->isa_int();
645 }
646
647
648 //-------------------------------find_long_type--------------------------------
find_long_type(Node * n)649 const TypeLong* PhaseTransform::find_long_type(Node* n) {
650 if (n == NULL) return NULL;
651 // (See comment above on type_or_null.)
652 const Type* t = type_or_null(n);
653 if (t == NULL) return NULL;
654 return t->isa_long();
655 }
656
657
658 #ifndef PRODUCT
dump_old2new_map() const659 void PhaseTransform::dump_old2new_map() const {
660 _nodes.dump();
661 }
662
dump_new(uint nidx) const663 void PhaseTransform::dump_new( uint nidx ) const {
664 for( uint i=0; i<_nodes.Size(); i++ )
665 if( _nodes[i] && _nodes[i]->_idx == nidx ) {
666 _nodes[i]->dump();
667 tty->cr();
668 tty->print_cr("Old index= %d",i);
669 return;
670 }
671 tty->print_cr("Node %d not found in the new indices", nidx);
672 }
673
674 //------------------------------dump_types-------------------------------------
dump_types() const675 void PhaseTransform::dump_types( ) const {
676 _types.dump();
677 }
678
679 //------------------------------dump_nodes_and_types---------------------------
dump_nodes_and_types(const Node * root,uint depth,bool only_ctrl)680 void PhaseTransform::dump_nodes_and_types(const Node *root, uint depth, bool only_ctrl) {
681 VectorSet visited(Thread::current()->resource_area());
682 dump_nodes_and_types_recur( root, depth, only_ctrl, visited );
683 }
684
685 //------------------------------dump_nodes_and_types_recur---------------------
dump_nodes_and_types_recur(const Node * n,uint depth,bool only_ctrl,VectorSet & visited)686 void PhaseTransform::dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited) {
687 if( !n ) return;
688 if( depth == 0 ) return;
689 if( visited.test_set(n->_idx) ) return;
690 for( uint i=0; i<n->len(); i++ ) {
691 if( only_ctrl && !(n->is_Region()) && i != TypeFunc::Control ) continue;
692 dump_nodes_and_types_recur( n->in(i), depth-1, only_ctrl, visited );
693 }
694 n->dump();
695 if (type_or_null(n) != NULL) {
696 tty->print(" "); type(n)->dump(); tty->cr();
697 }
698 }
699
700 #endif
701
702
703 //=============================================================================
704 //------------------------------PhaseValues------------------------------------
705 // Set minimum table size to "255"
PhaseValues(Arena * arena,uint est_max_size)706 PhaseValues::PhaseValues( Arena *arena, uint est_max_size ) : PhaseTransform(arena, GVN), _table(arena, est_max_size) {
707 NOT_PRODUCT( clear_new_values(); )
708 }
709
710 //------------------------------PhaseValues------------------------------------
711 // Set minimum table size to "255"
PhaseValues(PhaseValues * ptv)712 PhaseValues::PhaseValues( PhaseValues *ptv ) : PhaseTransform( ptv, GVN ),
713 _table(&ptv->_table) {
714 NOT_PRODUCT( clear_new_values(); )
715 }
716
717 //------------------------------PhaseValues------------------------------------
718 // Used by +VerifyOpto. Clear out hash table but copy _types array.
PhaseValues(PhaseValues * ptv,const char * dummy)719 PhaseValues::PhaseValues( PhaseValues *ptv, const char *dummy ) : PhaseTransform( ptv, GVN ),
720 _table(ptv->arena(),ptv->_table.size()) {
721 NOT_PRODUCT( clear_new_values(); )
722 }
723
724 //------------------------------~PhaseValues-----------------------------------
725 #ifndef PRODUCT
~PhaseValues()726 PhaseValues::~PhaseValues() {
727 _table.dump();
728
729 // Statistics for value progress and efficiency
730 if( PrintCompilation && Verbose && WizardMode ) {
731 tty->print("\n%sValues: %d nodes ---> %d/%d (%d)",
732 is_IterGVN() ? "Iter" : " ", C->unique(), made_progress(), made_transforms(), made_new_values());
733 if( made_transforms() != 0 ) {
734 tty->print_cr(" ratio %f", made_progress()/(float)made_transforms() );
735 } else {
736 tty->cr();
737 }
738 }
739 }
740 #endif
741
742 //------------------------------makecon----------------------------------------
makecon(const Type * t)743 ConNode* PhaseTransform::makecon(const Type *t) {
744 assert(t->singleton(), "must be a constant");
745 assert(!t->empty() || t == Type::TOP, "must not be vacuous range");
746 switch (t->base()) { // fast paths
747 case Type::Half:
748 case Type::Top: return (ConNode*) C->top();
749 case Type::Int: return intcon( t->is_int()->get_con() );
750 case Type::Long: return longcon( t->is_long()->get_con() );
751 default: break;
752 }
753 if (t->is_zero_type())
754 return zerocon(t->basic_type());
755 return uncached_makecon(t);
756 }
757
758 //--------------------------uncached_makecon-----------------------------------
759 // Make an idealized constant - one of ConINode, ConPNode, etc.
uncached_makecon(const Type * t)760 ConNode* PhaseValues::uncached_makecon(const Type *t) {
761 assert(t->singleton(), "must be a constant");
762 ConNode* x = ConNode::make(t);
763 ConNode* k = (ConNode*)hash_find_insert(x); // Value numbering
764 if (k == NULL) {
765 set_type(x, t); // Missed, provide type mapping
766 GrowableArray<Node_Notes*>* nna = C->node_note_array();
767 if (nna != NULL) {
768 Node_Notes* loc = C->locate_node_notes(nna, x->_idx, true);
769 loc->clear(); // do not put debug info on constants
770 }
771 } else {
772 x->destruct(); // Hit, destroy duplicate constant
773 x = k; // use existing constant
774 }
775 return x;
776 }
777
778 //------------------------------intcon-----------------------------------------
779 // Fast integer constant. Same as "transform(new ConINode(TypeInt::make(i)))"
intcon(jint i)780 ConINode* PhaseTransform::intcon(jint i) {
781 // Small integer? Check cache! Check that cached node is not dead
782 if (i >= _icon_min && i <= _icon_max) {
783 ConINode* icon = _icons[i-_icon_min];
784 if (icon != NULL && icon->in(TypeFunc::Control) != NULL)
785 return icon;
786 }
787 ConINode* icon = (ConINode*) uncached_makecon(TypeInt::make(i));
788 assert(icon->is_Con(), "");
789 if (i >= _icon_min && i <= _icon_max)
790 _icons[i-_icon_min] = icon; // Cache small integers
791 return icon;
792 }
793
794 //------------------------------longcon----------------------------------------
795 // Fast long constant.
longcon(jlong l)796 ConLNode* PhaseTransform::longcon(jlong l) {
797 // Small integer? Check cache! Check that cached node is not dead
798 if (l >= _lcon_min && l <= _lcon_max) {
799 ConLNode* lcon = _lcons[l-_lcon_min];
800 if (lcon != NULL && lcon->in(TypeFunc::Control) != NULL)
801 return lcon;
802 }
803 ConLNode* lcon = (ConLNode*) uncached_makecon(TypeLong::make(l));
804 assert(lcon->is_Con(), "");
805 if (l >= _lcon_min && l <= _lcon_max)
806 _lcons[l-_lcon_min] = lcon; // Cache small integers
807 return lcon;
808 }
809
810 //------------------------------zerocon-----------------------------------------
811 // Fast zero or null constant. Same as "transform(ConNode::make(Type::get_zero_type(bt)))"
zerocon(BasicType bt)812 ConNode* PhaseTransform::zerocon(BasicType bt) {
813 assert((uint)bt <= _zcon_max, "domain check");
814 ConNode* zcon = _zcons[bt];
815 if (zcon != NULL && zcon->in(TypeFunc::Control) != NULL)
816 return zcon;
817 zcon = (ConNode*) uncached_makecon(Type::get_zero_type(bt));
818 _zcons[bt] = zcon;
819 return zcon;
820 }
821
822
823
824 //=============================================================================
apply_ideal(Node * k,bool can_reshape)825 Node* PhaseGVN::apply_ideal(Node* k, bool can_reshape) {
826 Node* i = BarrierSet::barrier_set()->barrier_set_c2()->ideal_node(this, k, can_reshape);
827 if (i == NULL) {
828 i = k->Ideal(this, can_reshape);
829 }
830 return i;
831 }
832
apply_identity(Node * k)833 Node* PhaseGVN::apply_identity(Node* k) {
834 Node* i = BarrierSet::barrier_set()->barrier_set_c2()->identity_node(this, k);
835 if (i == k) {
836 i = k->Identity(this);
837 }
838 return i;
839 }
840
841 //------------------------------transform--------------------------------------
842 // Return a node which computes the same function as this node, but in a
843 // faster or cheaper fashion.
transform(Node * n)844 Node *PhaseGVN::transform( Node *n ) {
845 return transform_no_reclaim(n);
846 }
847
848 //------------------------------transform--------------------------------------
849 // Return a node which computes the same function as this node, but
850 // in a faster or cheaper fashion.
transform_no_reclaim(Node * n)851 Node *PhaseGVN::transform_no_reclaim( Node *n ) {
852 NOT_PRODUCT( set_transforms(); )
853
854 // Apply the Ideal call in a loop until it no longer applies
855 Node *k = n;
856 NOT_PRODUCT( uint loop_count = 0; )
857 while( 1 ) {
858 Node *i = apply_ideal(k, /*can_reshape=*/false);
859 if( !i ) break;
860 assert( i->_idx >= k->_idx, "Idealize should return new nodes, use Identity to return old nodes" );
861 k = i;
862 assert(loop_count++ < K, "infinite loop in PhaseGVN::transform");
863 }
864 NOT_PRODUCT( if( loop_count != 0 ) { set_progress(); } )
865
866
867 // If brand new node, make space in type array.
868 ensure_type_or_null(k);
869
870 // Since I just called 'Value' to compute the set of run-time values
871 // for this Node, and 'Value' is non-local (and therefore expensive) I'll
872 // cache Value. Later requests for the local phase->type of this Node can
873 // use the cached Value instead of suffering with 'bottom_type'.
874 const Type *t = k->Value(this); // Get runtime Value set
875 assert(t != NULL, "value sanity");
876 if (type_or_null(k) != t) {
877 #ifndef PRODUCT
878 // Do not count initial visit to node as a transformation
879 if (type_or_null(k) == NULL) {
880 inc_new_values();
881 set_progress();
882 }
883 #endif
884 set_type(k, t);
885 // If k is a TypeNode, capture any more-precise type permanently into Node
886 k->raise_bottom_type(t);
887 }
888
889 if( t->singleton() && !k->is_Con() ) {
890 NOT_PRODUCT( set_progress(); )
891 return makecon(t); // Turn into a constant
892 }
893
894 // Now check for Identities
895 Node *i = apply_identity(k); // Look for a nearby replacement
896 if( i != k ) { // Found? Return replacement!
897 NOT_PRODUCT( set_progress(); )
898 return i;
899 }
900
901 // Global Value Numbering
902 i = hash_find_insert(k); // Insert if new
903 if( i && (i != k) ) {
904 // Return the pre-existing node
905 NOT_PRODUCT( set_progress(); )
906 return i;
907 }
908
909 // Return Idealized original
910 return k;
911 }
912
is_dominator_helper(Node * d,Node * n,bool linear_only)913 bool PhaseGVN::is_dominator_helper(Node *d, Node *n, bool linear_only) {
914 if (d->is_top() || (d->is_Proj() && d->in(0)->is_top())) {
915 return false;
916 }
917 if (n->is_top() || (n->is_Proj() && n->in(0)->is_top())) {
918 return false;
919 }
920 assert(d->is_CFG() && n->is_CFG(), "must have CFG nodes");
921 int i = 0;
922 while (d != n) {
923 n = IfNode::up_one_dom(n, linear_only);
924 i++;
925 if (n == NULL || i >= 10) {
926 return false;
927 }
928 }
929 return true;
930 }
931
932 #ifdef ASSERT
933 //------------------------------dead_loop_check--------------------------------
934 // Check for a simple dead loop when a data node references itself directly
935 // or through an other data node excluding cons and phis.
dead_loop_check(Node * n)936 void PhaseGVN::dead_loop_check( Node *n ) {
937 // Phi may reference itself in a loop
938 if (n != NULL && !n->is_dead_loop_safe() && !n->is_CFG()) {
939 // Do 2 levels check and only data inputs.
940 bool no_dead_loop = true;
941 uint cnt = n->req();
942 for (uint i = 1; i < cnt && no_dead_loop; i++) {
943 Node *in = n->in(i);
944 if (in == n) {
945 no_dead_loop = false;
946 } else if (in != NULL && !in->is_dead_loop_safe()) {
947 uint icnt = in->req();
948 for (uint j = 1; j < icnt && no_dead_loop; j++) {
949 if (in->in(j) == n || in->in(j) == in)
950 no_dead_loop = false;
951 }
952 }
953 }
954 if (!no_dead_loop) n->dump(3);
955 assert(no_dead_loop, "dead loop detected");
956 }
957 }
958 #endif
959
960 //=============================================================================
961 //------------------------------PhaseIterGVN-----------------------------------
962 // Initialize hash table to fresh and clean for +VerifyOpto
PhaseIterGVN(PhaseIterGVN * igvn,const char * dummy)963 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ) : PhaseGVN(igvn,dummy), _worklist( ),
964 _stack(C->live_nodes() >> 1),
965 _delay_transform(false) {
966 }
967
968 //------------------------------PhaseIterGVN-----------------------------------
969 // Initialize with previous PhaseIterGVN info; used by PhaseCCP
PhaseIterGVN(PhaseIterGVN * igvn)970 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn ) : PhaseGVN(igvn),
971 _worklist( igvn->_worklist ),
972 _stack( igvn->_stack ),
973 _delay_transform(igvn->_delay_transform)
974 {
975 }
976
977 //------------------------------PhaseIterGVN-----------------------------------
978 // Initialize with previous PhaseGVN info from Parser
PhaseIterGVN(PhaseGVN * gvn)979 PhaseIterGVN::PhaseIterGVN( PhaseGVN *gvn ) : PhaseGVN(gvn),
980 _worklist(*C->for_igvn()),
981 // TODO: Before incremental inlining it was allocated only once and it was fine. Now that
982 // the constructor is used in incremental inlining, this consumes too much memory:
983 // _stack(C->live_nodes() >> 1),
984 // So, as a band-aid, we replace this by:
985 _stack(C->comp_arena(), 32),
986 _delay_transform(false)
987 {
988 uint max;
989
990 // Dead nodes in the hash table inherited from GVN were not treated as
991 // roots during def-use info creation; hence they represent an invisible
992 // use. Clear them out.
993 max = _table.size();
994 for( uint i = 0; i < max; ++i ) {
995 Node *n = _table.at(i);
996 if(n != NULL && n != _table.sentinel() && n->outcnt() == 0) {
997 if( n->is_top() ) continue;
998 assert( false, "Parse::remove_useless_nodes missed this node");
999 hash_delete(n);
1000 }
1001 }
1002
1003 // Any Phis or Regions on the worklist probably had uses that could not
1004 // make more progress because the uses were made while the Phis and Regions
1005 // were in half-built states. Put all uses of Phis and Regions on worklist.
1006 max = _worklist.size();
1007 for( uint j = 0; j < max; j++ ) {
1008 Node *n = _worklist.at(j);
1009 uint uop = n->Opcode();
1010 if( uop == Op_Phi || uop == Op_Region ||
1011 n->is_Type() ||
1012 n->is_Mem() )
1013 add_users_to_worklist(n);
1014 }
1015
1016 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1017 bs->add_users_to_worklist(&_worklist);
1018 }
1019
1020 /**
1021 * Initialize worklist for each node.
1022 */
init_worklist(Node * first)1023 void PhaseIterGVN::init_worklist(Node* first) {
1024 Unique_Node_List to_process;
1025 to_process.push(first);
1026
1027 while (to_process.size() > 0) {
1028 Node* n = to_process.pop();
1029 if (!_worklist.member(n)) {
1030 _worklist.push(n);
1031
1032 uint cnt = n->req();
1033 for(uint i = 0; i < cnt; i++) {
1034 Node* m = n->in(i);
1035 if (m != NULL) {
1036 to_process.push(m);
1037 }
1038 }
1039 }
1040 }
1041 }
1042
1043 #ifndef PRODUCT
verify_step(Node * n)1044 void PhaseIterGVN::verify_step(Node* n) {
1045 if (VerifyIterativeGVN) {
1046 _verify_window[_verify_counter % _verify_window_size] = n;
1047 ++_verify_counter;
1048 if (C->unique() < 1000 || 0 == _verify_counter % (C->unique() < 10000 ? 10 : 100)) {
1049 ++_verify_full_passes;
1050 Node::verify(C->root(), -1);
1051 }
1052 for (int i = 0; i < _verify_window_size; i++) {
1053 Node* n = _verify_window[i];
1054 if (n == NULL) {
1055 continue;
1056 }
1057 if (n->in(0) == NodeSentinel) { // xform_idom
1058 _verify_window[i] = n->in(1);
1059 --i;
1060 continue;
1061 }
1062 // Typical fanout is 1-2, so this call visits about 6 nodes.
1063 Node::verify(n, 4);
1064 }
1065 }
1066 }
1067
trace_PhaseIterGVN(Node * n,Node * nn,const Type * oldtype)1068 void PhaseIterGVN::trace_PhaseIterGVN(Node* n, Node* nn, const Type* oldtype) {
1069 if (TraceIterativeGVN) {
1070 uint wlsize = _worklist.size();
1071 const Type* newtype = type_or_null(n);
1072 if (nn != n) {
1073 // print old node
1074 tty->print("< ");
1075 if (oldtype != newtype && oldtype != NULL) {
1076 oldtype->dump();
1077 }
1078 do { tty->print("\t"); } while (tty->position() < 16);
1079 tty->print("<");
1080 n->dump();
1081 }
1082 if (oldtype != newtype || nn != n) {
1083 // print new node and/or new type
1084 if (oldtype == NULL) {
1085 tty->print("* ");
1086 } else if (nn != n) {
1087 tty->print("> ");
1088 } else {
1089 tty->print("= ");
1090 }
1091 if (newtype == NULL) {
1092 tty->print("null");
1093 } else {
1094 newtype->dump();
1095 }
1096 do { tty->print("\t"); } while (tty->position() < 16);
1097 nn->dump();
1098 }
1099 if (Verbose && wlsize < _worklist.size()) {
1100 tty->print(" Push {");
1101 while (wlsize != _worklist.size()) {
1102 Node* pushed = _worklist.at(wlsize++);
1103 tty->print(" %d", pushed->_idx);
1104 }
1105 tty->print_cr(" }");
1106 }
1107 if (nn != n) {
1108 // ignore n, it might be subsumed
1109 verify_step((Node*) NULL);
1110 }
1111 }
1112 }
1113
init_verifyPhaseIterGVN()1114 void PhaseIterGVN::init_verifyPhaseIterGVN() {
1115 _verify_counter = 0;
1116 _verify_full_passes = 0;
1117 for (int i = 0; i < _verify_window_size; i++) {
1118 _verify_window[i] = NULL;
1119 }
1120 #ifdef ASSERT
1121 // Verify that all modified nodes are on _worklist
1122 Unique_Node_List* modified_list = C->modified_nodes();
1123 while (modified_list != NULL && modified_list->size()) {
1124 Node* n = modified_list->pop();
1125 if (n->outcnt() != 0 && !n->is_Con() && !_worklist.member(n)) {
1126 n->dump();
1127 assert(false, "modified node is not on IGVN._worklist");
1128 }
1129 }
1130 #endif
1131 }
1132
verify_PhaseIterGVN()1133 void PhaseIterGVN::verify_PhaseIterGVN() {
1134 #ifdef ASSERT
1135 // Verify nodes with changed inputs.
1136 Unique_Node_List* modified_list = C->modified_nodes();
1137 while (modified_list != NULL && modified_list->size()) {
1138 Node* n = modified_list->pop();
1139 if (n->outcnt() != 0 && !n->is_Con()) { // skip dead and Con nodes
1140 n->dump();
1141 assert(false, "modified node was not processed by IGVN.transform_old()");
1142 }
1143 }
1144 #endif
1145
1146 C->verify_graph_edges();
1147 if( VerifyOpto && allow_progress() ) {
1148 // Must turn off allow_progress to enable assert and break recursion
1149 Node::verify(C->root(), -1);
1150 { // Check if any progress was missed using IterGVN
1151 // Def-Use info enables transformations not attempted in wash-pass
1152 // e.g. Region/Phi cleanup, ...
1153 // Null-check elision -- may not have reached fixpoint
1154 // do not propagate to dominated nodes
1155 ResourceMark rm;
1156 PhaseIterGVN igvn2(this,"Verify"); // Fresh and clean!
1157 // Fill worklist completely
1158 igvn2.init_worklist(C->root());
1159
1160 igvn2.set_allow_progress(false);
1161 igvn2.optimize();
1162 igvn2.set_allow_progress(true);
1163 }
1164 }
1165 if (VerifyIterativeGVN && PrintOpto) {
1166 if (_verify_counter == _verify_full_passes) {
1167 tty->print_cr("VerifyIterativeGVN: %d transforms and verify passes",
1168 (int) _verify_full_passes);
1169 } else {
1170 tty->print_cr("VerifyIterativeGVN: %d transforms, %d full verify passes",
1171 (int) _verify_counter, (int) _verify_full_passes);
1172 }
1173 }
1174
1175 #ifdef ASSERT
1176 while (modified_list->size()) {
1177 Node* n = modified_list->pop();
1178 n->dump();
1179 assert(false, "VerifyIterativeGVN: new modified node was added");
1180 }
1181 #endif
1182 }
1183 #endif /* PRODUCT */
1184
1185 #ifdef ASSERT
1186 /**
1187 * Dumps information that can help to debug the problem. A debug
1188 * build fails with an assert.
1189 */
dump_infinite_loop_info(Node * n)1190 void PhaseIterGVN::dump_infinite_loop_info(Node* n) {
1191 n->dump(4);
1192 _worklist.dump();
1193 assert(false, "infinite loop in PhaseIterGVN::optimize");
1194 }
1195
1196 /**
1197 * Prints out information about IGVN if the 'verbose' option is used.
1198 */
trace_PhaseIterGVN_verbose(Node * n,int num_processed)1199 void PhaseIterGVN::trace_PhaseIterGVN_verbose(Node* n, int num_processed) {
1200 if (TraceIterativeGVN && Verbose) {
1201 tty->print(" Pop ");
1202 n->dump();
1203 if ((num_processed % 100) == 0) {
1204 _worklist.print_set();
1205 }
1206 }
1207 }
1208 #endif /* ASSERT */
1209
optimize()1210 void PhaseIterGVN::optimize() {
1211 DEBUG_ONLY(uint num_processed = 0;)
1212 NOT_PRODUCT(init_verifyPhaseIterGVN();)
1213
1214 uint loop_count = 0;
1215 // Pull from worklist and transform the node. If the node has changed,
1216 // update edge info and put uses on worklist.
1217 while(_worklist.size()) {
1218 if (C->check_node_count(NodeLimitFudgeFactor * 2, "Out of nodes")) {
1219 return;
1220 }
1221 Node* n = _worklist.pop();
1222 if (++loop_count >= K * C->live_nodes()) {
1223 DEBUG_ONLY(dump_infinite_loop_info(n);)
1224 C->record_method_not_compilable("infinite loop in PhaseIterGVN::optimize");
1225 return;
1226 }
1227 DEBUG_ONLY(trace_PhaseIterGVN_verbose(n, num_processed++);)
1228 if (n->outcnt() != 0) {
1229 NOT_PRODUCT(const Type* oldtype = type_or_null(n));
1230 // Do the transformation
1231 Node* nn = transform_old(n);
1232 NOT_PRODUCT(trace_PhaseIterGVN(n, nn, oldtype);)
1233 } else if (!n->is_top()) {
1234 remove_dead_node(n);
1235 }
1236 }
1237 NOT_PRODUCT(verify_PhaseIterGVN();)
1238 }
1239
1240
1241 /**
1242 * Register a new node with the optimizer. Update the types array, the def-use
1243 * info. Put on worklist.
1244 */
register_new_node_with_optimizer(Node * n,Node * orig)1245 Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) {
1246 set_type_bottom(n);
1247 _worklist.push(n);
1248 if (orig != NULL) C->copy_node_notes_to(n, orig);
1249 return n;
1250 }
1251
1252 //------------------------------transform--------------------------------------
1253 // Non-recursive: idealize Node 'n' with respect to its inputs and its value
transform(Node * n)1254 Node *PhaseIterGVN::transform( Node *n ) {
1255 if (_delay_transform) {
1256 // Register the node but don't optimize for now
1257 register_new_node_with_optimizer(n);
1258 return n;
1259 }
1260
1261 // If brand new node, make space in type array, and give it a type.
1262 ensure_type_or_null(n);
1263 if (type_or_null(n) == NULL) {
1264 set_type_bottom(n);
1265 }
1266
1267 return transform_old(n);
1268 }
1269
transform_old(Node * n)1270 Node *PhaseIterGVN::transform_old(Node* n) {
1271 DEBUG_ONLY(uint loop_count = 0;);
1272 NOT_PRODUCT(set_transforms());
1273
1274 // Remove 'n' from hash table in case it gets modified
1275 _table.hash_delete(n);
1276 if (VerifyIterativeGVN) {
1277 assert(!_table.find_index(n->_idx), "found duplicate entry in table");
1278 }
1279
1280 // Apply the Ideal call in a loop until it no longer applies
1281 Node* k = n;
1282 DEBUG_ONLY(dead_loop_check(k);)
1283 DEBUG_ONLY(bool is_new = (k->outcnt() == 0);)
1284 C->remove_modified_node(k);
1285 Node* i = apply_ideal(k, /*can_reshape=*/true);
1286 assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes");
1287 #ifndef PRODUCT
1288 verify_step(k);
1289 if (i && VerifyOpto ) {
1290 if (!allow_progress()) {
1291 if (i->is_Add() && (i->outcnt() == 1)) {
1292 // Switched input to left side because this is the only use
1293 } else if (i->is_If() && (i->in(0) == NULL)) {
1294 // This IF is dead because it is dominated by an equivalent IF When
1295 // dominating if changed, info is not propagated sparsely to 'this'
1296 // Propagating this info further will spuriously identify other
1297 // progress.
1298 return i;
1299 } else
1300 set_progress();
1301 } else {
1302 set_progress();
1303 }
1304 }
1305 #endif
1306
1307 while (i != NULL) {
1308 #ifdef ASSERT
1309 if (loop_count >= K) {
1310 dump_infinite_loop_info(i);
1311 }
1312 loop_count++;
1313 #endif
1314 assert((i->_idx >= k->_idx) || i->is_top(), "Idealize should return new nodes, use Identity to return old nodes");
1315 // Made a change; put users of original Node on worklist
1316 add_users_to_worklist(k);
1317 // Replacing root of transform tree?
1318 if (k != i) {
1319 // Make users of old Node now use new.
1320 subsume_node(k, i);
1321 k = i;
1322 }
1323 DEBUG_ONLY(dead_loop_check(k);)
1324 // Try idealizing again
1325 DEBUG_ONLY(is_new = (k->outcnt() == 0);)
1326 C->remove_modified_node(k);
1327 i = apply_ideal(k, /*can_reshape=*/true);
1328 assert(i != k || is_new || (i->outcnt() > 0), "don't return dead nodes");
1329 #ifndef PRODUCT
1330 verify_step(k);
1331 if (i && VerifyOpto) {
1332 set_progress();
1333 }
1334 #endif
1335 }
1336
1337 // If brand new node, make space in type array.
1338 ensure_type_or_null(k);
1339
1340 // See what kind of values 'k' takes on at runtime
1341 const Type* t = k->Value(this);
1342 assert(t != NULL, "value sanity");
1343
1344 // Since I just called 'Value' to compute the set of run-time values
1345 // for this Node, and 'Value' is non-local (and therefore expensive) I'll
1346 // cache Value. Later requests for the local phase->type of this Node can
1347 // use the cached Value instead of suffering with 'bottom_type'.
1348 if (type_or_null(k) != t) {
1349 #ifndef PRODUCT
1350 inc_new_values();
1351 set_progress();
1352 #endif
1353 set_type(k, t);
1354 // If k is a TypeNode, capture any more-precise type permanently into Node
1355 k->raise_bottom_type(t);
1356 // Move users of node to worklist
1357 add_users_to_worklist(k);
1358 }
1359 // If 'k' computes a constant, replace it with a constant
1360 if (t->singleton() && !k->is_Con()) {
1361 NOT_PRODUCT(set_progress();)
1362 Node* con = makecon(t); // Make a constant
1363 add_users_to_worklist(k);
1364 subsume_node(k, con); // Everybody using k now uses con
1365 return con;
1366 }
1367
1368 // Now check for Identities
1369 i = apply_identity(k); // Look for a nearby replacement
1370 if (i != k) { // Found? Return replacement!
1371 NOT_PRODUCT(set_progress();)
1372 add_users_to_worklist(k);
1373 subsume_node(k, i); // Everybody using k now uses i
1374 return i;
1375 }
1376
1377 // Global Value Numbering
1378 i = hash_find_insert(k); // Check for pre-existing node
1379 if (i && (i != k)) {
1380 // Return the pre-existing node if it isn't dead
1381 NOT_PRODUCT(set_progress();)
1382 add_users_to_worklist(k);
1383 subsume_node(k, i); // Everybody using k now uses i
1384 return i;
1385 }
1386
1387 // Return Idealized original
1388 return k;
1389 }
1390
1391 //---------------------------------saturate------------------------------------
saturate(const Type * new_type,const Type * old_type,const Type * limit_type) const1392 const Type* PhaseIterGVN::saturate(const Type* new_type, const Type* old_type,
1393 const Type* limit_type) const {
1394 return new_type->narrow(old_type);
1395 }
1396
1397 //------------------------------remove_globally_dead_node----------------------
1398 // Kill a globally dead Node. All uses are also globally dead and are
1399 // aggressively trimmed.
remove_globally_dead_node(Node * dead)1400 void PhaseIterGVN::remove_globally_dead_node( Node *dead ) {
1401 enum DeleteProgress {
1402 PROCESS_INPUTS,
1403 PROCESS_OUTPUTS
1404 };
1405 assert(_stack.is_empty(), "not empty");
1406 _stack.push(dead, PROCESS_INPUTS);
1407
1408 while (_stack.is_nonempty()) {
1409 dead = _stack.node();
1410 if (dead->Opcode() == Op_SafePoint) {
1411 dead->as_SafePoint()->disconnect_from_root(this);
1412 }
1413 uint progress_state = _stack.index();
1414 assert(dead != C->root(), "killing root, eh?");
1415 assert(!dead->is_top(), "add check for top when pushing");
1416 NOT_PRODUCT( set_progress(); )
1417 if (progress_state == PROCESS_INPUTS) {
1418 // After following inputs, continue to outputs
1419 _stack.set_index(PROCESS_OUTPUTS);
1420 if (!dead->is_Con()) { // Don't kill cons but uses
1421 bool recurse = false;
1422 // Remove from hash table
1423 _table.hash_delete( dead );
1424 // Smash all inputs to 'dead', isolating him completely
1425 for (uint i = 0; i < dead->req(); i++) {
1426 Node *in = dead->in(i);
1427 if (in != NULL && in != C->top()) { // Points to something?
1428 int nrep = dead->replace_edge(in, NULL); // Kill edges
1429 assert((nrep > 0), "sanity");
1430 if (in->outcnt() == 0) { // Made input go dead?
1431 _stack.push(in, PROCESS_INPUTS); // Recursively remove
1432 recurse = true;
1433 } else if (in->outcnt() == 1 &&
1434 in->has_special_unique_user()) {
1435 _worklist.push(in->unique_out());
1436 } else if (in->outcnt() <= 2 && dead->is_Phi()) {
1437 if (in->Opcode() == Op_Region) {
1438 _worklist.push(in);
1439 } else if (in->is_Store()) {
1440 DUIterator_Fast imax, i = in->fast_outs(imax);
1441 _worklist.push(in->fast_out(i));
1442 i++;
1443 if (in->outcnt() == 2) {
1444 _worklist.push(in->fast_out(i));
1445 i++;
1446 }
1447 assert(!(i < imax), "sanity");
1448 }
1449 } else {
1450 BarrierSet::barrier_set()->barrier_set_c2()->enqueue_useful_gc_barrier(_worklist, in);
1451 }
1452 if (ReduceFieldZeroing && dead->is_Load() && i == MemNode::Memory &&
1453 in->is_Proj() && in->in(0) != NULL && in->in(0)->is_Initialize()) {
1454 // A Load that directly follows an InitializeNode is
1455 // going away. The Stores that follow are candidates
1456 // again to be captured by the InitializeNode.
1457 for (DUIterator_Fast jmax, j = in->fast_outs(jmax); j < jmax; j++) {
1458 Node *n = in->fast_out(j);
1459 if (n->is_Store()) {
1460 _worklist.push(n);
1461 }
1462 }
1463 }
1464 } // if (in != NULL && in != C->top())
1465 } // for (uint i = 0; i < dead->req(); i++)
1466 if (recurse) {
1467 continue;
1468 }
1469 } // if (!dead->is_Con())
1470 } // if (progress_state == PROCESS_INPUTS)
1471
1472 // Aggressively kill globally dead uses
1473 // (Rather than pushing all the outs at once, we push one at a time,
1474 // plus the parent to resume later, because of the indefinite number
1475 // of edge deletions per loop trip.)
1476 if (dead->outcnt() > 0) {
1477 // Recursively remove output edges
1478 _stack.push(dead->raw_out(0), PROCESS_INPUTS);
1479 } else {
1480 // Finished disconnecting all input and output edges.
1481 _stack.pop();
1482 // Remove dead node from iterative worklist
1483 _worklist.remove(dead);
1484 C->remove_modified_node(dead);
1485 // Constant node that has no out-edges and has only one in-edge from
1486 // root is usually dead. However, sometimes reshaping walk makes
1487 // it reachable by adding use edges. So, we will NOT count Con nodes
1488 // as dead to be conservative about the dead node count at any
1489 // given time.
1490 if (!dead->is_Con()) {
1491 C->record_dead_node(dead->_idx);
1492 }
1493 if (dead->is_macro()) {
1494 C->remove_macro_node(dead);
1495 }
1496 if (dead->is_expensive()) {
1497 C->remove_expensive_node(dead);
1498 }
1499 CastIINode* cast = dead->isa_CastII();
1500 if (cast != NULL && cast->has_range_check()) {
1501 C->remove_range_check_cast(cast);
1502 }
1503 if (dead->Opcode() == Op_Opaque4) {
1504 C->remove_opaque4_node(dead);
1505 }
1506 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1507 bs->unregister_potential_barrier_node(dead);
1508 }
1509 } // while (_stack.is_nonempty())
1510 }
1511
1512 //------------------------------subsume_node-----------------------------------
1513 // Remove users from node 'old' and add them to node 'nn'.
subsume_node(Node * old,Node * nn)1514 void PhaseIterGVN::subsume_node( Node *old, Node *nn ) {
1515 if (old->Opcode() == Op_SafePoint) {
1516 old->as_SafePoint()->disconnect_from_root(this);
1517 }
1518 assert( old != hash_find(old), "should already been removed" );
1519 assert( old != C->top(), "cannot subsume top node");
1520 // Copy debug or profile information to the new version:
1521 C->copy_node_notes_to(nn, old);
1522 // Move users of node 'old' to node 'nn'
1523 for (DUIterator_Last imin, i = old->last_outs(imin); i >= imin; ) {
1524 Node* use = old->last_out(i); // for each use...
1525 // use might need re-hashing (but it won't if it's a new node)
1526 rehash_node_delayed(use);
1527 // Update use-def info as well
1528 // We remove all occurrences of old within use->in,
1529 // so as to avoid rehashing any node more than once.
1530 // The hash table probe swamps any outer loop overhead.
1531 uint num_edges = 0;
1532 for (uint jmax = use->len(), j = 0; j < jmax; j++) {
1533 if (use->in(j) == old) {
1534 use->set_req(j, nn);
1535 ++num_edges;
1536 }
1537 }
1538 i -= num_edges; // we deleted 1 or more copies of this edge
1539 }
1540
1541 // Search for instance field data PhiNodes in the same region pointing to the old
1542 // memory PhiNode and update their instance memory ids to point to the new node.
1543 if (old->is_Phi() && old->as_Phi()->type()->has_memory() && old->in(0) != NULL) {
1544 Node* region = old->in(0);
1545 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
1546 PhiNode* phi = region->fast_out(i)->isa_Phi();
1547 if (phi != NULL && phi->inst_mem_id() == (int)old->_idx) {
1548 phi->set_inst_mem_id((int)nn->_idx);
1549 }
1550 }
1551 }
1552
1553 // Smash all inputs to 'old', isolating him completely
1554 Node *temp = new Node(1);
1555 temp->init_req(0,nn); // Add a use to nn to prevent him from dying
1556 remove_dead_node( old );
1557 temp->del_req(0); // Yank bogus edge
1558 #ifndef PRODUCT
1559 if( VerifyIterativeGVN ) {
1560 for ( int i = 0; i < _verify_window_size; i++ ) {
1561 if ( _verify_window[i] == old )
1562 _verify_window[i] = nn;
1563 }
1564 }
1565 #endif
1566 _worklist.remove(temp); // this can be necessary
1567 temp->destruct(); // reuse the _idx of this little guy
1568 }
1569
1570 //------------------------------add_users_to_worklist--------------------------
add_users_to_worklist0(Node * n)1571 void PhaseIterGVN::add_users_to_worklist0( Node *n ) {
1572 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1573 _worklist.push(n->fast_out(i)); // Push on worklist
1574 }
1575 }
1576
1577 // Return counted loop Phi if as a counted loop exit condition, cmp
1578 // compares the the induction variable with n
countedloop_phi_from_cmp(CmpINode * cmp,Node * n)1579 static PhiNode* countedloop_phi_from_cmp(CmpINode* cmp, Node* n) {
1580 for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) {
1581 Node* bol = cmp->fast_out(i);
1582 for (DUIterator_Fast i2max, i2 = bol->fast_outs(i2max); i2 < i2max; i2++) {
1583 Node* iff = bol->fast_out(i2);
1584 if (iff->is_CountedLoopEnd()) {
1585 CountedLoopEndNode* cle = iff->as_CountedLoopEnd();
1586 if (cle->limit() == n) {
1587 PhiNode* phi = cle->phi();
1588 if (phi != NULL) {
1589 return phi;
1590 }
1591 }
1592 }
1593 }
1594 }
1595 return NULL;
1596 }
1597
add_users_to_worklist(Node * n)1598 void PhaseIterGVN::add_users_to_worklist( Node *n ) {
1599 add_users_to_worklist0(n);
1600
1601 // Move users of node to worklist
1602 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1603 Node* use = n->fast_out(i); // Get use
1604
1605 if( use->is_Multi() || // Multi-definer? Push projs on worklist
1606 use->is_Store() ) // Enable store/load same address
1607 add_users_to_worklist0(use);
1608
1609 // If we changed the receiver type to a call, we need to revisit
1610 // the Catch following the call. It's looking for a non-NULL
1611 // receiver to know when to enable the regular fall-through path
1612 // in addition to the NullPtrException path.
1613 if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) {
1614 Node* p = use->as_CallDynamicJava()->proj_out_or_null(TypeFunc::Control);
1615 if (p != NULL) {
1616 add_users_to_worklist0(p);
1617 }
1618 }
1619
1620 uint use_op = use->Opcode();
1621 if(use->is_Cmp()) { // Enable CMP/BOOL optimization
1622 add_users_to_worklist(use); // Put Bool on worklist
1623 if (use->outcnt() > 0) {
1624 Node* bol = use->raw_out(0);
1625 if (bol->outcnt() > 0) {
1626 Node* iff = bol->raw_out(0);
1627 if (iff->outcnt() == 2) {
1628 // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the
1629 // phi merging either 0 or 1 onto the worklist
1630 Node* ifproj0 = iff->raw_out(0);
1631 Node* ifproj1 = iff->raw_out(1);
1632 if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) {
1633 Node* region0 = ifproj0->raw_out(0);
1634 Node* region1 = ifproj1->raw_out(0);
1635 if( region0 == region1 )
1636 add_users_to_worklist0(region0);
1637 }
1638 }
1639 }
1640 }
1641 if (use_op == Op_CmpI) {
1642 Node* phi = countedloop_phi_from_cmp((CmpINode*)use, n);
1643 if (phi != NULL) {
1644 // If an opaque node feeds into the limit condition of a
1645 // CountedLoop, we need to process the Phi node for the
1646 // induction variable when the opaque node is removed:
1647 // the range of values taken by the Phi is now known and
1648 // so its type is also known.
1649 _worklist.push(phi);
1650 }
1651 Node* in1 = use->in(1);
1652 for (uint i = 0; i < in1->outcnt(); i++) {
1653 if (in1->raw_out(i)->Opcode() == Op_CastII) {
1654 Node* castii = in1->raw_out(i);
1655 if (castii->in(0) != NULL && castii->in(0)->in(0) != NULL && castii->in(0)->in(0)->is_If()) {
1656 Node* ifnode = castii->in(0)->in(0);
1657 if (ifnode->in(1) != NULL && ifnode->in(1)->is_Bool() && ifnode->in(1)->in(1) == use) {
1658 // Reprocess a CastII node that may depend on an
1659 // opaque node value when the opaque node is
1660 // removed. In case it carries a dependency we can do
1661 // a better job of computing its type.
1662 _worklist.push(castii);
1663 }
1664 }
1665 }
1666 }
1667 }
1668 }
1669
1670 // If changed Cast input, check Phi users for simple cycles
1671 if (use->is_ConstraintCast()) {
1672 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
1673 Node* u = use->fast_out(i2);
1674 if (u->is_Phi())
1675 _worklist.push(u);
1676 }
1677 }
1678 // If changed LShift inputs, check RShift users for useless sign-ext
1679 if( use_op == Op_LShiftI ) {
1680 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
1681 Node* u = use->fast_out(i2);
1682 if (u->Opcode() == Op_RShiftI)
1683 _worklist.push(u);
1684 }
1685 }
1686 // If changed AddI/SubI inputs, check CmpU for range check optimization.
1687 if (use_op == Op_AddI || use_op == Op_SubI) {
1688 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
1689 Node* u = use->fast_out(i2);
1690 if (u->is_Cmp() && (u->Opcode() == Op_CmpU)) {
1691 _worklist.push(u);
1692 }
1693 }
1694 }
1695 // If changed AddP inputs, check Stores for loop invariant
1696 if( use_op == Op_AddP ) {
1697 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
1698 Node* u = use->fast_out(i2);
1699 if (u->is_Mem())
1700 _worklist.push(u);
1701 }
1702 }
1703 // If changed initialization activity, check dependent Stores
1704 if (use_op == Op_Allocate || use_op == Op_AllocateArray) {
1705 InitializeNode* init = use->as_Allocate()->initialization();
1706 if (init != NULL) {
1707 Node* imem = init->proj_out_or_null(TypeFunc::Memory);
1708 if (imem != NULL) add_users_to_worklist0(imem);
1709 }
1710 }
1711 if (use_op == Op_Initialize) {
1712 Node* imem = use->as_Initialize()->proj_out_or_null(TypeFunc::Memory);
1713 if (imem != NULL) add_users_to_worklist0(imem);
1714 }
1715 // Loading the java mirror from a Klass requires two loads and the type
1716 // of the mirror load depends on the type of 'n'. See LoadNode::Value().
1717 // LoadBarrier?(LoadP(LoadP(AddP(foo:Klass, #java_mirror))))
1718 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1719 bool has_load_barriers = bs->has_load_barriers();
1720
1721 if (use_op == Op_LoadP && use->bottom_type()->isa_rawptr()) {
1722 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
1723 Node* u = use->fast_out(i2);
1724 const Type* ut = u->bottom_type();
1725 if (u->Opcode() == Op_LoadP && ut->isa_instptr()) {
1726 if (has_load_barriers) {
1727 // Search for load barriers behind the load
1728 for (DUIterator_Fast i3max, i3 = u->fast_outs(i3max); i3 < i3max; i3++) {
1729 Node* b = u->fast_out(i3);
1730 if (bs->is_gc_barrier_node(b)) {
1731 _worklist.push(b);
1732 }
1733 }
1734 }
1735 _worklist.push(u);
1736 }
1737 }
1738 }
1739 #if INCLUDE_SHENANDOAHGC
1740 // TODO: Needed after the block above?
1741 if (use->is_ShenandoahBarrier()) {
1742 Node* cmp = use->find_out_with(Op_CmpP);
1743 if (cmp != NULL) {
1744 _worklist.push(cmp);
1745 }
1746 }
1747 #endif
1748 }
1749 }
1750
1751 /**
1752 * Remove the speculative part of all types that we know of
1753 */
remove_speculative_types()1754 void PhaseIterGVN::remove_speculative_types() {
1755 assert(UseTypeSpeculation, "speculation is off");
1756 for (uint i = 0; i < _types.Size(); i++) {
1757 const Type* t = _types.fast_lookup(i);
1758 if (t != NULL) {
1759 _types.map(i, t->remove_speculative());
1760 }
1761 }
1762 _table.check_no_speculative_types();
1763 }
1764
1765 // Check if the type of a divisor of a Div or Mod node includes zero.
no_dependent_zero_check(Node * n) const1766 bool PhaseIterGVN::no_dependent_zero_check(Node* n) const {
1767 switch (n->Opcode()) {
1768 case Op_DivI:
1769 case Op_ModI: {
1770 // Type of divisor includes 0?
1771 if (n->in(2)->is_top()) {
1772 // 'n' is dead. Treat as if zero check is still there to avoid any further optimizations.
1773 return false;
1774 }
1775 const TypeInt* type_divisor = type(n->in(2))->is_int();
1776 return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
1777 }
1778 case Op_DivL:
1779 case Op_ModL: {
1780 // Type of divisor includes 0?
1781 if (n->in(2)->is_top()) {
1782 // 'n' is dead. Treat as if zero check is still there to avoid any further optimizations.
1783 return false;
1784 }
1785 const TypeLong* type_divisor = type(n->in(2))->is_long();
1786 return (type_divisor->_hi < 0 || type_divisor->_lo > 0);
1787 }
1788 }
1789 return true;
1790 }
1791
1792 //=============================================================================
1793 #ifndef PRODUCT
1794 uint PhaseCCP::_total_invokes = 0;
1795 uint PhaseCCP::_total_constants = 0;
1796 #endif
1797 //------------------------------PhaseCCP---------------------------------------
1798 // Conditional Constant Propagation, ala Wegman & Zadeck
PhaseCCP(PhaseIterGVN * igvn)1799 PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) {
1800 NOT_PRODUCT( clear_constants(); )
1801 assert( _worklist.size() == 0, "" );
1802 // Clear out _nodes from IterGVN. Must be clear to transform call.
1803 _nodes.clear(); // Clear out from IterGVN
1804 analyze();
1805 }
1806
1807 #ifndef PRODUCT
1808 //------------------------------~PhaseCCP--------------------------------------
~PhaseCCP()1809 PhaseCCP::~PhaseCCP() {
1810 inc_invokes();
1811 _total_constants += count_constants();
1812 }
1813 #endif
1814
1815
1816 #ifdef ASSERT
ccp_type_widens(const Type * t,const Type * t0)1817 static bool ccp_type_widens(const Type* t, const Type* t0) {
1818 assert(t->meet(t0) == t, "Not monotonic");
1819 switch (t->base() == t0->base() ? t->base() : Type::Top) {
1820 case Type::Int:
1821 assert(t0->isa_int()->_widen <= t->isa_int()->_widen, "widen increases");
1822 break;
1823 case Type::Long:
1824 assert(t0->isa_long()->_widen <= t->isa_long()->_widen, "widen increases");
1825 break;
1826 default:
1827 break;
1828 }
1829 return true;
1830 }
1831 #endif //ASSERT
1832
1833 //------------------------------analyze----------------------------------------
analyze()1834 void PhaseCCP::analyze() {
1835 // Initialize all types to TOP, optimistic analysis
1836 for (int i = C->unique() - 1; i >= 0; i--) {
1837 _types.map(i,Type::TOP);
1838 }
1839
1840 // Push root onto worklist
1841 Unique_Node_List worklist;
1842 worklist.push(C->root());
1843
1844 // Pull from worklist; compute new value; push changes out.
1845 // This loop is the meat of CCP.
1846 while( worklist.size() ) {
1847 Node *n = worklist.pop();
1848 const Type *t = n->Value(this);
1849 if (t != type(n)) {
1850 assert(ccp_type_widens(t, type(n)), "ccp type must widen");
1851 #ifndef PRODUCT
1852 if( TracePhaseCCP ) {
1853 t->dump();
1854 do { tty->print("\t"); } while (tty->position() < 16);
1855 n->dump();
1856 }
1857 #endif
1858 set_type(n, t);
1859 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1860 Node* m = n->fast_out(i); // Get user
1861 if (m->is_Region()) { // New path to Region? Must recheck Phis too
1862 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
1863 Node* p = m->fast_out(i2); // Propagate changes to uses
1864 if (p->bottom_type() != type(p)) { // If not already bottomed out
1865 worklist.push(p); // Propagate change to user
1866 }
1867 }
1868 }
1869 // If we changed the receiver type to a call, we need to revisit
1870 // the Catch following the call. It's looking for a non-NULL
1871 // receiver to know when to enable the regular fall-through path
1872 // in addition to the NullPtrException path
1873 if (m->is_Call()) {
1874 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
1875 Node* p = m->fast_out(i2); // Propagate changes to uses
1876 if (p->is_Proj() && p->as_Proj()->_con == TypeFunc::Control) {
1877 Node* catch_node = p->find_out_with(Op_Catch);
1878 if (catch_node != NULL) {
1879 worklist.push(catch_node);
1880 }
1881 }
1882 }
1883 }
1884 if (m->bottom_type() != type(m)) { // If not already bottomed out
1885 worklist.push(m); // Propagate change to user
1886 }
1887
1888 // CmpU nodes can get their type information from two nodes up in the
1889 // graph (instead of from the nodes immediately above). Make sure they
1890 // are added to the worklist if nodes they depend on are updated, since
1891 // they could be missed and get wrong types otherwise.
1892 uint m_op = m->Opcode();
1893 if (m_op == Op_AddI || m_op == Op_SubI) {
1894 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
1895 Node* p = m->fast_out(i2); // Propagate changes to uses
1896 if (p->Opcode() == Op_CmpU) {
1897 // Got a CmpU which might need the new type information from node n.
1898 if(p->bottom_type() != type(p)) { // If not already bottomed out
1899 worklist.push(p); // Propagate change to user
1900 }
1901 }
1902 }
1903 }
1904 // If n is used in a counted loop exit condition then the type
1905 // of the counted loop's Phi depends on the type of n. See
1906 // PhiNode::Value().
1907 if (m_op == Op_CmpI) {
1908 PhiNode* phi = countedloop_phi_from_cmp((CmpINode*)m, n);
1909 if (phi != NULL) {
1910 worklist.push(phi);
1911 }
1912 }
1913 // Loading the java mirror from a Klass requires two loads and the type
1914 // of the mirror load depends on the type of 'n'. See LoadNode::Value().
1915 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
1916 bool has_load_barriers = bs->has_load_barriers();
1917
1918 if (m_op == Op_LoadP && m->bottom_type()->isa_rawptr()) {
1919 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
1920 Node* u = m->fast_out(i2);
1921 const Type* ut = u->bottom_type();
1922 if (u->Opcode() == Op_LoadP && ut->isa_instptr() && ut != type(u)) {
1923 if (has_load_barriers) {
1924 // Search for load barriers behind the load
1925 for (DUIterator_Fast i3max, i3 = u->fast_outs(i3max); i3 < i3max; i3++) {
1926 Node* b = u->fast_out(i3);
1927 if (bs->is_gc_barrier_node(b)) {
1928 _worklist.push(b);
1929 }
1930 }
1931 }
1932 worklist.push(u);
1933 }
1934 }
1935 }
1936 }
1937 }
1938 }
1939 }
1940
1941 //------------------------------do_transform-----------------------------------
1942 // Top level driver for the recursive transformer
do_transform()1943 void PhaseCCP::do_transform() {
1944 // Correct leaves of new-space Nodes; they point to old-space.
1945 C->set_root( transform(C->root())->as_Root() );
1946 assert( C->top(), "missing TOP node" );
1947 assert( C->root(), "missing root" );
1948 }
1949
1950 //------------------------------transform--------------------------------------
1951 // Given a Node in old-space, clone him into new-space.
1952 // Convert any of his old-space children into new-space children.
transform(Node * n)1953 Node *PhaseCCP::transform( Node *n ) {
1954 Node *new_node = _nodes[n->_idx]; // Check for transformed node
1955 if( new_node != NULL )
1956 return new_node; // Been there, done that, return old answer
1957 new_node = transform_once(n); // Check for constant
1958 _nodes.map( n->_idx, new_node ); // Flag as having been cloned
1959
1960 // Allocate stack of size _nodes.Size()/2 to avoid frequent realloc
1961 GrowableArray <Node *> trstack(C->live_nodes() >> 1);
1962
1963 trstack.push(new_node); // Process children of cloned node
1964 while ( trstack.is_nonempty() ) {
1965 Node *clone = trstack.pop();
1966 uint cnt = clone->req();
1967 for( uint i = 0; i < cnt; i++ ) { // For all inputs do
1968 Node *input = clone->in(i);
1969 if( input != NULL ) { // Ignore NULLs
1970 Node *new_input = _nodes[input->_idx]; // Check for cloned input node
1971 if( new_input == NULL ) {
1972 new_input = transform_once(input); // Check for constant
1973 _nodes.map( input->_idx, new_input );// Flag as having been cloned
1974 trstack.push(new_input);
1975 }
1976 assert( new_input == clone->in(i), "insanity check");
1977 }
1978 }
1979 }
1980 return new_node;
1981 }
1982
1983
1984 //------------------------------transform_once---------------------------------
1985 // For PhaseCCP, transformation is IDENTITY unless Node computed a constant.
transform_once(Node * n)1986 Node *PhaseCCP::transform_once( Node *n ) {
1987 const Type *t = type(n);
1988 // Constant? Use constant Node instead
1989 if( t->singleton() ) {
1990 Node *nn = n; // Default is to return the original constant
1991 if( t == Type::TOP ) {
1992 // cache my top node on the Compile instance
1993 if( C->cached_top_node() == NULL || C->cached_top_node()->in(0) == NULL ) {
1994 C->set_cached_top_node(ConNode::make(Type::TOP));
1995 set_type(C->top(), Type::TOP);
1996 }
1997 nn = C->top();
1998 }
1999 if( !n->is_Con() ) {
2000 if( t != Type::TOP ) {
2001 nn = makecon(t); // ConNode::make(t);
2002 NOT_PRODUCT( inc_constants(); )
2003 } else if( n->is_Region() ) { // Unreachable region
2004 // Note: nn == C->top()
2005 n->set_req(0, NULL); // Cut selfreference
2006 bool progress = true;
2007 uint max = n->outcnt();
2008 DUIterator i;
2009 while (progress) {
2010 progress = false;
2011 // Eagerly remove dead phis to avoid phis copies creation.
2012 for (i = n->outs(); n->has_out(i); i++) {
2013 Node* m = n->out(i);
2014 if (m->is_Phi()) {
2015 assert(type(m) == Type::TOP, "Unreachable region should not have live phis.");
2016 replace_node(m, nn);
2017 if (max != n->outcnt()) {
2018 progress = true;
2019 i = n->refresh_out_pos(i);
2020 max = n->outcnt();
2021 }
2022 }
2023 }
2024 }
2025 }
2026 replace_node(n,nn); // Update DefUse edges for new constant
2027 }
2028 return nn;
2029 }
2030
2031 // If x is a TypeNode, capture any more-precise type permanently into Node
2032 if (t != n->bottom_type()) {
2033 hash_delete(n); // changing bottom type may force a rehash
2034 n->raise_bottom_type(t);
2035 _worklist.push(n); // n re-enters the hash table via the worklist
2036 }
2037
2038 // TEMPORARY fix to ensure that 2nd GVN pass eliminates NULL checks
2039 switch( n->Opcode() ) {
2040 case Op_FastLock: // Revisit FastLocks for lock coarsening
2041 case Op_If:
2042 case Op_CountedLoopEnd:
2043 case Op_Region:
2044 case Op_Loop:
2045 case Op_CountedLoop:
2046 case Op_Conv2B:
2047 case Op_Opaque1:
2048 case Op_Opaque2:
2049 _worklist.push(n);
2050 break;
2051 default:
2052 break;
2053 }
2054
2055 return n;
2056 }
2057
2058 //---------------------------------saturate------------------------------------
saturate(const Type * new_type,const Type * old_type,const Type * limit_type) const2059 const Type* PhaseCCP::saturate(const Type* new_type, const Type* old_type,
2060 const Type* limit_type) const {
2061 const Type* wide_type = new_type->widen(old_type, limit_type);
2062 if (wide_type != new_type) { // did we widen?
2063 // If so, we may have widened beyond the limit type. Clip it back down.
2064 new_type = wide_type->filter(limit_type);
2065 }
2066 return new_type;
2067 }
2068
2069 //------------------------------print_statistics-------------------------------
2070 #ifndef PRODUCT
print_statistics()2071 void PhaseCCP::print_statistics() {
2072 tty->print_cr("CCP: %d constants found: %d", _total_invokes, _total_constants);
2073 }
2074 #endif
2075
2076
2077 //=============================================================================
2078 #ifndef PRODUCT
2079 uint PhasePeephole::_total_peepholes = 0;
2080 #endif
2081 //------------------------------PhasePeephole----------------------------------
2082 // Conditional Constant Propagation, ala Wegman & Zadeck
PhasePeephole(PhaseRegAlloc * regalloc,PhaseCFG & cfg)2083 PhasePeephole::PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg )
2084 : PhaseTransform(Peephole), _regalloc(regalloc), _cfg(cfg) {
2085 NOT_PRODUCT( clear_peepholes(); )
2086 }
2087
2088 #ifndef PRODUCT
2089 //------------------------------~PhasePeephole---------------------------------
~PhasePeephole()2090 PhasePeephole::~PhasePeephole() {
2091 _total_peepholes += count_peepholes();
2092 }
2093 #endif
2094
2095 //------------------------------transform--------------------------------------
transform(Node * n)2096 Node *PhasePeephole::transform( Node *n ) {
2097 ShouldNotCallThis();
2098 return NULL;
2099 }
2100
2101 //------------------------------do_transform-----------------------------------
do_transform()2102 void PhasePeephole::do_transform() {
2103 bool method_name_not_printed = true;
2104
2105 // Examine each basic block
2106 for (uint block_number = 1; block_number < _cfg.number_of_blocks(); ++block_number) {
2107 Block* block = _cfg.get_block(block_number);
2108 bool block_not_printed = true;
2109
2110 // and each instruction within a block
2111 uint end_index = block->number_of_nodes();
2112 // block->end_idx() not valid after PhaseRegAlloc
2113 for( uint instruction_index = 1; instruction_index < end_index; ++instruction_index ) {
2114 Node *n = block->get_node(instruction_index);
2115 if( n->is_Mach() ) {
2116 MachNode *m = n->as_Mach();
2117 int deleted_count = 0;
2118 // check for peephole opportunities
2119 MachNode *m2 = m->peephole(block, instruction_index, _regalloc, deleted_count);
2120 if( m2 != NULL ) {
2121 #ifndef PRODUCT
2122 if( PrintOptoPeephole ) {
2123 // Print method, first time only
2124 if( C->method() && method_name_not_printed ) {
2125 C->method()->print_short_name(); tty->cr();
2126 method_name_not_printed = false;
2127 }
2128 // Print this block
2129 if( Verbose && block_not_printed) {
2130 tty->print_cr("in block");
2131 block->dump();
2132 block_not_printed = false;
2133 }
2134 // Print instructions being deleted
2135 for( int i = (deleted_count - 1); i >= 0; --i ) {
2136 block->get_node(instruction_index-i)->as_Mach()->format(_regalloc); tty->cr();
2137 }
2138 tty->print_cr("replaced with");
2139 // Print new instruction
2140 m2->format(_regalloc);
2141 tty->print("\n\n");
2142 }
2143 #endif
2144 // Remove old nodes from basic block and update instruction_index
2145 // (old nodes still exist and may have edges pointing to them
2146 // as register allocation info is stored in the allocator using
2147 // the node index to live range mappings.)
2148 uint safe_instruction_index = (instruction_index - deleted_count);
2149 for( ; (instruction_index > safe_instruction_index); --instruction_index ) {
2150 block->remove_node( instruction_index );
2151 }
2152 // install new node after safe_instruction_index
2153 block->insert_node(m2, safe_instruction_index + 1);
2154 end_index = block->number_of_nodes() - 1; // Recompute new block size
2155 NOT_PRODUCT( inc_peepholes(); )
2156 }
2157 }
2158 }
2159 }
2160 }
2161
2162 //------------------------------print_statistics-------------------------------
2163 #ifndef PRODUCT
print_statistics()2164 void PhasePeephole::print_statistics() {
2165 tty->print_cr("Peephole: peephole rules applied: %d", _total_peepholes);
2166 }
2167 #endif
2168
2169
2170 //=============================================================================
2171 //------------------------------set_req_X--------------------------------------
set_req_X(uint i,Node * n,PhaseIterGVN * igvn)2172 void Node::set_req_X( uint i, Node *n, PhaseIterGVN *igvn ) {
2173 assert( is_not_dead(n), "can not use dead node");
2174 assert( igvn->hash_find(this) != this, "Need to remove from hash before changing edges" );
2175 Node *old = in(i);
2176 set_req(i, n);
2177
2178 // old goes dead?
2179 if( old ) {
2180 switch (old->outcnt()) {
2181 case 0:
2182 // Put into the worklist to kill later. We do not kill it now because the
2183 // recursive kill will delete the current node (this) if dead-loop exists
2184 if (!old->is_top())
2185 igvn->_worklist.push( old );
2186 break;
2187 case 1:
2188 if( old->is_Store() || old->has_special_unique_user() )
2189 igvn->add_users_to_worklist( old );
2190 break;
2191 case 2:
2192 if( old->is_Store() )
2193 igvn->add_users_to_worklist( old );
2194 if( old->Opcode() == Op_Region )
2195 igvn->_worklist.push(old);
2196 break;
2197 case 3:
2198 if( old->Opcode() == Op_Region ) {
2199 igvn->_worklist.push(old);
2200 igvn->add_users_to_worklist( old );
2201 }
2202 break;
2203 default:
2204 break;
2205 }
2206 #if INCLUDE_SHENANDOAHGC
2207 if (UseShenandoahGC) {
2208 // TODO: Should we call this for ZGC as well?
2209 BarrierSet::barrier_set()->barrier_set_c2()->enqueue_useful_gc_barrier(igvn->_worklist, old);
2210 }
2211 #endif
2212 }
2213
2214 }
2215
2216 //-------------------------------replace_by-----------------------------------
2217 // Using def-use info, replace one node for another. Follow the def-use info
2218 // to all users of the OLD node. Then make all uses point to the NEW node.
replace_by(Node * new_node)2219 void Node::replace_by(Node *new_node) {
2220 assert(!is_top(), "top node has no DU info");
2221 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; ) {
2222 Node* use = last_out(i);
2223 uint uses_found = 0;
2224 for (uint j = 0; j < use->len(); j++) {
2225 if (use->in(j) == this) {
2226 if (j < use->req())
2227 use->set_req(j, new_node);
2228 else use->set_prec(j, new_node);
2229 uses_found++;
2230 }
2231 }
2232 i -= uses_found; // we deleted 1 or more copies of this edge
2233 }
2234 }
2235
2236 //=============================================================================
2237 //-----------------------------------------------------------------------------
grow(uint i)2238 void Type_Array::grow( uint i ) {
2239 if( !_max ) {
2240 _max = 1;
2241 _types = (const Type**)_a->Amalloc( _max * sizeof(Type*) );
2242 _types[0] = NULL;
2243 }
2244 uint old = _max;
2245 while( i >= _max ) _max <<= 1; // Double to fit
2246 _types = (const Type**)_a->Arealloc( _types, old*sizeof(Type*),_max*sizeof(Type*));
2247 memset( &_types[old], 0, (_max-old)*sizeof(Type*) );
2248 }
2249
2250 //------------------------------dump-------------------------------------------
2251 #ifndef PRODUCT
dump() const2252 void Type_Array::dump() const {
2253 uint max = Size();
2254 for( uint i = 0; i < max; i++ ) {
2255 if( _types[i] != NULL ) {
2256 tty->print(" %d\t== ", i); _types[i]->dump(); tty->cr();
2257 }
2258 }
2259 }
2260 #endif
2261