Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/opto/escape.hpp
32285 views
/*1* Copyright (c) 2005, 2012, 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_VM_OPTO_ESCAPE_HPP25#define SHARE_VM_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 callnodes 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 v) {203if (v)204_flags |= ScalarReplaceable;205else206_flags &= ~ScalarReplaceable;207}208209int edge_count() const { return _edges.length(); }210PointsToNode* edge(int e) const { return _edges.at(e); }211bool add_edge(PointsToNode* edge) { return _edges.append_if_missing(edge); }212213int use_count() const { return _uses.length(); }214PointsToNode* use(int e) const { return _uses.at(e); }215bool add_use(PointsToNode* use) { return _uses.append_if_missing(use); }216217// Mark base edge use to distinguish from stored value edge.218bool add_base_use(FieldNode* use) { return _uses.append_if_missing((PointsToNode*)((intptr_t)use + 1)); }219static bool is_base_use(PointsToNode* use) { return (((intptr_t)use) & 1); }220static PointsToNode* get_use_node(PointsToNode* use) { return (PointsToNode*)(((intptr_t)use) & ~1); }221222// Return true if this node points to specified node or nodes it points to.223bool points_to(JavaObjectNode* ptn) const;224225// Return true if this node points only to non-escaping allocations.226bool non_escaping_allocation();227228// Return true if one node points to an other.229bool meet(PointsToNode* ptn);230231#ifndef PRODUCT232NodeType node_type() const { return (NodeType)_type;}233void dump(bool print_state=true) const;234#endif235236};237238class LocalVarNode: public PointsToNode {239public:240LocalVarNode(ConnectionGraph *CG, Node* n, EscapeState es):241PointsToNode(CG, n, es, LocalVar) {}242};243244class JavaObjectNode: public PointsToNode {245public:246JavaObjectNode(ConnectionGraph *CG, Node* n, EscapeState es):247PointsToNode(CG, n, es, JavaObject) {248if (es > NoEscape)249set_scalar_replaceable(false);250}251};252253class FieldNode: public PointsToNode {254GrowableArray<PointsToNode*> _bases; // List of JavaObject nodes which point to this node255const int _offset; // Field's offset.256const bool _is_oop; // Field points to object257bool _has_unknown_base; // Has phantom_object base258public:259FieldNode(ConnectionGraph *CG, Node* n, EscapeState es, int offs, bool is_oop):260PointsToNode(CG, n, es, Field),261_offset(offs), _is_oop(is_oop),262_has_unknown_base(false) {}263264int offset() const { return _offset;}265bool is_oop() const { return _is_oop;}266bool has_unknown_base() const { return _has_unknown_base; }267void set_has_unknown_base() { _has_unknown_base = true; }268269int base_count() const { return _bases.length(); }270PointsToNode* base(int e) const { return _bases.at(e); }271bool add_base(PointsToNode* base) { return _bases.append_if_missing(base); }272#ifdef ASSERT273// Return true if bases points to this java object.274bool has_base(JavaObjectNode* ptn) const;275#endif276277};278279class ArraycopyNode: public PointsToNode {280public:281ArraycopyNode(ConnectionGraph *CG, Node* n, EscapeState es):282PointsToNode(CG, n, es, Arraycopy) {}283};284285// Iterators for PointsTo node's edges:286// for (EdgeIterator i(n); i.has_next(); i.next()) {287// PointsToNode* u = i.get();288class PointsToIterator: public StackObj {289protected:290const PointsToNode* node;291const int cnt;292int i;293public:294inline PointsToIterator(const PointsToNode* n, int cnt) : node(n), cnt(cnt), i(0) { }295inline bool has_next() const { return i < cnt; }296inline void next() { i++; }297PointsToNode* get() const { ShouldNotCallThis(); return NULL; }298};299300class EdgeIterator: public PointsToIterator {301public:302inline EdgeIterator(const PointsToNode* n) : PointsToIterator(n, n->edge_count()) { }303inline PointsToNode* get() const { return node->edge(i); }304};305306class UseIterator: public PointsToIterator {307public:308inline UseIterator(const PointsToNode* n) : PointsToIterator(n, n->use_count()) { }309inline PointsToNode* get() const { return node->use(i); }310};311312class BaseIterator: public PointsToIterator {313public:314inline BaseIterator(const FieldNode* n) : PointsToIterator(n, n->base_count()) { }315inline PointsToNode* get() const { return ((PointsToNode*)node)->as_Field()->base(i); }316};317318319class ConnectionGraph: public ResourceObj {320friend class PointsToNode;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* phantom_obj; // Unknown object336JavaObjectNode* null_obj;337Node* _pcmp_neq; // ConI(#CC_GT)338Node* _pcmp_eq; // ConI(#CC_EQ)339340Compile* _compile; // Compile object for current compilation341PhaseIterGVN* _igvn; // Value numbering342343Unique_Node_List ideal_nodes; // Used by CG construction and types splitting.344345// Address of an element in _nodes. Used when the element is to be modified346PointsToNode* ptnode_adr(int idx) const {347// There should be no new ideal nodes during ConnectionGraph build,348// growableArray::at() will throw assert otherwise.349return _nodes.at(idx);350}351uint nodes_size() const { return _nodes.length(); }352353uint next_pidx() { return _next_pidx++; }354355// Add nodes to ConnectionGraph.356void add_local_var(Node* n, PointsToNode::EscapeState es);357void add_java_object(Node* n, PointsToNode::EscapeState es);358void add_field(Node* n, PointsToNode::EscapeState es, int offset);359void add_arraycopy(Node* n, PointsToNode::EscapeState es, PointsToNode* src, PointsToNode* dst);360361// Compute the escape state for arguments to a call.362void process_call_arguments(CallNode *call);363364// Add PointsToNode node corresponding to a call365void add_call_node(CallNode* call);366367// Map ideal node to existing PointsTo node (usually phantom_object).368void map_ideal_node(Node *n, PointsToNode* ptn) {369assert(ptn != NULL, "only existing PointsTo node");370_nodes.at_put(n->_idx, ptn);371}372373// Utility function for nodes that load an object374void add_objload_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist);375// Create PointsToNode node and add it to Connection Graph.376void add_node_to_connection_graph(Node *n, Unique_Node_List *delayed_worklist);377378// Add final simple edges to graph.379void add_final_edges(Node *n);380381// Finish Graph construction.382bool complete_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,383GrowableArray<JavaObjectNode*>& non_escaped_worklist,384GrowableArray<JavaObjectNode*>& java_objects_worklist,385GrowableArray<FieldNode*>& oop_fields_worklist);386387#ifdef ASSERT388void verify_connection_graph(GrowableArray<PointsToNode*>& ptnodes_worklist,389GrowableArray<JavaObjectNode*>& non_escaped_worklist,390GrowableArray<JavaObjectNode*>& java_objects_worklist,391GrowableArray<Node*>& addp_worklist);392#endif393394// Add all references to this JavaObject node.395int add_java_object_edges(JavaObjectNode* jobj, bool populate_worklist);396397// Put node on worklist if it is (or was) not there.398inline void add_to_worklist(PointsToNode* pt) {399PointsToNode* ptf = pt;400uint pidx_bias = 0;401if (PointsToNode::is_base_use(pt)) {402// Create a separate entry in _in_worklist for a marked base edge403// because _worklist may have an entry for a normal edge pointing404// to the same node. To separate them use _next_pidx as bias.405ptf = PointsToNode::get_use_node(pt)->as_Field();406pidx_bias = _next_pidx;407}408if (!_in_worklist.test_set(ptf->pidx() + pidx_bias)) {409_worklist.append(pt);410}411}412413// Put on worklist all uses of this node.414inline void add_uses_to_worklist(PointsToNode* pt) {415for (UseIterator i(pt); i.has_next(); i.next()) {416add_to_worklist(i.get());417}418}419420// Put on worklist all field's uses and related field nodes.421void add_field_uses_to_worklist(FieldNode* field);422423// Put on worklist all related field nodes.424void add_fields_to_worklist(FieldNode* field, PointsToNode* base);425426// Find fields which have unknown value.427int find_field_value(FieldNode* field);428429// Find fields initializing values for allocations.430int find_init_values(JavaObjectNode* ptn, PointsToNode* init_val, PhaseTransform* phase);431432// Set the escape state of an object and its fields.433void set_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc) {434// Don't change non-escaping state of NULL pointer.435if (ptn != null_obj) {436if (ptn->escape_state() < esc)437ptn->set_escape_state(esc);438if (ptn->fields_escape_state() < esc)439ptn->set_fields_escape_state(esc);440}441}442void set_fields_escape_state(PointsToNode* ptn, PointsToNode::EscapeState esc) {443// Don't change non-escaping state of NULL pointer.444if (ptn != null_obj) {445if (ptn->fields_escape_state() < esc)446ptn->set_fields_escape_state(esc);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.462Node* 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.503if (to->is_JavaObject()) {504is_new = to->add_edge(from);505} else {506is_new = to->add_base_use(from);507}508assert(is_new, "use should be also new");509}510return is_new;511}512513// Add LocalVar node and edge if possible514void add_local_var_and_edge(Node* n, PointsToNode::EscapeState es, Node* to,515Unique_Node_List *delayed_worklist) {516PointsToNode* ptn = ptnode_adr(to->_idx);517if (delayed_worklist != NULL) { // First iteration of CG construction518add_local_var(n, es);519if (ptn == NULL) {520delayed_worklist->push(n);521return; // Process it later.522}523} else {524assert(ptn != NULL, "node should be registered");525}526add_edge(ptnode_adr(n->_idx), ptn);527}528// Helper functions529bool is_oop_field(Node* n, int offset, bool* unsafe);530static Node* get_addp_base(Node *addp);531static Node* find_second_addp(Node* addp, Node* n);532// offset of a field reference533int address_offset(Node* adr, PhaseTransform *phase);534535536// Propagate unique types created for unescaped allocated objects537// through the graph538void split_unique_types(GrowableArray<Node *> &alloc_worklist);539540// Helper methods for unique types split.541bool split_AddP(Node *addp, Node *base);542543PhiNode *create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist, bool &new_created);544PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *> &orig_phi_worklist);545546void move_inst_mem(Node* n, GrowableArray<PhiNode *> &orig_phis);547Node* find_inst_mem(Node* mem, int alias_idx,GrowableArray<PhiNode *> &orig_phi_worklist);548Node* step_through_mergemem(MergeMemNode *mmem, int alias_idx, const TypeOopPtr *toop);549550551GrowableArray<MergeMemNode*> _mergemem_worklist; // List of all MergeMem nodes552553Node_Array _node_map; // used for bookeeping during type splitting554// Used for the following purposes:555// Memory Phi - most recent unique Phi split out556// from this Phi557// MemNode - new memory input for this node558// ChecCastPP - allocation that this is a cast of559// allocation - CheckCastPP of the allocation560561// manage entries in _node_map562563void set_map(Node* from, Node* to) {564ideal_nodes.push(from);565_node_map.map(from->_idx, to);566}567568Node* get_map(int idx) { return _node_map[idx]; }569570PhiNode* get_map_phi(int idx) {571Node* phi = _node_map[idx];572return (phi == NULL) ? NULL : phi->as_Phi();573}574575// Notify optimizer that a node has been modified576void record_for_optimizer(Node *n) {577_igvn->_worklist.push(n);578_igvn->add_users_to_worklist(n);579}580581// Compute the escape information582bool compute_escape();583584public:585ConnectionGraph(Compile *C, PhaseIterGVN *igvn);586587// Check for non-escaping candidates588static bool has_candidates(Compile *C);589590// Perform escape analysis591static void do_analysis(Compile *C, PhaseIterGVN *igvn);592593bool not_global_escape(Node *n);594595#ifndef PRODUCT596void dump(GrowableArray<PointsToNode*>& ptnodes_worklist);597#endif598};599600inline PointsToNode::PointsToNode(ConnectionGraph *CG, Node* n, EscapeState es, NodeType type):601_edges(CG->_compile->comp_arena(), 2, 0, NULL),602_uses (CG->_compile->comp_arena(), 2, 0, NULL),603_node(n),604_idx(n->_idx),605_pidx(CG->next_pidx()),606_type((u1)type),607_escape((u1)es),608_fields_escape((u1)es),609_flags(ScalarReplaceable) {610assert(n != NULL && es != UnknownEscape, "sanity");611}612613#endif // SHARE_VM_OPTO_ESCAPE_HPP614615616