1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)bcEscapeAnalyzer.hpp 1.6 07/05/05 17:05:11 JVM"
   3 #endif
   4 /*
   5  * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 define_array(ciObjectArray, ciObject*);
  29 define_stack(ciObjectList, ciObjectArray);
  30 
  31 // This class implements a fast, conservative analysis of effect of methods
  32 // on the escape state of their arguments.  The analysis is at the bytecode
  33 // level.
  34 
  35 class  ciMethodBlocks;
  36 class  ciBlock;
  37 
  38 class BCEscapeAnalyzer : public ResourceObj {
  39  private:
  40   bool              _conservative; // If true, return maximally
  41                                    // conservative results.
  42   ciMethod*         _method;
  43   ciMethodData*     _methodData;
  44   int               _arg_size;
  45 
  46   intStack          _stack;
  47 
  48   BitMap            _arg_local;
  49   BitMap            _arg_stack;
  50   BitMap            _arg_returned;
  51   BitMap            _dirty;
  52 
  53   bool              _return_local;
  54   bool              _allocated_escapes;
  55   bool              _return_allocated;
  56 
  57   ciObjectList     _dependencies;
  58 
  59   ciMethodBlocks   *_methodBlocks;
  60 
  61   BCEscapeAnalyzer* _parent;
  62   int               _level;
  63 
  64   class  ArgumentMap;
  65   class  StateInfo;
  66 
  67   // helper functions
  68   bool is_argument(int i)    { return i >= 0 && i < _arg_size; }
  69 
  70   void raw_push(int i)       { _stack.push(i); }
  71   int  raw_pop()             { return _stack.is_empty() ? -1 : _stack.pop(); }
  72   void apush(int i)          { raw_push(i); }
  73   void spush()               { raw_push(-1); }
  74   void lpush()               { spush(); spush(); }
  75   int  apop()                { return raw_pop(); }
  76   void spop()                { assert(_stack.is_empty() || _stack.top() == -1, ""); raw_pop(); }
  77   void lpop()                { spop(); spop(); }
  78 
  79   void set_returned(ArgumentMap vars);
  80   bool is_argument(ArgumentMap vars);
  81   bool is_arg_stack(ArgumentMap vars);
  82   void clear_bits(ArgumentMap vars, BitMap &bs);
  83   void set_method_escape(ArgumentMap vars);
  84   void set_global_escape(ArgumentMap vars);
  85   void set_dirty(ArgumentMap vars);
  86 
  87   bool is_recursive_call(ciMethod* callee);
  88   void add_dependence(ciKlass *klass, ciMethod *meth);
  89   void propagate_dependencies(ciMethod *meth);
  90   void invoke(StateInfo &state, Bytecodes::Code code, ciMethod* target, ciKlass* holder);
  91 
  92   void iterate_one_block(ciBlock *blk, StateInfo &state, GrowableArray<ciBlock *> &successors);
  93   void iterate_blocks(Arena *);
  94   void merge_block_states(StateInfo *blockstates, ciBlock *dest, StateInfo *s_state);
  95 
  96   // analysis
  97   void initialize();
  98   void clear_escape_info();
  99   void compute_escape_info();
 100   vmIntrinsics::ID known_intrinsic();
 101   bool compute_escape_for_intrinsic(vmIntrinsics::ID iid);
 102   bool do_analysis();
 103 
 104   void read_escape_info();
 105 
 106   bool contains(uint arg_set1, uint arg_set2);
 107 
 108  public:
 109   BCEscapeAnalyzer(ciMethod* method, BCEscapeAnalyzer* parent = NULL);
 110 
 111   // accessors
 112   ciMethod*         method() const               { return _method; }
 113   ciMethodData*     methodData() const           { return _methodData; }
 114   BCEscapeAnalyzer* parent() const               { return _parent; }
 115   int               level() const                { return _level; }
 116   ciObjectList*     dependencies()               { return &_dependencies; }
 117   bool              has_dependencies() const     { return !_dependencies.is_empty(); }
 118 
 119   // retrieval of interprocedural escape information
 120 
 121   // The given argument does not escape the callee.
 122   bool is_arg_local(int i) const {
 123     return !_conservative && _arg_local.at(i);
 124   }
 125 
 126   // The given argument escapes the callee, but does not become globally
 127   // reachable.
 128   bool is_arg_stack(int i) const {
 129     return !_conservative && _arg_stack.at(i);
 130   }
 131 
 132   // The given argument does not escape globally, and may be returned.
 133   bool is_arg_returned(int i) const {
 134     return !_conservative && _arg_returned.at(i); }
 135 
 136   // True iff only input arguments are returned.
 137   bool is_return_local() const {
 138     return !_conservative && _return_local;
 139   }
 140 
 141   // True iff only newly allocated unescaped objects are returned.
 142   bool is_return_allocated() const {
 143     return !_conservative && _return_allocated && !_allocated_escapes;
 144   }
 145 
 146   // Copy dependencies from this analysis into "deps"
 147   void copy_dependencies(Dependencies *deps);
 148 };