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