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