Thu, 20 Mar 2008 15:11:44 -0700
6674600: (Escape Analysis) Optimize memory graph for instance's fields
Summary: EA gives opportunite to do more aggressive memory optimizations.
Reviewed-by: never, jrose
1 /*
2 * Copyright 2005-2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_escape.cpp.incl"
28 uint PointsToNode::edge_target(uint e) const {
29 assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
30 return (_edges->at(e) >> EdgeShift);
31 }
33 PointsToNode::EdgeType PointsToNode::edge_type(uint e) const {
34 assert(_edges != NULL && e < (uint)_edges->length(), "valid edge index");
35 return (EdgeType) (_edges->at(e) & EdgeMask);
36 }
38 void PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) {
39 uint v = (targIdx << EdgeShift) + ((uint) et);
40 if (_edges == NULL) {
41 Arena *a = Compile::current()->comp_arena();
42 _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0);
43 }
44 _edges->append_if_missing(v);
45 }
47 void PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) {
48 uint v = (targIdx << EdgeShift) + ((uint) et);
50 _edges->remove(v);
51 }
53 #ifndef PRODUCT
54 static char *node_type_names[] = {
55 "UnknownType",
56 "JavaObject",
57 "LocalVar",
58 "Field"
59 };
61 static char *esc_names[] = {
62 "UnknownEscape",
63 "NoEscape",
64 "ArgEscape",
65 "GlobalEscape"
66 };
68 static char *edge_type_suffix[] = {
69 "?", // UnknownEdge
70 "P", // PointsToEdge
71 "D", // DeferredEdge
72 "F" // FieldEdge
73 };
75 void PointsToNode::dump() const {
76 NodeType nt = node_type();
77 EscapeState es = escape_state();
78 tty->print("%s %s %s [[", node_type_names[(int) nt], esc_names[(int) es], _scalar_replaceable ? "" : "NSR");
79 for (uint i = 0; i < edge_count(); i++) {
80 tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]);
81 }
82 tty->print("]] ");
83 if (_node == NULL)
84 tty->print_cr("<null>");
85 else
86 _node->dump();
87 }
88 #endif
90 ConnectionGraph::ConnectionGraph(Compile * C) : _processed(C->comp_arena()), _node_map(C->comp_arena()) {
91 _collecting = true;
92 this->_compile = C;
93 const PointsToNode &dummy = PointsToNode();
94 int sz = C->unique();
95 _nodes = new(C->comp_arena()) GrowableArray<PointsToNode>(C->comp_arena(), sz, sz, dummy);
96 _phantom_object = C->top()->_idx;
97 PointsToNode *phn = ptnode_adr(_phantom_object);
98 phn->_node = C->top();
99 phn->set_node_type(PointsToNode::JavaObject);
100 phn->set_escape_state(PointsToNode::GlobalEscape);
101 }
103 void ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) {
104 PointsToNode *f = ptnode_adr(from_i);
105 PointsToNode *t = ptnode_adr(to_i);
107 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
108 assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge");
109 assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge");
110 f->add_edge(to_i, PointsToNode::PointsToEdge);
111 }
113 void ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) {
114 PointsToNode *f = ptnode_adr(from_i);
115 PointsToNode *t = ptnode_adr(to_i);
117 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
118 assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of Deferred edge");
119 assert(t->node_type() == PointsToNode::LocalVar || t->node_type() == PointsToNode::Field, "invalid destination of Deferred edge");
120 // don't add a self-referential edge, this can occur during removal of
121 // deferred edges
122 if (from_i != to_i)
123 f->add_edge(to_i, PointsToNode::DeferredEdge);
124 }
126 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
127 const Type *adr_type = phase->type(adr);
128 if (adr->is_AddP() && adr_type->isa_oopptr() == NULL &&
129 adr->in(AddPNode::Address)->is_Proj() &&
130 adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
131 // We are computing a raw address for a store captured by an Initialize
132 // compute an appropriate address type. AddP cases #3 and #5 (see below).
133 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
134 assert(offs != Type::OffsetBot ||
135 adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
136 "offset must be a constant or it is initialization of array");
137 return offs;
138 }
139 const TypePtr *t_ptr = adr_type->isa_ptr();
140 assert(t_ptr != NULL, "must be a pointer type");
141 return t_ptr->offset();
142 }
144 void ConnectionGraph::add_field_edge(uint from_i, uint to_i, int offset) {
145 PointsToNode *f = ptnode_adr(from_i);
146 PointsToNode *t = ptnode_adr(to_i);
148 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
149 assert(f->node_type() == PointsToNode::JavaObject, "invalid destination of Field edge");
150 assert(t->node_type() == PointsToNode::Field, "invalid destination of Field edge");
151 assert (t->offset() == -1 || t->offset() == offset, "conflicting field offsets");
152 t->set_offset(offset);
154 f->add_edge(to_i, PointsToNode::FieldEdge);
155 }
157 void ConnectionGraph::set_escape_state(uint ni, PointsToNode::EscapeState es) {
158 PointsToNode *npt = ptnode_adr(ni);
159 PointsToNode::EscapeState old_es = npt->escape_state();
160 if (es > old_es)
161 npt->set_escape_state(es);
162 }
164 void ConnectionGraph::add_node(Node *n, PointsToNode::NodeType nt,
165 PointsToNode::EscapeState es, bool done) {
166 PointsToNode* ptadr = ptnode_adr(n->_idx);
167 ptadr->_node = n;
168 ptadr->set_node_type(nt);
170 // inline set_escape_state(idx, es);
171 PointsToNode::EscapeState old_es = ptadr->escape_state();
172 if (es > old_es)
173 ptadr->set_escape_state(es);
175 if (done)
176 _processed.set(n->_idx);
177 }
179 PointsToNode::EscapeState ConnectionGraph::escape_state(Node *n, PhaseTransform *phase) {
180 uint idx = n->_idx;
181 PointsToNode::EscapeState es;
183 // If we are still collecting or there were no non-escaping allocations
184 // we don't know the answer yet
185 if (_collecting || !_has_allocations)
186 return PointsToNode::UnknownEscape;
188 // if the node was created after the escape computation, return
189 // UnknownEscape
190 if (idx >= (uint)_nodes->length())
191 return PointsToNode::UnknownEscape;
193 es = _nodes->at_grow(idx).escape_state();
195 // if we have already computed a value, return it
196 if (es != PointsToNode::UnknownEscape)
197 return es;
199 // compute max escape state of anything this node could point to
200 VectorSet ptset(Thread::current()->resource_area());
201 PointsTo(ptset, n, phase);
202 for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) {
203 uint pt = i.elem;
204 PointsToNode::EscapeState pes = _nodes->adr_at(pt)->escape_state();
205 if (pes > es)
206 es = pes;
207 }
208 // cache the computed escape state
209 assert(es != PointsToNode::UnknownEscape, "should have computed an escape state");
210 _nodes->adr_at(idx)->set_escape_state(es);
211 return es;
212 }
214 void ConnectionGraph::PointsTo(VectorSet &ptset, Node * n, PhaseTransform *phase) {
215 VectorSet visited(Thread::current()->resource_area());
216 GrowableArray<uint> worklist;
218 n = n->uncast();
219 PointsToNode npt = _nodes->at_grow(n->_idx);
221 // If we have a JavaObject, return just that object
222 if (npt.node_type() == PointsToNode::JavaObject) {
223 ptset.set(n->_idx);
224 return;
225 }
226 assert(npt._node != NULL, "unregistered node");
228 worklist.push(n->_idx);
229 while(worklist.length() > 0) {
230 int ni = worklist.pop();
231 PointsToNode pn = _nodes->at_grow(ni);
232 if (!visited.test_set(ni)) {
233 // ensure that all inputs of a Phi have been processed
234 assert(!_collecting || !pn._node->is_Phi() || _processed.test(ni),"");
236 int edges_processed = 0;
237 for (uint e = 0; e < pn.edge_count(); e++) {
238 uint etgt = pn.edge_target(e);
239 PointsToNode::EdgeType et = pn.edge_type(e);
240 if (et == PointsToNode::PointsToEdge) {
241 ptset.set(etgt);
242 edges_processed++;
243 } else if (et == PointsToNode::DeferredEdge) {
244 worklist.push(etgt);
245 edges_processed++;
246 } else {
247 assert(false,"neither PointsToEdge or DeferredEdge");
248 }
249 }
250 if (edges_processed == 0) {
251 // no deferred or pointsto edges found. Assume the value was set
252 // outside this method. Add the phantom object to the pointsto set.
253 ptset.set(_phantom_object);
254 }
255 }
256 }
257 }
259 void ConnectionGraph::remove_deferred(uint ni) {
260 VectorSet visited(Thread::current()->resource_area());
262 uint i = 0;
263 PointsToNode *ptn = ptnode_adr(ni);
265 while(i < ptn->edge_count()) {
266 uint t = ptn->edge_target(i);
267 PointsToNode *ptt = ptnode_adr(t);
268 if (ptn->edge_type(i) != PointsToNode::DeferredEdge) {
269 i++;
270 } else {
271 ptn->remove_edge(t, PointsToNode::DeferredEdge);
272 if(!visited.test_set(t)) {
273 for (uint j = 0; j < ptt->edge_count(); j++) {
274 uint n1 = ptt->edge_target(j);
275 PointsToNode *pt1 = ptnode_adr(n1);
276 switch(ptt->edge_type(j)) {
277 case PointsToNode::PointsToEdge:
278 add_pointsto_edge(ni, n1);
279 if(n1 == _phantom_object) {
280 // Special case - field set outside (globally escaping).
281 ptn->set_escape_state(PointsToNode::GlobalEscape);
282 }
283 break;
284 case PointsToNode::DeferredEdge:
285 add_deferred_edge(ni, n1);
286 break;
287 case PointsToNode::FieldEdge:
288 assert(false, "invalid connection graph");
289 break;
290 }
291 }
292 }
293 }
294 }
295 }
298 // Add an edge to node given by "to_i" from any field of adr_i whose offset
299 // matches "offset" A deferred edge is added if to_i is a LocalVar, and
300 // a pointsto edge is added if it is a JavaObject
302 void ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) {
303 PointsToNode an = _nodes->at_grow(adr_i);
304 PointsToNode to = _nodes->at_grow(to_i);
305 bool deferred = (to.node_type() == PointsToNode::LocalVar);
307 for (uint fe = 0; fe < an.edge_count(); fe++) {
308 assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
309 int fi = an.edge_target(fe);
310 PointsToNode pf = _nodes->at_grow(fi);
311 int po = pf.offset();
312 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
313 if (deferred)
314 add_deferred_edge(fi, to_i);
315 else
316 add_pointsto_edge(fi, to_i);
317 }
318 }
319 }
321 // Add a deferred edge from node given by "from_i" to any field of adr_i
322 // whose offset matches "offset".
323 void ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) {
324 PointsToNode an = _nodes->at_grow(adr_i);
325 for (uint fe = 0; fe < an.edge_count(); fe++) {
326 assert(an.edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
327 int fi = an.edge_target(fe);
328 PointsToNode pf = _nodes->at_grow(fi);
329 int po = pf.offset();
330 if (pf.edge_count() == 0) {
331 // we have not seen any stores to this field, assume it was set outside this method
332 add_pointsto_edge(fi, _phantom_object);
333 }
334 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
335 add_deferred_edge(from_i, fi);
336 }
337 }
338 }
340 // Helper functions
342 static Node* get_addp_base(Node *addp) {
343 assert(addp->is_AddP(), "must be AddP");
344 //
345 // AddP cases for Base and Address inputs:
346 // case #1. Direct object's field reference:
347 // Allocate
348 // |
349 // Proj #5 ( oop result )
350 // |
351 // CheckCastPP (cast to instance type)
352 // | |
353 // AddP ( base == address )
354 //
355 // case #2. Indirect object's field reference:
356 // Phi
357 // |
358 // CastPP (cast to instance type)
359 // | |
360 // AddP ( base == address )
361 //
362 // case #3. Raw object's field reference for Initialize node:
363 // Allocate
364 // |
365 // Proj #5 ( oop result )
366 // top |
367 // \ |
368 // AddP ( base == top )
369 //
370 // case #4. Array's element reference:
371 // {CheckCastPP | CastPP}
372 // | | |
373 // | AddP ( array's element offset )
374 // | |
375 // AddP ( array's offset )
376 //
377 // case #5. Raw object's field reference for arraycopy stub call:
378 // The inline_native_clone() case when the arraycopy stub is called
379 // after the allocation before Initialize and CheckCastPP nodes.
380 // Allocate
381 // |
382 // Proj #5 ( oop result )
383 // | |
384 // AddP ( base == address )
385 //
386 // case #6. Constant Pool or ThreadLocal or Raw object's field reference:
387 // ConP # Object from Constant Pool.
388 // top |
389 // \ |
390 // AddP ( base == top )
391 //
392 Node *base = addp->in(AddPNode::Base)->uncast();
393 if (base->is_top()) { // The AddP case #3 and #6.
394 base = addp->in(AddPNode::Address)->uncast();
395 assert(base->Opcode() == Op_ConP || base->Opcode() == Op_ThreadLocal ||
396 base->is_Mem() && base->bottom_type() == TypeRawPtr::NOTNULL ||
397 base->is_Proj() && base->in(0)->is_Allocate(), "sanity");
398 }
399 return base;
400 }
402 static Node* find_second_addp(Node* addp, Node* n) {
403 assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
405 Node* addp2 = addp->raw_out(0);
406 if (addp->outcnt() == 1 && addp2->is_AddP() &&
407 addp2->in(AddPNode::Base) == n &&
408 addp2->in(AddPNode::Address) == addp) {
410 assert(addp->in(AddPNode::Base) == n, "expecting the same base");
411 //
412 // Find array's offset to push it on worklist first and
413 // as result process an array's element offset first (pushed second)
414 // to avoid CastPP for the array's offset.
415 // Otherwise the inserted CastPP (LocalVar) will point to what
416 // the AddP (Field) points to. Which would be wrong since
417 // the algorithm expects the CastPP has the same point as
418 // as AddP's base CheckCastPP (LocalVar).
419 //
420 // ArrayAllocation
421 // |
422 // CheckCastPP
423 // |
424 // memProj (from ArrayAllocation CheckCastPP)
425 // | ||
426 // | || Int (element index)
427 // | || | ConI (log(element size))
428 // | || | /
429 // | || LShift
430 // | || /
431 // | AddP (array's element offset)
432 // | |
433 // | | ConI (array's offset: #12(32-bits) or #24(64-bits))
434 // | / /
435 // AddP (array's offset)
436 // |
437 // Load/Store (memory operation on array's element)
438 //
439 return addp2;
440 }
441 return NULL;
442 }
444 //
445 // Adjust the type and inputs of an AddP which computes the
446 // address of a field of an instance
447 //
448 void ConnectionGraph::split_AddP(Node *addp, Node *base, PhaseGVN *igvn) {
449 const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
450 assert(base_t != NULL && base_t->is_instance(), "expecting instance oopptr");
451 const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
452 if (t == NULL) {
453 // We are computing a raw address for a store captured by an Initialize
454 // compute an appropriate address type.
455 assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
456 assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
457 int offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
458 assert(offs != Type::OffsetBot, "offset must be a constant");
459 t = base_t->add_offset(offs)->is_oopptr();
460 }
461 uint inst_id = base_t->instance_id();
462 assert(!t->is_instance() || t->instance_id() == inst_id,
463 "old type must be non-instance or match new type");
464 const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
465 // Do NOT remove the next call: ensure an new alias index is allocated
466 // for the instance type
467 int alias_idx = _compile->get_alias_index(tinst);
468 igvn->set_type(addp, tinst);
469 // record the allocation in the node map
470 set_map(addp->_idx, get_map(base->_idx));
471 // if the Address input is not the appropriate instance type
472 // (due to intervening casts,) insert a cast
473 Node *adr = addp->in(AddPNode::Address);
474 const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
475 if (atype != NULL && atype->instance_id() != inst_id) {
476 assert(!atype->is_instance(), "no conflicting instances");
477 const TypeOopPtr *new_atype = base_t->add_offset(atype->offset())->isa_oopptr();
478 Node *acast = new (_compile, 2) CastPPNode(adr, new_atype);
479 acast->set_req(0, adr->in(0));
480 igvn->set_type(acast, new_atype);
481 record_for_optimizer(acast);
482 Node *bcast = acast;
483 Node *abase = addp->in(AddPNode::Base);
484 if (abase != adr) {
485 bcast = new (_compile, 2) CastPPNode(abase, base_t);
486 bcast->set_req(0, abase->in(0));
487 igvn->set_type(bcast, base_t);
488 record_for_optimizer(bcast);
489 }
490 igvn->hash_delete(addp);
491 addp->set_req(AddPNode::Base, bcast);
492 addp->set_req(AddPNode::Address, acast);
493 igvn->hash_insert(addp);
494 }
495 // Put on IGVN worklist since at least addp's type was changed above.
496 record_for_optimizer(addp);
497 }
499 //
500 // Create a new version of orig_phi if necessary. Returns either the newly
501 // created phi or an existing phi. Sets create_new to indicate wheter a new
502 // phi was created. Cache the last newly created phi in the node map.
503 //
504 PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn, bool &new_created) {
505 Compile *C = _compile;
506 new_created = false;
507 int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
508 // nothing to do if orig_phi is bottom memory or matches alias_idx
509 if (phi_alias_idx == alias_idx) {
510 return orig_phi;
511 }
512 // have we already created a Phi for this alias index?
513 PhiNode *result = get_map_phi(orig_phi->_idx);
514 if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
515 return result;
516 }
517 if ((int)C->unique() + 2*NodeLimitFudgeFactor > MaxNodeLimit) {
518 if (C->do_escape_analysis() == true && !C->failing()) {
519 // Retry compilation without escape analysis.
520 // If this is the first failure, the sentinel string will "stick"
521 // to the Compile object, and the C2Compiler will see it and retry.
522 C->record_failure(C2Compiler::retry_no_escape_analysis());
523 }
524 return NULL;
525 }
526 orig_phi_worklist.append_if_missing(orig_phi);
527 const TypePtr *atype = C->get_adr_type(alias_idx);
528 result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
529 set_map_phi(orig_phi->_idx, result);
530 igvn->set_type(result, result->bottom_type());
531 record_for_optimizer(result);
532 new_created = true;
533 return result;
534 }
536 //
537 // Return a new version of Memory Phi "orig_phi" with the inputs having the
538 // specified alias index.
539 //
540 PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, PhaseGVN *igvn) {
542 assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
543 Compile *C = _compile;
544 bool new_phi_created;
545 PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, igvn, new_phi_created);
546 if (!new_phi_created) {
547 return result;
548 }
550 GrowableArray<PhiNode *> phi_list;
551 GrowableArray<uint> cur_input;
553 PhiNode *phi = orig_phi;
554 uint idx = 1;
555 bool finished = false;
556 while(!finished) {
557 while (idx < phi->req()) {
558 Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist, igvn);
559 if (mem != NULL && mem->is_Phi()) {
560 PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, igvn, new_phi_created);
561 if (new_phi_created) {
562 // found an phi for which we created a new split, push current one on worklist and begin
563 // processing new one
564 phi_list.push(phi);
565 cur_input.push(idx);
566 phi = mem->as_Phi();
567 result = newphi;
568 idx = 1;
569 continue;
570 } else {
571 mem = newphi;
572 }
573 }
574 if (C->failing()) {
575 return NULL;
576 }
577 result->set_req(idx++, mem);
578 }
579 #ifdef ASSERT
580 // verify that the new Phi has an input for each input of the original
581 assert( phi->req() == result->req(), "must have same number of inputs.");
582 assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
583 #endif
584 // Check if all new phi's inputs have specified alias index.
585 // Otherwise use old phi.
586 for (uint i = 1; i < phi->req(); i++) {
587 Node* in = result->in(i);
588 assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
589 }
590 // we have finished processing a Phi, see if there are any more to do
591 finished = (phi_list.length() == 0 );
592 if (!finished) {
593 phi = phi_list.pop();
594 idx = cur_input.pop();
595 PhiNode *prev_result = get_map_phi(phi->_idx);
596 prev_result->set_req(idx++, result);
597 result = prev_result;
598 }
599 }
600 return result;
601 }
604 //
605 // The next methods are derived from methods in MemNode.
606 //
607 static Node *step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *tinst) {
608 Node *mem = mmem;
609 // TypeInstPtr::NOTNULL+any is an OOP with unknown offset - generally
610 // means an array I have not precisely typed yet. Do not do any
611 // alias stuff with it any time soon.
612 if( tinst->base() != Type::AnyPtr &&
613 !(tinst->klass()->is_java_lang_Object() &&
614 tinst->offset() == Type::OffsetBot) ) {
615 mem = mmem->memory_at(alias_idx);
616 // Update input if it is progress over what we have now
617 }
618 return mem;
619 }
621 //
622 // Search memory chain of "mem" to find a MemNode whose address
623 // is the specified alias index.
624 //
625 Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *> &orig_phis, PhaseGVN *phase) {
626 if (orig_mem == NULL)
627 return orig_mem;
628 Compile* C = phase->C;
629 const TypeOopPtr *tinst = C->get_adr_type(alias_idx)->isa_oopptr();
630 bool is_instance = (tinst != NULL) && tinst->is_instance();
631 Node *prev = NULL;
632 Node *result = orig_mem;
633 while (prev != result) {
634 prev = result;
635 if (result->is_Mem()) {
636 MemNode *mem = result->as_Mem();
637 const Type *at = phase->type(mem->in(MemNode::Address));
638 if (at != Type::TOP) {
639 assert (at->isa_ptr() != NULL, "pointer type required.");
640 int idx = C->get_alias_index(at->is_ptr());
641 if (idx == alias_idx)
642 break;
643 }
644 result = mem->in(MemNode::Memory);
645 }
646 if (!is_instance)
647 continue; // don't search further for non-instance types
648 // skip over a call which does not affect this memory slice
649 if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
650 Node *proj_in = result->in(0);
651 if (proj_in->is_Call()) {
652 CallNode *call = proj_in->as_Call();
653 if (!call->may_modify(tinst, phase)) {
654 result = call->in(TypeFunc::Memory);
655 }
656 } else if (proj_in->is_Initialize()) {
657 AllocateNode* alloc = proj_in->as_Initialize()->allocation();
658 // Stop if this is the initialization for the object instance which
659 // which contains this memory slice, otherwise skip over it.
660 if (alloc == NULL || alloc->_idx != tinst->instance_id()) {
661 result = proj_in->in(TypeFunc::Memory);
662 }
663 } else if (proj_in->is_MemBar()) {
664 result = proj_in->in(TypeFunc::Memory);
665 }
666 } else if (result->is_MergeMem()) {
667 MergeMemNode *mmem = result->as_MergeMem();
668 result = step_through_mergemem(mmem, alias_idx, tinst);
669 if (result == mmem->base_memory()) {
670 // Didn't find instance memory, search through general slice recursively.
671 result = mmem->memory_at(C->get_general_index(alias_idx));
672 result = find_inst_mem(result, alias_idx, orig_phis, phase);
673 if (C->failing()) {
674 return NULL;
675 }
676 mmem->set_memory_at(alias_idx, result);
677 }
678 } else if (result->is_Phi() &&
679 C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
680 Node *un = result->as_Phi()->unique_input(phase);
681 if (un != NULL) {
682 result = un;
683 } else {
684 break;
685 }
686 }
687 }
688 if (is_instance && result->is_Phi()) {
689 PhiNode *mphi = result->as_Phi();
690 assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
691 const TypePtr *t = mphi->adr_type();
692 if (C->get_alias_index(t) != alias_idx) {
693 result = split_memory_phi(mphi, alias_idx, orig_phis, phase);
694 }
695 }
696 // the result is either MemNode, PhiNode, InitializeNode.
697 return result;
698 }
701 //
702 // Convert the types of unescaped object to instance types where possible,
703 // propagate the new type information through the graph, and update memory
704 // edges and MergeMem inputs to reflect the new type.
705 //
706 // We start with allocations (and calls which may be allocations) on alloc_worklist.
707 // The processing is done in 4 phases:
708 //
709 // Phase 1: Process possible allocations from alloc_worklist. Create instance
710 // types for the CheckCastPP for allocations where possible.
711 // Propagate the the new types through users as follows:
712 // casts and Phi: push users on alloc_worklist
713 // AddP: cast Base and Address inputs to the instance type
714 // push any AddP users on alloc_worklist and push any memnode
715 // users onto memnode_worklist.
716 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and
717 // search the Memory chain for a store with the appropriate type
718 // address type. If a Phi is found, create a new version with
719 // the approriate memory slices from each of the Phi inputs.
720 // For stores, process the users as follows:
721 // MemNode: push on memnode_worklist
722 // MergeMem: push on mergemem_worklist
723 // Phase 3: Process MergeMem nodes from mergemem_worklist. Walk each memory slice
724 // moving the first node encountered of each instance type to the
725 // the input corresponding to its alias index.
726 // appropriate memory slice.
727 // Phase 4: Update the inputs of non-instance memory Phis and the Memory input of memnodes.
728 //
729 // In the following example, the CheckCastPP nodes are the cast of allocation
730 // results and the allocation of node 29 is unescaped and eligible to be an
731 // instance type.
732 //
733 // We start with:
734 //
735 // 7 Parm #memory
736 // 10 ConI "12"
737 // 19 CheckCastPP "Foo"
738 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
739 // 29 CheckCastPP "Foo"
740 // 30 AddP _ 29 29 10 Foo+12 alias_index=4
741 //
742 // 40 StoreP 25 7 20 ... alias_index=4
743 // 50 StoreP 35 40 30 ... alias_index=4
744 // 60 StoreP 45 50 20 ... alias_index=4
745 // 70 LoadP _ 60 30 ... alias_index=4
746 // 80 Phi 75 50 60 Memory alias_index=4
747 // 90 LoadP _ 80 30 ... alias_index=4
748 // 100 LoadP _ 80 20 ... alias_index=4
749 //
750 //
751 // Phase 1 creates an instance type for node 29 assigning it an instance id of 24
752 // and creating a new alias index for node 30. This gives:
753 //
754 // 7 Parm #memory
755 // 10 ConI "12"
756 // 19 CheckCastPP "Foo"
757 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
758 // 29 CheckCastPP "Foo" iid=24
759 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
760 //
761 // 40 StoreP 25 7 20 ... alias_index=4
762 // 50 StoreP 35 40 30 ... alias_index=6
763 // 60 StoreP 45 50 20 ... alias_index=4
764 // 70 LoadP _ 60 30 ... alias_index=6
765 // 80 Phi 75 50 60 Memory alias_index=4
766 // 90 LoadP _ 80 30 ... alias_index=6
767 // 100 LoadP _ 80 20 ... alias_index=4
768 //
769 // In phase 2, new memory inputs are computed for the loads and stores,
770 // And a new version of the phi is created. In phase 4, the inputs to
771 // node 80 are updated and then the memory nodes are updated with the
772 // values computed in phase 2. This results in:
773 //
774 // 7 Parm #memory
775 // 10 ConI "12"
776 // 19 CheckCastPP "Foo"
777 // 20 AddP _ 19 19 10 Foo+12 alias_index=4
778 // 29 CheckCastPP "Foo" iid=24
779 // 30 AddP _ 29 29 10 Foo+12 alias_index=6 iid=24
780 //
781 // 40 StoreP 25 7 20 ... alias_index=4
782 // 50 StoreP 35 7 30 ... alias_index=6
783 // 60 StoreP 45 40 20 ... alias_index=4
784 // 70 LoadP _ 50 30 ... alias_index=6
785 // 80 Phi 75 40 60 Memory alias_index=4
786 // 120 Phi 75 50 50 Memory alias_index=6
787 // 90 LoadP _ 120 30 ... alias_index=6
788 // 100 LoadP _ 80 20 ... alias_index=4
789 //
790 void ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) {
791 GrowableArray<Node *> memnode_worklist;
792 GrowableArray<Node *> mergemem_worklist;
793 GrowableArray<PhiNode *> orig_phis;
794 PhaseGVN *igvn = _compile->initial_gvn();
795 uint new_index_start = (uint) _compile->num_alias_types();
796 VectorSet visited(Thread::current()->resource_area());
797 VectorSet ptset(Thread::current()->resource_area());
800 // Phase 1: Process possible allocations from alloc_worklist.
801 // Create instance types for the CheckCastPP for allocations where possible.
802 while (alloc_worklist.length() != 0) {
803 Node *n = alloc_worklist.pop();
804 uint ni = n->_idx;
805 const TypeOopPtr* tinst = NULL;
806 if (n->is_Call()) {
807 CallNode *alloc = n->as_Call();
808 // copy escape information to call node
809 PointsToNode* ptn = _nodes->adr_at(alloc->_idx);
810 PointsToNode::EscapeState es = escape_state(alloc, igvn);
811 // We have an allocation or call which returns a Java object,
812 // see if it is unescaped.
813 if (es != PointsToNode::NoEscape || !ptn->_scalar_replaceable)
814 continue;
815 if (alloc->is_Allocate()) {
816 // Set the scalar_replaceable flag before the next check.
817 alloc->as_Allocate()->_is_scalar_replaceable = true;
818 }
819 // find CheckCastPP of call return value
820 n = alloc->result_cast();
821 if (n == NULL || // No uses accept Initialize or
822 !n->is_CheckCastPP()) // not unique CheckCastPP.
823 continue;
824 // The inline code for Object.clone() casts the allocation result to
825 // java.lang.Object and then to the the actual type of the allocated
826 // object. Detect this case and use the second cast.
827 if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
828 && igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT) {
829 Node *cast2 = NULL;
830 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
831 Node *use = n->fast_out(i);
832 if (use->is_CheckCastPP()) {
833 cast2 = use;
834 break;
835 }
836 }
837 if (cast2 != NULL) {
838 n = cast2;
839 } else {
840 continue;
841 }
842 }
843 set_escape_state(n->_idx, es);
844 // in order for an object to be stackallocatable, it must be:
845 // - a direct allocation (not a call returning an object)
846 // - non-escaping
847 // - eligible to be a unique type
848 // - not determined to be ineligible by escape analysis
849 set_map(alloc->_idx, n);
850 set_map(n->_idx, alloc);
851 const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
852 if (t == NULL)
853 continue; // not a TypeInstPtr
854 tinst = t->cast_to_instance(ni);
855 igvn->hash_delete(n);
856 igvn->set_type(n, tinst);
857 n->raise_bottom_type(tinst);
858 igvn->hash_insert(n);
859 record_for_optimizer(n);
860 if (alloc->is_Allocate() && ptn->_scalar_replaceable &&
861 (t->isa_instptr() || t->isa_aryptr())) {
862 // An allocation may have an Initialize which has raw stores. Scan
863 // the users of the raw allocation result and push AddP users
864 // on alloc_worklist.
865 Node *raw_result = alloc->proj_out(TypeFunc::Parms);
866 assert (raw_result != NULL, "must have an allocation result");
867 for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
868 Node *use = raw_result->fast_out(i);
869 if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
870 Node* addp2 = find_second_addp(use, raw_result);
871 if (addp2 != NULL) {
872 assert(alloc->is_AllocateArray(),"array allocation was expected");
873 alloc_worklist.append_if_missing(addp2);
874 }
875 alloc_worklist.append_if_missing(use);
876 } else if (use->is_Initialize()) {
877 memnode_worklist.append_if_missing(use);
878 }
879 }
880 }
881 } else if (n->is_AddP()) {
882 ptset.Clear();
883 PointsTo(ptset, get_addp_base(n), igvn);
884 assert(ptset.Size() == 1, "AddP address is unique");
885 uint elem = ptset.getelem(); // Allocation node's index
886 if (elem == _phantom_object)
887 continue; // Assume the value was set outside this method.
888 Node *base = get_map(elem); // CheckCastPP node
889 split_AddP(n, base, igvn);
890 tinst = igvn->type(base)->isa_oopptr();
891 } else if (n->is_Phi() ||
892 n->is_CheckCastPP() ||
893 (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
894 if (visited.test_set(n->_idx)) {
895 assert(n->is_Phi(), "loops only through Phi's");
896 continue; // already processed
897 }
898 ptset.Clear();
899 PointsTo(ptset, n, igvn);
900 if (ptset.Size() == 1) {
901 uint elem = ptset.getelem(); // Allocation node's index
902 if (elem == _phantom_object)
903 continue; // Assume the value was set outside this method.
904 Node *val = get_map(elem); // CheckCastPP node
905 TypeNode *tn = n->as_Type();
906 tinst = igvn->type(val)->isa_oopptr();
907 assert(tinst != NULL && tinst->is_instance() &&
908 tinst->instance_id() == elem , "instance type expected.");
909 const TypeOopPtr *tn_t = igvn->type(tn)->isa_oopptr();
911 if (tn_t != NULL &&
912 tinst->cast_to_instance(TypeOopPtr::UNKNOWN_INSTANCE)->higher_equal(tn_t)) {
913 igvn->hash_delete(tn);
914 igvn->set_type(tn, tinst);
915 tn->set_type(tinst);
916 igvn->hash_insert(tn);
917 record_for_optimizer(n);
918 }
919 }
920 } else {
921 continue;
922 }
923 // push users on appropriate worklist
924 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
925 Node *use = n->fast_out(i);
926 if(use->is_Mem() && use->in(MemNode::Address) == n) {
927 memnode_worklist.append_if_missing(use);
928 } else if (use->is_Initialize()) {
929 memnode_worklist.append_if_missing(use);
930 } else if (use->is_MergeMem()) {
931 mergemem_worklist.append_if_missing(use);
932 } else if (use->is_Call() && tinst != NULL) {
933 // Look for MergeMem nodes for calls which reference unique allocation
934 // (through CheckCastPP nodes) even for debug info.
935 Node* m = use->in(TypeFunc::Memory);
936 uint iid = tinst->instance_id();
937 while (m->is_Proj() && m->in(0)->is_Call() &&
938 m->in(0) != use && !m->in(0)->_idx != iid) {
939 m = m->in(0)->in(TypeFunc::Memory);
940 }
941 if (m->is_MergeMem()) {
942 mergemem_worklist.append_if_missing(m);
943 }
944 } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
945 Node* addp2 = find_second_addp(use, n);
946 if (addp2 != NULL) {
947 alloc_worklist.append_if_missing(addp2);
948 }
949 alloc_worklist.append_if_missing(use);
950 } else if (use->is_Phi() ||
951 use->is_CheckCastPP() ||
952 (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
953 alloc_worklist.append_if_missing(use);
954 }
955 }
957 }
958 // New alias types were created in split_AddP().
959 uint new_index_end = (uint) _compile->num_alias_types();
961 // Phase 2: Process MemNode's from memnode_worklist. compute new address type and
962 // compute new values for Memory inputs (the Memory inputs are not
963 // actually updated until phase 4.)
964 if (memnode_worklist.length() == 0)
965 return; // nothing to do
967 while (memnode_worklist.length() != 0) {
968 Node *n = memnode_worklist.pop();
969 if (visited.test_set(n->_idx))
970 continue;
971 if (n->is_Phi()) {
972 assert(n->as_Phi()->adr_type() != TypePtr::BOTTOM, "narrow memory slice required");
973 // we don't need to do anything, but the users must be pushed if we haven't processed
974 // this Phi before
975 } else if (n->is_Initialize()) {
976 // we don't need to do anything, but the users of the memory projection must be pushed
977 n = n->as_Initialize()->proj_out(TypeFunc::Memory);
978 if (n == NULL)
979 continue;
980 } else {
981 assert(n->is_Mem(), "memory node required.");
982 Node *addr = n->in(MemNode::Address);
983 assert(addr->is_AddP(), "AddP required");
984 const Type *addr_t = igvn->type(addr);
985 if (addr_t == Type::TOP)
986 continue;
987 assert (addr_t->isa_ptr() != NULL, "pointer type required.");
988 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
989 assert ((uint)alias_idx < new_index_end, "wrong alias index");
990 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
991 if (_compile->failing()) {
992 return;
993 }
994 if (mem != n->in(MemNode::Memory)) {
995 set_map(n->_idx, mem);
996 _nodes->adr_at(n->_idx)->_node = n;
997 }
998 if (n->is_Load()) {
999 continue; // don't push users
1000 } else if (n->is_LoadStore()) {
1001 // get the memory projection
1002 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1003 Node *use = n->fast_out(i);
1004 if (use->Opcode() == Op_SCMemProj) {
1005 n = use;
1006 break;
1007 }
1008 }
1009 assert(n->Opcode() == Op_SCMemProj, "memory projection required");
1010 }
1011 }
1012 // push user on appropriate worklist
1013 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1014 Node *use = n->fast_out(i);
1015 if (use->is_Phi()) {
1016 memnode_worklist.append_if_missing(use);
1017 } else if(use->is_Mem() && use->in(MemNode::Memory) == n) {
1018 memnode_worklist.append_if_missing(use);
1019 } else if (use->is_Initialize()) {
1020 memnode_worklist.append_if_missing(use);
1021 } else if (use->is_MergeMem()) {
1022 mergemem_worklist.append_if_missing(use);
1023 }
1024 }
1025 }
1027 // Phase 3: Process MergeMem nodes from mergemem_worklist.
1028 // Walk each memory moving the first node encountered of each
1029 // instance type to the the input corresponding to its alias index.
1030 while (mergemem_worklist.length() != 0) {
1031 Node *n = mergemem_worklist.pop();
1032 assert(n->is_MergeMem(), "MergeMem node required.");
1033 if (visited.test_set(n->_idx))
1034 continue;
1035 MergeMemNode *nmm = n->as_MergeMem();
1036 // Note: we don't want to use MergeMemStream here because we only want to
1037 // scan inputs which exist at the start, not ones we add during processing.
1038 uint nslices = nmm->req();
1039 igvn->hash_delete(nmm);
1040 for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
1041 Node* mem = nmm->in(i);
1042 Node* cur = NULL;
1043 if (mem == NULL || mem->is_top())
1044 continue;
1045 while (mem->is_Mem()) {
1046 const Type *at = igvn->type(mem->in(MemNode::Address));
1047 if (at != Type::TOP) {
1048 assert (at->isa_ptr() != NULL, "pointer type required.");
1049 uint idx = (uint)_compile->get_alias_index(at->is_ptr());
1050 if (idx == i) {
1051 if (cur == NULL)
1052 cur = mem;
1053 } else {
1054 if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
1055 nmm->set_memory_at(idx, mem);
1056 }
1057 }
1058 }
1059 mem = mem->in(MemNode::Memory);
1060 }
1061 nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
1062 // Find any instance of the current type if we haven't encountered
1063 // a value of the instance along the chain.
1064 for (uint ni = new_index_start; ni < new_index_end; ni++) {
1065 if((uint)_compile->get_general_index(ni) == i) {
1066 Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
1067 if (nmm->is_empty_memory(m)) {
1068 Node* result = find_inst_mem(mem, ni, orig_phis, igvn);
1069 if (_compile->failing()) {
1070 return;
1071 }
1072 nmm->set_memory_at(ni, result);
1073 }
1074 }
1075 }
1076 }
1077 // Find the rest of instances values
1078 for (uint ni = new_index_start; ni < new_index_end; ni++) {
1079 const TypeOopPtr *tinst = igvn->C->get_adr_type(ni)->isa_oopptr();
1080 Node* result = step_through_mergemem(nmm, ni, tinst);
1081 if (result == nmm->base_memory()) {
1082 // Didn't find instance memory, search through general slice recursively.
1083 result = nmm->memory_at(igvn->C->get_general_index(ni));
1084 result = find_inst_mem(result, ni, orig_phis, igvn);
1085 if (_compile->failing()) {
1086 return;
1087 }
1088 nmm->set_memory_at(ni, result);
1089 }
1090 }
1091 igvn->hash_insert(nmm);
1092 record_for_optimizer(nmm);
1094 // Propagate new memory slices to following MergeMem nodes.
1095 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1096 Node *use = n->fast_out(i);
1097 if (use->is_Call()) {
1098 CallNode* in = use->as_Call();
1099 if (in->proj_out(TypeFunc::Memory) != NULL) {
1100 Node* m = in->proj_out(TypeFunc::Memory);
1101 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
1102 Node* mm = m->fast_out(j);
1103 if (mm->is_MergeMem()) {
1104 mergemem_worklist.append_if_missing(mm);
1105 }
1106 }
1107 }
1108 if (use->is_Allocate()) {
1109 use = use->as_Allocate()->initialization();
1110 if (use == NULL) {
1111 continue;
1112 }
1113 }
1114 }
1115 if (use->is_Initialize()) {
1116 InitializeNode* in = use->as_Initialize();
1117 if (in->proj_out(TypeFunc::Memory) != NULL) {
1118 Node* m = in->proj_out(TypeFunc::Memory);
1119 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) {
1120 Node* mm = m->fast_out(j);
1121 if (mm->is_MergeMem()) {
1122 mergemem_worklist.append_if_missing(mm);
1123 }
1124 }
1125 }
1126 }
1127 }
1128 }
1130 // Phase 4: Update the inputs of non-instance memory Phis and
1131 // the Memory input of memnodes
1132 // First update the inputs of any non-instance Phi's from
1133 // which we split out an instance Phi. Note we don't have
1134 // to recursively process Phi's encounted on the input memory
1135 // chains as is done in split_memory_phi() since they will
1136 // also be processed here.
1137 while (orig_phis.length() != 0) {
1138 PhiNode *phi = orig_phis.pop();
1139 int alias_idx = _compile->get_alias_index(phi->adr_type());
1140 igvn->hash_delete(phi);
1141 for (uint i = 1; i < phi->req(); i++) {
1142 Node *mem = phi->in(i);
1143 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
1144 if (_compile->failing()) {
1145 return;
1146 }
1147 if (mem != new_mem) {
1148 phi->set_req(i, new_mem);
1149 }
1150 }
1151 igvn->hash_insert(phi);
1152 record_for_optimizer(phi);
1153 }
1155 // Update the memory inputs of MemNodes with the value we computed
1156 // in Phase 2.
1157 for (int i = 0; i < _nodes->length(); i++) {
1158 Node *nmem = get_map(i);
1159 if (nmem != NULL) {
1160 Node *n = _nodes->adr_at(i)->_node;
1161 if (n != NULL && n->is_Mem()) {
1162 igvn->hash_delete(n);
1163 n->set_req(MemNode::Memory, nmem);
1164 igvn->hash_insert(n);
1165 record_for_optimizer(n);
1166 }
1167 }
1168 }
1169 }
1171 void ConnectionGraph::compute_escape() {
1173 // 1. Populate Connection Graph with Ideal nodes.
1175 Unique_Node_List worklist_init;
1176 worklist_init.map(_compile->unique(), NULL); // preallocate space
1178 // Initialize worklist
1179 if (_compile->root() != NULL) {
1180 worklist_init.push(_compile->root());
1181 }
1183 GrowableArray<int> cg_worklist;
1184 PhaseGVN* igvn = _compile->initial_gvn();
1185 bool has_allocations = false;
1187 // Push all useful nodes onto CG list and set their type.
1188 for( uint next = 0; next < worklist_init.size(); ++next ) {
1189 Node* n = worklist_init.at(next);
1190 record_for_escape_analysis(n, igvn);
1191 if (n->is_Call() &&
1192 _nodes->adr_at(n->_idx)->node_type() == PointsToNode::JavaObject) {
1193 has_allocations = true;
1194 }
1195 if(n->is_AddP())
1196 cg_worklist.append(n->_idx);
1197 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1198 Node* m = n->fast_out(i); // Get user
1199 worklist_init.push(m);
1200 }
1201 }
1203 if (has_allocations) {
1204 _has_allocations = true;
1205 } else {
1206 _has_allocations = false;
1207 _collecting = false;
1208 return; // Nothing to do.
1209 }
1211 // 2. First pass to create simple CG edges (doesn't require to walk CG).
1212 for( uint next = 0; next < _delayed_worklist.size(); ++next ) {
1213 Node* n = _delayed_worklist.at(next);
1214 build_connection_graph(n, igvn);
1215 }
1217 // 3. Pass to create fields edges (Allocate -F-> AddP).
1218 for( int next = 0; next < cg_worklist.length(); ++next ) {
1219 int ni = cg_worklist.at(next);
1220 build_connection_graph(_nodes->adr_at(ni)->_node, igvn);
1221 }
1223 cg_worklist.clear();
1224 cg_worklist.append(_phantom_object);
1226 // 4. Build Connection Graph which need
1227 // to walk the connection graph.
1228 for (uint ni = 0; ni < (uint)_nodes->length(); ni++) {
1229 PointsToNode* ptn = _nodes->adr_at(ni);
1230 Node *n = ptn->_node;
1231 if (n != NULL) { // Call, AddP, LoadP, StoreP
1232 build_connection_graph(n, igvn);
1233 if (ptn->node_type() != PointsToNode::UnknownType)
1234 cg_worklist.append(n->_idx); // Collect CG nodes
1235 }
1236 }
1238 VectorSet ptset(Thread::current()->resource_area());
1239 GrowableArray<Node*> alloc_worklist;
1240 GrowableArray<int> worklist;
1242 // remove deferred edges from the graph and collect
1243 // information we will need for type splitting
1244 for( int next = 0; next < cg_worklist.length(); ++next ) {
1245 int ni = cg_worklist.at(next);
1246 PointsToNode* ptn = _nodes->adr_at(ni);
1247 PointsToNode::NodeType nt = ptn->node_type();
1248 Node *n = ptn->_node;
1249 if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
1250 remove_deferred(ni);
1251 if (n->is_AddP()) {
1252 // If this AddP computes an address which may point to more that one
1253 // object, nothing the address points to can be scalar replaceable.
1254 Node *base = get_addp_base(n);
1255 ptset.Clear();
1256 PointsTo(ptset, base, igvn);
1257 if (ptset.Size() > 1) {
1258 for( VectorSetI j(&ptset); j.test(); ++j ) {
1259 uint pt = j.elem;
1260 ptnode_adr(pt)->_scalar_replaceable = false;
1261 }
1262 }
1263 }
1264 } else if (nt == PointsToNode::JavaObject && n->is_Call()) {
1265 // Push call on alloc_worlist (alocations are calls)
1266 // for processing by split_unique_types().
1267 alloc_worklist.append(n);
1268 }
1269 }
1271 // push all GlobalEscape nodes on the worklist
1272 for( int next = 0; next < cg_worklist.length(); ++next ) {
1273 int nk = cg_worklist.at(next);
1274 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::GlobalEscape)
1275 worklist.append(nk);
1276 }
1277 // mark all node reachable from GlobalEscape nodes
1278 while(worklist.length() > 0) {
1279 PointsToNode n = _nodes->at(worklist.pop());
1280 for (uint ei = 0; ei < n.edge_count(); ei++) {
1281 uint npi = n.edge_target(ei);
1282 PointsToNode *np = ptnode_adr(npi);
1283 if (np->escape_state() < PointsToNode::GlobalEscape) {
1284 np->set_escape_state(PointsToNode::GlobalEscape);
1285 worklist.append_if_missing(npi);
1286 }
1287 }
1288 }
1290 // push all ArgEscape nodes on the worklist
1291 for( int next = 0; next < cg_worklist.length(); ++next ) {
1292 int nk = cg_worklist.at(next);
1293 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::ArgEscape)
1294 worklist.push(nk);
1295 }
1296 // mark all node reachable from ArgEscape nodes
1297 while(worklist.length() > 0) {
1298 PointsToNode n = _nodes->at(worklist.pop());
1299 for (uint ei = 0; ei < n.edge_count(); ei++) {
1300 uint npi = n.edge_target(ei);
1301 PointsToNode *np = ptnode_adr(npi);
1302 if (np->escape_state() < PointsToNode::ArgEscape) {
1303 np->set_escape_state(PointsToNode::ArgEscape);
1304 worklist.append_if_missing(npi);
1305 }
1306 }
1307 }
1309 // push all NoEscape nodes on the worklist
1310 for( int next = 0; next < cg_worklist.length(); ++next ) {
1311 int nk = cg_worklist.at(next);
1312 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::NoEscape)
1313 worklist.push(nk);
1314 }
1315 // mark all node reachable from NoEscape nodes
1316 while(worklist.length() > 0) {
1317 PointsToNode n = _nodes->at(worklist.pop());
1318 for (uint ei = 0; ei < n.edge_count(); ei++) {
1319 uint npi = n.edge_target(ei);
1320 PointsToNode *np = ptnode_adr(npi);
1321 if (np->escape_state() < PointsToNode::NoEscape) {
1322 np->set_escape_state(PointsToNode::NoEscape);
1323 worklist.append_if_missing(npi);
1324 }
1325 }
1326 }
1328 _collecting = false;
1330 has_allocations = false; // Are there scalar replaceable allocations?
1332 for( int next = 0; next < alloc_worklist.length(); ++next ) {
1333 Node* n = alloc_worklist.at(next);
1334 uint ni = n->_idx;
1335 PointsToNode* ptn = _nodes->adr_at(ni);
1336 PointsToNode::EscapeState es = ptn->escape_state();
1337 if (ptn->escape_state() == PointsToNode::NoEscape &&
1338 ptn->_scalar_replaceable) {
1339 has_allocations = true;
1340 break;
1341 }
1342 }
1343 if (!has_allocations) {
1344 return; // Nothing to do.
1345 }
1347 if(_compile->AliasLevel() >= 3 && EliminateAllocations) {
1348 // Now use the escape information to create unique types for
1349 // unescaped objects
1350 split_unique_types(alloc_worklist);
1351 if (_compile->failing()) return;
1353 // Clean up after split unique types.
1354 ResourceMark rm;
1355 PhaseRemoveUseless pru(_compile->initial_gvn(), _compile->for_igvn());
1357 #ifdef ASSERT
1358 } else if (PrintEscapeAnalysis || PrintEliminateAllocations) {
1359 tty->print("=== No allocations eliminated for ");
1360 C()->method()->print_short_name();
1361 if(!EliminateAllocations) {
1362 tty->print(" since EliminateAllocations is off ===");
1363 } else if(_compile->AliasLevel() < 3) {
1364 tty->print(" since AliasLevel < 3 ===");
1365 }
1366 tty->cr();
1367 #endif
1368 }
1369 }
1371 void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {
1373 switch (call->Opcode()) {
1374 #ifdef ASSERT
1375 case Op_Allocate:
1376 case Op_AllocateArray:
1377 case Op_Lock:
1378 case Op_Unlock:
1379 assert(false, "should be done already");
1380 break;
1381 #endif
1382 case Op_CallLeafNoFP:
1383 {
1384 // Stub calls, objects do not escape but they are not scale replaceable.
1385 // Adjust escape state for outgoing arguments.
1386 const TypeTuple * d = call->tf()->domain();
1387 VectorSet ptset(Thread::current()->resource_area());
1388 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1389 const Type* at = d->field_at(i);
1390 Node *arg = call->in(i)->uncast();
1391 const Type *aat = phase->type(arg);
1392 if (!arg->is_top() && at->isa_ptr() && aat->isa_ptr()) {
1393 assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1394 aat->isa_ptr() != NULL, "expecting an Ptr");
1395 set_escape_state(arg->_idx, PointsToNode::ArgEscape);
1396 if (arg->is_AddP()) {
1397 //
1398 // The inline_native_clone() case when the arraycopy stub is called
1399 // after the allocation before Initialize and CheckCastPP nodes.
1400 //
1401 // Set AddP's base (Allocate) as not scalar replaceable since
1402 // pointer to the base (with offset) is passed as argument.
1403 //
1404 arg = get_addp_base(arg);
1405 }
1406 ptset.Clear();
1407 PointsTo(ptset, arg, phase);
1408 for( VectorSetI j(&ptset); j.test(); ++j ) {
1409 uint pt = j.elem;
1410 set_escape_state(pt, PointsToNode::ArgEscape);
1411 }
1412 }
1413 }
1414 break;
1415 }
1417 case Op_CallStaticJava:
1418 // For a static call, we know exactly what method is being called.
1419 // Use bytecode estimator to record the call's escape affects
1420 {
1421 ciMethod *meth = call->as_CallJava()->method();
1422 BCEscapeAnalyzer *call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1423 // fall-through if not a Java method or no analyzer information
1424 if (call_analyzer != NULL) {
1425 const TypeTuple * d = call->tf()->domain();
1426 VectorSet ptset(Thread::current()->resource_area());
1427 bool copy_dependencies = false;
1428 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1429 const Type* at = d->field_at(i);
1430 int k = i - TypeFunc::Parms;
1432 if (at->isa_oopptr() != NULL) {
1433 Node *arg = call->in(i)->uncast();
1435 bool global_escapes = false;
1436 bool fields_escapes = false;
1437 if (!call_analyzer->is_arg_stack(k)) {
1438 // The argument global escapes, mark everything it could point to
1439 set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
1440 global_escapes = true;
1441 } else {
1442 if (!call_analyzer->is_arg_local(k)) {
1443 // The argument itself doesn't escape, but any fields might
1444 fields_escapes = true;
1445 }
1446 set_escape_state(arg->_idx, PointsToNode::ArgEscape);
1447 copy_dependencies = true;
1448 }
1450 ptset.Clear();
1451 PointsTo(ptset, arg, phase);
1452 for( VectorSetI j(&ptset); j.test(); ++j ) {
1453 uint pt = j.elem;
1454 if (global_escapes) {
1455 //The argument global escapes, mark everything it could point to
1456 set_escape_state(pt, PointsToNode::GlobalEscape);
1457 } else {
1458 if (fields_escapes) {
1459 // The argument itself doesn't escape, but any fields might
1460 add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
1461 }
1462 set_escape_state(pt, PointsToNode::ArgEscape);
1463 }
1464 }
1465 }
1466 }
1467 if (copy_dependencies)
1468 call_analyzer->copy_dependencies(C()->dependencies());
1469 break;
1470 }
1471 }
1473 default:
1474 // Fall-through here if not a Java method or no analyzer information
1475 // or some other type of call, assume the worst case: all arguments
1476 // globally escape.
1477 {
1478 // adjust escape state for outgoing arguments
1479 const TypeTuple * d = call->tf()->domain();
1480 VectorSet ptset(Thread::current()->resource_area());
1481 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1482 const Type* at = d->field_at(i);
1483 if (at->isa_oopptr() != NULL) {
1484 Node *arg = call->in(i)->uncast();
1485 set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
1486 ptset.Clear();
1487 PointsTo(ptset, arg, phase);
1488 for( VectorSetI j(&ptset); j.test(); ++j ) {
1489 uint pt = j.elem;
1490 set_escape_state(pt, PointsToNode::GlobalEscape);
1491 PointsToNode *ptadr = ptnode_adr(pt);
1492 }
1493 }
1494 }
1495 }
1496 }
1497 }
1498 void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
1499 PointsToNode *ptadr = ptnode_adr(resproj->_idx);
1501 CallNode *call = resproj->in(0)->as_Call();
1502 switch (call->Opcode()) {
1503 case Op_Allocate:
1504 {
1505 Node *k = call->in(AllocateNode::KlassNode);
1506 const TypeKlassPtr *kt;
1507 if (k->Opcode() == Op_LoadKlass) {
1508 kt = k->as_Load()->type()->isa_klassptr();
1509 } else {
1510 kt = k->as_Type()->type()->isa_klassptr();
1511 }
1512 assert(kt != NULL, "TypeKlassPtr required.");
1513 ciKlass* cik = kt->klass();
1514 ciInstanceKlass* ciik = cik->as_instance_klass();
1516 PointsToNode *ptadr = ptnode_adr(call->_idx);
1517 PointsToNode::EscapeState es;
1518 uint edge_to;
1519 if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
1520 es = PointsToNode::GlobalEscape;
1521 edge_to = _phantom_object; // Could not be worse
1522 } else {
1523 es = PointsToNode::NoEscape;
1524 edge_to = call->_idx;
1525 }
1526 set_escape_state(call->_idx, es);
1527 add_pointsto_edge(resproj->_idx, edge_to);
1528 _processed.set(resproj->_idx);
1529 break;
1530 }
1532 case Op_AllocateArray:
1533 {
1534 PointsToNode *ptadr = ptnode_adr(call->_idx);
1535 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1536 if (length < 0 || length > EliminateAllocationArraySizeLimit) {
1537 // Not scalar replaceable if the length is not constant or too big.
1538 ptadr->_scalar_replaceable = false;
1539 }
1540 set_escape_state(call->_idx, PointsToNode::NoEscape);
1541 add_pointsto_edge(resproj->_idx, call->_idx);
1542 _processed.set(resproj->_idx);
1543 break;
1544 }
1546 case Op_CallStaticJava:
1547 // For a static call, we know exactly what method is being called.
1548 // Use bytecode estimator to record whether the call's return value escapes
1549 {
1550 bool done = true;
1551 const TypeTuple *r = call->tf()->range();
1552 const Type* ret_type = NULL;
1554 if (r->cnt() > TypeFunc::Parms)
1555 ret_type = r->field_at(TypeFunc::Parms);
1557 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1558 // _multianewarray functions return a TypeRawPtr.
1559 if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
1560 _processed.set(resproj->_idx);
1561 break; // doesn't return a pointer type
1562 }
1563 ciMethod *meth = call->as_CallJava()->method();
1564 const TypeTuple * d = call->tf()->domain();
1565 if (meth == NULL) {
1566 // not a Java method, assume global escape
1567 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1568 if (resproj != NULL)
1569 add_pointsto_edge(resproj->_idx, _phantom_object);
1570 } else {
1571 BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
1572 VectorSet ptset(Thread::current()->resource_area());
1573 bool copy_dependencies = false;
1575 if (call_analyzer->is_return_allocated()) {
1576 // Returns a newly allocated unescaped object, simply
1577 // update dependency information.
1578 // Mark it as NoEscape so that objects referenced by
1579 // it's fields will be marked as NoEscape at least.
1580 set_escape_state(call->_idx, PointsToNode::NoEscape);
1581 if (resproj != NULL)
1582 add_pointsto_edge(resproj->_idx, call->_idx);
1583 copy_dependencies = true;
1584 } else if (call_analyzer->is_return_local() && resproj != NULL) {
1585 // determine whether any arguments are returned
1586 set_escape_state(call->_idx, PointsToNode::NoEscape);
1587 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1588 const Type* at = d->field_at(i);
1590 if (at->isa_oopptr() != NULL) {
1591 Node *arg = call->in(i)->uncast();
1593 if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1594 PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
1595 if (arg_esp->node_type() == PointsToNode::UnknownType)
1596 done = false;
1597 else if (arg_esp->node_type() == PointsToNode::JavaObject)
1598 add_pointsto_edge(resproj->_idx, arg->_idx);
1599 else
1600 add_deferred_edge(resproj->_idx, arg->_idx);
1601 arg_esp->_hidden_alias = true;
1602 }
1603 }
1604 }
1605 copy_dependencies = true;
1606 } else {
1607 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1608 if (resproj != NULL)
1609 add_pointsto_edge(resproj->_idx, _phantom_object);
1610 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1611 const Type* at = d->field_at(i);
1612 if (at->isa_oopptr() != NULL) {
1613 Node *arg = call->in(i)->uncast();
1614 PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
1615 arg_esp->_hidden_alias = true;
1616 }
1617 }
1618 }
1619 if (copy_dependencies)
1620 call_analyzer->copy_dependencies(C()->dependencies());
1621 }
1622 if (done)
1623 _processed.set(resproj->_idx);
1624 break;
1625 }
1627 default:
1628 // Some other type of call, assume the worst case that the
1629 // returned value, if any, globally escapes.
1630 {
1631 const TypeTuple *r = call->tf()->range();
1632 if (r->cnt() > TypeFunc::Parms) {
1633 const Type* ret_type = r->field_at(TypeFunc::Parms);
1635 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1636 // _multianewarray functions return a TypeRawPtr.
1637 if (ret_type->isa_ptr() != NULL) {
1638 PointsToNode *ptadr = ptnode_adr(call->_idx);
1639 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1640 if (resproj != NULL)
1641 add_pointsto_edge(resproj->_idx, _phantom_object);
1642 }
1643 }
1644 _processed.set(resproj->_idx);
1645 }
1646 }
1647 }
1649 // Populate Connection Graph with Ideal nodes and create simple
1650 // connection graph edges (do not need to check the node_type of inputs
1651 // or to call PointsTo() to walk the connection graph).
1652 void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
1653 if (_processed.test(n->_idx))
1654 return; // No need to redefine node's state.
1656 if (n->is_Call()) {
1657 // Arguments to allocation and locking don't escape.
1658 if (n->is_Allocate()) {
1659 add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
1660 record_for_optimizer(n);
1661 } else if (n->is_Lock() || n->is_Unlock()) {
1662 // Put Lock and Unlock nodes on IGVN worklist to process them during
1663 // the first IGVN optimization when escape information is still available.
1664 record_for_optimizer(n);
1665 _processed.set(n->_idx);
1666 } else {
1667 // Have to process call's arguments first.
1668 PointsToNode::NodeType nt = PointsToNode::UnknownType;
1670 // Check if a call returns an object.
1671 const TypeTuple *r = n->as_Call()->tf()->range();
1672 if (r->cnt() > TypeFunc::Parms &&
1673 n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
1674 // Note: use isa_ptr() instead of isa_oopptr() here because
1675 // the _multianewarray functions return a TypeRawPtr.
1676 if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
1677 nt = PointsToNode::JavaObject;
1678 }
1679 }
1680 add_node(n, nt, PointsToNode::UnknownEscape, false);
1681 }
1682 return;
1683 }
1685 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1686 // ThreadLocal has RawPrt type.
1687 switch (n->Opcode()) {
1688 case Op_AddP:
1689 {
1690 add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
1691 break;
1692 }
1693 case Op_CastX2P:
1694 { // "Unsafe" memory access.
1695 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
1696 break;
1697 }
1698 case Op_CastPP:
1699 case Op_CheckCastPP:
1700 {
1701 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1702 int ti = n->in(1)->_idx;
1703 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1704 if (nt == PointsToNode::UnknownType) {
1705 _delayed_worklist.push(n); // Process it later.
1706 break;
1707 } else if (nt == PointsToNode::JavaObject) {
1708 add_pointsto_edge(n->_idx, ti);
1709 } else {
1710 add_deferred_edge(n->_idx, ti);
1711 }
1712 _processed.set(n->_idx);
1713 break;
1714 }
1715 case Op_ConP:
1716 {
1717 // assume all pointer constants globally escape except for null
1718 PointsToNode::EscapeState es;
1719 if (phase->type(n) == TypePtr::NULL_PTR)
1720 es = PointsToNode::NoEscape;
1721 else
1722 es = PointsToNode::GlobalEscape;
1724 add_node(n, PointsToNode::JavaObject, es, true);
1725 break;
1726 }
1727 case Op_CreateEx:
1728 {
1729 // assume that all exception objects globally escape
1730 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
1731 break;
1732 }
1733 case Op_LoadKlass:
1734 {
1735 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
1736 break;
1737 }
1738 case Op_LoadP:
1739 {
1740 const Type *t = phase->type(n);
1741 if (t->isa_ptr() == NULL) {
1742 _processed.set(n->_idx);
1743 return;
1744 }
1745 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1746 break;
1747 }
1748 case Op_Parm:
1749 {
1750 _processed.set(n->_idx); // No need to redefine it state.
1751 uint con = n->as_Proj()->_con;
1752 if (con < TypeFunc::Parms)
1753 return;
1754 const Type *t = n->in(0)->as_Start()->_domain->field_at(con);
1755 if (t->isa_ptr() == NULL)
1756 return;
1757 // We have to assume all input parameters globally escape
1758 // (Note: passing 'false' since _processed is already set).
1759 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
1760 break;
1761 }
1762 case Op_Phi:
1763 {
1764 if (n->as_Phi()->type()->isa_ptr() == NULL) {
1765 // nothing to do if not an oop
1766 _processed.set(n->_idx);
1767 return;
1768 }
1769 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1770 uint i;
1771 for (i = 1; i < n->req() ; i++) {
1772 Node* in = n->in(i);
1773 if (in == NULL)
1774 continue; // ignore NULL
1775 in = in->uncast();
1776 if (in->is_top() || in == n)
1777 continue; // ignore top or inputs which go back this node
1778 int ti = in->_idx;
1779 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1780 if (nt == PointsToNode::UnknownType) {
1781 break;
1782 } else if (nt == PointsToNode::JavaObject) {
1783 add_pointsto_edge(n->_idx, ti);
1784 } else {
1785 add_deferred_edge(n->_idx, ti);
1786 }
1787 }
1788 if (i >= n->req())
1789 _processed.set(n->_idx);
1790 else
1791 _delayed_worklist.push(n);
1792 break;
1793 }
1794 case Op_Proj:
1795 {
1796 // we are only interested in the result projection from a call
1797 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
1798 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1799 process_call_result(n->as_Proj(), phase);
1800 if (!_processed.test(n->_idx)) {
1801 // The call's result may need to be processed later if the call
1802 // returns it's argument and the argument is not processed yet.
1803 _delayed_worklist.push(n);
1804 }
1805 } else {
1806 _processed.set(n->_idx);
1807 }
1808 break;
1809 }
1810 case Op_Return:
1811 {
1812 if( n->req() > TypeFunc::Parms &&
1813 phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
1814 // Treat Return value as LocalVar with GlobalEscape escape state.
1815 add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
1816 int ti = n->in(TypeFunc::Parms)->_idx;
1817 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1818 if (nt == PointsToNode::UnknownType) {
1819 _delayed_worklist.push(n); // Process it later.
1820 break;
1821 } else if (nt == PointsToNode::JavaObject) {
1822 add_pointsto_edge(n->_idx, ti);
1823 } else {
1824 add_deferred_edge(n->_idx, ti);
1825 }
1826 }
1827 _processed.set(n->_idx);
1828 break;
1829 }
1830 case Op_StoreP:
1831 {
1832 const Type *adr_type = phase->type(n->in(MemNode::Address));
1833 if (adr_type->isa_oopptr()) {
1834 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1835 } else {
1836 Node* adr = n->in(MemNode::Address);
1837 if (adr->is_AddP() && phase->type(adr) == TypeRawPtr::NOTNULL &&
1838 adr->in(AddPNode::Address)->is_Proj() &&
1839 adr->in(AddPNode::Address)->in(0)->is_Allocate()) {
1840 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1841 // We are computing a raw address for a store captured
1842 // by an Initialize compute an appropriate address type.
1843 int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
1844 assert(offs != Type::OffsetBot, "offset must be a constant");
1845 } else {
1846 _processed.set(n->_idx);
1847 return;
1848 }
1849 }
1850 break;
1851 }
1852 case Op_StorePConditional:
1853 case Op_CompareAndSwapP:
1854 {
1855 const Type *adr_type = phase->type(n->in(MemNode::Address));
1856 if (adr_type->isa_oopptr()) {
1857 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1858 } else {
1859 _processed.set(n->_idx);
1860 return;
1861 }
1862 break;
1863 }
1864 case Op_ThreadLocal:
1865 {
1866 add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
1867 break;
1868 }
1869 default:
1870 ;
1871 // nothing to do
1872 }
1873 return;
1874 }
1876 void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
1877 // Don't set processed bit for AddP, LoadP, StoreP since
1878 // they may need more then one pass to process.
1879 if (_processed.test(n->_idx))
1880 return; // No need to redefine node's state.
1882 PointsToNode *ptadr = ptnode_adr(n->_idx);
1884 if (n->is_Call()) {
1885 CallNode *call = n->as_Call();
1886 process_call_arguments(call, phase);
1887 _processed.set(n->_idx);
1888 return;
1889 }
1891 switch (n->Opcode()) {
1892 case Op_AddP:
1893 {
1894 Node *base = get_addp_base(n);
1895 // Create a field edge to this node from everything base could point to.
1896 VectorSet ptset(Thread::current()->resource_area());
1897 PointsTo(ptset, base, phase);
1898 for( VectorSetI i(&ptset); i.test(); ++i ) {
1899 uint pt = i.elem;
1900 add_field_edge(pt, n->_idx, address_offset(n, phase));
1901 }
1902 break;
1903 }
1904 case Op_CastX2P:
1905 {
1906 assert(false, "Op_CastX2P");
1907 break;
1908 }
1909 case Op_CastPP:
1910 case Op_CheckCastPP:
1911 {
1912 int ti = n->in(1)->_idx;
1913 if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
1914 add_pointsto_edge(n->_idx, ti);
1915 } else {
1916 add_deferred_edge(n->_idx, ti);
1917 }
1918 _processed.set(n->_idx);
1919 break;
1920 }
1921 case Op_ConP:
1922 {
1923 assert(false, "Op_ConP");
1924 break;
1925 }
1926 case Op_CreateEx:
1927 {
1928 assert(false, "Op_CreateEx");
1929 break;
1930 }
1931 case Op_LoadKlass:
1932 {
1933 assert(false, "Op_LoadKlass");
1934 break;
1935 }
1936 case Op_LoadP:
1937 {
1938 const Type *t = phase->type(n);
1939 #ifdef ASSERT
1940 if (t->isa_ptr() == NULL)
1941 assert(false, "Op_LoadP");
1942 #endif
1944 Node* adr = n->in(MemNode::Address)->uncast();
1945 const Type *adr_type = phase->type(adr);
1946 Node* adr_base;
1947 if (adr->is_AddP()) {
1948 adr_base = get_addp_base(adr);
1949 } else {
1950 adr_base = adr;
1951 }
1953 // For everything "adr_base" could point to, create a deferred edge from
1954 // this node to each field with the same offset.
1955 VectorSet ptset(Thread::current()->resource_area());
1956 PointsTo(ptset, adr_base, phase);
1957 int offset = address_offset(adr, phase);
1958 for( VectorSetI i(&ptset); i.test(); ++i ) {
1959 uint pt = i.elem;
1960 add_deferred_edge_to_fields(n->_idx, pt, offset);
1961 }
1962 break;
1963 }
1964 case Op_Parm:
1965 {
1966 assert(false, "Op_Parm");
1967 break;
1968 }
1969 case Op_Phi:
1970 {
1971 #ifdef ASSERT
1972 if (n->as_Phi()->type()->isa_ptr() == NULL)
1973 assert(false, "Op_Phi");
1974 #endif
1975 for (uint i = 1; i < n->req() ; i++) {
1976 Node* in = n->in(i);
1977 if (in == NULL)
1978 continue; // ignore NULL
1979 in = in->uncast();
1980 if (in->is_top() || in == n)
1981 continue; // ignore top or inputs which go back this node
1982 int ti = in->_idx;
1983 if (_nodes->adr_at(in->_idx)->node_type() == PointsToNode::JavaObject) {
1984 add_pointsto_edge(n->_idx, ti);
1985 } else {
1986 add_deferred_edge(n->_idx, ti);
1987 }
1988 }
1989 _processed.set(n->_idx);
1990 break;
1991 }
1992 case Op_Proj:
1993 {
1994 // we are only interested in the result projection from a call
1995 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
1996 process_call_result(n->as_Proj(), phase);
1997 assert(_processed.test(n->_idx), "all call results should be processed");
1998 } else {
1999 assert(false, "Op_Proj");
2000 }
2001 break;
2002 }
2003 case Op_Return:
2004 {
2005 #ifdef ASSERT
2006 if( n->req() <= TypeFunc::Parms ||
2007 !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
2008 assert(false, "Op_Return");
2009 }
2010 #endif
2011 int ti = n->in(TypeFunc::Parms)->_idx;
2012 if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
2013 add_pointsto_edge(n->_idx, ti);
2014 } else {
2015 add_deferred_edge(n->_idx, ti);
2016 }
2017 _processed.set(n->_idx);
2018 break;
2019 }
2020 case Op_StoreP:
2021 case Op_StorePConditional:
2022 case Op_CompareAndSwapP:
2023 {
2024 Node *adr = n->in(MemNode::Address);
2025 const Type *adr_type = phase->type(adr);
2026 #ifdef ASSERT
2027 if (!adr_type->isa_oopptr())
2028 assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
2029 #endif
2031 assert(adr->is_AddP(), "expecting an AddP");
2032 Node *adr_base = get_addp_base(adr);
2033 Node *val = n->in(MemNode::ValueIn)->uncast();
2034 // For everything "adr_base" could point to, create a deferred edge
2035 // to "val" from each field with the same offset.
2036 VectorSet ptset(Thread::current()->resource_area());
2037 PointsTo(ptset, adr_base, phase);
2038 for( VectorSetI i(&ptset); i.test(); ++i ) {
2039 uint pt = i.elem;
2040 add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
2041 }
2042 break;
2043 }
2044 case Op_ThreadLocal:
2045 {
2046 assert(false, "Op_ThreadLocal");
2047 break;
2048 }
2049 default:
2050 ;
2051 // nothing to do
2052 }
2053 }
2055 #ifndef PRODUCT
2056 void ConnectionGraph::dump() {
2057 PhaseGVN *igvn = _compile->initial_gvn();
2058 bool first = true;
2060 uint size = (uint)_nodes->length();
2061 for (uint ni = 0; ni < size; ni++) {
2062 PointsToNode *ptn = _nodes->adr_at(ni);
2063 PointsToNode::NodeType ptn_type = ptn->node_type();
2065 if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
2066 continue;
2067 PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
2068 if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
2069 if (first) {
2070 tty->cr();
2071 tty->print("======== Connection graph for ");
2072 C()->method()->print_short_name();
2073 tty->cr();
2074 first = false;
2075 }
2076 tty->print("%6d ", ni);
2077 ptn->dump();
2078 // Print all locals which reference this allocation
2079 for (uint li = ni; li < size; li++) {
2080 PointsToNode *ptn_loc = _nodes->adr_at(li);
2081 PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
2082 if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
2083 ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
2084 tty->print("%6d LocalVar [[%d]]", li, ni);
2085 _nodes->adr_at(li)->_node->dump();
2086 }
2087 }
2088 if (Verbose) {
2089 // Print all fields which reference this allocation
2090 for (uint i = 0; i < ptn->edge_count(); i++) {
2091 uint ei = ptn->edge_target(i);
2092 tty->print("%6d Field [[%d]]", ei, ni);
2093 _nodes->adr_at(ei)->_node->dump();
2094 }
2095 }
2096 tty->cr();
2097 }
2098 }
2099 }
2100 #endif