Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/opto/escape.cpp
40930 views
1
/*
2
* Copyright (c) 2005, 2021, 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 "ci/bcEscapeAnalyzer.hpp"
27
#include "compiler/compileLog.hpp"
28
#include "gc/shared/barrierSet.hpp"
29
#include "gc/shared/c2/barrierSetC2.hpp"
30
#include "libadt/vectset.hpp"
31
#include "memory/allocation.hpp"
32
#include "memory/resourceArea.hpp"
33
#include "opto/c2compiler.hpp"
34
#include "opto/arraycopynode.hpp"
35
#include "opto/callnode.hpp"
36
#include "opto/cfgnode.hpp"
37
#include "opto/compile.hpp"
38
#include "opto/escape.hpp"
39
#include "opto/phaseX.hpp"
40
#include "opto/movenode.hpp"
41
#include "opto/rootnode.hpp"
42
#include "utilities/macros.hpp"
43
44
ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
45
_nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
46
_in_worklist(C->comp_arena()),
47
_next_pidx(0),
48
_collecting(true),
49
_verify(false),
50
_compile(C),
51
_igvn(igvn),
52
_node_map(C->comp_arena()) {
53
// Add unknown java object.
54
add_java_object(C->top(), PointsToNode::GlobalEscape);
55
phantom_obj = ptnode_adr(C->top()->_idx)->as_JavaObject();
56
// Add ConP(#NULL) and ConN(#NULL) nodes.
57
Node* oop_null = igvn->zerocon(T_OBJECT);
58
assert(oop_null->_idx < nodes_size(), "should be created already");
59
add_java_object(oop_null, PointsToNode::NoEscape);
60
null_obj = ptnode_adr(oop_null->_idx)->as_JavaObject();
61
if (UseCompressedOops) {
62
Node* noop_null = igvn->zerocon(T_NARROWOOP);
63
assert(noop_null->_idx < nodes_size(), "should be created already");
64
map_ideal_node(noop_null, null_obj);
65
}
66
}
67
68
bool ConnectionGraph::has_candidates(Compile *C) {
69
// EA brings benefits only when the code has allocations and/or locks which
70
// are represented by ideal Macro nodes.
71
int cnt = C->macro_count();
72
for (int i = 0; i < cnt; i++) {
73
Node *n = C->macro_node(i);
74
if (n->is_Allocate()) {
75
return true;
76
}
77
if (n->is_Lock()) {
78
Node* obj = n->as_Lock()->obj_node()->uncast();
79
if (!(obj->is_Parm() || obj->is_Con())) {
80
return true;
81
}
82
}
83
if (n->is_CallStaticJava() &&
84
n->as_CallStaticJava()->is_boxing_method()) {
85
return true;
86
}
87
}
88
return false;
89
}
90
91
void ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) {
92
Compile::TracePhase tp("escapeAnalysis", &Phase::timers[Phase::_t_escapeAnalysis]);
93
ResourceMark rm;
94
95
// Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction
96
// to create space for them in ConnectionGraph::_nodes[].
97
Node* oop_null = igvn->zerocon(T_OBJECT);
98
Node* noop_null = igvn->zerocon(T_NARROWOOP);
99
ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn);
100
// Perform escape analysis
101
if (congraph->compute_escape()) {
102
// There are non escaping objects.
103
C->set_congraph(congraph);
104
}
105
// Cleanup.
106
if (oop_null->outcnt() == 0) {
107
igvn->hash_delete(oop_null);
108
}
109
if (noop_null->outcnt() == 0) {
110
igvn->hash_delete(noop_null);
111
}
112
}
113
114
bool ConnectionGraph::compute_escape() {
115
Compile* C = _compile;
116
PhaseGVN* igvn = _igvn;
117
118
// Worklists used by EA.
119
Unique_Node_List delayed_worklist;
120
GrowableArray<Node*> alloc_worklist;
121
GrowableArray<Node*> ptr_cmp_worklist;
122
GrowableArray<Node*> storestore_worklist;
123
GrowableArray<ArrayCopyNode*> arraycopy_worklist;
124
GrowableArray<PointsToNode*> ptnodes_worklist;
125
GrowableArray<JavaObjectNode*> java_objects_worklist;
126
GrowableArray<JavaObjectNode*> non_escaped_allocs_worklist;
127
GrowableArray<FieldNode*> oop_fields_worklist;
128
GrowableArray<SafePointNode*> sfn_worklist;
129
DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
130
131
{ Compile::TracePhase tp("connectionGraph", &Phase::timers[Phase::_t_connectionGraph]);
132
133
// 1. Populate Connection Graph (CG) with PointsTo nodes.
134
ideal_nodes.map(C->live_nodes(), NULL); // preallocate space
135
// Initialize worklist
136
if (C->root() != NULL) {
137
ideal_nodes.push(C->root());
138
}
139
// Processed ideal nodes are unique on ideal_nodes list
140
// but several ideal nodes are mapped to the phantom_obj.
141
// To avoid duplicated entries on the following worklists
142
// add the phantom_obj only once to them.
143
ptnodes_worklist.append(phantom_obj);
144
java_objects_worklist.append(phantom_obj);
145
for( uint next = 0; next < ideal_nodes.size(); ++next ) {
146
Node* n = ideal_nodes.at(next);
147
// Create PointsTo nodes and add them to Connection Graph. Called
148
// only once per ideal node since ideal_nodes is Unique_Node list.
149
add_node_to_connection_graph(n, &delayed_worklist);
150
PointsToNode* ptn = ptnode_adr(n->_idx);
151
if (ptn != NULL && ptn != phantom_obj) {
152
ptnodes_worklist.append(ptn);
153
if (ptn->is_JavaObject()) {
154
java_objects_worklist.append(ptn->as_JavaObject());
155
if ((n->is_Allocate() || n->is_CallStaticJava()) &&
156
(ptn->escape_state() < PointsToNode::GlobalEscape)) {
157
// Only allocations and java static calls results are interesting.
158
non_escaped_allocs_worklist.append(ptn->as_JavaObject());
159
}
160
} else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
161
oop_fields_worklist.append(ptn->as_Field());
162
}
163
}
164
if (n->is_MergeMem()) {
165
// Collect all MergeMem nodes to add memory slices for
166
// scalar replaceable objects in split_unique_types().
167
_mergemem_worklist.append(n->as_MergeMem());
168
} else if (OptimizePtrCompare && n->is_Cmp() &&
169
(n->Opcode() == Op_CmpP || n->Opcode() == Op_CmpN)) {
170
// Collect compare pointers nodes.
171
ptr_cmp_worklist.append(n);
172
} else if (n->is_MemBarStoreStore()) {
173
// Collect all MemBarStoreStore nodes so that depending on the
174
// escape status of the associated Allocate node some of them
175
// may be eliminated.
176
storestore_worklist.append(n);
177
} else if (n->is_MemBar() && (n->Opcode() == Op_MemBarRelease) &&
178
(n->req() > MemBarNode::Precedent)) {
179
record_for_optimizer(n);
180
#ifdef ASSERT
181
} else if (n->is_AddP()) {
182
// Collect address nodes for graph verification.
183
addp_worklist.append(n);
184
#endif
185
} else if (n->is_ArrayCopy()) {
186
// Keep a list of ArrayCopy nodes so if one of its input is non
187
// escaping, we can record a unique type
188
arraycopy_worklist.append(n->as_ArrayCopy());
189
}
190
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
191
Node* m = n->fast_out(i); // Get user
192
ideal_nodes.push(m);
193
}
194
if (n-> is_SafePoint()) {
195
sfn_worklist.append(n->as_SafePoint());
196
}
197
}
198
if (non_escaped_allocs_worklist.length() == 0) {
199
_collecting = false;
200
return false; // Nothing to do.
201
}
202
// Add final simple edges to graph.
203
while(delayed_worklist.size() > 0) {
204
Node* n = delayed_worklist.pop();
205
add_final_edges(n);
206
}
207
208
#ifdef ASSERT
209
if (VerifyConnectionGraph) {
210
// Verify that no new simple edges could be created and all
211
// local vars has edges.
212
_verify = true;
213
int ptnodes_length = ptnodes_worklist.length();
214
for (int next = 0; next < ptnodes_length; ++next) {
215
PointsToNode* ptn = ptnodes_worklist.at(next);
216
add_final_edges(ptn->ideal_node());
217
if (ptn->is_LocalVar() && ptn->edge_count() == 0) {
218
ptn->dump();
219
assert(ptn->as_LocalVar()->edge_count() > 0, "sanity");
220
}
221
}
222
_verify = false;
223
}
224
#endif
225
// Bytecode analyzer BCEscapeAnalyzer, used for Call nodes
226
// processing, calls to CI to resolve symbols (types, fields, methods)
227
// referenced in bytecode. During symbol resolution VM may throw
228
// an exception which CI cleans and converts to compilation failure.
229
if (C->failing()) return false;
230
231
// 2. Finish Graph construction by propagating references to all
232
// java objects through graph.
233
if (!complete_connection_graph(ptnodes_worklist, non_escaped_allocs_worklist,
234
java_objects_worklist, oop_fields_worklist)) {
235
// All objects escaped or hit time or iterations limits.
236
_collecting = false;
237
return false;
238
}
239
240
// 3. Adjust scalar_replaceable state of nonescaping objects and push
241
// scalar replaceable allocations on alloc_worklist for processing
242
// in split_unique_types().
243
int non_escaped_length = non_escaped_allocs_worklist.length();
244
for (int next = 0; next < non_escaped_length; next++) {
245
JavaObjectNode* ptn = non_escaped_allocs_worklist.at(next);
246
bool noescape = (ptn->escape_state() == PointsToNode::NoEscape);
247
Node* n = ptn->ideal_node();
248
if (n->is_Allocate()) {
249
n->as_Allocate()->_is_non_escaping = noescape;
250
}
251
if (noescape && ptn->scalar_replaceable()) {
252
adjust_scalar_replaceable_state(ptn);
253
if (ptn->scalar_replaceable()) {
254
alloc_worklist.append(ptn->ideal_node());
255
}
256
}
257
}
258
259
#ifdef ASSERT
260
if (VerifyConnectionGraph) {
261
// Verify that graph is complete - no new edges could be added or needed.
262
verify_connection_graph(ptnodes_worklist, non_escaped_allocs_worklist,
263
java_objects_worklist, addp_worklist);
264
}
265
assert(C->unique() == nodes_size(), "no new ideal nodes should be added during ConnectionGraph build");
266
assert(null_obj->escape_state() == PointsToNode::NoEscape &&
267
null_obj->edge_count() == 0 &&
268
!null_obj->arraycopy_src() &&
269
!null_obj->arraycopy_dst(), "sanity");
270
#endif
271
272
_collecting = false;
273
274
} // TracePhase t3("connectionGraph")
275
276
// 4. Optimize ideal graph based on EA information.
277
bool has_non_escaping_obj = (non_escaped_allocs_worklist.length() > 0);
278
if (has_non_escaping_obj) {
279
optimize_ideal_graph(ptr_cmp_worklist, storestore_worklist);
280
}
281
282
#ifndef PRODUCT
283
if (PrintEscapeAnalysis) {
284
dump(ptnodes_worklist); // Dump ConnectionGraph
285
}
286
#endif
287
288
#ifdef ASSERT
289
if (VerifyConnectionGraph) {
290
int alloc_length = alloc_worklist.length();
291
for (int next = 0; next < alloc_length; ++next) {
292
Node* n = alloc_worklist.at(next);
293
PointsToNode* ptn = ptnode_adr(n->_idx);
294
assert(ptn->escape_state() == PointsToNode::NoEscape && ptn->scalar_replaceable(), "sanity");
295
}
296
}
297
#endif
298
299
// 5. Separate memory graph for scalar replaceable allcations.
300
bool has_scalar_replaceable_candidates = (alloc_worklist.length() > 0);
301
if (has_scalar_replaceable_candidates &&
302
C->AliasLevel() >= 3 && EliminateAllocations) {
303
// Now use the escape information to create unique types for
304
// scalar replaceable objects.
305
split_unique_types(alloc_worklist, arraycopy_worklist);
306
if (C->failing()) return false;
307
C->print_method(PHASE_AFTER_EA, 2);
308
309
#ifdef ASSERT
310
} else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
311
tty->print("=== No allocations eliminated for ");
312
C->method()->print_short_name();
313
if (!EliminateAllocations) {
314
tty->print(" since EliminateAllocations is off ===");
315
} else if(!has_scalar_replaceable_candidates) {
316
tty->print(" since there are no scalar replaceable candidates ===");
317
} else if(C->AliasLevel() < 3) {
318
tty->print(" since AliasLevel < 3 ===");
319
}
320
tty->cr();
321
#endif
322
}
323
324
// Annotate at safepoints if they have <= ArgEscape objects in their scope and at
325
// java calls if they pass ArgEscape objects as parameters.
326
if (has_non_escaping_obj &&
327
(C->env()->should_retain_local_variables() ||
328
C->env()->jvmti_can_get_owned_monitor_info() ||
329
C->env()->jvmti_can_walk_any_space() ||
330
DeoptimizeObjectsALot)) {
331
int sfn_length = sfn_worklist.length();
332
for (int next = 0; next < sfn_length; next++) {
333
SafePointNode* sfn = sfn_worklist.at(next);
334
sfn->set_has_ea_local_in_scope(has_ea_local_in_scope(sfn));
335
if (sfn->is_CallJava()) {
336
CallJavaNode* call = sfn->as_CallJava();
337
call->set_arg_escape(has_arg_escape(call));
338
}
339
}
340
}
341
342
return has_non_escaping_obj;
343
}
344
345
// Returns true if there is an object in the scope of sfn that does not escape globally.
346
bool ConnectionGraph::has_ea_local_in_scope(SafePointNode* sfn) {
347
Compile* C = _compile;
348
for (JVMState* jvms = sfn->jvms(); jvms != NULL; jvms = jvms->caller()) {
349
if (C->env()->should_retain_local_variables() || C->env()->jvmti_can_walk_any_space() ||
350
DeoptimizeObjectsALot) {
351
// Jvmti agents can access locals. Must provide info about local objects at runtime.
352
int num_locs = jvms->loc_size();
353
for (int idx = 0; idx < num_locs; idx++) {
354
Node* l = sfn->local(jvms, idx);
355
if (not_global_escape(l)) {
356
return true;
357
}
358
}
359
}
360
if (C->env()->jvmti_can_get_owned_monitor_info() ||
361
C->env()->jvmti_can_walk_any_space() || DeoptimizeObjectsALot) {
362
// Jvmti agents can read monitors. Must provide info about locked objects at runtime.
363
int num_mon = jvms->nof_monitors();
364
for (int idx = 0; idx < num_mon; idx++) {
365
Node* m = sfn->monitor_obj(jvms, idx);
366
if (m != NULL && not_global_escape(m)) {
367
return true;
368
}
369
}
370
}
371
}
372
return false;
373
}
374
375
// Returns true if at least one of the arguments to the call is an object
376
// that does not escape globally.
377
bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
378
if (call->method() != NULL) {
379
uint max_idx = TypeFunc::Parms + call->method()->arg_size();
380
for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
381
Node* p = call->in(idx);
382
if (not_global_escape(p)) {
383
return true;
384
}
385
}
386
} else {
387
const char* name = call->as_CallStaticJava()->_name;
388
assert(name != NULL, "no name");
389
// no arg escapes through uncommon traps
390
if (strcmp(name, "uncommon_trap") != 0) {
391
// process_call_arguments() assumes that all arguments escape globally
392
const TypeTuple* d = call->tf()->domain();
393
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
394
const Type* at = d->field_at(i);
395
if (at->isa_oopptr() != NULL) {
396
return true;
397
}
398
}
399
}
400
}
401
return false;
402
}
403
404
405
406
// Utility function for nodes that load an object
407
void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
408
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
409
// ThreadLocal has RawPtr type.
410
const Type* t = _igvn->type(n);
411
if (t->make_ptr() != NULL) {
412
Node* adr = n->in(MemNode::Address);
413
#ifdef ASSERT
414
if (!adr->is_AddP()) {
415
assert(_igvn->type(adr)->isa_rawptr(), "sanity");
416
} else {
417
assert((ptnode_adr(adr->_idx) == NULL ||
418
ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
419
}
420
#endif
421
add_local_var_and_edge(n, PointsToNode::NoEscape,
422
adr, delayed_worklist);
423
}
424
}
425
426
// Populate Connection Graph with PointsTo nodes and create simple
427
// connection graph edges.
428
void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
429
assert(!_verify, "this method should not be called for verification");
430
PhaseGVN* igvn = _igvn;
431
uint n_idx = n->_idx;
432
PointsToNode* n_ptn = ptnode_adr(n_idx);
433
if (n_ptn != NULL) {
434
return; // No need to redefine PointsTo node during first iteration.
435
}
436
int opcode = n->Opcode();
437
bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_to_con_graph(this, igvn, delayed_worklist, n, opcode);
438
if (gc_handled) {
439
return; // Ignore node if already handled by GC.
440
}
441
442
if (n->is_Call()) {
443
// Arguments to allocation and locking don't escape.
444
if (n->is_AbstractLock()) {
445
// Put Lock and Unlock nodes on IGVN worklist to process them during
446
// first IGVN optimization when escape information is still available.
447
record_for_optimizer(n);
448
} else if (n->is_Allocate()) {
449
add_call_node(n->as_Call());
450
record_for_optimizer(n);
451
} else {
452
if (n->is_CallStaticJava()) {
453
const char* name = n->as_CallStaticJava()->_name;
454
if (name != NULL && strcmp(name, "uncommon_trap") == 0) {
455
return; // Skip uncommon traps
456
}
457
}
458
// Don't mark as processed since call's arguments have to be processed.
459
delayed_worklist->push(n);
460
// Check if a call returns an object.
461
if ((n->as_Call()->returns_pointer() &&
462
n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
463
(n->is_CallStaticJava() &&
464
n->as_CallStaticJava()->is_boxing_method())) {
465
add_call_node(n->as_Call());
466
}
467
}
468
return;
469
}
470
// Put this check here to process call arguments since some call nodes
471
// point to phantom_obj.
472
if (n_ptn == phantom_obj || n_ptn == null_obj) {
473
return; // Skip predefined nodes.
474
}
475
switch (opcode) {
476
case Op_AddP: {
477
Node* base = get_addp_base(n);
478
PointsToNode* ptn_base = ptnode_adr(base->_idx);
479
// Field nodes are created for all field types. They are used in
480
// adjust_scalar_replaceable_state() and split_unique_types().
481
// Note, non-oop fields will have only base edges in Connection
482
// Graph because such fields are not used for oop loads and stores.
483
int offset = address_offset(n, igvn);
484
add_field(n, PointsToNode::NoEscape, offset);
485
if (ptn_base == NULL) {
486
delayed_worklist->push(n); // Process it later.
487
} else {
488
n_ptn = ptnode_adr(n_idx);
489
add_base(n_ptn->as_Field(), ptn_base);
490
}
491
break;
492
}
493
case Op_CastX2P: {
494
map_ideal_node(n, phantom_obj);
495
break;
496
}
497
case Op_CastPP:
498
case Op_CheckCastPP:
499
case Op_EncodeP:
500
case Op_DecodeN:
501
case Op_EncodePKlass:
502
case Op_DecodeNKlass: {
503
add_local_var_and_edge(n, PointsToNode::NoEscape,
504
n->in(1), delayed_worklist);
505
break;
506
}
507
case Op_CMoveP: {
508
add_local_var(n, PointsToNode::NoEscape);
509
// Do not add edges during first iteration because some could be
510
// not defined yet.
511
delayed_worklist->push(n);
512
break;
513
}
514
case Op_ConP:
515
case Op_ConN:
516
case Op_ConNKlass: {
517
// assume all oop constants globally escape except for null
518
PointsToNode::EscapeState es;
519
const Type* t = igvn->type(n);
520
if (t == TypePtr::NULL_PTR || t == TypeNarrowOop::NULL_PTR) {
521
es = PointsToNode::NoEscape;
522
} else {
523
es = PointsToNode::GlobalEscape;
524
}
525
add_java_object(n, es);
526
break;
527
}
528
case Op_CreateEx: {
529
// assume that all exception objects globally escape
530
map_ideal_node(n, phantom_obj);
531
break;
532
}
533
case Op_LoadKlass:
534
case Op_LoadNKlass: {
535
// Unknown class is loaded
536
map_ideal_node(n, phantom_obj);
537
break;
538
}
539
case Op_LoadP:
540
case Op_LoadN:
541
case Op_LoadPLocked: {
542
add_objload_to_connection_graph(n, delayed_worklist);
543
break;
544
}
545
case Op_Parm: {
546
map_ideal_node(n, phantom_obj);
547
break;
548
}
549
case Op_PartialSubtypeCheck: {
550
// Produces Null or notNull and is used in only in CmpP so
551
// phantom_obj could be used.
552
map_ideal_node(n, phantom_obj); // Result is unknown
553
break;
554
}
555
case Op_Phi: {
556
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
557
// ThreadLocal has RawPtr type.
558
const Type* t = n->as_Phi()->type();
559
if (t->make_ptr() != NULL) {
560
add_local_var(n, PointsToNode::NoEscape);
561
// Do not add edges during first iteration because some could be
562
// not defined yet.
563
delayed_worklist->push(n);
564
}
565
break;
566
}
567
case Op_Proj: {
568
// we are only interested in the oop result projection from a call
569
if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
570
n->in(0)->as_Call()->returns_pointer()) {
571
add_local_var_and_edge(n, PointsToNode::NoEscape,
572
n->in(0), delayed_worklist);
573
}
574
break;
575
}
576
case Op_Rethrow: // Exception object escapes
577
case Op_Return: {
578
if (n->req() > TypeFunc::Parms &&
579
igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
580
// Treat Return value as LocalVar with GlobalEscape escape state.
581
add_local_var_and_edge(n, PointsToNode::GlobalEscape,
582
n->in(TypeFunc::Parms), delayed_worklist);
583
}
584
break;
585
}
586
case Op_CompareAndExchangeP:
587
case Op_CompareAndExchangeN:
588
case Op_GetAndSetP:
589
case Op_GetAndSetN: {
590
add_objload_to_connection_graph(n, delayed_worklist);
591
// fall-through
592
}
593
case Op_StoreP:
594
case Op_StoreN:
595
case Op_StoreNKlass:
596
case Op_StorePConditional:
597
case Op_WeakCompareAndSwapP:
598
case Op_WeakCompareAndSwapN:
599
case Op_CompareAndSwapP:
600
case Op_CompareAndSwapN: {
601
add_to_congraph_unsafe_access(n, opcode, delayed_worklist);
602
break;
603
}
604
case Op_AryEq:
605
case Op_HasNegatives:
606
case Op_StrComp:
607
case Op_StrEquals:
608
case Op_StrIndexOf:
609
case Op_StrIndexOfChar:
610
case Op_StrInflatedCopy:
611
case Op_StrCompressedCopy:
612
case Op_EncodeISOArray: {
613
add_local_var(n, PointsToNode::ArgEscape);
614
delayed_worklist->push(n); // Process it later.
615
break;
616
}
617
case Op_ThreadLocal: {
618
add_java_object(n, PointsToNode::ArgEscape);
619
break;
620
}
621
default:
622
; // Do nothing for nodes not related to EA.
623
}
624
return;
625
}
626
627
#ifdef ASSERT
628
#define ELSE_FAIL(name) \
629
/* Should not be called for not pointer type. */ \
630
n->dump(1); \
631
assert(false, name); \
632
break;
633
#else
634
#define ELSE_FAIL(name) \
635
break;
636
#endif
637
638
// Add final simple edges to graph.
639
void ConnectionGraph::add_final_edges(Node *n) {
640
PointsToNode* n_ptn = ptnode_adr(n->_idx);
641
#ifdef ASSERT
642
if (_verify && n_ptn->is_JavaObject())
643
return; // This method does not change graph for JavaObject.
644
#endif
645
646
if (n->is_Call()) {
647
process_call_arguments(n->as_Call());
648
return;
649
}
650
assert(n->is_Store() || n->is_LoadStore() ||
651
(n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
652
"node should be registered already");
653
int opcode = n->Opcode();
654
bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
655
if (gc_handled) {
656
return; // Ignore node if already handled by GC.
657
}
658
switch (opcode) {
659
case Op_AddP: {
660
Node* base = get_addp_base(n);
661
PointsToNode* ptn_base = ptnode_adr(base->_idx);
662
assert(ptn_base != NULL, "field's base should be registered");
663
add_base(n_ptn->as_Field(), ptn_base);
664
break;
665
}
666
case Op_CastPP:
667
case Op_CheckCastPP:
668
case Op_EncodeP:
669
case Op_DecodeN:
670
case Op_EncodePKlass:
671
case Op_DecodeNKlass: {
672
add_local_var_and_edge(n, PointsToNode::NoEscape,
673
n->in(1), NULL);
674
break;
675
}
676
case Op_CMoveP: {
677
for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
678
Node* in = n->in(i);
679
if (in == NULL) {
680
continue; // ignore NULL
681
}
682
Node* uncast_in = in->uncast();
683
if (uncast_in->is_top() || uncast_in == n) {
684
continue; // ignore top or inputs which go back this node
685
}
686
PointsToNode* ptn = ptnode_adr(in->_idx);
687
assert(ptn != NULL, "node should be registered");
688
add_edge(n_ptn, ptn);
689
}
690
break;
691
}
692
case Op_LoadP:
693
case Op_LoadN:
694
case Op_LoadPLocked: {
695
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
696
// ThreadLocal has RawPtr type.
697
const Type* t = _igvn->type(n);
698
if (t->make_ptr() != NULL) {
699
Node* adr = n->in(MemNode::Address);
700
add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
701
break;
702
}
703
ELSE_FAIL("Op_LoadP");
704
}
705
case Op_Phi: {
706
// Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
707
// ThreadLocal has RawPtr type.
708
const Type* t = n->as_Phi()->type();
709
if (t->make_ptr() != NULL) {
710
for (uint i = 1; i < n->req(); i++) {
711
Node* in = n->in(i);
712
if (in == NULL) {
713
continue; // ignore NULL
714
}
715
Node* uncast_in = in->uncast();
716
if (uncast_in->is_top() || uncast_in == n) {
717
continue; // ignore top or inputs which go back this node
718
}
719
PointsToNode* ptn = ptnode_adr(in->_idx);
720
assert(ptn != NULL, "node should be registered");
721
add_edge(n_ptn, ptn);
722
}
723
break;
724
}
725
ELSE_FAIL("Op_Phi");
726
}
727
case Op_Proj: {
728
// we are only interested in the oop result projection from a call
729
if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
730
n->in(0)->as_Call()->returns_pointer()) {
731
add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
732
break;
733
}
734
ELSE_FAIL("Op_Proj");
735
}
736
case Op_Rethrow: // Exception object escapes
737
case Op_Return: {
738
if (n->req() > TypeFunc::Parms &&
739
_igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
740
// Treat Return value as LocalVar with GlobalEscape escape state.
741
add_local_var_and_edge(n, PointsToNode::GlobalEscape,
742
n->in(TypeFunc::Parms), NULL);
743
break;
744
}
745
ELSE_FAIL("Op_Return");
746
}
747
case Op_StoreP:
748
case Op_StoreN:
749
case Op_StoreNKlass:
750
case Op_StorePConditional:
751
case Op_CompareAndExchangeP:
752
case Op_CompareAndExchangeN:
753
case Op_CompareAndSwapP:
754
case Op_CompareAndSwapN:
755
case Op_WeakCompareAndSwapP:
756
case Op_WeakCompareAndSwapN:
757
case Op_GetAndSetP:
758
case Op_GetAndSetN: {
759
if (add_final_edges_unsafe_access(n, opcode)) {
760
break;
761
}
762
ELSE_FAIL("Op_StoreP");
763
}
764
case Op_AryEq:
765
case Op_HasNegatives:
766
case Op_StrComp:
767
case Op_StrEquals:
768
case Op_StrIndexOf:
769
case Op_StrIndexOfChar:
770
case Op_StrInflatedCopy:
771
case Op_StrCompressedCopy:
772
case Op_EncodeISOArray: {
773
// char[]/byte[] arrays passed to string intrinsic do not escape but
774
// they are not scalar replaceable. Adjust escape state for them.
775
// Start from in(2) edge since in(1) is memory edge.
776
for (uint i = 2; i < n->req(); i++) {
777
Node* adr = n->in(i);
778
const Type* at = _igvn->type(adr);
779
if (!adr->is_top() && at->isa_ptr()) {
780
assert(at == Type::TOP || at == TypePtr::NULL_PTR ||
781
at->isa_ptr() != NULL, "expecting a pointer");
782
if (adr->is_AddP()) {
783
adr = get_addp_base(adr);
784
}
785
PointsToNode* ptn = ptnode_adr(adr->_idx);
786
assert(ptn != NULL, "node should be registered");
787
add_edge(n_ptn, ptn);
788
}
789
}
790
break;
791
}
792
default: {
793
// This method should be called only for EA specific nodes which may
794
// miss some edges when they were created.
795
#ifdef ASSERT
796
n->dump(1);
797
#endif
798
guarantee(false, "unknown node");
799
}
800
}
801
return;
802
}
803
804
void ConnectionGraph::add_to_congraph_unsafe_access(Node* n, uint opcode, Unique_Node_List* delayed_worklist) {
805
Node* adr = n->in(MemNode::Address);
806
const Type* adr_type = _igvn->type(adr);
807
adr_type = adr_type->make_ptr();
808
if (adr_type == NULL) {
809
return; // skip dead nodes
810
}
811
if (adr_type->isa_oopptr()
812
|| ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
813
&& adr_type == TypeRawPtr::NOTNULL
814
&& is_captured_store_address(adr))) {
815
delayed_worklist->push(n); // Process it later.
816
#ifdef ASSERT
817
assert (adr->is_AddP(), "expecting an AddP");
818
if (adr_type == TypeRawPtr::NOTNULL) {
819
// Verify a raw address for a store captured by Initialize node.
820
int offs = (int) _igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
821
assert(offs != Type::OffsetBot, "offset must be a constant");
822
}
823
#endif
824
} else {
825
// Ignore copy the displaced header to the BoxNode (OSR compilation).
826
if (adr->is_BoxLock()) {
827
return;
828
}
829
// Stored value escapes in unsafe access.
830
if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
831
delayed_worklist->push(n); // Process unsafe access later.
832
return;
833
}
834
#ifdef ASSERT
835
n->dump(1);
836
assert(false, "not unsafe");
837
#endif
838
}
839
}
840
841
bool ConnectionGraph::add_final_edges_unsafe_access(Node* n, uint opcode) {
842
Node* adr = n->in(MemNode::Address);
843
const Type *adr_type = _igvn->type(adr);
844
adr_type = adr_type->make_ptr();
845
#ifdef ASSERT
846
if (adr_type == NULL) {
847
n->dump(1);
848
assert(adr_type != NULL, "dead node should not be on list");
849
return true;
850
}
851
#endif
852
853
if (opcode == Op_GetAndSetP || opcode == Op_GetAndSetN ||
854
opcode == Op_CompareAndExchangeN || opcode == Op_CompareAndExchangeP) {
855
add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
856
}
857
858
if (adr_type->isa_oopptr()
859
|| ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
860
&& adr_type == TypeRawPtr::NOTNULL
861
&& is_captured_store_address(adr))) {
862
// Point Address to Value
863
PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
864
assert(adr_ptn != NULL &&
865
adr_ptn->as_Field()->is_oop(), "node should be registered");
866
Node* val = n->in(MemNode::ValueIn);
867
PointsToNode* ptn = ptnode_adr(val->_idx);
868
assert(ptn != NULL, "node should be registered");
869
add_edge(adr_ptn, ptn);
870
return true;
871
} else if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
872
// Stored value escapes in unsafe access.
873
Node* val = n->in(MemNode::ValueIn);
874
PointsToNode* ptn = ptnode_adr(val->_idx);
875
assert(ptn != NULL, "node should be registered");
876
set_escape_state(ptn, PointsToNode::GlobalEscape);
877
// Add edge to object for unsafe access with offset.
878
PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
879
assert(adr_ptn != NULL, "node should be registered");
880
if (adr_ptn->is_Field()) {
881
assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
882
add_edge(adr_ptn, ptn);
883
}
884
return true;
885
}
886
return false;
887
}
888
889
void ConnectionGraph::add_call_node(CallNode* call) {
890
assert(call->returns_pointer(), "only for call which returns pointer");
891
uint call_idx = call->_idx;
892
if (call->is_Allocate()) {
893
Node* k = call->in(AllocateNode::KlassNode);
894
const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
895
assert(kt != NULL, "TypeKlassPtr required.");
896
ciKlass* cik = kt->klass();
897
PointsToNode::EscapeState es = PointsToNode::NoEscape;
898
bool scalar_replaceable = true;
899
if (call->is_AllocateArray()) {
900
if (!cik->is_array_klass()) { // StressReflectiveCode
901
es = PointsToNode::GlobalEscape;
902
} else {
903
int length = call->in(AllocateNode::ALength)->find_int_con(-1);
904
if (length < 0 || length > EliminateAllocationArraySizeLimit) {
905
// Not scalar replaceable if the length is not constant or too big.
906
scalar_replaceable = false;
907
}
908
}
909
} else { // Allocate instance
910
if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||
911
cik->is_subclass_of(_compile->env()->Reference_klass()) ||
912
!cik->is_instance_klass() || // StressReflectiveCode
913
!cik->as_instance_klass()->can_be_instantiated() ||
914
cik->as_instance_klass()->has_finalizer()) {
915
es = PointsToNode::GlobalEscape;
916
} else {
917
int nfields = cik->as_instance_klass()->nof_nonstatic_fields();
918
if (nfields > EliminateAllocationFieldsLimit) {
919
// Not scalar replaceable if there are too many fields.
920
scalar_replaceable = false;
921
}
922
}
923
}
924
add_java_object(call, es);
925
PointsToNode* ptn = ptnode_adr(call_idx);
926
if (!scalar_replaceable && ptn->scalar_replaceable()) {
927
ptn->set_scalar_replaceable(false);
928
}
929
} else if (call->is_CallStaticJava()) {
930
// Call nodes could be different types:
931
//
932
// 1. CallDynamicJavaNode (what happened during call is unknown):
933
//
934
// - mapped to GlobalEscape JavaObject node if oop is returned;
935
//
936
// - all oop arguments are escaping globally;
937
//
938
// 2. CallStaticJavaNode (execute bytecode analysis if possible):
939
//
940
// - the same as CallDynamicJavaNode if can't do bytecode analysis;
941
//
942
// - mapped to GlobalEscape JavaObject node if unknown oop is returned;
943
// - mapped to NoEscape JavaObject node if non-escaping object allocated
944
// during call is returned;
945
// - mapped to ArgEscape LocalVar node pointed to object arguments
946
// which are returned and does not escape during call;
947
//
948
// - oop arguments escaping status is defined by bytecode analysis;
949
//
950
// For a static call, we know exactly what method is being called.
951
// Use bytecode estimator to record whether the call's return value escapes.
952
ciMethod* meth = call->as_CallJava()->method();
953
if (meth == NULL) {
954
const char* name = call->as_CallStaticJava()->_name;
955
assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");
956
// Returns a newly allocated non-escaped object.
957
add_java_object(call, PointsToNode::NoEscape);
958
ptnode_adr(call_idx)->set_scalar_replaceable(false);
959
} else if (meth->is_boxing_method()) {
960
// Returns boxing object
961
PointsToNode::EscapeState es;
962
vmIntrinsics::ID intr = meth->intrinsic_id();
963
if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
964
// It does not escape if object is always allocated.
965
es = PointsToNode::NoEscape;
966
} else {
967
// It escapes globally if object could be loaded from cache.
968
es = PointsToNode::GlobalEscape;
969
}
970
add_java_object(call, es);
971
} else {
972
BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
973
call_analyzer->copy_dependencies(_compile->dependencies());
974
if (call_analyzer->is_return_allocated()) {
975
// Returns a newly allocated non-escaped object, simply
976
// update dependency information.
977
// Mark it as NoEscape so that objects referenced by
978
// it's fields will be marked as NoEscape at least.
979
add_java_object(call, PointsToNode::NoEscape);
980
ptnode_adr(call_idx)->set_scalar_replaceable(false);
981
} else {
982
// Determine whether any arguments are returned.
983
const TypeTuple* d = call->tf()->domain();
984
bool ret_arg = false;
985
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
986
if (d->field_at(i)->isa_ptr() != NULL &&
987
call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
988
ret_arg = true;
989
break;
990
}
991
}
992
if (ret_arg) {
993
add_local_var(call, PointsToNode::ArgEscape);
994
} else {
995
// Returns unknown object.
996
map_ideal_node(call, phantom_obj);
997
}
998
}
999
}
1000
} else {
1001
// An other type of call, assume the worst case:
1002
// returned value is unknown and globally escapes.
1003
assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
1004
map_ideal_node(call, phantom_obj);
1005
}
1006
}
1007
1008
void ConnectionGraph::process_call_arguments(CallNode *call) {
1009
bool is_arraycopy = false;
1010
switch (call->Opcode()) {
1011
#ifdef ASSERT
1012
case Op_Allocate:
1013
case Op_AllocateArray:
1014
case Op_Lock:
1015
case Op_Unlock:
1016
assert(false, "should be done already");
1017
break;
1018
#endif
1019
case Op_ArrayCopy:
1020
case Op_CallLeafNoFP:
1021
// Most array copies are ArrayCopy nodes at this point but there
1022
// are still a few direct calls to the copy subroutines (See
1023
// PhaseStringOpts::copy_string())
1024
is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
1025
call->as_CallLeaf()->is_call_to_arraycopystub();
1026
// fall through
1027
case Op_CallLeafVector:
1028
case Op_CallLeaf: {
1029
// Stub calls, objects do not escape but they are not scale replaceable.
1030
// Adjust escape state for outgoing arguments.
1031
const TypeTuple * d = call->tf()->domain();
1032
bool src_has_oops = false;
1033
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1034
const Type* at = d->field_at(i);
1035
Node *arg = call->in(i);
1036
if (arg == NULL) {
1037
continue;
1038
}
1039
const Type *aat = _igvn->type(arg);
1040
if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr()) {
1041
continue;
1042
}
1043
if (arg->is_AddP()) {
1044
//
1045
// The inline_native_clone() case when the arraycopy stub is called
1046
// after the allocation before Initialize and CheckCastPP nodes.
1047
// Or normal arraycopy for object arrays case.
1048
//
1049
// Set AddP's base (Allocate) as not scalar replaceable since
1050
// pointer to the base (with offset) is passed as argument.
1051
//
1052
arg = get_addp_base(arg);
1053
}
1054
PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1055
assert(arg_ptn != NULL, "should be registered");
1056
PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
1057
if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
1058
assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1059
aat->isa_ptr() != NULL, "expecting an Ptr");
1060
bool arg_has_oops = aat->isa_oopptr() &&
1061
(aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
1062
(aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));
1063
if (i == TypeFunc::Parms) {
1064
src_has_oops = arg_has_oops;
1065
}
1066
//
1067
// src or dst could be j.l.Object when other is basic type array:
1068
//
1069
// arraycopy(char[],0,Object*,0,size);
1070
// arraycopy(Object*,0,char[],0,size);
1071
//
1072
// Don't add edges in such cases.
1073
//
1074
bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
1075
arg_has_oops && (i > TypeFunc::Parms);
1076
#ifdef ASSERT
1077
if (!(is_arraycopy ||
1078
BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
1079
(call->as_CallLeaf()->_name != NULL &&
1080
(strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
1081
strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
1082
strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
1083
strcmp(call->as_CallLeaf()->_name, "aescrypt_encryptBlock") == 0 ||
1084
strcmp(call->as_CallLeaf()->_name, "aescrypt_decryptBlock") == 0 ||
1085
strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_encryptAESCrypt") == 0 ||
1086
strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_decryptAESCrypt") == 0 ||
1087
strcmp(call->as_CallLeaf()->_name, "electronicCodeBook_encryptAESCrypt") == 0 ||
1088
strcmp(call->as_CallLeaf()->_name, "electronicCodeBook_decryptAESCrypt") == 0 ||
1089
strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1090
strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1091
strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1092
strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1093
strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1094
strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1095
strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1096
strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1097
strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1098
strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1099
strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1100
strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1101
strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1102
strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1103
strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1104
strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1105
strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1106
strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1107
strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
1108
strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1109
strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1110
strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0 ||
1111
strcmp(call->as_CallLeaf()->_name, "get_class_id_intrinsic") == 0)
1112
))) {
1113
call->dump();
1114
fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1115
}
1116
#endif
1117
// Always process arraycopy's destination object since
1118
// we need to add all possible edges to references in
1119
// source object.
1120
if (arg_esc >= PointsToNode::ArgEscape &&
1121
!arg_is_arraycopy_dest) {
1122
continue;
1123
}
1124
PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1125
if (call->is_ArrayCopy()) {
1126
ArrayCopyNode* ac = call->as_ArrayCopy();
1127
if (ac->is_clonebasic() ||
1128
ac->is_arraycopy_validated() ||
1129
ac->is_copyof_validated() ||
1130
ac->is_copyofrange_validated()) {
1131
es = PointsToNode::NoEscape;
1132
}
1133
}
1134
set_escape_state(arg_ptn, es);
1135
if (arg_is_arraycopy_dest) {
1136
Node* src = call->in(TypeFunc::Parms);
1137
if (src->is_AddP()) {
1138
src = get_addp_base(src);
1139
}
1140
PointsToNode* src_ptn = ptnode_adr(src->_idx);
1141
assert(src_ptn != NULL, "should be registered");
1142
if (arg_ptn != src_ptn) {
1143
// Special arraycopy edge:
1144
// A destination object's field can't have the source object
1145
// as base since objects escape states are not related.
1146
// Only escape state of destination object's fields affects
1147
// escape state of fields in source object.
1148
add_arraycopy(call, es, src_ptn, arg_ptn);
1149
}
1150
}
1151
}
1152
}
1153
break;
1154
}
1155
case Op_CallStaticJava: {
1156
// For a static call, we know exactly what method is being called.
1157
// Use bytecode estimator to record the call's escape affects
1158
#ifdef ASSERT
1159
const char* name = call->as_CallStaticJava()->_name;
1160
assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
1161
#endif
1162
ciMethod* meth = call->as_CallJava()->method();
1163
if ((meth != NULL) && meth->is_boxing_method()) {
1164
break; // Boxing methods do not modify any oops.
1165
}
1166
BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1167
// fall-through if not a Java method or no analyzer information
1168
if (call_analyzer != NULL) {
1169
PointsToNode* call_ptn = ptnode_adr(call->_idx);
1170
const TypeTuple* d = call->tf()->domain();
1171
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1172
const Type* at = d->field_at(i);
1173
int k = i - TypeFunc::Parms;
1174
Node* arg = call->in(i);
1175
PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1176
if (at->isa_ptr() != NULL &&
1177
call_analyzer->is_arg_returned(k)) {
1178
// The call returns arguments.
1179
if (call_ptn != NULL) { // Is call's result used?
1180
assert(call_ptn->is_LocalVar(), "node should be registered");
1181
assert(arg_ptn != NULL, "node should be registered");
1182
add_edge(call_ptn, arg_ptn);
1183
}
1184
}
1185
if (at->isa_oopptr() != NULL &&
1186
arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
1187
if (!call_analyzer->is_arg_stack(k)) {
1188
// The argument global escapes
1189
set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1190
} else {
1191
set_escape_state(arg_ptn, PointsToNode::ArgEscape);
1192
if (!call_analyzer->is_arg_local(k)) {
1193
// The argument itself doesn't escape, but any fields might
1194
set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1195
}
1196
}
1197
}
1198
}
1199
if (call_ptn != NULL && call_ptn->is_LocalVar()) {
1200
// The call returns arguments.
1201
assert(call_ptn->edge_count() > 0, "sanity");
1202
if (!call_analyzer->is_return_local()) {
1203
// Returns also unknown object.
1204
add_edge(call_ptn, phantom_obj);
1205
}
1206
}
1207
break;
1208
}
1209
}
1210
default: {
1211
// Fall-through here if not a Java method or no analyzer information
1212
// or some other type of call, assume the worst case: all arguments
1213
// globally escape.
1214
const TypeTuple* d = call->tf()->domain();
1215
for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1216
const Type* at = d->field_at(i);
1217
if (at->isa_oopptr() != NULL) {
1218
Node* arg = call->in(i);
1219
if (arg->is_AddP()) {
1220
arg = get_addp_base(arg);
1221
}
1222
assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
1223
set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
1224
}
1225
}
1226
}
1227
}
1228
}
1229
1230
1231
// Finish Graph construction.
1232
bool ConnectionGraph::complete_connection_graph(
1233
GrowableArray<PointsToNode*>& ptnodes_worklist,
1234
GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
1235
GrowableArray<JavaObjectNode*>& java_objects_worklist,
1236
GrowableArray<FieldNode*>& oop_fields_worklist) {
1237
// Normally only 1-3 passes needed to build Connection Graph depending
1238
// on graph complexity. Observed 8 passes in jvm2008 compiler.compiler.
1239
// Set limit to 20 to catch situation when something did go wrong and
1240
// bailout Escape Analysis.
1241
// Also limit build time to 20 sec (60 in debug VM), EscapeAnalysisTimeout flag.
1242
#define GRAPH_BUILD_ITER_LIMIT 20
1243
1244
// Propagate GlobalEscape and ArgEscape escape states and check that
1245
// we still have non-escaping objects. The method pushs on _worklist
1246
// Field nodes which reference phantom_object.
1247
if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_allocs_worklist)) {
1248
return false; // Nothing to do.
1249
}
1250
// Now propagate references to all JavaObject nodes.
1251
int java_objects_length = java_objects_worklist.length();
1252
elapsedTimer build_time;
1253
build_time.start();
1254
elapsedTimer time;
1255
bool timeout = false;
1256
int new_edges = 1;
1257
int iterations = 0;
1258
do {
1259
while ((new_edges > 0) &&
1260
(iterations++ < GRAPH_BUILD_ITER_LIMIT)) {
1261
double start_time = time.seconds();
1262
time.start();
1263
new_edges = 0;
1264
// Propagate references to phantom_object for nodes pushed on _worklist
1265
// by find_non_escaped_objects() and find_field_value().
1266
new_edges += add_java_object_edges(phantom_obj, false);
1267
for (int next = 0; next < java_objects_length; ++next) {
1268
JavaObjectNode* ptn = java_objects_worklist.at(next);
1269
new_edges += add_java_object_edges(ptn, true);
1270
1271
#define SAMPLE_SIZE 4
1272
if ((next % SAMPLE_SIZE) == 0) {
1273
// Each 4 iterations calculate how much time it will take
1274
// to complete graph construction.
1275
time.stop();
1276
// Poll for requests from shutdown mechanism to quiesce compiler
1277
// because Connection graph construction may take long time.
1278
CompileBroker::maybe_block();
1279
double stop_time = time.seconds();
1280
double time_per_iter = (stop_time - start_time) / (double)SAMPLE_SIZE;
1281
double time_until_end = time_per_iter * (double)(java_objects_length - next);
1282
if ((start_time + time_until_end) >= EscapeAnalysisTimeout) {
1283
timeout = true;
1284
break; // Timeout
1285
}
1286
start_time = stop_time;
1287
time.start();
1288
}
1289
#undef SAMPLE_SIZE
1290
1291
}
1292
if (timeout) break;
1293
if (new_edges > 0) {
1294
// Update escape states on each iteration if graph was updated.
1295
if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_allocs_worklist)) {
1296
return false; // Nothing to do.
1297
}
1298
}
1299
time.stop();
1300
if (time.seconds() >= EscapeAnalysisTimeout) {
1301
timeout = true;
1302
break;
1303
}
1304
}
1305
if ((iterations < GRAPH_BUILD_ITER_LIMIT) && !timeout) {
1306
time.start();
1307
// Find fields which have unknown value.
1308
int fields_length = oop_fields_worklist.length();
1309
for (int next = 0; next < fields_length; next++) {
1310
FieldNode* field = oop_fields_worklist.at(next);
1311
if (field->edge_count() == 0) {
1312
new_edges += find_field_value(field);
1313
// This code may added new edges to phantom_object.
1314
// Need an other cycle to propagate references to phantom_object.
1315
}
1316
}
1317
time.stop();
1318
if (time.seconds() >= EscapeAnalysisTimeout) {
1319
timeout = true;
1320
break;
1321
}
1322
} else {
1323
new_edges = 0; // Bailout
1324
}
1325
} while (new_edges > 0);
1326
1327
build_time.stop();
1328
_build_time = build_time.seconds();
1329
_build_iterations = iterations;
1330
1331
// Bailout if passed limits.
1332
if ((iterations >= GRAPH_BUILD_ITER_LIMIT) || timeout) {
1333
Compile* C = _compile;
1334
if (C->log() != NULL) {
1335
C->log()->begin_elem("connectionGraph_bailout reason='reached ");
1336
C->log()->text("%s", timeout ? "time" : "iterations");
1337
C->log()->end_elem(" limit'");
1338
}
1339
assert(ExitEscapeAnalysisOnTimeout, "infinite EA connection graph build (%f sec, %d iterations) with %d nodes and worklist size %d",
1340
_build_time, _build_iterations, nodes_size(), ptnodes_worklist.length());
1341
// Possible infinite build_connection_graph loop,
1342
// bailout (no changes to ideal graph were made).
1343
return false;
1344
}
1345
#ifdef ASSERT
1346
if (Verbose && PrintEscapeAnalysis) {
1347
tty->print_cr("EA: %d iterations and %f sec to build connection graph with %d nodes and worklist size %d",
1348
_build_iterations, _build_time, nodes_size(), ptnodes_worklist.length());
1349
}
1350
#endif
1351
1352
#undef GRAPH_BUILD_ITER_LIMIT
1353
1354
// Find fields initialized by NULL for non-escaping Allocations.
1355
int non_escaped_length = non_escaped_allocs_worklist.length();
1356
for (int next = 0; next < non_escaped_length; next++) {
1357
JavaObjectNode* ptn = non_escaped_allocs_worklist.at(next);
1358
PointsToNode::EscapeState es = ptn->escape_state();
1359
assert(es <= PointsToNode::ArgEscape, "sanity");
1360
if (es == PointsToNode::NoEscape) {
1361
if (find_init_values_null(ptn, _igvn) > 0) {
1362
// Adding references to NULL object does not change escape states
1363
// since it does not escape. Also no fields are added to NULL object.
1364
add_java_object_edges(null_obj, false);
1365
}
1366
}
1367
Node* n = ptn->ideal_node();
1368
if (n->is_Allocate()) {
1369
// The object allocated by this Allocate node will never be
1370
// seen by an other thread. Mark it so that when it is
1371
// expanded no MemBarStoreStore is added.
1372
InitializeNode* ini = n->as_Allocate()->initialization();
1373
if (ini != NULL)
1374
ini->set_does_not_escape();
1375
}
1376
}
1377
return true; // Finished graph construction.
1378
}
1379
1380
// Propagate GlobalEscape and ArgEscape escape states to all nodes
1381
// and check that we still have non-escaping java objects.
1382
bool ConnectionGraph::find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,
1383
GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist) {
1384
GrowableArray<PointsToNode*> escape_worklist;
1385
// First, put all nodes with GlobalEscape and ArgEscape states on worklist.
1386
int ptnodes_length = ptnodes_worklist.length();
1387
for (int next = 0; next < ptnodes_length; ++next) {
1388
PointsToNode* ptn = ptnodes_worklist.at(next);
1389
if (ptn->escape_state() >= PointsToNode::ArgEscape ||
1390
ptn->fields_escape_state() >= PointsToNode::ArgEscape) {
1391
escape_worklist.push(ptn);
1392
}
1393
}
1394
// Set escape states to referenced nodes (edges list).
1395
while (escape_worklist.length() > 0) {
1396
PointsToNode* ptn = escape_worklist.pop();
1397
PointsToNode::EscapeState es = ptn->escape_state();
1398
PointsToNode::EscapeState field_es = ptn->fields_escape_state();
1399
if (ptn->is_Field() && ptn->as_Field()->is_oop() &&
1400
es >= PointsToNode::ArgEscape) {
1401
// GlobalEscape or ArgEscape state of field means it has unknown value.
1402
if (add_edge(ptn, phantom_obj)) {
1403
// New edge was added
1404
add_field_uses_to_worklist(ptn->as_Field());
1405
}
1406
}
1407
for (EdgeIterator i(ptn); i.has_next(); i.next()) {
1408
PointsToNode* e = i.get();
1409
if (e->is_Arraycopy()) {
1410
assert(ptn->arraycopy_dst(), "sanity");
1411
// Propagate only fields escape state through arraycopy edge.
1412
if (e->fields_escape_state() < field_es) {
1413
set_fields_escape_state(e, field_es);
1414
escape_worklist.push(e);
1415
}
1416
} else if (es >= field_es) {
1417
// fields_escape_state is also set to 'es' if it is less than 'es'.
1418
if (e->escape_state() < es) {
1419
set_escape_state(e, es);
1420
escape_worklist.push(e);
1421
}
1422
} else {
1423
// Propagate field escape state.
1424
bool es_changed = false;
1425
if (e->fields_escape_state() < field_es) {
1426
set_fields_escape_state(e, field_es);
1427
es_changed = true;
1428
}
1429
if ((e->escape_state() < field_es) &&
1430
e->is_Field() && ptn->is_JavaObject() &&
1431
e->as_Field()->is_oop()) {
1432
// Change escape state of referenced fields.
1433
set_escape_state(e, field_es);
1434
es_changed = true;
1435
} else if (e->escape_state() < es) {
1436
set_escape_state(e, es);
1437
es_changed = true;
1438
}
1439
if (es_changed) {
1440
escape_worklist.push(e);
1441
}
1442
}
1443
}
1444
}
1445
// Remove escaped objects from non_escaped list.
1446
for (int next = non_escaped_allocs_worklist.length()-1; next >= 0 ; --next) {
1447
JavaObjectNode* ptn = non_escaped_allocs_worklist.at(next);
1448
if (ptn->escape_state() >= PointsToNode::GlobalEscape) {
1449
non_escaped_allocs_worklist.delete_at(next);
1450
}
1451
if (ptn->escape_state() == PointsToNode::NoEscape) {
1452
// Find fields in non-escaped allocations which have unknown value.
1453
find_init_values_phantom(ptn);
1454
}
1455
}
1456
return (non_escaped_allocs_worklist.length() > 0);
1457
}
1458
1459
// Add all references to JavaObject node by walking over all uses.
1460
int ConnectionGraph::add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist) {
1461
int new_edges = 0;
1462
if (populate_worklist) {
1463
// Populate _worklist by uses of jobj's uses.
1464
for (UseIterator i(jobj); i.has_next(); i.next()) {
1465
PointsToNode* use = i.get();
1466
if (use->is_Arraycopy()) {
1467
continue;
1468
}
1469
add_uses_to_worklist(use);
1470
if (use->is_Field() && use->as_Field()->is_oop()) {
1471
// Put on worklist all field's uses (loads) and
1472
// related field nodes (same base and offset).
1473
add_field_uses_to_worklist(use->as_Field());
1474
}
1475
}
1476
}
1477
for (int l = 0; l < _worklist.length(); l++) {
1478
PointsToNode* use = _worklist.at(l);
1479
if (PointsToNode::is_base_use(use)) {
1480
// Add reference from jobj to field and from field to jobj (field's base).
1481
use = PointsToNode::get_use_node(use)->as_Field();
1482
if (add_base(use->as_Field(), jobj)) {
1483
new_edges++;
1484
}
1485
continue;
1486
}
1487
assert(!use->is_JavaObject(), "sanity");
1488
if (use->is_Arraycopy()) {
1489
if (jobj == null_obj) { // NULL object does not have field edges
1490
continue;
1491
}
1492
// Added edge from Arraycopy node to arraycopy's source java object
1493
if (add_edge(use, jobj)) {
1494
jobj->set_arraycopy_src();
1495
new_edges++;
1496
}
1497
// and stop here.
1498
continue;
1499
}
1500
if (!add_edge(use, jobj)) {
1501
continue; // No new edge added, there was such edge already.
1502
}
1503
new_edges++;
1504
if (use->is_LocalVar()) {
1505
add_uses_to_worklist(use);
1506
if (use->arraycopy_dst()) {
1507
for (EdgeIterator i(use); i.has_next(); i.next()) {
1508
PointsToNode* e = i.get();
1509
if (e->is_Arraycopy()) {
1510
if (jobj == null_obj) { // NULL object does not have field edges
1511
continue;
1512
}
1513
// Add edge from arraycopy's destination java object to Arraycopy node.
1514
if (add_edge(jobj, e)) {
1515
new_edges++;
1516
jobj->set_arraycopy_dst();
1517
}
1518
}
1519
}
1520
}
1521
} else {
1522
// Added new edge to stored in field values.
1523
// Put on worklist all field's uses (loads) and
1524
// related field nodes (same base and offset).
1525
add_field_uses_to_worklist(use->as_Field());
1526
}
1527
}
1528
_worklist.clear();
1529
_in_worklist.reset();
1530
return new_edges;
1531
}
1532
1533
// Put on worklist all related field nodes.
1534
void ConnectionGraph::add_field_uses_to_worklist(FieldNode* field) {
1535
assert(field->is_oop(), "sanity");
1536
int offset = field->offset();
1537
add_uses_to_worklist(field);
1538
// Loop over all bases of this field and push on worklist Field nodes
1539
// with the same offset and base (since they may reference the same field).
1540
for (BaseIterator i(field); i.has_next(); i.next()) {
1541
PointsToNode* base = i.get();
1542
add_fields_to_worklist(field, base);
1543
// Check if the base was source object of arraycopy and go over arraycopy's
1544
// destination objects since values stored to a field of source object are
1545
// accessable by uses (loads) of fields of destination objects.
1546
if (base->arraycopy_src()) {
1547
for (UseIterator j(base); j.has_next(); j.next()) {
1548
PointsToNode* arycp = j.get();
1549
if (arycp->is_Arraycopy()) {
1550
for (UseIterator k(arycp); k.has_next(); k.next()) {
1551
PointsToNode* abase = k.get();
1552
if (abase->arraycopy_dst() && abase != base) {
1553
// Look for the same arraycopy reference.
1554
add_fields_to_worklist(field, abase);
1555
}
1556
}
1557
}
1558
}
1559
}
1560
}
1561
}
1562
1563
// Put on worklist all related field nodes.
1564
void ConnectionGraph::add_fields_to_worklist(FieldNode* field, PointsToNode* base) {
1565
int offset = field->offset();
1566
if (base->is_LocalVar()) {
1567
for (UseIterator j(base); j.has_next(); j.next()) {
1568
PointsToNode* f = j.get();
1569
if (PointsToNode::is_base_use(f)) { // Field
1570
f = PointsToNode::get_use_node(f);
1571
if (f == field || !f->as_Field()->is_oop()) {
1572
continue;
1573
}
1574
int offs = f->as_Field()->offset();
1575
if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
1576
add_to_worklist(f);
1577
}
1578
}
1579
}
1580
} else {
1581
assert(base->is_JavaObject(), "sanity");
1582
if (// Skip phantom_object since it is only used to indicate that
1583
// this field's content globally escapes.
1584
(base != phantom_obj) &&
1585
// NULL object node does not have fields.
1586
(base != null_obj)) {
1587
for (EdgeIterator i(base); i.has_next(); i.next()) {
1588
PointsToNode* f = i.get();
1589
// Skip arraycopy edge since store to destination object field
1590
// does not update value in source object field.
1591
if (f->is_Arraycopy()) {
1592
assert(base->arraycopy_dst(), "sanity");
1593
continue;
1594
}
1595
if (f == field || !f->as_Field()->is_oop()) {
1596
continue;
1597
}
1598
int offs = f->as_Field()->offset();
1599
if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
1600
add_to_worklist(f);
1601
}
1602
}
1603
}
1604
}
1605
}
1606
1607
// Find fields which have unknown value.
1608
int ConnectionGraph::find_field_value(FieldNode* field) {
1609
// Escaped fields should have init value already.
1610
assert(field->escape_state() == PointsToNode::NoEscape, "sanity");
1611
int new_edges = 0;
1612
for (BaseIterator i(field); i.has_next(); i.next()) {
1613
PointsToNode* base = i.get();
1614
if (base->is_JavaObject()) {
1615
// Skip Allocate's fields which will be processed later.
1616
if (base->ideal_node()->is_Allocate()) {
1617
return 0;
1618
}
1619
assert(base == null_obj, "only NULL ptr base expected here");
1620
}
1621
}
1622
if (add_edge(field, phantom_obj)) {
1623
// New edge was added
1624
new_edges++;
1625
add_field_uses_to_worklist(field);
1626
}
1627
return new_edges;
1628
}
1629
1630
// Find fields initializing values for allocations.
1631
int ConnectionGraph::find_init_values_phantom(JavaObjectNode* pta) {
1632
assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1633
Node* alloc = pta->ideal_node();
1634
1635
// Do nothing for Allocate nodes since its fields values are
1636
// "known" unless they are initialized by arraycopy/clone.
1637
if (alloc->is_Allocate() && !pta->arraycopy_dst()) {
1638
return 0;
1639
}
1640
assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");
1641
#ifdef ASSERT
1642
if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == NULL) {
1643
const char* name = alloc->as_CallStaticJava()->_name;
1644
assert(strncmp(name, "_multianewarray", 15) == 0, "sanity");
1645
}
1646
#endif
1647
// Non-escaped allocation returned from Java or runtime call have unknown values in fields.
1648
int new_edges = 0;
1649
for (EdgeIterator i(pta); i.has_next(); i.next()) {
1650
PointsToNode* field = i.get();
1651
if (field->is_Field() && field->as_Field()->is_oop()) {
1652
if (add_edge(field, phantom_obj)) {
1653
// New edge was added
1654
new_edges++;
1655
add_field_uses_to_worklist(field->as_Field());
1656
}
1657
}
1658
}
1659
return new_edges;
1660
}
1661
1662
// Find fields initializing values for allocations.
1663
int ConnectionGraph::find_init_values_null(JavaObjectNode* pta, PhaseTransform* phase) {
1664
assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1665
Node* alloc = pta->ideal_node();
1666
// Do nothing for Call nodes since its fields values are unknown.
1667
if (!alloc->is_Allocate()) {
1668
return 0;
1669
}
1670
InitializeNode* ini = alloc->as_Allocate()->initialization();
1671
bool visited_bottom_offset = false;
1672
GrowableArray<int> offsets_worklist;
1673
int new_edges = 0;
1674
1675
// Check if an oop field's initializing value is recorded and add
1676
// a corresponding NULL if field's value if it is not recorded.
1677
// Connection Graph does not record a default initialization by NULL
1678
// captured by Initialize node.
1679
//
1680
for (EdgeIterator i(pta); i.has_next(); i.next()) {
1681
PointsToNode* field = i.get(); // Field (AddP)
1682
if (!field->is_Field() || !field->as_Field()->is_oop()) {
1683
continue; // Not oop field
1684
}
1685
int offset = field->as_Field()->offset();
1686
if (offset == Type::OffsetBot) {
1687
if (!visited_bottom_offset) {
1688
// OffsetBot is used to reference array's element,
1689
// always add reference to NULL to all Field nodes since we don't
1690
// known which element is referenced.
1691
if (add_edge(field, null_obj)) {
1692
// New edge was added
1693
new_edges++;
1694
add_field_uses_to_worklist(field->as_Field());
1695
visited_bottom_offset = true;
1696
}
1697
}
1698
} else {
1699
// Check only oop fields.
1700
const Type* adr_type = field->ideal_node()->as_AddP()->bottom_type();
1701
if (adr_type->isa_rawptr()) {
1702
#ifdef ASSERT
1703
// Raw pointers are used for initializing stores so skip it
1704
// since it should be recorded already
1705
Node* base = get_addp_base(field->ideal_node());
1706
assert(adr_type->isa_rawptr() && is_captured_store_address(field->ideal_node()), "unexpected pointer type");
1707
#endif
1708
continue;
1709
}
1710
if (!offsets_worklist.contains(offset)) {
1711
offsets_worklist.append(offset);
1712
Node* value = NULL;
1713
if (ini != NULL) {
1714
// StoreP::memory_type() == T_ADDRESS
1715
BasicType ft = UseCompressedOops ? T_NARROWOOP : T_ADDRESS;
1716
Node* store = ini->find_captured_store(offset, type2aelembytes(ft, true), phase);
1717
// Make sure initializing store has the same type as this AddP.
1718
// This AddP may reference non existing field because it is on a
1719
// dead branch of bimorphic call which is not eliminated yet.
1720
if (store != NULL && store->is_Store() &&
1721
store->as_Store()->memory_type() == ft) {
1722
value = store->in(MemNode::ValueIn);
1723
#ifdef ASSERT
1724
if (VerifyConnectionGraph) {
1725
// Verify that AddP already points to all objects the value points to.
1726
PointsToNode* val = ptnode_adr(value->_idx);
1727
assert((val != NULL), "should be processed already");
1728
PointsToNode* missed_obj = NULL;
1729
if (val->is_JavaObject()) {
1730
if (!field->points_to(val->as_JavaObject())) {
1731
missed_obj = val;
1732
}
1733
} else {
1734
if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1735
tty->print_cr("----------init store has invalid value -----");
1736
store->dump();
1737
val->dump();
1738
assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1739
}
1740
for (EdgeIterator j(val); j.has_next(); j.next()) {
1741
PointsToNode* obj = j.get();
1742
if (obj->is_JavaObject()) {
1743
if (!field->points_to(obj->as_JavaObject())) {
1744
missed_obj = obj;
1745
break;
1746
}
1747
}
1748
}
1749
}
1750
if (missed_obj != NULL) {
1751
tty->print_cr("----------field---------------------------------");
1752
field->dump();
1753
tty->print_cr("----------missed referernce to object-----------");
1754
missed_obj->dump();
1755
tty->print_cr("----------object referernced by init store -----");
1756
store->dump();
1757
val->dump();
1758
assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1759
}
1760
}
1761
#endif
1762
} else {
1763
// There could be initializing stores which follow allocation.
1764
// For example, a volatile field store is not collected
1765
// by Initialize node.
1766
//
1767
// Need to check for dependent loads to separate such stores from
1768
// stores which follow loads. For now, add initial value NULL so
1769
// that compare pointers optimization works correctly.
1770
}
1771
}
1772
if (value == NULL) {
1773
// A field's initializing value was not recorded. Add NULL.
1774
if (add_edge(field, null_obj)) {
1775
// New edge was added
1776
new_edges++;
1777
add_field_uses_to_worklist(field->as_Field());
1778
}
1779
}
1780
}
1781
}
1782
}
1783
return new_edges;
1784
}
1785
1786
// Adjust scalar_replaceable state after Connection Graph is built.
1787
void ConnectionGraph::adjust_scalar_replaceable_state(JavaObjectNode* jobj) {
1788
// Search for non-escaping objects which are not scalar replaceable
1789
// and mark them to propagate the state to referenced objects.
1790
1791
for (UseIterator i(jobj); i.has_next(); i.next()) {
1792
PointsToNode* use = i.get();
1793
if (use->is_Arraycopy()) {
1794
continue;
1795
}
1796
if (use->is_Field()) {
1797
FieldNode* field = use->as_Field();
1798
assert(field->is_oop() && field->scalar_replaceable(), "sanity");
1799
// 1. An object is not scalar replaceable if the field into which it is
1800
// stored has unknown offset (stored into unknown element of an array).
1801
if (field->offset() == Type::OffsetBot) {
1802
jobj->set_scalar_replaceable(false);
1803
return;
1804
}
1805
// 2. An object is not scalar replaceable if the field into which it is
1806
// stored has multiple bases one of which is null.
1807
if (field->base_count() > 1) {
1808
for (BaseIterator i(field); i.has_next(); i.next()) {
1809
PointsToNode* base = i.get();
1810
if (base == null_obj) {
1811
jobj->set_scalar_replaceable(false);
1812
return;
1813
}
1814
}
1815
}
1816
}
1817
assert(use->is_Field() || use->is_LocalVar(), "sanity");
1818
// 3. An object is not scalar replaceable if it is merged with other objects.
1819
for (EdgeIterator j(use); j.has_next(); j.next()) {
1820
PointsToNode* ptn = j.get();
1821
if (ptn->is_JavaObject() && ptn != jobj) {
1822
// Mark all objects.
1823
jobj->set_scalar_replaceable(false);
1824
ptn->set_scalar_replaceable(false);
1825
}
1826
}
1827
if (!jobj->scalar_replaceable()) {
1828
return;
1829
}
1830
}
1831
1832
for (EdgeIterator j(jobj); j.has_next(); j.next()) {
1833
if (j.get()->is_Arraycopy()) {
1834
continue;
1835
}
1836
1837
// Non-escaping object node should point only to field nodes.
1838
FieldNode* field = j.get()->as_Field();
1839
int offset = field->as_Field()->offset();
1840
1841
// 4. An object is not scalar replaceable if it has a field with unknown
1842
// offset (array's element is accessed in loop).
1843
if (offset == Type::OffsetBot) {
1844
jobj->set_scalar_replaceable(false);
1845
return;
1846
}
1847
// 5. Currently an object is not scalar replaceable if a LoadStore node
1848
// access its field since the field value is unknown after it.
1849
//
1850
Node* n = field->ideal_node();
1851
1852
// Test for an unsafe access that was parsed as maybe off heap
1853
// (with a CheckCastPP to raw memory).
1854
assert(n->is_AddP(), "expect an address computation");
1855
if (n->in(AddPNode::Base)->is_top() &&
1856
n->in(AddPNode::Address)->Opcode() == Op_CheckCastPP) {
1857
assert(n->in(AddPNode::Address)->bottom_type()->isa_rawptr(), "raw address so raw cast expected");
1858
assert(_igvn->type(n->in(AddPNode::Address)->in(1))->isa_oopptr(), "cast pattern at unsafe access expected");
1859
jobj->set_scalar_replaceable(false);
1860
return;
1861
}
1862
1863
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1864
Node* u = n->fast_out(i);
1865
if (u->is_LoadStore() || (u->is_Mem() && u->as_Mem()->is_mismatched_access())) {
1866
jobj->set_scalar_replaceable(false);
1867
return;
1868
}
1869
}
1870
1871
// 6. Or the address may point to more then one object. This may produce
1872
// the false positive result (set not scalar replaceable)
1873
// since the flow-insensitive escape analysis can't separate
1874
// the case when stores overwrite the field's value from the case
1875
// when stores happened on different control branches.
1876
//
1877
// Note: it will disable scalar replacement in some cases:
1878
//
1879
// Point p[] = new Point[1];
1880
// p[0] = new Point(); // Will be not scalar replaced
1881
//
1882
// but it will save us from incorrect optimizations in next cases:
1883
//
1884
// Point p[] = new Point[1];
1885
// if ( x ) p[0] = new Point(); // Will be not scalar replaced
1886
//
1887
if (field->base_count() > 1) {
1888
for (BaseIterator i(field); i.has_next(); i.next()) {
1889
PointsToNode* base = i.get();
1890
// Don't take into account LocalVar nodes which
1891
// may point to only one object which should be also
1892
// this field's base by now.
1893
if (base->is_JavaObject() && base != jobj) {
1894
// Mark all bases.
1895
jobj->set_scalar_replaceable(false);
1896
base->set_scalar_replaceable(false);
1897
}
1898
}
1899
}
1900
}
1901
}
1902
1903
#ifdef ASSERT
1904
void ConnectionGraph::verify_connection_graph(
1905
GrowableArray<PointsToNode*>& ptnodes_worklist,
1906
GrowableArray<JavaObjectNode*>& non_escaped_allocs_worklist,
1907
GrowableArray<JavaObjectNode*>& java_objects_worklist,
1908
GrowableArray<Node*>& addp_worklist) {
1909
// Verify that graph is complete - no new edges could be added.
1910
int java_objects_length = java_objects_worklist.length();
1911
int non_escaped_length = non_escaped_allocs_worklist.length();
1912
int new_edges = 0;
1913
for (int next = 0; next < java_objects_length; ++next) {
1914
JavaObjectNode* ptn = java_objects_worklist.at(next);
1915
new_edges += add_java_object_edges(ptn, true);
1916
}
1917
assert(new_edges == 0, "graph was not complete");
1918
// Verify that escape state is final.
1919
int length = non_escaped_allocs_worklist.length();
1920
find_non_escaped_objects(ptnodes_worklist, non_escaped_allocs_worklist);
1921
assert((non_escaped_length == non_escaped_allocs_worklist.length()) &&
1922
(non_escaped_length == length) &&
1923
(_worklist.length() == 0), "escape state was not final");
1924
1925
// Verify fields information.
1926
int addp_length = addp_worklist.length();
1927
for (int next = 0; next < addp_length; ++next ) {
1928
Node* n = addp_worklist.at(next);
1929
FieldNode* field = ptnode_adr(n->_idx)->as_Field();
1930
if (field->is_oop()) {
1931
// Verify that field has all bases
1932
Node* base = get_addp_base(n);
1933
PointsToNode* ptn = ptnode_adr(base->_idx);
1934
if (ptn->is_JavaObject()) {
1935
assert(field->has_base(ptn->as_JavaObject()), "sanity");
1936
} else {
1937
assert(ptn->is_LocalVar(), "sanity");
1938
for (EdgeIterator i(ptn); i.has_next(); i.next()) {
1939
PointsToNode* e = i.get();
1940
if (e->is_JavaObject()) {
1941
assert(field->has_base(e->as_JavaObject()), "sanity");
1942
}
1943
}
1944
}
1945
// Verify that all fields have initializing values.
1946
if (field->edge_count() == 0) {
1947
tty->print_cr("----------field does not have references----------");
1948
field->dump();
1949
for (BaseIterator i(field); i.has_next(); i.next()) {
1950
PointsToNode* base = i.get();
1951
tty->print_cr("----------field has next base---------------------");
1952
base->dump();
1953
if (base->is_JavaObject() && (base != phantom_obj) && (base != null_obj)) {
1954
tty->print_cr("----------base has fields-------------------------");
1955
for (EdgeIterator j(base); j.has_next(); j.next()) {
1956
j.get()->dump();
1957
}
1958
tty->print_cr("----------base has references---------------------");
1959
for (UseIterator j(base); j.has_next(); j.next()) {
1960
j.get()->dump();
1961
}
1962
}
1963
}
1964
for (UseIterator i(field); i.has_next(); i.next()) {
1965
i.get()->dump();
1966
}
1967
assert(field->edge_count() > 0, "sanity");
1968
}
1969
}
1970
}
1971
}
1972
#endif
1973
1974
// Optimize ideal graph.
1975
void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
1976
GrowableArray<Node*>& storestore_worklist) {
1977
Compile* C = _compile;
1978
PhaseIterGVN* igvn = _igvn;
1979
if (EliminateLocks) {
1980
// Mark locks before changing ideal graph.
1981
int cnt = C->macro_count();
1982
for (int i = 0; i < cnt; i++) {
1983
Node *n = C->macro_node(i);
1984
if (n->is_AbstractLock()) { // Lock and Unlock nodes
1985
AbstractLockNode* alock = n->as_AbstractLock();
1986
if (!alock->is_non_esc_obj()) {
1987
if (not_global_escape(alock->obj_node())) {
1988
assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
1989
// The lock could be marked eliminated by lock coarsening
1990
// code during first IGVN before EA. Replace coarsened flag
1991
// to eliminate all associated locks/unlocks.
1992
#ifdef ASSERT
1993
alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
1994
#endif
1995
alock->set_non_esc_obj();
1996
}
1997
}
1998
}
1999
}
2000
}
2001
2002
if (OptimizePtrCompare) {
2003
for (int i = 0; i < ptr_cmp_worklist.length(); i++) {
2004
Node *n = ptr_cmp_worklist.at(i);
2005
const TypeInt* tcmp = optimize_ptr_compare(n);
2006
if (tcmp->singleton()) {
2007
Node* cmp = igvn->makecon(tcmp);
2008
#ifndef PRODUCT
2009
if (PrintOptimizePtrCompare) {
2010
tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (tcmp == TypeInt::CC_EQ ? "EQ" : "NotEQ"));
2011
if (Verbose) {
2012
n->dump(1);
2013
}
2014
}
2015
#endif
2016
igvn->replace_node(n, cmp);
2017
}
2018
}
2019
}
2020
2021
// For MemBarStoreStore nodes added in library_call.cpp, check
2022
// escape status of associated AllocateNode and optimize out
2023
// MemBarStoreStore node if the allocated object never escapes.
2024
for (int i = 0; i < storestore_worklist.length(); i++) {
2025
Node* storestore = storestore_worklist.at(i);
2026
assert(storestore->is_MemBarStoreStore(), "");
2027
Node* alloc = storestore->in(MemBarNode::Precedent)->in(0);
2028
if (alloc->is_Allocate() && not_global_escape(alloc)) {
2029
MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
2030
mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
2031
mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
2032
igvn->register_new_node_with_optimizer(mb);
2033
igvn->replace_node(storestore, mb);
2034
}
2035
}
2036
}
2037
2038
// Optimize objects compare.
2039
const TypeInt* ConnectionGraph::optimize_ptr_compare(Node* n) {
2040
assert(OptimizePtrCompare, "sanity");
2041
const TypeInt* EQ = TypeInt::CC_EQ; // [0] == ZERO
2042
const TypeInt* NE = TypeInt::CC_GT; // [1] == ONE
2043
const TypeInt* UNKNOWN = TypeInt::CC; // [-1, 0,1]
2044
2045
PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
2046
PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
2047
JavaObjectNode* jobj1 = unique_java_object(n->in(1));
2048
JavaObjectNode* jobj2 = unique_java_object(n->in(2));
2049
assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
2050
assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
2051
2052
// Check simple cases first.
2053
if (jobj1 != NULL) {
2054
if (jobj1->escape_state() == PointsToNode::NoEscape) {
2055
if (jobj1 == jobj2) {
2056
// Comparing the same not escaping object.
2057
return EQ;
2058
}
2059
Node* obj = jobj1->ideal_node();
2060
// Comparing not escaping allocation.
2061
if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
2062
!ptn2->points_to(jobj1)) {
2063
return NE; // This includes nullness check.
2064
}
2065
}
2066
}
2067
if (jobj2 != NULL) {
2068
if (jobj2->escape_state() == PointsToNode::NoEscape) {
2069
Node* obj = jobj2->ideal_node();
2070
// Comparing not escaping allocation.
2071
if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
2072
!ptn1->points_to(jobj2)) {
2073
return NE; // This includes nullness check.
2074
}
2075
}
2076
}
2077
if (jobj1 != NULL && jobj1 != phantom_obj &&
2078
jobj2 != NULL && jobj2 != phantom_obj &&
2079
jobj1->ideal_node()->is_Con() &&
2080
jobj2->ideal_node()->is_Con()) {
2081
// Klass or String constants compare. Need to be careful with
2082
// compressed pointers - compare types of ConN and ConP instead of nodes.
2083
const Type* t1 = jobj1->ideal_node()->get_ptr_type();
2084
const Type* t2 = jobj2->ideal_node()->get_ptr_type();
2085
if (t1->make_ptr() == t2->make_ptr()) {
2086
return EQ;
2087
} else {
2088
return NE;
2089
}
2090
}
2091
if (ptn1->meet(ptn2)) {
2092
return UNKNOWN; // Sets are not disjoint
2093
}
2094
2095
// Sets are disjoint.
2096
bool set1_has_unknown_ptr = ptn1->points_to(phantom_obj);
2097
bool set2_has_unknown_ptr = ptn2->points_to(phantom_obj);
2098
bool set1_has_null_ptr = ptn1->points_to(null_obj);
2099
bool set2_has_null_ptr = ptn2->points_to(null_obj);
2100
if ((set1_has_unknown_ptr && set2_has_null_ptr) ||
2101
(set2_has_unknown_ptr && set1_has_null_ptr)) {
2102
// Check nullness of unknown object.
2103
return UNKNOWN;
2104
}
2105
2106
// Disjointness by itself is not sufficient since
2107
// alias analysis is not complete for escaped objects.
2108
// Disjoint sets are definitely unrelated only when
2109
// at least one set has only not escaping allocations.
2110
if (!set1_has_unknown_ptr && !set1_has_null_ptr) {
2111
if (ptn1->non_escaping_allocation()) {
2112
return NE;
2113
}
2114
}
2115
if (!set2_has_unknown_ptr && !set2_has_null_ptr) {
2116
if (ptn2->non_escaping_allocation()) {
2117
return NE;
2118
}
2119
}
2120
return UNKNOWN;
2121
}
2122
2123
// Connection Graph construction functions.
2124
2125
void ConnectionGraph::add_local_var(Node *n, PointsToNode::EscapeState es) {
2126
PointsToNode* ptadr = _nodes.at(n->_idx);
2127
if (ptadr != NULL) {
2128
assert(ptadr->is_LocalVar() && ptadr->ideal_node() == n, "sanity");
2129
return;
2130
}
2131
Compile* C = _compile;
2132
ptadr = new (C->comp_arena()) LocalVarNode(this, n, es);
2133
map_ideal_node(n, ptadr);
2134
}
2135
2136
void ConnectionGraph::add_java_object(Node *n, PointsToNode::EscapeState es) {
2137
PointsToNode* ptadr = _nodes.at(n->_idx);
2138
if (ptadr != NULL) {
2139
assert(ptadr->is_JavaObject() && ptadr->ideal_node() == n, "sanity");
2140
return;
2141
}
2142
Compile* C = _compile;
2143
ptadr = new (C->comp_arena()) JavaObjectNode(this, n, es);
2144
map_ideal_node(n, ptadr);
2145
}
2146
2147
void ConnectionGraph::add_field(Node *n, PointsToNode::EscapeState es, int offset) {
2148
PointsToNode* ptadr = _nodes.at(n->_idx);
2149
if (ptadr != NULL) {
2150
assert(ptadr->is_Field() && ptadr->ideal_node() == n, "sanity");
2151
return;
2152
}
2153
bool unsafe = false;
2154
bool is_oop = is_oop_field(n, offset, &unsafe);
2155
if (unsafe) {
2156
es = PointsToNode::GlobalEscape;
2157
}
2158
Compile* C = _compile;
2159
FieldNode* field = new (C->comp_arena()) FieldNode(this, n, es, offset, is_oop);
2160
map_ideal_node(n, field);
2161
}
2162
2163
void ConnectionGraph::add_arraycopy(Node *n, PointsToNode::EscapeState es,
2164
PointsToNode* src, PointsToNode* dst) {
2165
assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
2166
assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
2167
PointsToNode* ptadr = _nodes.at(n->_idx);
2168
if (ptadr != NULL) {
2169
assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
2170
return;
2171
}
2172
Compile* C = _compile;
2173
ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
2174
map_ideal_node(n, ptadr);
2175
// Add edge from arraycopy node to source object.
2176
(void)add_edge(ptadr, src);
2177
src->set_arraycopy_src();
2178
// Add edge from destination object to arraycopy node.
2179
(void)add_edge(dst, ptadr);
2180
dst->set_arraycopy_dst();
2181
}
2182
2183
bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
2184
const Type* adr_type = n->as_AddP()->bottom_type();
2185
BasicType bt = T_INT;
2186
if (offset == Type::OffsetBot) {
2187
// Check only oop fields.
2188
if (!adr_type->isa_aryptr() ||
2189
(adr_type->isa_aryptr()->klass() == NULL) ||
2190
adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
2191
// OffsetBot is used to reference array's element. Ignore first AddP.
2192
if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
2193
bt = T_OBJECT;
2194
}
2195
}
2196
} else if (offset != oopDesc::klass_offset_in_bytes()) {
2197
if (adr_type->isa_instptr()) {
2198
ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
2199
if (field != NULL) {
2200
bt = field->layout_type();
2201
} else {
2202
// Check for unsafe oop field access
2203
if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2204
n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2205
n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2206
BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2207
bt = T_OBJECT;
2208
(*unsafe) = true;
2209
}
2210
}
2211
} else if (adr_type->isa_aryptr()) {
2212
if (offset == arrayOopDesc::length_offset_in_bytes()) {
2213
// Ignore array length load.
2214
} else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
2215
// Ignore first AddP.
2216
} else {
2217
const Type* elemtype = adr_type->isa_aryptr()->elem();
2218
bt = elemtype->array_element_basic_type();
2219
}
2220
} else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
2221
// Allocation initialization, ThreadLocal field access, unsafe access
2222
if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2223
n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2224
n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2225
BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2226
bt = T_OBJECT;
2227
}
2228
}
2229
}
2230
// Note: T_NARROWOOP is not classed as a real reference type
2231
return (is_reference_type(bt) || bt == T_NARROWOOP);
2232
}
2233
2234
// Returns unique pointed java object or NULL.
2235
JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
2236
assert(!_collecting, "should not call when constructed graph");
2237
// If the node was created after the escape computation we can't answer.
2238
uint idx = n->_idx;
2239
if (idx >= nodes_size()) {
2240
return NULL;
2241
}
2242
PointsToNode* ptn = ptnode_adr(idx);
2243
if (ptn == NULL) {
2244
return NULL;
2245
}
2246
if (ptn->is_JavaObject()) {
2247
return ptn->as_JavaObject();
2248
}
2249
assert(ptn->is_LocalVar(), "sanity");
2250
// Check all java objects it points to.
2251
JavaObjectNode* jobj = NULL;
2252
for (EdgeIterator i(ptn); i.has_next(); i.next()) {
2253
PointsToNode* e = i.get();
2254
if (e->is_JavaObject()) {
2255
if (jobj == NULL) {
2256
jobj = e->as_JavaObject();
2257
} else if (jobj != e) {
2258
return NULL;
2259
}
2260
}
2261
}
2262
return jobj;
2263
}
2264
2265
// Return true if this node points only to non-escaping allocations.
2266
bool PointsToNode::non_escaping_allocation() {
2267
if (is_JavaObject()) {
2268
Node* n = ideal_node();
2269
if (n->is_Allocate() || n->is_CallStaticJava()) {
2270
return (escape_state() == PointsToNode::NoEscape);
2271
} else {
2272
return false;
2273
}
2274
}
2275
assert(is_LocalVar(), "sanity");
2276
// Check all java objects it points to.
2277
for (EdgeIterator i(this); i.has_next(); i.next()) {
2278
PointsToNode* e = i.get();
2279
if (e->is_JavaObject()) {
2280
Node* n = e->ideal_node();
2281
if ((e->escape_state() != PointsToNode::NoEscape) ||
2282
!(n->is_Allocate() || n->is_CallStaticJava())) {
2283
return false;
2284
}
2285
}
2286
}
2287
return true;
2288
}
2289
2290
// Return true if we know the node does not escape globally.
2291
bool ConnectionGraph::not_global_escape(Node *n) {
2292
assert(!_collecting, "should not call during graph construction");
2293
// If the node was created after the escape computation we can't answer.
2294
uint idx = n->_idx;
2295
if (idx >= nodes_size()) {
2296
return false;
2297
}
2298
PointsToNode* ptn = ptnode_adr(idx);
2299
if (ptn == NULL) {
2300
return false; // not in congraph (e.g. ConI)
2301
}
2302
PointsToNode::EscapeState es = ptn->escape_state();
2303
// If we have already computed a value, return it.
2304
if (es >= PointsToNode::GlobalEscape) {
2305
return false;
2306
}
2307
if (ptn->is_JavaObject()) {
2308
return true; // (es < PointsToNode::GlobalEscape);
2309
}
2310
assert(ptn->is_LocalVar(), "sanity");
2311
// Check all java objects it points to.
2312
for (EdgeIterator i(ptn); i.has_next(); i.next()) {
2313
if (i.get()->escape_state() >= PointsToNode::GlobalEscape) {
2314
return false;
2315
}
2316
}
2317
return true;
2318
}
2319
2320
2321
// Helper functions
2322
2323
// Return true if this node points to specified node or nodes it points to.
2324
bool PointsToNode::points_to(JavaObjectNode* ptn) const {
2325
if (is_JavaObject()) {
2326
return (this == ptn);
2327
}
2328
assert(is_LocalVar() || is_Field(), "sanity");
2329
for (EdgeIterator i(this); i.has_next(); i.next()) {
2330
if (i.get() == ptn) {
2331
return true;
2332
}
2333
}
2334
return false;
2335
}
2336
2337
// Return true if one node points to an other.
2338
bool PointsToNode::meet(PointsToNode* ptn) {
2339
if (this == ptn) {
2340
return true;
2341
} else if (ptn->is_JavaObject()) {
2342
return this->points_to(ptn->as_JavaObject());
2343
} else if (this->is_JavaObject()) {
2344
return ptn->points_to(this->as_JavaObject());
2345
}
2346
assert(this->is_LocalVar() && ptn->is_LocalVar(), "sanity");
2347
int ptn_count = ptn->edge_count();
2348
for (EdgeIterator i(this); i.has_next(); i.next()) {
2349
PointsToNode* this_e = i.get();
2350
for (int j = 0; j < ptn_count; j++) {
2351
if (this_e == ptn->edge(j)) {
2352
return true;
2353
}
2354
}
2355
}
2356
return false;
2357
}
2358
2359
#ifdef ASSERT
2360
// Return true if bases point to this java object.
2361
bool FieldNode::has_base(JavaObjectNode* jobj) const {
2362
for (BaseIterator i(this); i.has_next(); i.next()) {
2363
if (i.get() == jobj) {
2364
return true;
2365
}
2366
}
2367
return false;
2368
}
2369
#endif
2370
2371
bool ConnectionGraph::is_captured_store_address(Node* addp) {
2372
// Handle simple case first.
2373
assert(_igvn->type(addp)->isa_oopptr() == NULL, "should be raw access");
2374
if (addp->in(AddPNode::Address)->is_Proj() && addp->in(AddPNode::Address)->in(0)->is_Allocate()) {
2375
return true;
2376
} else if (addp->in(AddPNode::Address)->is_Phi()) {
2377
for (DUIterator_Fast imax, i = addp->fast_outs(imax); i < imax; i++) {
2378
Node* addp_use = addp->fast_out(i);
2379
if (addp_use->is_Store()) {
2380
for (DUIterator_Fast jmax, j = addp_use->fast_outs(jmax); j < jmax; j++) {
2381
if (addp_use->fast_out(j)->is_Initialize()) {
2382
return true;
2383
}
2384
}
2385
}
2386
}
2387
}
2388
return false;
2389
}
2390
2391
int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2392
const Type *adr_type = phase->type(adr);
2393
if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2394
// We are computing a raw address for a store captured by an Initialize
2395
// compute an appropriate address type. AddP cases #3 and #5 (see below).
2396
int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2397
assert(offs != Type::OffsetBot ||
2398
adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2399
"offset must be a constant or it is initialization of array");
2400
return offs;
2401
}
2402
const TypePtr *t_ptr = adr_type->isa_ptr();
2403
assert(t_ptr != NULL, "must be a pointer type");
2404
return t_ptr->offset();
2405
}
2406
2407
Node* ConnectionGraph::get_addp_base(Node *addp) {
2408
assert(addp->is_AddP(), "must be AddP");
2409
//
2410
// AddP cases for Base and Address inputs:
2411
// case #1. Direct object's field reference:
2412
// Allocate
2413
// |
2414
// Proj #5 ( oop result )
2415
// |
2416
// CheckCastPP (cast to instance type)
2417
// | |
2418
// AddP ( base == address )
2419
//
2420
// case #2. Indirect object's field reference:
2421
// Phi
2422
// |
2423
// CastPP (cast to instance type)
2424
// | |
2425
// AddP ( base == address )
2426
//
2427
// case #3. Raw object's field reference for Initialize node:
2428
// Allocate
2429
// |
2430
// Proj #5 ( oop result )
2431
// top |
2432
// \ |
2433
// AddP ( base == top )
2434
//
2435
// case #4. Array's element reference:
2436
// {CheckCastPP | CastPP}
2437
// | | |
2438
// | AddP ( array's element offset )
2439
// | |
2440
// AddP ( array's offset )
2441
//
2442
// case #5. Raw object's field reference for arraycopy stub call:
2443
// The inline_native_clone() case when the arraycopy stub is called
2444
// after the allocation before Initialize and CheckCastPP nodes.
2445
// Allocate
2446
// |
2447
// Proj #5 ( oop result )
2448
// | |
2449
// AddP ( base == address )
2450
//
2451
// case #6. Constant Pool, ThreadLocal, CastX2P or
2452
// Raw object's field reference:
2453
// {ConP, ThreadLocal, CastX2P, raw Load}
2454
// top |
2455
// \ |
2456
// AddP ( base == top )
2457
//
2458
// case #7. Klass's field reference.
2459
// LoadKlass
2460
// | |
2461
// AddP ( base == address )
2462
//
2463
// case #8. narrow Klass's field reference.
2464
// LoadNKlass
2465
// |
2466
// DecodeN
2467
// | |
2468
// AddP ( base == address )
2469
//
2470
// case #9. Mixed unsafe access
2471
// {instance}
2472
// |
2473
// CheckCastPP (raw)
2474
// top |
2475
// \ |
2476
// AddP ( base == top )
2477
//
2478
Node *base = addp->in(AddPNode::Base);
2479
if (base->uncast()->is_top()) { // The AddP case #3 and #6 and #9.
2480
base = addp->in(AddPNode::Address);
2481
while (base->is_AddP()) {
2482
// Case #6 (unsafe access) may have several chained AddP nodes.
2483
assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
2484
base = base->in(AddPNode::Address);
2485
}
2486
if (base->Opcode() == Op_CheckCastPP &&
2487
base->bottom_type()->isa_rawptr() &&
2488
_igvn->type(base->in(1))->isa_oopptr()) {
2489
base = base->in(1); // Case #9
2490
} else {
2491
Node* uncast_base = base->uncast();
2492
int opcode = uncast_base->Opcode();
2493
assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
2494
opcode == Op_CastX2P || uncast_base->is_DecodeNarrowPtr() ||
2495
(uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_rawptr() != NULL)) ||
2496
is_captured_store_address(addp), "sanity");
2497
}
2498
}
2499
return base;
2500
}
2501
2502
Node* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
2503
assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
2504
Node* addp2 = addp->raw_out(0);
2505
if (addp->outcnt() == 1 && addp2->is_AddP() &&
2506
addp2->in(AddPNode::Base) == n &&
2507
addp2->in(AddPNode::Address) == addp) {
2508
assert(addp->in(AddPNode::Base) == n, "expecting the same base");
2509
//
2510
// Find array's offset to push it on worklist first and
2511
// as result process an array's element offset first (pushed second)
2512
// to avoid CastPP for the array's offset.
2513
// Otherwise the inserted CastPP (LocalVar) will point to what
2514
// the AddP (Field) points to. Which would be wrong since
2515
// the algorithm expects the CastPP has the same point as
2516
// as AddP's base CheckCastPP (LocalVar).
2517
//
2518
// ArrayAllocation
2519
// |
2520
// CheckCastPP
2521
// |
2522
// memProj (from ArrayAllocation CheckCastPP)
2523
// | ||
2524
// | || Int (element index)
2525
// | || | ConI (log(element size))
2526
// | || | /
2527
// | || LShift
2528
// | || /
2529
// | AddP (array's element offset)
2530
// | |
2531
// | | ConI (array's offset: #12(32-bits) or #24(64-bits))
2532
// | / /
2533
// AddP (array's offset)
2534
// |
2535
// Load/Store (memory operation on array's element)
2536
//
2537
return addp2;
2538
}
2539
return NULL;
2540
}
2541
2542
//
2543
// Adjust the type and inputs of an AddP which computes the
2544
// address of a field of an instance
2545
//
2546
bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
2547
PhaseGVN* igvn = _igvn;
2548
const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
2549
assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
2550
const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
2551
if (t == NULL) {
2552
// We are computing a raw address for a store captured by an Initialize
2553
// compute an appropriate address type (cases #3 and #5).
2554
assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
2555
assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
2556
intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
2557
assert(offs != Type::OffsetBot, "offset must be a constant");
2558
t = base_t->add_offset(offs)->is_oopptr();
2559
}
2560
int inst_id = base_t->instance_id();
2561
assert(!t->is_known_instance() || t->instance_id() == inst_id,
2562
"old type must be non-instance or match new type");
2563
2564
// The type 't' could be subclass of 'base_t'.
2565
// As result t->offset() could be large then base_t's size and it will
2566
// cause the failure in add_offset() with narrow oops since TypeOopPtr()
2567
// constructor verifies correctness of the offset.
2568
//
2569
// It could happened on subclass's branch (from the type profiling
2570
// inlining) which was not eliminated during parsing since the exactness
2571
// of the allocation type was not propagated to the subclass type check.
2572
//
2573
// Or the type 't' could be not related to 'base_t' at all.
2574
// It could happened when CHA type is different from MDO type on a dead path
2575
// (for example, from instanceof check) which is not collapsed during parsing.
2576
//
2577
// Do nothing for such AddP node and don't process its users since
2578
// this code branch will go away.
2579
//
2580
if (!t->is_known_instance() &&
2581
!base_t->klass()->is_subtype_of(t->klass())) {
2582
return false; // bail out
2583
}
2584
const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
2585
// Do NOT remove the next line: ensure a new alias index is allocated
2586
// for the instance type. Note: C++ will not remove it since the call
2587
// has side effect.
2588
int alias_idx = _compile->get_alias_index(tinst);
2589
igvn->set_type(addp, tinst);
2590
// record the allocation in the node map
2591
set_map(addp, get_map(base->_idx));
2592
// Set addp's Base and Address to 'base'.
2593
Node *abase = addp->in(AddPNode::Base);
2594
Node *adr = addp->in(AddPNode::Address);
2595
if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
2596
adr->in(0)->_idx == (uint)inst_id) {
2597
// Skip AddP cases #3 and #5.
2598
} else {
2599
assert(!abase->is_top(), "sanity"); // AddP case #3
2600
if (abase != base) {
2601
igvn->hash_delete(addp);
2602
addp->set_req(AddPNode::Base, base);
2603
if (abase == adr) {
2604
addp->set_req(AddPNode::Address, base);
2605
} else {
2606
// AddP case #4 (adr is array's element offset AddP node)
2607
#ifdef ASSERT
2608
const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
2609
assert(adr->is_AddP() && atype != NULL &&
2610
atype->instance_id() == inst_id, "array's element offset should be processed first");
2611
#endif
2612
}
2613
igvn->hash_insert(addp);
2614
}
2615
}
2616
// Put on IGVN worklist since at least addp's type was changed above.
2617
record_for_optimizer(addp);
2618
return true;
2619
}
2620
2621
//
2622
// Create a new version of orig_phi if necessary. Returns either the newly
2623
// created phi or an existing phi. Sets create_new to indicate whether a new
2624
// phi was created. Cache the last newly created phi in the node map.
2625
//
2626
PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created) {
2627
Compile *C = _compile;
2628
PhaseGVN* igvn = _igvn;
2629
new_created = false;
2630
int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
2631
// nothing to do if orig_phi is bottom memory or matches alias_idx
2632
if (phi_alias_idx == alias_idx) {
2633
return orig_phi;
2634
}
2635
// Have we recently created a Phi for this alias index?
2636
PhiNode *result = get_map_phi(orig_phi->_idx);
2637
if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
2638
return result;
2639
}
2640
// Previous check may fail when the same wide memory Phi was split into Phis
2641
// for different memory slices. Search all Phis for this region.
2642
if (result != NULL) {
2643
Node* region = orig_phi->in(0);
2644
for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
2645
Node* phi = region->fast_out(i);
2646
if (phi->is_Phi() &&
2647
C->get_alias_index(phi->as_Phi()->adr_type()) == alias_idx) {
2648
assert(phi->_idx >= nodes_size(), "only new Phi per instance memory slice");
2649
return phi->as_Phi();
2650
}
2651
}
2652
}
2653
if (C->live_nodes() + 2*NodeLimitFudgeFactor > C->max_node_limit()) {
2654
if (C->do_escape_analysis() == true && !C->failing()) {
2655
// Retry compilation without escape analysis.
2656
// If this is the first failure, the sentinel string will "stick"
2657
// to the Compile object, and the C2Compiler will see it and retry.
2658
C->record_failure(C2Compiler::retry_no_escape_analysis());
2659
}
2660
return NULL;
2661
}
2662
orig_phi_worklist.append_if_missing(orig_phi);
2663
const TypePtr *atype = C->get_adr_type(alias_idx);
2664
result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
2665
C->copy_node_notes_to(result, orig_phi);
2666
igvn->set_type(result, result->bottom_type());
2667
record_for_optimizer(result);
2668
set_map(orig_phi, result);
2669
new_created = true;
2670
return result;
2671
}
2672
2673
//
2674
// Return a new version of Memory Phi "orig_phi" with the inputs having the
2675
// specified alias index.
2676
//
2677
PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist) {
2678
assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
2679
Compile *C = _compile;
2680
PhaseGVN* igvn = _igvn;
2681
bool new_phi_created;
2682
PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, new_phi_created);
2683
if (!new_phi_created) {
2684
return result;
2685
}
2686
GrowableArray<PhiNode *> phi_list;
2687
GrowableArray<uint> cur_input;
2688
PhiNode *phi = orig_phi;
2689
uint idx = 1;
2690
bool finished = false;
2691
while(!finished) {
2692
while (idx < phi->req()) {
2693
Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist);
2694
if (mem != NULL && mem->is_Phi()) {
2695
PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, new_phi_created);
2696
if (new_phi_created) {
2697
// found an phi for which we created a new split, push current one on worklist and begin
2698
// processing new one
2699
phi_list.push(phi);
2700
cur_input.push(idx);
2701
phi = mem->as_Phi();
2702
result = newphi;
2703
idx = 1;
2704
continue;
2705
} else {
2706
mem = newphi;
2707
}
2708
}
2709
if (C->failing()) {
2710
return NULL;
2711
}
2712
result->set_req(idx++, mem);
2713
}
2714
#ifdef ASSERT
2715
// verify that the new Phi has an input for each input of the original
2716
assert( phi->req() == result->req(), "must have same number of inputs.");
2717
assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
2718
#endif
2719
// Check if all new phi's inputs have specified alias index.
2720
// Otherwise use old phi.
2721
for (uint i = 1; i < phi->req(); i++) {
2722
Node* in = result->in(i);
2723
assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
2724
}
2725
// we have finished processing a Phi, see if there are any more to do
2726
finished = (phi_list.length() == 0 );
2727
if (!finished) {
2728
phi = phi_list.pop();
2729
idx = cur_input.pop();
2730
PhiNode *prev_result = get_map_phi(phi->_idx);
2731
prev_result->set_req(idx++, result);
2732
result = prev_result;
2733
}
2734
}
2735
return result;
2736
}
2737
2738
//
2739
// The next methods are derived from methods in MemNode.
2740
//
2741
Node* ConnectionGraph::step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop) {
2742
Node *mem = mmem;
2743
// TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
2744
// means an array I have not precisely typed yet. Do not do any
2745
// alias stuff with it any time soon.
2746
if (toop->base() != Type::AnyPtr &&
2747
!(toop->klass() != NULL &&
2748
toop->klass()->is_java_lang_Object() &&
2749
toop->offset() == Type::OffsetBot)) {
2750
mem = mmem->memory_at(alias_idx);
2751
// Update input if it is progress over what we have now
2752
}
2753
return mem;
2754
}
2755
2756
//
2757
// Move memory users to their memory slices.
2758
//
2759
void ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis) {
2760
Compile* C = _compile;
2761
PhaseGVN* igvn = _igvn;
2762
const TypePtr* tp = igvn->type(n->in(MemNode::Address))->isa_ptr();
2763
assert(tp != NULL, "ptr type");
2764
int alias_idx = C->get_alias_index(tp);
2765
int general_idx = C->get_general_index(alias_idx);
2766
2767
// Move users first
2768
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2769
Node* use = n->fast_out(i);
2770
if (use->is_MergeMem()) {
2771
MergeMemNode* mmem = use->as_MergeMem();
2772
assert(n == mmem->memory_at(alias_idx), "should be on instance memory slice");
2773
if (n != mmem->memory_at(general_idx) || alias_idx == general_idx) {
2774
continue; // Nothing to do
2775
}
2776
// Replace previous general reference to mem node.
2777
uint orig_uniq = C->unique();
2778
Node* m = find_inst_mem(n, general_idx, orig_phis);
2779
assert(orig_uniq == C->unique(), "no new nodes");
2780
mmem->set_memory_at(general_idx, m);
2781
--imax;
2782
--i;
2783
} else if (use->is_MemBar()) {
2784
assert(!use->is_Initialize(), "initializing stores should not be moved");
2785
if (use->req() > MemBarNode::Precedent &&
2786
use->in(MemBarNode::Precedent) == n) {
2787
// Don't move related membars.
2788
record_for_optimizer(use);
2789
continue;
2790
}
2791
tp = use->as_MemBar()->adr_type()->isa_ptr();
2792
if ((tp != NULL && C->get_alias_index(tp) == alias_idx) ||
2793
alias_idx == general_idx) {
2794
continue; // Nothing to do
2795
}
2796
// Move to general memory slice.
2797
uint orig_uniq = C->unique();
2798
Node* m = find_inst_mem(n, general_idx, orig_phis);
2799
assert(orig_uniq == C->unique(), "no new nodes");
2800
igvn->hash_delete(use);
2801
imax -= use->replace_edge(n, m, igvn);
2802
igvn->hash_insert(use);
2803
record_for_optimizer(use);
2804
--i;
2805
#ifdef ASSERT
2806
} else if (use->is_Mem()) {
2807
if (use->Opcode() == Op_StoreCM && use->in(MemNode::OopStore) == n) {
2808
// Don't move related cardmark.
2809
continue;
2810
}
2811
// Memory nodes should have new memory input.
2812
tp = igvn->type(use->in(MemNode::Address))->isa_ptr();
2813
assert(tp != NULL, "ptr type");
2814
int idx = C->get_alias_index(tp);
2815
assert(get_map(use->_idx) != NULL || idx == alias_idx,
2816
"Following memory nodes should have new memory input or be on the same memory slice");
2817
} else if (use->is_Phi()) {
2818
// Phi nodes should be split and moved already.
2819
tp = use->as_Phi()->adr_type()->isa_ptr();
2820
assert(tp != NULL, "ptr type");
2821
int idx = C->get_alias_index(tp);
2822
assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
2823
} else {
2824
use->dump();
2825
assert(false, "should not be here");
2826
#endif
2827
}
2828
}
2829
}
2830
2831
//
2832
// Search memory chain of "mem" to find a MemNode whose address
2833
// is the specified alias index.
2834
//
2835
Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis) {
2836
if (orig_mem == NULL) {
2837
return orig_mem;
2838
}
2839
Compile* C = _compile;
2840
PhaseGVN* igvn = _igvn;
2841
const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
2842
bool is_instance = (toop != NULL) && toop->is_known_instance();
2843
Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
2844
Node *prev = NULL;
2845
Node *result = orig_mem;
2846
while (prev != result) {
2847
prev = result;
2848
if (result == start_mem) {
2849
break; // hit one of our sentinels
2850
}
2851
if (result->is_Mem()) {
2852
const Type *at = igvn->type(result->in(MemNode::Address));
2853
if (at == Type::TOP) {
2854
break; // Dead
2855
}
2856
assert (at->isa_ptr() != NULL, "pointer type required.");
2857
int idx = C->get_alias_index(at->is_ptr());
2858
if (idx == alias_idx) {
2859
break; // Found
2860
}
2861
if (!is_instance && (at->isa_oopptr() == NULL ||
2862
!at->is_oopptr()->is_known_instance())) {
2863
break; // Do not skip store to general memory slice.
2864
}
2865
result = result->in(MemNode::Memory);
2866
}
2867
if (!is_instance) {
2868
continue; // don't search further for non-instance types
2869
}
2870
// skip over a call which does not affect this memory slice
2871
if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
2872
Node *proj_in = result->in(0);
2873
if (proj_in->is_Allocate() && proj_in->_idx == (uint)toop->instance_id()) {
2874
break; // hit one of our sentinels
2875
} else if (proj_in->is_Call()) {
2876
// ArrayCopy node processed here as well
2877
CallNode *call = proj_in->as_Call();
2878
if (!call->may_modify(toop, igvn)) {
2879
result = call->in(TypeFunc::Memory);
2880
}
2881
} else if (proj_in->is_Initialize()) {
2882
AllocateNode* alloc = proj_in->as_Initialize()->allocation();
2883
// Stop if this is the initialization for the object instance which
2884
// which contains this memory slice, otherwise skip over it.
2885
if (alloc == NULL || alloc->_idx != (uint)toop->instance_id()) {
2886
result = proj_in->in(TypeFunc::Memory);
2887
}
2888
} else if (proj_in->is_MemBar()) {
2889
// Check if there is an array copy for a clone
2890
// Step over GC barrier when ReduceInitialCardMarks is disabled
2891
BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2892
Node* control_proj_ac = bs->step_over_gc_barrier(proj_in->in(0));
2893
2894
if (control_proj_ac->is_Proj() && control_proj_ac->in(0)->is_ArrayCopy()) {
2895
// Stop if it is a clone
2896
ArrayCopyNode* ac = control_proj_ac->in(0)->as_ArrayCopy();
2897
if (ac->may_modify(toop, igvn)) {
2898
break;
2899
}
2900
}
2901
result = proj_in->in(TypeFunc::Memory);
2902
}
2903
} else if (result->is_MergeMem()) {
2904
MergeMemNode *mmem = result->as_MergeMem();
2905
result = step_through_mergemem(mmem, alias_idx, toop);
2906
if (result == mmem->base_memory()) {
2907
// Didn't find instance memory, search through general slice recursively.
2908
result = mmem->memory_at(C->get_general_index(alias_idx));
2909
result = find_inst_mem(result, alias_idx, orig_phis);
2910
if (C->failing()) {
2911
return NULL;
2912
}
2913
mmem->set_memory_at(alias_idx, result);
2914
}
2915
} else if (result->is_Phi() &&
2916
C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
2917
Node *un = result->as_Phi()->unique_input(igvn);
2918
if (un != NULL) {
2919
orig_phis.append_if_missing(result->as_Phi());
2920
result = un;
2921
} else {
2922
break;
2923
}
2924
} else if (result->is_ClearArray()) {
2925
if (!ClearArrayNode::step_through(&result, (uint)toop->instance_id(), igvn)) {
2926
// Can not bypass initialization of the instance
2927
// we are looking for.
2928
break;
2929
}
2930
// Otherwise skip it (the call updated 'result' value).
2931
} else if (result->Opcode() == Op_SCMemProj) {
2932
Node* mem = result->in(0);
2933
Node* adr = NULL;
2934
if (mem->is_LoadStore()) {
2935
adr = mem->in(MemNode::Address);
2936
} else {
2937
assert(mem->Opcode() == Op_EncodeISOArray ||
2938
mem->Opcode() == Op_StrCompressedCopy, "sanity");
2939
adr = mem->in(3); // Memory edge corresponds to destination array
2940
}
2941
const Type *at = igvn->type(adr);
2942
if (at != Type::TOP) {
2943
assert(at->isa_ptr() != NULL, "pointer type required.");
2944
int idx = C->get_alias_index(at->is_ptr());
2945
if (idx == alias_idx) {
2946
// Assert in debug mode
2947
assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
2948
break; // In product mode return SCMemProj node
2949
}
2950
}
2951
result = mem->in(MemNode::Memory);
2952
} else if (result->Opcode() == Op_StrInflatedCopy) {
2953
Node* adr = result->in(3); // Memory edge corresponds to destination array
2954
const Type *at = igvn->type(adr);
2955
if (at != Type::TOP) {
2956
assert(at->isa_ptr() != NULL, "pointer type required.");
2957
int idx = C->get_alias_index(at->is_ptr());
2958
if (idx == alias_idx) {
2959
// Assert in debug mode
2960
assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field");
2961
break; // In product mode return SCMemProj node
2962
}
2963
}
2964
result = result->in(MemNode::Memory);
2965
}
2966
}
2967
if (result->is_Phi()) {
2968
PhiNode *mphi = result->as_Phi();
2969
assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
2970
const TypePtr *t = mphi->adr_type();
2971
if (!is_instance) {
2972
// Push all non-instance Phis on the orig_phis worklist to update inputs
2973
// during Phase 4 if needed.
2974
orig_phis.append_if_missing(mphi);
2975
} else if (C->get_alias_index(t) != alias_idx) {
2976
// Create a new Phi with the specified alias index type.
2977
result = split_memory_phi(mphi, alias_idx, orig_phis);
2978
}
2979
}
2980
// the result is either MemNode, PhiNode, InitializeNode.
2981
return result;
2982
}
2983
2984
//
2985
// Convert the types of non-escaped object to instance types where possible,
2986
// propagate the new type information through the graph, and update memory
2987
// edges and MergeMem inputs to reflect the new type.
2988
//
2989
// We start with allocations (and calls which may be allocations) on alloc_worklist.
2990
// The processing is done in 4 phases:
2991
//
2992
// Phase 1: Process possible allocations from alloc_worklist. Create instance
2993
// types for the CheckCastPP for allocations where possible.
2994
// Propagate the new types through users as follows:
2995
// casts and Phi: push users on alloc_worklist
2996
// AddP: cast Base and Address inputs to the instance type
2997
// push any AddP users on alloc_worklist and push any memnode
2998
// users onto memnode_worklist.
2999
// Phase 2: Process MemNode's from memnode_worklist. compute new address type and
3000
// search the Memory chain for a store with the appropriate type
3001
// address type. If a Phi is found, create a new version with
3002
// the appropriate memory slices from each of the Phi inputs.
3003
// For stores, process the users as follows:
3004
// MemNode: push on memnode_worklist
3005
// MergeMem: push on mergemem_worklist
3006
// Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice
3007
// moving the first node encountered of each instance type to the
3008
// the input corresponding to its alias index.
3009
// appropriate memory slice.
3010
// Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes.
3011
//
3012
// In the following example, the CheckCastPP nodes are the cast of allocation
3013
// results and the allocation of node 29 is non-escaped and eligible to be an
3014
// instance type.
3015
//
3016
// We start with:
3017
//
3018
// 7 Parm #memory
3019
// 10 ConI "12"
3020
// 19 CheckCastPP "Foo"
3021
// 20 AddP _ 19 19 10 Foo+12 alias_index=4
3022
// 29 CheckCastPP "Foo"
3023
// 30 AddP _ 29 29 10 Foo+12 alias_index=4
3024
//
3025
// 40 StoreP 25 7 20 ... alias_index=4
3026
// 50 StoreP 35 40 30 ... alias_index=4
3027
// 60 StoreP 45 50 20 ... alias_index=4
3028
// 70 LoadP _ 60 30 ... alias_index=4
3029
// 80 Phi 75 50 60 Memory alias_index=4
3030
// 90 LoadP _ 80 30 ... alias_index=4
3031
// 100 LoadP _ 80 20 ... alias_index=4
3032
//
3033
//
3034
// Phase 1 creates an instance type for node 29 assigning it an instance id of 24
3035
// and creating a new alias index for node 30. This gives:
3036
//
3037
// 7 Parm #memory
3038
// 10 ConI "12"
3039
// 19 CheckCastPP "Foo"
3040
// 20 AddP _ 19 19 10 Foo+12 alias_index=4
3041
// 29 CheckCastPP "Foo" iid=24
3042
// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
3043
//
3044
// 40 StoreP 25 7 20 ... alias_index=4
3045
// 50 StoreP 35 40 30 ... alias_index=6
3046
// 60 StoreP 45 50 20 ... alias_index=4
3047
// 70 LoadP _ 60 30 ... alias_index=6
3048
// 80 Phi 75 50 60 Memory alias_index=4
3049
// 90 LoadP _ 80 30 ... alias_index=6
3050
// 100 LoadP _ 80 20 ... alias_index=4
3051
//
3052
// In phase 2, new memory inputs are computed for the loads and stores,
3053
// And a new version of the phi is created. In phase 4, the inputs to
3054
// node 80 are updated and then the memory nodes are updated with the
3055
// values computed in phase 2. This results in:
3056
//
3057
// 7 Parm #memory
3058
// 10 ConI "12"
3059
// 19 CheckCastPP "Foo"
3060
// 20 AddP _ 19 19 10 Foo+12 alias_index=4
3061
// 29 CheckCastPP "Foo" iid=24
3062
// 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
3063
//
3064
// 40 StoreP 25 7 20 ... alias_index=4
3065
// 50 StoreP 35 7 30 ... alias_index=6
3066
// 60 StoreP 45 40 20 ... alias_index=4
3067
// 70 LoadP _ 50 30 ... alias_index=6
3068
// 80 Phi 75 40 60 Memory alias_index=4
3069
// 120 Phi 75 50 50 Memory alias_index=6
3070
// 90 LoadP _ 120 30 ... alias_index=6
3071
// 100 LoadP _ 80 20 ... alias_index=4
3072
//
3073
void ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist, GrowableArray<ArrayCopyNode*> &arraycopy_worklist) {
3074
GrowableArray<Node *> memnode_worklist;
3075
GrowableArray<PhiNode *> orig_phis;
3076
PhaseIterGVN *igvn = _igvn;
3077
uint new_index_start = (uint) _compile->num_alias_types();
3078
VectorSet visited;
3079
ideal_nodes.clear(); // Reset for use with set_map/get_map.
3080
uint unique_old = _compile->unique();
3081
3082
// Phase 1: Process possible allocations from alloc_worklist.
3083
// Create instance types for the CheckCastPP for allocations where possible.
3084
//
3085
// (Note: don't forget to change the order of the second AddP node on
3086
// the alloc_worklist if the order of the worklist processing is changed,
3087
// see the comment in find_second_addp().)
3088
//
3089
while (alloc_worklist.length() != 0) {
3090
Node *n = alloc_worklist.pop();
3091
uint ni = n->_idx;
3092
if (n->is_Call()) {
3093
CallNode *alloc = n->as_Call();
3094
// copy escape information to call node
3095
PointsToNode* ptn = ptnode_adr(alloc->_idx);
3096
PointsToNode::EscapeState es = ptn->escape_state();
3097
// We have an allocation or call which returns a Java object,
3098
// see if it is non-escaped.
3099
if (es != PointsToNode::NoEscape || !ptn->scalar_replaceable()) {
3100
continue;
3101
}
3102
// Find CheckCastPP for the allocate or for the return value of a call
3103
n = alloc->result_cast();
3104
if (n == NULL) { // No uses except Initialize node
3105
if (alloc->is_Allocate()) {
3106
// Set the scalar_replaceable flag for allocation
3107
// so it could be eliminated if it has no uses.
3108
alloc->as_Allocate()->_is_scalar_replaceable = true;
3109
}
3110
continue;
3111
}
3112
if (!n->is_CheckCastPP()) { // not unique CheckCastPP.
3113
// we could reach here for allocate case if one init is associated with many allocs.
3114
if (alloc->is_Allocate()) {
3115
alloc->as_Allocate()->_is_scalar_replaceable = false;
3116
}
3117
continue;
3118
}
3119
3120
// The inline code for Object.clone() casts the allocation result to
3121
// java.lang.Object and then to the actual type of the allocated
3122
// object. Detect this case and use the second cast.
3123
// Also detect j.l.reflect.Array.newInstance(jobject, jint) case when
3124
// the allocation result is cast to java.lang.Object and then
3125
// to the actual Array type.
3126
if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
3127
&& (alloc->is_AllocateArray() ||
3128
igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT)) {
3129
Node *cast2 = NULL;
3130
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3131
Node *use = n->fast_out(i);
3132
if (use->is_CheckCastPP()) {
3133
cast2 = use;
3134
break;
3135
}
3136
}
3137
if (cast2 != NULL) {
3138
n = cast2;
3139
} else {
3140
// Non-scalar replaceable if the allocation type is unknown statically
3141
// (reflection allocation), the object can't be restored during
3142
// deoptimization without precise type.
3143
continue;
3144
}
3145
}
3146
3147
const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
3148
if (t == NULL) {
3149
continue; // not a TypeOopPtr
3150
}
3151
if (!t->klass_is_exact()) {
3152
continue; // not an unique type
3153
}
3154
if (alloc->is_Allocate()) {
3155
// Set the scalar_replaceable flag for allocation
3156
// so it could be eliminated.
3157
alloc->as_Allocate()->_is_scalar_replaceable = true;
3158
}
3159
set_escape_state(ptnode_adr(n->_idx), es); // CheckCastPP escape state
3160
// in order for an object to be scalar-replaceable, it must be:
3161
// - a direct allocation (not a call returning an object)
3162
// - non-escaping
3163
// - eligible to be a unique type
3164
// - not determined to be ineligible by escape analysis
3165
set_map(alloc, n);
3166
set_map(n, alloc);
3167
const TypeOopPtr* tinst = t->cast_to_instance_id(ni);
3168
igvn->hash_delete(n);
3169
igvn->set_type(n, tinst);
3170
n->raise_bottom_type(tinst);
3171
igvn->hash_insert(n);
3172
record_for_optimizer(n);
3173
// Allocate an alias index for the header fields. Accesses to
3174
// the header emitted during macro expansion wouldn't have
3175
// correct memory state otherwise.
3176
_compile->get_alias_index(tinst->add_offset(oopDesc::mark_offset_in_bytes()));
3177
_compile->get_alias_index(tinst->add_offset(oopDesc::klass_offset_in_bytes()));
3178
if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
3179
3180
// First, put on the worklist all Field edges from Connection Graph
3181
// which is more accurate than putting immediate users from Ideal Graph.
3182
for (EdgeIterator e(ptn); e.has_next(); e.next()) {
3183
PointsToNode* tgt = e.get();
3184
if (tgt->is_Arraycopy()) {
3185
continue;
3186
}
3187
Node* use = tgt->ideal_node();
3188
assert(tgt->is_Field() && use->is_AddP(),
3189
"only AddP nodes are Field edges in CG");
3190
if (use->outcnt() > 0) { // Don't process dead nodes
3191
Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
3192
if (addp2 != NULL) {
3193
assert(alloc->is_AllocateArray(),"array allocation was expected");
3194
alloc_worklist.append_if_missing(addp2);
3195
}
3196
alloc_worklist.append_if_missing(use);
3197
}
3198
}
3199
3200
// An allocation may have an Initialize which has raw stores. Scan
3201
// the users of the raw allocation result and push AddP users
3202
// on alloc_worklist.
3203
Node *raw_result = alloc->proj_out_or_null(TypeFunc::Parms);
3204
assert (raw_result != NULL, "must have an allocation result");
3205
for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
3206
Node *use = raw_result->fast_out(i);
3207
if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
3208
Node* addp2 = find_second_addp(use, raw_result);
3209
if (addp2 != NULL) {
3210
assert(alloc->is_AllocateArray(),"array allocation was expected");
3211
alloc_worklist.append_if_missing(addp2);
3212
}
3213
alloc_worklist.append_if_missing(use);
3214
} else if (use->is_MemBar()) {
3215
memnode_worklist.append_if_missing(use);
3216
}
3217
}
3218
}
3219
} else if (n->is_AddP()) {
3220
JavaObjectNode* jobj = unique_java_object(get_addp_base(n));
3221
if (jobj == NULL || jobj == phantom_obj) {
3222
#ifdef ASSERT
3223
ptnode_adr(get_addp_base(n)->_idx)->dump();
3224
ptnode_adr(n->_idx)->dump();
3225
assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3226
#endif
3227
_compile->record_failure(C2Compiler::retry_no_escape_analysis());
3228
return;
3229
}
3230
Node *base = get_map(jobj->idx()); // CheckCastPP node
3231
if (!split_AddP(n, base)) continue; // wrong type from dead path
3232
} else if (n->is_Phi() ||
3233
n->is_CheckCastPP() ||
3234
n->is_EncodeP() ||
3235
n->is_DecodeN() ||
3236
(n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
3237
if (visited.test_set(n->_idx)) {
3238
assert(n->is_Phi(), "loops only through Phi's");
3239
continue; // already processed
3240
}
3241
JavaObjectNode* jobj = unique_java_object(n);
3242
if (jobj == NULL || jobj == phantom_obj) {
3243
#ifdef ASSERT
3244
ptnode_adr(n->_idx)->dump();
3245
assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3246
#endif
3247
_compile->record_failure(C2Compiler::retry_no_escape_analysis());
3248
return;
3249
} else {
3250
Node *val = get_map(jobj->idx()); // CheckCastPP node
3251
TypeNode *tn = n->as_Type();
3252
const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
3253
assert(tinst != NULL && tinst->is_known_instance() &&
3254
tinst->instance_id() == jobj->idx() , "instance type expected.");
3255
3256
const Type *tn_type = igvn->type(tn);
3257
const TypeOopPtr *tn_t;
3258
if (tn_type->isa_narrowoop()) {
3259
tn_t = tn_type->make_ptr()->isa_oopptr();
3260
} else {
3261
tn_t = tn_type->isa_oopptr();
3262
}
3263
if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {
3264
if (tn_type->isa_narrowoop()) {
3265
tn_type = tinst->make_narrowoop();
3266
} else {
3267
tn_type = tinst;
3268
}
3269
igvn->hash_delete(tn);
3270
igvn->set_type(tn, tn_type);
3271
tn->set_type(tn_type);
3272
igvn->hash_insert(tn);
3273
record_for_optimizer(n);
3274
} else {
3275
assert(tn_type == TypePtr::NULL_PTR ||
3276
tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
3277
"unexpected type");
3278
continue; // Skip dead path with different type
3279
}
3280
}
3281
} else {
3282
debug_only(n->dump();)
3283
assert(false, "EA: unexpected node");
3284
continue;
3285
}
3286
// push allocation's users on appropriate worklist
3287
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3288
Node *use = n->fast_out(i);
3289
if(use->is_Mem() && use->in(MemNode::Address) == n) {
3290
// Load/store to instance's field
3291
memnode_worklist.append_if_missing(use);
3292
} else if (use->is_MemBar()) {
3293
if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3294
memnode_worklist.append_if_missing(use);
3295
}
3296
} else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
3297
Node* addp2 = find_second_addp(use, n);
3298
if (addp2 != NULL) {
3299
alloc_worklist.append_if_missing(addp2);
3300
}
3301
alloc_worklist.append_if_missing(use);
3302
} else if (use->is_Phi() ||
3303
use->is_CheckCastPP() ||
3304
use->is_EncodeNarrowPtr() ||
3305
use->is_DecodeNarrowPtr() ||
3306
(use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
3307
alloc_worklist.append_if_missing(use);
3308
#ifdef ASSERT
3309
} else if (use->is_Mem()) {
3310
assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
3311
} else if (use->is_MergeMem()) {
3312
assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3313
} else if (use->is_SafePoint()) {
3314
// Look for MergeMem nodes for calls which reference unique allocation
3315
// (through CheckCastPP nodes) even for debug info.
3316
Node* m = use->in(TypeFunc::Memory);
3317
if (m->is_MergeMem()) {
3318
assert(_mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3319
}
3320
} else if (use->Opcode() == Op_EncodeISOArray) {
3321
if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3322
// EncodeISOArray overwrites destination array
3323
memnode_worklist.append_if_missing(use);
3324
}
3325
} else {
3326
uint op = use->Opcode();
3327
if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
3328
(use->in(MemNode::Memory) == n)) {
3329
// They overwrite memory edge corresponding to destination array,
3330
memnode_worklist.append_if_missing(use);
3331
} else if (!(op == Op_CmpP || op == Op_Conv2B ||
3332
op == Op_CastP2X || op == Op_StoreCM ||
3333
op == Op_FastLock || op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3334
op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3335
op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
3336
op == Op_SubTypeCheck ||
3337
BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
3338
n->dump();
3339
use->dump();
3340
assert(false, "EA: missing allocation reference path");
3341
}
3342
#endif
3343
}
3344
}
3345
3346
}
3347
3348
// Go over all ArrayCopy nodes and if one of the inputs has a unique
3349
// type, record it in the ArrayCopy node so we know what memory this
3350
// node uses/modified.
3351
for (int next = 0; next < arraycopy_worklist.length(); next++) {
3352
ArrayCopyNode* ac = arraycopy_worklist.at(next);
3353
Node* dest = ac->in(ArrayCopyNode::Dest);
3354
if (dest->is_AddP()) {
3355
dest = get_addp_base(dest);
3356
}
3357
JavaObjectNode* jobj = unique_java_object(dest);
3358
if (jobj != NULL) {
3359
Node *base = get_map(jobj->idx());
3360
if (base != NULL) {
3361
const TypeOopPtr *base_t = _igvn->type(base)->isa_oopptr();
3362
ac->_dest_type = base_t;
3363
}
3364
}
3365
Node* src = ac->in(ArrayCopyNode::Src);
3366
if (src->is_AddP()) {
3367
src = get_addp_base(src);
3368
}
3369
jobj = unique_java_object(src);
3370
if (jobj != NULL) {
3371
Node* base = get_map(jobj->idx());
3372
if (base != NULL) {
3373
const TypeOopPtr *base_t = _igvn->type(base)->isa_oopptr();
3374
ac->_src_type = base_t;
3375
}
3376
}
3377
}
3378
3379
// New alias types were created in split_AddP().
3380
uint new_index_end = (uint) _compile->num_alias_types();
3381
assert(unique_old == _compile->unique(), "there should be no new ideal nodes after Phase 1");
3382
3383
// Phase 2: Process MemNode's from memnode_worklist. compute new address type and
3384
// compute new values for Memory inputs (the Memory inputs are not
3385
// actually updated until phase 4.)
3386
if (memnode_worklist.length() == 0)
3387
return; // nothing to do
3388
while (memnode_worklist.length() != 0) {
3389
Node *n = memnode_worklist.pop();
3390
if (visited.test_set(n->_idx)) {
3391
continue;
3392
}
3393
if (n->is_Phi() || n->is_ClearArray()) {
3394
// we don't need to do anything, but the users must be pushed
3395
} else if (n->is_MemBar()) { // Initialize, MemBar nodes
3396
// we don't need to do anything, but the users must be pushed
3397
n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3398
if (n == NULL) {
3399
continue;
3400
}
3401
} else if (n->Opcode() == Op_StrCompressedCopy ||
3402
n->Opcode() == Op_EncodeISOArray) {
3403
// get the memory projection
3404
n = n->find_out_with(Op_SCMemProj);
3405
assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3406
} else {
3407
assert(n->is_Mem(), "memory node required.");
3408
Node *addr = n->in(MemNode::Address);
3409
const Type *addr_t = igvn->type(addr);
3410
if (addr_t == Type::TOP) {
3411
continue;
3412
}
3413
assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3414
int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3415
assert ((uint)alias_idx < new_index_end, "wrong alias index");
3416
Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3417
if (_compile->failing()) {
3418
return;
3419
}
3420
if (mem != n->in(MemNode::Memory)) {
3421
// We delay the memory edge update since we need old one in
3422
// MergeMem code below when instances memory slices are separated.
3423
set_map(n, mem);
3424
}
3425
if (n->is_Load()) {
3426
continue; // don't push users
3427
} else if (n->is_LoadStore()) {
3428
// get the memory projection
3429
n = n->find_out_with(Op_SCMemProj);
3430
assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3431
}
3432
}
3433
// push user on appropriate worklist
3434
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3435
Node *use = n->fast_out(i);
3436
if (use->is_Phi() || use->is_ClearArray()) {
3437
memnode_worklist.append_if_missing(use);
3438
} else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
3439
if (use->Opcode() == Op_StoreCM) { // Ignore cardmark stores
3440
continue;
3441
}
3442
memnode_worklist.append_if_missing(use);
3443
} else if (use->is_MemBar()) {
3444
if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3445
memnode_worklist.append_if_missing(use);
3446
}
3447
#ifdef ASSERT
3448
} else if(use->is_Mem()) {
3449
assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
3450
} else if (use->is_MergeMem()) {
3451
assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3452
} else if (use->Opcode() == Op_EncodeISOArray) {
3453
if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3454
// EncodeISOArray overwrites destination array
3455
memnode_worklist.append_if_missing(use);
3456
}
3457
} else {
3458
uint op = use->Opcode();
3459
if ((use->in(MemNode::Memory) == n) &&
3460
(op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
3461
// They overwrite memory edge corresponding to destination array,
3462
memnode_worklist.append_if_missing(use);
3463
} else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
3464
op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3465
op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3466
op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
3467
n->dump();
3468
use->dump();
3469
assert(false, "EA: missing memory path");
3470
}
3471
#endif
3472
}
3473
}
3474
}
3475
3476
// Phase 3: Process MergeMem nodes from mergemem_worklist.
3477
// Walk each memory slice moving the first node encountered of each
3478
// instance type to the the input corresponding to its alias index.
3479
uint length = _mergemem_worklist.length();
3480
for( uint next = 0; next < length; ++next ) {
3481
MergeMemNode* nmm = _mergemem_worklist.at(next);
3482
assert(!visited.test_set(nmm->_idx), "should not be visited before");
3483
// Note: we don't want to use MergeMemStream here because we only want to
3484
// scan inputs which exist at the start, not ones we add during processing.
3485
// Note 2: MergeMem may already contains instance memory slices added
3486
// during find_inst_mem() call when memory nodes were processed above.
3487
igvn->hash_delete(nmm);
3488
uint nslices = MIN2(nmm->req(), new_index_start);
3489
for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
3490
Node* mem = nmm->in(i);
3491
Node* cur = NULL;
3492
if (mem == NULL || mem->is_top()) {
3493
continue;
3494
}
3495
// First, update mergemem by moving memory nodes to corresponding slices
3496
// if their type became more precise since this mergemem was created.
3497
while (mem->is_Mem()) {
3498
const Type *at = igvn->type(mem->in(MemNode::Address));
3499
if (at != Type::TOP) {
3500
assert (at->isa_ptr() != NULL, "pointer type required.");
3501
uint idx = (uint)_compile->get_alias_index(at->is_ptr());
3502
if (idx == i) {
3503
if (cur == NULL) {
3504
cur = mem;
3505
}
3506
} else {
3507
if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
3508
nmm->set_memory_at(idx, mem);
3509
}
3510
}
3511
}
3512
mem = mem->in(MemNode::Memory);
3513
}
3514
nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
3515
// Find any instance of the current type if we haven't encountered
3516
// already a memory slice of the instance along the memory chain.
3517
for (uint ni = new_index_start; ni < new_index_end; ni++) {
3518
if((uint)_compile->get_general_index(ni) == i) {
3519
Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
3520
if (nmm->is_empty_memory(m)) {
3521
Node* result = find_inst_mem(mem, ni, orig_phis);
3522
if (_compile->failing()) {
3523
return;
3524
}
3525
nmm->set_memory_at(ni, result);
3526
}
3527
}
3528
}
3529
}
3530
// Find the rest of instances values
3531
for (uint ni = new_index_start; ni < new_index_end; ni++) {
3532
const TypeOopPtr *tinst = _compile->get_adr_type(ni)->isa_oopptr();
3533
Node* result = step_through_mergemem(nmm, ni, tinst);
3534
if (result == nmm->base_memory()) {
3535
// Didn't find instance memory, search through general slice recursively.
3536
result = nmm->memory_at(_compile->get_general_index(ni));
3537
result = find_inst_mem(result, ni, orig_phis);
3538
if (_compile->failing()) {
3539
return;
3540
}
3541
nmm->set_memory_at(ni, result);
3542
}
3543
}
3544
igvn->hash_insert(nmm);
3545
record_for_optimizer(nmm);
3546
}
3547
3548
// Phase 4: Update the inputs of non-instance memory Phis and
3549
// the Memory input of memnodes
3550
// First update the inputs of any non-instance Phi's from
3551
// which we split out an instance Phi. Note we don't have
3552
// to recursively process Phi's encountered on the input memory
3553
// chains as is done in split_memory_phi() since they will
3554
// also be processed here.
3555
for (int j = 0; j < orig_phis.length(); j++) {
3556
PhiNode *phi = orig_phis.at(j);
3557
int alias_idx = _compile->get_alias_index(phi->adr_type());
3558
igvn->hash_delete(phi);
3559
for (uint i = 1; i < phi->req(); i++) {
3560
Node *mem = phi->in(i);
3561
Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3562
if (_compile->failing()) {
3563
return;
3564
}
3565
if (mem != new_mem) {
3566
phi->set_req(i, new_mem);
3567
}
3568
}
3569
igvn->hash_insert(phi);
3570
record_for_optimizer(phi);
3571
}
3572
3573
// Update the memory inputs of MemNodes with the value we computed
3574
// in Phase 2 and move stores memory users to corresponding memory slices.
3575
// Disable memory split verification code until the fix for 6984348.
3576
// Currently it produces false negative results since it does not cover all cases.
3577
#if 0 // ifdef ASSERT
3578
visited.Reset();
3579
Node_Stack old_mems(arena, _compile->unique() >> 2);
3580
#endif
3581
for (uint i = 0; i < ideal_nodes.size(); i++) {
3582
Node* n = ideal_nodes.at(i);
3583
Node* nmem = get_map(n->_idx);
3584
assert(nmem != NULL, "sanity");
3585
if (n->is_Mem()) {
3586
#if 0 // ifdef ASSERT
3587
Node* old_mem = n->in(MemNode::Memory);
3588
if (!visited.test_set(old_mem->_idx)) {
3589
old_mems.push(old_mem, old_mem->outcnt());
3590
}
3591
#endif
3592
assert(n->in(MemNode::Memory) != nmem, "sanity");
3593
if (!n->is_Load()) {
3594
// Move memory users of a store first.
3595
move_inst_mem(n, orig_phis);
3596
}
3597
// Now update memory input
3598
igvn->hash_delete(n);
3599
n->set_req(MemNode::Memory, nmem);
3600
igvn->hash_insert(n);
3601
record_for_optimizer(n);
3602
} else {
3603
assert(n->is_Allocate() || n->is_CheckCastPP() ||
3604
n->is_AddP() || n->is_Phi(), "unknown node used for set_map()");
3605
}
3606
}
3607
#if 0 // ifdef ASSERT
3608
// Verify that memory was split correctly
3609
while (old_mems.is_nonempty()) {
3610
Node* old_mem = old_mems.node();
3611
uint old_cnt = old_mems.index();
3612
old_mems.pop();
3613
assert(old_cnt == old_mem->outcnt(), "old mem could be lost");
3614
}
3615
#endif
3616
}
3617
3618
#ifndef PRODUCT
3619
static const char *node_type_names[] = {
3620
"UnknownType",
3621
"JavaObject",
3622
"LocalVar",
3623
"Field",
3624
"Arraycopy"
3625
};
3626
3627
static const char *esc_names[] = {
3628
"UnknownEscape",
3629
"NoEscape",
3630
"ArgEscape",
3631
"GlobalEscape"
3632
};
3633
3634
void PointsToNode::dump(bool print_state) const {
3635
NodeType nt = node_type();
3636
tty->print("%s ", node_type_names[(int) nt]);
3637
if (print_state) {
3638
EscapeState es = escape_state();
3639
EscapeState fields_es = fields_escape_state();
3640
tty->print("%s(%s) ", esc_names[(int)es], esc_names[(int)fields_es]);
3641
if (nt == PointsToNode::JavaObject && !this->scalar_replaceable()) {
3642
tty->print("NSR ");
3643
}
3644
}
3645
if (is_Field()) {
3646
FieldNode* f = (FieldNode*)this;
3647
if (f->is_oop()) {
3648
tty->print("oop ");
3649
}
3650
if (f->offset() > 0) {
3651
tty->print("+%d ", f->offset());
3652
}
3653
tty->print("(");
3654
for (BaseIterator i(f); i.has_next(); i.next()) {
3655
PointsToNode* b = i.get();
3656
tty->print(" %d%s", b->idx(),(b->is_JavaObject() ? "P" : ""));
3657
}
3658
tty->print(" )");
3659
}
3660
tty->print("[");
3661
for (EdgeIterator i(this); i.has_next(); i.next()) {
3662
PointsToNode* e = i.get();
3663
tty->print(" %d%s%s", e->idx(),(e->is_JavaObject() ? "P" : (e->is_Field() ? "F" : "")), e->is_Arraycopy() ? "cp" : "");
3664
}
3665
tty->print(" [");
3666
for (UseIterator i(this); i.has_next(); i.next()) {
3667
PointsToNode* u = i.get();
3668
bool is_base = false;
3669
if (PointsToNode::is_base_use(u)) {
3670
is_base = true;
3671
u = PointsToNode::get_use_node(u)->as_Field();
3672
}
3673
tty->print(" %d%s%s", u->idx(), is_base ? "b" : "", u->is_Arraycopy() ? "cp" : "");
3674
}
3675
tty->print(" ]] ");
3676
if (_node == NULL) {
3677
tty->print_cr("<null>");
3678
} else {
3679
_node->dump();
3680
}
3681
}
3682
3683
void ConnectionGraph::dump(GrowableArray<PointsToNode*>& ptnodes_worklist) {
3684
bool first = true;
3685
int ptnodes_length = ptnodes_worklist.length();
3686
for (int i = 0; i < ptnodes_length; i++) {
3687
PointsToNode *ptn = ptnodes_worklist.at(i);
3688
if (ptn == NULL || !ptn->is_JavaObject()) {
3689
continue;
3690
}
3691
PointsToNode::EscapeState es = ptn->escape_state();
3692
if ((es != PointsToNode::NoEscape) && !Verbose) {
3693
continue;
3694
}
3695
Node* n = ptn->ideal_node();
3696
if (n->is_Allocate() || (n->is_CallStaticJava() &&
3697
n->as_CallStaticJava()->is_boxing_method())) {
3698
if (first) {
3699
tty->cr();
3700
tty->print("======== Connection graph for ");
3701
_compile->method()->print_short_name();
3702
tty->cr();
3703
first = false;
3704
}
3705
ptn->dump();
3706
// Print all locals and fields which reference this allocation
3707
for (UseIterator j(ptn); j.has_next(); j.next()) {
3708
PointsToNode* use = j.get();
3709
if (use->is_LocalVar()) {
3710
use->dump(Verbose);
3711
} else if (Verbose) {
3712
use->dump();
3713
}
3714
}
3715
tty->cr();
3716
}
3717
}
3718
}
3719
#endif
3720
3721
void ConnectionGraph::record_for_optimizer(Node *n) {
3722
_igvn->_worklist.push(n);
3723
_igvn->add_users_to_worklist(n);
3724
}
3725
3726