1 /*
   2  * Copyright (c) 2005, 2008, 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 // This class implements a fast, conservative analysis of effect of methods
  26 // on the escape state of their arguments.  The analysis is at the bytecode
  27 // level.
  28 
  29 class  ciMethodBlocks;
  30 class  ciBlock;
  31 
  32 class BCEscapeAnalyzer : public ResourceObj {
  33  private:
  34   Arena*            _arena;        // ciEnv arena
  35 
  36   bool              _conservative; // If true, return maximally
  37                                    // conservative results.
  38   ciMethod*         _method;
  39   ciMethodData*     _methodData;
  40   int               _arg_size;
  41   VectorSet         _arg_local;
  42   VectorSet         _arg_stack;
  43   VectorSet         _arg_returned;
  44   VectorSet         _dirty;
  45   enum{ ARG_OFFSET_MAX = 31};
  46   uint              *_arg_modified;
  47 
  48   bool              _return_local;
  49   bool              _return_allocated;
  50   bool              _allocated_escapes;
  51   bool              _unknown_modified;
  52 
  53   GrowableArray<ciObject *> _dependencies;
  54 
  55   ciMethodBlocks   *_methodBlocks;
  56 
  57   BCEscapeAnalyzer* _parent;
  58   int               _level;
  59 
  60  public:
  61   class  ArgumentMap;
  62   class  StateInfo;
  63 
  64  private:
  65   // helper functions
  66   bool is_argument(int i)    { return i >= 0 && i < _arg_size; }
  67   void set_returned(ArgumentMap vars);
  68   bool is_argument(ArgumentMap vars);
  69   bool is_arg_stack(ArgumentMap vars);
  70   void clear_bits(ArgumentMap vars, VectorSet &bs);
  71   void set_method_escape(ArgumentMap vars);
  72   void set_global_escape(ArgumentMap vars);
  73   void set_dirty(ArgumentMap vars);
  74   void set_modified(ArgumentMap vars, int offs, int size);
  75 
  76   bool is_recursive_call(ciMethod* callee);
  77   void add_dependence(ciKlass *klass, ciMethod *meth);
  78   void propagate_dependencies(ciMethod *meth);
  79   void invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder);
  80 
  81   void iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors);
  82   void iterate_blocks(Arena *);
  83   void merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state);
  84 
  85   // analysis
  86   void initialize();
  87   void clear_escape_info();
  88   void compute_escape_info();
  89   vmIntrinsics::ID known_intrinsic();
  90   bool compute_escape_for_intrinsic(vmIntrinsics::ID iid);
  91   bool do_analysis();
  92 
  93   void read_escape_info();
  94 
  95   bool contains(uint arg_set1, uint arg_set2);
  96 
  97  public:
  98   BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent = NULL);
  99 
 100   // accessors
 101   ciMethod*         method() const               { return _method; }
 102   ciMethodData*     methodData() const           { return _methodData; }
 103   BCEscapeAnalyzer* parent() const               { return _parent; }
 104   int               level() const                { return _level; }
 105   GrowableArray<ciObject *>* dependencies()               { return &_dependencies; }
 106   bool              has_dependencies() const     { return !_dependencies.is_empty(); }
 107 
 108   // retrieval of interprocedural escape information
 109 
 110   // The given argument does not escape the callee.
 111   bool is_arg_local(int i) const {
 112     return !_conservative && _arg_local.test(i);
 113   }
 114 
 115   // The given argument escapes the callee, but does not become globally
 116   // reachable.
 117   bool is_arg_stack(int i) const {
 118     return !_conservative && _arg_stack.test(i);
 119   }
 120 
 121   // The given argument does not escape globally, and may be returned.
 122   bool is_arg_returned(int i) const {
 123     return !_conservative && _arg_returned.test(i); }
 124 
 125   // True iff only input arguments are returned.
 126   bool is_return_local() const {
 127     return !_conservative && _return_local;
 128   }
 129 
 130   // True iff only newly allocated unescaped objects are returned.
 131   bool is_return_allocated() const {
 132     return !_conservative && _return_allocated && !_allocated_escapes;
 133   }
 134 
 135   // Tracking of argument modification
 136 
 137   enum {OFFSET_ANY = -1};
 138   bool is_arg_modified(int arg, int offset, int size_in_bytes);
 139   void set_arg_modified(int arg, int offset, int size_in_bytes);
 140   bool has_non_arg_side_affects()    { return _unknown_modified; }
 141 
 142   // Copy dependencies from this analysis into "deps"
 143   void copy_dependencies(Dependencies *deps);
 144 
 145 #ifndef PRODUCT
 146   // dump escape information
 147   void dump();
 148 #endif
 149 };