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",
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);
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) {
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 // |
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;
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_id(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
1045 // we don't need to do anything, but the users of the memory projection must be pushed
1046 n = n->as_Initialize()->proj_out(TypeFunc::Memory);
1047 if (n == NULL)
1048 continue;
1049 } else {
1050 assert(n->is_Mem(), "memory node required.");
1051 Node *addr = n->in(MemNode::Address);
1052 assert(addr->is_AddP(), "AddP required");
1053 const Type *addr_t = igvn->type(addr);
1054 if (addr_t == Type::TOP)
1055 continue;
1056 assert (addr_t->isa_ptr() != NULL, "pointer type required.");
1057 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
1058 assert ((uint)alias_idx < new_index_end, "wrong alias index");
1059 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
1060 if (_compile->failing()) {
1061 return;
1062 }
1063 if (mem != n->in(MemNode::Memory)) {
1064 set_map(n->_idx, mem);
1065 _nodes->adr_at(n->_idx)->_node = n;
1066 }
1067 if (n->is_Load()) {
1068 continue; // don't push users
1069 } else if (n->is_LoadStore()) {
1070 // get the memory projection
1071 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1072 Node *use = n->fast_out(i);
1073 if (use->Opcode() == Op_SCMemProj) {
1074 n = use;
1075 break;
1076 }
1077 }
1078 assert(n->Opcode() == Op_SCMemProj, "memory projection required");
1079 }
1080 }
1081 // push user on appropriate worklist
1082 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1083 Node *use = n->fast_out(i);
1084 if (use->is_Phi()) {
1085 memnode_worklist.append_if_missing(use);
1206 while (orig_phis.length() != 0) {
1207 PhiNode *phi = orig_phis.pop();
1208 int alias_idx = _compile->get_alias_index(phi->adr_type());
1209 igvn->hash_delete(phi);
1210 for (uint i = 1; i < phi->req(); i++) {
1211 Node *mem = phi->in(i);
1212 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
1213 if (_compile->failing()) {
1214 return;
1215 }
1216 if (mem != new_mem) {
1217 phi->set_req(i, new_mem);
1218 }
1219 }
1220 igvn->hash_insert(phi);
1221 record_for_optimizer(phi);
1222 }
1223
1224 // Update the memory inputs of MemNodes with the value we computed
1225 // in Phase 2.
1226 for (int i = 0; i < _nodes->length(); i++) {
1227 Node *nmem = get_map(i);
1228 if (nmem != NULL) {
1229 Node *n = _nodes->adr_at(i)->_node;
1230 if (n != NULL && n->is_Mem()) {
1231 igvn->hash_delete(n);
1232 n->set_req(MemNode::Memory, nmem);
1233 igvn->hash_insert(n);
1234 record_for_optimizer(n);
1235 }
1236 }
1237 }
1238 }
1239
1240 void ConnectionGraph::compute_escape() {
1241
1242 // 1. Populate Connection Graph (CG) with Ideal nodes.
1243
1244 Unique_Node_List worklist_init;
1245 worklist_init.map(_compile->unique(), NULL); // preallocate space
1246
1247 // Initialize worklist
1248 if (_compile->root() != NULL) {
1249 worklist_init.push(_compile->root());
1250 }
1251
1252 GrowableArray<int> cg_worklist;
1253 PhaseGVN* igvn = _compile->initial_gvn();
1254 bool has_allocations = false;
1255
1256 // Push all useful nodes onto CG list and set their type.
1257 for( uint next = 0; next < worklist_init.size(); ++next ) {
1258 Node* n = worklist_init.at(next);
1259 record_for_escape_analysis(n, igvn);
1260 if (n->is_Call() &&
1261 _nodes->adr_at(n->_idx)->node_type() == PointsToNode::JavaObject) {
1262 has_allocations = true;
1263 }
1264 if(n->is_AddP())
1265 cg_worklist.append(n->_idx);
1266 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1267 Node* m = n->fast_out(i); // Get user
1268 worklist_init.push(m);
1269 }
1270 }
1271
1272 if (has_allocations) {
1273 _has_allocations = true;
1274 } else {
1275 _has_allocations = false;
1276 _collecting = false;
1277 return; // Nothing to do.
1278 }
1279
1280 // 2. First pass to create simple CG edges (doesn't require to walk CG).
1281 for( uint next = 0; next < _delayed_worklist.size(); ++next ) {
1282 Node* n = _delayed_worklist.at(next);
1283 build_connection_graph(n, igvn);
1284 }
1285
1286 // 3. Pass to create fields edges (Allocate -F-> AddP).
1287 for( int next = 0; next < cg_worklist.length(); ++next ) {
1288 int ni = cg_worklist.at(next);
1289 build_connection_graph(_nodes->adr_at(ni)->_node, igvn);
1290 }
1291
1292 cg_worklist.clear();
1293 cg_worklist.append(_phantom_object);
1294
1295 // 4. Build Connection Graph which need
1296 // to walk the connection graph.
1297 for (uint ni = 0; ni < (uint)_nodes->length(); ni++) {
1298 PointsToNode* ptn = _nodes->adr_at(ni);
1299 Node *n = ptn->_node;
1300 if (n != NULL) { // Call, AddP, LoadP, StoreP
1301 build_connection_graph(n, igvn);
1302 if (ptn->node_type() != PointsToNode::UnknownType)
1303 cg_worklist.append(n->_idx); // Collect CG nodes
1304 }
1305 }
1306
1307 VectorSet ptset(Thread::current()->resource_area());
1308 GrowableArray<Node*> alloc_worklist;
1309 GrowableArray<int> worklist;
1310 GrowableArray<uint> deferred_edges;
1311 VectorSet visited(Thread::current()->resource_area());
1312
1313 // remove deferred edges from the graph and collect
1314 // information we will need for type splitting
1315 for( int next = 0; next < cg_worklist.length(); ++next ) {
1316 int ni = cg_worklist.at(next);
1317 PointsToNode* ptn = _nodes->adr_at(ni);
1318 PointsToNode::NodeType nt = ptn->node_type();
1319 Node *n = ptn->_node;
1320 if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
1321 remove_deferred(ni, &deferred_edges, &visited);
1322 if (n->is_AddP()) {
1323 // If this AddP computes an address which may point to more that one
1324 // object or more then one field (array's element), nothing the address
1325 // points to can be scalar replaceable.
1326 Node *base = get_addp_base(n);
1327 ptset.Clear();
1328 PointsTo(ptset, base, igvn);
1329 if (ptset.Size() > 1 ||
1330 (ptset.Size() != 0 && ptn->offset() == Type::OffsetBot)) {
1331 for( VectorSetI j(&ptset); j.test(); ++j ) {
1332 uint pt = j.elem;
1333 ptnode_adr(pt)->_scalar_replaceable = false;
1334 }
1335 }
1336 }
1337 } else if (nt == PointsToNode::JavaObject && n->is_Call()) {
1338 // Push call on alloc_worlist (alocations are calls)
1339 // for processing by split_unique_types().
1340 alloc_worklist.append(n);
1341 }
1342 }
1343
1344 // push all GlobalEscape nodes on the worklist
1345 for( int next = 0; next < cg_worklist.length(); ++next ) {
1346 int nk = cg_worklist.at(next);
1347 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::GlobalEscape)
1348 worklist.append(nk);
1349 }
1350 // mark all node reachable from GlobalEscape nodes
1351 while(worklist.length() > 0) {
1352 PointsToNode n = _nodes->at(worklist.pop());
1353 for (uint ei = 0; ei < n.edge_count(); ei++) {
1354 uint npi = n.edge_target(ei);
1355 PointsToNode *np = ptnode_adr(npi);
1356 if (np->escape_state() < PointsToNode::GlobalEscape) {
1357 np->set_escape_state(PointsToNode::GlobalEscape);
1358 worklist.append_if_missing(npi);
1359 }
1360 }
1361 }
1362
1363 // push all ArgEscape nodes on the worklist
1364 for( int next = 0; next < cg_worklist.length(); ++next ) {
1365 int nk = cg_worklist.at(next);
1366 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::ArgEscape)
1367 worklist.push(nk);
1368 }
1369 // mark all node reachable from ArgEscape nodes
1370 while(worklist.length() > 0) {
1371 PointsToNode n = _nodes->at(worklist.pop());
1372 for (uint ei = 0; ei < n.edge_count(); ei++) {
1373 uint npi = n.edge_target(ei);
1374 PointsToNode *np = ptnode_adr(npi);
1375 if (np->escape_state() < PointsToNode::ArgEscape) {
1376 np->set_escape_state(PointsToNode::ArgEscape);
1377 worklist.append_if_missing(npi);
1378 }
1379 }
1380 }
1381
1382 // push all NoEscape nodes on the worklist
1383 for( int next = 0; next < cg_worklist.length(); ++next ) {
1384 int nk = cg_worklist.at(next);
1385 if (_nodes->adr_at(nk)->escape_state() == PointsToNode::NoEscape)
1386 worklist.push(nk);
1387 }
1388 // mark all node reachable from NoEscape nodes
1389 while(worklist.length() > 0) {
1390 PointsToNode n = _nodes->at(worklist.pop());
1391 for (uint ei = 0; ei < n.edge_count(); ei++) {
1392 uint npi = n.edge_target(ei);
1393 PointsToNode *np = ptnode_adr(npi);
1394 if (np->escape_state() < PointsToNode::NoEscape) {
1395 np->set_escape_state(PointsToNode::NoEscape);
1396 worklist.append_if_missing(npi);
1397 }
1398 }
1399 }
1400
1401 _collecting = false;
1402
1403 has_allocations = false; // Are there scalar replaceable allocations?
1404
1405 for( int next = 0; next < alloc_worklist.length(); ++next ) {
1406 Node* n = alloc_worklist.at(next);
1407 uint ni = n->_idx;
1408 PointsToNode* ptn = _nodes->adr_at(ni);
1409 PointsToNode::EscapeState es = ptn->escape_state();
1410 if (ptn->escape_state() == PointsToNode::NoEscape &&
1411 ptn->_scalar_replaceable) {
1412 has_allocations = true;
1413 break;
1414 }
1415 }
1416 if (!has_allocations) {
1417 return; // Nothing to do.
1418 }
1419
1420 if(_compile->AliasLevel() >= 3 && EliminateAllocations) {
1421 // Now use the escape information to create unique types for
1422 // unescaped objects
1423 split_unique_types(alloc_worklist);
1424 if (_compile->failing()) return;
1425
1426 // Clean up after split unique types.
1427 ResourceMark rm;
1428 PhaseRemoveUseless pru(_compile->initial_gvn(), _compile->for_igvn());
1429
1430 #ifdef ASSERT
1431 } else if (PrintEscapeAnalysis || PrintEliminateAllocations) {
1432 tty->print("=== No allocations eliminated for ");
1433 C()->method()->print_short_name();
1434 if(!EliminateAllocations) {
1435 tty->print(" since EliminateAllocations is off ===");
1436 } else if(_compile->AliasLevel() < 3) {
1437 tty->print(" since AliasLevel < 3 ===");
1438 }
1439 tty->cr();
1440 #endif
1441 }
1442 }
1443
1444 void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {
1445
1446 switch (call->Opcode()) {
1447 #ifdef ASSERT
1448 case Op_Allocate:
1449 case Op_AllocateArray:
1450 case Op_Lock:
1451 case Op_Unlock:
1452 assert(false, "should be done already");
1453 break;
1454 #endif
1455 case Op_CallLeafNoFP:
1456 {
1457 // Stub calls, objects do not escape but they are not scale replaceable.
1458 // Adjust escape state for outgoing arguments.
1459 const TypeTuple * d = call->tf()->domain();
1460 VectorSet ptset(Thread::current()->resource_area());
1461 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1521 }
1522
1523 ptset.Clear();
1524 PointsTo(ptset, arg, phase);
1525 for( VectorSetI j(&ptset); j.test(); ++j ) {
1526 uint pt = j.elem;
1527 if (global_escapes) {
1528 //The argument global escapes, mark everything it could point to
1529 set_escape_state(pt, PointsToNode::GlobalEscape);
1530 } else {
1531 if (fields_escapes) {
1532 // The argument itself doesn't escape, but any fields might
1533 add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
1534 }
1535 set_escape_state(pt, PointsToNode::ArgEscape);
1536 }
1537 }
1538 }
1539 }
1540 if (copy_dependencies)
1541 call_analyzer->copy_dependencies(C()->dependencies());
1542 break;
1543 }
1544 }
1545
1546 default:
1547 // Fall-through here if not a Java method or no analyzer information
1548 // or some other type of call, assume the worst case: all arguments
1549 // globally escape.
1550 {
1551 // adjust escape state for outgoing arguments
1552 const TypeTuple * d = call->tf()->domain();
1553 VectorSet ptset(Thread::current()->resource_area());
1554 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1555 const Type* at = d->field_at(i);
1556 if (at->isa_oopptr() != NULL) {
1557 Node *arg = call->in(i)->uncast();
1558 set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
1559 ptset.Clear();
1560 PointsTo(ptset, arg, phase);
1561 for( VectorSetI j(&ptset); j.test(); ++j ) {
1562 uint pt = j.elem;
1563 set_escape_state(pt, PointsToNode::GlobalEscape);
1564 PointsToNode *ptadr = ptnode_adr(pt);
1565 }
1566 }
1567 }
1568 }
1569 }
1570 }
1571 void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
1572 PointsToNode *ptadr = ptnode_adr(resproj->_idx);
1573
1574 CallNode *call = resproj->in(0)->as_Call();
1575 switch (call->Opcode()) {
1576 case Op_Allocate:
1577 {
1578 Node *k = call->in(AllocateNode::KlassNode);
1579 const TypeKlassPtr *kt;
1580 if (k->Opcode() == Op_LoadKlass) {
1581 kt = k->as_Load()->type()->isa_klassptr();
1582 } else {
1583 // Also works for DecodeN(LoadNKlass).
1584 kt = k->as_Type()->type()->isa_klassptr();
1585 }
1586 assert(kt != NULL, "TypeKlassPtr required.");
1587 ciKlass* cik = kt->klass();
1588 ciInstanceKlass* ciik = cik->as_instance_klass();
1589
1590 PointsToNode *ptadr = ptnode_adr(call->_idx);
1591 PointsToNode::EscapeState es;
1592 uint edge_to;
1593 if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
1594 es = PointsToNode::GlobalEscape;
1595 edge_to = _phantom_object; // Could not be worse
1596 } else {
1597 es = PointsToNode::NoEscape;
1598 edge_to = call->_idx;
1599 }
1600 set_escape_state(call->_idx, es);
1601 add_pointsto_edge(resproj->_idx, edge_to);
1602 _processed.set(resproj->_idx);
1603 break;
1604 }
1605
1606 case Op_AllocateArray:
1607 {
1608 PointsToNode *ptadr = ptnode_adr(call->_idx);
1609 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1610 if (length < 0 || length > EliminateAllocationArraySizeLimit) {
1611 // Not scalar replaceable if the length is not constant or too big.
1612 ptadr->_scalar_replaceable = false;
1613 }
1614 set_escape_state(call->_idx, PointsToNode::NoEscape);
1615 add_pointsto_edge(resproj->_idx, call->_idx);
1616 _processed.set(resproj->_idx);
1617 break;
1618 }
1619
1620 case Op_CallStaticJava:
1621 // For a static call, we know exactly what method is being called.
1622 // Use bytecode estimator to record whether the call's return value escapes
1623 {
1624 bool done = true;
1625 const TypeTuple *r = call->tf()->range();
1626 const Type* ret_type = NULL;
1627
1628 if (r->cnt() > TypeFunc::Parms)
1629 ret_type = r->field_at(TypeFunc::Parms);
1630
1631 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1632 // _multianewarray functions return a TypeRawPtr.
1633 if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
1634 _processed.set(resproj->_idx);
1635 break; // doesn't return a pointer type
1636 }
1637 ciMethod *meth = call->as_CallJava()->method();
1638 const TypeTuple * d = call->tf()->domain();
1639 if (meth == NULL) {
1640 // not a Java method, assume global escape
1641 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1642 if (resproj != NULL)
1643 add_pointsto_edge(resproj->_idx, _phantom_object);
1644 } else {
1645 BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
1646 VectorSet ptset(Thread::current()->resource_area());
1647 bool copy_dependencies = false;
1648
1649 if (call_analyzer->is_return_allocated()) {
1650 // Returns a newly allocated unescaped object, simply
1651 // update dependency information.
1652 // Mark it as NoEscape so that objects referenced by
1653 // it's fields will be marked as NoEscape at least.
1654 set_escape_state(call->_idx, PointsToNode::NoEscape);
1655 if (resproj != NULL)
1656 add_pointsto_edge(resproj->_idx, call->_idx);
1657 copy_dependencies = true;
1658 } else if (call_analyzer->is_return_local() && resproj != NULL) {
1659 // determine whether any arguments are returned
1660 set_escape_state(call->_idx, PointsToNode::NoEscape);
1661 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1662 const Type* at = d->field_at(i);
1663
1664 if (at->isa_oopptr() != NULL) {
1665 Node *arg = call->in(i)->uncast();
1666
1667 if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1668 PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
1669 if (arg_esp->node_type() == PointsToNode::UnknownType)
1670 done = false;
1671 else if (arg_esp->node_type() == PointsToNode::JavaObject)
1672 add_pointsto_edge(resproj->_idx, arg->_idx);
1673 else
1674 add_deferred_edge(resproj->_idx, arg->_idx);
1675 arg_esp->_hidden_alias = true;
1676 }
1677 }
1678 }
1679 copy_dependencies = true;
1680 } else {
1681 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1682 if (resproj != NULL)
1683 add_pointsto_edge(resproj->_idx, _phantom_object);
1684 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1685 const Type* at = d->field_at(i);
1686 if (at->isa_oopptr() != NULL) {
1687 Node *arg = call->in(i)->uncast();
1688 PointsToNode *arg_esp = _nodes->adr_at(arg->_idx);
1689 arg_esp->_hidden_alias = true;
1690 }
1691 }
1692 }
1693 if (copy_dependencies)
1694 call_analyzer->copy_dependencies(C()->dependencies());
1695 }
1696 if (done)
1697 _processed.set(resproj->_idx);
1698 break;
1699 }
1700
1701 default:
1702 // Some other type of call, assume the worst case that the
1703 // returned value, if any, globally escapes.
1704 {
1705 const TypeTuple *r = call->tf()->range();
1706 if (r->cnt() > TypeFunc::Parms) {
1707 const Type* ret_type = r->field_at(TypeFunc::Parms);
1708
1709 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1710 // _multianewarray functions return a TypeRawPtr.
1711 if (ret_type->isa_ptr() != NULL) {
1712 PointsToNode *ptadr = ptnode_adr(call->_idx);
1713 set_escape_state(call->_idx, PointsToNode::GlobalEscape);
1714 if (resproj != NULL)
1715 add_pointsto_edge(resproj->_idx, _phantom_object);
1716 }
1717 }
1718 _processed.set(resproj->_idx);
1719 }
1720 }
1721 }
1722
1723 // Populate Connection Graph with Ideal nodes and create simple
1724 // connection graph edges (do not need to check the node_type of inputs
1725 // or to call PointsTo() to walk the connection graph).
1726 void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
1727 if (_processed.test(n->_idx))
1728 return; // No need to redefine node's state.
1729
1730 if (n->is_Call()) {
1731 // Arguments to allocation and locking don't escape.
1732 if (n->is_Allocate()) {
1733 add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
1734 record_for_optimizer(n);
1735 } else if (n->is_Lock() || n->is_Unlock()) {
1736 // Put Lock and Unlock nodes on IGVN worklist to process them during
1737 // the first IGVN optimization when escape information is still available.
1738 record_for_optimizer(n);
1739 _processed.set(n->_idx);
1740 } else {
1741 // Have to process call's arguments first.
1742 PointsToNode::NodeType nt = PointsToNode::UnknownType;
1743
1744 // Check if a call returns an object.
1745 const TypeTuple *r = n->as_Call()->tf()->range();
1746 if (r->cnt() > TypeFunc::Parms &&
1747 n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
1748 // Note: use isa_ptr() instead of isa_oopptr() here because
1749 // the _multianewarray functions return a TypeRawPtr.
1750 if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
1751 nt = PointsToNode::JavaObject;
1752 }
1753 }
1754 add_node(n, nt, PointsToNode::UnknownEscape, false);
1755 }
1756 return;
1757 }
1758
1759 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1760 // ThreadLocal has RawPrt type.
1761 switch (n->Opcode()) {
1762 case Op_AddP:
1763 {
1764 add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
1765 break;
1766 }
1767 case Op_CastX2P:
1768 { // "Unsafe" memory access.
1769 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
1770 break;
1771 }
1772 case Op_CastPP:
1773 case Op_CheckCastPP:
1774 case Op_EncodeP:
1775 case Op_DecodeN:
1776 {
1777 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1778 int ti = n->in(1)->_idx;
1779 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1780 if (nt == PointsToNode::UnknownType) {
1781 _delayed_worklist.push(n); // Process it later.
1782 break;
1783 } else if (nt == PointsToNode::JavaObject) {
1784 add_pointsto_edge(n->_idx, ti);
1785 } else {
1786 add_deferred_edge(n->_idx, ti);
1787 }
1788 _processed.set(n->_idx);
1789 break;
1790 }
1791 case Op_ConP:
1792 {
1793 // assume all pointer constants globally escape except for null
1794 PointsToNode::EscapeState es;
1795 if (phase->type(n) == TypePtr::NULL_PTR)
1796 es = PointsToNode::NoEscape;
1797 else
1798 es = PointsToNode::GlobalEscape;
1799
1849 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
1850 break;
1851 }
1852 case Op_Phi:
1853 {
1854 if (n->as_Phi()->type()->isa_ptr() == NULL) {
1855 // nothing to do if not an oop
1856 _processed.set(n->_idx);
1857 return;
1858 }
1859 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1860 uint i;
1861 for (i = 1; i < n->req() ; i++) {
1862 Node* in = n->in(i);
1863 if (in == NULL)
1864 continue; // ignore NULL
1865 in = in->uncast();
1866 if (in->is_top() || in == n)
1867 continue; // ignore top or inputs which go back this node
1868 int ti = in->_idx;
1869 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1870 if (nt == PointsToNode::UnknownType) {
1871 break;
1872 } else if (nt == PointsToNode::JavaObject) {
1873 add_pointsto_edge(n->_idx, ti);
1874 } else {
1875 add_deferred_edge(n->_idx, ti);
1876 }
1877 }
1878 if (i >= n->req())
1879 _processed.set(n->_idx);
1880 else
1881 _delayed_worklist.push(n);
1882 break;
1883 }
1884 case Op_Proj:
1885 {
1886 // we are only interested in the result projection from a call
1887 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
1888 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1889 process_call_result(n->as_Proj(), phase);
1890 if (!_processed.test(n->_idx)) {
1891 // The call's result may need to be processed later if the call
1892 // returns it's argument and the argument is not processed yet.
1893 _delayed_worklist.push(n);
1894 }
1895 } else {
1896 _processed.set(n->_idx);
1897 }
1898 break;
1899 }
1900 case Op_Return:
1901 {
1902 if( n->req() > TypeFunc::Parms &&
1903 phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
1904 // Treat Return value as LocalVar with GlobalEscape escape state.
1905 add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
1906 int ti = n->in(TypeFunc::Parms)->_idx;
1907 PointsToNode::NodeType nt = _nodes->adr_at(ti)->node_type();
1908 if (nt == PointsToNode::UnknownType) {
1909 _delayed_worklist.push(n); // Process it later.
1910 break;
1911 } else if (nt == PointsToNode::JavaObject) {
1912 add_pointsto_edge(n->_idx, ti);
1913 } else {
1914 add_deferred_edge(n->_idx, ti);
1915 }
1916 }
1917 _processed.set(n->_idx);
1918 break;
1919 }
1920 case Op_StoreP:
1921 case Op_StoreN:
1922 {
1923 const Type *adr_type = phase->type(n->in(MemNode::Address));
1924 adr_type = adr_type->make_ptr();
1925 if (adr_type->isa_oopptr()) {
1926 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1927 } else {
1951 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1952 } else {
1953 _processed.set(n->_idx);
1954 return;
1955 }
1956 break;
1957 }
1958 case Op_ThreadLocal:
1959 {
1960 add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
1961 break;
1962 }
1963 default:
1964 ;
1965 // nothing to do
1966 }
1967 return;
1968 }
1969
1970 void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
1971 // Don't set processed bit for AddP, LoadP, StoreP since
1972 // they may need more then one pass to process.
1973 if (_processed.test(n->_idx))
1974 return; // No need to redefine node's state.
1975
1976 PointsToNode *ptadr = ptnode_adr(n->_idx);
1977
1978 if (n->is_Call()) {
1979 CallNode *call = n->as_Call();
1980 process_call_arguments(call, phase);
1981 _processed.set(n->_idx);
1982 return;
1983 }
1984
1985 switch (n->Opcode()) {
1986 case Op_AddP:
1987 {
1988 Node *base = get_addp_base(n);
1989 // Create a field edge to this node from everything base could point to.
1990 VectorSet ptset(Thread::current()->resource_area());
1991 PointsTo(ptset, base, phase);
1992 for( VectorSetI i(&ptset); i.test(); ++i ) {
1993 uint pt = i.elem;
1994 add_field_edge(pt, n->_idx, address_offset(n, phase));
1995 }
1996 break;
1997 }
1998 case Op_CastX2P:
1999 {
2000 assert(false, "Op_CastX2P");
2001 break;
2002 }
2003 case Op_CastPP:
2004 case Op_CheckCastPP:
2005 case Op_EncodeP:
2006 case Op_DecodeN:
2007 {
2008 int ti = n->in(1)->_idx;
2009 if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
2010 add_pointsto_edge(n->_idx, ti);
2011 } else {
2012 add_deferred_edge(n->_idx, ti);
2013 }
2014 _processed.set(n->_idx);
2015 break;
2016 }
2017 case Op_ConP:
2018 {
2019 assert(false, "Op_ConP");
2020 break;
2021 }
2022 case Op_ConN:
2023 {
2024 assert(false, "Op_ConN");
2025 break;
2026 }
2027 case Op_CreateEx:
2028 {
2029 assert(false, "Op_CreateEx");
2030 break;
2031 }
2032 case Op_LoadKlass:
2033 case Op_LoadNKlass:
2034 {
2043 if (!t->isa_narrowoop() && t->isa_ptr() == NULL)
2044 assert(false, "Op_LoadP");
2045 #endif
2046
2047 Node* adr = n->in(MemNode::Address)->uncast();
2048 const Type *adr_type = phase->type(adr);
2049 Node* adr_base;
2050 if (adr->is_AddP()) {
2051 adr_base = get_addp_base(adr);
2052 } else {
2053 adr_base = adr;
2054 }
2055
2056 // For everything "adr_base" could point to, create a deferred edge from
2057 // this node to each field with the same offset.
2058 VectorSet ptset(Thread::current()->resource_area());
2059 PointsTo(ptset, adr_base, phase);
2060 int offset = address_offset(adr, phase);
2061 for( VectorSetI i(&ptset); i.test(); ++i ) {
2062 uint pt = i.elem;
2063 add_deferred_edge_to_fields(n->_idx, pt, offset);
2064 }
2065 break;
2066 }
2067 case Op_Parm:
2068 {
2069 assert(false, "Op_Parm");
2070 break;
2071 }
2072 case Op_Phi:
2073 {
2074 #ifdef ASSERT
2075 if (n->as_Phi()->type()->isa_ptr() == NULL)
2076 assert(false, "Op_Phi");
2077 #endif
2078 for (uint i = 1; i < n->req() ; i++) {
2079 Node* in = n->in(i);
2080 if (in == NULL)
2081 continue; // ignore NULL
2082 in = in->uncast();
2083 if (in->is_top() || in == n)
2084 continue; // ignore top or inputs which go back this node
2085 int ti = in->_idx;
2086 if (_nodes->adr_at(in->_idx)->node_type() == PointsToNode::JavaObject) {
2087 add_pointsto_edge(n->_idx, ti);
2088 } else {
2089 add_deferred_edge(n->_idx, ti);
2090 }
2091 }
2092 _processed.set(n->_idx);
2093 break;
2094 }
2095 case Op_Proj:
2096 {
2097 // we are only interested in the result projection from a call
2098 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
2099 process_call_result(n->as_Proj(), phase);
2100 assert(_processed.test(n->_idx), "all call results should be processed");
2101 } else {
2102 assert(false, "Op_Proj");
2103 }
2104 break;
2105 }
2106 case Op_Return:
2107 {
2108 #ifdef ASSERT
2109 if( n->req() <= TypeFunc::Parms ||
2110 !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
2111 assert(false, "Op_Return");
2112 }
2113 #endif
2114 int ti = n->in(TypeFunc::Parms)->_idx;
2115 if (_nodes->adr_at(ti)->node_type() == PointsToNode::JavaObject) {
2116 add_pointsto_edge(n->_idx, ti);
2117 } else {
2118 add_deferred_edge(n->_idx, ti);
2119 }
2120 _processed.set(n->_idx);
2121 break;
2122 }
2123 case Op_StoreP:
2124 case Op_StoreN:
2125 case Op_StorePConditional:
2126 case Op_CompareAndSwapP:
2127 case Op_CompareAndSwapN:
2128 {
2129 Node *adr = n->in(MemNode::Address);
2130 const Type *adr_type = phase->type(adr)->make_ptr();
2131 #ifdef ASSERT
2132 if (!adr_type->isa_oopptr())
2133 assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
2134 #endif
2135
2136 assert(adr->is_AddP(), "expecting an AddP");
2137 Node *adr_base = get_addp_base(adr);
2138 Node *val = n->in(MemNode::ValueIn)->uncast();
2139 // For everything "adr_base" could point to, create a deferred edge
2140 // to "val" from each field with the same offset.
2145 add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
2146 }
2147 break;
2148 }
2149 case Op_ThreadLocal:
2150 {
2151 assert(false, "Op_ThreadLocal");
2152 break;
2153 }
2154 default:
2155 ;
2156 // nothing to do
2157 }
2158 }
2159
2160 #ifndef PRODUCT
2161 void ConnectionGraph::dump() {
2162 PhaseGVN *igvn = _compile->initial_gvn();
2163 bool first = true;
2164
2165 uint size = (uint)_nodes->length();
2166 for (uint ni = 0; ni < size; ni++) {
2167 PointsToNode *ptn = _nodes->adr_at(ni);
2168 PointsToNode::NodeType ptn_type = ptn->node_type();
2169
2170 if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
2171 continue;
2172 PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
2173 if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
2174 if (first) {
2175 tty->cr();
2176 tty->print("======== Connection graph for ");
2177 C()->method()->print_short_name();
2178 tty->cr();
2179 first = false;
2180 }
2181 tty->print("%6d ", ni);
2182 ptn->dump();
2183 // Print all locals which reference this allocation
2184 for (uint li = ni; li < size; li++) {
2185 PointsToNode *ptn_loc = _nodes->adr_at(li);
2186 PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
2187 if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
2188 ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
2189 tty->print("%6d LocalVar [[%d]]", li, ni);
2190 _nodes->adr_at(li)->_node->dump();
2191 }
2192 }
2193 if (Verbose) {
2194 // Print all fields which reference this allocation
2195 for (uint i = 0; i < ptn->edge_count(); i++) {
2196 uint ei = ptn->edge_target(i);
2197 tty->print("%6d Field [[%d]]", ei, ni);
2198 _nodes->adr_at(ei)->_node->dump();
2199 }
2200 }
2201 tty->cr();
2202 }
2203 }
2204 }
2205 #endif
|
1 /*
2 * Copyright 2005-2008 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 void PointsToNode::add_edge(uint targIdx, PointsToNode::EdgeType et) {
29 uint v = (targIdx << EdgeShift) + ((uint) et);
30 if (_edges == NULL) {
31 Arena *a = Compile::current()->comp_arena();
32 _edges = new(a) GrowableArray<uint>(a, INITIAL_EDGE_COUNT, 0, 0);
33 }
34 _edges->append_if_missing(v);
35 }
36
37 void PointsToNode::remove_edge(uint targIdx, PointsToNode::EdgeType et) {
38 uint v = (targIdx << EdgeShift) + ((uint) et);
39
40 _edges->remove(v);
41 }
42
43 #ifndef PRODUCT
44 static const char *node_type_names[] = {
45 "UnknownType",
46 "JavaObject",
47 "LocalVar",
60 "P", // PointsToEdge
61 "D", // DeferredEdge
62 "F" // FieldEdge
63 };
64
65 void PointsToNode::dump() const {
66 NodeType nt = node_type();
67 EscapeState es = escape_state();
68 tty->print("%s %s %s [[", node_type_names[(int) nt], esc_names[(int) es], _scalar_replaceable ? "" : "NSR");
69 for (uint i = 0; i < edge_count(); i++) {
70 tty->print(" %d%s", edge_target(i), edge_type_suffix[(int) edge_type(i)]);
71 }
72 tty->print("]] ");
73 if (_node == NULL)
74 tty->print_cr("<null>");
75 else
76 _node->dump();
77 }
78 #endif
79
80 ConnectionGraph::ConnectionGraph(Compile * C) :
81 _nodes(C->comp_arena(), C->unique(), C->unique(), PointsToNode()),
82 _processed(C->comp_arena()),
83 _collecting(true),
84 _compile(C),
85 _node_map(C->comp_arena()) {
86
87 _phantom_object = C->top()->_idx;
88 PointsToNode *phn = ptnode_adr(_phantom_object);
89 phn->_node = C->top();
90 phn->set_node_type(PointsToNode::JavaObject);
91 phn->set_escape_state(PointsToNode::GlobalEscape);
92 }
93
94 void ConnectionGraph::add_pointsto_edge(uint from_i, uint to_i) {
95 PointsToNode *f = ptnode_adr(from_i);
96 PointsToNode *t = ptnode_adr(to_i);
97
98 assert(f->node_type() != PointsToNode::UnknownType && t->node_type() != PointsToNode::UnknownType, "node types must be set");
99 assert(f->node_type() == PointsToNode::LocalVar || f->node_type() == PointsToNode::Field, "invalid source of PointsTo edge");
100 assert(t->node_type() == PointsToNode::JavaObject, "invalid destination of PointsTo edge");
101 f->add_edge(to_i, PointsToNode::PointsToEdge);
102 }
103
104 void ConnectionGraph::add_deferred_edge(uint from_i, uint to_i) {
105 PointsToNode *f = ptnode_adr(from_i);
106 PointsToNode *t = ptnode_adr(to_i);
156 PointsToNode::EscapeState es, bool done) {
157 PointsToNode* ptadr = ptnode_adr(n->_idx);
158 ptadr->_node = n;
159 ptadr->set_node_type(nt);
160
161 // inline set_escape_state(idx, es);
162 PointsToNode::EscapeState old_es = ptadr->escape_state();
163 if (es > old_es)
164 ptadr->set_escape_state(es);
165
166 if (done)
167 _processed.set(n->_idx);
168 }
169
170 PointsToNode::EscapeState ConnectionGraph::escape_state(Node *n, PhaseTransform *phase) {
171 uint idx = n->_idx;
172 PointsToNode::EscapeState es;
173
174 // If we are still collecting or there were no non-escaping allocations
175 // we don't know the answer yet
176 if (_collecting)
177 return PointsToNode::UnknownEscape;
178
179 // if the node was created after the escape computation, return
180 // UnknownEscape
181 if (idx >= nodes_size())
182 return PointsToNode::UnknownEscape;
183
184 es = ptnode_adr(idx)->escape_state();
185
186 // if we have already computed a value, return it
187 if (es != PointsToNode::UnknownEscape)
188 return es;
189
190 // PointsTo() calls n->uncast() which can return a new ideal node.
191 if (n->uncast()->_idx >= nodes_size())
192 return PointsToNode::UnknownEscape;
193
194 // compute max escape state of anything this node could point to
195 VectorSet ptset(Thread::current()->resource_area());
196 PointsTo(ptset, n, phase);
197 for(VectorSetI i(&ptset); i.test() && es != PointsToNode::GlobalEscape; ++i) {
198 uint pt = i.elem;
199 PointsToNode::EscapeState pes = ptnode_adr(pt)->escape_state();
200 if (pes > es)
201 es = pes;
202 }
203 // cache the computed escape state
204 assert(es != PointsToNode::UnknownEscape, "should have computed an escape state");
205 ptnode_adr(idx)->set_escape_state(es);
206 return es;
207 }
208
209 void ConnectionGraph::PointsTo(VectorSet &ptset, Node * n, PhaseTransform *phase) {
210 VectorSet visited(Thread::current()->resource_area());
211 GrowableArray<uint> worklist;
212
213 #ifdef ASSERT
214 Node *orig_n = n;
215 #endif
216
217 n = n->uncast();
218 PointsToNode* npt = ptnode_adr(n->_idx);
219
220 // If we have a JavaObject, return just that object
221 if (npt->node_type() == PointsToNode::JavaObject) {
222 ptset.set(n->_idx);
223 return;
224 }
225 #ifdef ASSERT
226 if (npt->_node == NULL) {
227 if (orig_n != n)
228 orig_n->dump();
229 n->dump();
230 assert(npt->_node != NULL, "unregistered node");
231 }
232 #endif
233 worklist.push(n->_idx);
234 while(worklist.length() > 0) {
235 int ni = worklist.pop();
236 PointsToNode* pn = ptnode_adr(ni);
237 if (!visited.test_set(ni)) {
238 // ensure that all inputs of a Phi have been processed
239 assert(!_collecting || !pn->_node->is_Phi() || _processed.test(ni),"");
240
241 int edges_processed = 0;
242 for (uint e = 0; e < pn->edge_count(); e++) {
243 uint etgt = pn->edge_target(e);
244 PointsToNode::EdgeType et = pn->edge_type(e);
245 if (et == PointsToNode::PointsToEdge) {
246 ptset.set(etgt);
247 edges_processed++;
248 } else if (et == PointsToNode::DeferredEdge) {
249 worklist.push(etgt);
250 edges_processed++;
251 } else {
252 assert(false,"neither PointsToEdge or DeferredEdge");
253 }
254 }
255 if (edges_processed == 0) {
256 // no deferred or pointsto edges found. Assume the value was set
257 // outside this method. Add the phantom object to the pointsto set.
258 ptset.set(_phantom_object);
259 }
260 }
261 }
262 }
263
264 void ConnectionGraph::remove_deferred(uint ni, GrowableArray<uint>* deferred_edges, VectorSet* visited) {
300 ptn->set_escape_state(PointsToNode::GlobalEscape);
301 }
302 break;
303 case PointsToNode::DeferredEdge:
304 deferred_edges->append(n1);
305 break;
306 case PointsToNode::FieldEdge:
307 assert(false, "invalid connection graph");
308 break;
309 }
310 }
311 }
312 }
313
314
315 // Add an edge to node given by "to_i" from any field of adr_i whose offset
316 // matches "offset" A deferred edge is added if to_i is a LocalVar, and
317 // a pointsto edge is added if it is a JavaObject
318
319 void ConnectionGraph::add_edge_from_fields(uint adr_i, uint to_i, int offs) {
320 PointsToNode* an = ptnode_adr(adr_i);
321 PointsToNode* to = ptnode_adr(to_i);
322 bool deferred = (to->node_type() == PointsToNode::LocalVar);
323
324 for (uint fe = 0; fe < an->edge_count(); fe++) {
325 assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
326 int fi = an->edge_target(fe);
327 PointsToNode* pf = ptnode_adr(fi);
328 int po = pf->offset();
329 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
330 if (deferred)
331 add_deferred_edge(fi, to_i);
332 else
333 add_pointsto_edge(fi, to_i);
334 }
335 }
336 }
337
338 // Add a deferred edge from node given by "from_i" to any field of adr_i
339 // whose offset matches "offset".
340 void ConnectionGraph::add_deferred_edge_to_fields(uint from_i, uint adr_i, int offs) {
341 PointsToNode* an = ptnode_adr(adr_i);
342 for (uint fe = 0; fe < an->edge_count(); fe++) {
343 assert(an->edge_type(fe) == PointsToNode::FieldEdge, "expecting a field edge");
344 int fi = an->edge_target(fe);
345 PointsToNode* pf = ptnode_adr(fi);
346 int po = pf->offset();
347 if (pf->edge_count() == 0) {
348 // we have not seen any stores to this field, assume it was set outside this method
349 add_pointsto_edge(fi, _phantom_object);
350 }
351 if (po == offs || po == Type::OffsetBot || offs == Type::OffsetBot) {
352 add_deferred_edge(from_i, fi);
353 }
354 }
355 }
356
357 // Helper functions
358
359 static Node* get_addp_base(Node *addp) {
360 assert(addp->is_AddP(), "must be AddP");
361 //
362 // AddP cases for Base and Address inputs:
363 // case #1. Direct object's field reference:
364 // Allocate
365 // |
366 // Proj #5 ( oop result )
367 // |
813 // 50 StoreP 35 7 30 ... alias_index=6
814 // 60 StoreP 45 40 20 ... alias_index=4
815 // 70 LoadP _ 50 30 ... alias_index=6
816 // 80 Phi 75 40 60 Memory alias_index=4
817 // 120 Phi 75 50 50 Memory alias_index=6
818 // 90 LoadP _ 120 30 ... alias_index=6
819 // 100 LoadP _ 80 20 ... alias_index=4
820 //
821 void ConnectionGraph::split_unique_types(GrowableArray<Node *> &alloc_worklist) {
822 GrowableArray<Node *> memnode_worklist;
823 GrowableArray<Node *> mergemem_worklist;
824 GrowableArray<PhiNode *> orig_phis;
825 PhaseGVN *igvn = _compile->initial_gvn();
826 uint new_index_start = (uint) _compile->num_alias_types();
827 VectorSet visited(Thread::current()->resource_area());
828 VectorSet ptset(Thread::current()->resource_area());
829
830
831 // Phase 1: Process possible allocations from alloc_worklist.
832 // Create instance types for the CheckCastPP for allocations where possible.
833 //
834 // (Note: don't forget to change the order of the second AddP node on
835 // the alloc_worklist if the order of the worklist processing is changed,
836 // see the comment in find_second_addp().)
837 //
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 = ptnode_adr(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;
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_id(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 = ptnode_adr(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
1045 // we don't need to do anything, but the users of the memory projection must be pushed
1046 n = n->as_Initialize()->proj_out(TypeFunc::Memory);
1047 if (n == NULL)
1048 continue;
1049 } else {
1050 assert(n->is_Mem(), "memory node required.");
1051 Node *addr = n->in(MemNode::Address);
1052 assert(addr->is_AddP(), "AddP required");
1053 const Type *addr_t = igvn->type(addr);
1054 if (addr_t == Type::TOP)
1055 continue;
1056 assert (addr_t->isa_ptr() != NULL, "pointer type required.");
1057 int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
1058 assert ((uint)alias_idx < new_index_end, "wrong alias index");
1059 Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis, igvn);
1060 if (_compile->failing()) {
1061 return;
1062 }
1063 if (mem != n->in(MemNode::Memory)) {
1064 set_map(n->_idx, mem);
1065 ptnode_adr(n->_idx)->_node = n;
1066 }
1067 if (n->is_Load()) {
1068 continue; // don't push users
1069 } else if (n->is_LoadStore()) {
1070 // get the memory projection
1071 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1072 Node *use = n->fast_out(i);
1073 if (use->Opcode() == Op_SCMemProj) {
1074 n = use;
1075 break;
1076 }
1077 }
1078 assert(n->Opcode() == Op_SCMemProj, "memory projection required");
1079 }
1080 }
1081 // push user on appropriate worklist
1082 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1083 Node *use = n->fast_out(i);
1084 if (use->is_Phi()) {
1085 memnode_worklist.append_if_missing(use);
1206 while (orig_phis.length() != 0) {
1207 PhiNode *phi = orig_phis.pop();
1208 int alias_idx = _compile->get_alias_index(phi->adr_type());
1209 igvn->hash_delete(phi);
1210 for (uint i = 1; i < phi->req(); i++) {
1211 Node *mem = phi->in(i);
1212 Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis, igvn);
1213 if (_compile->failing()) {
1214 return;
1215 }
1216 if (mem != new_mem) {
1217 phi->set_req(i, new_mem);
1218 }
1219 }
1220 igvn->hash_insert(phi);
1221 record_for_optimizer(phi);
1222 }
1223
1224 // Update the memory inputs of MemNodes with the value we computed
1225 // in Phase 2.
1226 for (uint i = 0; i < nodes_size(); i++) {
1227 Node *nmem = get_map(i);
1228 if (nmem != NULL) {
1229 Node *n = ptnode_adr(i)->_node;
1230 if (n != NULL && n->is_Mem()) {
1231 igvn->hash_delete(n);
1232 n->set_req(MemNode::Memory, nmem);
1233 igvn->hash_insert(n);
1234 record_for_optimizer(n);
1235 }
1236 }
1237 }
1238 }
1239
1240 bool ConnectionGraph::has_candidates(Compile *C) {
1241 // EA brings benefits only when the code has allocations and/or locks which
1242 // are represented by ideal Macro nodes.
1243 int cnt = C->macro_count();
1244 for( int i=0; i < cnt; i++ ) {
1245 Node *n = C->macro_node(i);
1246 if ( n->is_Allocate() )
1247 return true;
1248 if( n->is_Lock() ) {
1249 Node* obj = n->as_Lock()->obj_node()->uncast();
1250 if( !(obj->is_Parm() || obj->is_Con()) )
1251 return true;
1252 }
1253 }
1254 return false;
1255 }
1256
1257 bool ConnectionGraph::compute_escape() {
1258 Compile* C = _compile;
1259
1260 // 1. Populate Connection Graph (CG) with Ideal nodes.
1261
1262 Unique_Node_List worklist_init;
1263 worklist_init.map(C->unique(), NULL); // preallocate space
1264
1265 // Initialize worklist
1266 if (C->root() != NULL) {
1267 worklist_init.push(C->root());
1268 }
1269
1270 GrowableArray<int> cg_worklist;
1271 PhaseGVN* igvn = C->initial_gvn();
1272 bool has_allocations = false;
1273
1274 // Push all useful nodes onto CG list and set their type.
1275 for( uint next = 0; next < worklist_init.size(); ++next ) {
1276 Node* n = worklist_init.at(next);
1277 record_for_escape_analysis(n, igvn);
1278 // Only allocations and java static calls results are checked
1279 // for an escape status. See process_call_result() below.
1280 if (n->is_Allocate() || n->is_CallStaticJava() &&
1281 ptnode_adr(n->_idx)->node_type() == PointsToNode::JavaObject) {
1282 has_allocations = true;
1283 }
1284 if(n->is_AddP())
1285 cg_worklist.append(n->_idx);
1286 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1287 Node* m = n->fast_out(i); // Get user
1288 worklist_init.push(m);
1289 }
1290 }
1291
1292 if (!has_allocations) {
1293 _collecting = false;
1294 return false; // Nothing to do.
1295 }
1296
1297 // 2. First pass to create simple CG edges (doesn't require to walk CG).
1298 uint delayed_size = _delayed_worklist.size();
1299 for( uint next = 0; next < delayed_size; ++next ) {
1300 Node* n = _delayed_worklist.at(next);
1301 build_connection_graph(n, igvn);
1302 }
1303
1304 // 3. Pass to create fields edges (Allocate -F-> AddP).
1305 uint cg_length = cg_worklist.length();
1306 for( uint next = 0; next < cg_length; ++next ) {
1307 int ni = cg_worklist.at(next);
1308 build_connection_graph(ptnode_adr(ni)->_node, igvn);
1309 }
1310
1311 cg_worklist.clear();
1312 cg_worklist.append(_phantom_object);
1313
1314 // 4. Build Connection Graph which need
1315 // to walk the connection graph.
1316 for (uint ni = 0; ni < nodes_size(); ni++) {
1317 PointsToNode* ptn = ptnode_adr(ni);
1318 Node *n = ptn->_node;
1319 if (n != NULL) { // Call, AddP, LoadP, StoreP
1320 build_connection_graph(n, igvn);
1321 if (ptn->node_type() != PointsToNode::UnknownType)
1322 cg_worklist.append(n->_idx); // Collect CG nodes
1323 }
1324 }
1325
1326 VectorSet ptset(Thread::current()->resource_area());
1327 GrowableArray<uint> deferred_edges;
1328 VectorSet visited(Thread::current()->resource_area());
1329
1330 // 5. Remove deferred edges from the graph and collect
1331 // information needed for type splitting.
1332 cg_length = cg_worklist.length();
1333 for( uint next = 0; next < cg_length; ++next ) {
1334 int ni = cg_worklist.at(next);
1335 PointsToNode* ptn = ptnode_adr(ni);
1336 PointsToNode::NodeType nt = ptn->node_type();
1337 if (nt == PointsToNode::LocalVar || nt == PointsToNode::Field) {
1338 remove_deferred(ni, &deferred_edges, &visited);
1339 Node *n = ptn->_node;
1340 if (n->is_AddP()) {
1341 // If this AddP computes an address which may point to more that one
1342 // object or more then one field (array's element), nothing the address
1343 // points to can be scalar replaceable.
1344 Node *base = get_addp_base(n);
1345 ptset.Clear();
1346 PointsTo(ptset, base, igvn);
1347 if (ptset.Size() > 1 ||
1348 (ptset.Size() != 0 && ptn->offset() == Type::OffsetBot)) {
1349 for( VectorSetI j(&ptset); j.test(); ++j ) {
1350 ptnode_adr(j.elem)->_scalar_replaceable = false;
1351 }
1352 }
1353 }
1354 }
1355 }
1356
1357 // 6. Propagate escape states.
1358 GrowableArray<int> worklist;
1359 bool has_non_escaping_obj = false;
1360
1361 // push all GlobalEscape nodes on the worklist
1362 for( uint next = 0; next < cg_length; ++next ) {
1363 int nk = cg_worklist.at(next);
1364 if (ptnode_adr(nk)->escape_state() == PointsToNode::GlobalEscape)
1365 worklist.push(nk);
1366 }
1367 // mark all nodes reachable from GlobalEscape nodes
1368 while(worklist.length() > 0) {
1369 PointsToNode* ptn = ptnode_adr(worklist.pop());
1370 uint e_cnt = ptn->edge_count();
1371 for (uint ei = 0; ei < e_cnt; ei++) {
1372 uint npi = ptn->edge_target(ei);
1373 PointsToNode *np = ptnode_adr(npi);
1374 if (np->escape_state() < PointsToNode::GlobalEscape) {
1375 np->set_escape_state(PointsToNode::GlobalEscape);
1376 worklist.push(npi);
1377 }
1378 }
1379 }
1380
1381 // push all ArgEscape nodes on the worklist
1382 for( uint next = 0; next < cg_length; ++next ) {
1383 int nk = cg_worklist.at(next);
1384 if (ptnode_adr(nk)->escape_state() == PointsToNode::ArgEscape)
1385 worklist.push(nk);
1386 }
1387 // mark all nodes reachable from ArgEscape nodes
1388 while(worklist.length() > 0) {
1389 PointsToNode* ptn = ptnode_adr(worklist.pop());
1390 if (ptn->node_type() == PointsToNode::JavaObject)
1391 has_non_escaping_obj = true; // Non GlobalEscape
1392 uint e_cnt = ptn->edge_count();
1393 for (uint ei = 0; ei < e_cnt; ei++) {
1394 uint npi = ptn->edge_target(ei);
1395 PointsToNode *np = ptnode_adr(npi);
1396 if (np->escape_state() < PointsToNode::ArgEscape) {
1397 np->set_escape_state(PointsToNode::ArgEscape);
1398 worklist.push(npi);
1399 }
1400 }
1401 }
1402
1403 GrowableArray<Node*> alloc_worklist;
1404
1405 // push all NoEscape nodes on the worklist
1406 for( uint next = 0; next < cg_length; ++next ) {
1407 int nk = cg_worklist.at(next);
1408 if (ptnode_adr(nk)->escape_state() == PointsToNode::NoEscape)
1409 worklist.push(nk);
1410 }
1411 // mark all nodes reachable from NoEscape nodes
1412 while(worklist.length() > 0) {
1413 PointsToNode* ptn = ptnode_adr(worklist.pop());
1414 if (ptn->node_type() == PointsToNode::JavaObject)
1415 has_non_escaping_obj = true; // Non GlobalEscape
1416 Node* n = ptn->_node;
1417 if (n->is_Allocate() && ptn->_scalar_replaceable ) {
1418 // Push scalar replaceable alocations on alloc_worklist
1419 // for processing in split_unique_types().
1420 alloc_worklist.append(n);
1421 }
1422 uint e_cnt = ptn->edge_count();
1423 for (uint ei = 0; ei < e_cnt; ei++) {
1424 uint npi = ptn->edge_target(ei);
1425 PointsToNode *np = ptnode_adr(npi);
1426 if (np->escape_state() < PointsToNode::NoEscape) {
1427 np->set_escape_state(PointsToNode::NoEscape);
1428 worklist.push(npi);
1429 }
1430 }
1431 }
1432
1433 _collecting = false;
1434 assert(C->unique() == nodes_size(), "there should be no new ideal nodes during ConnectionGraph build");
1435
1436 bool has_scalar_replaceable_candidates = alloc_worklist.length() > 0;
1437 if ( has_scalar_replaceable_candidates &&
1438 C->AliasLevel() >= 3 && EliminateAllocations ) {
1439
1440 // Now use the escape information to create unique types for
1441 // scalar replaceable objects.
1442 split_unique_types(alloc_worklist);
1443
1444 if (C->failing()) return false;
1445
1446 // Clean up after split unique types.
1447 ResourceMark rm;
1448 PhaseRemoveUseless pru(C->initial_gvn(), C->for_igvn());
1449
1450 C->print_method("After Escape Analysis", 2);
1451
1452 #ifdef ASSERT
1453 } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
1454 tty->print("=== No allocations eliminated for ");
1455 C->method()->print_short_name();
1456 if(!EliminateAllocations) {
1457 tty->print(" since EliminateAllocations is off ===");
1458 } else if(!has_scalar_replaceable_candidates) {
1459 tty->print(" since there are no scalar replaceable candidates ===");
1460 } else if(C->AliasLevel() < 3) {
1461 tty->print(" since AliasLevel < 3 ===");
1462 }
1463 tty->cr();
1464 #endif
1465 }
1466 return has_non_escaping_obj;
1467 }
1468
1469 void ConnectionGraph::process_call_arguments(CallNode *call, PhaseTransform *phase) {
1470
1471 switch (call->Opcode()) {
1472 #ifdef ASSERT
1473 case Op_Allocate:
1474 case Op_AllocateArray:
1475 case Op_Lock:
1476 case Op_Unlock:
1477 assert(false, "should be done already");
1478 break;
1479 #endif
1480 case Op_CallLeafNoFP:
1481 {
1482 // Stub calls, objects do not escape but they are not scale replaceable.
1483 // Adjust escape state for outgoing arguments.
1484 const TypeTuple * d = call->tf()->domain();
1485 VectorSet ptset(Thread::current()->resource_area());
1486 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1546 }
1547
1548 ptset.Clear();
1549 PointsTo(ptset, arg, phase);
1550 for( VectorSetI j(&ptset); j.test(); ++j ) {
1551 uint pt = j.elem;
1552 if (global_escapes) {
1553 //The argument global escapes, mark everything it could point to
1554 set_escape_state(pt, PointsToNode::GlobalEscape);
1555 } else {
1556 if (fields_escapes) {
1557 // The argument itself doesn't escape, but any fields might
1558 add_edge_from_fields(pt, _phantom_object, Type::OffsetBot);
1559 }
1560 set_escape_state(pt, PointsToNode::ArgEscape);
1561 }
1562 }
1563 }
1564 }
1565 if (copy_dependencies)
1566 call_analyzer->copy_dependencies(_compile->dependencies());
1567 break;
1568 }
1569 }
1570
1571 default:
1572 // Fall-through here if not a Java method or no analyzer information
1573 // or some other type of call, assume the worst case: all arguments
1574 // globally escape.
1575 {
1576 // adjust escape state for outgoing arguments
1577 const TypeTuple * d = call->tf()->domain();
1578 VectorSet ptset(Thread::current()->resource_area());
1579 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1580 const Type* at = d->field_at(i);
1581 if (at->isa_oopptr() != NULL) {
1582 Node *arg = call->in(i)->uncast();
1583 set_escape_state(arg->_idx, PointsToNode::GlobalEscape);
1584 ptset.Clear();
1585 PointsTo(ptset, arg, phase);
1586 for( VectorSetI j(&ptset); j.test(); ++j ) {
1587 uint pt = j.elem;
1588 set_escape_state(pt, PointsToNode::GlobalEscape);
1589 }
1590 }
1591 }
1592 }
1593 }
1594 }
1595 void ConnectionGraph::process_call_result(ProjNode *resproj, PhaseTransform *phase) {
1596 CallNode *call = resproj->in(0)->as_Call();
1597 uint call_idx = call->_idx;
1598 uint resproj_idx = resproj->_idx;
1599
1600 switch (call->Opcode()) {
1601 case Op_Allocate:
1602 {
1603 Node *k = call->in(AllocateNode::KlassNode);
1604 const TypeKlassPtr *kt;
1605 if (k->Opcode() == Op_LoadKlass) {
1606 kt = k->as_Load()->type()->isa_klassptr();
1607 } else {
1608 // Also works for DecodeN(LoadNKlass).
1609 kt = k->as_Type()->type()->isa_klassptr();
1610 }
1611 assert(kt != NULL, "TypeKlassPtr required.");
1612 ciKlass* cik = kt->klass();
1613 ciInstanceKlass* ciik = cik->as_instance_klass();
1614
1615 PointsToNode::EscapeState es;
1616 uint edge_to;
1617 if (cik->is_subclass_of(_compile->env()->Thread_klass()) || ciik->has_finalizer()) {
1618 es = PointsToNode::GlobalEscape;
1619 edge_to = _phantom_object; // Could not be worse
1620 } else {
1621 es = PointsToNode::NoEscape;
1622 edge_to = call_idx;
1623 }
1624 set_escape_state(call_idx, es);
1625 add_pointsto_edge(resproj_idx, edge_to);
1626 _processed.set(resproj_idx);
1627 break;
1628 }
1629
1630 case Op_AllocateArray:
1631 {
1632 int length = call->in(AllocateNode::ALength)->find_int_con(-1);
1633 if (length < 0 || length > EliminateAllocationArraySizeLimit) {
1634 // Not scalar replaceable if the length is not constant or too big.
1635 ptnode_adr(call_idx)->_scalar_replaceable = false;
1636 }
1637 set_escape_state(call_idx, PointsToNode::NoEscape);
1638 add_pointsto_edge(resproj_idx, call_idx);
1639 _processed.set(resproj_idx);
1640 break;
1641 }
1642
1643 case Op_CallStaticJava:
1644 // For a static call, we know exactly what method is being called.
1645 // Use bytecode estimator to record whether the call's return value escapes
1646 {
1647 bool done = true;
1648 const TypeTuple *r = call->tf()->range();
1649 const Type* ret_type = NULL;
1650
1651 if (r->cnt() > TypeFunc::Parms)
1652 ret_type = r->field_at(TypeFunc::Parms);
1653
1654 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1655 // _multianewarray functions return a TypeRawPtr.
1656 if (ret_type == NULL || ret_type->isa_ptr() == NULL) {
1657 _processed.set(resproj_idx);
1658 break; // doesn't return a pointer type
1659 }
1660 ciMethod *meth = call->as_CallJava()->method();
1661 const TypeTuple * d = call->tf()->domain();
1662 if (meth == NULL) {
1663 // not a Java method, assume global escape
1664 set_escape_state(call_idx, PointsToNode::GlobalEscape);
1665 add_pointsto_edge(resproj_idx, _phantom_object);
1666 } else {
1667 BCEscapeAnalyzer *call_analyzer = meth->get_bcea();
1668 bool copy_dependencies = false;
1669
1670 if (call_analyzer->is_return_allocated()) {
1671 // Returns a newly allocated unescaped object, simply
1672 // update dependency information.
1673 // Mark it as NoEscape so that objects referenced by
1674 // it's fields will be marked as NoEscape at least.
1675 set_escape_state(call_idx, PointsToNode::NoEscape);
1676 add_pointsto_edge(resproj_idx, call_idx);
1677 copy_dependencies = true;
1678 } else if (call_analyzer->is_return_local()) {
1679 // determine whether any arguments are returned
1680 set_escape_state(call_idx, PointsToNode::NoEscape);
1681 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1682 const Type* at = d->field_at(i);
1683
1684 if (at->isa_oopptr() != NULL) {
1685 Node *arg = call->in(i)->uncast();
1686
1687 if (call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
1688 PointsToNode *arg_esp = ptnode_adr(arg->_idx);
1689 if (arg_esp->node_type() == PointsToNode::UnknownType)
1690 done = false;
1691 else if (arg_esp->node_type() == PointsToNode::JavaObject)
1692 add_pointsto_edge(resproj_idx, arg->_idx);
1693 else
1694 add_deferred_edge(resproj_idx, arg->_idx);
1695 arg_esp->_hidden_alias = true;
1696 }
1697 }
1698 }
1699 copy_dependencies = true;
1700 } else {
1701 set_escape_state(call_idx, PointsToNode::GlobalEscape);
1702 add_pointsto_edge(resproj_idx, _phantom_object);
1703 for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1704 const Type* at = d->field_at(i);
1705 if (at->isa_oopptr() != NULL) {
1706 Node *arg = call->in(i)->uncast();
1707 PointsToNode *arg_esp = ptnode_adr(arg->_idx);
1708 arg_esp->_hidden_alias = true;
1709 }
1710 }
1711 }
1712 if (copy_dependencies)
1713 call_analyzer->copy_dependencies(_compile->dependencies());
1714 }
1715 if (done)
1716 _processed.set(resproj_idx);
1717 break;
1718 }
1719
1720 default:
1721 // Some other type of call, assume the worst case that the
1722 // returned value, if any, globally escapes.
1723 {
1724 const TypeTuple *r = call->tf()->range();
1725 if (r->cnt() > TypeFunc::Parms) {
1726 const Type* ret_type = r->field_at(TypeFunc::Parms);
1727
1728 // Note: we use isa_ptr() instead of isa_oopptr() here because the
1729 // _multianewarray functions return a TypeRawPtr.
1730 if (ret_type->isa_ptr() != NULL) {
1731 set_escape_state(call_idx, PointsToNode::GlobalEscape);
1732 add_pointsto_edge(resproj_idx, _phantom_object);
1733 }
1734 }
1735 _processed.set(resproj_idx);
1736 }
1737 }
1738 }
1739
1740 // Populate Connection Graph with Ideal nodes and create simple
1741 // connection graph edges (do not need to check the node_type of inputs
1742 // or to call PointsTo() to walk the connection graph).
1743 void ConnectionGraph::record_for_escape_analysis(Node *n, PhaseTransform *phase) {
1744 if (_processed.test(n->_idx))
1745 return; // No need to redefine node's state.
1746
1747 if (n->is_Call()) {
1748 // Arguments to allocation and locking don't escape.
1749 if (n->is_Allocate()) {
1750 add_node(n, PointsToNode::JavaObject, PointsToNode::UnknownEscape, true);
1751 record_for_optimizer(n);
1752 } else if (n->is_Lock() || n->is_Unlock()) {
1753 // Put Lock and Unlock nodes on IGVN worklist to process them during
1754 // the first IGVN optimization when escape information is still available.
1755 record_for_optimizer(n);
1756 _processed.set(n->_idx);
1757 } else {
1758 // Have to process call's arguments first.
1759 PointsToNode::NodeType nt = PointsToNode::UnknownType;
1760
1761 // Check if a call returns an object.
1762 const TypeTuple *r = n->as_Call()->tf()->range();
1763 if (n->is_CallStaticJava() && r->cnt() > TypeFunc::Parms &&
1764 n->as_Call()->proj_out(TypeFunc::Parms) != NULL) {
1765 // Note: use isa_ptr() instead of isa_oopptr() here because
1766 // the _multianewarray functions return a TypeRawPtr.
1767 if (r->field_at(TypeFunc::Parms)->isa_ptr() != NULL) {
1768 nt = PointsToNode::JavaObject;
1769 }
1770 }
1771 add_node(n, nt, PointsToNode::UnknownEscape, false);
1772 }
1773 return;
1774 }
1775
1776 // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
1777 // ThreadLocal has RawPrt type.
1778 switch (n->Opcode()) {
1779 case Op_AddP:
1780 {
1781 add_node(n, PointsToNode::Field, PointsToNode::UnknownEscape, false);
1782 break;
1783 }
1784 case Op_CastX2P:
1785 { // "Unsafe" memory access.
1786 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, true);
1787 break;
1788 }
1789 case Op_CastPP:
1790 case Op_CheckCastPP:
1791 case Op_EncodeP:
1792 case Op_DecodeN:
1793 {
1794 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1795 int ti = n->in(1)->_idx;
1796 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1797 if (nt == PointsToNode::UnknownType) {
1798 _delayed_worklist.push(n); // Process it later.
1799 break;
1800 } else if (nt == PointsToNode::JavaObject) {
1801 add_pointsto_edge(n->_idx, ti);
1802 } else {
1803 add_deferred_edge(n->_idx, ti);
1804 }
1805 _processed.set(n->_idx);
1806 break;
1807 }
1808 case Op_ConP:
1809 {
1810 // assume all pointer constants globally escape except for null
1811 PointsToNode::EscapeState es;
1812 if (phase->type(n) == TypePtr::NULL_PTR)
1813 es = PointsToNode::NoEscape;
1814 else
1815 es = PointsToNode::GlobalEscape;
1816
1866 add_node(n, PointsToNode::JavaObject, PointsToNode::GlobalEscape, false);
1867 break;
1868 }
1869 case Op_Phi:
1870 {
1871 if (n->as_Phi()->type()->isa_ptr() == NULL) {
1872 // nothing to do if not an oop
1873 _processed.set(n->_idx);
1874 return;
1875 }
1876 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1877 uint i;
1878 for (i = 1; i < n->req() ; i++) {
1879 Node* in = n->in(i);
1880 if (in == NULL)
1881 continue; // ignore NULL
1882 in = in->uncast();
1883 if (in->is_top() || in == n)
1884 continue; // ignore top or inputs which go back this node
1885 int ti = in->_idx;
1886 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1887 if (nt == PointsToNode::UnknownType) {
1888 break;
1889 } else if (nt == PointsToNode::JavaObject) {
1890 add_pointsto_edge(n->_idx, ti);
1891 } else {
1892 add_deferred_edge(n->_idx, ti);
1893 }
1894 }
1895 if (i >= n->req())
1896 _processed.set(n->_idx);
1897 else
1898 _delayed_worklist.push(n);
1899 break;
1900 }
1901 case Op_Proj:
1902 {
1903 // we are only interested in the result projection from a call
1904 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
1905 add_node(n, PointsToNode::LocalVar, PointsToNode::UnknownEscape, false);
1906 process_call_result(n->as_Proj(), phase);
1907 if (!_processed.test(n->_idx)) {
1908 // The call's result may need to be processed later if the call
1909 // returns it's argument and the argument is not processed yet.
1910 _delayed_worklist.push(n);
1911 }
1912 } else {
1913 _processed.set(n->_idx);
1914 }
1915 break;
1916 }
1917 case Op_Return:
1918 {
1919 if( n->req() > TypeFunc::Parms &&
1920 phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
1921 // Treat Return value as LocalVar with GlobalEscape escape state.
1922 add_node(n, PointsToNode::LocalVar, PointsToNode::GlobalEscape, false);
1923 int ti = n->in(TypeFunc::Parms)->_idx;
1924 PointsToNode::NodeType nt = ptnode_adr(ti)->node_type();
1925 if (nt == PointsToNode::UnknownType) {
1926 _delayed_worklist.push(n); // Process it later.
1927 break;
1928 } else if (nt == PointsToNode::JavaObject) {
1929 add_pointsto_edge(n->_idx, ti);
1930 } else {
1931 add_deferred_edge(n->_idx, ti);
1932 }
1933 }
1934 _processed.set(n->_idx);
1935 break;
1936 }
1937 case Op_StoreP:
1938 case Op_StoreN:
1939 {
1940 const Type *adr_type = phase->type(n->in(MemNode::Address));
1941 adr_type = adr_type->make_ptr();
1942 if (adr_type->isa_oopptr()) {
1943 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1944 } else {
1968 add_node(n, PointsToNode::UnknownType, PointsToNode::UnknownEscape, false);
1969 } else {
1970 _processed.set(n->_idx);
1971 return;
1972 }
1973 break;
1974 }
1975 case Op_ThreadLocal:
1976 {
1977 add_node(n, PointsToNode::JavaObject, PointsToNode::ArgEscape, true);
1978 break;
1979 }
1980 default:
1981 ;
1982 // nothing to do
1983 }
1984 return;
1985 }
1986
1987 void ConnectionGraph::build_connection_graph(Node *n, PhaseTransform *phase) {
1988 uint n_idx = n->_idx;
1989
1990 // Don't set processed bit for AddP, LoadP, StoreP since
1991 // they may need more then one pass to process.
1992 if (_processed.test(n_idx))
1993 return; // No need to redefine node's state.
1994
1995 if (n->is_Call()) {
1996 CallNode *call = n->as_Call();
1997 process_call_arguments(call, phase);
1998 _processed.set(n_idx);
1999 return;
2000 }
2001
2002 switch (n->Opcode()) {
2003 case Op_AddP:
2004 {
2005 Node *base = get_addp_base(n);
2006 // Create a field edge to this node from everything base could point to.
2007 VectorSet ptset(Thread::current()->resource_area());
2008 PointsTo(ptset, base, phase);
2009 for( VectorSetI i(&ptset); i.test(); ++i ) {
2010 uint pt = i.elem;
2011 add_field_edge(pt, n_idx, address_offset(n, phase));
2012 }
2013 break;
2014 }
2015 case Op_CastX2P:
2016 {
2017 assert(false, "Op_CastX2P");
2018 break;
2019 }
2020 case Op_CastPP:
2021 case Op_CheckCastPP:
2022 case Op_EncodeP:
2023 case Op_DecodeN:
2024 {
2025 int ti = n->in(1)->_idx;
2026 if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) {
2027 add_pointsto_edge(n_idx, ti);
2028 } else {
2029 add_deferred_edge(n_idx, ti);
2030 }
2031 _processed.set(n_idx);
2032 break;
2033 }
2034 case Op_ConP:
2035 {
2036 assert(false, "Op_ConP");
2037 break;
2038 }
2039 case Op_ConN:
2040 {
2041 assert(false, "Op_ConN");
2042 break;
2043 }
2044 case Op_CreateEx:
2045 {
2046 assert(false, "Op_CreateEx");
2047 break;
2048 }
2049 case Op_LoadKlass:
2050 case Op_LoadNKlass:
2051 {
2060 if (!t->isa_narrowoop() && t->isa_ptr() == NULL)
2061 assert(false, "Op_LoadP");
2062 #endif
2063
2064 Node* adr = n->in(MemNode::Address)->uncast();
2065 const Type *adr_type = phase->type(adr);
2066 Node* adr_base;
2067 if (adr->is_AddP()) {
2068 adr_base = get_addp_base(adr);
2069 } else {
2070 adr_base = adr;
2071 }
2072
2073 // For everything "adr_base" could point to, create a deferred edge from
2074 // this node to each field with the same offset.
2075 VectorSet ptset(Thread::current()->resource_area());
2076 PointsTo(ptset, adr_base, phase);
2077 int offset = address_offset(adr, phase);
2078 for( VectorSetI i(&ptset); i.test(); ++i ) {
2079 uint pt = i.elem;
2080 add_deferred_edge_to_fields(n_idx, pt, offset);
2081 }
2082 break;
2083 }
2084 case Op_Parm:
2085 {
2086 assert(false, "Op_Parm");
2087 break;
2088 }
2089 case Op_Phi:
2090 {
2091 #ifdef ASSERT
2092 if (n->as_Phi()->type()->isa_ptr() == NULL)
2093 assert(false, "Op_Phi");
2094 #endif
2095 for (uint i = 1; i < n->req() ; i++) {
2096 Node* in = n->in(i);
2097 if (in == NULL)
2098 continue; // ignore NULL
2099 in = in->uncast();
2100 if (in->is_top() || in == n)
2101 continue; // ignore top or inputs which go back this node
2102 int ti = in->_idx;
2103 if (ptnode_adr(in->_idx)->node_type() == PointsToNode::JavaObject) {
2104 add_pointsto_edge(n_idx, ti);
2105 } else {
2106 add_deferred_edge(n_idx, ti);
2107 }
2108 }
2109 _processed.set(n_idx);
2110 break;
2111 }
2112 case Op_Proj:
2113 {
2114 // we are only interested in the result projection from a call
2115 if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() ) {
2116 process_call_result(n->as_Proj(), phase);
2117 assert(_processed.test(n_idx), "all call results should be processed");
2118 } else {
2119 assert(false, "Op_Proj");
2120 }
2121 break;
2122 }
2123 case Op_Return:
2124 {
2125 #ifdef ASSERT
2126 if( n->req() <= TypeFunc::Parms ||
2127 !phase->type(n->in(TypeFunc::Parms))->isa_oopptr() ) {
2128 assert(false, "Op_Return");
2129 }
2130 #endif
2131 int ti = n->in(TypeFunc::Parms)->_idx;
2132 if (ptnode_adr(ti)->node_type() == PointsToNode::JavaObject) {
2133 add_pointsto_edge(n_idx, ti);
2134 } else {
2135 add_deferred_edge(n_idx, ti);
2136 }
2137 _processed.set(n_idx);
2138 break;
2139 }
2140 case Op_StoreP:
2141 case Op_StoreN:
2142 case Op_StorePConditional:
2143 case Op_CompareAndSwapP:
2144 case Op_CompareAndSwapN:
2145 {
2146 Node *adr = n->in(MemNode::Address);
2147 const Type *adr_type = phase->type(adr)->make_ptr();
2148 #ifdef ASSERT
2149 if (!adr_type->isa_oopptr())
2150 assert(phase->type(adr) == TypeRawPtr::NOTNULL, "Op_StoreP");
2151 #endif
2152
2153 assert(adr->is_AddP(), "expecting an AddP");
2154 Node *adr_base = get_addp_base(adr);
2155 Node *val = n->in(MemNode::ValueIn)->uncast();
2156 // For everything "adr_base" could point to, create a deferred edge
2157 // to "val" from each field with the same offset.
2162 add_edge_from_fields(pt, val->_idx, address_offset(adr, phase));
2163 }
2164 break;
2165 }
2166 case Op_ThreadLocal:
2167 {
2168 assert(false, "Op_ThreadLocal");
2169 break;
2170 }
2171 default:
2172 ;
2173 // nothing to do
2174 }
2175 }
2176
2177 #ifndef PRODUCT
2178 void ConnectionGraph::dump() {
2179 PhaseGVN *igvn = _compile->initial_gvn();
2180 bool first = true;
2181
2182 uint size = nodes_size();
2183 for (uint ni = 0; ni < size; ni++) {
2184 PointsToNode *ptn = ptnode_adr(ni);
2185 PointsToNode::NodeType ptn_type = ptn->node_type();
2186
2187 if (ptn_type != PointsToNode::JavaObject || ptn->_node == NULL)
2188 continue;
2189 PointsToNode::EscapeState es = escape_state(ptn->_node, igvn);
2190 if (ptn->_node->is_Allocate() && (es == PointsToNode::NoEscape || Verbose)) {
2191 if (first) {
2192 tty->cr();
2193 tty->print("======== Connection graph for ");
2194 _compile->method()->print_short_name();
2195 tty->cr();
2196 first = false;
2197 }
2198 tty->print("%6d ", ni);
2199 ptn->dump();
2200 // Print all locals which reference this allocation
2201 for (uint li = ni; li < size; li++) {
2202 PointsToNode *ptn_loc = ptnode_adr(li);
2203 PointsToNode::NodeType ptn_loc_type = ptn_loc->node_type();
2204 if ( ptn_loc_type == PointsToNode::LocalVar && ptn_loc->_node != NULL &&
2205 ptn_loc->edge_count() == 1 && ptn_loc->edge_target(0) == ni ) {
2206 tty->print("%6d LocalVar [[%d]]", li, ni);
2207 ptnode_adr(li)->_node->dump();
2208 }
2209 }
2210 if (Verbose) {
2211 // Print all fields which reference this allocation
2212 for (uint i = 0; i < ptn->edge_count(); i++) {
2213 uint ei = ptn->edge_target(i);
2214 tty->print("%6d Field [[%d]]", ei, ni);
2215 ptnode_adr(ei)->_node->dump();
2216 }
2217 }
2218 tty->cr();
2219 }
2220 }
2221 }
2222 #endif
|