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