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