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