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