1 /* 2 * Copyright (c) 2005, 2010, 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 "libadt/vectset.hpp" 28 #include "memory/allocation.hpp" 29 #include "opto/c2compiler.hpp" 30 #include "opto/callnode.hpp" 31 #include "opto/cfgnode.hpp" 32 #include "opto/compile.hpp" 33 #include "opto/escape.hpp" 34 #include "opto/phaseX.hpp" 35 #include "opto/rootnode.hpp" 36 37 void PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) { 38 uint v = (targIdx << EdgeShift) + ((uint) et); 39 if (_edges == NULL) { 40 Arena *a = Compile::current()->comp_arena(); 41 _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0); 42 } 43 _edges->append_if_missing(v); 44 } 45 46 void PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) { 47 uint v = (targIdx << EdgeShift) + ((uint) et); 48 49 _edges->remove(v); 50 } 51 52 #ifndef PRODUCT 53 static const char *node_type_names[] = { 54 "UnknownType", 55 "JavaObject", 56 "LocalVar", 57 "Field" 58 }; 59 60 static const char *esc_names[] = { 61 "UnknownEscape", 62 "NoEscape", 63 "ArgEscape", 64 "GlobalEscape" 65 }; 66 67 static const char *edge_type_suffix[] = { 68 "?", // UnknownEdge 69 "P", // PointsToEdge 70 "D", // DeferredEdge 71 "F" // FieldEdge 72 }; 73 74 void PointsToNode::dump(bool print_state) const { 75 NodeType nt = node_type(); 76 tty->print("%s ", node_type_names[(int) nt]); 77 if (print_state) { 78 EscapeState es = escape_state(); 79 tty->print("%s %s ", esc_names[(int) es], _scalar_replaceable ? "":"NSR"); 80 } 81 tty->print("[["); 82 for (uint i = 0; i < edge_count(); i++) { 83 tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]); 84 } 85 tty->print("]] "); 86 if (_node == NULL) 87 tty->print_cr("<null>"); 88 else 89 _node->dump(); 90 } 91 #endif 92 93 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) : 94 _nodes(C->comp_arena(), C->unique(), C->unique(), PointsToNode()), 95 _processed(C->comp_arena()), 96 _collecting(true), 97 _compile(C), 98 _igvn(igvn), 99 _node_map(C->comp_arena()) { 100 101 _phantom_object = C->top()->_idx, 102 add_node(C->top(), PointsToNode::JavaObject, PointsToNode::GlobalEscape,true); 103 104 // Add ConP(#NULL) and ConN(#NULL) nodes. 105 Node* oop_null = igvn->zerocon(T_OBJECT); 106 _oop_null = oop_null->_idx; 107 assert(_oop_null < C->unique(), "should be created already"); 108 add_node(oop_null, PointsToNode::JavaObject, PointsToNode::NoEscape, true); 109 110 if (UseCompressedOops) { 111 Node* noop_null = igvn->zerocon(T_NARROWOOP); 112 _noop_null = noop_null->_idx; 113 assert(_noop_null < C->unique(), "should be created already"); 114 add_node(noop_null, PointsToNode::JavaObject, PointsToNode::NoEscape, true); 115 } 116 } 117 118 void ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) { 119 PointsToNode *f = ptnode_adr(from_i); 120 PointsToNode *t = ptnode_adr(to_i); 121 122 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set"); 123 assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge"); 124 assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge"); 125 f->add_edge(to_i, PointsToNode::PointsToEdge); 126 } 127 128 void ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) { 129 PointsToNode *f = ptnode_adr(from_i); 130 PointsToNode *t = ptnode_adr(to_i); 131 132 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set"); 133 assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of Deferred edge"); 134 assert(t->node_type() == PointsToNode::LocalVar || t->node_type() == PointsToNode::Field, "invalid destination of Deferred edge"); 135 // don't add a self-referential edge, this can occur during removal of 136 // deferred edges 137 if (from_i != to_i) 138 f->add_edge(to_i, PointsToNode::DeferredEdge); 139 } 140 141 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) { 142 const Type *adr_type = phase->type(adr); 143 if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && 144 adr->in(AddPNode::Address)->is_Proj() && 145 adr->in(AddPNode::Address)->in(0)->is_Allocate()) { 146 // We are computing a raw address for a store captured by an Initialize 147 // compute an appropriate address type. AddP cases #3 and #5 (see below). 148 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot); 149 assert(offs != Type::OffsetBot || 150 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(), 151 "offset must be a constant or it is initialization of array"); 152 return offs; 153 } 154 const TypePtr *t_ptr = adr_type->isa_ptr(); 155 assert(t_ptr != NULL, "must be a pointer type"); 156 return t_ptr->offset(); 157 } 158 159 void ConnectionGraph::add_field_edge(uint from_i, uint to_i, int offset) { 160 PointsToNode *f = ptnode_adr(from_i); 161 PointsToNode *t = ptnode_adr(to_i); 162 163 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set"); 164 assert(f->node_type() == PointsToNode::JavaObject, "invalid destination of Field edge"); 165 assert(t->node_type() == PointsToNode::Field, "invalid destination of Field edge"); 166 assert (t->offset() == -1 || t->offset() == offset, "conflicting field offsets"); 167 t->set_offset(offset); 168 169 f->add_edge(to_i, PointsToNode::FieldEdge); 170 } 171 172 void ConnectionGraph::set_escape_state(uint ni, PointsToNode::EscapeState es) { 173 PointsToNode *npt = ptnode_adr(ni); 174 PointsToNode::EscapeState old_es = npt->escape_state(); 175 if (es > old_es) 176 npt->set_escape_state(es); 177 } 178 179 void ConnectionGraph::add_node(Node *n, PointsToNode::NodeType nt, 180 PointsToNode::EscapeState es, bool done) { 181 PointsToNode* ptadr = ptnode_adr(n->_idx); 182 ptadr->_node = n; 183 ptadr->set_node_type(nt); 184 185 // inline set_escape_state(idx, es); 186 PointsToNode::EscapeState old_es = ptadr->escape_state(); 187 if (es > old_es) 188 ptadr->set_escape_state(es); 189 190 if (done) 191 _processed.set(n->_idx); 192 } 193 194 PointsToNode::EscapeState ConnectionGraph::escape_state(Node *n) { 195 uint idx = n->_idx; 196 PointsToNode::EscapeState es; 197 198 // If we are still collecting or there were no non-escaping allocations 199 // we don't know the answer yet 200 if (_collecting) 201 return PointsToNode::UnknownEscape; 202 203 // if the node was created after the escape computation, return 204 // UnknownEscape 205 if (idx >= nodes_size()) 206 return PointsToNode::UnknownEscape; 207 208 es = ptnode_adr(idx)->escape_state(); 209 210 // if we have already computed a value, return it 211 if (es != PointsToNode::UnknownEscape && 212 ptnode_adr(idx)->node_type() == PointsToNode::JavaObject) 213 return es; 214 215 // PointsTo() calls n->uncast() which can return a new ideal node. 216 if (n->uncast()->_idx >= nodes_size()) 217 return PointsToNode::UnknownEscape; 218 219 PointsToNode::EscapeState orig_es = es; 220 221 // compute max escape state of anything this node could point to 222 VectorSet ptset(Thread::current()->resource_area()); 223 PointsTo(ptset, n); 224 for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) { 225 uint pt = i.elem; 226 PointsToNode::EscapeState pes = ptnode_adr(pt)->escape_state(); 227 if (pes > es) 228 es = pes; 229 } 230 if (orig_es != es) { 231 // cache the computed escape state 232 assert(es != PointsToNode::UnknownEscape, "should have computed an escape state"); 233 ptnode_adr(idx)->set_escape_state(es); 234 } // orig_es could be PointsToNode::UnknownEscape 235 return es; 236 } 237 238 void ConnectionGraph::PointsTo(VectorSet &ptset, Node * n) { 239 VectorSet visited(Thread::current()->resource_area()); 240 GrowableArray<uint> worklist; 241 242 #ifdef ASSERT 243 Node *orig_n = n; 244 #endif 245 246 n = n->uncast(); 247 PointsToNode* npt = ptnode_adr(n->_idx); 248 249 // If we have a JavaObject, return just that object 250 if (npt->node_type() == PointsToNode::JavaObject) { 251 ptset.set(n->_idx); 252 return; 253 } 254 #ifdef ASSERT 255 if (npt->_node == NULL) { 256 if (orig_n != n) 257 orig_n->dump(); 258 n->dump(); 259 assert(npt->_node != NULL, "unregistered node"); 260 } 261 #endif 262 worklist.push(n->_idx); 263 while(worklist.length() > 0) { 264 int ni = worklist.pop(); 265 if (visited.test_set(ni)) 266 continue; 267 268 PointsToNode* pn = ptnode_adr(ni); 269 // ensure that all inputs of a Phi have been processed 270 assert(!_collecting || !pn->_node->is_Phi() || _processed.test(ni),""); 271 272 int edges_processed = 0; 273 uint e_cnt = pn->edge_count(); 274 for (uint e = 0; e < e_cnt; e++) { 275 uint etgt = pn->edge_target(e); 276 PointsToNode::EdgeType et = pn->edge_type(e); 277 if (et == PointsToNode::PointsToEdge) { 278 ptset.set(etgt); 279 edges_processed++; 280 } else if (et == PointsToNode::DeferredEdge) { 281 worklist.push(etgt); 282 edges_processed++; 283 } else { 284 assert(false,"neither PointsToEdge or DeferredEdge"); 285 } 286 } 287 if (edges_processed == 0) { 288 // no deferred or pointsto edges found. Assume the value was set 289 // outside this method. Add the phantom object to the pointsto set. 290 ptset.set(_phantom_object); 291 } 292 } 293 } 294 295 void ConnectionGraph::remove_deferred(uint ni, GrowableArray<uint>* deferred_edges, VectorSet* visited) { 296 // This method is most expensive during ConnectionGraph construction. 297 // Reuse vectorSet and an additional growable array for deferred edges. 298 deferred_edges->clear(); 299 visited->Clear(); 300 301 visited->set(ni); 302 PointsToNode *ptn = ptnode_adr(ni); 303 304 // Mark current edges as visited and move deferred edges to separate array. 305 for (uint i = 0; i < ptn->edge_count(); ) { 306 uint t = ptn->edge_target(i); 307 #ifdef ASSERT 308 assert(!visited->test_set(t), "expecting no duplications"); 309 #else 310 visited->set(t); 311 #endif 312 if (ptn->edge_type(i) == PointsToNode::DeferredEdge) { 313 ptn->remove_edge(t, PointsToNode::DeferredEdge); 314 deferred_edges->append(t); 315 } else { 316 i++; 317 } 318 } 319 for (int next = 0; next < deferred_edges->length(); ++next) { 320 uint t = deferred_edges->at(next); 321 PointsToNode *ptt = ptnode_adr(t); 322 uint e_cnt = ptt->edge_count(); 323 for (uint e = 0; e < e_cnt; e++) { 324 uint etgt = ptt->edge_target(e); 325 if (visited->test_set(etgt)) 326 continue; 327 328 PointsToNode::EdgeType et = ptt->edge_type(e); 329 if (et == PointsToNode::PointsToEdge) { 330 add_pointsto_edge(ni, etgt); 331 if(etgt == _phantom_object) { 332 // Special case - field set outside (globally escaping). 333 ptn->set_escape_state(PointsToNode::GlobalEscape); 334 } 335 } else if (et == PointsToNode::DeferredEdge) { 336 deferred_edges->append(etgt); 337 } else { 338 assert(false,"invalid connection graph"); 339 } 340 } 341 } 342 } 343 344 345 // Add an edge to node given by "to_i" from any field of adr_i whose offset 346 // matches "offset" A deferred edge is added if to_i is a LocalVar, and 347 // a pointsto edge is added if it is a JavaObject 348 349 void ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) { 350 PointsToNode* an = ptnode_adr(adr_i); 351 PointsToNode* to = ptnode_adr(to_i); 352 bool deferred = (to->node_type() == PointsToNode::LocalVar); 353 354 for (uint fe = 0; fe < an->edge_count(); fe++) { 355 assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge"); 356 int fi = an->edge_target(fe); 357 PointsToNode* pf = ptnode_adr(fi); 358 int po = pf->offset(); 359 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) { 360 if (deferred) 361 add_deferred_edge(fi, to_i); 362 else 363 add_pointsto_edge(fi, to_i); 364 } 365 } 366 } 367 368 // Add a deferred edge from node given by "from_i" to any field of adr_i 369 // whose offset matches "offset". 370 void ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) { 371 PointsToNode* an = ptnode_adr(adr_i); 372 for (uint fe = 0; fe < an->edge_count(); fe++) { 373 assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge"); 374 int fi = an->edge_target(fe); 375 PointsToNode* pf = ptnode_adr(fi); 376 int po = pf->offset(); 377 if (pf->edge_count() == 0) { 378 // we have not seen any stores to this field, assume it was set outside this method 379 add_pointsto_edge(fi, _phantom_object); 380 } 381 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) { 382 add_deferred_edge(from_i, fi); 383 } 384 } 385 } 386 387 // Helper functions 388 389 static Node* get_addp_base(Node *addp) { 390 assert(addp->is_AddP(), "must be AddP"); 391 // 392 // AddP cases for Base and Address inputs: 393 // case #1. Direct object's field reference: 394 // Allocate 395 // | 396 // Proj #5 ( oop result ) 397 // | 398 // CheckCastPP (cast to instance type) 399 // | | 400 // AddP ( base == address ) 401 // 402 // case #2. Indirect object's field reference: 403 // Phi 404 // | 405 // CastPP (cast to instance type) 406 // | | 407 // AddP ( base == address ) 408 // 409 // case #3. Raw object's field reference for Initialize node: 410 // Allocate 411 // | 412 // Proj #5 ( oop result ) 413 // top | 414 // \ | 415 // AddP ( base == top ) 416 // 417 // case #4. Array's element reference: 418 // {CheckCastPP | CastPP} 419 // | | | 420 // | AddP ( array's element offset ) 421 // | | 422 // AddP ( array's offset ) 423 // 424 // case #5. Raw object's field reference for arraycopy stub call: 425 // The inline_native_clone() case when the arraycopy stub is called 426 // after the allocation before Initialize and CheckCastPP nodes. 427 // Allocate 428 // | 429 // Proj #5 ( oop result ) 430 // | | 431 // AddP ( base == address ) 432 // 433 // case #6. Constant Pool, ThreadLocal, CastX2P or 434 // Raw object's field reference: 435 // {ConP, ThreadLocal, CastX2P, raw Load} 436 // top | 437 // \ | 438 // AddP ( base == top ) 439 // 440 // case #7. Klass's field reference. 441 // LoadKlass 442 // | | 443 // AddP ( base == address ) 444 // 445 // case #8. narrow Klass's field reference. 446 // LoadNKlass 447 // | 448 // DecodeN 449 // | | 450 // AddP ( base == address ) 451 // 452 Node *base = addp->in(AddPNode::Base)->uncast(); 453 if (base->is_top()) { // The AddP case #3 and #6. 454 base = addp->in(AddPNode::Address)->uncast(); 455 while (base->is_AddP()) { 456 // Case #6 (unsafe access) may have several chained AddP nodes. 457 assert(base->in(AddPNode::Base)->is_top(), "expected unsafe access address only"); 458 base = base->in(AddPNode::Address)->uncast(); 459 } 460 assert(base->Opcode() == Op_ConP || base->Opcode() == Op_ThreadLocal || 461 base->Opcode() == Op_CastX2P || base->is_DecodeN() || 462 (base->is_Mem() && base->bottom_type() == TypeRawPtr::NOTNULL) || 463 (base->is_Proj() && base->in(0)->is_Allocate()), "sanity"); 464 } 465 return base; 466 } 467 468 static Node* find_second_addp(Node* addp, Node* n) { 469 assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes"); 470 471 Node* addp2 = addp->raw_out(0); 472 if (addp->outcnt() == 1 && addp2->is_AddP() && 473 addp2->in(AddPNode::Base) == n && 474 addp2->in(AddPNode::Address) == addp) { 475 476 assert(addp->in(AddPNode::Base) == n, "expecting the same base"); 477 // 478 // Find array's offset to push it on worklist first and 479 // as result process an array's element offset first (pushed second) 480 // to avoid CastPP for the array's offset. 481 // Otherwise the inserted CastPP (LocalVar) will point to what 482 // the AddP (Field) points to. Which would be wrong since 483 // the algorithm expects the CastPP has the same point as 484 // as AddP's base CheckCastPP (LocalVar). 485 // 486 // ArrayAllocation 487 // | 488 // CheckCastPP 489 // | 490 // memProj (from ArrayAllocation CheckCastPP) 491 // | || 492 // | || Int (element index) 493 // | || | ConI (log(element size)) 494 // | || | / 495 // | || LShift 496 // | || / 497 // | AddP (array's element offset) 498 // | | 499 // | | ConI (array's offset: #12(32-bits) or #24(64-bits)) 500 // | / / 501 // AddP (array's offset) 502 // | 503 // Load/Store (memory operation on array's element) 504 // 505 return addp2; 506 } 507 return NULL; 508 } 509 510 // 511 // Adjust the type and inputs of an AddP which computes the 512 // address of a field of an instance 513 // 514 bool ConnectionGraph::split_AddP(Node *addp, Node *base, PhaseGVN *igvn) { 515 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr(); 516 assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr"); 517 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr(); 518 if (t == NULL) { 519 // We are computing a raw address for a store captured by an Initialize 520 // compute an appropriate address type (cases #3 and #5). 521 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer"); 522 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation"); 523 intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot); 524 assert(offs != Type::OffsetBot, "offset must be a constant"); 525 t = base_t->add_offset(offs)->is_oopptr(); 526 } 527 int inst_id = base_t->instance_id(); 528 assert(!t->is_known_instance() || t->instance_id() == inst_id, 529 "old type must be non-instance or match new type"); 530 531 // The type 't' could be subclass of 'base_t'. 532 // As result t->offset() could be large then base_t's size and it will 533 // cause the failure in add_offset() with narrow oops since TypeOopPtr() 534 // constructor verifies correctness of the offset. 535 // 536 // It could happened on subclass's branch (from the type profiling 537 // inlining) which was not eliminated during parsing since the exactness 538 // of the allocation type was not propagated to the subclass type check. 539 // 540 // Or the type 't' could be not related to 'base_t' at all. 541 // It could happened when CHA type is different from MDO type on a dead path 542 // (for example, from instanceof check) which is not collapsed during parsing. 543 // 544 // Do nothing for such AddP node and don't process its users since 545 // this code branch will go away. 546 // 547 if (!t->is_known_instance() && 548 !base_t->klass()->is_subtype_of(t->klass())) { 549 return false; // bail out 550 } 551 552 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr(); 553 // Do NOT remove the next line: ensure a new alias index is allocated 554 // for the instance type. Note: C++ will not remove it since the call 555 // has side effect. 556 int alias_idx = _compile->get_alias_index(tinst); 557 igvn->set_type(addp, tinst); 558 // record the allocation in the node map 559 assert(ptnode_adr(addp->_idx)->_node != NULL, "should be registered"); 560 set_map(addp->_idx, get_map(base->_idx)); 561 562 // Set addp's Base and Address to 'base'. 563 Node *abase = addp->in(AddPNode::Base); 564 Node *adr = addp->in(AddPNode::Address); 565 if (adr->is_Proj() && adr->in(0)->is_Allocate() && 566 adr->in(0)->_idx == (uint)inst_id) { 567 // Skip AddP cases #3 and #5. 568 } else { 569 assert(!abase->is_top(), "sanity"); // AddP case #3 570 if (abase != base) { 571 igvn->hash_delete(addp); 572 addp->set_req(AddPNode::Base, base); 573 if (abase == adr) { 574 addp->set_req(AddPNode::Address, base); 575 } else { 576 // AddP case #4 (adr is array's element offset AddP node) 577 #ifdef ASSERT 578 const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr(); 579 assert(adr->is_AddP() && atype != NULL && 580 atype->instance_id() == inst_id, "array's element offset should be processed first"); 581 #endif 582 } 583 igvn->hash_insert(addp); 584 } 585 } 586 // Put on IGVN worklist since at least addp's type was changed above. 587 record_for_optimizer(addp); 588 return true; 589 } 590 591 // 592 // Create a new version of orig_phi if necessary. Returns either the newly 593 // created phi or an existing phi. Sets create_new to indicate wheter a new 594 // phi was created. Cache the last newly created phi in the node map. 595 // 596 PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn, bool &new_created) { 597 Compile *C = _compile; 598 new_created = false; 599 int phi_alias_idx = C->get_alias_index(orig_phi->adr_type()); 600 // nothing to do if orig_phi is bottom memory or matches alias_idx 601 if (phi_alias_idx == alias_idx) { 602 return orig_phi; 603 } 604 // Have we recently created a Phi for this alias index? 605 PhiNode *result = get_map_phi(orig_phi->_idx); 606 if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) { 607 return result; 608 } 609 // Previous check may fail when the same wide memory Phi was split into Phis 610 // for different memory slices. Search all Phis for this region. 611 if (result != NULL) { 612 Node* region = orig_phi->in(0); 613 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 614 Node* phi = region->fast_out(i); 615 if (phi->is_Phi() && 616 C->get_alias_index(phi->as_Phi()->adr_type()) == alias_idx) { 617 assert(phi->_idx >= nodes_size(), "only new Phi per instance memory slice"); 618 return phi->as_Phi(); 619 } 620 } 621 } 622 if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) { 623 if (C->do_escape_analysis() == true && !C->failing()) { 624 // Retry compilation without escape analysis. 625 // If this is the first failure, the sentinel string will "stick" 626 // to the Compile object, and the C2Compiler will see it and retry. 627 C->record_failure(C2Compiler::retry_no_escape_analysis()); 628 } 629 return NULL; 630 } 631 orig_phi_worklist.append_if_missing(orig_phi); 632 const TypePtr *atype = C->get_adr_type(alias_idx); 633 result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype); 634 C->copy_node_notes_to(result, orig_phi); 635 igvn->set_type(result, result->bottom_type()); 636 record_for_optimizer(result); 637 638 debug_only(Node* pn = ptnode_adr(orig_phi->_idx)->_node;) 639 assert(pn == NULL || pn == orig_phi, "wrong node"); 640 set_map(orig_phi->_idx, result); 641 ptnode_adr(orig_phi->_idx)->_node = orig_phi; 642 643 new_created = true; 644 return result; 645 } 646 647 // 648 // Return a new version of Memory Phi "orig_phi" with the inputs having the 649 // specified alias index. 650 // 651 PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn) { 652 653 assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory"); 654 Compile *C = _compile; 655 bool new_phi_created; 656 PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, igvn, new_phi_created); 657 if (!new_phi_created) { 658 return result; 659 } 660 661 GrowableArray<PhiNode *> phi_list; 662 GrowableArray<uint> cur_input; 663 664 PhiNode *phi = orig_phi; 665 uint idx = 1; 666 bool finished = false; 667 while(!finished) { 668 while (idx < phi->req()) { 669 Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, igvn); 670 if (mem != NULL && mem->is_Phi()) { 671 PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, igvn, new_phi_created); 672 if (new_phi_created) { 673 // found an phi for which we created a new split, push current one on worklist and begin 674 // processing new one 675 phi_list.push(phi); 676 cur_input.push(idx); 677 phi = mem->as_Phi(); 678 result = newphi; 679 idx = 1; 680 continue; 681 } else { 682 mem = newphi; 683 } 684 } 685 if (C->failing()) { 686 return NULL; 687 } 688 result->set_req(idx++, mem); 689 } 690 #ifdef ASSERT 691 // verify that the new Phi has an input for each input of the original 692 assert( phi->req() == result->req(), "must have same number of inputs."); 693 assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match"); 694 #endif 695 // Check if all new phi's inputs have specified alias index. 696 // Otherwise use old phi. 697 for (uint i = 1; i < phi->req(); i++) { 698 Node* in = result->in(i); 699 assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond."); 700 } 701 // we have finished processing a Phi, see if there are any more to do 702 finished = (phi_list.length() == 0 ); 703 if (!finished) { 704 phi = phi_list.pop(); 705 idx = cur_input.pop(); 706 PhiNode *prev_result = get_map_phi(phi->_idx); 707 prev_result->set_req(idx++, result); 708 result = prev_result; 709 } 710 } 711 return result; 712 } 713 714 715 // 716 // The next methods are derived from methods in MemNode. 717 // 718 static Node *step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop) { 719 Node *mem = mmem; 720 // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally 721 // means an array I have not precisely typed yet. Do not do any 722 // alias stuff with it any time soon. 723 if( toop->base() != Type::AnyPtr && 724 !(toop->klass() != NULL && 725 toop->klass()->is_java_lang_Object() && 726 toop->offset() == Type::OffsetBot) ) { 727 mem = mmem->memory_at(alias_idx); 728 // Update input if it is progress over what we have now 729 } 730 return mem; 731 } 732 733 // 734 // Move memory users to their memory slices. 735 // 736 void ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis, PhaseGVN *igvn) { 737 Compile* C = _compile; 738 739 const TypePtr* tp = igvn->type(n->in(MemNode::Address))->isa_ptr(); 740 assert(tp != NULL, "ptr type"); 741 int alias_idx = C->get_alias_index(tp); 742 int general_idx = C->get_general_index(alias_idx); 743 744 // Move users first 745 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 746 Node* use = n->fast_out(i); 747 if (use->is_MergeMem()) { 748 MergeMemNode* mmem = use->as_MergeMem(); 749 assert(n == mmem->memory_at(alias_idx), "should be on instance memory slice"); 750 if (n != mmem->memory_at(general_idx) || alias_idx == general_idx) { 751 continue; // Nothing to do 752 } 753 // Replace previous general reference to mem node. 754 uint orig_uniq = C->unique(); 755 Node* m = find_inst_mem(n, general_idx, orig_phis, igvn); 756 assert(orig_uniq == C->unique(), "no new nodes"); 757 mmem->set_memory_at(general_idx, m); 758 --imax; 759 --i; 760 } else if (use->is_MemBar()) { 761 assert(!use->is_Initialize(), "initializing stores should not be moved"); 762 if (use->req() > MemBarNode::Precedent && 763 use->in(MemBarNode::Precedent) == n) { 764 // Don't move related membars. 765 record_for_optimizer(use); 766 continue; 767 } 768 tp = use->as_MemBar()->adr_type()->isa_ptr(); 769 if (tp != NULL && C->get_alias_index(tp) == alias_idx || 770 alias_idx == general_idx) { 771 continue; // Nothing to do 772 } 773 // Move to general memory slice. 774 uint orig_uniq = C->unique(); 775 Node* m = find_inst_mem(n, general_idx, orig_phis, igvn); 776 assert(orig_uniq == C->unique(), "no new nodes"); 777 igvn->hash_delete(use); 778 imax -= use->replace_edge(n, m); 779 igvn->hash_insert(use); 780 record_for_optimizer(use); 781 --i; 782 #ifdef ASSERT 783 } else if (use->is_Mem()) { 784 if (use->Opcode() == Op_StoreCM && use->in(MemNode::OopStore) == n) { 785 // Don't move related cardmark. 786 continue; 787 } 788 // Memory nodes should have new memory input. 789 tp = igvn->type(use->in(MemNode::Address))->isa_ptr(); 790 assert(tp != NULL, "ptr type"); 791 int idx = C->get_alias_index(tp); 792 assert(get_map(use->_idx) != NULL || idx == alias_idx, 793 "Following memory nodes should have new memory input or be on the same memory slice"); 794 } else if (use->is_Phi()) { 795 // Phi nodes should be split and moved already. 796 tp = use->as_Phi()->adr_type()->isa_ptr(); 797 assert(tp != NULL, "ptr type"); 798 int idx = C->get_alias_index(tp); 799 assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice"); 800 } else { 801 use->dump(); 802 assert(false, "should not be here"); 803 #endif 804 } 805 } 806 } 807 808 // 809 // Search memory chain of "mem" to find a MemNode whose address 810 // is the specified alias index. 811 // 812 Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis, PhaseGVN *phase) { 813 if (orig_mem == NULL) 814 return orig_mem; 815 Compile* C = phase->C; 816 const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr(); 817 bool is_instance = (toop != NULL) && toop->is_known_instance(); 818 Node *start_mem = C->start()->proj_out(TypeFunc::Memory); 819 Node *prev = NULL; 820 Node *result = orig_mem; 821 while (prev != result) { 822 prev = result; 823 if (result == start_mem) 824 break; // hit one of our sentinels 825 if (result->is_Mem()) { 826 const Type *at = phase->type(result->in(MemNode::Address)); 827 if (at != Type::TOP) { 828 assert (at->isa_ptr() != NULL, "pointer type required."); 829 int idx = C->get_alias_index(at->is_ptr()); 830 if (idx == alias_idx) 831 break; 832 } 833 result = result->in(MemNode::Memory); 834 } 835 if (!is_instance) 836 continue; // don't search further for non-instance types 837 // skip over a call which does not affect this memory slice 838 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) { 839 Node *proj_in = result->in(0); 840 if (proj_in->is_Allocate() && proj_in->_idx == (uint)toop->instance_id()) { 841 break; // hit one of our sentinels 842 } else if (proj_in->is_Call()) { 843 CallNode *call = proj_in->as_Call(); 844 if (!call->may_modify(toop, phase)) { 845 result = call->in(TypeFunc::Memory); 846 } 847 } else if (proj_in->is_Initialize()) { 848 AllocateNode* alloc = proj_in->as_Initialize()->allocation(); 849 // Stop if this is the initialization for the object instance which 850 // which contains this memory slice, otherwise skip over it. 851 if (alloc == NULL || alloc->_idx != (uint)toop->instance_id()) { 852 result = proj_in->in(TypeFunc::Memory); 853 } 854 } else if (proj_in->is_MemBar()) { 855 result = proj_in->in(TypeFunc::Memory); 856 } 857 } else if (result->is_MergeMem()) { 858 MergeMemNode *mmem = result->as_MergeMem(); 859 result = step_through_mergemem(mmem, alias_idx, toop); 860 if (result == mmem->base_memory()) { 861 // Didn't find instance memory, search through general slice recursively. 862 result = mmem->memory_at(C->get_general_index(alias_idx)); 863 result = find_inst_mem(result, alias_idx, orig_phis, phase); 864 if (C->failing()) { 865 return NULL; 866 } 867 mmem->set_memory_at(alias_idx, result); 868 } 869 } else if (result->is_Phi() && 870 C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) { 871 Node *un = result->as_Phi()->unique_input(phase); 872 if (un != NULL) { 873 orig_phis.append_if_missing(result->as_Phi()); 874 result = un; 875 } else { 876 break; 877 } 878 } else if (result->is_ClearArray()) { 879 if (!ClearArrayNode::step_through(&result, (uint)toop->instance_id(), phase)) { 880 // Can not bypass initialization of the instance 881 // we are looking for. 882 break; 883 } 884 // Otherwise skip it (the call updated 'result' value). 885 } else if (result->Opcode() == Op_SCMemProj) { 886 assert(result->in(0)->is_LoadStore(), "sanity"); 887 const Type *at = phase->type(result->in(0)->in(MemNode::Address)); 888 if (at != Type::TOP) { 889 assert (at->isa_ptr() != NULL, "pointer type required."); 890 int idx = C->get_alias_index(at->is_ptr()); 891 assert(idx != alias_idx, "Object is not scalar replaceable if a LoadStore node access its field"); 892 break; 893 } 894 result = result->in(0)->in(MemNode::Memory); 895 } 896 } 897 if (result->is_Phi()) { 898 PhiNode *mphi = result->as_Phi(); 899 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required"); 900 const TypePtr *t = mphi->adr_type(); 901 if (C->get_alias_index(t) != alias_idx) { 902 // Create a new Phi with the specified alias index type. 903 result = split_memory_phi(mphi, alias_idx, orig_phis, phase); 904 } else if (!is_instance) { 905 // Push all non-instance Phis on the orig_phis worklist to update inputs 906 // during Phase 4 if needed. 907 orig_phis.append_if_missing(mphi); 908 } 909 } 910 // the result is either MemNode, PhiNode, InitializeNode. 911 return result; 912 } 913 914 // 915 // Convert the types of unescaped object to instance types where possible, 916 // propagate the new type information through the graph, and update memory 917 // edges and MergeMem inputs to reflect the new type. 918 // 919 // We start with allocations (and calls which may be allocations) on alloc_worklist. 920 // The processing is done in 4 phases: 921 // 922 // Phase 1: Process possible allocations from alloc_worklist. Create instance 923 // types for the CheckCastPP for allocations where possible. 924 // Propagate the the new types through users as follows: 925 // casts and Phi: push users on alloc_worklist 926 // AddP: cast Base and Address inputs to the instance type 927 // push any AddP users on alloc_worklist and push any memnode 928 // users onto memnode_worklist. 929 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and 930 // search the Memory chain for a store with the appropriate type 931 // address type. If a Phi is found, create a new version with 932 // the appropriate memory slices from each of the Phi inputs. 933 // For stores, process the users as follows: 934 // MemNode: push on memnode_worklist 935 // MergeMem: push on mergemem_worklist 936 // Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice 937 // moving the first node encountered of each instance type to the 938 // the input corresponding to its alias index. 939 // appropriate memory slice. 940 // Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes. 941 // 942 // In the following example, the CheckCastPP nodes are the cast of allocation 943 // results and the allocation of node 29 is unescaped and eligible to be an 944 // instance type. 945 // 946 // We start with: 947 // 948 // 7 Parm #memory 949 // 10 ConI "12" 950 // 19 CheckCastPP "Foo" 951 // 20 AddP _ 19 19 10 Foo+12 alias_index=4 952 // 29 CheckCastPP "Foo" 953 // 30 AddP _ 29 29 10 Foo+12 alias_index=4 954 // 955 // 40 StoreP 25 7 20 ... alias_index=4 956 // 50 StoreP 35 40 30 ... alias_index=4 957 // 60 StoreP 45 50 20 ... alias_index=4 958 // 70 LoadP _ 60 30 ... alias_index=4 959 // 80 Phi 75 50 60 Memory alias_index=4 960 // 90 LoadP _ 80 30 ... alias_index=4 961 // 100 LoadP _ 80 20 ... alias_index=4 962 // 963 // 964 // Phase 1 creates an instance type for node 29 assigning it an instance id of 24 965 // and creating a new alias index for node 30. This gives: 966 // 967 // 7 Parm #memory 968 // 10 ConI "12" 969 // 19 CheckCastPP "Foo" 970 // 20 AddP _ 19 19 10 Foo+12 alias_index=4 971 // 29 CheckCastPP "Foo" iid=24 972 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24 973 // 974 // 40 StoreP 25 7 20 ... alias_index=4 975 // 50 StoreP 35 40 30 ... alias_index=6 976 // 60 StoreP 45 50 20 ... alias_index=4 977 // 70 LoadP _ 60 30 ... alias_index=6 978 // 80 Phi 75 50 60 Memory alias_index=4 979 // 90 LoadP _ 80 30 ... alias_index=6 980 // 100 LoadP _ 80 20 ... alias_index=4 981 // 982 // In phase 2, new memory inputs are computed for the loads and stores, 983 // And a new version of the phi is created. In phase 4, the inputs to 984 // node 80 are updated and then the memory nodes are updated with the 985 // values computed in phase 2. This results in: 986 // 987 // 7 Parm #memory 988 // 10 ConI "12" 989 // 19 CheckCastPP "Foo" 990 // 20 AddP _ 19 19 10 Foo+12 alias_index=4 991 // 29 CheckCastPP "Foo" iid=24 992 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24 993 // 994 // 40 StoreP 25 7 20 ... alias_index=4 995 // 50 StoreP 35 7 30 ... alias_index=6 996 // 60 StoreP 45 40 20 ... alias_index=4 997 // 70 LoadP _ 50 30 ... alias_index=6 998 // 80 Phi 75 40 60 Memory alias_index=4 999 // 120 Phi 75 50 50 Memory alias_index=6 1000 // 90 LoadP _ 120 30 ... alias_index=6 1001 // 100 LoadP _ 80 20 ... alias_index=4 1002 // 1003 void ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) { 1004 GrowableArray<Node *> memnode_worklist; 1005 GrowableArray<PhiNode *> orig_phis; 1006 1007 PhaseGVN *igvn = _igvn; 1008 uint new_index_start = (uint) _compile->num_alias_types(); 1009 Arena* arena = Thread::current()->resource_area(); 1010 VectorSet visited(arena); 1011 VectorSet ptset(arena); 1012 1013 1014 // Phase 1: Process possible allocations from alloc_worklist. 1015 // Create instance types for the CheckCastPP for allocations where possible. 1016 // 1017 // (Note: don't forget to change the order of the second AddP node on 1018 // the alloc_worklist if the order of the worklist processing is changed, 1019 // see the comment in find_second_addp().) 1020 // 1021 while (alloc_worklist.length() != 0) { 1022 Node *n = alloc_worklist.pop(); 1023 uint ni = n->_idx; 1024 const TypeOopPtr* tinst = NULL; 1025 if (n->is_Call()) { 1026 CallNode *alloc = n->as_Call(); 1027 // copy escape information to call node 1028 PointsToNode* ptn = ptnode_adr(alloc->_idx); 1029 PointsToNode::EscapeState es = escape_state(alloc); 1030 // We have an allocation or call which returns a Java object, 1031 // see if it is unescaped. 1032 if (es != PointsToNode::NoEscape || !ptn->_scalar_replaceable) 1033 continue; 1034 1035 // Find CheckCastPP for the allocate or for the return value of a call 1036 n = alloc->result_cast(); 1037 if (n == NULL) { // No uses except Initialize node 1038 if (alloc->is_Allocate()) { 1039 // Set the scalar_replaceable flag for allocation 1040 // so it could be eliminated if it has no uses. 1041 alloc->as_Allocate()->_is_scalar_replaceable = true; 1042 } 1043 continue; 1044 } 1045 if (!n->is_CheckCastPP()) { // not unique CheckCastPP. 1046 assert(!alloc->is_Allocate(), "allocation should have unique type"); 1047 continue; 1048 } 1049 1050 // The inline code for Object.clone() casts the allocation result to 1051 // java.lang.Object and then to the actual type of the allocated 1052 // object. Detect this case and use the second cast. 1053 // Also detect j.l.reflect.Array.newInstance(jobject, jint) case when 1054 // the allocation result is cast to java.lang.Object and then 1055 // to the actual Array type. 1056 if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL 1057 && (alloc->is_AllocateArray() || 1058 igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT)) { 1059 Node *cast2 = NULL; 1060 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1061 Node *use = n->fast_out(i); 1062 if (use->is_CheckCastPP()) { 1063 cast2 = use; 1064 break; 1065 } 1066 } 1067 if (cast2 != NULL) { 1068 n = cast2; 1069 } else { 1070 // Non-scalar replaceable if the allocation type is unknown statically 1071 // (reflection allocation), the object can't be restored during 1072 // deoptimization without precise type. 1073 continue; 1074 } 1075 } 1076 if (alloc->is_Allocate()) { 1077 // Set the scalar_replaceable flag for allocation 1078 // so it could be eliminated. 1079 alloc->as_Allocate()->_is_scalar_replaceable = true; 1080 } 1081 set_escape_state(n->_idx, es); 1082 // in order for an object to be scalar-replaceable, it must be: 1083 // - a direct allocation (not a call returning an object) 1084 // - non-escaping 1085 // - eligible to be a unique type 1086 // - not determined to be ineligible by escape analysis 1087 assert(ptnode_adr(alloc->_idx)->_node != NULL && 1088 ptnode_adr(n->_idx)->_node != NULL, "should be registered"); 1089 set_map(alloc->_idx, n); 1090 set_map(n->_idx, alloc); 1091 const TypeOopPtr *t = igvn->type(n)->isa_oopptr(); 1092 if (t == NULL) 1093 continue; // not a TypeInstPtr 1094 tinst = t->cast_to_exactness(true)->is_oopptr()->cast_to_instance_id(ni); 1095 igvn->hash_delete(n); 1096 igvn->set_type(n, tinst); 1097 n->raise_bottom_type(tinst); 1098 igvn->hash_insert(n); 1099 record_for_optimizer(n); 1100 if (alloc->is_Allocate() && ptn->_scalar_replaceable && 1101 (t->isa_instptr() || t->isa_aryptr())) { 1102 1103 // First, put on the worklist all Field edges from Connection Graph 1104 // which is more accurate then putting immediate users from Ideal Graph. 1105 for (uint e = 0; e < ptn->edge_count(); e++) { 1106 Node *use = ptnode_adr(ptn->edge_target(e))->_node; 1107 assert(ptn->edge_type(e) == PointsToNode::FieldEdge && use->is_AddP(), 1108 "only AddP nodes are Field edges in CG"); 1109 if (use->outcnt() > 0) { // Don't process dead nodes 1110 Node* addp2 = find_second_addp(use, use->in(AddPNode::Base)); 1111 if (addp2 != NULL) { 1112 assert(alloc->is_AllocateArray(),"array allocation was expected"); 1113 alloc_worklist.append_if_missing(addp2); 1114 } 1115 alloc_worklist.append_if_missing(use); 1116 } 1117 } 1118 1119 // An allocation may have an Initialize which has raw stores. Scan 1120 // the users of the raw allocation result and push AddP users 1121 // on alloc_worklist. 1122 Node *raw_result = alloc->proj_out(TypeFunc::Parms); 1123 assert (raw_result != NULL, "must have an allocation result"); 1124 for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) { 1125 Node *use = raw_result->fast_out(i); 1126 if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes 1127 Node* addp2 = find_second_addp(use, raw_result); 1128 if (addp2 != NULL) { 1129 assert(alloc->is_AllocateArray(),"array allocation was expected"); 1130 alloc_worklist.append_if_missing(addp2); 1131 } 1132 alloc_worklist.append_if_missing(use); 1133 } else if (use->is_MemBar()) { 1134 memnode_worklist.append_if_missing(use); 1135 } 1136 } 1137 } 1138 } else if (n->is_AddP()) { 1139 ptset.Clear(); 1140 PointsTo(ptset, get_addp_base(n)); 1141 assert(ptset.Size() == 1, "AddP address is unique"); 1142 uint elem = ptset.getelem(); // Allocation node's index 1143 if (elem == _phantom_object) { 1144 assert(false, "escaped allocation"); 1145 continue; // Assume the value was set outside this method. 1146 } 1147 Node *base = get_map(elem); // CheckCastPP node 1148 if (!split_AddP(n, base, igvn)) continue; // wrong type from dead path 1149 tinst = igvn->type(base)->isa_oopptr(); 1150 } else if (n->is_Phi() || 1151 n->is_CheckCastPP() || 1152 n->is_EncodeP() || 1153 n->is_DecodeN() || 1154 (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) { 1155 if (visited.test_set(n->_idx)) { 1156 assert(n->is_Phi(), "loops only through Phi's"); 1157 continue; // already processed 1158 } 1159 ptset.Clear(); 1160 PointsTo(ptset, n); 1161 if (ptset.Size() == 1) { 1162 uint elem = ptset.getelem(); // Allocation node's index 1163 if (elem == _phantom_object) { 1164 assert(false, "escaped allocation"); 1165 continue; // Assume the value was set outside this method. 1166 } 1167 Node *val = get_map(elem); // CheckCastPP node 1168 TypeNode *tn = n->as_Type(); 1169 tinst = igvn->type(val)->isa_oopptr(); 1170 assert(tinst != NULL && tinst->is_known_instance() && 1171 (uint)tinst->instance_id() == elem , "instance type expected."); 1172 1173 const Type *tn_type = igvn->type(tn); 1174 const TypeOopPtr *tn_t; 1175 if (tn_type->isa_narrowoop()) { 1176 tn_t = tn_type->make_ptr()->isa_oopptr(); 1177 } else { 1178 tn_t = tn_type->isa_oopptr(); 1179 } 1180 1181 if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) { 1182 if (tn_type->isa_narrowoop()) { 1183 tn_type = tinst->make_narrowoop(); 1184 } else { 1185 tn_type = tinst; 1186 } 1187 igvn->hash_delete(tn); 1188 igvn->set_type(tn, tn_type); 1189 tn->set_type(tn_type); 1190 igvn->hash_insert(tn); 1191 record_for_optimizer(n); 1192 } else { 1193 assert(tn_type == TypePtr::NULL_PTR || 1194 tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()), 1195 "unexpected type"); 1196 continue; // Skip dead path with different type 1197 } 1198 } 1199 } else { 1200 debug_only(n->dump();) 1201 assert(false, "EA: unexpected node"); 1202 continue; 1203 } 1204 // push allocation's users on appropriate worklist 1205 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1206 Node *use = n->fast_out(i); 1207 if(use->is_Mem() && use->in(MemNode::Address) == n) { 1208 // Load/store to instance's field 1209 memnode_worklist.append_if_missing(use); 1210 } else if (use->is_MemBar()) { 1211 memnode_worklist.append_if_missing(use); 1212 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes 1213 Node* addp2 = find_second_addp(use, n); 1214 if (addp2 != NULL) { 1215 alloc_worklist.append_if_missing(addp2); 1216 } 1217 alloc_worklist.append_if_missing(use); 1218 } else if (use->is_Phi() || 1219 use->is_CheckCastPP() || 1220 use->is_EncodeP() || 1221 use->is_DecodeN() || 1222 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) { 1223 alloc_worklist.append_if_missing(use); 1224 #ifdef ASSERT 1225 } else if (use->is_Mem()) { 1226 assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path"); 1227 } else if (use->is_MergeMem()) { 1228 assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist"); 1229 } else if (use->is_SafePoint()) { 1230 // Look for MergeMem nodes for calls which reference unique allocation 1231 // (through CheckCastPP nodes) even for debug info. 1232 Node* m = use->in(TypeFunc::Memory); 1233 if (m->is_MergeMem()) { 1234 assert(_mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist"); 1235 } 1236 } else { 1237 uint op = use->Opcode(); 1238 if (!(op == Op_CmpP || op == Op_Conv2B || 1239 op == Op_CastP2X || op == Op_StoreCM || 1240 op == Op_FastLock || op == Op_AryEq || op == Op_StrComp || 1241 op == Op_StrEquals || op == Op_StrIndexOf)) { 1242 n->dump(); 1243 use->dump(); 1244 assert(false, "EA: missing allocation reference path"); 1245 } 1246 #endif 1247 } 1248 } 1249 1250 } 1251 // New alias types were created in split_AddP(). 1252 uint new_index_end = (uint) _compile->num_alias_types(); 1253 1254 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and 1255 // compute new values for Memory inputs (the Memory inputs are not 1256 // actually updated until phase 4.) 1257 if (memnode_worklist.length() == 0) 1258 return; // nothing to do 1259 1260 while (memnode_worklist.length() != 0) { 1261 Node *n = memnode_worklist.pop(); 1262 if (visited.test_set(n->_idx)) 1263 continue; 1264 if (n->is_Phi() || n->is_ClearArray()) { 1265 // we don't need to do anything, but the users must be pushed 1266 } else if (n->is_MemBar()) { // Initialize, MemBar nodes 1267 // we don't need to do anything, but the users must be pushed 1268 n = n->as_MemBar()->proj_out(TypeFunc::Memory); 1269 if (n == NULL) 1270 continue; 1271 } else { 1272 assert(n->is_Mem(), "memory node required."); 1273 Node *addr = n->in(MemNode::Address); 1274 const Type *addr_t = igvn->type(addr); 1275 if (addr_t == Type::TOP) 1276 continue; 1277 assert (addr_t->isa_ptr() != NULL, "pointer type required."); 1278 int alias_idx = _compile->get_alias_index(addr_t->is_ptr()); 1279 assert ((uint)alias_idx < new_index_end, "wrong alias index"); 1280 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn); 1281 if (_compile->failing()) { 1282 return; 1283 } 1284 if (mem != n->in(MemNode::Memory)) { 1285 // We delay the memory edge update since we need old one in 1286 // MergeMem code below when instances memory slices are separated. 1287 debug_only(Node* pn = ptnode_adr(n->_idx)->_node;) 1288 assert(pn == NULL || pn == n, "wrong node"); 1289 set_map(n->_idx, mem); 1290 ptnode_adr(n->_idx)->_node = n; 1291 } 1292 if (n->is_Load()) { 1293 continue; // don't push users 1294 } else if (n->is_LoadStore()) { 1295 // get the memory projection 1296 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1297 Node *use = n->fast_out(i); 1298 if (use->Opcode() == Op_SCMemProj) { 1299 n = use; 1300 break; 1301 } 1302 } 1303 assert(n->Opcode() == Op_SCMemProj, "memory projection required"); 1304 } 1305 } 1306 // push user on appropriate worklist 1307 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1308 Node *use = n->fast_out(i); 1309 if (use->is_Phi() || use->is_ClearArray()) { 1310 memnode_worklist.append_if_missing(use); 1311 } else if(use->is_Mem() && use->in(MemNode::Memory) == n) { 1312 if (use->Opcode() == Op_StoreCM) // Ignore cardmark stores 1313 continue; 1314 memnode_worklist.append_if_missing(use); 1315 } else if (use->is_MemBar()) { 1316 memnode_worklist.append_if_missing(use); 1317 #ifdef ASSERT 1318 } else if(use->is_Mem()) { 1319 assert(use->in(MemNode::Memory) != n, "EA: missing memory path"); 1320 } else if (use->is_MergeMem()) { 1321 assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist"); 1322 } else { 1323 uint op = use->Opcode(); 1324 if (!(op == Op_StoreCM || 1325 (op == Op_CallLeaf && use->as_CallLeaf()->_name != NULL && 1326 strcmp(use->as_CallLeaf()->_name, "g1_wb_pre") == 0) || 1327 op == Op_AryEq || op == Op_StrComp || 1328 op == Op_StrEquals || op == Op_StrIndexOf)) { 1329 n->dump(); 1330 use->dump(); 1331 assert(false, "EA: missing memory path"); 1332 } 1333 #endif 1334 } 1335 } 1336 } 1337 1338 // Phase 3: Process MergeMem nodes from mergemem_worklist. 1339 // Walk each memory slice moving the first node encountered of each 1340 // instance type to the the input corresponding to its alias index. 1341 uint length = _mergemem_worklist.length(); 1342 for( uint next = 0; next < length; ++next ) { 1343 MergeMemNode* nmm = _mergemem_worklist.at(next); 1344 assert(!visited.test_set(nmm->_idx), "should not be visited before"); 1345 // Note: we don't want to use MergeMemStream here because we only want to 1346 // scan inputs which exist at the start, not ones we add during processing. 1347 // Note 2: MergeMem may already contains instance memory slices added 1348 // during find_inst_mem() call when memory nodes were processed above. 1349 igvn->hash_delete(nmm); 1350 uint nslices = nmm->req(); 1351 for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) { 1352 Node* mem = nmm->in(i); 1353 Node* cur = NULL; 1354 if (mem == NULL || mem->is_top()) 1355 continue; 1356 // First, update mergemem by moving memory nodes to corresponding slices 1357 // if their type became more precise since this mergemem was created. 1358 while (mem->is_Mem()) { 1359 const Type *at = igvn->type(mem->in(MemNode::Address)); 1360 if (at != Type::TOP) { 1361 assert (at->isa_ptr() != NULL, "pointer type required."); 1362 uint idx = (uint)_compile->get_alias_index(at->is_ptr()); 1363 if (idx == i) { 1364 if (cur == NULL) 1365 cur = mem; 1366 } else { 1367 if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) { 1368 nmm->set_memory_at(idx, mem); 1369 } 1370 } 1371 } 1372 mem = mem->in(MemNode::Memory); 1373 } 1374 nmm->set_memory_at(i, (cur != NULL) ? cur : mem); 1375 // Find any instance of the current type if we haven't encountered 1376 // already a memory slice of the instance along the memory chain. 1377 for (uint ni = new_index_start; ni < new_index_end; ni++) { 1378 if((uint)_compile->get_general_index(ni) == i) { 1379 Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni); 1380 if (nmm->is_empty_memory(m)) { 1381 Node* result = find_inst_mem(mem, ni, orig_phis, igvn); 1382 if (_compile->failing()) { 1383 return; 1384 } 1385 nmm->set_memory_at(ni, result); 1386 } 1387 } 1388 } 1389 } 1390 // Find the rest of instances values 1391 for (uint ni = new_index_start; ni < new_index_end; ni++) { 1392 const TypeOopPtr *tinst = _compile->get_adr_type(ni)->isa_oopptr(); 1393 Node* result = step_through_mergemem(nmm, ni, tinst); 1394 if (result == nmm->base_memory()) { 1395 // Didn't find instance memory, search through general slice recursively. 1396 result = nmm->memory_at(_compile->get_general_index(ni)); 1397 result = find_inst_mem(result, ni, orig_phis, igvn); 1398 if (_compile->failing()) { 1399 return; 1400 } 1401 nmm->set_memory_at(ni, result); 1402 } 1403 } 1404 igvn->hash_insert(nmm); 1405 record_for_optimizer(nmm); 1406 } 1407 1408 // Phase 4: Update the inputs of non-instance memory Phis and 1409 // the Memory input of memnodes 1410 // First update the inputs of any non-instance Phi's from 1411 // which we split out an instance Phi. Note we don't have 1412 // to recursively process Phi's encounted on the input memory 1413 // chains as is done in split_memory_phi() since they will 1414 // also be processed here. 1415 for (int j = 0; j < orig_phis.length(); j++) { 1416 PhiNode *phi = orig_phis.at(j); 1417 int alias_idx = _compile->get_alias_index(phi->adr_type()); 1418 igvn->hash_delete(phi); 1419 for (uint i = 1; i < phi->req(); i++) { 1420 Node *mem = phi->in(i); 1421 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn); 1422 if (_compile->failing()) { 1423 return; 1424 } 1425 if (mem != new_mem) { 1426 phi->set_req(i, new_mem); 1427 } 1428 } 1429 igvn->hash_insert(phi); 1430 record_for_optimizer(phi); 1431 } 1432 1433 // Update the memory inputs of MemNodes with the value we computed 1434 // in Phase 2 and move stores memory users to corresponding memory slices. 1435 #ifdef ASSERT 1436 visited.Clear(); 1437 Node_Stack old_mems(arena, _compile->unique() >> 2); 1438 #endif 1439 for (uint i = 0; i < nodes_size(); i++) { 1440 Node *nmem = get_map(i); 1441 if (nmem != NULL) { 1442 Node *n = ptnode_adr(i)->_node; 1443 assert(n != NULL, "sanity"); 1444 if (n->is_Mem()) { 1445 #ifdef ASSERT 1446 Node* old_mem = n->in(MemNode::Memory); 1447 if (!visited.test_set(old_mem->_idx)) { 1448 old_mems.push(old_mem, old_mem->outcnt()); 1449 } 1450 #endif 1451 assert(n->in(MemNode::Memory) != nmem, "sanity"); 1452 if (!n->is_Load()) { 1453 // Move memory users of a store first. 1454 move_inst_mem(n, orig_phis, igvn); 1455 } 1456 // Now update memory input 1457 igvn->hash_delete(n); 1458 n->set_req(MemNode::Memory, nmem); 1459 igvn->hash_insert(n); 1460 record_for_optimizer(n); 1461 } else { 1462 assert(n->is_Allocate() || n->is_CheckCastPP() || 1463 n->is_AddP() || n->is_Phi(), "unknown node used for set_map()"); 1464 } 1465 } 1466 } 1467 #ifdef ASSERT 1468 // Verify that memory was split correctly 1469 while (old_mems.is_nonempty()) { 1470 Node* old_mem = old_mems.node(); 1471 uint old_cnt = old_mems.index(); 1472 old_mems.pop(); 1473 assert(old_cnt = old_mem->outcnt(), "old mem could be lost"); 1474 } 1475 #endif 1476 } 1477 1478 bool ConnectionGraph::has_candidates(Compile *C) { 1479 // EA brings benefits only when the code has allocations and/or locks which 1480 // are represented by ideal Macro nodes. 1481 int cnt = C->macro_count(); 1482 for( int i=0; i < cnt; i++ ) { 1483 Node *n = C->macro_node(i); 1484 if ( n->is_Allocate() ) 1485 return true; 1486 if( n->is_Lock() ) { 1487 Node* obj = n->as_Lock()->obj_node()->uncast(); 1488 if( !(obj->is_Parm() || obj->is_Con()) ) 1489 return true; 1490 } 1491 } 1492 return false; 1493 } 1494 1495 void ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) { 1496 // Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction 1497 // to create space for them in ConnectionGraph::_nodes[]. 1498 Node* oop_null = igvn->zerocon(T_OBJECT); 1499 Node* noop_null = igvn->zerocon(T_NARROWOOP); 1500 1501 ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn); 1502 // Perform escape analysis 1503 if (congraph->compute_escape()) { 1504 // There are non escaping objects. 1505 C->set_congraph(congraph); 1506 } 1507 1508 // Cleanup. 1509 if (oop_null->outcnt() == 0) 1510 igvn->hash_delete(oop_null); 1511 if (noop_null->outcnt() == 0) 1512 igvn->hash_delete(noop_null); 1513 } 1514 1515 bool ConnectionGraph::compute_escape() { 1516 Compile* C = _compile; 1517 1518 // 1. Populate Connection Graph (CG) with Ideal nodes. 1519 1520 Unique_Node_List worklist_init; 1521 worklist_init.map(C->unique(), NULL); // preallocate space 1522 1523 // Initialize worklist 1524 if (C->root() != NULL) { 1525 worklist_init.push(C->root()); 1526 } 1527 1528 GrowableArray<int> cg_worklist; 1529 PhaseGVN* igvn = _igvn; 1530 bool has_allocations = false; 1531 1532 // Push all useful nodes onto CG list and set their type. 1533 for( uint next = 0; next < worklist_init.size(); ++next ) { 1534 Node* n = worklist_init.at(next); 1535 record_for_escape_analysis(n, igvn); 1536 // Only allocations and java static calls results are checked 1537 // for an escape status. See process_call_result() below. 1538 if (n->is_Allocate() || n->is_CallStaticJava() && 1539 ptnode_adr(n->_idx)->node_type() == PointsToNode::JavaObject) { 1540 has_allocations = true; 1541 } 1542 if(n->is_AddP()) { 1543 // Collect address nodes which directly reference an allocation. 1544 // Use them during stage 3 below to build initial connection graph 1545 // field edges. Other field edges could be added after StoreP/LoadP 1546 // nodes are processed during stage 4 below. 1547 Node* base = get_addp_base(n); 1548 if(base->is_Proj() && base->in(0)->is_Allocate()) { 1549 cg_worklist.append(n->_idx); 1550 } 1551 } else if (n->is_MergeMem()) { 1552 // Collect all MergeMem nodes to add memory slices for 1553 // scalar replaceable objects in split_unique_types(). 1554 _mergemem_worklist.append(n->as_MergeMem()); 1555 } 1556 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1557 Node* m = n->fast_out(i); // Get user 1558 worklist_init.push(m); 1559 } 1560 } 1561 1562 if (!has_allocations) { 1563 _collecting = false; 1564 return false; // Nothing to do. 1565 } 1566 1567 // 2. First pass to create simple CG edges (doesn't require to walk CG). 1568 uint delayed_size = _delayed_worklist.size(); 1569 for( uint next = 0; next < delayed_size; ++next ) { 1570 Node* n = _delayed_worklist.at(next); 1571 build_connection_graph(n, igvn); 1572 } 1573 1574 // 3. Pass to create fields edges (Allocate -F-> AddP). 1575 uint cg_length = cg_worklist.length(); 1576 for( uint next = 0; next < cg_length; ++next ) { 1577 int ni = cg_worklist.at(next); 1578 build_connection_graph(ptnode_adr(ni)->_node, igvn); 1579 } 1580 1581 cg_worklist.clear(); 1582 cg_worklist.append(_phantom_object); 1583 1584 // 4. Build Connection Graph which need 1585 // to walk the connection graph. 1586 for (uint ni = 0; ni < nodes_size(); ni++) { 1587 PointsToNode* ptn = ptnode_adr(ni); 1588 Node *n = ptn->_node; 1589 if (n != NULL) { // Call, AddP, LoadP, StoreP 1590 build_connection_graph(n, igvn); 1591 if (ptn->node_type() != PointsToNode::UnknownType) 1592 cg_worklist.append(n->_idx); // Collect CG nodes 1593 } 1594 } 1595 1596 Arena* arena = Thread::current()->resource_area(); 1597 VectorSet ptset(arena); 1598 GrowableArray<uint> deferred_edges; 1599 VectorSet visited(arena); 1600 1601 // 5. Remove deferred edges from the graph and adjust 1602 // escape state of nonescaping objects. 1603 cg_length = cg_worklist.length(); 1604 for( uint next = 0; next < cg_length; ++next ) { 1605 int ni = cg_worklist.at(next); 1606 PointsToNode* ptn = ptnode_adr(ni); 1607 PointsToNode::NodeType nt = ptn->node_type(); 1608 if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) { 1609 remove_deferred(ni, &deferred_edges, &visited); 1610 Node *n = ptn->_node; 1611 if (n->is_AddP()) { 1612 // Search for objects which are not scalar replaceable 1613 // and adjust their escape state. 1614 verify_escape_state(ni, ptset, igvn); 1615 } 1616 } 1617 } 1618 1619 // 6. Propagate escape states. 1620 GrowableArray<int> worklist; 1621 bool has_non_escaping_obj = false; 1622 1623 // push all GlobalEscape nodes on the worklist 1624 for( uint next = 0; next < cg_length; ++next ) { 1625 int nk = cg_worklist.at(next); 1626 if (ptnode_adr(nk)->escape_state() == PointsToNode::GlobalEscape) 1627 worklist.push(nk); 1628 } 1629 // mark all nodes reachable from GlobalEscape nodes 1630 while(worklist.length() > 0) { 1631 PointsToNode* ptn = ptnode_adr(worklist.pop()); 1632 uint e_cnt = ptn->edge_count(); 1633 for (uint ei = 0; ei < e_cnt; ei++) { 1634 uint npi = ptn->edge_target(ei); 1635 PointsToNode *np = ptnode_adr(npi); 1636 if (np->escape_state() < PointsToNode::GlobalEscape) { 1637 np->set_escape_state(PointsToNode::GlobalEscape); 1638 worklist.push(npi); 1639 } 1640 } 1641 } 1642 1643 // push all ArgEscape nodes on the worklist 1644 for( uint next = 0; next < cg_length; ++next ) { 1645 int nk = cg_worklist.at(next); 1646 if (ptnode_adr(nk)->escape_state() == PointsToNode::ArgEscape) 1647 worklist.push(nk); 1648 } 1649 // mark all nodes reachable from ArgEscape nodes 1650 while(worklist.length() > 0) { 1651 PointsToNode* ptn = ptnode_adr(worklist.pop()); 1652 if (ptn->node_type() == PointsToNode::JavaObject) 1653 has_non_escaping_obj = true; // Non GlobalEscape 1654 uint e_cnt = ptn->edge_count(); 1655 for (uint ei = 0; ei < e_cnt; ei++) { 1656 uint npi = ptn->edge_target(ei); 1657 PointsToNode *np = ptnode_adr(npi); 1658 if (np->escape_state() < PointsToNode::ArgEscape) { 1659 np->set_escape_state(PointsToNode::ArgEscape); 1660 worklist.push(npi); 1661 } 1662 } 1663 } 1664 1665 GrowableArray<Node*> alloc_worklist; 1666 1667 // push all NoEscape nodes on the worklist 1668 for( uint next = 0; next < cg_length; ++next ) { 1669 int nk = cg_worklist.at(next); 1670 if (ptnode_adr(nk)->escape_state() == PointsToNode::NoEscape) 1671 worklist.push(nk); 1672 } 1673 // mark all nodes reachable from NoEscape nodes 1674 while(worklist.length() > 0) { 1675 PointsToNode* ptn = ptnode_adr(worklist.pop()); 1676 if (ptn->node_type() == PointsToNode::JavaObject) 1677 has_non_escaping_obj = true; // Non GlobalEscape 1678 Node* n = ptn->_node; 1679 if (n->is_Allocate() && ptn->_scalar_replaceable ) { 1680 // Push scalar replaceable allocations on alloc_worklist 1681 // for processing in split_unique_types(). 1682 alloc_worklist.append(n); 1683 } 1684 uint e_cnt = ptn->edge_count(); 1685 for (uint ei = 0; ei < e_cnt; ei++) { 1686 uint npi = ptn->edge_target(ei); 1687 PointsToNode *np = ptnode_adr(npi); 1688 if (np->escape_state() < PointsToNode::NoEscape) { 1689 np->set_escape_state(PointsToNode::NoEscape); 1690 worklist.push(npi); 1691 } 1692 } 1693 } 1694 1695 _collecting = false; 1696 assert(C->unique() == nodes_size(), "there should be no new ideal nodes during ConnectionGraph build"); 1697 1698 #ifndef PRODUCT 1699 if (PrintEscapeAnalysis) { 1700 dump(); // Dump ConnectionGraph 1701 } 1702 #endif 1703 1704 bool has_scalar_replaceable_candidates = alloc_worklist.length() > 0; 1705 if ( has_scalar_replaceable_candidates && 1706 C->AliasLevel() >= 3 && EliminateAllocations ) { 1707 1708 // Now use the escape information to create unique types for 1709 // scalar replaceable objects. 1710 split_unique_types(alloc_worklist); 1711 1712 if (C->failing()) return false; 1713 1714 C->print_method("After Escape Analysis", 2); 1715 1716 #ifdef ASSERT 1717 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) { 1718 tty->print("=== No allocations eliminated for "); 1719 C->method()->print_short_name(); 1720 if(!EliminateAllocations) { 1721 tty->print(" since EliminateAllocations is off ==="); 1722 } else if(!has_scalar_replaceable_candidates) { 1723 tty->print(" since there are no scalar replaceable candidates ==="); 1724 } else if(C->AliasLevel() < 3) { 1725 tty->print(" since AliasLevel < 3 ==="); 1726 } 1727 tty->cr(); 1728 #endif 1729 } 1730 return has_non_escaping_obj; 1731 } 1732 1733 // Search for objects which are not scalar replaceable. 1734 void ConnectionGraph::verify_escape_state(int nidx, VectorSet& ptset, PhaseTransform* phase) { 1735 PointsToNode* ptn = ptnode_adr(nidx); 1736 Node* n = ptn->_node; 1737 assert(n->is_AddP(), "Should be called for AddP nodes only"); 1738 // Search for objects which are not scalar replaceable. 1739 // Mark their escape state as ArgEscape to propagate the state 1740 // to referenced objects. 1741 // Note: currently there are no difference in compiler optimizations 1742 // for ArgEscape objects and NoEscape objects which are not 1743 // scalar replaceable. 1744 1745 Compile* C = _compile; 1746 1747 int offset = ptn->offset(); 1748 Node* base = get_addp_base(n); 1749 ptset.Clear(); 1750 PointsTo(ptset, base); 1751 int ptset_size = ptset.Size(); 1752 1753 // Check if a oop field's initializing value is recorded and add 1754 // a corresponding NULL field's value if it is not recorded. 1755 // Connection Graph does not record a default initialization by NULL 1756 // captured by Initialize node. 1757 // 1758 // Note: it will disable scalar replacement in some cases: 1759 // 1760 // Point p[] = new Point[1]; 1761 // p[0] = new Point(); // Will be not scalar replaced 1762 // 1763 // but it will save us from incorrect optimizations in next cases: 1764 // 1765 // Point p[] = new Point[1]; 1766 // if ( x ) p[0] = new Point(); // Will be not scalar replaced 1767 // 1768 // Do a simple control flow analysis to distinguish above cases. 1769 // 1770 if (offset != Type::OffsetBot && ptset_size == 1) { 1771 uint elem = ptset.getelem(); // Allocation node's index 1772 // It does not matter if it is not Allocation node since 1773 // only non-escaping allocations are scalar replaced. 1774 if (ptnode_adr(elem)->_node->is_Allocate() && 1775 ptnode_adr(elem)->escape_state() == PointsToNode::NoEscape) { 1776 AllocateNode* alloc = ptnode_adr(elem)->_node->as_Allocate(); 1777 InitializeNode* ini = alloc->initialization(); 1778 1779 // Check only oop fields. 1780 const Type* adr_type = n->as_AddP()->bottom_type(); 1781 BasicType basic_field_type = T_INT; 1782 if (adr_type->isa_instptr()) { 1783 ciField* field = C->alias_type(adr_type->isa_instptr())->field(); 1784 if (field != NULL) { 1785 basic_field_type = field->layout_type(); 1786 } else { 1787 // Ignore non field load (for example, klass load) 1788 } 1789 } else if (adr_type->isa_aryptr()) { 1790 const Type* elemtype = adr_type->isa_aryptr()->elem(); 1791 basic_field_type = elemtype->array_element_basic_type(); 1792 } else { 1793 // Raw pointers are used for initializing stores so skip it. 1794 assert(adr_type->isa_rawptr() && base->is_Proj() && 1795 (base->in(0) == alloc),"unexpected pointer type"); 1796 } 1797 if (basic_field_type == T_OBJECT || 1798 basic_field_type == T_NARROWOOP || 1799 basic_field_type == T_ARRAY) { 1800 Node* value = NULL; 1801 if (ini != NULL) { 1802 BasicType ft = UseCompressedOops ? T_NARROWOOP : T_OBJECT; 1803 Node* store = ini->find_captured_store(offset, type2aelembytes(ft), phase); 1804 if (store != NULL && store->is_Store()) { 1805 value = store->in(MemNode::ValueIn); 1806 } else if (ptn->edge_count() > 0) { // Are there oop stores? 1807 // Check for a store which follows allocation without branches. 1808 // For example, a volatile field store is not collected 1809 // by Initialize node. TODO: it would be nice to use idom() here. 1810 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1811 store = n->fast_out(i); 1812 if (store->is_Store() && store->in(0) != NULL) { 1813 Node* ctrl = store->in(0); 1814 while(!(ctrl == ini || ctrl == alloc || ctrl == NULL || 1815 ctrl == C->root() || ctrl == C->top() || ctrl->is_Region() || 1816 ctrl->is_IfTrue() || ctrl->is_IfFalse())) { 1817 ctrl = ctrl->in(0); 1818 } 1819 if (ctrl == ini || ctrl == alloc) { 1820 value = store->in(MemNode::ValueIn); 1821 break; 1822 } 1823 } 1824 } 1825 } 1826 } 1827 if (value == NULL || value != ptnode_adr(value->_idx)->_node) { 1828 // A field's initializing value was not recorded. Add NULL. 1829 uint null_idx = UseCompressedOops ? _noop_null : _oop_null; 1830 add_pointsto_edge(nidx, null_idx); 1831 } 1832 } 1833 } 1834 } 1835 1836 // An object is not scalar replaceable if the field which may point 1837 // to it has unknown offset (unknown element of an array of objects). 1838 // 1839 if (offset == Type::OffsetBot) { 1840 uint e_cnt = ptn->edge_count(); 1841 for (uint ei = 0; ei < e_cnt; ei++) { 1842 uint npi = ptn->edge_target(ei); 1843 set_escape_state(npi, PointsToNode::ArgEscape); 1844 ptnode_adr(npi)->_scalar_replaceable = false; 1845 } 1846 } 1847 1848 // Currently an object is not scalar replaceable if a LoadStore node 1849 // access its field since the field value is unknown after it. 1850 // 1851 bool has_LoadStore = false; 1852 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1853 Node *use = n->fast_out(i); 1854 if (use->is_LoadStore()) { 1855 has_LoadStore = true; 1856 break; 1857 } 1858 } 1859 // An object is not scalar replaceable if the address points 1860 // to unknown field (unknown element for arrays, offset is OffsetBot). 1861 // 1862 // Or the address may point to more then one object. This may produce 1863 // the false positive result (set scalar_replaceable to false) 1864 // since the flow-insensitive escape analysis can't separate 1865 // the case when stores overwrite the field's value from the case 1866 // when stores happened on different control branches. 1867 // 1868 if (ptset_size > 1 || ptset_size != 0 && 1869 (has_LoadStore || offset == Type::OffsetBot)) { 1870 for( VectorSetI j(&ptset); j.test(); ++j ) { 1871 set_escape_state(j.elem, PointsToNode::ArgEscape); 1872 ptnode_adr(j.elem)->_scalar_replaceable = false; 1873 } 1874 } 1875 } 1876 1877 void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) { 1878 1879 switch (call->Opcode()) { 1880 #ifdef ASSERT 1881 case Op_Allocate: 1882 case Op_AllocateArray: 1883 case Op_Lock: 1884 case Op_Unlock: 1885 assert(false, "should be done already"); 1886 break; 1887 #endif 1888 case Op_CallLeaf: 1889 case Op_CallLeafNoFP: 1890 { 1891 // Stub calls, objects do not escape but they are not scale replaceable. 1892 // Adjust escape state for outgoing arguments. 1893 const TypeTuple * d = call->tf()->domain(); 1894 VectorSet ptset(Thread::current()->resource_area()); 1895 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 1896 const Type* at = d->field_at(i); 1897 Node *arg = call->in(i)->uncast(); 1898 const Type *aat = phase->type(arg); 1899 if (!arg->is_top() && at->isa_ptr() && aat->isa_ptr() && 1900 ptnode_adr(arg->_idx)->escape_state() < PointsToNode::ArgEscape) { 1901 1902 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR || 1903 aat->isa_ptr() != NULL, "expecting an Ptr"); 1904 #ifdef ASSERT 1905 if (!(call->Opcode() == Op_CallLeafNoFP && 1906 call->as_CallLeaf()->_name != NULL && 1907 (strstr(call->as_CallLeaf()->_name, "arraycopy") != 0) || 1908 call->as_CallLeaf()->_name != NULL && 1909 (strcmp(call->as_CallLeaf()->_name, "g1_wb_pre") == 0 || 1910 strcmp(call->as_CallLeaf()->_name, "g1_wb_post") == 0 )) 1911 ) { 1912 call->dump(); 1913 assert(false, "EA: unexpected CallLeaf"); 1914 } 1915 #endif 1916 set_escape_state(arg->_idx, PointsToNode::ArgEscape); 1917 if (arg->is_AddP()) { 1918 // 1919 // The inline_native_clone() case when the arraycopy stub is called 1920 // after the allocation before Initialize and CheckCastPP nodes. 1921 // 1922 // Set AddP's base (Allocate) as not scalar replaceable since 1923 // pointer to the base (with offset) is passed as argument. 1924 // 1925 arg = get_addp_base(arg); 1926 } 1927 ptset.Clear(); 1928 PointsTo(ptset, arg); 1929 for( VectorSetI j(&ptset); j.test(); ++j ) { 1930 uint pt = j.elem; 1931 set_escape_state(pt, PointsToNode::ArgEscape); 1932 } 1933 } 1934 } 1935 break; 1936 } 1937 1938 case Op_CallStaticJava: 1939 // For a static call, we know exactly what method is being called. 1940 // Use bytecode estimator to record the call's escape affects 1941 { 1942 ciMethod *meth = call->as_CallJava()->method(); 1943 BCEscapeAnalyzer *call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL; 1944 // fall-through if not a Java method or no analyzer information 1945 if (call_analyzer != NULL) { 1946 const TypeTuple * d = call->tf()->domain(); 1947 VectorSet ptset(Thread::current()->resource_area()); 1948 bool copy_dependencies = false; 1949 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 1950 const Type* at = d->field_at(i); 1951 int k = i - TypeFunc::Parms; 1952 Node *arg = call->in(i)->uncast(); 1953 1954 if (at->isa_oopptr() != NULL && 1955 ptnode_adr(arg->_idx)->escape_state() < PointsToNode::GlobalEscape) { 1956 1957 bool global_escapes = false; 1958 bool fields_escapes = false; 1959 if (!call_analyzer->is_arg_stack(k)) { 1960 // The argument global escapes, mark everything it could point to 1961 set_escape_state(arg->_idx, PointsToNode::GlobalEscape); 1962 global_escapes = true; 1963 } else { 1964 if (!call_analyzer->is_arg_local(k)) { 1965 // The argument itself doesn't escape, but any fields might 1966 fields_escapes = true; 1967 } 1968 set_escape_state(arg->_idx, PointsToNode::ArgEscape); 1969 copy_dependencies = true; 1970 } 1971 1972 ptset.Clear(); 1973 PointsTo(ptset, arg); 1974 for( VectorSetI j(&ptset); j.test(); ++j ) { 1975 uint pt = j.elem; 1976 if (global_escapes) { 1977 //The argument global escapes, mark everything it could point to 1978 set_escape_state(pt, PointsToNode::GlobalEscape); 1979 } else { 1980 if (fields_escapes) { 1981 // The argument itself doesn't escape, but any fields might 1982 add_edge_from_fields(pt, _phantom_object, Type::OffsetBot); 1983 } 1984 set_escape_state(pt, PointsToNode::ArgEscape); 1985 } 1986 } 1987 } 1988 } 1989 if (copy_dependencies) 1990 call_analyzer->copy_dependencies(_compile->dependencies()); 1991 break; 1992 } 1993 } 1994 1995 default: 1996 // Fall-through here if not a Java method or no analyzer information 1997 // or some other type of call, assume the worst case: all arguments 1998 // globally escape. 1999 { 2000 // adjust escape state for outgoing arguments 2001 const TypeTuple * d = call->tf()->domain(); 2002 VectorSet ptset(Thread::current()->resource_area()); 2003 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 2004 const Type* at = d->field_at(i); 2005 if (at->isa_oopptr() != NULL) { 2006 Node *arg = call->in(i)->uncast(); 2007 set_escape_state(arg->_idx, PointsToNode::GlobalEscape); 2008 ptset.Clear(); 2009 PointsTo(ptset, arg); 2010 for( VectorSetI j(&ptset); j.test(); ++j ) { 2011 uint pt = j.elem; 2012 set_escape_state(pt, PointsToNode::GlobalEscape); 2013 } 2014 } 2015 } 2016 } 2017 } 2018 } 2019 void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) { 2020 CallNode *call = resproj->in(0)->as_Call(); 2021 uint call_idx = call->_idx; 2022 uint resproj_idx = resproj->_idx; 2023 2024 switch (call->Opcode()) { 2025 case Op_Allocate: 2026 { 2027 Node *k = call->in(AllocateNode::KlassNode); 2028 const TypeKlassPtr *kt = k->bottom_type()->isa_klassptr(); 2029 assert(kt != NULL, "TypeKlassPtr required."); 2030 ciKlass* cik = kt->klass(); 2031 2032 PointsToNode::EscapeState es; 2033 uint edge_to; 2034 if (cik->is_subclass_of(_compile->env()->Thread_klass()) || 2035 !cik->is_instance_klass() || // StressReflectiveCode 2036 cik->as_instance_klass()->has_finalizer()) { 2037 es = PointsToNode::GlobalEscape; 2038 edge_to = _phantom_object; // Could not be worse 2039 } else { 2040 es = PointsToNode::NoEscape; 2041 edge_to = call_idx; 2042 } 2043 set_escape_state(call_idx, es); 2044 add_pointsto_edge(resproj_idx, edge_to); 2045 _processed.set(resproj_idx); 2046 break; 2047 } 2048 2049 case Op_AllocateArray: 2050 { 2051 2052 Node *k = call->in(AllocateNode::KlassNode); 2053 const TypeKlassPtr *kt = k->bottom_type()->isa_klassptr(); 2054 assert(kt != NULL, "TypeKlassPtr required."); 2055 ciKlass* cik = kt->klass(); 2056 2057 PointsToNode::EscapeState es; 2058 uint edge_to; 2059 if (!cik->is_array_klass()) { // StressReflectiveCode 2060 es = PointsToNode::GlobalEscape; 2061 edge_to = _phantom_object; 2062 } else { 2063 es = PointsToNode::NoEscape; 2064 edge_to = call_idx; 2065 int length = call->in(AllocateNode::ALength)->find_int_con(-1); 2066 if (length < 0 || length > EliminateAllocationArraySizeLimit) { 2067 // Not scalar replaceable if the length is not constant or too big. 2068 ptnode_adr(call_idx)->_scalar_replaceable = false; 2069 } 2070 } 2071 set_escape_state(call_idx, es); 2072 add_pointsto_edge(resproj_idx, edge_to); 2073 _processed.set(resproj_idx); 2074 break; 2075 } 2076 2077 case Op_CallStaticJava: 2078 // For a static call, we know exactly what method is being called. 2079 // Use bytecode estimator to record whether the call's return value escapes 2080 { 2081 bool done = true; 2082 const TypeTuple *r = call->tf()->range(); 2083 const Type* ret_type = NULL; 2084 2085 if (r->cnt() > TypeFunc::Parms) 2086 ret_type = r->field_at(TypeFunc::Parms); 2087 2088 // Note: we use isa_ptr() instead of isa_oopptr() here because the 2089 // _multianewarray functions return a TypeRawPtr. 2090 if (ret_type == NULL || ret_type->isa_ptr() == NULL) { 2091 _processed.set(resproj_idx); 2092 break; // doesn't return a pointer type 2093 } 2094 ciMethod *meth = call->as_CallJava()->method(); 2095 const TypeTuple * d = call->tf()->domain(); 2096 if (meth == NULL) { 2097 // not a Java method, assume global escape 2098 set_escape_state(call_idx, PointsToNode::GlobalEscape); 2099 add_pointsto_edge(resproj_idx, _phantom_object); 2100 } else { 2101 BCEscapeAnalyzer *call_analyzer = meth->get_bcea(); 2102 bool copy_dependencies = false; 2103 2104 if (call_analyzer->is_return_allocated()) { 2105 // Returns a newly allocated unescaped object, simply 2106 // update dependency information. 2107 // Mark it as NoEscape so that objects referenced by 2108 // it's fields will be marked as NoEscape at least. 2109 set_escape_state(call_idx, PointsToNode::NoEscape); 2110 add_pointsto_edge(resproj_idx, call_idx); 2111 copy_dependencies = true; 2112 } else if (call_analyzer->is_return_local()) { 2113 // determine whether any arguments are returned 2114 set_escape_state(call_idx, PointsToNode::NoEscape); 2115 bool ret_arg = false; 2116 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 2117 const Type* at = d->field_at(i); 2118 2119 if (at->isa_oopptr() != NULL) { 2120 Node *arg = call->in(i)->uncast(); 2121 2122 if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) { 2123 ret_arg = true; 2124 PointsToNode *arg_esp = ptnode_adr(arg->_idx); 2125 if (arg_esp->node_type() == PointsToNode::UnknownType) 2126 done = false; 2127 else if (arg_esp->node_type() == PointsToNode::JavaObject) 2128 add_pointsto_edge(resproj_idx, arg->_idx); 2129 else 2130 add_deferred_edge(resproj_idx, arg->_idx); 2131 arg_esp->_hidden_alias = true; 2132 } 2133 } 2134 } 2135 if (done && !ret_arg) { 2136 // Returns unknown object. 2137 set_escape_state(call_idx, PointsToNode::GlobalEscape); 2138 add_pointsto_edge(resproj_idx, _phantom_object); 2139 } 2140 copy_dependencies = true; 2141 } else { 2142 set_escape_state(call_idx, PointsToNode::GlobalEscape); 2143 add_pointsto_edge(resproj_idx, _phantom_object); 2144 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) { 2145 const Type* at = d->field_at(i); 2146 if (at->isa_oopptr() != NULL) { 2147 Node *arg = call->in(i)->uncast(); 2148 PointsToNode *arg_esp = ptnode_adr(arg->_idx); 2149 arg_esp->_hidden_alias = true; 2150 } 2151 } 2152 } 2153 if (copy_dependencies) 2154 call_analyzer->copy_dependencies(_compile->dependencies()); 2155 } 2156 if (done) 2157 _processed.set(resproj_idx); 2158 break; 2159 } 2160 2161 default: 2162 // Some other type of call, assume the worst case that the 2163 // returned value, if any, globally escapes. 2164 { 2165 const TypeTuple *r = call->tf()->range(); 2166 if (r->cnt() > TypeFunc::Parms) { 2167 const Type* ret_type = r->field_at(TypeFunc::Parms); 2168 2169 // Note: we use isa_ptr() instead of isa_oopptr() here because the 2170 // _multianewarray functions return a TypeRawPtr. 2171 if (ret_type->isa_ptr() != NULL) { 2172 set_escape_state(call_idx, PointsToNode::GlobalEscape); 2173 add_pointsto_edge(resproj_idx, _phantom_object); 2174 } 2175 } 2176 _processed.set(resproj_idx); 2177 } 2178 } 2179 } 2180 2181 // Populate Connection Graph with Ideal nodes and create simple 2182 // connection graph edges (do not need to check the node_type of inputs 2183 // or to call PointsTo() to walk the connection graph). 2184 void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) { 2185 if (_processed.test(n->_idx)) 2186 return; // No need to redefine node's state. 2187 2188 if (n->is_Call()) { 2189 // Arguments to allocation and locking don't escape. 2190 if (n->is_Allocate()) { 2191 add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true); 2192 record_for_optimizer(n); 2193 } else if (n->is_Lock() || n->is_Unlock()) { 2194 // Put Lock and Unlock nodes on IGVN worklist to process them during 2195 // the first IGVN optimization when escape information is still available. 2196 record_for_optimizer(n); 2197 _processed.set(n->_idx); 2198 } else { 2199 // Don't mark as processed since call's arguments have to be processed. 2200 PointsToNode::NodeType nt = PointsToNode::UnknownType; 2201 PointsToNode::EscapeState es = PointsToNode::UnknownEscape; 2202 2203 // Check if a call returns an object. 2204 const TypeTuple *r = n->as_Call()->tf()->range(); 2205 if (r->cnt() > TypeFunc::Parms && 2206 r->field_at(TypeFunc::Parms)->isa_ptr() && 2207 n->as_Call()->proj_out(TypeFunc::Parms) != NULL) { 2208 nt = PointsToNode::JavaObject; 2209 if (!n->is_CallStaticJava()) { 2210 // Since the called mathod is statically unknown assume 2211 // the worst case that the returned value globally escapes. 2212 es = PointsToNode::GlobalEscape; 2213 } 2214 } 2215 add_node(n, nt, es, false); 2216 } 2217 return; 2218 } 2219 2220 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because 2221 // ThreadLocal has RawPrt type. 2222 switch (n->Opcode()) { 2223 case Op_AddP: 2224 { 2225 add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false); 2226 break; 2227 } 2228 case Op_CastX2P: 2229 { // "Unsafe" memory access. 2230 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true); 2231 break; 2232 } 2233 case Op_CastPP: 2234 case Op_CheckCastPP: 2235 case Op_EncodeP: 2236 case Op_DecodeN: 2237 { 2238 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false); 2239 int ti = n->in(1)->_idx; 2240 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type(); 2241 if (nt == PointsToNode::UnknownType) { 2242 _delayed_worklist.push(n); // Process it later. 2243 break; 2244 } else if (nt == PointsToNode::JavaObject) { 2245 add_pointsto_edge(n->_idx, ti); 2246 } else { 2247 add_deferred_edge(n->_idx, ti); 2248 } 2249 _processed.set(n->_idx); 2250 break; 2251 } 2252 case Op_ConP: 2253 { 2254 // assume all pointer constants globally escape except for null 2255 PointsToNode::EscapeState es; 2256 if (phase->type(n) == TypePtr::NULL_PTR) 2257 es = PointsToNode::NoEscape; 2258 else 2259 es = PointsToNode::GlobalEscape; 2260 2261 add_node(n, PointsToNode::JavaObject, es, true); 2262 break; 2263 } 2264 case Op_ConN: 2265 { 2266 // assume all narrow oop constants globally escape except for null 2267 PointsToNode::EscapeState es; 2268 if (phase->type(n) == TypeNarrowOop::NULL_PTR) 2269 es = PointsToNode::NoEscape; 2270 else 2271 es = PointsToNode::GlobalEscape; 2272 2273 add_node(n, PointsToNode::JavaObject, es, true); 2274 break; 2275 } 2276 case Op_CreateEx: 2277 { 2278 // assume that all exception objects globally escape 2279 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true); 2280 break; 2281 } 2282 case Op_LoadKlass: 2283 case Op_LoadNKlass: 2284 { 2285 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true); 2286 break; 2287 } 2288 case Op_LoadP: 2289 case Op_LoadN: 2290 { 2291 const Type *t = phase->type(n); 2292 if (t->make_ptr() == NULL) { 2293 _processed.set(n->_idx); 2294 return; 2295 } 2296 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false); 2297 break; 2298 } 2299 case Op_Parm: 2300 { 2301 _processed.set(n->_idx); // No need to redefine it state. 2302 uint con = n->as_Proj()->_con; 2303 if (con < TypeFunc::Parms) 2304 return; 2305 const Type *t = n->in(0)->as_Start()->_domain->field_at(con); 2306 if (t->isa_ptr() == NULL) 2307 return; 2308 // We have to assume all input parameters globally escape 2309 // (Note: passing 'false' since _processed is already set). 2310 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false); 2311 break; 2312 } 2313 case Op_Phi: 2314 { 2315 const Type *t = n->as_Phi()->type(); 2316 if (t->make_ptr() == NULL) { 2317 // nothing to do if not an oop or narrow oop 2318 _processed.set(n->_idx); 2319 return; 2320 } 2321 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false); 2322 uint i; 2323 for (i = 1; i < n->req() ; i++) { 2324 Node* in = n->in(i); 2325 if (in == NULL) 2326 continue; // ignore NULL 2327 in = in->uncast(); 2328 if (in->is_top() || in == n) 2329 continue; // ignore top or inputs which go back this node 2330 int ti = in->_idx; 2331 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type(); 2332 if (nt == PointsToNode::UnknownType) { 2333 break; 2334 } else if (nt == PointsToNode::JavaObject) { 2335 add_pointsto_edge(n->_idx, ti); 2336 } else { 2337 add_deferred_edge(n->_idx, ti); 2338 } 2339 } 2340 if (i >= n->req()) 2341 _processed.set(n->_idx); 2342 else 2343 _delayed_worklist.push(n); 2344 break; 2345 } 2346 case Op_Proj: 2347 { 2348 // we are only interested in the oop result projection from a call 2349 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) { 2350 const TypeTuple *r = n->in(0)->as_Call()->tf()->range(); 2351 assert(r->cnt() > TypeFunc::Parms, "sanity"); 2352 if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) { 2353 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false); 2354 int ti = n->in(0)->_idx; 2355 // The call may not be registered yet (since not all its inputs are registered) 2356 // if this is the projection from backbranch edge of Phi. 2357 if (ptnode_adr(ti)->node_type() != PointsToNode::UnknownType) { 2358 process_call_result(n->as_Proj(), phase); 2359 } 2360 if (!_processed.test(n->_idx)) { 2361 // The call's result may need to be processed later if the call 2362 // returns it's argument and the argument is not processed yet. 2363 _delayed_worklist.push(n); 2364 } 2365 break; 2366 } 2367 } 2368 _processed.set(n->_idx); 2369 break; 2370 } 2371 case Op_Return: 2372 { 2373 if( n->req() > TypeFunc::Parms && 2374 phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) { 2375 // Treat Return value as LocalVar with GlobalEscape escape state. 2376 add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false); 2377 int ti = n->in(TypeFunc::Parms)->_idx; 2378 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type(); 2379 if (nt == PointsToNode::UnknownType) { 2380 _delayed_worklist.push(n); // Process it later. 2381 break; 2382 } else if (nt == PointsToNode::JavaObject) { 2383 add_pointsto_edge(n->_idx, ti); 2384 } else { 2385 add_deferred_edge(n->_idx, ti); 2386 } 2387 } 2388 _processed.set(n->_idx); 2389 break; 2390 } 2391 case Op_StoreP: 2392 case Op_StoreN: 2393 { 2394 const Type *adr_type = phase->type(n->in(MemNode::Address)); 2395 adr_type = adr_type->make_ptr(); 2396 if (adr_type->isa_oopptr()) { 2397 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false); 2398 } else { 2399 Node* adr = n->in(MemNode::Address); 2400 if (adr->is_AddP() && phase->type(adr) == TypeRawPtr::NOTNULL && 2401 adr->in(AddPNode::Address)->is_Proj() && 2402 adr->in(AddPNode::Address)->in(0)->is_Allocate()) { 2403 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false); 2404 // We are computing a raw address for a store captured 2405 // by an Initialize compute an appropriate address type. 2406 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot); 2407 assert(offs != Type::OffsetBot, "offset must be a constant"); 2408 } else { 2409 _processed.set(n->_idx); 2410 return; 2411 } 2412 } 2413 break; 2414 } 2415 case Op_StorePConditional: 2416 case Op_CompareAndSwapP: 2417 case Op_CompareAndSwapN: 2418 { 2419 const Type *adr_type = phase->type(n->in(MemNode::Address)); 2420 adr_type = adr_type->make_ptr(); 2421 if (adr_type->isa_oopptr()) { 2422 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false); 2423 } else { 2424 _processed.set(n->_idx); 2425 return; 2426 } 2427 break; 2428 } 2429 case Op_AryEq: 2430 case Op_StrComp: 2431 case Op_StrEquals: 2432 case Op_StrIndexOf: 2433 { 2434 // char[] arrays passed to string intrinsics are not scalar replaceable. 2435 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false); 2436 break; 2437 } 2438 case Op_ThreadLocal: 2439 { 2440 add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true); 2441 break; 2442 } 2443 default: 2444 ; 2445 // nothing to do 2446 } 2447 return; 2448 } 2449 2450 void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) { 2451 uint n_idx = n->_idx; 2452 assert(ptnode_adr(n_idx)->_node != NULL, "node should be registered"); 2453 2454 // Don't set processed bit for AddP, LoadP, StoreP since 2455 // they may need more then one pass to process. 2456 if (_processed.test(n_idx)) 2457 return; // No need to redefine node's state. 2458 2459 if (n->is_Call()) { 2460 CallNode *call = n->as_Call(); 2461 process_call_arguments(call, phase); 2462 _processed.set(n_idx); 2463 return; 2464 } 2465 2466 switch (n->Opcode()) { 2467 case Op_AddP: 2468 { 2469 Node *base = get_addp_base(n); 2470 // Create a field edge to this node from everything base could point to. 2471 VectorSet ptset(Thread::current()->resource_area()); 2472 PointsTo(ptset, base); 2473 for( VectorSetI i(&ptset); i.test(); ++i ) { 2474 uint pt = i.elem; 2475 add_field_edge(pt, n_idx, address_offset(n, phase)); 2476 } 2477 break; 2478 } 2479 case Op_CastX2P: 2480 { 2481 assert(false, "Op_CastX2P"); 2482 break; 2483 } 2484 case Op_CastPP: 2485 case Op_CheckCastPP: 2486 case Op_EncodeP: 2487 case Op_DecodeN: 2488 { 2489 int ti = n->in(1)->_idx; 2490 assert(ptnode_adr(ti)->node_type() != PointsToNode::UnknownType, "all nodes should be registered"); 2491 if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) { 2492 add_pointsto_edge(n_idx, ti); 2493 } else { 2494 add_deferred_edge(n_idx, ti); 2495 } 2496 _processed.set(n_idx); 2497 break; 2498 } 2499 case Op_ConP: 2500 { 2501 assert(false, "Op_ConP"); 2502 break; 2503 } 2504 case Op_ConN: 2505 { 2506 assert(false, "Op_ConN"); 2507 break; 2508 } 2509 case Op_CreateEx: 2510 { 2511 assert(false, "Op_CreateEx"); 2512 break; 2513 } 2514 case Op_LoadKlass: 2515 case Op_LoadNKlass: 2516 { 2517 assert(false, "Op_LoadKlass"); 2518 break; 2519 } 2520 case Op_LoadP: 2521 case Op_LoadN: 2522 { 2523 const Type *t = phase->type(n); 2524 #ifdef ASSERT 2525 if (t->make_ptr() == NULL) 2526 assert(false, "Op_LoadP"); 2527 #endif 2528 2529 Node* adr = n->in(MemNode::Address)->uncast(); 2530 Node* adr_base; 2531 if (adr->is_AddP()) { 2532 adr_base = get_addp_base(adr); 2533 } else { 2534 adr_base = adr; 2535 } 2536 2537 // For everything "adr_base" could point to, create a deferred edge from 2538 // this node to each field with the same offset. 2539 VectorSet ptset(Thread::current()->resource_area()); 2540 PointsTo(ptset, adr_base); 2541 int offset = address_offset(adr, phase); 2542 for( VectorSetI i(&ptset); i.test(); ++i ) { 2543 uint pt = i.elem; 2544 add_deferred_edge_to_fields(n_idx, pt, offset); 2545 } 2546 break; 2547 } 2548 case Op_Parm: 2549 { 2550 assert(false, "Op_Parm"); 2551 break; 2552 } 2553 case Op_Phi: 2554 { 2555 #ifdef ASSERT 2556 const Type *t = n->as_Phi()->type(); 2557 if (t->make_ptr() == NULL) 2558 assert(false, "Op_Phi"); 2559 #endif 2560 for (uint i = 1; i < n->req() ; i++) { 2561 Node* in = n->in(i); 2562 if (in == NULL) 2563 continue; // ignore NULL 2564 in = in->uncast(); 2565 if (in->is_top() || in == n) 2566 continue; // ignore top or inputs which go back this node 2567 int ti = in->_idx; 2568 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type(); 2569 assert(nt != PointsToNode::UnknownType, "all nodes should be known"); 2570 if (nt == PointsToNode::JavaObject) { 2571 add_pointsto_edge(n_idx, ti); 2572 } else { 2573 add_deferred_edge(n_idx, ti); 2574 } 2575 } 2576 _processed.set(n_idx); 2577 break; 2578 } 2579 case Op_Proj: 2580 { 2581 // we are only interested in the oop result projection from a call 2582 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) { 2583 assert(ptnode_adr(n->in(0)->_idx)->node_type() != PointsToNode::UnknownType, 2584 "all nodes should be registered"); 2585 const TypeTuple *r = n->in(0)->as_Call()->tf()->range(); 2586 assert(r->cnt() > TypeFunc::Parms, "sanity"); 2587 if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) { 2588 process_call_result(n->as_Proj(), phase); 2589 assert(_processed.test(n_idx), "all call results should be processed"); 2590 break; 2591 } 2592 } 2593 assert(false, "Op_Proj"); 2594 break; 2595 } 2596 case Op_Return: 2597 { 2598 #ifdef ASSERT 2599 if( n->req() <= TypeFunc::Parms || 2600 !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) { 2601 assert(false, "Op_Return"); 2602 } 2603 #endif 2604 int ti = n->in(TypeFunc::Parms)->_idx; 2605 assert(ptnode_adr(ti)->node_type() != PointsToNode::UnknownType, "node should be registered"); 2606 if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) { 2607 add_pointsto_edge(n_idx, ti); 2608 } else { 2609 add_deferred_edge(n_idx, ti); 2610 } 2611 _processed.set(n_idx); 2612 break; 2613 } 2614 case Op_StoreP: 2615 case Op_StoreN: 2616 case Op_StorePConditional: 2617 case Op_CompareAndSwapP: 2618 case Op_CompareAndSwapN: 2619 { 2620 Node *adr = n->in(MemNode::Address); 2621 const Type *adr_type = phase->type(adr)->make_ptr(); 2622 #ifdef ASSERT 2623 if (!adr_type->isa_oopptr()) 2624 assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP"); 2625 #endif 2626 2627 assert(adr->is_AddP(), "expecting an AddP"); 2628 Node *adr_base = get_addp_base(adr); 2629 Node *val = n->in(MemNode::ValueIn)->uncast(); 2630 // For everything "adr_base" could point to, create a deferred edge 2631 // to "val" from each field with the same offset. 2632 VectorSet ptset(Thread::current()->resource_area()); 2633 PointsTo(ptset, adr_base); 2634 for( VectorSetI i(&ptset); i.test(); ++i ) { 2635 uint pt = i.elem; 2636 add_edge_from_fields(pt, val->_idx, address_offset(adr, phase)); 2637 } 2638 break; 2639 } 2640 case Op_AryEq: 2641 case Op_StrComp: 2642 case Op_StrEquals: 2643 case Op_StrIndexOf: 2644 { 2645 // char[] arrays passed to string intrinsic do not escape but 2646 // they are not scalar replaceable. Adjust escape state for them. 2647 // Start from in(2) edge since in(1) is memory edge. 2648 for (uint i = 2; i < n->req(); i++) { 2649 Node* adr = n->in(i)->uncast(); 2650 const Type *at = phase->type(adr); 2651 if (!adr->is_top() && at->isa_ptr()) { 2652 assert(at == Type::TOP || at == TypePtr::NULL_PTR || 2653 at->isa_ptr() != NULL, "expecting an Ptr"); 2654 if (adr->is_AddP()) { 2655 adr = get_addp_base(adr); 2656 } 2657 // Mark as ArgEscape everything "adr" could point to. 2658 set_escape_state(adr->_idx, PointsToNode::ArgEscape); 2659 } 2660 } 2661 _processed.set(n_idx); 2662 break; 2663 } 2664 case Op_ThreadLocal: 2665 { 2666 assert(false, "Op_ThreadLocal"); 2667 break; 2668 } 2669 default: 2670 // This method should be called only for EA specific nodes. 2671 ShouldNotReachHere(); 2672 } 2673 } 2674 2675 #ifndef PRODUCT 2676 void ConnectionGraph::dump() { 2677 bool first = true; 2678 2679 uint size = nodes_size(); 2680 for (uint ni = 0; ni < size; ni++) { 2681 PointsToNode *ptn = ptnode_adr(ni); 2682 PointsToNode::NodeType ptn_type = ptn->node_type(); 2683 2684 if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL) 2685 continue; 2686 PointsToNode::EscapeState es = escape_state(ptn->_node); 2687 if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) { 2688 if (first) { 2689 tty->cr(); 2690 tty->print("======== Connection graph for "); 2691 _compile->method()->print_short_name(); 2692 tty->cr(); 2693 first = false; 2694 } 2695 tty->print("%6d ", ni); 2696 ptn->dump(); 2697 // Print all locals which reference this allocation 2698 for (uint li = ni; li < size; li++) { 2699 PointsToNode *ptn_loc = ptnode_adr(li); 2700 PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type(); 2701 if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL && 2702 ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) { 2703 ptnode_adr(li)->dump(false); 2704 } 2705 } 2706 if (Verbose) { 2707 // Print all fields which reference this allocation 2708 for (uint i = 0; i < ptn->edge_count(); i++) { 2709 uint ei = ptn->edge_target(i); 2710 ptnode_adr(ei)->dump(false); 2711 } 2712 } 2713 tty->cr(); 2714 } 2715 } 2716 } 2717 #endif