1 /*
2  * Copyright (c) 2005, 2020, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #include "precompiled.hpp"
26 #include "ci/bcEscapeAnalyzer.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "gc/shared/barrierSet.hpp"
29 #include "gc/shared/c2/barrierSetC2.hpp"
30 #include "libadt/vectset.hpp"
31 #include "memory/allocation.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "opto/c2compiler.hpp"
34 #include "opto/arraycopynode.hpp"
35 #include "opto/callnode.hpp"
36 #include "opto/cfgnode.hpp"
37 #include "opto/compile.hpp"
38 #include "opto/escape.hpp"
39 #include "opto/phaseX.hpp"
40 #include "opto/movenode.hpp"
41 #include "opto/rootnode.hpp"
42 #include "utilities/macros.hpp"
43 
ConnectionGraph(Compile * C,PhaseIterGVN * igvn)44 ConnectionGraph::ConnectionGraph(Compile * C, PhaseIterGVN *igvn) :
45   _nodes(C->comp_arena(), C->unique(), C->unique(), NULL),
46   _in_worklist(C->comp_arena()),
47   _next_pidx(0),
48   _collecting(true),
49   _verify(false),
50   _compile(C),
51   _igvn(igvn),
52   _node_map(C->comp_arena()) {
53   // Add unknown java object.
54   add_java_object(C->top(), PointsToNode::GlobalEscape);
55   phantom_obj = ptnode_adr(C->top()->_idx)->as_JavaObject();
56   // Add ConP(#NULL) and ConN(#NULL) nodes.
57   Node* oop_null = igvn->zerocon(T_OBJECT);
58   assert(oop_null->_idx < nodes_size(), "should be created already");
59   add_java_object(oop_null, PointsToNode::NoEscape);
60   null_obj = ptnode_adr(oop_null->_idx)->as_JavaObject();
61   if (UseCompressedOops) {
62     Node* noop_null = igvn->zerocon(T_NARROWOOP);
63     assert(noop_null->_idx < nodes_size(), "should be created already");
64     map_ideal_node(noop_null, null_obj);
65   }
66   _pcmp_neq = NULL; // Should be initialized
67   _pcmp_eq  = NULL;
68 }
69 
has_candidates(Compile * C)70 bool ConnectionGraph::has_candidates(Compile *C) {
71   // EA brings benefits only when the code has allocations and/or locks which
72   // are represented by ideal Macro nodes.
73   int cnt = C->macro_count();
74   for (int i = 0; i < cnt; i++) {
75     Node *n = C->macro_node(i);
76     if (n->is_Allocate())
77       return true;
78     if (n->is_Lock()) {
79       Node* obj = n->as_Lock()->obj_node()->uncast();
80       if (!(obj->is_Parm() || obj->is_Con()))
81         return true;
82     }
83     if (n->is_CallStaticJava() &&
84         n->as_CallStaticJava()->is_boxing_method()) {
85       return true;
86     }
87   }
88   return false;
89 }
90 
do_analysis(Compile * C,PhaseIterGVN * igvn)91 void ConnectionGraph::do_analysis(Compile *C, PhaseIterGVN *igvn) {
92   Compile::TracePhase tp("escapeAnalysis", &Phase::timers[Phase::_t_escapeAnalysis]);
93   ResourceMark rm;
94 
95   // Add ConP#NULL and ConN#NULL nodes before ConnectionGraph construction
96   // to create space for them in ConnectionGraph::_nodes[].
97   Node* oop_null = igvn->zerocon(T_OBJECT);
98   Node* noop_null = igvn->zerocon(T_NARROWOOP);
99   ConnectionGraph* congraph = new(C->comp_arena()) ConnectionGraph(C, igvn);
100   // Perform escape analysis
101   if (congraph->compute_escape()) {
102     // There are non escaping objects.
103     C->set_congraph(congraph);
104   }
105   // Cleanup.
106   if (oop_null->outcnt() == 0)
107     igvn->hash_delete(oop_null);
108   if (noop_null->outcnt() == 0)
109     igvn->hash_delete(noop_null);
110 }
111 
compute_escape()112 bool ConnectionGraph::compute_escape() {
113   Compile* C = _compile;
114   PhaseGVN* igvn = _igvn;
115 
116   // Worklists used by EA.
117   Unique_Node_List delayed_worklist;
118   GrowableArray<Node*> alloc_worklist;
119   GrowableArray<Node*> ptr_cmp_worklist;
120   GrowableArray<Node*> storestore_worklist;
121   GrowableArray<ArrayCopyNode*> arraycopy_worklist;
122   GrowableArray<PointsToNode*>   ptnodes_worklist;
123   GrowableArray<JavaObjectNode*> java_objects_worklist;
124   GrowableArray<JavaObjectNode*> non_escaped_worklist;
125   GrowableArray<FieldNode*>      oop_fields_worklist;
126   GrowableArray<SafePointNode*>  sfn_worklist;
127   DEBUG_ONLY( GrowableArray<Node*> addp_worklist; )
128 
129   { Compile::TracePhase tp("connectionGraph", &Phase::timers[Phase::_t_connectionGraph]);
130 
131   // 1. Populate Connection Graph (CG) with PointsTo nodes.
132   ideal_nodes.map(C->live_nodes(), NULL);  // preallocate space
133   // Initialize worklist
134   if (C->root() != NULL) {
135     ideal_nodes.push(C->root());
136   }
137   // Processed ideal nodes are unique on ideal_nodes list
138   // but several ideal nodes are mapped to the phantom_obj.
139   // To avoid duplicated entries on the following worklists
140   // add the phantom_obj only once to them.
141   ptnodes_worklist.append(phantom_obj);
142   java_objects_worklist.append(phantom_obj);
143   for( uint next = 0; next < ideal_nodes.size(); ++next ) {
144     Node* n = ideal_nodes.at(next);
145     // Create PointsTo nodes and add them to Connection Graph. Called
146     // only once per ideal node since ideal_nodes is Unique_Node list.
147     add_node_to_connection_graph(n, &delayed_worklist);
148     PointsToNode* ptn = ptnode_adr(n->_idx);
149     if (ptn != NULL && ptn != phantom_obj) {
150       ptnodes_worklist.append(ptn);
151       if (ptn->is_JavaObject()) {
152         java_objects_worklist.append(ptn->as_JavaObject());
153         if ((n->is_Allocate() || n->is_CallStaticJava()) &&
154             (ptn->escape_state() < PointsToNode::GlobalEscape)) {
155           // Only allocations and java static calls results are interesting.
156           non_escaped_worklist.append(ptn->as_JavaObject());
157         }
158       } else if (ptn->is_Field() && ptn->as_Field()->is_oop()) {
159         oop_fields_worklist.append(ptn->as_Field());
160       }
161     }
162     if (n->is_MergeMem()) {
163       // Collect all MergeMem nodes to add memory slices for
164       // scalar replaceable objects in split_unique_types().
165       _mergemem_worklist.append(n->as_MergeMem());
166     } else if (OptimizePtrCompare && n->is_Cmp() &&
167                (n->Opcode() == Op_CmpP || n->Opcode() == Op_CmpN)) {
168       // Collect compare pointers nodes.
169       ptr_cmp_worklist.append(n);
170     } else if (n->is_MemBarStoreStore()) {
171       // Collect all MemBarStoreStore nodes so that depending on the
172       // escape status of the associated Allocate node some of them
173       // may be eliminated.
174       storestore_worklist.append(n);
175     } else if (n->is_MemBar() && (n->Opcode() == Op_MemBarRelease) &&
176                (n->req() > MemBarNode::Precedent)) {
177       record_for_optimizer(n);
178 #ifdef ASSERT
179     } else if (n->is_AddP()) {
180       // Collect address nodes for graph verification.
181       addp_worklist.append(n);
182 #endif
183     } else if (n->is_ArrayCopy()) {
184       // Keep a list of ArrayCopy nodes so if one of its input is non
185       // escaping, we can record a unique type
186       arraycopy_worklist.append(n->as_ArrayCopy());
187     }
188     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
189       Node* m = n->fast_out(i);   // Get user
190       ideal_nodes.push(m);
191     }
192     if (n-> is_SafePoint()) {
193       sfn_worklist.append(n->as_SafePoint());
194     }
195   }
196   if (non_escaped_worklist.length() == 0) {
197     _collecting = false;
198     return false; // Nothing to do.
199   }
200   // Add final simple edges to graph.
201   while(delayed_worklist.size() > 0) {
202     Node* n = delayed_worklist.pop();
203     add_final_edges(n);
204   }
205   int ptnodes_length = ptnodes_worklist.length();
206 
207 #ifdef ASSERT
208   if (VerifyConnectionGraph) {
209     // Verify that no new simple edges could be created and all
210     // local vars has edges.
211     _verify = true;
212     for (int next = 0; next < ptnodes_length; ++next) {
213       PointsToNode* ptn = ptnodes_worklist.at(next);
214       add_final_edges(ptn->ideal_node());
215       if (ptn->is_LocalVar() && ptn->edge_count() == 0) {
216         ptn->dump();
217         assert(ptn->as_LocalVar()->edge_count() > 0, "sanity");
218       }
219     }
220     _verify = false;
221   }
222 #endif
223   // Bytecode analyzer BCEscapeAnalyzer, used for Call nodes
224   // processing, calls to CI to resolve symbols (types, fields, methods)
225   // referenced in bytecode. During symbol resolution VM may throw
226   // an exception which CI cleans and converts to compilation failure.
227   if (C->failing())  return false;
228 
229   // 2. Finish Graph construction by propagating references to all
230   //    java objects through graph.
231   if (!complete_connection_graph(ptnodes_worklist, non_escaped_worklist,
232                                  java_objects_worklist, oop_fields_worklist)) {
233     // All objects escaped or hit time or iterations limits.
234     _collecting = false;
235     return false;
236   }
237 
238   // 3. Adjust scalar_replaceable state of nonescaping objects and push
239   //    scalar replaceable allocations on alloc_worklist for processing
240   //    in split_unique_types().
241   int non_escaped_length = non_escaped_worklist.length();
242   for (int next = 0; next < non_escaped_length; next++) {
243     JavaObjectNode* ptn = non_escaped_worklist.at(next);
244     bool noescape = (ptn->escape_state() == PointsToNode::NoEscape);
245     Node* n = ptn->ideal_node();
246     if (n->is_Allocate()) {
247       n->as_Allocate()->_is_non_escaping = noescape;
248     }
249     if (noescape && ptn->scalar_replaceable()) {
250       adjust_scalar_replaceable_state(ptn);
251       if (ptn->scalar_replaceable()) {
252         alloc_worklist.append(ptn->ideal_node());
253       }
254     }
255   }
256 
257 #ifdef ASSERT
258   if (VerifyConnectionGraph) {
259     // Verify that graph is complete - no new edges could be added or needed.
260     verify_connection_graph(ptnodes_worklist, non_escaped_worklist,
261                             java_objects_worklist, addp_worklist);
262   }
263   assert(C->unique() == nodes_size(), "no new ideal nodes should be added during ConnectionGraph build");
264   assert(null_obj->escape_state() == PointsToNode::NoEscape &&
265          null_obj->edge_count() == 0 &&
266          !null_obj->arraycopy_src() &&
267          !null_obj->arraycopy_dst(), "sanity");
268 #endif
269 
270   _collecting = false;
271 
272   } // TracePhase t3("connectionGraph")
273 
274   // 4. Optimize ideal graph based on EA information.
275   bool has_non_escaping_obj = (non_escaped_worklist.length() > 0);
276   if (has_non_escaping_obj) {
277     optimize_ideal_graph(ptr_cmp_worklist, storestore_worklist);
278   }
279 
280 #ifndef PRODUCT
281   if (PrintEscapeAnalysis) {
282     dump(ptnodes_worklist); // Dump ConnectionGraph
283   }
284 #endif
285 
286   bool has_scalar_replaceable_candidates = (alloc_worklist.length() > 0);
287 #ifdef ASSERT
288   if (VerifyConnectionGraph) {
289     int alloc_length = alloc_worklist.length();
290     for (int next = 0; next < alloc_length; ++next) {
291       Node* n = alloc_worklist.at(next);
292       PointsToNode* ptn = ptnode_adr(n->_idx);
293       assert(ptn->escape_state() == PointsToNode::NoEscape && ptn->scalar_replaceable(), "sanity");
294     }
295   }
296 #endif
297 
298   // 5. Separate memory graph for scalar replaceable allcations.
299   if (has_scalar_replaceable_candidates &&
300       C->AliasLevel() >= 3 && EliminateAllocations) {
301     // Now use the escape information to create unique types for
302     // scalar replaceable objects.
303     split_unique_types(alloc_worklist, arraycopy_worklist);
304     if (C->failing())  return false;
305     C->print_method(PHASE_AFTER_EA, 2);
306 
307 #ifdef ASSERT
308   } else if (Verbose && (PrintEscapeAnalysis || PrintEliminateAllocations)) {
309     tty->print("=== No allocations eliminated for ");
310     C->method()->print_short_name();
311     if(!EliminateAllocations) {
312       tty->print(" since EliminateAllocations is off ===");
313     } else if(!has_scalar_replaceable_candidates) {
314       tty->print(" since there are no scalar replaceable candidates ===");
315     } else if(C->AliasLevel() < 3) {
316       tty->print(" since AliasLevel < 3 ===");
317     }
318     tty->cr();
319 #endif
320   }
321 
322   // Annotate at safepoints if they have <= ArgEscape objects in their scope and at
323   // java calls if they pass ArgEscape objects as parameters.
324   if (has_non_escaping_obj &&
325       (C->env()->should_retain_local_variables() ||
326        C->env()->jvmti_can_get_owned_monitor_info() ||
327        C->env()->jvmti_can_walk_any_space() ||
328        DeoptimizeObjectsALot)) {
329     int sfn_length = sfn_worklist.length();
330     for (int next = 0; next < sfn_length; next++) {
331       SafePointNode* sfn = sfn_worklist.at(next);
332       sfn->set_has_ea_local_in_scope(has_ea_local_in_scope(sfn));
333       if (sfn->is_CallJava()) {
334         CallJavaNode* call = sfn->as_CallJava();
335         call->set_arg_escape(has_arg_escape(call));
336       }
337     }
338   }
339 
340   return has_non_escaping_obj;
341 }
342 
343 // Returns true if there is an object in the scope of sfn that does not escape globally.
has_ea_local_in_scope(SafePointNode * sfn)344 bool ConnectionGraph::has_ea_local_in_scope(SafePointNode* sfn) {
345   Compile* C = _compile;
346   for (JVMState* jvms = sfn->jvms(); jvms != NULL; jvms = jvms->caller()) {
347     if (C->env()->should_retain_local_variables() || C->env()->jvmti_can_walk_any_space() ||
348         DeoptimizeObjectsALot) {
349       // Jvmti agents can access locals. Must provide info about local objects at runtime.
350       int num_locs = jvms->loc_size();
351       for (int idx = 0; idx < num_locs; idx++) {
352         Node* l = sfn->local(jvms, idx);
353         if (not_global_escape(l)) {
354           return true;
355         }
356       }
357     }
358     if (C->env()->jvmti_can_get_owned_monitor_info() ||
359         C->env()->jvmti_can_walk_any_space() || DeoptimizeObjectsALot) {
360       // Jvmti agents can read monitors. Must provide info about locked objects at runtime.
361       int num_mon = jvms->nof_monitors();
362       for (int idx = 0; idx < num_mon; idx++) {
363         Node* m = sfn->monitor_obj(jvms, idx);
364         if (m != NULL && not_global_escape(m)) {
365           return true;
366         }
367       }
368     }
369   }
370   return false;
371 }
372 
373 // Returns true if at least one of the arguments to the call is an object
374 // that does not escape globally.
has_arg_escape(CallJavaNode * call)375 bool ConnectionGraph::has_arg_escape(CallJavaNode* call) {
376   if (call->method() != NULL) {
377     uint max_idx = TypeFunc::Parms + call->method()->arg_size();
378     for (uint idx = TypeFunc::Parms; idx < max_idx; idx++) {
379       Node* p = call->in(idx);
380       if (not_global_escape(p)) {
381         return true;
382       }
383     }
384   } else {
385     const char* name = call->as_CallStaticJava()->_name;
386     assert(name != NULL, "no name");
387     // no arg escapes through uncommon traps
388     if (strcmp(name, "uncommon_trap") != 0) {
389       // process_call_arguments() assumes that all arguments escape globally
390       const TypeTuple* d = call->tf()->domain();
391       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
392         const Type* at = d->field_at(i);
393         if (at->isa_oopptr() != NULL) {
394           return true;
395         }
396       }
397     }
398   }
399   return false;
400 }
401 
402 
403 
404 // Utility function for nodes that load an object
add_objload_to_connection_graph(Node * n,Unique_Node_List * delayed_worklist)405 void ConnectionGraph::add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
406   // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
407   // ThreadLocal has RawPtr type.
408   const Type* t = _igvn->type(n);
409   if (t->make_ptr() != NULL) {
410     Node* adr = n->in(MemNode::Address);
411 #ifdef ASSERT
412     if (!adr->is_AddP()) {
413       assert(_igvn->type(adr)->isa_rawptr(), "sanity");
414     } else {
415       assert((ptnode_adr(adr->_idx) == NULL ||
416               ptnode_adr(adr->_idx)->as_Field()->is_oop()), "sanity");
417     }
418 #endif
419     add_local_var_and_edge(n, PointsToNode::NoEscape,
420                            adr, delayed_worklist);
421   }
422 }
423 
424 // Populate Connection Graph with PointsTo nodes and create simple
425 // connection graph edges.
add_node_to_connection_graph(Node * n,Unique_Node_List * delayed_worklist)426 void ConnectionGraph::add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist) {
427   assert(!_verify, "this method should not be called for verification");
428   PhaseGVN* igvn = _igvn;
429   uint n_idx = n->_idx;
430   PointsToNode* n_ptn = ptnode_adr(n_idx);
431   if (n_ptn != NULL)
432     return; // No need to redefine PointsTo node during first iteration.
433 
434   int opcode = n->Opcode();
435   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_to_con_graph(this, igvn, delayed_worklist, n, opcode);
436   if (gc_handled) {
437     return; // Ignore node if already handled by GC.
438   }
439 
440   if (n->is_Call()) {
441     // Arguments to allocation and locking don't escape.
442     if (n->is_AbstractLock()) {
443       // Put Lock and Unlock nodes on IGVN worklist to process them during
444       // first IGVN optimization when escape information is still available.
445       record_for_optimizer(n);
446     } else if (n->is_Allocate()) {
447       add_call_node(n->as_Call());
448       record_for_optimizer(n);
449     } else {
450       if (n->is_CallStaticJava()) {
451         const char* name = n->as_CallStaticJava()->_name;
452         if (name != NULL && strcmp(name, "uncommon_trap") == 0)
453           return; // Skip uncommon traps
454       }
455       // Don't mark as processed since call's arguments have to be processed.
456       delayed_worklist->push(n);
457       // Check if a call returns an object.
458       if ((n->as_Call()->returns_pointer() &&
459            n->as_Call()->proj_out_or_null(TypeFunc::Parms) != NULL) ||
460           (n->is_CallStaticJava() &&
461            n->as_CallStaticJava()->is_boxing_method())) {
462         add_call_node(n->as_Call());
463       }
464     }
465     return;
466   }
467   // Put this check here to process call arguments since some call nodes
468   // point to phantom_obj.
469   if (n_ptn == phantom_obj || n_ptn == null_obj)
470     return; // Skip predefined nodes.
471 
472   switch (opcode) {
473     case Op_AddP: {
474       Node* base = get_addp_base(n);
475       PointsToNode* ptn_base = ptnode_adr(base->_idx);
476       // Field nodes are created for all field types. They are used in
477       // adjust_scalar_replaceable_state() and split_unique_types().
478       // Note, non-oop fields will have only base edges in Connection
479       // Graph because such fields are not used for oop loads and stores.
480       int offset = address_offset(n, igvn);
481       add_field(n, PointsToNode::NoEscape, offset);
482       if (ptn_base == NULL) {
483         delayed_worklist->push(n); // Process it later.
484       } else {
485         n_ptn = ptnode_adr(n_idx);
486         add_base(n_ptn->as_Field(), ptn_base);
487       }
488       break;
489     }
490     case Op_CastX2P: {
491       map_ideal_node(n, phantom_obj);
492       break;
493     }
494     case Op_CastPP:
495     case Op_CheckCastPP:
496     case Op_EncodeP:
497     case Op_DecodeN:
498     case Op_EncodePKlass:
499     case Op_DecodeNKlass: {
500       add_local_var_and_edge(n, PointsToNode::NoEscape,
501                              n->in(1), delayed_worklist);
502       break;
503     }
504     case Op_CMoveP: {
505       add_local_var(n, PointsToNode::NoEscape);
506       // Do not add edges during first iteration because some could be
507       // not defined yet.
508       delayed_worklist->push(n);
509       break;
510     }
511     case Op_ConP:
512     case Op_ConN:
513     case Op_ConNKlass: {
514       // assume all oop constants globally escape except for null
515       PointsToNode::EscapeState es;
516       const Type* t = igvn->type(n);
517       if (t == TypePtr::NULL_PTR || t == TypeNarrowOop::NULL_PTR) {
518         es = PointsToNode::NoEscape;
519       } else {
520         es = PointsToNode::GlobalEscape;
521       }
522       add_java_object(n, es);
523       break;
524     }
525     case Op_CreateEx: {
526       // assume that all exception objects globally escape
527       map_ideal_node(n, phantom_obj);
528       break;
529     }
530     case Op_LoadKlass:
531     case Op_LoadNKlass: {
532       // Unknown class is loaded
533       map_ideal_node(n, phantom_obj);
534       break;
535     }
536     case Op_LoadP:
537     case Op_LoadN:
538     case Op_LoadPLocked: {
539       add_objload_to_connection_graph(n, delayed_worklist);
540       break;
541     }
542     case Op_Parm: {
543       map_ideal_node(n, phantom_obj);
544       break;
545     }
546     case Op_PartialSubtypeCheck: {
547       // Produces Null or notNull and is used in only in CmpP so
548       // phantom_obj could be used.
549       map_ideal_node(n, phantom_obj); // Result is unknown
550       break;
551     }
552     case Op_Phi: {
553       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
554       // ThreadLocal has RawPtr type.
555       const Type* t = n->as_Phi()->type();
556       if (t->make_ptr() != NULL) {
557         add_local_var(n, PointsToNode::NoEscape);
558         // Do not add edges during first iteration because some could be
559         // not defined yet.
560         delayed_worklist->push(n);
561       }
562       break;
563     }
564     case Op_Proj: {
565       // we are only interested in the oop result projection from a call
566       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
567           n->in(0)->as_Call()->returns_pointer()) {
568         add_local_var_and_edge(n, PointsToNode::NoEscape,
569                                n->in(0), delayed_worklist);
570       }
571       break;
572     }
573     case Op_Rethrow: // Exception object escapes
574     case Op_Return: {
575       if (n->req() > TypeFunc::Parms &&
576           igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
577         // Treat Return value as LocalVar with GlobalEscape escape state.
578         add_local_var_and_edge(n, PointsToNode::GlobalEscape,
579                                n->in(TypeFunc::Parms), delayed_worklist);
580       }
581       break;
582     }
583     case Op_CompareAndExchangeP:
584     case Op_CompareAndExchangeN:
585     case Op_GetAndSetP:
586     case Op_GetAndSetN: {
587       add_objload_to_connection_graph(n, delayed_worklist);
588       // fallthrough
589     }
590     case Op_StoreP:
591     case Op_StoreN:
592     case Op_StoreNKlass:
593     case Op_StorePConditional:
594     case Op_WeakCompareAndSwapP:
595     case Op_WeakCompareAndSwapN:
596     case Op_CompareAndSwapP:
597     case Op_CompareAndSwapN: {
598       add_to_congraph_unsafe_access(n, opcode, delayed_worklist);
599       break;
600     }
601     case Op_AryEq:
602     case Op_HasNegatives:
603     case Op_StrComp:
604     case Op_StrEquals:
605     case Op_StrIndexOf:
606     case Op_StrIndexOfChar:
607     case Op_StrInflatedCopy:
608     case Op_StrCompressedCopy:
609     case Op_EncodeISOArray: {
610       add_local_var(n, PointsToNode::ArgEscape);
611       delayed_worklist->push(n); // Process it later.
612       break;
613     }
614     case Op_ThreadLocal: {
615       add_java_object(n, PointsToNode::ArgEscape);
616       break;
617     }
618     default:
619       ; // Do nothing for nodes not related to EA.
620   }
621   return;
622 }
623 
624 #ifdef ASSERT
625 #define ELSE_FAIL(name)                               \
626       /* Should not be called for not pointer type. */  \
627       n->dump(1);                                       \
628       assert(false, name);                              \
629       break;
630 #else
631 #define ELSE_FAIL(name) \
632       break;
633 #endif
634 
635 // Add final simple edges to graph.
add_final_edges(Node * n)636 void ConnectionGraph::add_final_edges(Node *n) {
637   PointsToNode* n_ptn = ptnode_adr(n->_idx);
638 #ifdef ASSERT
639   if (_verify && n_ptn->is_JavaObject())
640     return; // This method does not change graph for JavaObject.
641 #endif
642 
643   if (n->is_Call()) {
644     process_call_arguments(n->as_Call());
645     return;
646   }
647   assert(n->is_Store() || n->is_LoadStore() ||
648          (n_ptn != NULL) && (n_ptn->ideal_node() != NULL),
649          "node should be registered already");
650   int opcode = n->Opcode();
651   bool gc_handled = BarrierSet::barrier_set()->barrier_set_c2()->escape_add_final_edges(this, _igvn, n, opcode);
652   if (gc_handled) {
653     return; // Ignore node if already handled by GC.
654   }
655   switch (opcode) {
656     case Op_AddP: {
657       Node* base = get_addp_base(n);
658       PointsToNode* ptn_base = ptnode_adr(base->_idx);
659       assert(ptn_base != NULL, "field's base should be registered");
660       add_base(n_ptn->as_Field(), ptn_base);
661       break;
662     }
663     case Op_CastPP:
664     case Op_CheckCastPP:
665     case Op_EncodeP:
666     case Op_DecodeN:
667     case Op_EncodePKlass:
668     case Op_DecodeNKlass: {
669       add_local_var_and_edge(n, PointsToNode::NoEscape,
670                              n->in(1), NULL);
671       break;
672     }
673     case Op_CMoveP: {
674       for (uint i = CMoveNode::IfFalse; i < n->req(); i++) {
675         Node* in = n->in(i);
676         if (in == NULL)
677           continue;  // ignore NULL
678         Node* uncast_in = in->uncast();
679         if (uncast_in->is_top() || uncast_in == n)
680           continue;  // ignore top or inputs which go back this node
681         PointsToNode* ptn = ptnode_adr(in->_idx);
682         assert(ptn != NULL, "node should be registered");
683         add_edge(n_ptn, ptn);
684       }
685       break;
686     }
687     case Op_LoadP:
688     case Op_LoadN:
689     case Op_LoadPLocked: {
690       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
691       // ThreadLocal has RawPtr type.
692       const Type* t = _igvn->type(n);
693       if (t->make_ptr() != NULL) {
694         Node* adr = n->in(MemNode::Address);
695         add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
696         break;
697       }
698       ELSE_FAIL("Op_LoadP");
699     }
700     case Op_Phi: {
701       // Using isa_ptr() instead of isa_oopptr() for LoadP and Phi because
702       // ThreadLocal has RawPtr type.
703       const Type* t = n->as_Phi()->type();
704       if (t->make_ptr() != NULL) {
705         for (uint i = 1; i < n->req(); i++) {
706           Node* in = n->in(i);
707           if (in == NULL)
708             continue;  // ignore NULL
709           Node* uncast_in = in->uncast();
710           if (uncast_in->is_top() || uncast_in == n)
711             continue;  // ignore top or inputs which go back this node
712           PointsToNode* ptn = ptnode_adr(in->_idx);
713           assert(ptn != NULL, "node should be registered");
714           add_edge(n_ptn, ptn);
715         }
716         break;
717       }
718       ELSE_FAIL("Op_Phi");
719     }
720     case Op_Proj: {
721       // we are only interested in the oop result projection from a call
722       if (n->as_Proj()->_con == TypeFunc::Parms && n->in(0)->is_Call() &&
723           n->in(0)->as_Call()->returns_pointer()) {
724         add_local_var_and_edge(n, PointsToNode::NoEscape, n->in(0), NULL);
725         break;
726       }
727       ELSE_FAIL("Op_Proj");
728     }
729     case Op_Rethrow: // Exception object escapes
730     case Op_Return: {
731       if (n->req() > TypeFunc::Parms &&
732           _igvn->type(n->in(TypeFunc::Parms))->isa_oopptr()) {
733         // Treat Return value as LocalVar with GlobalEscape escape state.
734         add_local_var_and_edge(n, PointsToNode::GlobalEscape,
735                                n->in(TypeFunc::Parms), NULL);
736         break;
737       }
738       ELSE_FAIL("Op_Return");
739     }
740     case Op_StoreP:
741     case Op_StoreN:
742     case Op_StoreNKlass:
743     case Op_StorePConditional:
744     case Op_CompareAndExchangeP:
745     case Op_CompareAndExchangeN:
746     case Op_CompareAndSwapP:
747     case Op_CompareAndSwapN:
748     case Op_WeakCompareAndSwapP:
749     case Op_WeakCompareAndSwapN:
750     case Op_GetAndSetP:
751     case Op_GetAndSetN: {
752       if (add_final_edges_unsafe_access(n, opcode)) {
753         break;
754       }
755       ELSE_FAIL("Op_StoreP");
756     }
757     case Op_AryEq:
758     case Op_HasNegatives:
759     case Op_StrComp:
760     case Op_StrEquals:
761     case Op_StrIndexOf:
762     case Op_StrIndexOfChar:
763     case Op_StrInflatedCopy:
764     case Op_StrCompressedCopy:
765     case Op_EncodeISOArray: {
766       // char[]/byte[] arrays passed to string intrinsic do not escape but
767       // they are not scalar replaceable. Adjust escape state for them.
768       // Start from in(2) edge since in(1) is memory edge.
769       for (uint i = 2; i < n->req(); i++) {
770         Node* adr = n->in(i);
771         const Type* at = _igvn->type(adr);
772         if (!adr->is_top() && at->isa_ptr()) {
773           assert(at == Type::TOP || at == TypePtr::NULL_PTR ||
774                  at->isa_ptr() != NULL, "expecting a pointer");
775           if (adr->is_AddP()) {
776             adr = get_addp_base(adr);
777           }
778           PointsToNode* ptn = ptnode_adr(adr->_idx);
779           assert(ptn != NULL, "node should be registered");
780           add_edge(n_ptn, ptn);
781         }
782       }
783       break;
784     }
785     default: {
786       // This method should be called only for EA specific nodes which may
787       // miss some edges when they were created.
788 #ifdef ASSERT
789       n->dump(1);
790 #endif
791       guarantee(false, "unknown node");
792     }
793   }
794   return;
795 }
796 
add_to_congraph_unsafe_access(Node * n,uint opcode,Unique_Node_List * delayed_worklist)797 void ConnectionGraph::add_to_congraph_unsafe_access(Node* n, uint opcode, Unique_Node_List* delayed_worklist) {
798   Node* adr = n->in(MemNode::Address);
799   const Type* adr_type = _igvn->type(adr);
800   adr_type = adr_type->make_ptr();
801   if (adr_type == NULL) {
802     return; // skip dead nodes
803   }
804   if (adr_type->isa_oopptr()
805       || ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
806           && adr_type == TypeRawPtr::NOTNULL
807           && is_captured_store_address(adr))) {
808     delayed_worklist->push(n); // Process it later.
809 #ifdef ASSERT
810     assert (adr->is_AddP(), "expecting an AddP");
811     if (adr_type == TypeRawPtr::NOTNULL) {
812       // Verify a raw address for a store captured by Initialize node.
813       int offs = (int) _igvn->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
814       assert(offs != Type::OffsetBot, "offset must be a constant");
815     }
816 #endif
817   } else {
818     // Ignore copy the displaced header to the BoxNode (OSR compilation).
819     if (adr->is_BoxLock()) {
820       return;
821     }
822     // Stored value escapes in unsafe access.
823     if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
824       delayed_worklist->push(n); // Process unsafe access later.
825       return;
826     }
827 #ifdef ASSERT
828     n->dump(1);
829     assert(false, "not unsafe");
830 #endif
831   }
832 }
833 
add_final_edges_unsafe_access(Node * n,uint opcode)834 bool ConnectionGraph::add_final_edges_unsafe_access(Node* n, uint opcode) {
835   Node* adr = n->in(MemNode::Address);
836   const Type *adr_type = _igvn->type(adr);
837   adr_type = adr_type->make_ptr();
838 #ifdef ASSERT
839   if (adr_type == NULL) {
840     n->dump(1);
841     assert(adr_type != NULL, "dead node should not be on list");
842     return true;
843   }
844 #endif
845 
846   if (opcode == Op_GetAndSetP || opcode == Op_GetAndSetN ||
847       opcode == Op_CompareAndExchangeN || opcode == Op_CompareAndExchangeP) {
848     add_local_var_and_edge(n, PointsToNode::NoEscape, adr, NULL);
849   }
850 
851   if (adr_type->isa_oopptr()
852       || ((opcode == Op_StoreP || opcode == Op_StoreN || opcode == Op_StoreNKlass)
853            && adr_type == TypeRawPtr::NOTNULL
854            && is_captured_store_address(adr))) {
855     // Point Address to Value
856     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
857     assert(adr_ptn != NULL &&
858            adr_ptn->as_Field()->is_oop(), "node should be registered");
859     Node* val = n->in(MemNode::ValueIn);
860     PointsToNode* ptn = ptnode_adr(val->_idx);
861     assert(ptn != NULL, "node should be registered");
862     add_edge(adr_ptn, ptn);
863     return true;
864   } else if ((opcode == Op_StoreP) && adr_type->isa_rawptr()) {
865     // Stored value escapes in unsafe access.
866     Node* val = n->in(MemNode::ValueIn);
867     PointsToNode* ptn = ptnode_adr(val->_idx);
868     assert(ptn != NULL, "node should be registered");
869     set_escape_state(ptn, PointsToNode::GlobalEscape);
870     // Add edge to object for unsafe access with offset.
871     PointsToNode* adr_ptn = ptnode_adr(adr->_idx);
872     assert(adr_ptn != NULL, "node should be registered");
873     if (adr_ptn->is_Field()) {
874       assert(adr_ptn->as_Field()->is_oop(), "should be oop field");
875       add_edge(adr_ptn, ptn);
876     }
877     return true;
878   }
879   return false;
880 }
881 
add_call_node(CallNode * call)882 void ConnectionGraph::add_call_node(CallNode* call) {
883   assert(call->returns_pointer(), "only for call which returns pointer");
884   uint call_idx = call->_idx;
885   if (call->is_Allocate()) {
886     Node* k = call->in(AllocateNode::KlassNode);
887     const TypeKlassPtr* kt = k->bottom_type()->isa_klassptr();
888     assert(kt != NULL, "TypeKlassPtr  required.");
889     ciKlass* cik = kt->klass();
890     PointsToNode::EscapeState es = PointsToNode::NoEscape;
891     bool scalar_replaceable = true;
892     if (call->is_AllocateArray()) {
893       if (!cik->is_array_klass()) { // StressReflectiveCode
894         es = PointsToNode::GlobalEscape;
895       } else {
896         int length = call->in(AllocateNode::ALength)->find_int_con(-1);
897         if (length < 0 || length > EliminateAllocationArraySizeLimit) {
898           // Not scalar replaceable if the length is not constant or too big.
899           scalar_replaceable = false;
900         }
901       }
902     } else {  // Allocate instance
903       if (cik->is_subclass_of(_compile->env()->Thread_klass()) ||
904           cik->is_subclass_of(_compile->env()->Reference_klass()) ||
905          !cik->is_instance_klass() || // StressReflectiveCode
906          !cik->as_instance_klass()->can_be_instantiated() ||
907           cik->as_instance_klass()->has_finalizer()) {
908         es = PointsToNode::GlobalEscape;
909       }
910     }
911     add_java_object(call, es);
912     PointsToNode* ptn = ptnode_adr(call_idx);
913     if (!scalar_replaceable && ptn->scalar_replaceable()) {
914       ptn->set_scalar_replaceable(false);
915     }
916   } else if (call->is_CallStaticJava()) {
917     // Call nodes could be different types:
918     //
919     // 1. CallDynamicJavaNode (what happened during call is unknown):
920     //
921     //    - mapped to GlobalEscape JavaObject node if oop is returned;
922     //
923     //    - all oop arguments are escaping globally;
924     //
925     // 2. CallStaticJavaNode (execute bytecode analysis if possible):
926     //
927     //    - the same as CallDynamicJavaNode if can't do bytecode analysis;
928     //
929     //    - mapped to GlobalEscape JavaObject node if unknown oop is returned;
930     //    - mapped to NoEscape JavaObject node if non-escaping object allocated
931     //      during call is returned;
932     //    - mapped to ArgEscape LocalVar node pointed to object arguments
933     //      which are returned and does not escape during call;
934     //
935     //    - oop arguments escaping status is defined by bytecode analysis;
936     //
937     // For a static call, we know exactly what method is being called.
938     // Use bytecode estimator to record whether the call's return value escapes.
939     ciMethod* meth = call->as_CallJava()->method();
940     if (meth == NULL) {
941       const char* name = call->as_CallStaticJava()->_name;
942       assert(strncmp(name, "_multianewarray", 15) == 0, "TODO: add failed case check");
943       // Returns a newly allocated unescaped object.
944       add_java_object(call, PointsToNode::NoEscape);
945       ptnode_adr(call_idx)->set_scalar_replaceable(false);
946     } else if (meth->is_boxing_method()) {
947       // Returns boxing object
948       PointsToNode::EscapeState es;
949       vmIntrinsics::ID intr = meth->intrinsic_id();
950       if (intr == vmIntrinsics::_floatValue || intr == vmIntrinsics::_doubleValue) {
951         // It does not escape if object is always allocated.
952         es = PointsToNode::NoEscape;
953       } else {
954         // It escapes globally if object could be loaded from cache.
955         es = PointsToNode::GlobalEscape;
956       }
957       add_java_object(call, es);
958     } else {
959       BCEscapeAnalyzer* call_analyzer = meth->get_bcea();
960       call_analyzer->copy_dependencies(_compile->dependencies());
961       if (call_analyzer->is_return_allocated()) {
962         // Returns a newly allocated unescaped object, simply
963         // update dependency information.
964         // Mark it as NoEscape so that objects referenced by
965         // it's fields will be marked as NoEscape at least.
966         add_java_object(call, PointsToNode::NoEscape);
967         ptnode_adr(call_idx)->set_scalar_replaceable(false);
968       } else {
969         // Determine whether any arguments are returned.
970         const TypeTuple* d = call->tf()->domain();
971         bool ret_arg = false;
972         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
973           if (d->field_at(i)->isa_ptr() != NULL &&
974               call_analyzer->is_arg_returned(i - TypeFunc::Parms)) {
975             ret_arg = true;
976             break;
977           }
978         }
979         if (ret_arg) {
980           add_local_var(call, PointsToNode::ArgEscape);
981         } else {
982           // Returns unknown object.
983           map_ideal_node(call, phantom_obj);
984         }
985       }
986     }
987   } else {
988     // An other type of call, assume the worst case:
989     // returned value is unknown and globally escapes.
990     assert(call->Opcode() == Op_CallDynamicJava, "add failed case check");
991     map_ideal_node(call, phantom_obj);
992   }
993 }
994 
process_call_arguments(CallNode * call)995 void ConnectionGraph::process_call_arguments(CallNode *call) {
996     bool is_arraycopy = false;
997     switch (call->Opcode()) {
998 #ifdef ASSERT
999     case Op_Allocate:
1000     case Op_AllocateArray:
1001     case Op_Lock:
1002     case Op_Unlock:
1003       assert(false, "should be done already");
1004       break;
1005 #endif
1006     case Op_ArrayCopy:
1007     case Op_CallLeafNoFP:
1008       // Most array copies are ArrayCopy nodes at this point but there
1009       // are still a few direct calls to the copy subroutines (See
1010       // PhaseStringOpts::copy_string())
1011       is_arraycopy = (call->Opcode() == Op_ArrayCopy) ||
1012         call->as_CallLeaf()->is_call_to_arraycopystub();
1013       // fall through
1014     case Op_CallLeaf: {
1015       // Stub calls, objects do not escape but they are not scale replaceable.
1016       // Adjust escape state for outgoing arguments.
1017       const TypeTuple * d = call->tf()->domain();
1018       bool src_has_oops = false;
1019       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1020         const Type* at = d->field_at(i);
1021         Node *arg = call->in(i);
1022         if (arg == NULL) {
1023           continue;
1024         }
1025         const Type *aat = _igvn->type(arg);
1026         if (arg->is_top() || !at->isa_ptr() || !aat->isa_ptr())
1027           continue;
1028         if (arg->is_AddP()) {
1029           //
1030           // The inline_native_clone() case when the arraycopy stub is called
1031           // after the allocation before Initialize and CheckCastPP nodes.
1032           // Or normal arraycopy for object arrays case.
1033           //
1034           // Set AddP's base (Allocate) as not scalar replaceable since
1035           // pointer to the base (with offset) is passed as argument.
1036           //
1037           arg = get_addp_base(arg);
1038         }
1039         PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1040         assert(arg_ptn != NULL, "should be registered");
1041         PointsToNode::EscapeState arg_esc = arg_ptn->escape_state();
1042         if (is_arraycopy || arg_esc < PointsToNode::ArgEscape) {
1043           assert(aat == Type::TOP || aat == TypePtr::NULL_PTR ||
1044                  aat->isa_ptr() != NULL, "expecting an Ptr");
1045           bool arg_has_oops = aat->isa_oopptr() &&
1046                               (aat->isa_oopptr()->klass() == NULL || aat->isa_instptr() ||
1047                                (aat->isa_aryptr() && aat->isa_aryptr()->klass()->is_obj_array_klass()));
1048           if (i == TypeFunc::Parms) {
1049             src_has_oops = arg_has_oops;
1050           }
1051           //
1052           // src or dst could be j.l.Object when other is basic type array:
1053           //
1054           //   arraycopy(char[],0,Object*,0,size);
1055           //   arraycopy(Object*,0,char[],0,size);
1056           //
1057           // Don't add edges in such cases.
1058           //
1059           bool arg_is_arraycopy_dest = src_has_oops && is_arraycopy &&
1060                                        arg_has_oops && (i > TypeFunc::Parms);
1061 #ifdef ASSERT
1062           if (!(is_arraycopy ||
1063                 BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(call) ||
1064                 (call->as_CallLeaf()->_name != NULL &&
1065                  (strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32") == 0 ||
1066                   strcmp(call->as_CallLeaf()->_name, "updateBytesCRC32C") == 0 ||
1067                   strcmp(call->as_CallLeaf()->_name, "updateBytesAdler32") == 0 ||
1068                   strcmp(call->as_CallLeaf()->_name, "aescrypt_encryptBlock") == 0 ||
1069                   strcmp(call->as_CallLeaf()->_name, "aescrypt_decryptBlock") == 0 ||
1070                   strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_encryptAESCrypt") == 0 ||
1071                   strcmp(call->as_CallLeaf()->_name, "cipherBlockChaining_decryptAESCrypt") == 0 ||
1072                   strcmp(call->as_CallLeaf()->_name, "electronicCodeBook_encryptAESCrypt") == 0 ||
1073                   strcmp(call->as_CallLeaf()->_name, "electronicCodeBook_decryptAESCrypt") == 0 ||
1074                   strcmp(call->as_CallLeaf()->_name, "counterMode_AESCrypt") == 0 ||
1075                   strcmp(call->as_CallLeaf()->_name, "ghash_processBlocks") == 0 ||
1076                   strcmp(call->as_CallLeaf()->_name, "encodeBlock") == 0 ||
1077                   strcmp(call->as_CallLeaf()->_name, "decodeBlock") == 0 ||
1078                   strcmp(call->as_CallLeaf()->_name, "md5_implCompress") == 0 ||
1079                   strcmp(call->as_CallLeaf()->_name, "md5_implCompressMB") == 0 ||
1080                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompress") == 0 ||
1081                   strcmp(call->as_CallLeaf()->_name, "sha1_implCompressMB") == 0 ||
1082                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompress") == 0 ||
1083                   strcmp(call->as_CallLeaf()->_name, "sha256_implCompressMB") == 0 ||
1084                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompress") == 0 ||
1085                   strcmp(call->as_CallLeaf()->_name, "sha512_implCompressMB") == 0 ||
1086                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompress") == 0 ||
1087                   strcmp(call->as_CallLeaf()->_name, "sha3_implCompressMB") == 0 ||
1088                   strcmp(call->as_CallLeaf()->_name, "multiplyToLen") == 0 ||
1089                   strcmp(call->as_CallLeaf()->_name, "squareToLen") == 0 ||
1090                   strcmp(call->as_CallLeaf()->_name, "mulAdd") == 0 ||
1091                   strcmp(call->as_CallLeaf()->_name, "montgomery_multiply") == 0 ||
1092                   strcmp(call->as_CallLeaf()->_name, "montgomery_square") == 0 ||
1093                   strcmp(call->as_CallLeaf()->_name, "bigIntegerRightShiftWorker") == 0 ||
1094                   strcmp(call->as_CallLeaf()->_name, "bigIntegerLeftShiftWorker") == 0 ||
1095                   strcmp(call->as_CallLeaf()->_name, "vectorizedMismatch") == 0)
1096                  ))) {
1097             call->dump();
1098             fatal("EA unexpected CallLeaf %s", call->as_CallLeaf()->_name);
1099           }
1100 #endif
1101           // Always process arraycopy's destination object since
1102           // we need to add all possible edges to references in
1103           // source object.
1104           if (arg_esc >= PointsToNode::ArgEscape &&
1105               !arg_is_arraycopy_dest) {
1106             continue;
1107           }
1108           PointsToNode::EscapeState es = PointsToNode::ArgEscape;
1109           if (call->is_ArrayCopy()) {
1110             ArrayCopyNode* ac = call->as_ArrayCopy();
1111             if (ac->is_clonebasic() ||
1112                 ac->is_arraycopy_validated() ||
1113                 ac->is_copyof_validated() ||
1114                 ac->is_copyofrange_validated()) {
1115               es = PointsToNode::NoEscape;
1116             }
1117           }
1118           set_escape_state(arg_ptn, es);
1119           if (arg_is_arraycopy_dest) {
1120             Node* src = call->in(TypeFunc::Parms);
1121             if (src->is_AddP()) {
1122               src = get_addp_base(src);
1123             }
1124             PointsToNode* src_ptn = ptnode_adr(src->_idx);
1125             assert(src_ptn != NULL, "should be registered");
1126             if (arg_ptn != src_ptn) {
1127               // Special arraycopy edge:
1128               // A destination object's field can't have the source object
1129               // as base since objects escape states are not related.
1130               // Only escape state of destination object's fields affects
1131               // escape state of fields in source object.
1132               add_arraycopy(call, es, src_ptn, arg_ptn);
1133             }
1134           }
1135         }
1136       }
1137       break;
1138     }
1139     case Op_CallStaticJava: {
1140       // For a static call, we know exactly what method is being called.
1141       // Use bytecode estimator to record the call's escape affects
1142 #ifdef ASSERT
1143       const char* name = call->as_CallStaticJava()->_name;
1144       assert((name == NULL || strcmp(name, "uncommon_trap") != 0), "normal calls only");
1145 #endif
1146       ciMethod* meth = call->as_CallJava()->method();
1147       if ((meth != NULL) && meth->is_boxing_method()) {
1148         break; // Boxing methods do not modify any oops.
1149       }
1150       BCEscapeAnalyzer* call_analyzer = (meth !=NULL) ? meth->get_bcea() : NULL;
1151       // fall-through if not a Java method or no analyzer information
1152       if (call_analyzer != NULL) {
1153         PointsToNode* call_ptn = ptnode_adr(call->_idx);
1154         const TypeTuple* d = call->tf()->domain();
1155         for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1156           const Type* at = d->field_at(i);
1157           int k = i - TypeFunc::Parms;
1158           Node* arg = call->in(i);
1159           PointsToNode* arg_ptn = ptnode_adr(arg->_idx);
1160           if (at->isa_ptr() != NULL &&
1161               call_analyzer->is_arg_returned(k)) {
1162             // The call returns arguments.
1163             if (call_ptn != NULL) { // Is call's result used?
1164               assert(call_ptn->is_LocalVar(), "node should be registered");
1165               assert(arg_ptn != NULL, "node should be registered");
1166               add_edge(call_ptn, arg_ptn);
1167             }
1168           }
1169           if (at->isa_oopptr() != NULL &&
1170               arg_ptn->escape_state() < PointsToNode::GlobalEscape) {
1171             if (!call_analyzer->is_arg_stack(k)) {
1172               // The argument global escapes
1173               set_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1174             } else {
1175               set_escape_state(arg_ptn, PointsToNode::ArgEscape);
1176               if (!call_analyzer->is_arg_local(k)) {
1177                 // The argument itself doesn't escape, but any fields might
1178                 set_fields_escape_state(arg_ptn, PointsToNode::GlobalEscape);
1179               }
1180             }
1181           }
1182         }
1183         if (call_ptn != NULL && call_ptn->is_LocalVar()) {
1184           // The call returns arguments.
1185           assert(call_ptn->edge_count() > 0, "sanity");
1186           if (!call_analyzer->is_return_local()) {
1187             // Returns also unknown object.
1188             add_edge(call_ptn, phantom_obj);
1189           }
1190         }
1191         break;
1192       }
1193     }
1194     default: {
1195       // Fall-through here if not a Java method or no analyzer information
1196       // or some other type of call, assume the worst case: all arguments
1197       // globally escape.
1198       const TypeTuple* d = call->tf()->domain();
1199       for (uint i = TypeFunc::Parms; i < d->cnt(); i++) {
1200         const Type* at = d->field_at(i);
1201         if (at->isa_oopptr() != NULL) {
1202           Node* arg = call->in(i);
1203           if (arg->is_AddP()) {
1204             arg = get_addp_base(arg);
1205           }
1206           assert(ptnode_adr(arg->_idx) != NULL, "should be defined already");
1207           set_escape_state(ptnode_adr(arg->_idx), PointsToNode::GlobalEscape);
1208         }
1209       }
1210     }
1211   }
1212 }
1213 
1214 
1215 // Finish Graph construction.
complete_connection_graph(GrowableArray<PointsToNode * > & ptnodes_worklist,GrowableArray<JavaObjectNode * > & non_escaped_worklist,GrowableArray<JavaObjectNode * > & java_objects_worklist,GrowableArray<FieldNode * > & oop_fields_worklist)1216 bool ConnectionGraph::complete_connection_graph(
1217                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
1218                          GrowableArray<JavaObjectNode*>& non_escaped_worklist,
1219                          GrowableArray<JavaObjectNode*>& java_objects_worklist,
1220                          GrowableArray<FieldNode*>&      oop_fields_worklist) {
1221   // Normally only 1-3 passes needed to build Connection Graph depending
1222   // on graph complexity. Observed 8 passes in jvm2008 compiler.compiler.
1223   // Set limit to 20 to catch situation when something did go wrong and
1224   // bailout Escape Analysis.
1225   // Also limit build time to 20 sec (60 in debug VM), EscapeAnalysisTimeout flag.
1226 #define CG_BUILD_ITER_LIMIT 20
1227 
1228   // Propagate GlobalEscape and ArgEscape escape states and check that
1229   // we still have non-escaping objects. The method pushs on _worklist
1230   // Field nodes which reference phantom_object.
1231   if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
1232     return false; // Nothing to do.
1233   }
1234   // Now propagate references to all JavaObject nodes.
1235   int java_objects_length = java_objects_worklist.length();
1236   elapsedTimer time;
1237   bool timeout = false;
1238   int new_edges = 1;
1239   int iterations = 0;
1240   do {
1241     while ((new_edges > 0) &&
1242            (iterations++ < CG_BUILD_ITER_LIMIT)) {
1243       double start_time = time.seconds();
1244       time.start();
1245       new_edges = 0;
1246       // Propagate references to phantom_object for nodes pushed on _worklist
1247       // by find_non_escaped_objects() and find_field_value().
1248       new_edges += add_java_object_edges(phantom_obj, false);
1249       for (int next = 0; next < java_objects_length; ++next) {
1250         JavaObjectNode* ptn = java_objects_worklist.at(next);
1251         new_edges += add_java_object_edges(ptn, true);
1252 
1253 #define SAMPLE_SIZE 4
1254         if ((next % SAMPLE_SIZE) == 0) {
1255           // Each 4 iterations calculate how much time it will take
1256           // to complete graph construction.
1257           time.stop();
1258           // Poll for requests from shutdown mechanism to quiesce compiler
1259           // because Connection graph construction may take long time.
1260           CompileBroker::maybe_block();
1261           double stop_time = time.seconds();
1262           double time_per_iter = (stop_time - start_time) / (double)SAMPLE_SIZE;
1263           double time_until_end = time_per_iter * (double)(java_objects_length - next);
1264           if ((start_time + time_until_end) >= EscapeAnalysisTimeout) {
1265             timeout = true;
1266             break; // Timeout
1267           }
1268           start_time = stop_time;
1269           time.start();
1270         }
1271 #undef SAMPLE_SIZE
1272 
1273       }
1274       if (timeout) break;
1275       if (new_edges > 0) {
1276         // Update escape states on each iteration if graph was updated.
1277         if (!find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist)) {
1278           return false; // Nothing to do.
1279         }
1280       }
1281       time.stop();
1282       if (time.seconds() >= EscapeAnalysisTimeout) {
1283         timeout = true;
1284         break;
1285       }
1286     }
1287     if ((iterations < CG_BUILD_ITER_LIMIT) && !timeout) {
1288       time.start();
1289       // Find fields which have unknown value.
1290       int fields_length = oop_fields_worklist.length();
1291       for (int next = 0; next < fields_length; next++) {
1292         FieldNode* field = oop_fields_worklist.at(next);
1293         if (field->edge_count() == 0) {
1294           new_edges += find_field_value(field);
1295           // This code may added new edges to phantom_object.
1296           // Need an other cycle to propagate references to phantom_object.
1297         }
1298       }
1299       time.stop();
1300       if (time.seconds() >= EscapeAnalysisTimeout) {
1301         timeout = true;
1302         break;
1303       }
1304     } else {
1305       new_edges = 0; // Bailout
1306     }
1307   } while (new_edges > 0);
1308 
1309   // Bailout if passed limits.
1310   if ((iterations >= CG_BUILD_ITER_LIMIT) || timeout) {
1311     Compile* C = _compile;
1312     if (C->log() != NULL) {
1313       C->log()->begin_elem("connectionGraph_bailout reason='reached ");
1314       C->log()->text("%s", timeout ? "time" : "iterations");
1315       C->log()->end_elem(" limit'");
1316     }
1317     assert(ExitEscapeAnalysisOnTimeout, "infinite EA connection graph build (%f sec, %d iterations) with %d nodes and worklist size %d",
1318            time.seconds(), iterations, nodes_size(), ptnodes_worklist.length());
1319     // Possible infinite build_connection_graph loop,
1320     // bailout (no changes to ideal graph were made).
1321     return false;
1322   }
1323 #ifdef ASSERT
1324   if (Verbose && PrintEscapeAnalysis) {
1325     tty->print_cr("EA: %d iterations to build connection graph with %d nodes and worklist size %d",
1326                   iterations, nodes_size(), ptnodes_worklist.length());
1327   }
1328 #endif
1329 
1330 #undef CG_BUILD_ITER_LIMIT
1331 
1332   // Find fields initialized by NULL for non-escaping Allocations.
1333   int non_escaped_length = non_escaped_worklist.length();
1334   for (int next = 0; next < non_escaped_length; next++) {
1335     JavaObjectNode* ptn = non_escaped_worklist.at(next);
1336     PointsToNode::EscapeState es = ptn->escape_state();
1337     assert(es <= PointsToNode::ArgEscape, "sanity");
1338     if (es == PointsToNode::NoEscape) {
1339       if (find_init_values(ptn, null_obj, _igvn) > 0) {
1340         // Adding references to NULL object does not change escape states
1341         // since it does not escape. Also no fields are added to NULL object.
1342         add_java_object_edges(null_obj, false);
1343       }
1344     }
1345     Node* n = ptn->ideal_node();
1346     if (n->is_Allocate()) {
1347       // The object allocated by this Allocate node will never be
1348       // seen by an other thread. Mark it so that when it is
1349       // expanded no MemBarStoreStore is added.
1350       InitializeNode* ini = n->as_Allocate()->initialization();
1351       if (ini != NULL)
1352         ini->set_does_not_escape();
1353     }
1354   }
1355   return true; // Finished graph construction.
1356 }
1357 
1358 // Propagate GlobalEscape and ArgEscape escape states to all nodes
1359 // and check that we still have non-escaping java objects.
find_non_escaped_objects(GrowableArray<PointsToNode * > & ptnodes_worklist,GrowableArray<JavaObjectNode * > & non_escaped_worklist)1360 bool ConnectionGraph::find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,
1361                                                GrowableArray<JavaObjectNode*>& non_escaped_worklist) {
1362   GrowableArray<PointsToNode*> escape_worklist;
1363   // First, put all nodes with GlobalEscape and ArgEscape states on worklist.
1364   int ptnodes_length = ptnodes_worklist.length();
1365   for (int next = 0; next < ptnodes_length; ++next) {
1366     PointsToNode* ptn = ptnodes_worklist.at(next);
1367     if (ptn->escape_state() >= PointsToNode::ArgEscape ||
1368         ptn->fields_escape_state() >= PointsToNode::ArgEscape) {
1369       escape_worklist.push(ptn);
1370     }
1371   }
1372   // Set escape states to referenced nodes (edges list).
1373   while (escape_worklist.length() > 0) {
1374     PointsToNode* ptn = escape_worklist.pop();
1375     PointsToNode::EscapeState es  = ptn->escape_state();
1376     PointsToNode::EscapeState field_es = ptn->fields_escape_state();
1377     if (ptn->is_Field() && ptn->as_Field()->is_oop() &&
1378         es >= PointsToNode::ArgEscape) {
1379       // GlobalEscape or ArgEscape state of field means it has unknown value.
1380       if (add_edge(ptn, phantom_obj)) {
1381         // New edge was added
1382         add_field_uses_to_worklist(ptn->as_Field());
1383       }
1384     }
1385     for (EdgeIterator i(ptn); i.has_next(); i.next()) {
1386       PointsToNode* e = i.get();
1387       if (e->is_Arraycopy()) {
1388         assert(ptn->arraycopy_dst(), "sanity");
1389         // Propagate only fields escape state through arraycopy edge.
1390         if (e->fields_escape_state() < field_es) {
1391           set_fields_escape_state(e, field_es);
1392           escape_worklist.push(e);
1393         }
1394       } else if (es >= field_es) {
1395         // fields_escape_state is also set to 'es' if it is less than 'es'.
1396         if (e->escape_state() < es) {
1397           set_escape_state(e, es);
1398           escape_worklist.push(e);
1399         }
1400       } else {
1401         // Propagate field escape state.
1402         bool es_changed = false;
1403         if (e->fields_escape_state() < field_es) {
1404           set_fields_escape_state(e, field_es);
1405           es_changed = true;
1406         }
1407         if ((e->escape_state() < field_es) &&
1408             e->is_Field() && ptn->is_JavaObject() &&
1409             e->as_Field()->is_oop()) {
1410           // Change escape state of referenced fields.
1411           set_escape_state(e, field_es);
1412           es_changed = true;
1413         } else if (e->escape_state() < es) {
1414           set_escape_state(e, es);
1415           es_changed = true;
1416         }
1417         if (es_changed) {
1418           escape_worklist.push(e);
1419         }
1420       }
1421     }
1422   }
1423   // Remove escaped objects from non_escaped list.
1424   for (int next = non_escaped_worklist.length()-1; next >= 0 ; --next) {
1425     JavaObjectNode* ptn = non_escaped_worklist.at(next);
1426     if (ptn->escape_state() >= PointsToNode::GlobalEscape) {
1427       non_escaped_worklist.delete_at(next);
1428     }
1429     if (ptn->escape_state() == PointsToNode::NoEscape) {
1430       // Find fields in non-escaped allocations which have unknown value.
1431       find_init_values(ptn, phantom_obj, NULL);
1432     }
1433   }
1434   return (non_escaped_worklist.length() > 0);
1435 }
1436 
1437 // Add all references to JavaObject node by walking over all uses.
add_java_object_edges(JavaObjectNode * jobj,bool populate_worklist)1438 int ConnectionGraph::add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist) {
1439   int new_edges = 0;
1440   if (populate_worklist) {
1441     // Populate _worklist by uses of jobj's uses.
1442     for (UseIterator i(jobj); i.has_next(); i.next()) {
1443       PointsToNode* use = i.get();
1444       if (use->is_Arraycopy())
1445         continue;
1446       add_uses_to_worklist(use);
1447       if (use->is_Field() && use->as_Field()->is_oop()) {
1448         // Put on worklist all field's uses (loads) and
1449         // related field nodes (same base and offset).
1450         add_field_uses_to_worklist(use->as_Field());
1451       }
1452     }
1453   }
1454   for (int l = 0; l < _worklist.length(); l++) {
1455     PointsToNode* use = _worklist.at(l);
1456     if (PointsToNode::is_base_use(use)) {
1457       // Add reference from jobj to field and from field to jobj (field's base).
1458       use = PointsToNode::get_use_node(use)->as_Field();
1459       if (add_base(use->as_Field(), jobj)) {
1460         new_edges++;
1461       }
1462       continue;
1463     }
1464     assert(!use->is_JavaObject(), "sanity");
1465     if (use->is_Arraycopy()) {
1466       if (jobj == null_obj) // NULL object does not have field edges
1467         continue;
1468       // Added edge from Arraycopy node to arraycopy's source java object
1469       if (add_edge(use, jobj)) {
1470         jobj->set_arraycopy_src();
1471         new_edges++;
1472       }
1473       // and stop here.
1474       continue;
1475     }
1476     if (!add_edge(use, jobj))
1477       continue; // No new edge added, there was such edge already.
1478     new_edges++;
1479     if (use->is_LocalVar()) {
1480       add_uses_to_worklist(use);
1481       if (use->arraycopy_dst()) {
1482         for (EdgeIterator i(use); i.has_next(); i.next()) {
1483           PointsToNode* e = i.get();
1484           if (e->is_Arraycopy()) {
1485             if (jobj == null_obj) // NULL object does not have field edges
1486               continue;
1487             // Add edge from arraycopy's destination java object to Arraycopy node.
1488             if (add_edge(jobj, e)) {
1489               new_edges++;
1490               jobj->set_arraycopy_dst();
1491             }
1492           }
1493         }
1494       }
1495     } else {
1496       // Added new edge to stored in field values.
1497       // Put on worklist all field's uses (loads) and
1498       // related field nodes (same base and offset).
1499       add_field_uses_to_worklist(use->as_Field());
1500     }
1501   }
1502   _worklist.clear();
1503   _in_worklist.reset();
1504   return new_edges;
1505 }
1506 
1507 // Put on worklist all related field nodes.
add_field_uses_to_worklist(FieldNode * field)1508 void ConnectionGraph::add_field_uses_to_worklist(FieldNode* field) {
1509   assert(field->is_oop(), "sanity");
1510   int offset = field->offset();
1511   add_uses_to_worklist(field);
1512   // Loop over all bases of this field and push on worklist Field nodes
1513   // with the same offset and base (since they may reference the same field).
1514   for (BaseIterator i(field); i.has_next(); i.next()) {
1515     PointsToNode* base = i.get();
1516     add_fields_to_worklist(field, base);
1517     // Check if the base was source object of arraycopy and go over arraycopy's
1518     // destination objects since values stored to a field of source object are
1519     // accessable by uses (loads) of fields of destination objects.
1520     if (base->arraycopy_src()) {
1521       for (UseIterator j(base); j.has_next(); j.next()) {
1522         PointsToNode* arycp = j.get();
1523         if (arycp->is_Arraycopy()) {
1524           for (UseIterator k(arycp); k.has_next(); k.next()) {
1525             PointsToNode* abase = k.get();
1526             if (abase->arraycopy_dst() && abase != base) {
1527               // Look for the same arraycopy reference.
1528               add_fields_to_worklist(field, abase);
1529             }
1530           }
1531         }
1532       }
1533     }
1534   }
1535 }
1536 
1537 // Put on worklist all related field nodes.
add_fields_to_worklist(FieldNode * field,PointsToNode * base)1538 void ConnectionGraph::add_fields_to_worklist(FieldNode* field, PointsToNode* base) {
1539   int offset = field->offset();
1540   if (base->is_LocalVar()) {
1541     for (UseIterator j(base); j.has_next(); j.next()) {
1542       PointsToNode* f = j.get();
1543       if (PointsToNode::is_base_use(f)) { // Field
1544         f = PointsToNode::get_use_node(f);
1545         if (f == field || !f->as_Field()->is_oop())
1546           continue;
1547         int offs = f->as_Field()->offset();
1548         if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
1549           add_to_worklist(f);
1550         }
1551       }
1552     }
1553   } else {
1554     assert(base->is_JavaObject(), "sanity");
1555     if (// Skip phantom_object since it is only used to indicate that
1556         // this field's content globally escapes.
1557         (base != phantom_obj) &&
1558         // NULL object node does not have fields.
1559         (base != null_obj)) {
1560       for (EdgeIterator i(base); i.has_next(); i.next()) {
1561         PointsToNode* f = i.get();
1562         // Skip arraycopy edge since store to destination object field
1563         // does not update value in source object field.
1564         if (f->is_Arraycopy()) {
1565           assert(base->arraycopy_dst(), "sanity");
1566           continue;
1567         }
1568         if (f == field || !f->as_Field()->is_oop())
1569           continue;
1570         int offs = f->as_Field()->offset();
1571         if (offs == offset || offset == Type::OffsetBot || offs == Type::OffsetBot) {
1572           add_to_worklist(f);
1573         }
1574       }
1575     }
1576   }
1577 }
1578 
1579 // Find fields which have unknown value.
find_field_value(FieldNode * field)1580 int ConnectionGraph::find_field_value(FieldNode* field) {
1581   // Escaped fields should have init value already.
1582   assert(field->escape_state() == PointsToNode::NoEscape, "sanity");
1583   int new_edges = 0;
1584   for (BaseIterator i(field); i.has_next(); i.next()) {
1585     PointsToNode* base = i.get();
1586     if (base->is_JavaObject()) {
1587       // Skip Allocate's fields which will be processed later.
1588       if (base->ideal_node()->is_Allocate())
1589         return 0;
1590       assert(base == null_obj, "only NULL ptr base expected here");
1591     }
1592   }
1593   if (add_edge(field, phantom_obj)) {
1594     // New edge was added
1595     new_edges++;
1596     add_field_uses_to_worklist(field);
1597   }
1598   return new_edges;
1599 }
1600 
1601 // Find fields initializing values for allocations.
find_init_values(JavaObjectNode * pta,PointsToNode * init_val,PhaseTransform * phase)1602 int ConnectionGraph::find_init_values(JavaObjectNode* pta, PointsToNode* init_val, PhaseTransform* phase) {
1603   assert(pta->escape_state() == PointsToNode::NoEscape, "Not escaped Allocate nodes only");
1604   int new_edges = 0;
1605   Node* alloc = pta->ideal_node();
1606   if (init_val == phantom_obj) {
1607     // Do nothing for Allocate nodes since its fields values are
1608     // "known" unless they are initialized by arraycopy/clone.
1609     if (alloc->is_Allocate() && !pta->arraycopy_dst())
1610       return 0;
1611     assert(pta->arraycopy_dst() || alloc->as_CallStaticJava(), "sanity");
1612 #ifdef ASSERT
1613     if (!pta->arraycopy_dst() && alloc->as_CallStaticJava()->method() == NULL) {
1614       const char* name = alloc->as_CallStaticJava()->_name;
1615       assert(strncmp(name, "_multianewarray", 15) == 0, "sanity");
1616     }
1617 #endif
1618     // Non-escaped allocation returned from Java or runtime call have
1619     // unknown values in fields.
1620     for (EdgeIterator i(pta); i.has_next(); i.next()) {
1621       PointsToNode* field = i.get();
1622       if (field->is_Field() && field->as_Field()->is_oop()) {
1623         if (add_edge(field, phantom_obj)) {
1624           // New edge was added
1625           new_edges++;
1626           add_field_uses_to_worklist(field->as_Field());
1627         }
1628       }
1629     }
1630     return new_edges;
1631   }
1632   assert(init_val == null_obj, "sanity");
1633   // Do nothing for Call nodes since its fields values are unknown.
1634   if (!alloc->is_Allocate())
1635     return 0;
1636 
1637   InitializeNode* ini = alloc->as_Allocate()->initialization();
1638   bool visited_bottom_offset = false;
1639   GrowableArray<int> offsets_worklist;
1640 
1641   // Check if an oop field's initializing value is recorded and add
1642   // a corresponding NULL if field's value if it is not recorded.
1643   // Connection Graph does not record a default initialization by NULL
1644   // captured by Initialize node.
1645   //
1646   for (EdgeIterator i(pta); i.has_next(); i.next()) {
1647     PointsToNode* field = i.get(); // Field (AddP)
1648     if (!field->is_Field() || !field->as_Field()->is_oop())
1649       continue; // Not oop field
1650     int offset = field->as_Field()->offset();
1651     if (offset == Type::OffsetBot) {
1652       if (!visited_bottom_offset) {
1653         // OffsetBot is used to reference array's element,
1654         // always add reference to NULL to all Field nodes since we don't
1655         // known which element is referenced.
1656         if (add_edge(field, null_obj)) {
1657           // New edge was added
1658           new_edges++;
1659           add_field_uses_to_worklist(field->as_Field());
1660           visited_bottom_offset = true;
1661         }
1662       }
1663     } else {
1664       // Check only oop fields.
1665       const Type* adr_type = field->ideal_node()->as_AddP()->bottom_type();
1666       if (adr_type->isa_rawptr()) {
1667 #ifdef ASSERT
1668         // Raw pointers are used for initializing stores so skip it
1669         // since it should be recorded already
1670         Node* base = get_addp_base(field->ideal_node());
1671         assert(adr_type->isa_rawptr() && is_captured_store_address(field->ideal_node()), "unexpected pointer type");
1672 #endif
1673         continue;
1674       }
1675       if (!offsets_worklist.contains(offset)) {
1676         offsets_worklist.append(offset);
1677         Node* value = NULL;
1678         if (ini != NULL) {
1679           // StoreP::memory_type() == T_ADDRESS
1680           BasicType ft = UseCompressedOops ? T_NARROWOOP : T_ADDRESS;
1681           Node* store = ini->find_captured_store(offset, type2aelembytes(ft, true), phase);
1682           // Make sure initializing store has the same type as this AddP.
1683           // This AddP may reference non existing field because it is on a
1684           // dead branch of bimorphic call which is not eliminated yet.
1685           if (store != NULL && store->is_Store() &&
1686               store->as_Store()->memory_type() == ft) {
1687             value = store->in(MemNode::ValueIn);
1688 #ifdef ASSERT
1689             if (VerifyConnectionGraph) {
1690               // Verify that AddP already points to all objects the value points to.
1691               PointsToNode* val = ptnode_adr(value->_idx);
1692               assert((val != NULL), "should be processed already");
1693               PointsToNode* missed_obj = NULL;
1694               if (val->is_JavaObject()) {
1695                 if (!field->points_to(val->as_JavaObject())) {
1696                   missed_obj = val;
1697                 }
1698               } else {
1699                 if (!val->is_LocalVar() || (val->edge_count() == 0)) {
1700                   tty->print_cr("----------init store has invalid value -----");
1701                   store->dump();
1702                   val->dump();
1703                   assert(val->is_LocalVar() && (val->edge_count() > 0), "should be processed already");
1704                 }
1705                 for (EdgeIterator j(val); j.has_next(); j.next()) {
1706                   PointsToNode* obj = j.get();
1707                   if (obj->is_JavaObject()) {
1708                     if (!field->points_to(obj->as_JavaObject())) {
1709                       missed_obj = obj;
1710                       break;
1711                     }
1712                   }
1713                 }
1714               }
1715               if (missed_obj != NULL) {
1716                 tty->print_cr("----------field---------------------------------");
1717                 field->dump();
1718                 tty->print_cr("----------missed referernce to object-----------");
1719                 missed_obj->dump();
1720                 tty->print_cr("----------object referernced by init store -----");
1721                 store->dump();
1722                 val->dump();
1723                 assert(!field->points_to(missed_obj->as_JavaObject()), "missed JavaObject reference");
1724               }
1725             }
1726 #endif
1727           } else {
1728             // There could be initializing stores which follow allocation.
1729             // For example, a volatile field store is not collected
1730             // by Initialize node.
1731             //
1732             // Need to check for dependent loads to separate such stores from
1733             // stores which follow loads. For now, add initial value NULL so
1734             // that compare pointers optimization works correctly.
1735           }
1736         }
1737         if (value == NULL) {
1738           // A field's initializing value was not recorded. Add NULL.
1739           if (add_edge(field, null_obj)) {
1740             // New edge was added
1741             new_edges++;
1742             add_field_uses_to_worklist(field->as_Field());
1743           }
1744         }
1745       }
1746     }
1747   }
1748   return new_edges;
1749 }
1750 
1751 // Adjust scalar_replaceable state after Connection Graph is built.
adjust_scalar_replaceable_state(JavaObjectNode * jobj)1752 void ConnectionGraph::adjust_scalar_replaceable_state(JavaObjectNode* jobj) {
1753   // Search for non-escaping objects which are not scalar replaceable
1754   // and mark them to propagate the state to referenced objects.
1755 
1756   // 1. An object is not scalar replaceable if the field into which it is
1757   // stored has unknown offset (stored into unknown element of an array).
1758   //
1759   for (UseIterator i(jobj); i.has_next(); i.next()) {
1760     PointsToNode* use = i.get();
1761     if (use->is_Arraycopy()) {
1762       continue;
1763     }
1764     if (use->is_Field()) {
1765       FieldNode* field = use->as_Field();
1766       assert(field->is_oop() && field->scalar_replaceable(), "sanity");
1767       if (field->offset() == Type::OffsetBot) {
1768         jobj->set_scalar_replaceable(false);
1769         return;
1770       }
1771       // 2. An object is not scalar replaceable if the field into which it is
1772       // stored has multiple bases one of which is null.
1773       if (field->base_count() > 1) {
1774         for (BaseIterator i(field); i.has_next(); i.next()) {
1775           PointsToNode* base = i.get();
1776           if (base == null_obj) {
1777             jobj->set_scalar_replaceable(false);
1778             return;
1779           }
1780         }
1781       }
1782     }
1783     assert(use->is_Field() || use->is_LocalVar(), "sanity");
1784     // 3. An object is not scalar replaceable if it is merged with other objects.
1785     for (EdgeIterator j(use); j.has_next(); j.next()) {
1786       PointsToNode* ptn = j.get();
1787       if (ptn->is_JavaObject() && ptn != jobj) {
1788         // Mark all objects.
1789         jobj->set_scalar_replaceable(false);
1790          ptn->set_scalar_replaceable(false);
1791       }
1792     }
1793     if (!jobj->scalar_replaceable()) {
1794       return;
1795     }
1796   }
1797 
1798   for (EdgeIterator j(jobj); j.has_next(); j.next()) {
1799     if (j.get()->is_Arraycopy()) {
1800       continue;
1801     }
1802 
1803     // Non-escaping object node should point only to field nodes.
1804     FieldNode* field = j.get()->as_Field();
1805     int offset = field->as_Field()->offset();
1806 
1807     // 4. An object is not scalar replaceable if it has a field with unknown
1808     // offset (array's element is accessed in loop).
1809     if (offset == Type::OffsetBot) {
1810       jobj->set_scalar_replaceable(false);
1811       return;
1812     }
1813     // 5. Currently an object is not scalar replaceable if a LoadStore node
1814     // access its field since the field value is unknown after it.
1815     //
1816     Node* n = field->ideal_node();
1817 
1818     // Test for an unsafe access that was parsed as maybe off heap
1819     // (with a CheckCastPP to raw memory).
1820     assert(n->is_AddP(), "expect an address computation");
1821     if (n->in(AddPNode::Base)->is_top() &&
1822         n->in(AddPNode::Address)->Opcode() == Op_CheckCastPP) {
1823       assert(n->in(AddPNode::Address)->bottom_type()->isa_rawptr(), "raw address so raw cast expected");
1824       assert(_igvn->type(n->in(AddPNode::Address)->in(1))->isa_oopptr(), "cast pattern at unsafe access expected");
1825       jobj->set_scalar_replaceable(false);
1826       return;
1827     }
1828 
1829     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1830       Node* u = n->fast_out(i);
1831       if (u->is_LoadStore() || (u->is_Mem() && u->as_Mem()->is_mismatched_access())) {
1832         jobj->set_scalar_replaceable(false);
1833         return;
1834       }
1835     }
1836 
1837     // 6. Or the address may point to more then one object. This may produce
1838     // the false positive result (set not scalar replaceable)
1839     // since the flow-insensitive escape analysis can't separate
1840     // the case when stores overwrite the field's value from the case
1841     // when stores happened on different control branches.
1842     //
1843     // Note: it will disable scalar replacement in some cases:
1844     //
1845     //    Point p[] = new Point[1];
1846     //    p[0] = new Point(); // Will be not scalar replaced
1847     //
1848     // but it will save us from incorrect optimizations in next cases:
1849     //
1850     //    Point p[] = new Point[1];
1851     //    if ( x ) p[0] = new Point(); // Will be not scalar replaced
1852     //
1853     if (field->base_count() > 1) {
1854       for (BaseIterator i(field); i.has_next(); i.next()) {
1855         PointsToNode* base = i.get();
1856         // Don't take into account LocalVar nodes which
1857         // may point to only one object which should be also
1858         // this field's base by now.
1859         if (base->is_JavaObject() && base != jobj) {
1860           // Mark all bases.
1861           jobj->set_scalar_replaceable(false);
1862           base->set_scalar_replaceable(false);
1863         }
1864       }
1865     }
1866   }
1867 }
1868 
1869 #ifdef ASSERT
verify_connection_graph(GrowableArray<PointsToNode * > & ptnodes_worklist,GrowableArray<JavaObjectNode * > & non_escaped_worklist,GrowableArray<JavaObjectNode * > & java_objects_worklist,GrowableArray<Node * > & addp_worklist)1870 void ConnectionGraph::verify_connection_graph(
1871                          GrowableArray<PointsToNode*>&   ptnodes_worklist,
1872                          GrowableArray<JavaObjectNode*>& non_escaped_worklist,
1873                          GrowableArray<JavaObjectNode*>& java_objects_worklist,
1874                          GrowableArray<Node*>& addp_worklist) {
1875   // Verify that graph is complete - no new edges could be added.
1876   int java_objects_length = java_objects_worklist.length();
1877   int non_escaped_length  = non_escaped_worklist.length();
1878   int new_edges = 0;
1879   for (int next = 0; next < java_objects_length; ++next) {
1880     JavaObjectNode* ptn = java_objects_worklist.at(next);
1881     new_edges += add_java_object_edges(ptn, true);
1882   }
1883   assert(new_edges == 0, "graph was not complete");
1884   // Verify that escape state is final.
1885   int length = non_escaped_worklist.length();
1886   find_non_escaped_objects(ptnodes_worklist, non_escaped_worklist);
1887   assert((non_escaped_length == non_escaped_worklist.length()) &&
1888          (non_escaped_length == length) &&
1889          (_worklist.length() == 0), "escape state was not final");
1890 
1891   // Verify fields information.
1892   int addp_length = addp_worklist.length();
1893   for (int next = 0; next < addp_length; ++next ) {
1894     Node* n = addp_worklist.at(next);
1895     FieldNode* field = ptnode_adr(n->_idx)->as_Field();
1896     if (field->is_oop()) {
1897       // Verify that field has all bases
1898       Node* base = get_addp_base(n);
1899       PointsToNode* ptn = ptnode_adr(base->_idx);
1900       if (ptn->is_JavaObject()) {
1901         assert(field->has_base(ptn->as_JavaObject()), "sanity");
1902       } else {
1903         assert(ptn->is_LocalVar(), "sanity");
1904         for (EdgeIterator i(ptn); i.has_next(); i.next()) {
1905           PointsToNode* e = i.get();
1906           if (e->is_JavaObject()) {
1907             assert(field->has_base(e->as_JavaObject()), "sanity");
1908           }
1909         }
1910       }
1911       // Verify that all fields have initializing values.
1912       if (field->edge_count() == 0) {
1913         tty->print_cr("----------field does not have references----------");
1914         field->dump();
1915         for (BaseIterator i(field); i.has_next(); i.next()) {
1916           PointsToNode* base = i.get();
1917           tty->print_cr("----------field has next base---------------------");
1918           base->dump();
1919           if (base->is_JavaObject() && (base != phantom_obj) && (base != null_obj)) {
1920             tty->print_cr("----------base has fields-------------------------");
1921             for (EdgeIterator j(base); j.has_next(); j.next()) {
1922               j.get()->dump();
1923             }
1924             tty->print_cr("----------base has references---------------------");
1925             for (UseIterator j(base); j.has_next(); j.next()) {
1926               j.get()->dump();
1927             }
1928           }
1929         }
1930         for (UseIterator i(field); i.has_next(); i.next()) {
1931           i.get()->dump();
1932         }
1933         assert(field->edge_count() > 0, "sanity");
1934       }
1935     }
1936   }
1937 }
1938 #endif
1939 
1940 // Optimize ideal graph.
optimize_ideal_graph(GrowableArray<Node * > & ptr_cmp_worklist,GrowableArray<Node * > & storestore_worklist)1941 void ConnectionGraph::optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
1942                                            GrowableArray<Node*>& storestore_worklist) {
1943   Compile* C = _compile;
1944   PhaseIterGVN* igvn = _igvn;
1945   if (EliminateLocks) {
1946     // Mark locks before changing ideal graph.
1947     int cnt = C->macro_count();
1948     for( int i=0; i < cnt; i++ ) {
1949       Node *n = C->macro_node(i);
1950       if (n->is_AbstractLock()) { // Lock and Unlock nodes
1951         AbstractLockNode* alock = n->as_AbstractLock();
1952         if (!alock->is_non_esc_obj()) {
1953           if (not_global_escape(alock->obj_node())) {
1954             assert(!alock->is_eliminated() || alock->is_coarsened(), "sanity");
1955             // The lock could be marked eliminated by lock coarsening
1956             // code during first IGVN before EA. Replace coarsened flag
1957             // to eliminate all associated locks/unlocks.
1958 #ifdef ASSERT
1959             alock->log_lock_optimization(C, "eliminate_lock_set_non_esc3");
1960 #endif
1961             alock->set_non_esc_obj();
1962           }
1963         }
1964       }
1965     }
1966   }
1967 
1968   if (OptimizePtrCompare) {
1969     // Add ConI(#CC_GT) and ConI(#CC_EQ).
1970     _pcmp_neq = igvn->makecon(TypeInt::CC_GT);
1971     _pcmp_eq = igvn->makecon(TypeInt::CC_EQ);
1972     // Optimize objects compare.
1973     while (ptr_cmp_worklist.length() != 0) {
1974       Node *n = ptr_cmp_worklist.pop();
1975       Node *res = optimize_ptr_compare(n);
1976       if (res != NULL) {
1977 #ifndef PRODUCT
1978         if (PrintOptimizePtrCompare) {
1979           tty->print_cr("++++ Replaced: %d %s(%d,%d) --> %s", n->_idx, (n->Opcode() == Op_CmpP ? "CmpP" : "CmpN"), n->in(1)->_idx, n->in(2)->_idx, (res == _pcmp_eq ? "EQ" : "NotEQ"));
1980           if (Verbose) {
1981             n->dump(1);
1982           }
1983         }
1984 #endif
1985         igvn->replace_node(n, res);
1986       }
1987     }
1988     // cleanup
1989     if (_pcmp_neq->outcnt() == 0)
1990       igvn->hash_delete(_pcmp_neq);
1991     if (_pcmp_eq->outcnt()  == 0)
1992       igvn->hash_delete(_pcmp_eq);
1993   }
1994 
1995   // For MemBarStoreStore nodes added in library_call.cpp, check
1996   // escape status of associated AllocateNode and optimize out
1997   // MemBarStoreStore node if the allocated object never escapes.
1998   while (storestore_worklist.length() != 0) {
1999     Node *n = storestore_worklist.pop();
2000     MemBarStoreStoreNode *storestore = n ->as_MemBarStoreStore();
2001     Node *alloc = storestore->in(MemBarNode::Precedent)->in(0);
2002     if (alloc->is_Allocate() && not_global_escape(alloc)) {
2003       MemBarNode* mb = MemBarNode::make(C, Op_MemBarCPUOrder, Compile::AliasIdxBot);
2004       mb->init_req(TypeFunc::Memory, storestore->in(TypeFunc::Memory));
2005       mb->init_req(TypeFunc::Control, storestore->in(TypeFunc::Control));
2006       igvn->register_new_node_with_optimizer(mb);
2007       igvn->replace_node(storestore, mb);
2008     }
2009   }
2010 }
2011 
2012 // Optimize objects compare.
optimize_ptr_compare(Node * n)2013 Node* ConnectionGraph::optimize_ptr_compare(Node* n) {
2014   assert(OptimizePtrCompare, "sanity");
2015   PointsToNode* ptn1 = ptnode_adr(n->in(1)->_idx);
2016   PointsToNode* ptn2 = ptnode_adr(n->in(2)->_idx);
2017   JavaObjectNode* jobj1 = unique_java_object(n->in(1));
2018   JavaObjectNode* jobj2 = unique_java_object(n->in(2));
2019   assert(ptn1->is_JavaObject() || ptn1->is_LocalVar(), "sanity");
2020   assert(ptn2->is_JavaObject() || ptn2->is_LocalVar(), "sanity");
2021 
2022   // Check simple cases first.
2023   if (jobj1 != NULL) {
2024     if (jobj1->escape_state() == PointsToNode::NoEscape) {
2025       if (jobj1 == jobj2) {
2026         // Comparing the same not escaping object.
2027         return _pcmp_eq;
2028       }
2029       Node* obj = jobj1->ideal_node();
2030       // Comparing not escaping allocation.
2031       if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
2032           !ptn2->points_to(jobj1)) {
2033         return _pcmp_neq; // This includes nullness check.
2034       }
2035     }
2036   }
2037   if (jobj2 != NULL) {
2038     if (jobj2->escape_state() == PointsToNode::NoEscape) {
2039       Node* obj = jobj2->ideal_node();
2040       // Comparing not escaping allocation.
2041       if ((obj->is_Allocate() || obj->is_CallStaticJava()) &&
2042           !ptn1->points_to(jobj2)) {
2043         return _pcmp_neq; // This includes nullness check.
2044       }
2045     }
2046   }
2047   if (jobj1 != NULL && jobj1 != phantom_obj &&
2048       jobj2 != NULL && jobj2 != phantom_obj &&
2049       jobj1->ideal_node()->is_Con() &&
2050       jobj2->ideal_node()->is_Con()) {
2051     // Klass or String constants compare. Need to be careful with
2052     // compressed pointers - compare types of ConN and ConP instead of nodes.
2053     const Type* t1 = jobj1->ideal_node()->get_ptr_type();
2054     const Type* t2 = jobj2->ideal_node()->get_ptr_type();
2055     if (t1->make_ptr() == t2->make_ptr()) {
2056       return _pcmp_eq;
2057     } else {
2058       return _pcmp_neq;
2059     }
2060   }
2061   if (ptn1->meet(ptn2)) {
2062     return NULL; // Sets are not disjoint
2063   }
2064 
2065   // Sets are disjoint.
2066   bool set1_has_unknown_ptr = ptn1->points_to(phantom_obj);
2067   bool set2_has_unknown_ptr = ptn2->points_to(phantom_obj);
2068   bool set1_has_null_ptr    = ptn1->points_to(null_obj);
2069   bool set2_has_null_ptr    = ptn2->points_to(null_obj);
2070   if ((set1_has_unknown_ptr && set2_has_null_ptr) ||
2071       (set2_has_unknown_ptr && set1_has_null_ptr)) {
2072     // Check nullness of unknown object.
2073     return NULL;
2074   }
2075 
2076   // Disjointness by itself is not sufficient since
2077   // alias analysis is not complete for escaped objects.
2078   // Disjoint sets are definitely unrelated only when
2079   // at least one set has only not escaping allocations.
2080   if (!set1_has_unknown_ptr && !set1_has_null_ptr) {
2081     if (ptn1->non_escaping_allocation()) {
2082       return _pcmp_neq;
2083     }
2084   }
2085   if (!set2_has_unknown_ptr && !set2_has_null_ptr) {
2086     if (ptn2->non_escaping_allocation()) {
2087       return _pcmp_neq;
2088     }
2089   }
2090   return NULL;
2091 }
2092 
2093 // Connection Graph constuction functions.
2094 
add_local_var(Node * n,PointsToNode::EscapeState es)2095 void ConnectionGraph::add_local_var(Node *n, PointsToNode::EscapeState es) {
2096   PointsToNode* ptadr = _nodes.at(n->_idx);
2097   if (ptadr != NULL) {
2098     assert(ptadr->is_LocalVar() && ptadr->ideal_node() == n, "sanity");
2099     return;
2100   }
2101   Compile* C = _compile;
2102   ptadr = new (C->comp_arena()) LocalVarNode(this, n, es);
2103   _nodes.at_put(n->_idx, ptadr);
2104 }
2105 
add_java_object(Node * n,PointsToNode::EscapeState es)2106 void ConnectionGraph::add_java_object(Node *n, PointsToNode::EscapeState es) {
2107   PointsToNode* ptadr = _nodes.at(n->_idx);
2108   if (ptadr != NULL) {
2109     assert(ptadr->is_JavaObject() && ptadr->ideal_node() == n, "sanity");
2110     return;
2111   }
2112   Compile* C = _compile;
2113   ptadr = new (C->comp_arena()) JavaObjectNode(this, n, es);
2114   _nodes.at_put(n->_idx, ptadr);
2115 }
2116 
add_field(Node * n,PointsToNode::EscapeState es,int offset)2117 void ConnectionGraph::add_field(Node *n, PointsToNode::EscapeState es, int offset) {
2118   PointsToNode* ptadr = _nodes.at(n->_idx);
2119   if (ptadr != NULL) {
2120     assert(ptadr->is_Field() && ptadr->ideal_node() == n, "sanity");
2121     return;
2122   }
2123   bool unsafe = false;
2124   bool is_oop = is_oop_field(n, offset, &unsafe);
2125   if (unsafe) {
2126     es = PointsToNode::GlobalEscape;
2127   }
2128   Compile* C = _compile;
2129   FieldNode* field = new (C->comp_arena()) FieldNode(this, n, es, offset, is_oop);
2130   _nodes.at_put(n->_idx, field);
2131 }
2132 
add_arraycopy(Node * n,PointsToNode::EscapeState es,PointsToNode * src,PointsToNode * dst)2133 void ConnectionGraph::add_arraycopy(Node *n, PointsToNode::EscapeState es,
2134                                     PointsToNode* src, PointsToNode* dst) {
2135   assert(!src->is_Field() && !dst->is_Field(), "only for JavaObject and LocalVar");
2136   assert((src != null_obj) && (dst != null_obj), "not for ConP NULL");
2137   PointsToNode* ptadr = _nodes.at(n->_idx);
2138   if (ptadr != NULL) {
2139     assert(ptadr->is_Arraycopy() && ptadr->ideal_node() == n, "sanity");
2140     return;
2141   }
2142   Compile* C = _compile;
2143   ptadr = new (C->comp_arena()) ArraycopyNode(this, n, es);
2144   _nodes.at_put(n->_idx, ptadr);
2145   // Add edge from arraycopy node to source object.
2146   (void)add_edge(ptadr, src);
2147   src->set_arraycopy_src();
2148   // Add edge from destination object to arraycopy node.
2149   (void)add_edge(dst, ptadr);
2150   dst->set_arraycopy_dst();
2151 }
2152 
is_oop_field(Node * n,int offset,bool * unsafe)2153 bool ConnectionGraph::is_oop_field(Node* n, int offset, bool* unsafe) {
2154   const Type* adr_type = n->as_AddP()->bottom_type();
2155   BasicType bt = T_INT;
2156   if (offset == Type::OffsetBot) {
2157     // Check only oop fields.
2158     if (!adr_type->isa_aryptr() ||
2159         (adr_type->isa_aryptr()->klass() == NULL) ||
2160          adr_type->isa_aryptr()->klass()->is_obj_array_klass()) {
2161       // OffsetBot is used to reference array's element. Ignore first AddP.
2162       if (find_second_addp(n, n->in(AddPNode::Base)) == NULL) {
2163         bt = T_OBJECT;
2164       }
2165     }
2166   } else if (offset != oopDesc::klass_offset_in_bytes()) {
2167     if (adr_type->isa_instptr()) {
2168       ciField* field = _compile->alias_type(adr_type->isa_instptr())->field();
2169       if (field != NULL) {
2170         bt = field->layout_type();
2171       } else {
2172         // Check for unsafe oop field access
2173         if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2174             n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2175             n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2176             BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2177           bt = T_OBJECT;
2178           (*unsafe) = true;
2179         }
2180       }
2181     } else if (adr_type->isa_aryptr()) {
2182       if (offset == arrayOopDesc::length_offset_in_bytes()) {
2183         // Ignore array length load.
2184       } else if (find_second_addp(n, n->in(AddPNode::Base)) != NULL) {
2185         // Ignore first AddP.
2186       } else {
2187         const Type* elemtype = adr_type->isa_aryptr()->elem();
2188         bt = elemtype->array_element_basic_type();
2189       }
2190     } else if (adr_type->isa_rawptr() || adr_type->isa_klassptr()) {
2191       // Allocation initialization, ThreadLocal field access, unsafe access
2192       if (n->has_out_with(Op_StoreP, Op_LoadP, Op_StoreN, Op_LoadN) ||
2193           n->has_out_with(Op_GetAndSetP, Op_GetAndSetN, Op_CompareAndExchangeP, Op_CompareAndExchangeN) ||
2194           n->has_out_with(Op_CompareAndSwapP, Op_CompareAndSwapN, Op_WeakCompareAndSwapP, Op_WeakCompareAndSwapN) ||
2195           BarrierSet::barrier_set()->barrier_set_c2()->escape_has_out_with_unsafe_object(n)) {
2196         bt = T_OBJECT;
2197       }
2198     }
2199   }
2200   // Note: T_NARROWOOP is not classed as a real reference type
2201   return (is_reference_type(bt) || bt == T_NARROWOOP);
2202 }
2203 
2204 // Returns unique pointed java object or NULL.
unique_java_object(Node * n)2205 JavaObjectNode* ConnectionGraph::unique_java_object(Node *n) {
2206   assert(!_collecting, "should not call when contructed graph");
2207   // If the node was created after the escape computation we can't answer.
2208   uint idx = n->_idx;
2209   if (idx >= nodes_size()) {
2210     return NULL;
2211   }
2212   PointsToNode* ptn = ptnode_adr(idx);
2213   if (ptn == NULL) {
2214     return NULL;
2215   }
2216   if (ptn->is_JavaObject()) {
2217     return ptn->as_JavaObject();
2218   }
2219   assert(ptn->is_LocalVar(), "sanity");
2220   // Check all java objects it points to.
2221   JavaObjectNode* jobj = NULL;
2222   for (EdgeIterator i(ptn); i.has_next(); i.next()) {
2223     PointsToNode* e = i.get();
2224     if (e->is_JavaObject()) {
2225       if (jobj == NULL) {
2226         jobj = e->as_JavaObject();
2227       } else if (jobj != e) {
2228         return NULL;
2229       }
2230     }
2231   }
2232   return jobj;
2233 }
2234 
2235 // Return true if this node points only to non-escaping allocations.
non_escaping_allocation()2236 bool PointsToNode::non_escaping_allocation() {
2237   if (is_JavaObject()) {
2238     Node* n = ideal_node();
2239     if (n->is_Allocate() || n->is_CallStaticJava()) {
2240       return (escape_state() == PointsToNode::NoEscape);
2241     } else {
2242       return false;
2243     }
2244   }
2245   assert(is_LocalVar(), "sanity");
2246   // Check all java objects it points to.
2247   for (EdgeIterator i(this); i.has_next(); i.next()) {
2248     PointsToNode* e = i.get();
2249     if (e->is_JavaObject()) {
2250       Node* n = e->ideal_node();
2251       if ((e->escape_state() != PointsToNode::NoEscape) ||
2252           !(n->is_Allocate() || n->is_CallStaticJava())) {
2253         return false;
2254       }
2255     }
2256   }
2257   return true;
2258 }
2259 
2260 // Return true if we know the node does not escape globally.
not_global_escape(Node * n)2261 bool ConnectionGraph::not_global_escape(Node *n) {
2262   assert(!_collecting, "should not call during graph construction");
2263   // If the node was created after the escape computation we can't answer.
2264   uint idx = n->_idx;
2265   if (idx >= nodes_size()) {
2266     return false;
2267   }
2268   PointsToNode* ptn = ptnode_adr(idx);
2269   if (ptn == NULL) {
2270     return false; // not in congraph (e.g. ConI)
2271   }
2272   PointsToNode::EscapeState es = ptn->escape_state();
2273   // If we have already computed a value, return it.
2274   if (es >= PointsToNode::GlobalEscape)
2275     return false;
2276   if (ptn->is_JavaObject()) {
2277     return true; // (es < PointsToNode::GlobalEscape);
2278   }
2279   assert(ptn->is_LocalVar(), "sanity");
2280   // Check all java objects it points to.
2281   for (EdgeIterator i(ptn); i.has_next(); i.next()) {
2282     if (i.get()->escape_state() >= PointsToNode::GlobalEscape)
2283       return false;
2284   }
2285   return true;
2286 }
2287 
2288 
2289 // Helper functions
2290 
2291 // Return true if this node points to specified node or nodes it points to.
points_to(JavaObjectNode * ptn) const2292 bool PointsToNode::points_to(JavaObjectNode* ptn) const {
2293   if (is_JavaObject()) {
2294     return (this == ptn);
2295   }
2296   assert(is_LocalVar() || is_Field(), "sanity");
2297   for (EdgeIterator i(this); i.has_next(); i.next()) {
2298     if (i.get() == ptn)
2299       return true;
2300   }
2301   return false;
2302 }
2303 
2304 // Return true if one node points to an other.
meet(PointsToNode * ptn)2305 bool PointsToNode::meet(PointsToNode* ptn) {
2306   if (this == ptn) {
2307     return true;
2308   } else if (ptn->is_JavaObject()) {
2309     return this->points_to(ptn->as_JavaObject());
2310   } else if (this->is_JavaObject()) {
2311     return ptn->points_to(this->as_JavaObject());
2312   }
2313   assert(this->is_LocalVar() && ptn->is_LocalVar(), "sanity");
2314   int ptn_count =  ptn->edge_count();
2315   for (EdgeIterator i(this); i.has_next(); i.next()) {
2316     PointsToNode* this_e = i.get();
2317     for (int j = 0; j < ptn_count; j++) {
2318       if (this_e == ptn->edge(j))
2319         return true;
2320     }
2321   }
2322   return false;
2323 }
2324 
2325 #ifdef ASSERT
2326 // Return true if bases point to this java object.
has_base(JavaObjectNode * jobj) const2327 bool FieldNode::has_base(JavaObjectNode* jobj) const {
2328   for (BaseIterator i(this); i.has_next(); i.next()) {
2329     if (i.get() == jobj)
2330       return true;
2331   }
2332   return false;
2333 }
2334 #endif
2335 
is_captured_store_address(Node * addp)2336 bool ConnectionGraph::is_captured_store_address(Node* addp) {
2337   // Handle simple case first.
2338   assert(_igvn->type(addp)->isa_oopptr() == NULL, "should be raw access");
2339   if (addp->in(AddPNode::Address)->is_Proj() && addp->in(AddPNode::Address)->in(0)->is_Allocate()) {
2340     return true;
2341   } else if (addp->in(AddPNode::Address)->is_Phi()) {
2342     for (DUIterator_Fast imax, i = addp->fast_outs(imax); i < imax; i++) {
2343       Node* addp_use = addp->fast_out(i);
2344       if (addp_use->is_Store()) {
2345         for (DUIterator_Fast jmax, j = addp_use->fast_outs(jmax); j < jmax; j++) {
2346           if (addp_use->fast_out(j)->is_Initialize()) {
2347             return true;
2348           }
2349         }
2350       }
2351     }
2352   }
2353   return false;
2354 }
2355 
address_offset(Node * adr,PhaseTransform * phase)2356 int ConnectionGraph::address_offset(Node* adr, PhaseTransform *phase) {
2357   const Type *adr_type = phase->type(adr);
2358   if (adr->is_AddP() && adr_type->isa_oopptr() == NULL && is_captured_store_address(adr)) {
2359     // We are computing a raw address for a store captured by an Initialize
2360     // compute an appropriate address type. AddP cases #3 and #5 (see below).
2361     int offs = (int)phase->find_intptr_t_con(adr->in(AddPNode::Offset), Type::OffsetBot);
2362     assert(offs != Type::OffsetBot ||
2363            adr->in(AddPNode::Address)->in(0)->is_AllocateArray(),
2364            "offset must be a constant or it is initialization of array");
2365     return offs;
2366   }
2367   const TypePtr *t_ptr = adr_type->isa_ptr();
2368   assert(t_ptr != NULL, "must be a pointer type");
2369   return t_ptr->offset();
2370 }
2371 
get_addp_base(Node * addp)2372 Node* ConnectionGraph::get_addp_base(Node *addp) {
2373   assert(addp->is_AddP(), "must be AddP");
2374   //
2375   // AddP cases for Base and Address inputs:
2376   // case #1. Direct object's field reference:
2377   //     Allocate
2378   //       |
2379   //     Proj #5 ( oop result )
2380   //       |
2381   //     CheckCastPP (cast to instance type)
2382   //      | |
2383   //     AddP  ( base == address )
2384   //
2385   // case #2. Indirect object's field reference:
2386   //      Phi
2387   //       |
2388   //     CastPP (cast to instance type)
2389   //      | |
2390   //     AddP  ( base == address )
2391   //
2392   // case #3. Raw object's field reference for Initialize node:
2393   //      Allocate
2394   //        |
2395   //      Proj #5 ( oop result )
2396   //  top   |
2397   //     \  |
2398   //     AddP  ( base == top )
2399   //
2400   // case #4. Array's element reference:
2401   //   {CheckCastPP | CastPP}
2402   //     |  | |
2403   //     |  AddP ( array's element offset )
2404   //     |  |
2405   //     AddP ( array's offset )
2406   //
2407   // case #5. Raw object's field reference for arraycopy stub call:
2408   //          The inline_native_clone() case when the arraycopy stub is called
2409   //          after the allocation before Initialize and CheckCastPP nodes.
2410   //      Allocate
2411   //        |
2412   //      Proj #5 ( oop result )
2413   //       | |
2414   //       AddP  ( base == address )
2415   //
2416   // case #6. Constant Pool, ThreadLocal, CastX2P or
2417   //          Raw object's field reference:
2418   //      {ConP, ThreadLocal, CastX2P, raw Load}
2419   //  top   |
2420   //     \  |
2421   //     AddP  ( base == top )
2422   //
2423   // case #7. Klass's field reference.
2424   //      LoadKlass
2425   //       | |
2426   //       AddP  ( base == address )
2427   //
2428   // case #8. narrow Klass's field reference.
2429   //      LoadNKlass
2430   //       |
2431   //      DecodeN
2432   //       | |
2433   //       AddP  ( base == address )
2434   //
2435   // case #9. Mixed unsafe access
2436   //    {instance}
2437   //        |
2438   //      CheckCastPP (raw)
2439   //  top   |
2440   //     \  |
2441   //     AddP  ( base == top )
2442   //
2443   Node *base = addp->in(AddPNode::Base);
2444   if (base->uncast()->is_top()) { // The AddP case #3 and #6 and #9.
2445     base = addp->in(AddPNode::Address);
2446     while (base->is_AddP()) {
2447       // Case #6 (unsafe access) may have several chained AddP nodes.
2448       assert(base->in(AddPNode::Base)->uncast()->is_top(), "expected unsafe access address only");
2449       base = base->in(AddPNode::Address);
2450     }
2451     if (base->Opcode() == Op_CheckCastPP &&
2452         base->bottom_type()->isa_rawptr() &&
2453         _igvn->type(base->in(1))->isa_oopptr()) {
2454       base = base->in(1); // Case #9
2455     } else {
2456       Node* uncast_base = base->uncast();
2457       int opcode = uncast_base->Opcode();
2458       assert(opcode == Op_ConP || opcode == Op_ThreadLocal ||
2459              opcode == Op_CastX2P || uncast_base->is_DecodeNarrowPtr() ||
2460              (uncast_base->is_Mem() && (uncast_base->bottom_type()->isa_rawptr() != NULL)) ||
2461              is_captured_store_address(addp), "sanity");
2462     }
2463   }
2464   return base;
2465 }
2466 
find_second_addp(Node * addp,Node * n)2467 Node* ConnectionGraph::find_second_addp(Node* addp, Node* n) {
2468   assert(addp->is_AddP() && addp->outcnt() > 0, "Don't process dead nodes");
2469   Node* addp2 = addp->raw_out(0);
2470   if (addp->outcnt() == 1 && addp2->is_AddP() &&
2471       addp2->in(AddPNode::Base) == n &&
2472       addp2->in(AddPNode::Address) == addp) {
2473     assert(addp->in(AddPNode::Base) == n, "expecting the same base");
2474     //
2475     // Find array's offset to push it on worklist first and
2476     // as result process an array's element offset first (pushed second)
2477     // to avoid CastPP for the array's offset.
2478     // Otherwise the inserted CastPP (LocalVar) will point to what
2479     // the AddP (Field) points to. Which would be wrong since
2480     // the algorithm expects the CastPP has the same point as
2481     // as AddP's base CheckCastPP (LocalVar).
2482     //
2483     //    ArrayAllocation
2484     //     |
2485     //    CheckCastPP
2486     //     |
2487     //    memProj (from ArrayAllocation CheckCastPP)
2488     //     |  ||
2489     //     |  ||   Int (element index)
2490     //     |  ||    |   ConI (log(element size))
2491     //     |  ||    |   /
2492     //     |  ||   LShift
2493     //     |  ||  /
2494     //     |  AddP (array's element offset)
2495     //     |  |
2496     //     |  | ConI (array's offset: #12(32-bits) or #24(64-bits))
2497     //     | / /
2498     //     AddP (array's offset)
2499     //      |
2500     //     Load/Store (memory operation on array's element)
2501     //
2502     return addp2;
2503   }
2504   return NULL;
2505 }
2506 
2507 //
2508 // Adjust the type and inputs of an AddP which computes the
2509 // address of a field of an instance
2510 //
split_AddP(Node * addp,Node * base)2511 bool ConnectionGraph::split_AddP(Node *addp, Node *base) {
2512   PhaseGVN* igvn = _igvn;
2513   const TypeOopPtr *base_t = igvn->type(base)->isa_oopptr();
2514   assert(base_t != NULL && base_t->is_known_instance(), "expecting instance oopptr");
2515   const TypeOopPtr *t = igvn->type(addp)->isa_oopptr();
2516   if (t == NULL) {
2517     // We are computing a raw address for a store captured by an Initialize
2518     // compute an appropriate address type (cases #3 and #5).
2519     assert(igvn->type(addp) == TypeRawPtr::NOTNULL, "must be raw pointer");
2520     assert(addp->in(AddPNode::Address)->is_Proj(), "base of raw address must be result projection from allocation");
2521     intptr_t offs = (int)igvn->find_intptr_t_con(addp->in(AddPNode::Offset), Type::OffsetBot);
2522     assert(offs != Type::OffsetBot, "offset must be a constant");
2523     t = base_t->add_offset(offs)->is_oopptr();
2524   }
2525   int inst_id =  base_t->instance_id();
2526   assert(!t->is_known_instance() || t->instance_id() == inst_id,
2527                              "old type must be non-instance or match new type");
2528 
2529   // The type 't' could be subclass of 'base_t'.
2530   // As result t->offset() could be large then base_t's size and it will
2531   // cause the failure in add_offset() with narrow oops since TypeOopPtr()
2532   // constructor verifies correctness of the offset.
2533   //
2534   // It could happened on subclass's branch (from the type profiling
2535   // inlining) which was not eliminated during parsing since the exactness
2536   // of the allocation type was not propagated to the subclass type check.
2537   //
2538   // Or the type 't' could be not related to 'base_t' at all.
2539   // It could happened when CHA type is different from MDO type on a dead path
2540   // (for example, from instanceof check) which is not collapsed during parsing.
2541   //
2542   // Do nothing for such AddP node and don't process its users since
2543   // this code branch will go away.
2544   //
2545   if (!t->is_known_instance() &&
2546       !base_t->klass()->is_subtype_of(t->klass())) {
2547      return false; // bail out
2548   }
2549   const TypeOopPtr *tinst = base_t->add_offset(t->offset())->is_oopptr();
2550   // Do NOT remove the next line: ensure a new alias index is allocated
2551   // for the instance type. Note: C++ will not remove it since the call
2552   // has side effect.
2553   int alias_idx = _compile->get_alias_index(tinst);
2554   igvn->set_type(addp, tinst);
2555   // record the allocation in the node map
2556   set_map(addp, get_map(base->_idx));
2557   // Set addp's Base and Address to 'base'.
2558   Node *abase = addp->in(AddPNode::Base);
2559   Node *adr   = addp->in(AddPNode::Address);
2560   if (adr->is_Proj() && adr->in(0)->is_Allocate() &&
2561       adr->in(0)->_idx == (uint)inst_id) {
2562     // Skip AddP cases #3 and #5.
2563   } else {
2564     assert(!abase->is_top(), "sanity"); // AddP case #3
2565     if (abase != base) {
2566       igvn->hash_delete(addp);
2567       addp->set_req(AddPNode::Base, base);
2568       if (abase == adr) {
2569         addp->set_req(AddPNode::Address, base);
2570       } else {
2571         // AddP case #4 (adr is array's element offset AddP node)
2572 #ifdef ASSERT
2573         const TypeOopPtr *atype = igvn->type(adr)->isa_oopptr();
2574         assert(adr->is_AddP() && atype != NULL &&
2575                atype->instance_id() == inst_id, "array's element offset should be processed first");
2576 #endif
2577       }
2578       igvn->hash_insert(addp);
2579     }
2580   }
2581   // Put on IGVN worklist since at least addp's type was changed above.
2582   record_for_optimizer(addp);
2583   return true;
2584 }
2585 
2586 //
2587 // Create a new version of orig_phi if necessary. Returns either the newly
2588 // created phi or an existing phi.  Sets create_new to indicate whether a new
2589 // phi was created.  Cache the last newly created phi in the node map.
2590 //
create_split_phi(PhiNode * orig_phi,int alias_idx,GrowableArray<PhiNode * > & orig_phi_worklist,bool & new_created)2591 PhiNode *ConnectionGraph::create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, bool &new_created) {
2592   Compile *C = _compile;
2593   PhaseGVN* igvn = _igvn;
2594   new_created = false;
2595   int phi_alias_idx = C->get_alias_index(orig_phi->adr_type());
2596   // nothing to do if orig_phi is bottom memory or matches alias_idx
2597   if (phi_alias_idx == alias_idx) {
2598     return orig_phi;
2599   }
2600   // Have we recently created a Phi for this alias index?
2601   PhiNode *result = get_map_phi(orig_phi->_idx);
2602   if (result != NULL && C->get_alias_index(result->adr_type()) == alias_idx) {
2603     return result;
2604   }
2605   // Previous check may fail when the same wide memory Phi was split into Phis
2606   // for different memory slices. Search all Phis for this region.
2607   if (result != NULL) {
2608     Node* region = orig_phi->in(0);
2609     for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) {
2610       Node* phi = region->fast_out(i);
2611       if (phi->is_Phi() &&
2612           C->get_alias_index(phi->as_Phi()->adr_type()) == alias_idx) {
2613         assert(phi->_idx >= nodes_size(), "only new Phi per instance memory slice");
2614         return phi->as_Phi();
2615       }
2616     }
2617   }
2618   if (C->live_nodes() + 2*NodeLimitFudgeFactor > C->max_node_limit()) {
2619     if (C->do_escape_analysis() == true && !C->failing()) {
2620       // Retry compilation without escape analysis.
2621       // If this is the first failure, the sentinel string will "stick"
2622       // to the Compile object, and the C2Compiler will see it and retry.
2623       C->record_failure(C2Compiler::retry_no_escape_analysis());
2624     }
2625     return NULL;
2626   }
2627   orig_phi_worklist.append_if_missing(orig_phi);
2628   const TypePtr *atype = C->get_adr_type(alias_idx);
2629   result = PhiNode::make(orig_phi->in(0), NULL, Type::MEMORY, atype);
2630   C->copy_node_notes_to(result, orig_phi);
2631   igvn->set_type(result, result->bottom_type());
2632   record_for_optimizer(result);
2633   set_map(orig_phi, result);
2634   new_created = true;
2635   return result;
2636 }
2637 
2638 //
2639 // Return a new version of Memory Phi "orig_phi" with the inputs having the
2640 // specified alias index.
2641 //
split_memory_phi(PhiNode * orig_phi,int alias_idx,GrowableArray<PhiNode * > & orig_phi_worklist)2642 PhiNode *ConnectionGraph::split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist) {
2643   assert(alias_idx != Compile::AliasIdxBot, "can't split out bottom memory");
2644   Compile *C = _compile;
2645   PhaseGVN* igvn = _igvn;
2646   bool new_phi_created;
2647   PhiNode *result = create_split_phi(orig_phi, alias_idx, orig_phi_worklist, new_phi_created);
2648   if (!new_phi_created) {
2649     return result;
2650   }
2651   GrowableArray<PhiNode *>  phi_list;
2652   GrowableArray<uint>  cur_input;
2653   PhiNode *phi = orig_phi;
2654   uint idx = 1;
2655   bool finished = false;
2656   while(!finished) {
2657     while (idx < phi->req()) {
2658       Node *mem = find_inst_mem(phi->in(idx), alias_idx, orig_phi_worklist);
2659       if (mem != NULL && mem->is_Phi()) {
2660         PhiNode *newphi = create_split_phi(mem->as_Phi(), alias_idx, orig_phi_worklist, new_phi_created);
2661         if (new_phi_created) {
2662           // found an phi for which we created a new split, push current one on worklist and begin
2663           // processing new one
2664           phi_list.push(phi);
2665           cur_input.push(idx);
2666           phi = mem->as_Phi();
2667           result = newphi;
2668           idx = 1;
2669           continue;
2670         } else {
2671           mem = newphi;
2672         }
2673       }
2674       if (C->failing()) {
2675         return NULL;
2676       }
2677       result->set_req(idx++, mem);
2678     }
2679 #ifdef ASSERT
2680     // verify that the new Phi has an input for each input of the original
2681     assert( phi->req() == result->req(), "must have same number of inputs.");
2682     assert( result->in(0) != NULL && result->in(0) == phi->in(0), "regions must match");
2683 #endif
2684     // Check if all new phi's inputs have specified alias index.
2685     // Otherwise use old phi.
2686     for (uint i = 1; i < phi->req(); i++) {
2687       Node* in = result->in(i);
2688       assert((phi->in(i) == NULL) == (in == NULL), "inputs must correspond.");
2689     }
2690     // we have finished processing a Phi, see if there are any more to do
2691     finished = (phi_list.length() == 0 );
2692     if (!finished) {
2693       phi = phi_list.pop();
2694       idx = cur_input.pop();
2695       PhiNode *prev_result = get_map_phi(phi->_idx);
2696       prev_result->set_req(idx++, result);
2697       result = prev_result;
2698     }
2699   }
2700   return result;
2701 }
2702 
2703 //
2704 // The next methods are derived from methods in MemNode.
2705 //
step_through_mergemem(MergeMemNode * mmem,int alias_idx,const TypeOopPtr * toop)2706 Node* ConnectionGraph::step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop) {
2707   Node *mem = mmem;
2708   // TypeOopPtr::NOTNULL+any is an OOP with unknown offset - generally
2709   // means an array I have not precisely typed yet.  Do not do any
2710   // alias stuff with it any time soon.
2711   if (toop->base() != Type::AnyPtr &&
2712       !(toop->klass() != NULL &&
2713         toop->klass()->is_java_lang_Object() &&
2714         toop->offset() == Type::OffsetBot)) {
2715     mem = mmem->memory_at(alias_idx);
2716     // Update input if it is progress over what we have now
2717   }
2718   return mem;
2719 }
2720 
2721 //
2722 // Move memory users to their memory slices.
2723 //
move_inst_mem(Node * n,GrowableArray<PhiNode * > & orig_phis)2724 void ConnectionGraph::move_inst_mem(Node* n, GrowableArray<PhiNode *>  &orig_phis) {
2725   Compile* C = _compile;
2726   PhaseGVN* igvn = _igvn;
2727   const TypePtr* tp = igvn->type(n->in(MemNode::Address))->isa_ptr();
2728   assert(tp != NULL, "ptr type");
2729   int alias_idx = C->get_alias_index(tp);
2730   int general_idx = C->get_general_index(alias_idx);
2731 
2732   // Move users first
2733   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
2734     Node* use = n->fast_out(i);
2735     if (use->is_MergeMem()) {
2736       MergeMemNode* mmem = use->as_MergeMem();
2737       assert(n == mmem->memory_at(alias_idx), "should be on instance memory slice");
2738       if (n != mmem->memory_at(general_idx) || alias_idx == general_idx) {
2739         continue; // Nothing to do
2740       }
2741       // Replace previous general reference to mem node.
2742       uint orig_uniq = C->unique();
2743       Node* m = find_inst_mem(n, general_idx, orig_phis);
2744       assert(orig_uniq == C->unique(), "no new nodes");
2745       mmem->set_memory_at(general_idx, m);
2746       --imax;
2747       --i;
2748     } else if (use->is_MemBar()) {
2749       assert(!use->is_Initialize(), "initializing stores should not be moved");
2750       if (use->req() > MemBarNode::Precedent &&
2751           use->in(MemBarNode::Precedent) == n) {
2752         // Don't move related membars.
2753         record_for_optimizer(use);
2754         continue;
2755       }
2756       tp = use->as_MemBar()->adr_type()->isa_ptr();
2757       if ((tp != NULL && C->get_alias_index(tp) == alias_idx) ||
2758           alias_idx == general_idx) {
2759         continue; // Nothing to do
2760       }
2761       // Move to general memory slice.
2762       uint orig_uniq = C->unique();
2763       Node* m = find_inst_mem(n, general_idx, orig_phis);
2764       assert(orig_uniq == C->unique(), "no new nodes");
2765       igvn->hash_delete(use);
2766       imax -= use->replace_edge(n, m);
2767       igvn->hash_insert(use);
2768       record_for_optimizer(use);
2769       --i;
2770 #ifdef ASSERT
2771     } else if (use->is_Mem()) {
2772       if (use->Opcode() == Op_StoreCM && use->in(MemNode::OopStore) == n) {
2773         // Don't move related cardmark.
2774         continue;
2775       }
2776       // Memory nodes should have new memory input.
2777       tp = igvn->type(use->in(MemNode::Address))->isa_ptr();
2778       assert(tp != NULL, "ptr type");
2779       int idx = C->get_alias_index(tp);
2780       assert(get_map(use->_idx) != NULL || idx == alias_idx,
2781              "Following memory nodes should have new memory input or be on the same memory slice");
2782     } else if (use->is_Phi()) {
2783       // Phi nodes should be split and moved already.
2784       tp = use->as_Phi()->adr_type()->isa_ptr();
2785       assert(tp != NULL, "ptr type");
2786       int idx = C->get_alias_index(tp);
2787       assert(idx == alias_idx, "Following Phi nodes should be on the same memory slice");
2788     } else {
2789       use->dump();
2790       assert(false, "should not be here");
2791 #endif
2792     }
2793   }
2794 }
2795 
2796 //
2797 // Search memory chain of "mem" to find a MemNode whose address
2798 // is the specified alias index.
2799 //
find_inst_mem(Node * orig_mem,int alias_idx,GrowableArray<PhiNode * > & orig_phis)2800 Node* ConnectionGraph::find_inst_mem(Node *orig_mem, int alias_idx, GrowableArray<PhiNode *>  &orig_phis) {
2801   if (orig_mem == NULL)
2802     return orig_mem;
2803   Compile* C = _compile;
2804   PhaseGVN* igvn = _igvn;
2805   const TypeOopPtr *toop = C->get_adr_type(alias_idx)->isa_oopptr();
2806   bool is_instance = (toop != NULL) && toop->is_known_instance();
2807   Node *start_mem = C->start()->proj_out_or_null(TypeFunc::Memory);
2808   Node *prev = NULL;
2809   Node *result = orig_mem;
2810   while (prev != result) {
2811     prev = result;
2812     if (result == start_mem)
2813       break;  // hit one of our sentinels
2814     if (result->is_Mem()) {
2815       const Type *at = igvn->type(result->in(MemNode::Address));
2816       if (at == Type::TOP)
2817         break; // Dead
2818       assert (at->isa_ptr() != NULL, "pointer type required.");
2819       int idx = C->get_alias_index(at->is_ptr());
2820       if (idx == alias_idx)
2821         break; // Found
2822       if (!is_instance && (at->isa_oopptr() == NULL ||
2823                            !at->is_oopptr()->is_known_instance())) {
2824         break; // Do not skip store to general memory slice.
2825       }
2826       result = result->in(MemNode::Memory);
2827     }
2828     if (!is_instance)
2829       continue;  // don't search further for non-instance types
2830     // skip over a call which does not affect this memory slice
2831     if (result->is_Proj() && result->as_Proj()->_con == TypeFunc::Memory) {
2832       Node *proj_in = result->in(0);
2833       if (proj_in->is_Allocate() && proj_in->_idx == (uint)toop->instance_id()) {
2834         break;  // hit one of our sentinels
2835       } else if (proj_in->is_Call()) {
2836         // ArrayCopy node processed here as well
2837         CallNode *call = proj_in->as_Call();
2838         if (!call->may_modify(toop, igvn)) {
2839           result = call->in(TypeFunc::Memory);
2840         }
2841       } else if (proj_in->is_Initialize()) {
2842         AllocateNode* alloc = proj_in->as_Initialize()->allocation();
2843         // Stop if this is the initialization for the object instance which
2844         // which contains this memory slice, otherwise skip over it.
2845         if (alloc == NULL || alloc->_idx != (uint)toop->instance_id()) {
2846           result = proj_in->in(TypeFunc::Memory);
2847         }
2848       } else if (proj_in->is_MemBar()) {
2849         // Check if there is an array copy for a clone
2850         // Step over GC barrier when ReduceInitialCardMarks is disabled
2851         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
2852         Node* control_proj_ac = bs->step_over_gc_barrier(proj_in->in(0));
2853 
2854         if (control_proj_ac->is_Proj() && control_proj_ac->in(0)->is_ArrayCopy()) {
2855           // Stop if it is a clone
2856           ArrayCopyNode* ac = control_proj_ac->in(0)->as_ArrayCopy();
2857           if (ac->may_modify(toop, igvn)) {
2858             break;
2859           }
2860         }
2861         result = proj_in->in(TypeFunc::Memory);
2862       }
2863     } else if (result->is_MergeMem()) {
2864       MergeMemNode *mmem = result->as_MergeMem();
2865       result = step_through_mergemem(mmem, alias_idx, toop);
2866       if (result == mmem->base_memory()) {
2867         // Didn't find instance memory, search through general slice recursively.
2868         result = mmem->memory_at(C->get_general_index(alias_idx));
2869         result = find_inst_mem(result, alias_idx, orig_phis);
2870         if (C->failing()) {
2871           return NULL;
2872         }
2873         mmem->set_memory_at(alias_idx, result);
2874       }
2875     } else if (result->is_Phi() &&
2876                C->get_alias_index(result->as_Phi()->adr_type()) != alias_idx) {
2877       Node *un = result->as_Phi()->unique_input(igvn);
2878       if (un != NULL) {
2879         orig_phis.append_if_missing(result->as_Phi());
2880         result = un;
2881       } else {
2882         break;
2883       }
2884     } else if (result->is_ClearArray()) {
2885       if (!ClearArrayNode::step_through(&result, (uint)toop->instance_id(), igvn)) {
2886         // Can not bypass initialization of the instance
2887         // we are looking for.
2888         break;
2889       }
2890       // Otherwise skip it (the call updated 'result' value).
2891     } else if (result->Opcode() == Op_SCMemProj) {
2892       Node* mem = result->in(0);
2893       Node* adr = NULL;
2894       if (mem->is_LoadStore()) {
2895         adr = mem->in(MemNode::Address);
2896       } else {
2897         assert(mem->Opcode() == Op_EncodeISOArray ||
2898                mem->Opcode() == Op_StrCompressedCopy, "sanity");
2899         adr = mem->in(3); // Memory edge corresponds to destination array
2900       }
2901       const Type *at = igvn->type(adr);
2902       if (at != Type::TOP) {
2903         assert(at->isa_ptr() != NULL, "pointer type required.");
2904         int idx = C->get_alias_index(at->is_ptr());
2905         if (idx == alias_idx) {
2906           // Assert in debug mode
2907           assert(false, "Object is not scalar replaceable if a LoadStore node accesses its field");
2908           break; // In product mode return SCMemProj node
2909         }
2910       }
2911       result = mem->in(MemNode::Memory);
2912     } else if (result->Opcode() == Op_StrInflatedCopy) {
2913       Node* adr = result->in(3); // Memory edge corresponds to destination array
2914       const Type *at = igvn->type(adr);
2915       if (at != Type::TOP) {
2916         assert(at->isa_ptr() != NULL, "pointer type required.");
2917         int idx = C->get_alias_index(at->is_ptr());
2918         if (idx == alias_idx) {
2919           // Assert in debug mode
2920           assert(false, "Object is not scalar replaceable if a StrInflatedCopy node accesses its field");
2921           break; // In product mode return SCMemProj node
2922         }
2923       }
2924       result = result->in(MemNode::Memory);
2925     }
2926   }
2927   if (result->is_Phi()) {
2928     PhiNode *mphi = result->as_Phi();
2929     assert(mphi->bottom_type() == Type::MEMORY, "memory phi required");
2930     const TypePtr *t = mphi->adr_type();
2931     if (!is_instance) {
2932       // Push all non-instance Phis on the orig_phis worklist to update inputs
2933       // during Phase 4 if needed.
2934       orig_phis.append_if_missing(mphi);
2935     } else if (C->get_alias_index(t) != alias_idx) {
2936       // Create a new Phi with the specified alias index type.
2937       result = split_memory_phi(mphi, alias_idx, orig_phis);
2938     }
2939   }
2940   // the result is either MemNode, PhiNode, InitializeNode.
2941   return result;
2942 }
2943 
2944 //
2945 //  Convert the types of unescaped object to instance types where possible,
2946 //  propagate the new type information through the graph, and update memory
2947 //  edges and MergeMem inputs to reflect the new type.
2948 //
2949 //  We start with allocations (and calls which may be allocations)  on alloc_worklist.
2950 //  The processing is done in 4 phases:
2951 //
2952 //  Phase 1:  Process possible allocations from alloc_worklist.  Create instance
2953 //            types for the CheckCastPP for allocations where possible.
2954 //            Propagate the new types through users as follows:
2955 //               casts and Phi:  push users on alloc_worklist
2956 //               AddP:  cast Base and Address inputs to the instance type
2957 //                      push any AddP users on alloc_worklist and push any memnode
2958 //                      users onto memnode_worklist.
2959 //  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
2960 //            search the Memory chain for a store with the appropriate type
2961 //            address type.  If a Phi is found, create a new version with
2962 //            the appropriate memory slices from each of the Phi inputs.
2963 //            For stores, process the users as follows:
2964 //               MemNode:  push on memnode_worklist
2965 //               MergeMem: push on mergemem_worklist
2966 //  Phase 3:  Process MergeMem nodes from mergemem_worklist.  Walk each memory slice
2967 //            moving the first node encountered of each  instance type to the
2968 //            the input corresponding to its alias index.
2969 //            appropriate memory slice.
2970 //  Phase 4:  Update the inputs of non-instance memory Phis and the Memory input of memnodes.
2971 //
2972 // In the following example, the CheckCastPP nodes are the cast of allocation
2973 // results and the allocation of node 29 is unescaped and eligible to be an
2974 // instance type.
2975 //
2976 // We start with:
2977 //
2978 //     7 Parm #memory
2979 //    10  ConI  "12"
2980 //    19  CheckCastPP   "Foo"
2981 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
2982 //    29  CheckCastPP   "Foo"
2983 //    30  AddP  _ 29 29 10  Foo+12  alias_index=4
2984 //
2985 //    40  StoreP  25   7  20   ... alias_index=4
2986 //    50  StoreP  35  40  30   ... alias_index=4
2987 //    60  StoreP  45  50  20   ... alias_index=4
2988 //    70  LoadP    _  60  30   ... alias_index=4
2989 //    80  Phi     75  50  60   Memory alias_index=4
2990 //    90  LoadP    _  80  30   ... alias_index=4
2991 //   100  LoadP    _  80  20   ... alias_index=4
2992 //
2993 //
2994 // Phase 1 creates an instance type for node 29 assigning it an instance id of 24
2995 // and creating a new alias index for node 30.  This gives:
2996 //
2997 //     7 Parm #memory
2998 //    10  ConI  "12"
2999 //    19  CheckCastPP   "Foo"
3000 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
3001 //    29  CheckCastPP   "Foo"  iid=24
3002 //    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
3003 //
3004 //    40  StoreP  25   7  20   ... alias_index=4
3005 //    50  StoreP  35  40  30   ... alias_index=6
3006 //    60  StoreP  45  50  20   ... alias_index=4
3007 //    70  LoadP    _  60  30   ... alias_index=6
3008 //    80  Phi     75  50  60   Memory alias_index=4
3009 //    90  LoadP    _  80  30   ... alias_index=6
3010 //   100  LoadP    _  80  20   ... alias_index=4
3011 //
3012 // In phase 2, new memory inputs are computed for the loads and stores,
3013 // And a new version of the phi is created.  In phase 4, the inputs to
3014 // node 80 are updated and then the memory nodes are updated with the
3015 // values computed in phase 2.  This results in:
3016 //
3017 //     7 Parm #memory
3018 //    10  ConI  "12"
3019 //    19  CheckCastPP   "Foo"
3020 //    20  AddP  _ 19 19 10  Foo+12  alias_index=4
3021 //    29  CheckCastPP   "Foo"  iid=24
3022 //    30  AddP  _ 29 29 10  Foo+12  alias_index=6  iid=24
3023 //
3024 //    40  StoreP  25  7   20   ... alias_index=4
3025 //    50  StoreP  35  7   30   ... alias_index=6
3026 //    60  StoreP  45  40  20   ... alias_index=4
3027 //    70  LoadP    _  50  30   ... alias_index=6
3028 //    80  Phi     75  40  60   Memory alias_index=4
3029 //   120  Phi     75  50  50   Memory alias_index=6
3030 //    90  LoadP    _ 120  30   ... alias_index=6
3031 //   100  LoadP    _  80  20   ... alias_index=4
3032 //
split_unique_types(GrowableArray<Node * > & alloc_worklist,GrowableArray<ArrayCopyNode * > & arraycopy_worklist)3033 void ConnectionGraph::split_unique_types(GrowableArray<Node *>  &alloc_worklist, GrowableArray<ArrayCopyNode*> &arraycopy_worklist) {
3034   GrowableArray<Node *>  memnode_worklist;
3035   GrowableArray<PhiNode *>  orig_phis;
3036   PhaseIterGVN  *igvn = _igvn;
3037   uint new_index_start = (uint) _compile->num_alias_types();
3038   VectorSet visited;
3039   ideal_nodes.clear(); // Reset for use with set_map/get_map.
3040   uint unique_old = _compile->unique();
3041 
3042   //  Phase 1:  Process possible allocations from alloc_worklist.
3043   //  Create instance types for the CheckCastPP for allocations where possible.
3044   //
3045   // (Note: don't forget to change the order of the second AddP node on
3046   //  the alloc_worklist if the order of the worklist processing is changed,
3047   //  see the comment in find_second_addp().)
3048   //
3049   while (alloc_worklist.length() != 0) {
3050     Node *n = alloc_worklist.pop();
3051     uint ni = n->_idx;
3052     if (n->is_Call()) {
3053       CallNode *alloc = n->as_Call();
3054       // copy escape information to call node
3055       PointsToNode* ptn = ptnode_adr(alloc->_idx);
3056       PointsToNode::EscapeState es = ptn->escape_state();
3057       // We have an allocation or call which returns a Java object,
3058       // see if it is unescaped.
3059       if (es != PointsToNode::NoEscape || !ptn->scalar_replaceable())
3060         continue;
3061       // Find CheckCastPP for the allocate or for the return value of a call
3062       n = alloc->result_cast();
3063       if (n == NULL) {            // No uses except Initialize node
3064         if (alloc->is_Allocate()) {
3065           // Set the scalar_replaceable flag for allocation
3066           // so it could be eliminated if it has no uses.
3067           alloc->as_Allocate()->_is_scalar_replaceable = true;
3068         }
3069         continue;
3070       }
3071       if (!n->is_CheckCastPP()) { // not unique CheckCastPP.
3072         // we could reach here for allocate case if one init is associated with many allocs.
3073         if (alloc->is_Allocate()) {
3074           alloc->as_Allocate()->_is_scalar_replaceable = false;
3075         }
3076         continue;
3077       }
3078 
3079       // The inline code for Object.clone() casts the allocation result to
3080       // java.lang.Object and then to the actual type of the allocated
3081       // object. Detect this case and use the second cast.
3082       // Also detect j.l.reflect.Array.newInstance(jobject, jint) case when
3083       // the allocation result is cast to java.lang.Object and then
3084       // to the actual Array type.
3085       if (alloc->is_Allocate() && n->as_Type()->type() == TypeInstPtr::NOTNULL
3086           && (alloc->is_AllocateArray() ||
3087               igvn->type(alloc->in(AllocateNode::KlassNode)) != TypeKlassPtr::OBJECT)) {
3088         Node *cast2 = NULL;
3089         for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3090           Node *use = n->fast_out(i);
3091           if (use->is_CheckCastPP()) {
3092             cast2 = use;
3093             break;
3094           }
3095         }
3096         if (cast2 != NULL) {
3097           n = cast2;
3098         } else {
3099           // Non-scalar replaceable if the allocation type is unknown statically
3100           // (reflection allocation), the object can't be restored during
3101           // deoptimization without precise type.
3102           continue;
3103         }
3104       }
3105 
3106       const TypeOopPtr *t = igvn->type(n)->isa_oopptr();
3107       if (t == NULL)
3108         continue;  // not a TypeOopPtr
3109       if (!t->klass_is_exact())
3110         continue; // not an unique type
3111 
3112       if (alloc->is_Allocate()) {
3113         // Set the scalar_replaceable flag for allocation
3114         // so it could be eliminated.
3115         alloc->as_Allocate()->_is_scalar_replaceable = true;
3116       }
3117       set_escape_state(ptnode_adr(n->_idx), es); // CheckCastPP escape state
3118       // in order for an object to be scalar-replaceable, it must be:
3119       //   - a direct allocation (not a call returning an object)
3120       //   - non-escaping
3121       //   - eligible to be a unique type
3122       //   - not determined to be ineligible by escape analysis
3123       set_map(alloc, n);
3124       set_map(n, alloc);
3125       const TypeOopPtr* tinst = t->cast_to_instance_id(ni);
3126       igvn->hash_delete(n);
3127       igvn->set_type(n,  tinst);
3128       n->raise_bottom_type(tinst);
3129       igvn->hash_insert(n);
3130       record_for_optimizer(n);
3131       // Allocate an alias index for the header fields. Accesses to
3132       // the header emitted during macro expansion wouldn't have
3133       // correct memory state otherwise.
3134       _compile->get_alias_index(tinst->add_offset(oopDesc::mark_offset_in_bytes()));
3135       _compile->get_alias_index(tinst->add_offset(oopDesc::klass_offset_in_bytes()));
3136       if (alloc->is_Allocate() && (t->isa_instptr() || t->isa_aryptr())) {
3137 
3138         // First, put on the worklist all Field edges from Connection Graph
3139         // which is more accurate than putting immediate users from Ideal Graph.
3140         for (EdgeIterator e(ptn); e.has_next(); e.next()) {
3141           PointsToNode* tgt = e.get();
3142           if (tgt->is_Arraycopy()) {
3143             continue;
3144           }
3145           Node* use = tgt->ideal_node();
3146           assert(tgt->is_Field() && use->is_AddP(),
3147                  "only AddP nodes are Field edges in CG");
3148           if (use->outcnt() > 0) { // Don't process dead nodes
3149             Node* addp2 = find_second_addp(use, use->in(AddPNode::Base));
3150             if (addp2 != NULL) {
3151               assert(alloc->is_AllocateArray(),"array allocation was expected");
3152               alloc_worklist.append_if_missing(addp2);
3153             }
3154             alloc_worklist.append_if_missing(use);
3155           }
3156         }
3157 
3158         // An allocation may have an Initialize which has raw stores. Scan
3159         // the users of the raw allocation result and push AddP users
3160         // on alloc_worklist.
3161         Node *raw_result = alloc->proj_out_or_null(TypeFunc::Parms);
3162         assert (raw_result != NULL, "must have an allocation result");
3163         for (DUIterator_Fast imax, i = raw_result->fast_outs(imax); i < imax; i++) {
3164           Node *use = raw_result->fast_out(i);
3165           if (use->is_AddP() && use->outcnt() > 0) { // Don't process dead nodes
3166             Node* addp2 = find_second_addp(use, raw_result);
3167             if (addp2 != NULL) {
3168               assert(alloc->is_AllocateArray(),"array allocation was expected");
3169               alloc_worklist.append_if_missing(addp2);
3170             }
3171             alloc_worklist.append_if_missing(use);
3172           } else if (use->is_MemBar()) {
3173             memnode_worklist.append_if_missing(use);
3174           }
3175         }
3176       }
3177     } else if (n->is_AddP()) {
3178       JavaObjectNode* jobj = unique_java_object(get_addp_base(n));
3179       if (jobj == NULL || jobj == phantom_obj) {
3180 #ifdef ASSERT
3181         ptnode_adr(get_addp_base(n)->_idx)->dump();
3182         ptnode_adr(n->_idx)->dump();
3183         assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3184 #endif
3185         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3186         return;
3187       }
3188       Node *base = get_map(jobj->idx());  // CheckCastPP node
3189       if (!split_AddP(n, base)) continue; // wrong type from dead path
3190     } else if (n->is_Phi() ||
3191                n->is_CheckCastPP() ||
3192                n->is_EncodeP() ||
3193                n->is_DecodeN() ||
3194                (n->is_ConstraintCast() && n->Opcode() == Op_CastPP)) {
3195       if (visited.test_set(n->_idx)) {
3196         assert(n->is_Phi(), "loops only through Phi's");
3197         continue;  // already processed
3198       }
3199       JavaObjectNode* jobj = unique_java_object(n);
3200       if (jobj == NULL || jobj == phantom_obj) {
3201 #ifdef ASSERT
3202         ptnode_adr(n->_idx)->dump();
3203         assert(jobj != NULL && jobj != phantom_obj, "escaped allocation");
3204 #endif
3205         _compile->record_failure(C2Compiler::retry_no_escape_analysis());
3206         return;
3207       } else {
3208         Node *val = get_map(jobj->idx());   // CheckCastPP node
3209         TypeNode *tn = n->as_Type();
3210         const TypeOopPtr* tinst = igvn->type(val)->isa_oopptr();
3211         assert(tinst != NULL && tinst->is_known_instance() &&
3212                tinst->instance_id() == jobj->idx() , "instance type expected.");
3213 
3214         const Type *tn_type = igvn->type(tn);
3215         const TypeOopPtr *tn_t;
3216         if (tn_type->isa_narrowoop()) {
3217           tn_t = tn_type->make_ptr()->isa_oopptr();
3218         } else {
3219           tn_t = tn_type->isa_oopptr();
3220         }
3221         if (tn_t != NULL && tinst->klass()->is_subtype_of(tn_t->klass())) {
3222           if (tn_type->isa_narrowoop()) {
3223             tn_type = tinst->make_narrowoop();
3224           } else {
3225             tn_type = tinst;
3226           }
3227           igvn->hash_delete(tn);
3228           igvn->set_type(tn, tn_type);
3229           tn->set_type(tn_type);
3230           igvn->hash_insert(tn);
3231           record_for_optimizer(n);
3232         } else {
3233           assert(tn_type == TypePtr::NULL_PTR ||
3234                  tn_t != NULL && !tinst->klass()->is_subtype_of(tn_t->klass()),
3235                  "unexpected type");
3236           continue; // Skip dead path with different type
3237         }
3238       }
3239     } else {
3240       debug_only(n->dump();)
3241       assert(false, "EA: unexpected node");
3242       continue;
3243     }
3244     // push allocation's users on appropriate worklist
3245     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3246       Node *use = n->fast_out(i);
3247       if(use->is_Mem() && use->in(MemNode::Address) == n) {
3248         // Load/store to instance's field
3249         memnode_worklist.append_if_missing(use);
3250       } else if (use->is_MemBar()) {
3251         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3252           memnode_worklist.append_if_missing(use);
3253         }
3254       } else if (use->is_AddP() && use->outcnt() > 0) { // No dead nodes
3255         Node* addp2 = find_second_addp(use, n);
3256         if (addp2 != NULL) {
3257           alloc_worklist.append_if_missing(addp2);
3258         }
3259         alloc_worklist.append_if_missing(use);
3260       } else if (use->is_Phi() ||
3261                  use->is_CheckCastPP() ||
3262                  use->is_EncodeNarrowPtr() ||
3263                  use->is_DecodeNarrowPtr() ||
3264                  (use->is_ConstraintCast() && use->Opcode() == Op_CastPP)) {
3265         alloc_worklist.append_if_missing(use);
3266 #ifdef ASSERT
3267       } else if (use->is_Mem()) {
3268         assert(use->in(MemNode::Address) != n, "EA: missing allocation reference path");
3269       } else if (use->is_MergeMem()) {
3270         assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3271       } else if (use->is_SafePoint()) {
3272         // Look for MergeMem nodes for calls which reference unique allocation
3273         // (through CheckCastPP nodes) even for debug info.
3274         Node* m = use->in(TypeFunc::Memory);
3275         if (m->is_MergeMem()) {
3276           assert(_mergemem_worklist.contains(m->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3277         }
3278       } else if (use->Opcode() == Op_EncodeISOArray) {
3279         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3280           // EncodeISOArray overwrites destination array
3281           memnode_worklist.append_if_missing(use);
3282         }
3283       } else {
3284         uint op = use->Opcode();
3285         if ((op == Op_StrCompressedCopy || op == Op_StrInflatedCopy) &&
3286             (use->in(MemNode::Memory) == n)) {
3287           // They overwrite memory edge corresponding to destination array,
3288           memnode_worklist.append_if_missing(use);
3289         } else if (!(op == Op_CmpP || op == Op_Conv2B ||
3290               op == Op_CastP2X || op == Op_StoreCM ||
3291               op == Op_FastLock || op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3292               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3293               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar ||
3294               op == Op_SubTypeCheck ||
3295               BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use))) {
3296           n->dump();
3297           use->dump();
3298           assert(false, "EA: missing allocation reference path");
3299         }
3300 #endif
3301       }
3302     }
3303 
3304   }
3305 
3306   // Go over all ArrayCopy nodes and if one of the inputs has a unique
3307   // type, record it in the ArrayCopy node so we know what memory this
3308   // node uses/modified.
3309   for (int next = 0; next < arraycopy_worklist.length(); next++) {
3310     ArrayCopyNode* ac = arraycopy_worklist.at(next);
3311     Node* dest = ac->in(ArrayCopyNode::Dest);
3312     if (dest->is_AddP()) {
3313       dest = get_addp_base(dest);
3314     }
3315     JavaObjectNode* jobj = unique_java_object(dest);
3316     if (jobj != NULL) {
3317       Node *base = get_map(jobj->idx());
3318       if (base != NULL) {
3319         const TypeOopPtr *base_t = _igvn->type(base)->isa_oopptr();
3320         ac->_dest_type = base_t;
3321       }
3322     }
3323     Node* src = ac->in(ArrayCopyNode::Src);
3324     if (src->is_AddP()) {
3325       src = get_addp_base(src);
3326     }
3327     jobj = unique_java_object(src);
3328     if (jobj != NULL) {
3329       Node* base = get_map(jobj->idx());
3330       if (base != NULL) {
3331         const TypeOopPtr *base_t = _igvn->type(base)->isa_oopptr();
3332         ac->_src_type = base_t;
3333       }
3334     }
3335   }
3336 
3337   // New alias types were created in split_AddP().
3338   uint new_index_end = (uint) _compile->num_alias_types();
3339   assert(unique_old == _compile->unique(), "there should be no new ideal nodes after Phase 1");
3340 
3341   //  Phase 2:  Process MemNode's from memnode_worklist. compute new address type and
3342   //            compute new values for Memory inputs  (the Memory inputs are not
3343   //            actually updated until phase 4.)
3344   if (memnode_worklist.length() == 0)
3345     return;  // nothing to do
3346   while (memnode_worklist.length() != 0) {
3347     Node *n = memnode_worklist.pop();
3348     if (visited.test_set(n->_idx))
3349       continue;
3350     if (n->is_Phi() || n->is_ClearArray()) {
3351       // we don't need to do anything, but the users must be pushed
3352     } else if (n->is_MemBar()) { // Initialize, MemBar nodes
3353       // we don't need to do anything, but the users must be pushed
3354       n = n->as_MemBar()->proj_out_or_null(TypeFunc::Memory);
3355       if (n == NULL)
3356         continue;
3357     } else if (n->Opcode() == Op_StrCompressedCopy ||
3358                n->Opcode() == Op_EncodeISOArray) {
3359       // get the memory projection
3360       n = n->find_out_with(Op_SCMemProj);
3361       assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3362     } else {
3363       assert(n->is_Mem(), "memory node required.");
3364       Node *addr = n->in(MemNode::Address);
3365       const Type *addr_t = igvn->type(addr);
3366       if (addr_t == Type::TOP)
3367         continue;
3368       assert (addr_t->isa_ptr() != NULL, "pointer type required.");
3369       int alias_idx = _compile->get_alias_index(addr_t->is_ptr());
3370       assert ((uint)alias_idx < new_index_end, "wrong alias index");
3371       Node *mem = find_inst_mem(n->in(MemNode::Memory), alias_idx, orig_phis);
3372       if (_compile->failing()) {
3373         return;
3374       }
3375       if (mem != n->in(MemNode::Memory)) {
3376         // We delay the memory edge update since we need old one in
3377         // MergeMem code below when instances memory slices are separated.
3378         set_map(n, mem);
3379       }
3380       if (n->is_Load()) {
3381         continue;  // don't push users
3382       } else if (n->is_LoadStore()) {
3383         // get the memory projection
3384         n = n->find_out_with(Op_SCMemProj);
3385         assert(n != NULL && n->Opcode() == Op_SCMemProj, "memory projection required");
3386       }
3387     }
3388     // push user on appropriate worklist
3389     for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
3390       Node *use = n->fast_out(i);
3391       if (use->is_Phi() || use->is_ClearArray()) {
3392         memnode_worklist.append_if_missing(use);
3393       } else if (use->is_Mem() && use->in(MemNode::Memory) == n) {
3394         if (use->Opcode() == Op_StoreCM) // Ignore cardmark stores
3395           continue;
3396         memnode_worklist.append_if_missing(use);
3397       } else if (use->is_MemBar()) {
3398         if (use->in(TypeFunc::Memory) == n) { // Ignore precedent edge
3399           memnode_worklist.append_if_missing(use);
3400         }
3401 #ifdef ASSERT
3402       } else if(use->is_Mem()) {
3403         assert(use->in(MemNode::Memory) != n, "EA: missing memory path");
3404       } else if (use->is_MergeMem()) {
3405         assert(_mergemem_worklist.contains(use->as_MergeMem()), "EA: missing MergeMem node in the worklist");
3406       } else if (use->Opcode() == Op_EncodeISOArray) {
3407         if (use->in(MemNode::Memory) == n || use->in(3) == n) {
3408           // EncodeISOArray overwrites destination array
3409           memnode_worklist.append_if_missing(use);
3410         }
3411       } else {
3412         uint op = use->Opcode();
3413         if ((use->in(MemNode::Memory) == n) &&
3414             (op == Op_StrCompressedCopy || op == Op_StrInflatedCopy)) {
3415           // They overwrite memory edge corresponding to destination array,
3416           memnode_worklist.append_if_missing(use);
3417         } else if (!(BarrierSet::barrier_set()->barrier_set_c2()->is_gc_barrier_node(use) ||
3418               op == Op_AryEq || op == Op_StrComp || op == Op_HasNegatives ||
3419               op == Op_StrCompressedCopy || op == Op_StrInflatedCopy ||
3420               op == Op_StrEquals || op == Op_StrIndexOf || op == Op_StrIndexOfChar)) {
3421           n->dump();
3422           use->dump();
3423           assert(false, "EA: missing memory path");
3424         }
3425 #endif
3426       }
3427     }
3428   }
3429 
3430   //  Phase 3:  Process MergeMem nodes from mergemem_worklist.
3431   //            Walk each memory slice moving the first node encountered of each
3432   //            instance type to the the input corresponding to its alias index.
3433   uint length = _mergemem_worklist.length();
3434   for( uint next = 0; next < length; ++next ) {
3435     MergeMemNode* nmm = _mergemem_worklist.at(next);
3436     assert(!visited.test_set(nmm->_idx), "should not be visited before");
3437     // Note: we don't want to use MergeMemStream here because we only want to
3438     // scan inputs which exist at the start, not ones we add during processing.
3439     // Note 2: MergeMem may already contains instance memory slices added
3440     // during find_inst_mem() call when memory nodes were processed above.
3441     igvn->hash_delete(nmm);
3442     uint nslices = MIN2(nmm->req(), new_index_start);
3443     for (uint i = Compile::AliasIdxRaw+1; i < nslices; i++) {
3444       Node* mem = nmm->in(i);
3445       Node* cur = NULL;
3446       if (mem == NULL || mem->is_top())
3447         continue;
3448       // First, update mergemem by moving memory nodes to corresponding slices
3449       // if their type became more precise since this mergemem was created.
3450       while (mem->is_Mem()) {
3451         const Type *at = igvn->type(mem->in(MemNode::Address));
3452         if (at != Type::TOP) {
3453           assert (at->isa_ptr() != NULL, "pointer type required.");
3454           uint idx = (uint)_compile->get_alias_index(at->is_ptr());
3455           if (idx == i) {
3456             if (cur == NULL)
3457               cur = mem;
3458           } else {
3459             if (idx >= nmm->req() || nmm->is_empty_memory(nmm->in(idx))) {
3460               nmm->set_memory_at(idx, mem);
3461             }
3462           }
3463         }
3464         mem = mem->in(MemNode::Memory);
3465       }
3466       nmm->set_memory_at(i, (cur != NULL) ? cur : mem);
3467       // Find any instance of the current type if we haven't encountered
3468       // already a memory slice of the instance along the memory chain.
3469       for (uint ni = new_index_start; ni < new_index_end; ni++) {
3470         if((uint)_compile->get_general_index(ni) == i) {
3471           Node *m = (ni >= nmm->req()) ? nmm->empty_memory() : nmm->in(ni);
3472           if (nmm->is_empty_memory(m)) {
3473             Node* result = find_inst_mem(mem, ni, orig_phis);
3474             if (_compile->failing()) {
3475               return;
3476             }
3477             nmm->set_memory_at(ni, result);
3478           }
3479         }
3480       }
3481     }
3482     // Find the rest of instances values
3483     for (uint ni = new_index_start; ni < new_index_end; ni++) {
3484       const TypeOopPtr *tinst = _compile->get_adr_type(ni)->isa_oopptr();
3485       Node* result = step_through_mergemem(nmm, ni, tinst);
3486       if (result == nmm->base_memory()) {
3487         // Didn't find instance memory, search through general slice recursively.
3488         result = nmm->memory_at(_compile->get_general_index(ni));
3489         result = find_inst_mem(result, ni, orig_phis);
3490         if (_compile->failing()) {
3491           return;
3492         }
3493         nmm->set_memory_at(ni, result);
3494       }
3495     }
3496     igvn->hash_insert(nmm);
3497     record_for_optimizer(nmm);
3498   }
3499 
3500   //  Phase 4:  Update the inputs of non-instance memory Phis and
3501   //            the Memory input of memnodes
3502   // First update the inputs of any non-instance Phi's from
3503   // which we split out an instance Phi.  Note we don't have
3504   // to recursively process Phi's encounted on the input memory
3505   // chains as is done in split_memory_phi() since they  will
3506   // also be processed here.
3507   for (int j = 0; j < orig_phis.length(); j++) {
3508     PhiNode *phi = orig_phis.at(j);
3509     int alias_idx = _compile->get_alias_index(phi->adr_type());
3510     igvn->hash_delete(phi);
3511     for (uint i = 1; i < phi->req(); i++) {
3512       Node *mem = phi->in(i);
3513       Node *new_mem = find_inst_mem(mem, alias_idx, orig_phis);
3514       if (_compile->failing()) {
3515         return;
3516       }
3517       if (mem != new_mem) {
3518         phi->set_req(i, new_mem);
3519       }
3520     }
3521     igvn->hash_insert(phi);
3522     record_for_optimizer(phi);
3523   }
3524 
3525   // Update the memory inputs of MemNodes with the value we computed
3526   // in Phase 2 and move stores memory users to corresponding memory slices.
3527   // Disable memory split verification code until the fix for 6984348.
3528   // Currently it produces false negative results since it does not cover all cases.
3529 #if 0 // ifdef ASSERT
3530   visited.Reset();
3531   Node_Stack old_mems(arena, _compile->unique() >> 2);
3532 #endif
3533   for (uint i = 0; i < ideal_nodes.size(); i++) {
3534     Node*    n = ideal_nodes.at(i);
3535     Node* nmem = get_map(n->_idx);
3536     assert(nmem != NULL, "sanity");
3537     if (n->is_Mem()) {
3538 #if 0 // ifdef ASSERT
3539       Node* old_mem = n->in(MemNode::Memory);
3540       if (!visited.test_set(old_mem->_idx)) {
3541         old_mems.push(old_mem, old_mem->outcnt());
3542       }
3543 #endif
3544       assert(n->in(MemNode::Memory) != nmem, "sanity");
3545       if (!n->is_Load()) {
3546         // Move memory users of a store first.
3547         move_inst_mem(n, orig_phis);
3548       }
3549       // Now update memory input
3550       igvn->hash_delete(n);
3551       n->set_req(MemNode::Memory, nmem);
3552       igvn->hash_insert(n);
3553       record_for_optimizer(n);
3554     } else {
3555       assert(n->is_Allocate() || n->is_CheckCastPP() ||
3556              n->is_AddP() || n->is_Phi(), "unknown node used for set_map()");
3557     }
3558   }
3559 #if 0 // ifdef ASSERT
3560   // Verify that memory was split correctly
3561   while (old_mems.is_nonempty()) {
3562     Node* old_mem = old_mems.node();
3563     uint  old_cnt = old_mems.index();
3564     old_mems.pop();
3565     assert(old_cnt == old_mem->outcnt(), "old mem could be lost");
3566   }
3567 #endif
3568 }
3569 
3570 #ifndef PRODUCT
3571 static const char *node_type_names[] = {
3572   "UnknownType",
3573   "JavaObject",
3574   "LocalVar",
3575   "Field",
3576   "Arraycopy"
3577 };
3578 
3579 static const char *esc_names[] = {
3580   "UnknownEscape",
3581   "NoEscape",
3582   "ArgEscape",
3583   "GlobalEscape"
3584 };
3585 
dump(bool print_state) const3586 void PointsToNode::dump(bool print_state) const {
3587   NodeType nt = node_type();
3588   tty->print("%s ", node_type_names[(int) nt]);
3589   if (print_state) {
3590     EscapeState es = escape_state();
3591     EscapeState fields_es = fields_escape_state();
3592     tty->print("%s(%s) ", esc_names[(int)es], esc_names[(int)fields_es]);
3593     if (nt == PointsToNode::JavaObject && !this->scalar_replaceable())
3594       tty->print("NSR ");
3595   }
3596   if (is_Field()) {
3597     FieldNode* f = (FieldNode*)this;
3598     if (f->is_oop())
3599       tty->print("oop ");
3600     if (f->offset() > 0)
3601       tty->print("+%d ", f->offset());
3602     tty->print("(");
3603     for (BaseIterator i(f); i.has_next(); i.next()) {
3604       PointsToNode* b = i.get();
3605       tty->print(" %d%s", b->idx(),(b->is_JavaObject() ? "P" : ""));
3606     }
3607     tty->print(" )");
3608   }
3609   tty->print("[");
3610   for (EdgeIterator i(this); i.has_next(); i.next()) {
3611     PointsToNode* e = i.get();
3612     tty->print(" %d%s%s", e->idx(),(e->is_JavaObject() ? "P" : (e->is_Field() ? "F" : "")), e->is_Arraycopy() ? "cp" : "");
3613   }
3614   tty->print(" [");
3615   for (UseIterator i(this); i.has_next(); i.next()) {
3616     PointsToNode* u = i.get();
3617     bool is_base = false;
3618     if (PointsToNode::is_base_use(u)) {
3619       is_base = true;
3620       u = PointsToNode::get_use_node(u)->as_Field();
3621     }
3622     tty->print(" %d%s%s", u->idx(), is_base ? "b" : "", u->is_Arraycopy() ? "cp" : "");
3623   }
3624   tty->print(" ]]  ");
3625   if (_node == NULL)
3626     tty->print_cr("<null>");
3627   else
3628     _node->dump();
3629 }
3630 
dump(GrowableArray<PointsToNode * > & ptnodes_worklist)3631 void ConnectionGraph::dump(GrowableArray<PointsToNode*>& ptnodes_worklist) {
3632   bool first = true;
3633   int ptnodes_length = ptnodes_worklist.length();
3634   for (int i = 0; i < ptnodes_length; i++) {
3635     PointsToNode *ptn = ptnodes_worklist.at(i);
3636     if (ptn == NULL || !ptn->is_JavaObject())
3637       continue;
3638     PointsToNode::EscapeState es = ptn->escape_state();
3639     if ((es != PointsToNode::NoEscape) && !Verbose) {
3640       continue;
3641     }
3642     Node* n = ptn->ideal_node();
3643     if (n->is_Allocate() || (n->is_CallStaticJava() &&
3644                              n->as_CallStaticJava()->is_boxing_method())) {
3645       if (first) {
3646         tty->cr();
3647         tty->print("======== Connection graph for ");
3648         _compile->method()->print_short_name();
3649         tty->cr();
3650         first = false;
3651       }
3652       ptn->dump();
3653       // Print all locals and fields which reference this allocation
3654       for (UseIterator j(ptn); j.has_next(); j.next()) {
3655         PointsToNode* use = j.get();
3656         if (use->is_LocalVar()) {
3657           use->dump(Verbose);
3658         } else if (Verbose) {
3659           use->dump();
3660         }
3661       }
3662       tty->cr();
3663     }
3664   }
3665 }
3666 #endif
3667 
record_for_optimizer(Node * n)3668 void ConnectionGraph::record_for_optimizer(Node *n) {
3669   _igvn->_worklist.push(n);
3670   _igvn->add_users_to_worklist(n);
3671 }
3672