forked from openjdk/valhalla
/
escape.cpp
3647 lines (3533 loc) · 139 KB
/
escape.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) 2005, 2019, 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 "ci/bcEscapeAnalyzer.hpp"
#include "compiler/compileLog.hpp"
#include "gc/shared/barrierSet.hpp"
#include "gc/shared/c2/barrierSetC2.hpp"
#include "libadt/vectset.hpp"
#include "memory/allocation.hpp"
#include "memory/resourceArea.hpp"
#include "opto/c2compiler.hpp"
#include "opto/arraycopynode.hpp"
#include "opto/callnode.hpp"
#include "opto/cfgnode.hpp"
#include "opto/compile.hpp"
#include "opto/escape.hpp"
#include "opto/phaseX.hpp"
#include "opto/movenode.hpp"
#include "opto/rootnode.hpp"
#include "utilities/macros.hpp"
ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
_nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
_in_worklist(C->comp_arena()),
_next_pidx(0),
_collecting(true),
_verify(false),
_compile(C),
_igvn(igvn),
_node_map(C->comp_arena()) {
// Add unknown java object.
add_java_object(C->top(), PointsToNode::GlobalEscape);
phantom_obj = ptnode_adr(C->top()->_idx)->as_JavaObject();
// Add ConP(#NULL) and ConN(#NULL) nodes.
Node* oop_null = igvn->zerocon(T_OBJECT);
assert(oop_null->_idx < nodes_size(), "should be created already");
add_java_object(oop_null, PointsToNode::NoEscape);
null_obj = ptnode_adr(oop_null->_idx)->as_JavaObject();
if (UseCompressedOops) {
Node* noop_null = igvn->zerocon(T_NARROWOOP);
assert(noop_null->_idx < nodes_size(), "should be created already");
map_ideal_node(noop_null, null_obj);
}
_pcmp_neq = NULL; // Should be initialized
_pcmp_eq = NULL;
}
bool ConnectionGraph::has_candidates(Compile *C) {
// EA brings benefits only when the code has allocations and/or locks which
// are represented by ideal Macro nodes.
int cnt = C->macro_count();
for (int i = 0; i < cnt; i++) {
Node *n = C->macro_node(i);
if (n->is_Allocate())
return true;
if (n->is_Lock()) {
Node* obj = n->as_Lock()->obj_node()->uncast();
if (!(obj->is_Parm() || obj->is_Con()))
return true;
}
if (n->is_CallStaticJava() &&
n->as_CallStaticJava()->is_boxing_method()) {
return true;
}
}
return false;
}
void ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) {
Compile::TracePhase tp("escapeAnalysis", &Phase::timers[Phase::_t_escapeAnalysis]);
ResourceMark rm;
// Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction
// to create space for them in ConnectionGraph::_nodes[].
Node* oop_null = igvn->zerocon(T_OBJECT);
Node* noop_null = igvn->zerocon(T_NARROWOOP);
ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn);
// Perform escape analysis
if (congraph->compute_escape()) {
// There are non escaping objects.
C->set_congraph(congraph);
}
// Cleanup.
if (oop_null->outcnt() == 0)
igvn->hash_delete(oop_null);
if (noop_null->outcnt() == 0)
igvn->hash_delete(noop_null);
}
bool ConnectionGraph::compute_escape() {
Compile* C = _compile;
PhaseGVN* igvn = _igvn;
// Worklists used by EA.
Unique_Node_List delayed_worklist;
GrowableArray<Node*> alloc_worklist;
GrowableArray<Node*> ptr_cmp_worklist;
GrowableArray<Node*> storestore_worklist;
GrowableArray<ArrayCopyNode*> arraycopy_worklist;
GrowableArray<PointsToNode*> ptnodes_worklist;
GrowableArray<JavaObjectNode*> java_objects_worklist;
GrowableArray<JavaObjectNode*> non_escaped_worklist;
GrowableArray<FieldNode*> oop_fields_worklist;
DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
{ Compile::TracePhase tp("connectionGraph", &Phase::timers[Phase::_t_connectionGraph]);
// 1. Populate Connection Graph (CG) with PointsTo nodes.
ideal_nodes.map(C->live_nodes(), NULL); // preallocate space
// Initialize worklist
if (C->root() != NULL) {
ideal_nodes.push(C->root());
}
// Processed ideal nodes are unique on ideal_nodes list
// but several ideal nodes are mapped to the phantom_obj.
// To avoid duplicated entries on the following worklists
// add the phantom_obj only once to them.
ptnodes_worklist.append(phantom_obj);
java_objects_worklist.append(phantom_obj);
for( uint next = 0; next < ideal_nodes.size(); ++next ) {
Node* n = ideal_nodes.at(next);
if ((n->Opcode() == Op_LoadX || n->Opcode() == Op_StoreX) &&
!n->in(MemNode::Address)->is_AddP() &&
_igvn->type(n->in(MemNode::Address))->isa_oopptr()) {
// Load/Store at mark work address is at offset 0 so has no AddP which confuses EA
Node* addp = new AddPNode(n->in(MemNode::Address), n->in(MemNode::Address), _igvn->MakeConX(0));
_igvn->register_new_node_with_optimizer(addp);
_igvn->replace_input_of(n, MemNode::Address, addp);
ideal_nodes.push(addp);
_nodes.at_put_grow(addp->_idx, NULL, NULL);
}
// Create PointsTo nodes and add them to Connection Graph. Called
// only once per ideal node since ideal_nodes is Unique_Node list.
add_node_to_connection_graph(n, &delayed_worklist);
PointsToNode* ptn = ptnode_adr(n->_idx);
if (ptn != NULL && ptn != phantom_obj) {
ptnodes_worklist.append(ptn);
if (ptn->is_JavaObject()) {
java_objects_worklist.append(ptn->as_JavaObject());
if ((n->is_Allocate() || n->is_CallStaticJava()) &&
(ptn->escape_state() < PointsToNode::GlobalEscape)) {
// Only allocations and java static calls results are interesting.
non_escaped_worklist.append(ptn->as_JavaObject());
}
} else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
oop_fields_worklist.append(ptn->as_Field());
}
}
if (n->is_MergeMem()) {
// Collect all MergeMem nodes to add memory slices for
// scalar replaceable objects in split_unique_types().
_mergemem_worklist.append(n->as_MergeMem());
} else if (OptimizePtrCompare && n->is_Cmp() &&
(n->Opcode() == Op_CmpP || n->Opcode() == Op_CmpN)) {
// Collect compare pointers nodes.
ptr_cmp_worklist.append(n);
} else if (n->is_MemBarStoreStore()) {
// Collect all MemBarStoreStore nodes so that depending on the
// escape status of the associated Allocate node some of them
// may be eliminated.
storestore_worklist.append(n);
} else if (n->is_MemBar() && (n->Opcode() == Op_MemBarRelease) &&
(n->req() > MemBarNode::Precedent)) {
record_for_optimizer(n);
#ifdef ASSERT
} else if (n->is_AddP()) {
// Collect address nodes for graph verification.
addp_worklist.append(n);
#endif
} else if (n->is_ArrayCopy()) {
// Keep a list of ArrayCopy nodes so if one of its input is non
// escaping, we can record a unique type
arraycopy_worklist.append(n->as_ArrayCopy());
}
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
Node* m = n->fast_out(i); // Get user
ideal_nodes.push(m);
}
}
if (non_escaped_worklist.length() == 0) {
_collecting = false;
return false; // Nothing to do.
}
// Add final simple edges to graph.
while(delayed_worklist.size() > 0) {
Node* n = delayed_worklist.pop();
add_final_edges(n);
}
int ptnodes_length = ptnodes_worklist.length();
#ifdef ASSERT
if (VerifyConnectionGraph) {
// Verify that no new simple edges could be created and all
// local vars has edges.
_verify = true;
for (int next = 0; next < ptnodes_length; ++next) {
PointsToNode* ptn = ptnodes_worklist.at(next);
add_final_edges(ptn->ideal_node());
if (ptn->is_LocalVar() && ptn->edge_count() == 0) {
ptn->dump();
assert(ptn->as_LocalVar()->edge_count() > 0, "sanity");
}
}
_verify = false;
}
#endif
// Bytecode analyzer BCEscapeAnalyzer, used for Call nodes
// processing, calls to CI to resolve symbols (types, fields, methods)
// referenced in bytecode. During symbol resolution VM may throw
// an exception which CI cleans and converts to compilation failure.
if (C->failing()) return false;
// 2. Finish Graph construction by propagating references to all
// java objects through graph.
if (!complete_connection_graph(ptnodes_worklist, non_escaped_worklist,
java_objects_worklist, oop_fields_worklist)) {
// All objects escaped or hit time or iterations limits.
_collecting = false;
return false;
}
// 3. Adjust scalar_replaceable state of nonescaping objects and push
// scalar replaceable allocations on alloc_worklist for processing
// in split_unique_types().
int non_escaped_length = non_escaped_worklist.length();
for (int next = 0; next < non_escaped_length; next++) {
JavaObjectNode* ptn = non_escaped_worklist.at(next);
bool noescape = (ptn->escape_state() == PointsToNode::NoEscape);
Node* n = ptn->ideal_node();
if (n->is_Allocate()) {
n->as_Allocate()->_is_non_escaping = noescape;
}
if (n->is_CallStaticJava()) {
n->as_CallStaticJava()->_is_non_escaping = noescape;
}
if (noescape && ptn->scalar_replaceable()) {
adjust_scalar_replaceable_state(ptn);
if (ptn->scalar_replaceable()) {
alloc_worklist.append(ptn->ideal_node());
}
}
}
#ifdef ASSERT
if (VerifyConnectionGraph) {
// Verify that graph is complete - no new edges could be added or needed.
verify_connection_graph(ptnodes_worklist, non_escaped_worklist,
java_objects_worklist, addp_worklist);
}
assert(C->unique() == nodes_size(), "no new ideal nodes should be added during ConnectionGraph build");
assert(null_obj->escape_state() == PointsToNode::NoEscape &&
null_obj->edge_count() == 0 &&
!null_obj->arraycopy_src() &&
!null_obj->arraycopy_dst(), "sanity");
#endif
_collecting = false;
} // TracePhase t3("connectionGraph")
// 4. Optimize ideal graph based on EA information.
bool has_non_escaping_obj = (non_escaped_worklist.length() > 0);
if (has_non_escaping_obj) {
optimize_ideal_graph(ptr_cmp_worklist, storestore_worklist);
}
#ifndef PRODUCT
if (PrintEscapeAnalysis) {
dump(ptnodes_worklist); // Dump ConnectionGraph
}
#endif
bool has_scalar_replaceable_candidates = (alloc_worklist.length() > 0);
#ifdef ASSERT
if (VerifyConnectionGraph) {
int alloc_length = alloc_worklist.length();
for (int next = 0; next < alloc_length; ++next) {
Node* n = alloc_worklist.at(next);
PointsToNode* ptn = ptnode_adr(n->_idx);
assert(ptn->escape_state() == PointsToNode::NoEscape && ptn->scalar_replaceable(), "sanity");
}
}
#endif
// 5. Separate memory graph for scalar replaceable allcations.
if (has_scalar_replaceable_candidates &&
C->AliasLevel() >= 3 && EliminateAllocations) {
// Now use the escape information to create unique types for
// scalar replaceable objects.
split_unique_types(alloc_worklist, arraycopy_worklist);
if (C->failing()) return false;
C->print_method(PHASE_AFTER_EA, 2);
#ifdef ASSERT
} else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
tty->print("=== No allocations eliminated for ");
C->method()->print_short_name();
if(!EliminateAllocations) {
tty->print(" since EliminateAllocations is off ===");
} else if(!has_scalar_replaceable_candidates) {
tty->print(" since there are no scalar replaceable candidates ===");
} else if(C->AliasLevel() < 3) {
tty->print(" since AliasLevel < 3 ===");
}
tty->cr();
#endif
}
return has_non_escaping_obj;
}
// Utility function for nodes that load an object
void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
// ThreadLocal has RawPtr type.
const Type* t = _igvn->type(n);
if (t->make_ptr() != NULL) {
Node* adr = n->in(MemNode::Address);
#ifdef ASSERT
if (!adr->is_AddP()) {
assert(_igvn->type(adr)->isa_rawptr(), "sanity");
} else {
assert((ptnode_adr(adr->_idx) == NULL ||
ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
}
#endif
add_local_var_and_edge(n, PointsToNode::NoEscape,
adr, delayed_worklist);
}
}
// Populate Connection Graph with PointsTo nodes and create simple
// connection graph edges.
void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
assert(!_verify, "this method should not be called for verification");
PhaseGVN* igvn = _igvn;
uint n_idx = n->_idx;
PointsToNode* n_ptn = ptnode_adr(n_idx);
if (n_ptn != NULL)
return; // No need to redefine PointsTo node during first iteration.
int opcode = n->Opcode();
bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_to_con_graph(this, igvn, delayed_worklist, n, opcode);
if (gc_handled) {
return; // Ignore node if already handled by GC.
}
if (n->is_Call()) {
// Arguments to allocation and locking don't escape.
if (n->is_AbstractLock()) {
// Put Lock and Unlock nodes on IGVN worklist to process them during
// first IGVN optimization when escape information is still available.
record_for_optimizer(n);
} else if (n->is_Allocate()) {
add_call_node(n->as_Call());
record_for_optimizer(n);
} else {
if (n->is_CallStaticJava()) {
const char* name = n->as_CallStaticJava()->_name;
if (name != NULL && strcmp(name, "uncommon_trap") == 0)
return; // Skip uncommon traps
}
// Don't mark as processed since call's arguments have to be processed.
delayed_worklist->push(n);
// Check if a call returns an object.
if ((n->as_Call()->returns_pointer() &&
n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
(n->is_CallStaticJava() &&
n->as_CallStaticJava()->is_boxing_method())) {
add_call_node(n->as_Call());
} else if (n->as_Call()->tf()->returns_value_type_as_fields()) {
bool returns_oop = false;
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax && !returns_oop; i++) {
ProjNode* pn = n->fast_out(i)->as_Proj();
if (pn->_con >= TypeFunc::Parms && pn->bottom_type()->isa_ptr()) {
returns_oop = true;
}
}
if (returns_oop) {
add_call_node(n->as_Call());
}
}
}
return;
}
// Put this check here to process call arguments since some call nodes
// point to phantom_obj.
if (n_ptn == phantom_obj || n_ptn == null_obj)
return; // Skip predefined nodes.
switch (opcode) {
case Op_AddP: {
Node* base = get_addp_base(n);
PointsToNode* ptn_base = ptnode_adr(base->_idx);
// Field nodes are created for all field types. They are used in
// adjust_scalar_replaceable_state() and split_unique_types().
// Note, non-oop fields will have only base edges in Connection
// Graph because such fields are not used for oop loads and stores.
int offset = address_offset(n, igvn);
add_field(n, PointsToNode::NoEscape, offset);
if (ptn_base == NULL) {
delayed_worklist->push(n); // Process it later.
} else {
n_ptn = ptnode_adr(n_idx);
add_base(n_ptn->as_Field(), ptn_base);
}
break;
}
case Op_CastX2P: {
map_ideal_node(n, phantom_obj);
break;
}
case Op_ValueTypePtr:
case Op_CastPP:
case Op_CheckCastPP:
case Op_EncodeP:
case Op_DecodeN:
case Op_EncodePKlass:
case Op_DecodeNKlass: {
add_local_var_and_edge(n, PointsToNode::NoEscape,
n->in(1), delayed_worklist);
break;
}
case Op_CMoveP: {
add_local_var(n, PointsToNode::NoEscape);
// Do not add edges during first iteration because some could be
// not defined yet.
delayed_worklist->push(n);
break;
}
case Op_ConP:
case Op_ConN:
case Op_ConNKlass: {
// assume all oop constants globally escape except for null
PointsToNode::EscapeState es;
const Type* t = igvn->type(n);
if (t == TypePtr::NULL_PTR || t == TypeNarrowOop::NULL_PTR) {
es = PointsToNode::NoEscape;
} else {
es = PointsToNode::GlobalEscape;
}
add_java_object(n, es);
break;
}
case Op_CreateEx: {
// assume that all exception objects globally escape
map_ideal_node(n, phantom_obj);
break;
}
case Op_LoadKlass:
case Op_LoadNKlass: {
// Unknown class is loaded
map_ideal_node(n, phantom_obj);
break;
}
case Op_LoadP:
case Op_LoadN:
case Op_LoadPLocked: {
add_objload_to_connection_graph(n, delayed_worklist);
break;
}
case Op_Parm: {
map_ideal_node(n, phantom_obj);
break;
}
case Op_PartialSubtypeCheck: {
// Produces Null or notNull and is used in only in CmpP so
// phantom_obj could be used.
map_ideal_node(n, phantom_obj); // Result is unknown
break;
}
case Op_Phi: {
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
// ThreadLocal has RawPtr type.
const Type* t = n->as_Phi()->type();
if (t->make_ptr() != NULL) {
add_local_var(n, PointsToNode::NoEscape);
// Do not add edges during first iteration because some could be
// not defined yet.
delayed_worklist->push(n);
}
break;
}
case Op_Proj: {
// we are only interested in the oop result projection from a call
if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
(n->in(0)->as_Call()->returns_pointer() || n->bottom_type()->isa_ptr())) {
assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
n->in(0)->as_Call()->tf()->returns_value_type_as_fields(), "what kind of oop return is it?");
add_local_var_and_edge(n, PointsToNode::NoEscape,
n->in(0), delayed_worklist);
}
break;
}
case Op_Rethrow: // Exception object escapes
case Op_Return: {
if (n->req() > TypeFunc::Parms &&
igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
// Treat Return value as LocalVar with GlobalEscape escape state.
add_local_var_and_edge(n, PointsToNode::GlobalEscape,
n->in(TypeFunc::Parms), delayed_worklist);
}
break;
}
case Op_CompareAndExchangeP:
case Op_CompareAndExchangeN:
case Op_GetAndSetP:
case Op_GetAndSetN: {
add_objload_to_connection_graph(n, delayed_worklist);
// fallthrough
}
case Op_StoreP:
case Op_StoreN:
case Op_StoreNKlass:
case Op_StorePConditional:
case Op_WeakCompareAndSwapP:
case Op_WeakCompareAndSwapN:
case Op_CompareAndSwapP:
case Op_CompareAndSwapN: {
add_to_congraph_unsafe_access(n, opcode, delayed_worklist);
break;
}
case Op_AryEq:
case Op_HasNegatives:
case Op_StrComp:
case Op_StrEquals:
case Op_StrIndexOf:
case Op_StrIndexOfChar:
case Op_StrInflatedCopy:
case Op_StrCompressedCopy:
case Op_EncodeISOArray: {
add_local_var(n, PointsToNode::ArgEscape);
delayed_worklist->push(n); // Process it later.
break;
}
case Op_ThreadLocal: {
add_java_object(n, PointsToNode::ArgEscape);
break;
}
default:
; // Do nothing for nodes not related to EA.
}
return;
}
#ifdef ASSERT
#define ELSE_FAIL(name) \
/* Should not be called for not pointer type. */ \
n->dump(1); \
assert(false, name); \
break;
#else
#define ELSE_FAIL(name) \
break;
#endif
// Add final simple edges to graph.
void ConnectionGraph::add_final_edges(Node *n) {
PointsToNode* n_ptn = ptnode_adr(n->_idx);
#ifdef ASSERT
if (_verify && n_ptn->is_JavaObject())
return; // This method does not change graph for JavaObject.
#endif
if (n->is_Call()) {
process_call_arguments(n->as_Call());
return;
}
assert(n->is_Store() || n->is_LoadStore() ||
(n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
"node should be registered already");
int opcode = n->Opcode();
bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
if (gc_handled) {
return; // Ignore node if already handled by GC.
}
switch (opcode) {
case Op_AddP: {
Node* base = get_addp_base(n);
PointsToNode* ptn_base = ptnode_adr(base->_idx);
assert(ptn_base != NULL, "field's base should be registered");
add_base(n_ptn->as_Field(), ptn_base);
break;
}
case Op_ValueTypePtr:
case Op_CastPP:
case Op_CheckCastPP:
case Op_EncodeP:
case Op_DecodeN:
case Op_EncodePKlass:
case Op_DecodeNKlass: {
add_local_var_and_edge(n, PointsToNode::NoEscape,
n->in(1), NULL);
break;
}
case Op_CMoveP: {
for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
Node* in = n->in(i);
if (in == NULL)
continue; // ignore NULL
Node* uncast_in = in->uncast();
if (uncast_in->is_top() || uncast_in == n)
continue; // ignore top or inputs which go back this node
PointsToNode* ptn = ptnode_adr(in->_idx);
assert(ptn != NULL, "node should be registered");
add_edge(n_ptn, ptn);
}
break;
}
case Op_LoadP:
case Op_LoadN:
case Op_LoadPLocked: {
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
// ThreadLocal has RawPtr type.
const Type* t = _igvn->type(n);
if (t->make_ptr() != NULL) {
Node* adr = n->in(MemNode::Address);
add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
break;
}
ELSE_FAIL("Op_LoadP");
}
case Op_Phi: {
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
// ThreadLocal has RawPtr type.
const Type* t = n->as_Phi()->type();
if (t->make_ptr() != NULL) {
for (uint i = 1; i < n->req(); i++) {
Node* in = n->in(i);
if (in == NULL)
continue; // ignore NULL
Node* uncast_in = in->uncast();
if (uncast_in->is_top() || uncast_in == n)
continue; // ignore top or inputs which go back this node
PointsToNode* ptn = ptnode_adr(in->_idx);
assert(ptn != NULL, "node should be registered");
add_edge(n_ptn, ptn);
}
break;
}
ELSE_FAIL("Op_Phi");
}
case Op_Proj: {
// we are only interested in the oop result projection from a call
if (n->as_Proj()->_con >= TypeFunc::Parms && n->in(0)->is_Call() &&
(n->in(0)->as_Call()->returns_pointer()|| n->bottom_type()->isa_ptr())) {
assert((n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->as_Call()->returns_pointer()) ||
n->in(0)->as_Call()->tf()->returns_value_type_as_fields(), "what kind of oop return is it?");
add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
break;
}
ELSE_FAIL("Op_Proj");
}
case Op_Rethrow: // Exception object escapes
case Op_Return: {
if (n->req() > TypeFunc::Parms &&
_igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
// Treat Return value as LocalVar with GlobalEscape escape state.
add_local_var_and_edge(n, PointsToNode::GlobalEscape,
n->in(TypeFunc::Parms), NULL);
break;
}
ELSE_FAIL("Op_Return");
}
case Op_StoreP:
case Op_StoreN:
case Op_StoreNKlass:
case Op_StorePConditional:
case Op_CompareAndExchangeP:
case Op_CompareAndExchangeN:
case Op_CompareAndSwapP:
case Op_CompareAndSwapN:
case Op_WeakCompareAndSwapP:
case Op_WeakCompareAndSwapN:
case Op_GetAndSetP:
case Op_GetAndSetN: {
if (add_final_edges_unsafe_access(n, opcode)) {
break;
}
ELSE_FAIL("Op_StoreP");
}
case Op_AryEq:
case Op_HasNegatives:
case Op_StrComp:
case Op_StrEquals:
case Op_StrIndexOf:
case Op_StrIndexOfChar:
case Op_StrInflatedCopy:
case Op_StrCompressedCopy:
case Op_EncodeISOArray: {
// char[]/byte[] arrays passed to string intrinsic do not escape but
// they are not scalar replaceable. Adjust escape state for them.
// Start from in(2) edge since in(1) is memory edge.
for (uint i = 2; i < n->req(); i++) {
Node* adr = n->in(i);
const Type* at = _igvn->type(adr);
if (!adr->is_top() && at->isa_ptr()) {
assert(at == Type::TOP || at == TypePtr::NULL_PTR ||
at->isa_ptr() != NULL, "expecting a pointer");
if (adr->is_AddP()) {
adr = get_addp_base(adr);
}
PointsToNode* ptn = ptnode_adr(adr->_idx);
assert(ptn != NULL, "node should be registered");
add_edge(n_ptn, ptn);
}
}
break;
}
default: {
// This method should be called only for EA specific nodes which may
// miss some edges when they were created.
#ifdef ASSERT
n->dump(1);
#endif
guarantee(false, "unknown node");
}
}
return;
}
void ConnectionGraph::add_to_congraph_unsafe_access(Node* n, uint opcode, Unique_Node_List* delayed_worklist) {
Node* adr = n->in(MemNode::Address);
const Type* adr_type = _igvn->type(adr);
adr_type = adr_type->make_ptr();
if (adr_type == NULL) {
return; // skip dead nodes
}
if (adr_type->isa_oopptr()
|| ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
&& adr_type == TypeRawPtr::NOTNULL
&& adr->in(AddPNode::Address)->is_Proj()
&& adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
delayed_worklist->push(n); // Process it later.
#ifdef ASSERT
assert (adr->is_AddP(), "expecting an AddP");
if (adr_type == TypeRawPtr::NOTNULL) {
// Verify a raw address for a store captured by Initialize node.
int offs = (int) _igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
assert(offs != Type::OffsetBot, "offset must be a constant");
}
#endif
} else {
// Ignore copy the displaced header to the BoxNode (OSR compilation).
if (adr->is_BoxLock()) {
return;
}
// Stored value escapes in unsafe access.
if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
delayed_worklist->push(n); // Process unsafe access later.
return;
}
#ifdef ASSERT
n->dump(1);
assert(false, "not unsafe");
#endif
}
}
bool ConnectionGraph::add_final_edges_unsafe_access(Node* n, uint opcode) {
Node* adr = n->in(MemNode::Address);
const Type *adr_type = _igvn->type(adr);
adr_type = adr_type->make_ptr();
#ifdef ASSERT
if (adr_type == NULL) {
n->dump(1);
assert(adr_type != NULL, "dead node should not be on list");
return true;
}
#endif
if (opcode == Op_GetAndSetP || opcode == Op_GetAndSetN ||
opcode == Op_CompareAndExchangeN || opcode == Op_CompareAndExchangeP) {
add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
}
if (adr_type->isa_oopptr()
|| ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
&& adr_type == TypeRawPtr::NOTNULL
&& adr->in(AddPNode::Address)->is_Proj()
&& adr->in(AddPNode::Address)->in(0)->is_Allocate())) {
// Point Address to Value
PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
assert(adr_ptn != NULL &&
adr_ptn->as_Field()->is_oop(), "node should be registered");
Node* val = n->in(MemNode::ValueIn);
PointsToNode* ptn = ptnode_adr(val->_idx);
assert(ptn != NULL, "node should be registered");
add_edge(adr_ptn, ptn);
return true;
} else if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
// Stored value escapes in unsafe access.
Node* val = n->in(MemNode::ValueIn);
PointsToNode* ptn = ptnode_adr(val->_idx);
assert(ptn != NULL, "node should be registered");
set_escape_state(ptn, PointsToNode::GlobalEscape);
// Add edge to object for unsafe access with offset.
PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
assert(adr_ptn != NULL, "node should be registered");
if (adr_ptn->is_Field()) {
assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
add_edge(adr_ptn, ptn);
}
return true;
}
return false;
}
void ConnectionGraph::add_call_node(CallNode* call) {
assert(call->returns_pointer() || call->tf()->returns_value_type_as_fields(), "only for call which returns pointer");
uint call_idx = call->_idx;
if (call->is_Allocate()) {
Node* k = call->in(AllocateNode::KlassNode);
const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
assert(kt != NULL, "TypeKlassPtr required.");
ciKlass* cik = kt->klass();
PointsToNode::EscapeState es = PointsToNode::NoEscape;
bool scalar_replaceable = true;
if (call->is_AllocateArray()) {
if (!cik->is_array_klass()) { // StressReflectiveCode
es = PointsToNode::GlobalEscape;
} else {
int length = call->in(AllocateNode::ALength)->find_int_con(-1);
if (length < 0 || length > EliminateAllocationArraySizeLimit) {
// Not scalar replaceable if the length is not constant or too big.
scalar_replaceable = false;
}
}
} else { // Allocate instance
if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||
cik->is_subclass_of(_compile->env()->Reference_klass()) ||
!cik->is_instance_klass() || // StressReflectiveCode
!cik->as_instance_klass()->can_be_instantiated() ||
cik->as_instance_klass()->has_finalizer()) {
es = PointsToNode::GlobalEscape;
}
}
add_java_object(call, es);
PointsToNode* ptn = ptnode_adr(call_idx);
if (!scalar_replaceable && ptn->scalar_replaceable()) {
ptn->set_scalar_replaceable(false);
}
} else if (call->is_CallStaticJava()) {
// Call nodes could be different types:
//
// 1. CallDynamicJavaNode (what happened during call is unknown):
//
// - mapped to GlobalEscape JavaObject node if oop is returned;
//
// - all oop arguments are escaping globally;
//
// 2. CallStaticJavaNode (execute bytecode analysis if possible):
//
// - the same as CallDynamicJavaNode if can't do bytecode analysis;
//
// - mapped to GlobalEscape JavaObject node if unknown oop is returned;
// - mapped to NoEscape JavaObject node if non-escaping object allocated
// during call is returned;
// - mapped to ArgEscape LocalVar node pointed to object arguments
// which are returned and does not escape during call;
//
// - oop arguments escaping status is defined by bytecode analysis;
//
// For a static call, we know exactly what method is being called.
// Use bytecode estimator to record whether the call's return value escapes.
ciMethod* meth = call->as_CallJava()->method();
if (meth == NULL) {
const char* name = call->as_CallStaticJava()->_name;
assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");
// Returns a newly allocated unescaped object.
add_java_object(call, PointsToNode::NoEscape);
ptnode_adr(call_idx)->set_scalar_replaceable(false);
} else if (meth->is_boxing_method()) {
// Returns boxing object
PointsToNode::EscapeState es;
vmIntrinsics::ID intr = meth->intrinsic_id();
if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
// It does not escape if object is always allocated.
es = PointsToNode::NoEscape;
} else {
// It escapes globally if object could be loaded from cache.
es = PointsToNode::GlobalEscape;
}
add_java_object(call, es);
} else {
BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
call_analyzer->copy_dependencies(_compile->dependencies());
if (call_analyzer->is_return_allocated()) {
// Returns a newly allocated unescaped object, simply
// update dependency information.
// Mark it as NoEscape so that objects referenced by
// it's fields will be marked as NoEscape at least.
add_java_object(call, PointsToNode::NoEscape);
ptnode_adr(call_idx)->set_scalar_replaceable(false);
} else {
// Determine whether any arguments are returned.
const TypeTuple* d = call->tf()->domain_cc();
bool ret_arg = false;
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
if (d->field_at(i)->isa_ptr() != NULL &&
call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
ret_arg = true;
break;
}
}
if (ret_arg) {
add_local_var(call, PointsToNode::ArgEscape);
} else {
// Returns unknown object.
map_ideal_node(call, phantom_obj);
}
}
}
} else {
// An other type of call, assume the worst case:
// returned value is unknown and globally escapes.
assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
map_ideal_node(call, phantom_obj);
}
}
void ConnectionGraph::process_call_arguments(CallNode *call) {
bool is_arraycopy = false;
switch (call->Opcode()) {
#ifdef ASSERT
case Op_Allocate:
case Op_AllocateArray:
case Op_Lock:
case Op_Unlock:
assert(false, "should be done already");
break;
#endif
case Op_ArrayCopy:
case Op_CallLeafNoFP:
// Most array copies are ArrayCopy nodes at this point but there
// are still a few direct calls to the copy subroutines (See
// PhaseStringOpts::copy_string())
is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
call->as_CallLeaf()->is_call_to_arraycopystub();
// fall through
case Op_CallLeaf: {
// Stub calls, objects do not escape but they are not scale replaceable.
// Adjust escape state for outgoing arguments.
const TypeTuple * d = call->tf()->domain_sig();
bool src_has_oops = false;
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
const Type* at = d->field_at(i);
Node *arg = call->in(i);
if (arg == NULL) {
continue;
}
const Type *aat = _igvn->type(arg);
if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr())
continue;
if (arg->is_AddP()) {
//
// The inline_native_clone() case when the arraycopy stub is called
// after the allocation before Initialize and CheckCastPP nodes.
// Or normal arraycopy for object arrays case.
//
// Set AddP's base (Allocate) as not scalar replaceable since
// pointer to the base (with offset) is passed as argument.
//
arg = get_addp_base(arg);
}
PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
assert(arg_ptn != NULL, "should be registered");
PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
aat->isa_ptr() != NULL, "expecting an Ptr");
bool arg_has_oops = aat->isa_oopptr() &&
(aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
(aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()) ||
(aat->isa_aryptr() && aat->isa_aryptr()->elem() != NULL &&
aat->isa_aryptr()->elem()->isa_valuetype() &&
aat->isa_aryptr()->elem()->value_klass()->contains_oops()));
if (i == TypeFunc::Parms) {
src_has_oops = arg_has_oops;