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