1 /*
2 * Copyright (c) 2005, 2024, 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 #ifndef SHARE_OPTO_ESCAPE_HPP
26 #define SHARE_OPTO_ESCAPE_HPP
27
28 #include "opto/addnode.hpp"
29 #include "opto/node.hpp"
30 #include "utilities/growableArray.hpp"
31
32 //
33 // Adaptation for C2 of the escape analysis algorithm described in:
34 //
35 // [Choi99] Jong-Deok Shoi, Manish Gupta, Mauricio Seffano,
36 // Vugranam C. Sreedhar, Sam Midkiff,
37 // "Escape Analysis for Java", Proceedings of ACM SIGPLAN
38 // OOPSLA Conference, November 1, 1999
39 //
40 // The flow-insensitive analysis described in the paper has been implemented.
41 //
42 // The analysis requires construction of a "connection graph" (CG) for
43 // the method being analyzed. The nodes of the connection graph are:
44 //
45 // - Java objects (JO)
46 // - Local variables (LV)
47 // - Fields of an object (OF), these also include array elements
48 //
49 // The CG contains 3 types of edges:
50 //
51 // - PointsTo (-P>) {LV, OF} to JO
52 // - Deferred (-D>) from {LV, OF} to {LV, OF}
53 // - Field (-F>) from JO to OF
54 //
55 // The following utility functions is used by the algorithm:
56 //
57 // PointsTo(n) - n is any CG node, it returns the set of JO that n could
58 // point to.
59 //
60 // The algorithm describes how to construct the connection graph
61 // in the following 4 cases:
62 //
63 // Case Edges Created
64 //
65 // (1) p = new T() LV -P> JO
66 // (2) p = q LV -D> LV
67 // (3) p.f = q JO -F> OF, OF -D> LV
68 // (4) p = q.f JO -F> OF, LV -D> OF
69 //
70 // In all these cases, p and q are local variables. For static field
71 // references, we can construct a local variable containing a reference
72 // to the static memory.
73 //
74 // C2 does not have local variables. However for the purposes of constructing
75 // the connection graph, the following IR nodes are treated as local variables:
76 // Phi (pointer values)
77 // LoadP, LoadN
78 // Proj#5 (value returned from call nodes including allocations)
79 // CheckCastPP, CastPP
80 //
81 // The LoadP, Proj and CheckCastPP behave like variables assigned to only once.
82 // Only a Phi can have multiple assignments. Each input to a Phi is treated
83 // as an assignment to it.
84 //
85 // The following node types are JavaObject:
86 //
87 // phantom_object (general globally escaped object)
88 // Allocate
89 // AllocateArray
90 // Parm (for incoming arguments)
91 // CastX2P ("unsafe" operations)
92 // CreateEx
93 // ConP
94 // LoadKlass
95 // ThreadLocal
96 // CallStaticJava (which returns Object)
97 //
98 // AddP nodes are fields.
99 //
100 // After building the graph, a pass is made over the nodes, deleting deferred
101 // nodes and copying the edges from the target of the deferred edge to the
102 // source. This results in a graph with no deferred edges, only:
103 //
104 // LV -P> JO
105 // OF -P> JO (the object whose oop is stored in the field)
106 // JO -F> OF
107 //
108 // Then, for each node which is GlobalEscape, anything it could point to
109 // is marked GlobalEscape. Finally, for any node marked ArgEscape, anything
110 // it could point to is marked ArgEscape.
111 //
112
113 class Compile;
114 class Node;
115 class AbstractLockNode;
116 class CallNode;
117 class PhiNode;
118 class PhaseTransform;
119 class PointsToNode;
120 class Type;
121 class TypePtr;
122 class VectorSet;
123
124 class JavaObjectNode;
125 class LocalVarNode;
126 class FieldNode;
127 class ArraycopyNode;
128
129 class ConnectionGraph;
130
131 // ConnectionGraph nodes
132 class PointsToNode : public ArenaObj {
133 GrowableArray<PointsToNode*> _edges; // List of nodes this node points to
134 GrowableArray<PointsToNode*> _uses; // List of nodes which point to this node
135
136 const u1 _type; // NodeType
137 u1 _flags; // NodeFlags
138 u1 _escape; // EscapeState of object
139 u1 _fields_escape; // EscapeState of object's fields
140
141 Node* const _node; // Ideal node corresponding to this PointsTo node.
142 const int _idx; // Cached ideal node's _idx
143 const uint _pidx; // Index of this node
144
145 public:
146 typedef enum {
147 UnknownType = 0,
148 JavaObject = 1,
149 LocalVar = 2,
150 Field = 3,
151 Arraycopy = 4
152 } NodeType;
153
154 typedef enum {
155 UnknownEscape = 0,
156 NoEscape = 1, // An object does not escape method or thread and it is
157 // not passed to call. It could be replaced with scalar.
158 ArgEscape = 2, // An object does not escape method or thread but it is
159 // passed as argument to call or referenced by argument
160 // and it does not escape during call.
161 GlobalEscape = 3 // An object escapes the method or thread.
162 } EscapeState;
163
164 typedef enum {
165 ScalarReplaceable = 1, // Not escaped object could be replaced with scalar
166 PointsToUnknown = 2, // Has edge to phantom_object
167 ArraycopySrc = 4, // Has edge from Arraycopy node
168 ArraycopyDst = 8 // Has edge to Arraycopy node
169 } NodeFlags;
170
171
172 inline PointsToNode(ConnectionGraph* CG, Node* n, EscapeState es, NodeType type);
173
174 uint pidx() const { return _pidx; }
175
176 Node* ideal_node() const { return _node; }
177 int idx() const { return _idx; }
178
179 bool is_JavaObject() const { return _type == (u1)JavaObject; }
180 bool is_LocalVar() const { return _type == (u1)LocalVar; }
181 bool is_Field() const { return _type == (u1)Field; }
182 bool is_Arraycopy() const { return _type == (u1)Arraycopy; }
183
184 JavaObjectNode* as_JavaObject() { assert(is_JavaObject(),""); return (JavaObjectNode*)this; }
185 LocalVarNode* as_LocalVar() { assert(is_LocalVar(),""); return (LocalVarNode*)this; }
186 FieldNode* as_Field() { assert(is_Field(),""); return (FieldNode*)this; }
187 ArraycopyNode* as_Arraycopy() { assert(is_Arraycopy(),""); return (ArraycopyNode*)this; }
188
189 EscapeState escape_state() const { return (EscapeState)_escape; }
190 void set_escape_state(EscapeState state) { _escape = (u1)state; }
191
192 EscapeState fields_escape_state() const { return (EscapeState)_fields_escape; }
193 void set_fields_escape_state(EscapeState state) { _fields_escape = (u1)state; }
194
195 bool has_unknown_ptr() const { return (_flags & PointsToUnknown) != 0; }
196 void set_has_unknown_ptr() { _flags |= PointsToUnknown; }
197
198 bool arraycopy_src() const { return (_flags & ArraycopySrc) != 0; }
199 void set_arraycopy_src() { _flags |= ArraycopySrc; }
200 bool arraycopy_dst() const { return (_flags & ArraycopyDst) != 0; }
201 void set_arraycopy_dst() { _flags |= ArraycopyDst; }
202
203 bool scalar_replaceable() const { return (_flags & ScalarReplaceable) != 0;}
204 void set_scalar_replaceable(bool set) {
205 if (set) {
206 _flags |= ScalarReplaceable;
207 } else {
208 _flags &= ~ScalarReplaceable;
209 }
210 }
211
212 int edge_count() const { return _edges.length(); }
213 PointsToNode* edge(int e) const { return _edges.at(e); }
214 bool add_edge(PointsToNode* edge) { return _edges.append_if_missing(edge); }
215
216 int use_count() const { return _uses.length(); }
217 PointsToNode* use(int e) const { return _uses.at(e); }
218 bool add_use(PointsToNode* use) { return _uses.append_if_missing(use); }
219
220 // Mark base edge use to distinguish from stored value edge.
221 bool add_base_use(FieldNode* use) { return _uses.append_if_missing((PointsToNode*)((intptr_t)use + 1)); }
222 static bool is_base_use(PointsToNode* use) { return (((intptr_t)use) & 1); }
223 static PointsToNode* get_use_node(PointsToNode* use) { return (PointsToNode*)(((intptr_t)use) & ~1); }
224
225 // Return true if this node points to specified node or nodes it points to.
226 bool points_to(JavaObjectNode* ptn) const;
227
228 // Return true if this node points only to non-escaping allocations.
229 bool non_escaping_allocation();
230
231 // Return true if one node points to an other.
232 bool meet(PointsToNode* ptn);
233
234 #ifndef PRODUCT
235 NodeType node_type() const { return (NodeType)_type;}
236 void dump(bool print_state=true, outputStream* out=tty, bool newline=true) const;
237 void dump_header(bool print_state=true, outputStream* out=tty) const;
238 #endif
239
240 };
241
242 class LocalVarNode: public PointsToNode {
243 public:
244 LocalVarNode(ConnectionGraph *CG, Node* n, EscapeState es):
245 PointsToNode(CG, n, es, LocalVar) {}
246 };
247
248 class JavaObjectNode: public PointsToNode {
249 public:
250 JavaObjectNode(ConnectionGraph *CG, Node* n, EscapeState es):
251 PointsToNode(CG, n, es, JavaObject) {
252 if (es > NoEscape) {
253 set_scalar_replaceable(false);
254 }
255 }
256 };
257
258 class FieldNode: public PointsToNode {
259 GrowableArray<PointsToNode*> _bases; // List of JavaObject nodes which point to this node
260 const int _offset; // Field's offset.
261 const bool _is_oop; // Field points to object
262 bool _has_unknown_base; // Has phantom_object base
263 public:
264 inline FieldNode(ConnectionGraph *CG, Node* n, EscapeState es, int offs, bool is_oop);
265
266 int offset() const { return _offset;}
267 bool is_oop() const { return _is_oop;}
268 bool has_unknown_base() const { return _has_unknown_base; }
269 void set_has_unknown_base() { _has_unknown_base = true; }
270
271 int base_count() const { return _bases.length(); }
272 PointsToNode* base(int e) const { return _bases.at(e); }
273 bool add_base(PointsToNode* base) { return _bases.append_if_missing(base); }
274 #ifdef ASSERT
275 // Return true if bases points to this java object.
276 bool has_base(JavaObjectNode* ptn) const;
277 #endif
278
279 };
280
281 class ArraycopyNode: public PointsToNode {
282 public:
283 ArraycopyNode(ConnectionGraph *CG, Node* n, EscapeState es):
284 PointsToNode(CG, n, es, Arraycopy) {}
285 };
286
287 // Iterators for PointsTo node's edges:
288 // for (EdgeIterator i(n); i.has_next(); i.next()) {
289 // PointsToNode* u = i.get();
290 class PointsToIterator: public StackObj {
291 protected:
292 const PointsToNode* node;
293 const int cnt;
294 int i;
295 public:
296 inline PointsToIterator(const PointsToNode* n, int cnt) : node(n), cnt(cnt), i(0) { }
297 inline bool has_next() const { return i < cnt; }
298 inline void next() { i++; }
299 PointsToNode* get() const { ShouldNotCallThis(); return nullptr; }
300 };
301
302 class EdgeIterator: public PointsToIterator {
303 public:
304 inline EdgeIterator(const PointsToNode* n) : PointsToIterator(n, n->edge_count()) { }
305 inline PointsToNode* get() const { return node->edge(i); }
306 };
307
308 class UseIterator: public PointsToIterator {
309 public:
310 inline UseIterator(const PointsToNode* n) : PointsToIterator(n, n->use_count()) { }
311 inline PointsToNode* get() const { return node->use(i); }
312 };
313
314 class BaseIterator: public PointsToIterator {
315 public:
316 inline BaseIterator(const FieldNode* n) : PointsToIterator(n, n->base_count()) { }
317 inline PointsToNode* get() const { return ((PointsToNode*)node)->as_Field()->base(i); }
318 };
319
320
321 class ConnectionGraph: public ArenaObj {
322 friend class PointsToNode; // to access _compile
323 friend class FieldNode;
324 private:
325 GrowableArray<PointsToNode*> _nodes; // Map from ideal nodes to
326 // ConnectionGraph nodes.
327
328 GrowableArray<PointsToNode*> _worklist; // Nodes to be processed
329 VectorSet _in_worklist;
330 uint _next_pidx;
331
332 bool _collecting; // Indicates whether escape information
333 // is still being collected. If false,
334 // no new nodes will be processed.
335
336 bool _verify; // verify graph
337
338 JavaObjectNode* null_obj;
339
340 Compile* _compile; // Compile object for current compilation
341 PhaseIterGVN* _igvn; // Value numbering
342
343 Unique_Node_List ideal_nodes; // Used by CG construction and types splitting.
344
345 int _invocation; // Current number of analysis invocation
346 int _build_iterations; // Number of iterations took to build graph
347 double _build_time; // Time (sec) took to build graph
348
349 public:
350 JavaObjectNode* phantom_obj; // Unknown object
351
352 private:
353 // Address of an element in _nodes. Used when the element is to be modified
354 PointsToNode* ptnode_adr(int idx) const {
355 // There should be no new ideal nodes during ConnectionGraph build,
356 // growableArray::at() will throw assert otherwise.
357 return _nodes.at(idx);
358 }
359 uint nodes_size() const { return _nodes.length(); }
360
361 uint next_pidx() { return _next_pidx++; }
362
363 // Add nodes to ConnectionGraph.
364 void add_local_var(Node* n, PointsToNode::EscapeState es);
365 PointsToNode* add_java_object(Node* n, PointsToNode::EscapeState es);
366 void add_field(Node* n, PointsToNode::EscapeState es, int offset);
367 void add_arraycopy(Node* n, PointsToNode::EscapeState es, PointsToNode* src, PointsToNode* dst);
368
369 // Compute the escape state for arguments to a call.
370 void process_call_arguments(CallNode *call);
371
372 // Add PointsToNode node corresponding to a call
373 void add_call_node(CallNode* call);
374
375 // Create PointsToNode node and add it to Connection Graph.
376 void add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist);
377
378 // Add final simple edges to graph.
379 void add_final_edges(Node *n);
380
381 // Finish Graph construction.
382 bool complete_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,
383 GrowableArray<JavaObjectNode*>& non_escaped_worklist,
384 GrowableArray<JavaObjectNode*>& java_objects_worklist,
385 GrowableArray<FieldNode*>& oop_fields_worklist);
386
387 #ifdef ASSERT
388 void verify_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,
389 GrowableArray<JavaObjectNode*>& non_escaped_worklist,
390 GrowableArray<JavaObjectNode*>& java_objects_worklist,
391 GrowableArray<Node*>& addp_worklist);
392 #endif
393
394 // Add all references to this JavaObject node.
395 int add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist);
396
397 // Put node on worklist if it is (or was) not there.
398 inline void add_to_worklist(PointsToNode* pt) {
399 PointsToNode* ptf = pt;
400 uint pidx_bias = 0;
401 if (PointsToNode::is_base_use(pt)) {
402 // Create a separate entry in _in_worklist for a marked base edge
403 // because _worklist may have an entry for a normal edge pointing
404 // to the same node. To separate them use _next_pidx as bias.
405 ptf = PointsToNode::get_use_node(pt)->as_Field();
406 pidx_bias = _next_pidx;
407 }
408 if (!_in_worklist.test_set(ptf->pidx() + pidx_bias)) {
409 _worklist.append(pt);
410 }
411 }
412
413 // Put on worklist all uses of this node.
414 inline void add_uses_to_worklist(PointsToNode* pt) {
415 for (UseIterator i(pt); i.has_next(); i.next()) {
416 add_to_worklist(i.get());
417 }
418 }
419
420 // Put on worklist all field's uses and related field nodes.
421 void add_field_uses_to_worklist(FieldNode* field);
422
423 // Put on worklist all related field nodes.
424 void add_fields_to_worklist(FieldNode* field, PointsToNode* base);
425
426 // Find fields which have unknown value.
427 int find_field_value(FieldNode* field);
428
429 // Find fields initializing values for allocations.
430 int find_init_values_null (JavaObjectNode* ptn, PhaseValues* phase);
431 int find_init_values_phantom(JavaObjectNode* ptn);
432
433 // Set the escape state of an object and its fields.
434 void set_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc
435 NOT_PRODUCT(COMMA const char* reason)) {
436 // Don't change non-escaping state of null pointer.
437 if (ptn != null_obj) {
438 if (ptn->escape_state() < esc) {
439 NOT_PRODUCT(trace_es_update_helper(ptn, esc, false, reason));
440 ptn->set_escape_state(esc);
441 }
442 if (ptn->fields_escape_state() < esc) {
443 NOT_PRODUCT(trace_es_update_helper(ptn, esc, true, reason));
444 ptn->set_fields_escape_state(esc);
445 }
446
447 if (esc != PointsToNode::NoEscape) {
448 ptn->set_scalar_replaceable(false);
449 }
450 }
451 }
452 void set_fields_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc
453 NOT_PRODUCT(COMMA const char* reason)) {
454 // Don't change non-escaping state of null pointer.
455 if (ptn != null_obj) {
456 if (ptn->fields_escape_state() < esc) {
457 NOT_PRODUCT(trace_es_update_helper(ptn, esc, true, reason));
458 ptn->set_fields_escape_state(esc);
459 }
460
461 if (esc != PointsToNode::NoEscape) {
462 ptn->set_scalar_replaceable(false);
463 }
464 }
465 }
466
467 // Propagate GlobalEscape and ArgEscape escape states to all nodes
468 // and check that we still have non-escaping java objects.
469 bool find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,
470 GrowableArray<JavaObjectNode*>& non_escaped_worklist);
471
472 // Adjust scalar_replaceable state after Connection Graph is built.
473 void adjust_scalar_replaceable_state(JavaObjectNode* jobj, Unique_Node_List &reducible_merges);
474
475 // Reevaluate Phis reducible status after 'obj' became NSR.
476 void revisit_reducible_phi_status(JavaObjectNode* jobj, Unique_Node_List& reducible_merges);
477
478 // Propagate NSR (Not scalar replaceable) state.
479 void find_scalar_replaceable_allocs(GrowableArray<JavaObjectNode*>& jobj_worklist, Unique_Node_List &reducible_merges);
480
481 // Optimize ideal graph.
482 void optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,
483 GrowableArray<MemBarStoreStoreNode*>& storestore_worklist);
484 // Optimize objects compare.
485 const TypeInt* optimize_ptr_compare(Node* left, Node* right);
486
487 // Returns unique corresponding java object or null.
488 JavaObjectNode* unique_java_object(Node *n) const;
489
490 // Add an edge of the specified type pointing to the specified target.
491 bool add_edge(PointsToNode* from, PointsToNode* to) {
492 assert(!from->is_Field() || from->as_Field()->is_oop(), "sanity");
493
494 if (to == phantom_obj) {
495 if (from->has_unknown_ptr()) {
496 return false; // already points to phantom_obj
497 }
498 from->set_has_unknown_ptr();
499 }
500
501 bool is_new = from->add_edge(to);
502 assert(to != phantom_obj || is_new, "sanity");
503 if (is_new) { // New edge?
504 assert(!_verify, "graph is incomplete");
505 is_new = to->add_use(from);
506 assert(is_new, "use should be also new");
507 }
508 return is_new;
509 }
510
511 // Add an edge from Field node to its base and back.
512 bool add_base(FieldNode* from, PointsToNode* to) {
513 assert(!to->is_Arraycopy(), "sanity");
514 if (to == phantom_obj) {
515 if (from->has_unknown_base()) {
516 return false; // already has phantom_obj base
517 }
518 from->set_has_unknown_base();
519 }
520 bool is_new = from->add_base(to);
521 assert(to != phantom_obj || is_new, "sanity");
522 if (is_new) { // New edge?
523 assert(!_verify, "graph is incomplete");
524 if (to == null_obj) {
525 return is_new; // Don't add fields to null pointer.
526 }
527 if (to->is_JavaObject()) {
528 is_new = to->add_edge(from);
529 } else {
530 is_new = to->add_base_use(from);
531 }
532 assert(is_new, "use should be also new");
533 }
534 return is_new;
535 }
536
537 // Helper functions
538 bool is_oop_field(Node* n, int offset, bool* unsafe);
539 static Node* find_second_addp(Node* addp, Node* n);
540 // offset of a field reference
541 int address_offset(Node* adr, PhaseValues* phase);
542
543 bool is_captured_store_address(Node* addp);
544
545 // Propagate unique types created for non-escaped allocated objects through the graph
546 void split_unique_types(GrowableArray<Node *> &alloc_worklist,
547 GrowableArray<ArrayCopyNode*> &arraycopy_worklist,
548 GrowableArray<MergeMemNode*> &mergemem_worklist,
549 Unique_Node_List &reducible_merges);
550
551 // Helper methods for unique types split.
552 bool split_AddP(Node *addp, Node *base);
553
554 PhiNode *create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created);
555 PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, uint rec_depth);
556
557 void move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis);
558 Node* find_inst_mem(Node* mem, int alias_idx,GrowableArray<PhiNode *> &orig_phi_worklist, uint rec_depth = 0);
559 Node* step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop);
560
561 Node_Array _node_map; // used for bookkeeping during type splitting
562 // Used for the following purposes:
563 // Memory Phi - most recent unique Phi split out
564 // from this Phi
565 // MemNode - new memory input for this node
566 // ChecCastPP - allocation that this is a cast of
567 // allocation - CheckCastPP of the allocation
568
569 // manage entries in _node_map
570
571 void set_map(Node* from, Node* to) {
572 ideal_nodes.push(from);
573 _node_map.map(from->_idx, to);
574 }
575
576 Node* get_map(int idx) { return _node_map[idx]; }
577
578 PhiNode* get_map_phi(int idx) {
579 Node* phi = _node_map[idx];
580 return (phi == nullptr) ? nullptr : phi->as_Phi();
581 }
582
583 // Returns true if there is an object in the scope of sfn that does not escape globally.
584 bool has_ea_local_in_scope(SafePointNode* sfn);
585
586 bool has_arg_escape(CallJavaNode* call);
587
588 // Notify optimizer that a node has been modified
589 void record_for_optimizer(Node *n);
590
591 // Compute the escape information
592 bool compute_escape();
593
594 // -------------------------------------------
595 // Methods related to Reduce Allocation Merges
596 bool has_non_reducible_merge(FieldNode* field, Unique_Node_List& reducible_merges);
597 PhiNode* create_selector(PhiNode* ophi) const;
598 void updates_after_load_split(Node* data_phi, Node* previous_load, GrowableArray<Node *> &alloc_worklist);
599 Node* split_castpp_load_through_phi(Node* curr_addp, Node* curr_load, Node* region, GrowableArray<Node*>* bases_for_loads, GrowableArray<Node *> &alloc_worklist);
600 void reset_scalar_replaceable_entries(PhiNode* ophi);
601 bool has_reducible_merge_base(AddPNode* n, Unique_Node_List &reducible_merges);
602 Node* specialize_cmp(Node* base, Node* curr_ctrl);
603 Node* specialize_castpp(Node* castpp, Node* base, Node* current_control);
604
605 bool can_reduce_cmp(Node* n, Node* cmp) const;
606 bool has_been_reduced(PhiNode* n, SafePointNode* sfpt) const;
607 bool can_reduce_phi(PhiNode* ophi) const;
608 bool can_reduce_check_users(Node* n, uint nesting) const;
609 bool can_reduce_phi_check_inputs(PhiNode* ophi) const;
610
611 void reduce_phi_on_field_access(Node* previous_addp, GrowableArray<Node *> &alloc_worklist);
612 void reduce_phi_on_castpp_field_load(Node* castpp, GrowableArray<Node *> &alloc_worklist, GrowableArray<Node *> &memnode_worklist);
613 void reduce_phi_on_cmp(Node* cmp);
614 bool reduce_phi_on_safepoints(PhiNode* ophi);
615 bool reduce_phi_on_safepoints_helper(Node* ophi, Node* cast, Node* selector, Unique_Node_List& safepoints);
616 void reduce_phi(PhiNode* ophi, GrowableArray<Node *> &alloc_worklist, GrowableArray<Node *> &memnode_worklist);
617
618 void set_not_scalar_replaceable(PointsToNode* ptn NOT_PRODUCT(COMMA const char* reason)) const {
619 #ifndef PRODUCT
620 if (_compile->directive()->TraceEscapeAnalysisOption) {
621 assert(ptn != nullptr, "should not be null");
622 ptn->dump_header(true);
623 tty->print_cr("is NSR. %s", reason);
624 }
625 #endif
626 ptn->set_scalar_replaceable(false);
627 }
628
629 #ifndef PRODUCT
630 void trace_es_update_helper(PointsToNode* ptn, PointsToNode::EscapeState es, bool fields, const char* reason) const;
631 const char* trace_propagate_message(PointsToNode* from) const;
632 const char* trace_arg_escape_message(CallNode* call) const;
633 const char* trace_merged_message(PointsToNode* other) const;
634 #endif
635
636 public:
637 ConnectionGraph(Compile *C, PhaseIterGVN *igvn, int iteration);
638
639 // Verify that SafePointScalarMerge nodes are correctly connected
640 static void verify_ram_nodes(Compile* C, Node* root);
641
642 // Check for non-escaping candidates
643 static bool has_candidates(Compile *C);
644
645 // Perform escape analysis
646 static void do_analysis(Compile *C, PhaseIterGVN *igvn);
647
648 bool not_global_escape(Node *n);
649
650 bool can_eliminate_lock(AbstractLockNode* alock);
651
652 // To be used by, e.g., BarrierSetC2 impls
653 Node* get_addp_base(Node* addp);
654
655 // Utility function for nodes that load an object
656 void add_objload_to_connection_graph(Node* n, Unique_Node_List* delayed_worklist);
657
658 // Add LocalVar node and edge if possible
659 void add_local_var_and_edge(Node* n, PointsToNode::EscapeState es, Node* to,
660 Unique_Node_List *delayed_worklist) {
661 PointsToNode* ptn = ptnode_adr(to->_idx);
662 if (delayed_worklist != nullptr) { // First iteration of CG construction
663 add_local_var(n, es);
664 if (ptn == nullptr) {
665 delayed_worklist->push(n);
666 return; // Process it later.
667 }
668 } else {
669 assert(ptn != nullptr, "node should be registered");
670 }
671 add_edge(ptnode_adr(n->_idx), ptn);
672 }
673
674 // Map ideal node to existing PointsTo node (usually phantom_object).
675 void map_ideal_node(Node *n, PointsToNode* ptn) {
676 assert(ptn != nullptr, "only existing PointsTo node");
677 _nodes.at_put(n->_idx, ptn);
678 }
679
680 void add_to_congraph_unsafe_access(Node* n, uint opcode, Unique_Node_List* delayed_worklist);
681 bool add_final_edges_unsafe_access(Node* n, uint opcode);
682
683 #ifndef PRODUCT
684 static int _no_escape_counter;
685 static int _arg_escape_counter;
686 static int _global_escape_counter;
687 void dump(GrowableArray<PointsToNode*>& ptnodes_worklist);
688 static void print_statistics();
689 void escape_state_statistics(GrowableArray<JavaObjectNode*>& java_objects_worklist);
690 #endif
691 };
692
693 inline PointsToNode::PointsToNode(ConnectionGraph *CG, Node* n, EscapeState es, NodeType type):
694 _edges(CG->_compile->comp_arena(), 2, 0, nullptr),
695 _uses (CG->_compile->comp_arena(), 2, 0, nullptr),
696 _type((u1)type),
697 _flags(ScalarReplaceable),
698 _escape((u1)es),
699 _fields_escape((u1)es),
700 _node(n),
701 _idx(n->_idx),
702 _pidx(CG->next_pidx()) {
703 assert(n != nullptr && es != UnknownEscape, "sanity");
704 }
705
706 inline FieldNode::FieldNode(ConnectionGraph *CG, Node* n, EscapeState es, int offs, bool is_oop):
707 PointsToNode(CG, n, es, Field),
708 _bases(CG->_compile->comp_arena(), 2, 0, nullptr),
709 _offset(offs), _is_oop(is_oop),
710 _has_unknown_base(false) {
711 }
712
713 #endif // SHARE_OPTO_ESCAPE_HPP