Path: blob/master/src/hotspot/share/opto/escape.hpp
40930 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_OPTO_ESCAPE_HPP25#define SHARE_OPTO_ESCAPE_HPP2627#include "opto/addnode.hpp"28#include "opto/node.hpp"29#include "utilities/growableArray.hpp"3031//32// Adaptation for C2 of the escape analysis algorithm described in:33//34// [Choi99] Jong-Deok Shoi, Manish Gupta, Mauricio Seffano,35// Vugranam C. Sreedhar, Sam Midkiff,36// "Escape Analysis for Java", Procedings of ACM SIGPLAN37// OOPSLA Conference, November 1, 199938//39// The flow-insensitive analysis described in the paper has been implemented.40//41// The analysis requires construction of a "connection graph" (CG) for42// the method being analyzed. The nodes of the connection graph are:43//44// - Java objects (JO)45// - Local variables (LV)46// - Fields of an object (OF), these also include array elements47//48// The CG contains 3 types of edges:49//50// - PointsTo (-P>) {LV, OF} to JO51// - Deferred (-D>) from {LV, OF} to {LV, OF}52// - Field (-F>) from JO to OF53//54// The following utility functions is used by the algorithm:55//56// PointsTo(n) - n is any CG node, it returns the set of JO that n could57// point to.58//59// The algorithm describes how to construct the connection graph60// in the following 4 cases:61//62// Case Edges Created63//64// (1) p = new T() LV -P> JO65// (2) p = q LV -D> LV66// (3) p.f = q JO -F> OF, OF -D> LV67// (4) p = q.f JO -F> OF, LV -D> OF68//69// In all these cases, p and q are local variables. For static field70// references, we can construct a local variable containing a reference71// to the static memory.72//73// C2 does not have local variables. However for the purposes of constructing74// the connection graph, the following IR nodes are treated as local variables:75// Phi (pointer values)76// LoadP, LoadN77// Proj#5 (value returned from call nodes including allocations)78// CheckCastPP, CastPP79//80// The LoadP, Proj and CheckCastPP behave like variables assigned to only once.81// Only a Phi can have multiple assignments. Each input to a Phi is treated82// as an assignment to it.83//84// The following node types are JavaObject:85//86// phantom_object (general globally escaped object)87// Allocate88// AllocateArray89// Parm (for incoming arguments)90// CastX2P ("unsafe" operations)91// CreateEx92// ConP93// LoadKlass94// ThreadLocal95// CallStaticJava (which returns Object)96//97// AddP nodes are fields.98//99// After building the graph, a pass is made over the nodes, deleting deferred100// nodes and copying the edges from the target of the deferred edge to the101// source. This results in a graph with no deferred edges, only:102//103// LV -P> JO104// OF -P> JO (the object whose oop is stored in the field)105// JO -F> OF106//107// Then, for each node which is GlobalEscape, anything it could point to108// is marked GlobalEscape. Finally, for any node marked ArgEscape, anything109// it could point to is marked ArgEscape.110//111112class Compile;113class Node;114class CallNode;115class PhiNode;116class PhaseTransform;117class PointsToNode;118class Type;119class TypePtr;120class VectorSet;121122class JavaObjectNode;123class LocalVarNode;124class FieldNode;125class ArraycopyNode;126127class ConnectionGraph;128129// ConnectionGraph nodes130class PointsToNode : public ResourceObj {131GrowableArray<PointsToNode*> _edges; // List of nodes this node points to132GrowableArray<PointsToNode*> _uses; // List of nodes which point to this node133134const u1 _type; // NodeType135u1 _flags; // NodeFlags136u1 _escape; // EscapeState of object137u1 _fields_escape; // EscapeState of object's fields138139Node* const _node; // Ideal node corresponding to this PointsTo node.140const int _idx; // Cached ideal node's _idx141const uint _pidx; // Index of this node142143public:144typedef enum {145UnknownType = 0,146JavaObject = 1,147LocalVar = 2,148Field = 3,149Arraycopy = 4150} NodeType;151152typedef enum {153UnknownEscape = 0,154NoEscape = 1, // An object does not escape method or thread and it is155// not passed to call. It could be replaced with scalar.156ArgEscape = 2, // An object does not escape method or thread but it is157// passed as argument to call or referenced by argument158// and it does not escape during call.159GlobalEscape = 3 // An object escapes the method or thread.160} EscapeState;161162typedef enum {163ScalarReplaceable = 1, // Not escaped object could be replaced with scalar164PointsToUnknown = 2, // Has edge to phantom_object165ArraycopySrc = 4, // Has edge from Arraycopy node166ArraycopyDst = 8 // Has edge to Arraycopy node167} NodeFlags;168169170inline PointsToNode(ConnectionGraph* CG, Node* n, EscapeState es, NodeType type);171172uint pidx() const { return _pidx; }173174Node* ideal_node() const { return _node; }175int idx() const { return _idx; }176177bool is_JavaObject() const { return _type == (u1)JavaObject; }178bool is_LocalVar() const { return _type == (u1)LocalVar; }179bool is_Field() const { return _type == (u1)Field; }180bool is_Arraycopy() const { return _type == (u1)Arraycopy; }181182JavaObjectNode* as_JavaObject() { assert(is_JavaObject(),""); return (JavaObjectNode*)this; }183LocalVarNode* as_LocalVar() { assert(is_LocalVar(),""); return (LocalVarNode*)this; }184FieldNode* as_Field() { assert(is_Field(),""); return (FieldNode*)this; }185ArraycopyNode* as_Arraycopy() { assert(is_Arraycopy(),""); return (ArraycopyNode*)this; }186187EscapeState escape_state() const { return (EscapeState)_escape; }188void set_escape_state(EscapeState state) { _escape = (u1)state; }189190EscapeState fields_escape_state() const { return (EscapeState)_fields_escape; }191void set_fields_escape_state(EscapeState state) { _fields_escape = (u1)state; }192193bool has_unknown_ptr() const { return (_flags & PointsToUnknown) != 0; }194void set_has_unknown_ptr() { _flags |= PointsToUnknown; }195196bool arraycopy_src() const { return (_flags & ArraycopySrc) != 0; }197void set_arraycopy_src() { _flags |= ArraycopySrc; }198bool arraycopy_dst() const { return (_flags & ArraycopyDst) != 0; }199void set_arraycopy_dst() { _flags |= ArraycopyDst; }200201bool scalar_replaceable() const { return (_flags & ScalarReplaceable) != 0;}202void set_scalar_replaceable(bool set) {203if (set) {204_flags |= ScalarReplaceable;205} else {206_flags &= ~ScalarReplaceable;207}208}209210int edge_count() const { return _edges.length(); }211PointsToNode* edge(int e) const { return _edges.at(e); }212bool add_edge(PointsToNode* edge) { return _edges.append_if_missing(edge); }213214int use_count() const { return _uses.length(); }215PointsToNode* use(int e) const { return _uses.at(e); }216bool add_use(PointsToNode* use) { return _uses.append_if_missing(use); }217218// Mark base edge use to distinguish from stored value edge.219bool add_base_use(FieldNode* use) { return _uses.append_if_missing((PointsToNode*)((intptr_t)use + 1)); }220static bool is_base_use(PointsToNode* use) { return (((intptr_t)use) & 1); }221static PointsToNode* get_use_node(PointsToNode* use) { return (PointsToNode*)(((intptr_t)use) & ~1); }222223// Return true if this node points to specified node or nodes it points to.224bool points_to(JavaObjectNode* ptn) const;225226// Return true if this node points only to non-escaping allocations.227bool non_escaping_allocation();228229// Return true if one node points to an other.230bool meet(PointsToNode* ptn);231232#ifndef PRODUCT233NodeType node_type() const { return (NodeType)_type;}234void dump(bool print_state=true) const;235#endif236237};238239class LocalVarNode: public PointsToNode {240public:241LocalVarNode(ConnectionGraph *CG, Node* n, EscapeState es):242PointsToNode(CG, n, es, LocalVar) {}243};244245class JavaObjectNode: public PointsToNode {246public:247JavaObjectNode(ConnectionGraph *CG, Node* n, EscapeState es):248PointsToNode(CG, n, es, JavaObject) {249if (es > NoEscape) {250set_scalar_replaceable(false);251}252}253};254255class FieldNode: public PointsToNode {256GrowableArray<PointsToNode*> _bases; // List of JavaObject nodes which point to this node257const int _offset; // Field's offset.258const bool _is_oop; // Field points to object259bool _has_unknown_base; // Has phantom_object base260public:261inline FieldNode(ConnectionGraph *CG, Node* n, EscapeState es, int offs, bool is_oop);262263int offset() const { return _offset;}264bool is_oop() const { return _is_oop;}265bool has_unknown_base() const { return _has_unknown_base; }266void set_has_unknown_base() { _has_unknown_base = true; }267268int base_count() const { return _bases.length(); }269PointsToNode* base(int e) const { return _bases.at(e); }270bool add_base(PointsToNode* base) { return _bases.append_if_missing(base); }271#ifdef ASSERT272// Return true if bases points to this java object.273bool has_base(JavaObjectNode* ptn) const;274#endif275276};277278class ArraycopyNode: public PointsToNode {279public:280ArraycopyNode(ConnectionGraph *CG, Node* n, EscapeState es):281PointsToNode(CG, n, es, Arraycopy) {}282};283284// Iterators for PointsTo node's edges:285// for (EdgeIterator i(n); i.has_next(); i.next()) {286// PointsToNode* u = i.get();287class PointsToIterator: public StackObj {288protected:289const PointsToNode* node;290const int cnt;291int i;292public:293inline PointsToIterator(const PointsToNode* n, int cnt) : node(n), cnt(cnt), i(0) { }294inline bool has_next() const { return i < cnt; }295inline void next() { i++; }296PointsToNode* get() const { ShouldNotCallThis(); return NULL; }297};298299class EdgeIterator: public PointsToIterator {300public:301inline EdgeIterator(const PointsToNode* n) : PointsToIterator(n, n->edge_count()) { }302inline PointsToNode* get() const { return node->edge(i); }303};304305class UseIterator: public PointsToIterator {306public:307inline UseIterator(const PointsToNode* n) : PointsToIterator(n, n->use_count()) { }308inline PointsToNode* get() const { return node->use(i); }309};310311class BaseIterator: public PointsToIterator {312public:313inline BaseIterator(const FieldNode* n) : PointsToIterator(n, n->base_count()) { }314inline PointsToNode* get() const { return ((PointsToNode*)node)->as_Field()->base(i); }315};316317318class ConnectionGraph: public ResourceObj {319friend class PointsToNode; // to access _compile320friend class FieldNode;321private:322GrowableArray<PointsToNode*> _nodes; // Map from ideal nodes to323// ConnectionGraph nodes.324325GrowableArray<PointsToNode*> _worklist; // Nodes to be processed326VectorSet _in_worklist;327uint _next_pidx;328329bool _collecting; // Indicates whether escape information330// is still being collected. If false,331// no new nodes will be processed.332333bool _verify; // verify graph334335JavaObjectNode* null_obj;336337Compile* _compile; // Compile object for current compilation338PhaseIterGVN* _igvn; // Value numbering339340Unique_Node_List ideal_nodes; // Used by CG construction and types splitting.341342int _build_iterations; // Number of iterations took to build graph343double _build_time; // Time (sec) took to build graph344345public:346JavaObjectNode* phantom_obj; // Unknown object347348private:349// Address of an element in _nodes. Used when the element is to be modified350PointsToNode* ptnode_adr(int idx) const {351// There should be no new ideal nodes during ConnectionGraph build,352// growableArray::at() will throw assert otherwise.353return _nodes.at(idx);354}355uint nodes_size() const { return _nodes.length(); }356357uint next_pidx() { return _next_pidx++; }358359// Add nodes to ConnectionGraph.360void add_local_var(Node* n, PointsToNode::EscapeState es);361void add_java_object(Node* n, PointsToNode::EscapeState es);362void add_field(Node* n, PointsToNode::EscapeState es, int offset);363void add_arraycopy(Node* n, PointsToNode::EscapeState es, PointsToNode* src, PointsToNode* dst);364365// Compute the escape state for arguments to a call.366void process_call_arguments(CallNode *call);367368// Add PointsToNode node corresponding to a call369void add_call_node(CallNode* call);370371// Create PointsToNode node and add it to Connection Graph.372void add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist);373374// Add final simple edges to graph.375void add_final_edges(Node *n);376377// Finish Graph construction.378bool complete_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,379GrowableArray<JavaObjectNode*>& non_escaped_worklist,380GrowableArray<JavaObjectNode*>& java_objects_worklist,381GrowableArray<FieldNode*>& oop_fields_worklist);382383#ifdef ASSERT384void verify_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,385GrowableArray<JavaObjectNode*>& non_escaped_worklist,386GrowableArray<JavaObjectNode*>& java_objects_worklist,387GrowableArray<Node*>& addp_worklist);388#endif389390// Add all references to this JavaObject node.391int add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist);392393// Put node on worklist if it is (or was) not there.394inline void add_to_worklist(PointsToNode* pt) {395PointsToNode* ptf = pt;396uint pidx_bias = 0;397if (PointsToNode::is_base_use(pt)) {398// Create a separate entry in _in_worklist for a marked base edge399// because _worklist may have an entry for a normal edge pointing400// to the same node. To separate them use _next_pidx as bias.401ptf = PointsToNode::get_use_node(pt)->as_Field();402pidx_bias = _next_pidx;403}404if (!_in_worklist.test_set(ptf->pidx() + pidx_bias)) {405_worklist.append(pt);406}407}408409// Put on worklist all uses of this node.410inline void add_uses_to_worklist(PointsToNode* pt) {411for (UseIterator i(pt); i.has_next(); i.next()) {412add_to_worklist(i.get());413}414}415416// Put on worklist all field's uses and related field nodes.417void add_field_uses_to_worklist(FieldNode* field);418419// Put on worklist all related field nodes.420void add_fields_to_worklist(FieldNode* field, PointsToNode* base);421422// Find fields which have unknown value.423int find_field_value(FieldNode* field);424425// Find fields initializing values for allocations.426int find_init_values_null (JavaObjectNode* ptn, PhaseTransform* phase);427int find_init_values_phantom(JavaObjectNode* ptn);428429// Set the escape state of an object and its fields.430void set_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc) {431// Don't change non-escaping state of NULL pointer.432if (ptn != null_obj) {433if (ptn->escape_state() < esc) {434ptn->set_escape_state(esc);435}436if (ptn->fields_escape_state() < esc) {437ptn->set_fields_escape_state(esc);438}439}440}441void set_fields_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc) {442// Don't change non-escaping state of NULL pointer.443if (ptn != null_obj) {444if (ptn->fields_escape_state() < esc) {445ptn->set_fields_escape_state(esc);446}447}448}449450// Propagate GlobalEscape and ArgEscape escape states to all nodes451// and check that we still have non-escaping java objects.452bool find_non_escaped_objects(GrowableArray<PointsToNode*>& ptnodes_worklist,453GrowableArray<JavaObjectNode*>& non_escaped_worklist);454455// Adjust scalar_replaceable state after Connection Graph is built.456void adjust_scalar_replaceable_state(JavaObjectNode* jobj);457458// Optimize ideal graph.459void optimize_ideal_graph(GrowableArray<Node*>& ptr_cmp_worklist,460GrowableArray<Node*>& storestore_worklist);461// Optimize objects compare.462const TypeInt* optimize_ptr_compare(Node* n);463464// Returns unique corresponding java object or NULL.465JavaObjectNode* unique_java_object(Node *n);466467// Add an edge of the specified type pointing to the specified target.468bool add_edge(PointsToNode* from, PointsToNode* to) {469assert(!from->is_Field() || from->as_Field()->is_oop(), "sanity");470471if (to == phantom_obj) {472if (from->has_unknown_ptr()) {473return false; // already points to phantom_obj474}475from->set_has_unknown_ptr();476}477478bool is_new = from->add_edge(to);479assert(to != phantom_obj || is_new, "sanity");480if (is_new) { // New edge?481assert(!_verify, "graph is incomplete");482is_new = to->add_use(from);483assert(is_new, "use should be also new");484}485return is_new;486}487488// Add an edge from Field node to its base and back.489bool add_base(FieldNode* from, PointsToNode* to) {490assert(!to->is_Arraycopy(), "sanity");491if (to == phantom_obj) {492if (from->has_unknown_base()) {493return false; // already has phantom_obj base494}495from->set_has_unknown_base();496}497bool is_new = from->add_base(to);498assert(to != phantom_obj || is_new, "sanity");499if (is_new) { // New edge?500assert(!_verify, "graph is incomplete");501if (to == null_obj) {502return is_new; // Don't add fields to NULL pointer.503}504if (to->is_JavaObject()) {505is_new = to->add_edge(from);506} else {507is_new = to->add_base_use(from);508}509assert(is_new, "use should be also new");510}511return is_new;512}513514// Helper functions515bool is_oop_field(Node* n, int offset, bool* unsafe);516static Node* find_second_addp(Node* addp, Node* n);517// offset of a field reference518int address_offset(Node* adr, PhaseTransform *phase);519520bool is_captured_store_address(Node* addp);521522// Propagate unique types created for non-escaped allocated objects through the graph523void split_unique_types(GrowableArray<Node *> &alloc_worklist, GrowableArray<ArrayCopyNode*> &arraycopy_worklist);524525// Helper methods for unique types split.526bool split_AddP(Node *addp, Node *base);527528PhiNode *create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created);529PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist);530531void move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis);532Node* find_inst_mem(Node* mem, int alias_idx,GrowableArray<PhiNode *> &orig_phi_worklist);533Node* step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop);534535536GrowableArray<MergeMemNode*> _mergemem_worklist; // List of all MergeMem nodes537538Node_Array _node_map; // used for bookkeeping during type splitting539// Used for the following purposes:540// Memory Phi - most recent unique Phi split out541// from this Phi542// MemNode - new memory input for this node543// ChecCastPP - allocation that this is a cast of544// allocation - CheckCastPP of the allocation545546// manage entries in _node_map547548void set_map(Node* from, Node* to) {549ideal_nodes.push(from);550_node_map.map(from->_idx, to);551}552553Node* get_map(int idx) { return _node_map[idx]; }554555PhiNode* get_map_phi(int idx) {556Node* phi = _node_map[idx];557return (phi == NULL) ? NULL : phi->as_Phi();558}559560// Returns true if there is an object in the scope of sfn that does not escape globally.561bool has_ea_local_in_scope(SafePointNode* sfn);562563bool has_arg_escape(CallJavaNode* call);564565// Notify optimizer that a node has been modified566void record_for_optimizer(Node *n);567568// Compute the escape information569bool compute_escape();570571public:572ConnectionGraph(Compile *C, PhaseIterGVN *igvn);573574// Check for non-escaping candidates575static bool has_candidates(Compile *C);576577// Perform escape analysis578static void do_analysis(Compile *C, PhaseIterGVN *igvn);579580bool not_global_escape(Node *n);581582// To be used by, e.g., BarrierSetC2 impls583Node* get_addp_base(Node* addp);584585// Utility function for nodes that load an object586void add_objload_to_connection_graph(Node* n, Unique_Node_List* delayed_worklist);587588// Add LocalVar node and edge if possible589void add_local_var_and_edge(Node* n, PointsToNode::EscapeState es, Node* to,590Unique_Node_List *delayed_worklist) {591PointsToNode* ptn = ptnode_adr(to->_idx);592if (delayed_worklist != NULL) { // First iteration of CG construction593add_local_var(n, es);594if (ptn == NULL) {595delayed_worklist->push(n);596return; // Process it later.597}598} else {599assert(ptn != NULL, "node should be registered");600}601add_edge(ptnode_adr(n->_idx), ptn);602}603604// Map ideal node to existing PointsTo node (usually phantom_object).605void map_ideal_node(Node *n, PointsToNode* ptn) {606assert(ptn != NULL, "only existing PointsTo node");607_nodes.at_put(n->_idx, ptn);608}609610void add_to_congraph_unsafe_access(Node* n, uint opcode, Unique_Node_List* delayed_worklist);611bool add_final_edges_unsafe_access(Node* n, uint opcode);612613#ifndef PRODUCT614void dump(GrowableArray<PointsToNode*>& ptnodes_worklist);615#endif616};617618inline PointsToNode::PointsToNode(ConnectionGraph *CG, Node* n, EscapeState es, NodeType type):619_edges(CG->_compile->comp_arena(), 2, 0, NULL),620_uses (CG->_compile->comp_arena(), 2, 0, NULL),621_type((u1)type),622_flags(ScalarReplaceable),623_escape((u1)es),624_fields_escape((u1)es),625_node(n),626_idx(n->_idx),627_pidx(CG->next_pidx()) {628assert(n != NULL && es != UnknownEscape, "sanity");629}630631inline FieldNode::FieldNode(ConnectionGraph *CG, Node* n, EscapeState es, int offs, bool is_oop):632PointsToNode(CG, n, es, Field),633_bases(CG->_compile->comp_arena(), 2, 0, NULL),634_offset(offs), _is_oop(is_oop),635_has_unknown_base(false) {636}637638#endif // SHARE_OPTO_ESCAPE_HPP639640641