forked from openjdk/jdk17u-dev
/
phaseX.cpp
2194 lines (2015 loc) · 75.7 KB
/
phaseX.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "precompiled.hpp"
#include "gc/shared/barrierSet.hpp"
#include "gc/shared/c2/barrierSetC2.hpp"
#include "memory/allocation.inline.hpp"
#include "memory/resourceArea.hpp"
#include "opto/block.hpp"
#include "opto/callnode.hpp"
#include "opto/castnode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/idealGraphPrinter.hpp"
#include "opto/loopnode.hpp"
#include "opto/machnode.hpp"
#include "opto/opcodes.hpp"
#include "opto/phaseX.hpp"
#include "opto/regalloc.hpp"
#include "opto/rootnode.hpp"
#include "utilities/macros.hpp"
#include "utilities/powerOfTwo.hpp"
//=============================================================================
#define NODE_HASH_MINIMUM_SIZE 255
//------------------------------NodeHash---------------------------------------
NodeHash::NodeHash(uint est_max_size) :
_a(Thread::current()->resource_area()),
_max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
_inserts(0), _insert_limit( insert_limit() ),
_table( NEW_ARENA_ARRAY( _a , Node* , _max ) ) // (Node**)_a->Amalloc(_max * sizeof(Node*)) ),
#ifndef PRODUCT
, _grows(0),_look_probes(0), _lookup_hits(0), _lookup_misses(0),
_insert_probes(0), _delete_probes(0), _delete_hits(0), _delete_misses(0),
_total_inserts(0), _total_insert_probes(0)
#endif
{
// _sentinel must be in the current node space
_sentinel = new ProjNode(NULL, TypeFunc::Control);
memset(_table,0,sizeof(Node*)*_max);
}
//------------------------------NodeHash---------------------------------------
NodeHash::NodeHash(Arena *arena, uint est_max_size) :
_a(arena),
_max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
_inserts(0), _insert_limit( insert_limit() ),
_table( NEW_ARENA_ARRAY( _a , Node* , _max ) )
#ifndef PRODUCT
, _grows(0),_look_probes(0), _lookup_hits(0), _lookup_misses(0),
_insert_probes(0), _delete_probes(0), _delete_hits(0), _delete_misses(0),
_total_inserts(0), _total_insert_probes(0)
#endif
{
// _sentinel must be in the current node space
_sentinel = new ProjNode(NULL, TypeFunc::Control);
memset(_table,0,sizeof(Node*)*_max);
}
//------------------------------NodeHash---------------------------------------
NodeHash::NodeHash(NodeHash *nh) {
debug_only(_table = (Node**)badAddress); // interact correctly w/ operator=
// just copy in all the fields
*this = *nh;
// nh->_sentinel must be in the current node space
}
void NodeHash::replace_with(NodeHash *nh) {
debug_only(_table = (Node**)badAddress); // interact correctly w/ operator=
// just copy in all the fields
*this = *nh;
// nh->_sentinel must be in the current node space
}
//------------------------------hash_find--------------------------------------
// Find in hash table
Node *NodeHash::hash_find( const Node *n ) {
// ((Node*)n)->set_hash( n->hash() );
uint hash = n->hash();
if (hash == Node::NO_HASH) {
NOT_PRODUCT( _lookup_misses++ );
return NULL;
}
uint key = hash & (_max-1);
uint stride = key | 0x01;
NOT_PRODUCT( _look_probes++ );
Node *k = _table[key]; // Get hashed value
if( !k ) { // ?Miss?
NOT_PRODUCT( _lookup_misses++ );
return NULL; // Miss!
}
int op = n->Opcode();
uint req = n->req();
while( 1 ) { // While probing hash table
if( k->req() == req && // Same count of inputs
k->Opcode() == op ) { // Same Opcode
for( uint i=0; i<req; i++ )
if( n->in(i)!=k->in(i)) // Different inputs?
goto collision; // "goto" is a speed hack...
if( n->cmp(*k) ) { // Check for any special bits
NOT_PRODUCT( _lookup_hits++ );
return k; // Hit!
}
}
collision:
NOT_PRODUCT( _look_probes++ );
key = (key + stride/*7*/) & (_max-1); // Stride through table with relative prime
k = _table[key]; // Get hashed value
if( !k ) { // ?Miss?
NOT_PRODUCT( _lookup_misses++ );
return NULL; // Miss!
}
}
ShouldNotReachHere();
return NULL;
}
//------------------------------hash_find_insert-------------------------------
// Find in hash table, insert if not already present
// Used to preserve unique entries in hash table
Node *NodeHash::hash_find_insert( Node *n ) {
// n->set_hash( );
uint hash = n->hash();
if (hash == Node::NO_HASH) {
NOT_PRODUCT( _lookup_misses++ );
return NULL;
}
uint key = hash & (_max-1);
uint stride = key | 0x01; // stride must be relatively prime to table siz
uint first_sentinel = 0; // replace a sentinel if seen.
NOT_PRODUCT( _look_probes++ );
Node *k = _table[key]; // Get hashed value
if( !k ) { // ?Miss?
NOT_PRODUCT( _lookup_misses++ );
_table[key] = n; // Insert into table!
debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
check_grow(); // Grow table if insert hit limit
return NULL; // Miss!
}
else if( k == _sentinel ) {
first_sentinel = key; // Can insert here
}
int op = n->Opcode();
uint req = n->req();
while( 1 ) { // While probing hash table
if( k->req() == req && // Same count of inputs
k->Opcode() == op ) { // Same Opcode
for( uint i=0; i<req; i++ )
if( n->in(i)!=k->in(i)) // Different inputs?
goto collision; // "goto" is a speed hack...
if( n->cmp(*k) ) { // Check for any special bits
NOT_PRODUCT( _lookup_hits++ );
return k; // Hit!
}
}
collision:
NOT_PRODUCT( _look_probes++ );
key = (key + stride) & (_max-1); // Stride through table w/ relative prime
k = _table[key]; // Get hashed value
if( !k ) { // ?Miss?
NOT_PRODUCT( _lookup_misses++ );
key = (first_sentinel == 0) ? key : first_sentinel; // ?saw sentinel?
_table[key] = n; // Insert into table!
debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
check_grow(); // Grow table if insert hit limit
return NULL; // Miss!
}
else if( first_sentinel == 0 && k == _sentinel ) {
first_sentinel = key; // Can insert here
}
}
ShouldNotReachHere();
return NULL;
}
//------------------------------hash_insert------------------------------------
// Insert into hash table
void NodeHash::hash_insert( Node *n ) {
// // "conflict" comments -- print nodes that conflict
// bool conflict = false;
// n->set_hash();
uint hash = n->hash();
if (hash == Node::NO_HASH) {
return;
}
check_grow();
uint key = hash & (_max-1);
uint stride = key | 0x01;
while( 1 ) { // While probing hash table
NOT_PRODUCT( _insert_probes++ );
Node *k = _table[key]; // Get hashed value
if( !k || (k == _sentinel) ) break; // Found a slot
assert( k != n, "already inserted" );
// if( PrintCompilation && PrintOptoStatistics && Verbose ) { tty->print(" conflict: "); k->dump(); conflict = true; }
key = (key + stride) & (_max-1); // Stride through table w/ relative prime
}
_table[key] = n; // Insert into table!
debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
// if( conflict ) { n->dump(); }
}
//------------------------------hash_delete------------------------------------
// Replace in hash table with sentinel
bool NodeHash::hash_delete( const Node *n ) {
Node *k;
uint hash = n->hash();
if (hash == Node::NO_HASH) {
NOT_PRODUCT( _delete_misses++ );
return false;
}
uint key = hash & (_max-1);
uint stride = key | 0x01;
debug_only( uint counter = 0; );
for( ; /* (k != NULL) && (k != _sentinel) */; ) {
debug_only( counter++ );
NOT_PRODUCT( _delete_probes++ );
k = _table[key]; // Get hashed value
if( !k ) { // Miss?
NOT_PRODUCT( _delete_misses++ );
return false; // Miss! Not in chain
}
else if( n == k ) {
NOT_PRODUCT( _delete_hits++ );
_table[key] = _sentinel; // Hit! Label as deleted entry
debug_only(((Node*)n)->exit_hash_lock()); // Unlock the node upon removal from table.
return true;
}
else {
// collision: move through table with prime offset
key = (key + stride/*7*/) & (_max-1);
assert( counter <= _insert_limit, "Cycle in hash-table");
}
}
ShouldNotReachHere();
return false;
}
//------------------------------round_up---------------------------------------
// Round up to nearest power of 2
uint NodeHash::round_up(uint x) {
x += (x >> 2); // Add 25% slop
return MAX2(16U, round_up_power_of_2(x));
}
//------------------------------grow-------------------------------------------
// Grow _table to next power of 2 and insert old entries
void NodeHash::grow() {
// Record old state
uint old_max = _max;
Node **old_table = _table;
// Construct new table with twice the space
#ifndef PRODUCT
_grows++;
_total_inserts += _inserts;
_total_insert_probes += _insert_probes;
_insert_probes = 0;
#endif
_inserts = 0;
_max = _max << 1;
_table = NEW_ARENA_ARRAY( _a , Node* , _max ); // (Node**)_a->Amalloc( _max * sizeof(Node*) );
memset(_table,0,sizeof(Node*)*_max);
_insert_limit = insert_limit();
// Insert old entries into the new table
for( uint i = 0; i < old_max; i++ ) {
Node *m = *old_table++;
if( !m || m == _sentinel ) continue;
debug_only(m->exit_hash_lock()); // Unlock the node upon removal from old table.
hash_insert(m);
}
}
//------------------------------clear------------------------------------------
// Clear all entries in _table to NULL but keep storage
void NodeHash::clear() {
#ifdef ASSERT
// Unlock all nodes upon removal from table.
for (uint i = 0; i < _max; i++) {
Node* n = _table[i];
if (!n || n == _sentinel) continue;
n->exit_hash_lock();
}
#endif
memset( _table, 0, _max * sizeof(Node*) );
}
//-----------------------remove_useless_nodes----------------------------------
// Remove useless nodes from value table,
// implementation does not depend on hash function
void NodeHash::remove_useless_nodes(VectorSet &useful) {
// Dead nodes in the hash table inherited from GVN should not replace
// existing nodes, remove dead nodes.
uint max = size();
Node *sentinel_node = sentinel();
for( uint i = 0; i < max; ++i ) {
Node *n = at(i);
if(n != NULL && n != sentinel_node && !useful.test(n->_idx)) {
debug_only(n->exit_hash_lock()); // Unlock the node when removed
_table[i] = sentinel_node; // Replace with placeholder
}
}
}
void NodeHash::check_no_speculative_types() {
#ifdef ASSERT
uint max = size();
Unique_Node_List live_nodes;
Compile::current()->identify_useful_nodes(live_nodes);
Node *sentinel_node = sentinel();
for (uint i = 0; i < max; ++i) {
Node *n = at(i);
if (n != NULL &&
n != sentinel_node &&
n->is_Type() &&
live_nodes.member(n)) {
TypeNode* tn = n->as_Type();
const Type* t = tn->type();
const Type* t_no_spec = t->remove_speculative();
assert(t == t_no_spec, "dead node in hash table or missed node during speculative cleanup");
}
}
#endif
}
#ifndef PRODUCT
//------------------------------dump-------------------------------------------
// Dump statistics for the hash table
void NodeHash::dump() {
_total_inserts += _inserts;
_total_insert_probes += _insert_probes;
if (PrintCompilation && PrintOptoStatistics && Verbose && (_inserts > 0)) {
if (WizardMode) {
for (uint i=0; i<_max; i++) {
if (_table[i])
tty->print("%d/%d/%d ",i,_table[i]->hash()&(_max-1),_table[i]->_idx);
}
}
tty->print("\nGVN Hash stats: %d grows to %d max_size\n", _grows, _max);
tty->print(" %d/%d (%8.1f%% full)\n", _inserts, _max, (double)_inserts/_max*100.0);
tty->print(" %dp/(%dh+%dm) (%8.2f probes/lookup)\n", _look_probes, _lookup_hits, _lookup_misses, (double)_look_probes/(_lookup_hits+_lookup_misses));
tty->print(" %dp/%di (%8.2f probes/insert)\n", _total_insert_probes, _total_inserts, (double)_total_insert_probes/_total_inserts);
// sentinels increase lookup cost, but not insert cost
assert((_lookup_misses+_lookup_hits)*4+100 >= _look_probes, "bad hash function");
assert( _inserts+(_inserts>>3) < _max, "table too full" );
assert( _inserts*3+100 >= _insert_probes, "bad hash function" );
}
}
Node *NodeHash::find_index(uint idx) { // For debugging
// Find an entry by its index value
for( uint i = 0; i < _max; i++ ) {
Node *m = _table[i];
if( !m || m == _sentinel ) continue;
if( m->_idx == (uint)idx ) return m;
}
return NULL;
}
#endif
#ifdef ASSERT
NodeHash::~NodeHash() {
// Unlock all nodes upon destruction of table.
if (_table != (Node**)badAddress) clear();
}
void NodeHash::operator=(const NodeHash& nh) {
// Unlock all nodes upon replacement of table.
if (&nh == this) return;
if (_table != (Node**)badAddress) clear();
memcpy((void*)this, (void*)&nh, sizeof(*this));
// Do not increment hash_lock counts again.
// Instead, be sure we never again use the source table.
((NodeHash*)&nh)->_table = (Node**)badAddress;
}
#endif
//=============================================================================
//------------------------------PhaseRemoveUseless-----------------------------
// 1) Use a breadthfirst walk to collect useful nodes reachable from root.
PhaseRemoveUseless::PhaseRemoveUseless(PhaseGVN* gvn, Unique_Node_List* worklist, PhaseNumber phase_num) : Phase(phase_num) {
// Implementation requires an edge from root to each SafePointNode
// at a backward branch. Inserted in add_safepoint().
// Identify nodes that are reachable from below, useful.
C->identify_useful_nodes(_useful);
// Update dead node list
C->update_dead_node_list(_useful);
// Remove all useless nodes from PhaseValues' recorded types
// Must be done before disconnecting nodes to preserve hash-table-invariant
gvn->remove_useless_nodes(_useful.member_set());
// Remove all useless nodes from future worklist
worklist->remove_useless_nodes(_useful.member_set());
// Disconnect 'useless' nodes that are adjacent to useful nodes
C->remove_useless_nodes(_useful);
}
//=============================================================================
//------------------------------PhaseRenumberLive------------------------------
// First, remove useless nodes (equivalent to identifying live nodes).
// Then, renumber live nodes.
//
// The set of live nodes is returned by PhaseRemoveUseless in the _useful structure.
// If the number of live nodes is 'x' (where 'x' == _useful.size()), then the
// PhaseRenumberLive updates the node ID of each node (the _idx field) with a unique
// value in the range [0, x).
//
// At the end of the PhaseRenumberLive phase, the compiler's count of unique nodes is
// updated to 'x' and the list of dead nodes is reset (as there are no dead nodes).
//
// The PhaseRenumberLive phase updates two data structures with the new node IDs.
// (1) The worklist is used by the PhaseIterGVN phase to identify nodes that must be
// processed. A new worklist (with the updated node IDs) is returned in 'new_worklist'.
// 'worklist' is cleared upon returning.
// (2) Type information (the field PhaseGVN::_types) maps type information to each
// node ID. The mapping is updated to use the new node IDs as well. Updated type
// information is returned in PhaseGVN::_types.
//
// The PhaseRenumberLive phase does not preserve the order of elements in the worklist.
//
// Other data structures used by the compiler are not updated. The hash table for value
// numbering (the field PhaseGVN::_table) is not updated because computing the hash
// values is not based on node IDs. The field PhaseGVN::_nodes is not updated either
// because it is empty wherever PhaseRenumberLive is used.
PhaseRenumberLive::PhaseRenumberLive(PhaseGVN* gvn,
Unique_Node_List* worklist, Unique_Node_List* new_worklist,
PhaseNumber phase_num) :
PhaseRemoveUseless(gvn, worklist, Remove_Useless_And_Renumber_Live),
_new_type_array(C->comp_arena()),
_old2new_map(C->unique(), C->unique(), -1),
_is_pass_finished(false),
_live_node_count(C->live_nodes())
{
assert(RenumberLiveNodes, "RenumberLiveNodes must be set to true for node renumbering to take place");
assert(C->live_nodes() == _useful.size(), "the number of live nodes must match the number of useful nodes");
assert(gvn->nodes_size() == 0, "GVN must not contain any nodes at this point");
assert(_delayed.size() == 0, "should be empty");
uint worklist_size = worklist->size();
// Iterate over the set of live nodes.
for (uint current_idx = 0; current_idx < _useful.size(); current_idx++) {
Node* n = _useful.at(current_idx);
bool in_worklist = false;
if (worklist->member(n)) {
in_worklist = true;
}
const Type* type = gvn->type_or_null(n);
_new_type_array.map(current_idx, type);
assert(_old2new_map.at(n->_idx) == -1, "already seen");
_old2new_map.at_put(n->_idx, current_idx);
n->set_idx(current_idx); // Update node ID.
if (in_worklist) {
new_worklist->push(n);
}
if (update_embedded_ids(n) < 0) {
_delayed.push(n); // has embedded IDs; handle later
}
}
assert(worklist_size == new_worklist->size(), "the new worklist must have the same size as the original worklist");
assert(_live_node_count == _useful.size(), "all live nodes must be processed");
_is_pass_finished = true; // pass finished; safe to process delayed updates
while (_delayed.size() > 0) {
Node* n = _delayed.pop();
int no_of_updates = update_embedded_ids(n);
assert(no_of_updates > 0, "should be updated");
}
// Replace the compiler's type information with the updated type information.
gvn->replace_types(_new_type_array);
// Update the unique node count of the compilation to the number of currently live nodes.
C->set_unique(_live_node_count);
// Set the dead node count to 0 and reset dead node list.
C->reset_dead_node_list();
// Clear the original worklist
worklist->clear();
}
int PhaseRenumberLive::new_index(int old_idx) {
assert(_is_pass_finished, "not finished");
if (_old2new_map.at(old_idx) == -1) { // absent
// Allocate a placeholder to preserve uniqueness
_old2new_map.at_put(old_idx, _live_node_count);
_live_node_count++;
}
return _old2new_map.at(old_idx);
}
int PhaseRenumberLive::update_embedded_ids(Node* n) {
int no_of_updates = 0;
if (n->is_Phi()) {
PhiNode* phi = n->as_Phi();
if (phi->_inst_id != -1) {
if (!_is_pass_finished) {
return -1; // delay
}
int new_idx = new_index(phi->_inst_id);
assert(new_idx != -1, "");
phi->_inst_id = new_idx;
no_of_updates++;
}
if (phi->_inst_mem_id != -1) {
if (!_is_pass_finished) {
return -1; // delay
}
int new_idx = new_index(phi->_inst_mem_id);
assert(new_idx != -1, "");
phi->_inst_mem_id = new_idx;
no_of_updates++;
}
}
const Type* type = _new_type_array.fast_lookup(n->_idx);
if (type != NULL && type->isa_oopptr() && type->is_oopptr()->is_known_instance()) {
if (!_is_pass_finished) {
return -1; // delay
}
int old_idx = type->is_oopptr()->instance_id();
int new_idx = new_index(old_idx);
const Type* new_type = type->is_oopptr()->with_instance_id(new_idx);
_new_type_array.map(n->_idx, new_type);
no_of_updates++;
}
return no_of_updates;
}
//=============================================================================
//------------------------------PhaseTransform---------------------------------
PhaseTransform::PhaseTransform( PhaseNumber pnum ) : Phase(pnum),
_arena(Thread::current()->resource_area()),
_nodes(_arena),
_types(_arena)
{
init_con_caches();
#ifndef PRODUCT
clear_progress();
clear_transforms();
set_allow_progress(true);
#endif
// Force allocation for currently existing nodes
_types.map(C->unique(), NULL);
}
//------------------------------PhaseTransform---------------------------------
PhaseTransform::PhaseTransform( Arena *arena, PhaseNumber pnum ) : Phase(pnum),
_arena(arena),
_nodes(arena),
_types(arena)
{
init_con_caches();
#ifndef PRODUCT
clear_progress();
clear_transforms();
set_allow_progress(true);
#endif
// Force allocation for currently existing nodes
_types.map(C->unique(), NULL);
}
//------------------------------PhaseTransform---------------------------------
// Initialize with previously generated type information
PhaseTransform::PhaseTransform( PhaseTransform *pt, PhaseNumber pnum ) : Phase(pnum),
_arena(pt->_arena),
_nodes(pt->_nodes),
_types(pt->_types)
{
init_con_caches();
#ifndef PRODUCT
clear_progress();
clear_transforms();
set_allow_progress(true);
#endif
}
void PhaseTransform::init_con_caches() {
memset(_icons,0,sizeof(_icons));
memset(_lcons,0,sizeof(_lcons));
memset(_zcons,0,sizeof(_zcons));
}
//--------------------------------find_int_type--------------------------------
const TypeInt* PhaseTransform::find_int_type(Node* n) {
if (n == NULL) return NULL;
// Call type_or_null(n) to determine node's type since we might be in
// parse phase and call n->Value() may return wrong type.
// (For example, a phi node at the beginning of loop parsing is not ready.)
const Type* t = type_or_null(n);
if (t == NULL) return NULL;
return t->isa_int();
}
//-------------------------------find_long_type--------------------------------
const TypeLong* PhaseTransform::find_long_type(Node* n) {
if (n == NULL) return NULL;
// (See comment above on type_or_null.)
const Type* t = type_or_null(n);
if (t == NULL) return NULL;
return t->isa_long();
}
#ifndef PRODUCT
void PhaseTransform::dump_old2new_map() const {
_nodes.dump();
}
void PhaseTransform::dump_new( uint nidx ) const {
for( uint i=0; i<_nodes.Size(); i++ )
if( _nodes[i] && _nodes[i]->_idx == nidx ) {
_nodes[i]->dump();
tty->cr();
tty->print_cr("Old index= %d",i);
return;
}
tty->print_cr("Node %d not found in the new indices", nidx);
}
//------------------------------dump_types-------------------------------------
void PhaseTransform::dump_types( ) const {
_types.dump();
}
//------------------------------dump_nodes_and_types---------------------------
void PhaseTransform::dump_nodes_and_types(const Node* root, uint depth, bool only_ctrl) {
VectorSet visited;
dump_nodes_and_types_recur(root, depth, only_ctrl, visited);
}
//------------------------------dump_nodes_and_types_recur---------------------
void PhaseTransform::dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited) {
if( !n ) return;
if( depth == 0 ) return;
if( visited.test_set(n->_idx) ) return;
for( uint i=0; i<n->len(); i++ ) {
if( only_ctrl && !(n->is_Region()) && i != TypeFunc::Control ) continue;
dump_nodes_and_types_recur( n->in(i), depth-1, only_ctrl, visited );
}
n->dump();
if (type_or_null(n) != NULL) {
tty->print(" "); type(n)->dump(); tty->cr();
}
}
#endif
//=============================================================================
//------------------------------PhaseValues------------------------------------
// Set minimum table size to "255"
PhaseValues::PhaseValues( Arena *arena, uint est_max_size )
: PhaseTransform(arena, GVN), _table(arena, est_max_size), _iterGVN(false) {
NOT_PRODUCT( clear_new_values(); )
}
//------------------------------PhaseValues------------------------------------
// Set minimum table size to "255"
PhaseValues::PhaseValues(PhaseValues* ptv)
: PhaseTransform(ptv, GVN), _table(&ptv->_table), _iterGVN(false) {
NOT_PRODUCT( clear_new_values(); )
}
//------------------------------~PhaseValues-----------------------------------
#ifndef PRODUCT
PhaseValues::~PhaseValues() {
_table.dump();
// Statistics for value progress and efficiency
if( PrintCompilation && Verbose && WizardMode ) {
tty->print("\n%sValues: %d nodes ---> %d/%d (%d)",
is_IterGVN() ? "Iter" : " ", C->unique(), made_progress(), made_transforms(), made_new_values());
if( made_transforms() != 0 ) {
tty->print_cr(" ratio %f", made_progress()/(float)made_transforms() );
} else {
tty->cr();
}
}
}
#endif
//------------------------------makecon----------------------------------------
ConNode* PhaseTransform::makecon(const Type *t) {
assert(t->singleton(), "must be a constant");
assert(!t->empty() || t == Type::TOP, "must not be vacuous range");
switch (t->base()) { // fast paths
case Type::Half:
case Type::Top: return (ConNode*) C->top();
case Type::Int: return intcon( t->is_int()->get_con() );
case Type::Long: return longcon( t->is_long()->get_con() );
default: break;
}
if (t->is_zero_type())
return zerocon(t->basic_type());
return uncached_makecon(t);
}
//--------------------------uncached_makecon-----------------------------------
// Make an idealized constant - one of ConINode, ConPNode, etc.
ConNode* PhaseValues::uncached_makecon(const Type *t) {
assert(t->singleton(), "must be a constant");
ConNode* x = ConNode::make(t);
ConNode* k = (ConNode*)hash_find_insert(x); // Value numbering
if (k == NULL) {
set_type(x, t); // Missed, provide type mapping
GrowableArray<Node_Notes*>* nna = C->node_note_array();
if (nna != NULL) {
Node_Notes* loc = C->locate_node_notes(nna, x->_idx, true);
loc->clear(); // do not put debug info on constants
}
} else {
x->destruct(this); // Hit, destroy duplicate constant
x = k; // use existing constant
}
return x;
}
//------------------------------intcon-----------------------------------------
// Fast integer constant. Same as "transform(new ConINode(TypeInt::make(i)))"
ConINode* PhaseTransform::intcon(jint i) {
// Small integer? Check cache! Check that cached node is not dead
if (i >= _icon_min && i <= _icon_max) {
ConINode* icon = _icons[i-_icon_min];
if (icon != NULL && icon->in(TypeFunc::Control) != NULL)
return icon;
}
ConINode* icon = (ConINode*) uncached_makecon(TypeInt::make(i));
assert(icon->is_Con(), "");
if (i >= _icon_min && i <= _icon_max)
_icons[i-_icon_min] = icon; // Cache small integers
return icon;
}
//------------------------------longcon----------------------------------------
// Fast long constant.
ConLNode* PhaseTransform::longcon(jlong l) {
// Small integer? Check cache! Check that cached node is not dead
if (l >= _lcon_min && l <= _lcon_max) {
ConLNode* lcon = _lcons[l-_lcon_min];
if (lcon != NULL && lcon->in(TypeFunc::Control) != NULL)
return lcon;
}
ConLNode* lcon = (ConLNode*) uncached_makecon(TypeLong::make(l));
assert(lcon->is_Con(), "");
if (l >= _lcon_min && l <= _lcon_max)
_lcons[l-_lcon_min] = lcon; // Cache small integers
return lcon;
}
ConNode* PhaseTransform::integercon(jlong l, BasicType bt) {
if (bt == T_INT) {
jint int_con = (jint)l;
assert(((long)int_con) == l, "not an int");
return intcon(int_con);
}
assert(bt == T_LONG, "not an integer");
return longcon(l);
}
//------------------------------zerocon-----------------------------------------
// Fast zero or null constant. Same as "transform(ConNode::make(Type::get_zero_type(bt)))"
ConNode* PhaseTransform::zerocon(BasicType bt) {
assert((uint)bt <= _zcon_max, "domain check");
ConNode* zcon = _zcons[bt];
if (zcon != NULL && zcon->in(TypeFunc::Control) != NULL)
return zcon;
zcon = (ConNode*) uncached_makecon(Type::get_zero_type(bt));
_zcons[bt] = zcon;
return zcon;
}
//=============================================================================
Node* PhaseGVN::apply_ideal(Node* k, bool can_reshape) {
Node* i = BarrierSet::barrier_set()->barrier_set_c2()->ideal_node(this, k, can_reshape);
if (i == NULL) {
i = k->Ideal(this, can_reshape);
}
return i;
}
//------------------------------transform--------------------------------------
// Return a node which computes the same function as this node, but in a
// faster or cheaper fashion.
Node *PhaseGVN::transform( Node *n ) {
return transform_no_reclaim(n);
}
//------------------------------transform--------------------------------------
// Return a node which computes the same function as this node, but
// in a faster or cheaper fashion.
Node *PhaseGVN::transform_no_reclaim(Node *n) {
NOT_PRODUCT( set_transforms(); )
// Apply the Ideal call in a loop until it no longer applies
Node* k = n;
Node* i = apply_ideal(k, /*can_reshape=*/false);
NOT_PRODUCT(uint loop_count = 1;)
while (i != NULL) {
assert(i->_idx >= k->_idx, "Idealize should return new nodes, use Identity to return old nodes" );
k = i;
#ifdef ASSERT
if (loop_count >= K + C->live_nodes()) {
dump_infinite_loop_info(i, "PhaseGVN::transform_no_reclaim");
}
#endif
i = apply_ideal(k, /*can_reshape=*/false);
NOT_PRODUCT(loop_count++;)
}
NOT_PRODUCT(if (loop_count != 0) { set_progress(); })
// If brand new node, make space in type array.
ensure_type_or_null(k);
// Since I just called 'Value' to compute the set of run-time values
// for this Node, and 'Value' is non-local (and therefore expensive) I'll
// cache Value. Later requests for the local phase->type of this Node can
// use the cached Value instead of suffering with 'bottom_type'.
const Type* t = k->Value(this); // Get runtime Value set
assert(t != NULL, "value sanity");
if (type_or_null(k) != t) {
#ifndef PRODUCT
// Do not count initial visit to node as a transformation
if (type_or_null(k) == NULL) {
inc_new_values();
set_progress();
}
#endif
set_type(k, t);
// If k is a TypeNode, capture any more-precise type permanently into Node
k->raise_bottom_type(t);
}
if (t->singleton() && !k->is_Con()) {
NOT_PRODUCT(set_progress();)
return makecon(t); // Turn into a constant
}
// Now check for Identities
i = k->Identity(this); // Look for a nearby replacement
if (i != k) { // Found? Return replacement!
NOT_PRODUCT(set_progress();)
return i;
}
// Global Value Numbering
i = hash_find_insert(k); // Insert if new
if (i && (i != k)) {
// Return the pre-existing node
NOT_PRODUCT(set_progress();)
return i;
}
// Return Idealized original
return k;
}
bool PhaseGVN::is_dominator_helper(Node *d, Node *n, bool linear_only) {
if (d->is_top() || (d->is_Proj() && d->in(0)->is_top())) {
return false;
}
if (n->is_top() || (n->is_Proj() && n->in(0)->is_top())) {
return false;
}
assert(d->is_CFG() && n->is_CFG(), "must have CFG nodes");
int i = 0;
while (d != n) {
n = IfNode::up_one_dom(n, linear_only);
i++;
if (n == NULL || i >= 100) {
return false;
}
}
return true;
}
#ifdef ASSERT
//------------------------------dead_loop_check--------------------------------
// Check for a simple dead loop when a data node references itself directly
// or through an other data node excluding cons and phis.
void PhaseGVN::dead_loop_check( Node *n ) {
// Phi may reference itself in a loop
if (n != NULL && !n->is_dead_loop_safe() && !n->is_CFG()) {
// Do 2 levels check and only data inputs.
bool no_dead_loop = true;
uint cnt = n->req();
for (uint i = 1; i < cnt && no_dead_loop; i++) {
Node *in = n->in(i);
if (in == n) {
no_dead_loop = false;
} else if (in != NULL && !in->is_dead_loop_safe()) {
uint icnt = in->req();
for (uint j = 1; j < icnt && no_dead_loop; j++) {
if (in->in(j) == n || in->in(j) == in)
no_dead_loop = false;
}
}
}
if (!no_dead_loop) n->dump(3);
assert(no_dead_loop, "dead loop detected");
}
}
/**
* Dumps information that can help to debug the problem. A debug
* build fails with an assert.
*/
void PhaseGVN::dump_infinite_loop_info(Node* n, const char* where) {
n->dump(4);
assert(false, "infinite loop in %s", where);
}
#endif
//=============================================================================
//------------------------------PhaseIterGVN-----------------------------------
// Initialize with previous PhaseIterGVN info; used by PhaseCCP
PhaseIterGVN::PhaseIterGVN(PhaseIterGVN* igvn) : PhaseGVN(igvn),
_delay_transform(igvn->_delay_transform),
_stack(igvn->_stack ),
_worklist(igvn->_worklist)
{
_iterGVN = true;
}
//------------------------------PhaseIterGVN-----------------------------------
// Initialize with previous PhaseGVN info from Parser
PhaseIterGVN::PhaseIterGVN(PhaseGVN* gvn) : PhaseGVN(gvn),
_delay_transform(false),
// TODO: Before incremental inlining it was allocated only once and it was fine. Now that
// the constructor is used in incremental inlining, this consumes too much memory:
// _stack(C->live_nodes() >> 1),
// So, as a band-aid, we replace this by:
_stack(C->comp_arena(), 32),
_worklist(*C->for_igvn())
{
_iterGVN = true;
uint max;
// Dead nodes in the hash table inherited from GVN were not treated as
// roots during def-use info creation; hence they represent an invisible
// use. Clear them out.
max = _table.size();
for( uint i = 0; i < max; ++i ) {
Node *n = _table.at(i);
if(n != NULL && n != _table.sentinel() && n->outcnt() == 0) {
if( n->is_top() ) continue;
// If remove_useless_nodes() has run, we expect no such nodes left.
assert(false, "remove_useless_nodes missed this node");
hash_delete(n);
}
}
// Any Phis or Regions on the worklist probably had uses that could not
// make more progress because the uses were made while the Phis and Regions