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