escape.hpp 13.7 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31
#ifndef SHARE_VM_OPTO_ESCAPE_HPP
#define SHARE_VM_OPTO_ESCAPE_HPP

#include "opto/addnode.hpp"
#include "opto/node.hpp"
#include "utilities/growableArray.hpp"

D
duke 已提交
32 33 34
//
// Adaptation for C2 of the escape analysis algorithm described in:
//
35 36 37 38
// [Choi99] Jong-Deok Shoi, Manish Gupta, Mauricio Seffano,
//          Vugranam C. Sreedhar, Sam Midkiff,
//          "Escape Analysis for Java", Procedings of ACM SIGPLAN
//          OOPSLA  Conference, November 1, 1999
D
duke 已提交
39 40 41
//
// The flow-insensitive analysis described in the paper has been implemented.
//
42 43
// The analysis requires construction of a "connection graph" (CG) for
// the method being analyzed.  The nodes of the connection graph are:
D
duke 已提交
44 45 46 47 48 49 50
//
//     -  Java objects (JO)
//     -  Local variables (LV)
//     -  Fields of an object (OF),  these also include array elements
//
// The CG contains 3 types of edges:
//
51 52
//   -  PointsTo  (-P>)    {LV, OF} to JO
//   -  Deferred  (-D>)    from {LV, OF} to {LV, OF}
D
duke 已提交
53 54 55 56
//   -  Field     (-F>)    from JO to OF
//
// The following  utility functions is used by the algorithm:
//
57 58
//   PointsTo(n) - n is any CG node, it returns the set of JO that n could
//                 point to.
D
duke 已提交
59
//
60 61
// The algorithm describes how to construct the connection graph
// in the following 4 cases:
D
duke 已提交
62 63 64
//
//          Case                  Edges Created
//
65 66 67 68
// (1)   p   = new T()              LV -P> JO
// (2)   p   = q                    LV -D> LV
// (3)   p.f = q                    JO -F> OF,  OF -D> LV
// (4)   p   = q.f                  JO -F> OF,  LV -D> OF
D
duke 已提交
69
//
70 71 72
// In all these cases, p and q are local variables.  For static field
// references, we can construct a local variable containing a reference
// to the static memory.
D
duke 已提交
73 74 75 76
//
// C2 does not have local variables.  However for the purposes of constructing
// the connection graph, the following IR nodes are treated as local variables:
//     Phi    (pointer values)
77
//     LoadP, LoadN
78 79
//     Proj#5 (value returned from callnodes including allocations)
//     CheckCastPP, CastPP
D
duke 已提交
80
//
81 82
// The LoadP, Proj and CheckCastPP behave like variables assigned to only once.
// Only a Phi can have multiple assignments.  Each input to a Phi is treated
D
duke 已提交
83 84
// as an assignment to it.
//
85
// The following node types are JavaObject:
D
duke 已提交
86
//
87
//     phantom_object (general globally escaped object)
D
duke 已提交
88 89 90
//     Allocate
//     AllocateArray
//     Parm  (for incoming arguments)
91
//     CastX2P ("unsafe" operations)
D
duke 已提交
92 93 94
//     CreateEx
//     ConP
//     LoadKlass
95
//     ThreadLocal
96
//     CallStaticJava (which returns Object)
D
duke 已提交
97 98 99 100 101 102 103 104
//
// AddP nodes are fields.
//
// After building the graph, a pass is made over the nodes, deleting deferred
// nodes and copying the edges from the target of the deferred edge to the
// source.  This results in a graph with no deferred edges, only:
//
//    LV -P> JO
105
//    OF -P> JO (the object whose oop is stored in the field)
D
duke 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
//    JO -F> OF
//
// Then, for each node which is GlobalEscape, anything it could point to
// is marked GlobalEscape.  Finally, for any node marked ArgEscape, anything
// it could point to is marked ArgEscape.
//

class  Compile;
class  Node;
class  CallNode;
class  PhiNode;
class  PhaseTransform;
class  Type;
class  TypePtr;
class  VectorSet;

class PointsToNode {
friend class ConnectionGraph;
public:
  typedef enum {
126 127 128 129
    UnknownType = 0,
    JavaObject  = 1,
    LocalVar    = 2,
    Field       = 3
D
duke 已提交
130 131 132 133
  } NodeType;

  typedef enum {
    UnknownEscape = 0,
134 135 136 137 138 139
    NoEscape      = 1, // An object does not escape method or thread and it is
                       // not passed to call. It could be replaced with scalar.
    ArgEscape     = 2, // An object does not escape method or thread but it is
                       // passed as argument to call or referenced by argument
                       // and it does not escape during call.
    GlobalEscape  = 3  // An object escapes the method or thread.
D
duke 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  } EscapeState;

  typedef enum {
    UnknownEdge   = 0,
    PointsToEdge  = 1,
    DeferredEdge  = 2,
    FieldEdge     = 3
  } EdgeType;

private:
  enum {
    EdgeMask = 3,
    EdgeShift = 2,

    INITIAL_EDGE_COUNT = 4
  };

  NodeType             _type;
  EscapeState          _escape;
159 160 161 162
  GrowableArray<uint>* _edges; // outgoing edges
  Node* _node;                 // Ideal node corresponding to this PointsTo node.
  int   _offset;               // Object fields offsets.
  bool  _scalar_replaceable;   // Not escaped object could be replaced with scalar
D
duke 已提交
163 164

public:
165 166 167 168 169 170
  PointsToNode():
    _type(UnknownType),
    _escape(UnknownEscape),
    _edges(NULL),
    _node(NULL),
    _offset(-1),
171
    _scalar_replaceable(true) {}
D
duke 已提交
172 173 174 175 176


  EscapeState escape_state() const { return _escape; }
  NodeType node_type() const { return _type;}
  int offset() { return _offset;}
177
  bool scalar_replaceable() { return _scalar_replaceable;}
D
duke 已提交
178 179 180 181 182 183 184

  void set_offset(int offs) { _offset = offs;}
  void set_escape_state(EscapeState state) { _escape = state; }
  void set_node_type(NodeType ntype) {
    assert(_type == UnknownType || _type == ntype, "Can't change node type");
    _type = ntype;
  }
185
  void set_scalar_replaceable(bool v) { _scalar_replaceable = v; }
D
duke 已提交
186 187 188

  // count of outgoing edges
  uint edge_count() const { return (_edges == NULL) ? 0 : _edges->length(); }
189

D
duke 已提交
190
  // node index of target of outgoing edge "e"
191 192 193 194
  uint edge_target(uint e) const {
    assert(_edges != NULL, "valid edge index");
    return (_edges->at(e) >> EdgeShift);
  }
D
duke 已提交
195
  // type of outgoing edge "e"
196 197 198 199 200
  EdgeType edge_type(uint e) const {
    assert(_edges != NULL, "valid edge index");
    return (EdgeType) (_edges->at(e) & EdgeMask);
  }

D
duke 已提交
201 202
  // add a edge of the specified type pointing to the specified target
  void add_edge(uint targIdx, EdgeType et);
203

D
duke 已提交
204 205
  // remove an edge of the specified type pointing to the specified target
  void remove_edge(uint targIdx, EdgeType et);
206

D
duke 已提交
207
#ifndef PRODUCT
208
  void dump(bool print_state=true) const;
D
duke 已提交
209 210 211 212 213 214
#endif

};

class ConnectionGraph: public ResourceObj {
private:
215
  GrowableArray<PointsToNode>  _nodes; // Connection graph nodes indexed
216 217 218 219
                                       // by ideal node index.

  Unique_Node_List  _delayed_worklist; // Nodes to be processed before
                                       // the call build_connection_graph().
D
duke 已提交
220

221 222
  GrowableArray<MergeMemNode *>  _mergemem_worklist; // List of all MergeMem nodes

223 224
  VectorSet                _processed; // Records which nodes have been
                                       // processed.
D
duke 已提交
225

226 227 228 229
  bool                    _collecting; // Indicates whether escape information
                                       // is still being collected. If false,
                                       // no new nodes will be processed.

K
kvn 已提交
230 231 232
  bool                    _progress;   // Indicates whether new Graph's edges
                                       // were created.

233 234 235 236
  uint                _phantom_object; // Index of globally escaping object
                                       // that pointer values loaded from
                                       // a field which has not been set
                                       // are assumed to point to.
237 238
  uint                      _oop_null; // ConP(#NULL)->_idx
  uint                     _noop_null; // ConN(#NULL)->_idx
239 240
  Node*                     _pcmp_neq; // ConI(#CC_GT)
  Node*                      _pcmp_eq; // ConI(#CC_EQ)
241 242

  Compile *                  _compile; // Compile object for current compilation
243
  PhaseIterGVN *                _igvn; // Value numbering
D
duke 已提交
244

245 246 247 248 249
  // Address of an element in _nodes.  Used when the element is to be modified
  PointsToNode *ptnode_adr(uint idx) const {
    // There should be no new ideal nodes during ConnectionGraph build,
    // growableArray::adr_at() will throw assert otherwise.
    return _nodes.adr_at(idx);
D
duke 已提交
250
  }
251
  uint nodes_size() const { return _nodes.length(); }
D
duke 已提交
252

253 254 255
  // Add node to ConnectionGraph.
  void add_node(Node *n, PointsToNode::NodeType nt, PointsToNode::EscapeState es, bool done);

D
duke 已提交
256
  // offset of a field reference
257
  int address_offset(Node* adr, PhaseTransform *phase);
D
duke 已提交
258 259 260 261 262 263 264

  // compute the escape state for arguments to a call
  void process_call_arguments(CallNode *call, PhaseTransform *phase);

  // compute the escape state for the return value of a call
  void process_call_result(ProjNode *resproj, PhaseTransform *phase);

265 266
  // Populate Connection Graph with Ideal nodes.
  void record_for_escape_analysis(Node *n, PhaseTransform *phase);
D
duke 已提交
267

268 269
  // Build Connection Graph and set nodes escape state.
  void build_connection_graph(Node *n, PhaseTransform *phase);
D
duke 已提交
270 271 272 273

  // walk the connection graph starting at the node corresponding to "n" and
  // add the index of everything it could point to, to "ptset".  This may cause
  // Phi's encountered to get (re)processed  (which requires "phase".)
274 275 276 277 278 279
  VectorSet* PointsTo(Node * n);

  // Reused structures for PointsTo().
  VectorSet            pt_ptset;
  VectorSet            pt_visited;
  GrowableArray<uint>  pt_worklist;
D
duke 已提交
280 281 282 283 284 285 286

  //  Edge manipulation.  The "from_i" and "to_i" arguments are the
  //  node indices of the source and destination of the edge
  void add_pointsto_edge(uint from_i, uint to_i);
  void add_deferred_edge(uint from_i, uint to_i);
  void add_field_edge(uint from_i, uint to_i, int offs);

K
kvn 已提交
287 288 289 290 291 292 293
  // Add an edge of the specified type pointing to the specified target.
  // Set _progress if new edge is added.
  void add_edge(PointsToNode *f, uint to_i, PointsToNode::EdgeType et) {
    uint e_cnt = f->edge_count();
    f->add_edge(to_i, et);
    _progress |= (f->edge_count() != e_cnt);
  }
D
duke 已提交
294 295 296 297 298 299

  // Add an edge to node given by "to_i" from any field of adr_i whose offset
  // matches "offset"  A deferred edge is added if to_i is a LocalVar, and
  // a pointsto edge is added if it is a JavaObject
  void add_edge_from_fields(uint adr, uint to_i, int offs);

300 301
  // Add a deferred  edge from node given by "from_i" to any field
  // of adr_i whose offset matches "offset"
D
duke 已提交
302 303 304 305 306 307
  void add_deferred_edge_to_fields(uint from_i, uint adr, int offs);


  // Remove outgoing deferred edges from the node referenced by "ni".
  // Any outgoing edges from the target of the deferred edge are copied
  // to "ni".
308
  void remove_deferred(uint ni, GrowableArray<uint>* deferred_edges, VectorSet* visited);
D
duke 已提交
309 310 311 312 313 314 315 316

  Node_Array _node_map; // used for bookeeping during type splitting
                        // Used for the following purposes:
                        // Memory Phi    - most recent unique Phi split out
                        //                 from this Phi
                        // MemNode       - new memory input for this node
                        // ChecCastPP    - allocation that this is a cast of
                        // allocation    - CheckCastPP of the allocation
317
  bool split_AddP(Node *addp, Node *base,  PhaseGVN  *igvn);
D
duke 已提交
318 319
  PhiNode *create_split_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn, bool &new_created);
  PhiNode *split_memory_phi(PhiNode *orig_phi, int alias_idx, GrowableArray<PhiNode *>  &orig_phi_worklist, PhaseGVN  *igvn);
320
  void  move_inst_mem(Node* n, GrowableArray<PhiNode *>  &orig_phis, PhaseGVN *igvn);
321 322
  Node *find_inst_mem(Node *mem, int alias_idx,GrowableArray<PhiNode *>  &orig_phi_worklist,  PhaseGVN  *igvn);

D
duke 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  // Propagate unique types created for unescaped allocated objects
  // through the graph
  void split_unique_types(GrowableArray<Node *>  &alloc_worklist);

  // manage entries in _node_map
  void  set_map(int idx, Node *n)        { _node_map.map(idx, n); }
  Node *get_map(int idx)                 { return _node_map[idx]; }
  PhiNode *get_map_phi(int idx) {
    Node *phi = _node_map[idx];
    return (phi == NULL) ? NULL : phi->as_Phi();
  }

  // Notify optimizer that a node has been modified
  // Node:  This assumes that escape analysis is run before
  //        PhaseIterGVN creation
  void record_for_optimizer(Node *n) {
339
    _igvn->_worklist.push(n);
D
duke 已提交
340 341 342 343 344
  }

  // Set the escape state of a node
  void set_escape_state(uint ni, PointsToNode::EscapeState es);

345 346 347
  // Find fields initializing values for allocations.
  void find_init_values(Node* n, VectorSet* visited, PhaseTransform* phase);

348
  // Adjust escape state after Connection Graph is built.
349 350 351 352 353 354
  void adjust_escape_state(Node* n);

  // Propagate escape states to referenced nodes.
  bool propagate_escape_state(GrowableArray<int>* cg_worklist,
                              GrowableArray<uint>* worklist,
                              PointsToNode::EscapeState esc_state);
355

356 357 358
  // Optimize objects compare.
  Node* optimize_ptr_compare(Node* n);

359 360
  // Compute the escape information
  bool compute_escape();
361

D
duke 已提交
362
public:
363
  ConnectionGraph(Compile *C, PhaseIterGVN *igvn);
D
duke 已提交
364

365 366 367
  // Check for non-escaping candidates
  static bool has_candidates(Compile *C);

368 369 370
  // Perform escape analysis
  static void do_analysis(Compile *C, PhaseIterGVN *igvn);

D
duke 已提交
371
  // escape state of a node
372 373
  PointsToNode::EscapeState escape_state(Node *n);

D
duke 已提交
374 375 376 377
#ifndef PRODUCT
  void dump();
#endif
};
378 379

#endif // SHARE_VM_OPTO_ESCAPE_HPP