1 /*
   2  * Copyright (c) 1999, 2010, 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 class MemoryBuffer;
  26 
  27 class GraphBuilder VALUE_OBJ_CLASS_SPEC {
  28  private:
  29   // Per-scope data. These are pushed and popped as we descend into
  30   // inlined methods. Currently in order to generate good code in the
  31   // inliner we have to attempt to inline methods directly into the
  32   // basic block we are parsing; this adds complexity.
  33   class ScopeData: public CompilationResourceObj {
  34    private:
  35     ScopeData*  _parent;
  36     // bci-to-block mapping
  37     BlockList*   _bci2block;
  38     // Scope
  39     IRScope*     _scope;
  40     // Whether this scope or any parent scope has exception handlers
  41     bool         _has_handler;
  42     // The bytecodes
  43     ciBytecodeStream* _stream;
  44 
  45     // Work list
  46     BlockList*   _work_list;
  47 
  48     // Maximum inline size for this scope
  49     intx         _max_inline_size;
  50     // Expression stack depth at point where inline occurred
  51     int          _caller_stack_size;
  52 
  53     // The continuation point for the inline. Currently only used in
  54     // multi-block inlines, but eventually would like to use this for
  55     // all inlines for uniformity and simplicity; in this case would
  56     // get the continuation point from the BlockList instead of
  57     // fabricating it anew because Invokes would be considered to be
  58     // BlockEnds.
  59     BlockBegin*  _continuation;
  60 
  61     // Was this ScopeData created only for the parsing and inlining of
  62     // a jsr?
  63     bool         _parsing_jsr;
  64     // We track the destination bci of the jsr only to determine
  65     // bailout conditions, since we only handle a subset of all of the
  66     // possible jsr-ret control structures. Recursive invocations of a
  67     // jsr are disallowed by the verifier.
  68     int          _jsr_entry_bci;
  69     // We need to track the local variable in which the return address
  70     // was stored to ensure we can handle inlining the jsr, because we
  71     // don't handle arbitrary jsr/ret constructs.
  72     int          _jsr_ret_addr_local;
  73     // If we are parsing a jsr, the continuation point for rets
  74     BlockBegin*  _jsr_continuation;
  75     // Cloned XHandlers for jsr-related ScopeDatas
  76     XHandlers*   _jsr_xhandlers;
  77 
  78     // Number of returns seen in this scope
  79     int          _num_returns;
  80 
  81     // In order to generate profitable code for inlining, we currently
  82     // have to perform an optimization for single-block inlined
  83     // methods where we continue parsing into the same block. This
  84     // allows us to perform CSE across inlined scopes and to avoid
  85     // storing parameters to the stack. Having a global register
  86     // allocator and being able to perform global CSE would allow this
  87     // code to be removed and thereby simplify the inliner.
  88     BlockBegin*  _cleanup_block;       // The block to which the return was added
  89     Instruction* _cleanup_return_prev; // Instruction before return instruction
  90     ValueStack*  _cleanup_state;       // State of that block (not yet pinned)
  91 
  92    public:
  93     ScopeData(ScopeData* parent);
  94 
  95     ScopeData* parent() const                      { return _parent;            }
  96 
  97     BlockList* bci2block() const                   { return _bci2block;         }
  98     void       set_bci2block(BlockList* bci2block) { _bci2block = bci2block;    }
  99 
 100     // NOTE: this has a different effect when parsing jsrs
 101     BlockBegin* block_at(int bci);
 102 
 103     IRScope* scope() const                         { return _scope;             }
 104     // Has side-effect of setting has_handler flag
 105     void set_scope(IRScope* scope);
 106 
 107     // Whether this or any parent scope has exception handlers
 108     bool has_handler() const                       { return _has_handler;       }
 109     void set_has_handler()                         { _has_handler = true;       }
 110 
 111     // Exception handlers list to be used for this scope
 112     XHandlers* xhandlers() const;
 113 
 114     // How to get a block to be parsed
 115     void add_to_work_list(BlockBegin* block);
 116     // How to remove the next block to be parsed; returns NULL if none left
 117     BlockBegin* remove_from_work_list();
 118     // Indicates parse is over
 119     bool is_work_list_empty() const;
 120 
 121     ciBytecodeStream* stream()                     { return _stream;            }
 122     void set_stream(ciBytecodeStream* stream)      { _stream = stream;          }
 123 
 124     intx max_inline_size() const                   { return _max_inline_size;   }
 125 
 126     BlockBegin* continuation() const               { return _continuation;      }
 127     void set_continuation(BlockBegin* cont)        { _continuation = cont;      }
 128 
 129     // Indicates whether this ScopeData was pushed only for the
 130     // parsing and inlining of a jsr
 131     bool parsing_jsr() const                       { return _parsing_jsr;       }
 132     void set_parsing_jsr()                         { _parsing_jsr = true;       }
 133     int  jsr_entry_bci() const                     { return _jsr_entry_bci;     }
 134     void set_jsr_entry_bci(int bci)                { _jsr_entry_bci = bci;      }
 135     void set_jsr_return_address_local(int local_no){ _jsr_ret_addr_local = local_no; }
 136     int  jsr_return_address_local() const          { return _jsr_ret_addr_local; }
 137     // Must be called after scope is set up for jsr ScopeData
 138     void setup_jsr_xhandlers();
 139 
 140     // The jsr continuation is only used when parsing_jsr is true, and
 141     // is different from the "normal" continuation since we can end up
 142     // doing a return (rather than a ret) from within a subroutine
 143     BlockBegin* jsr_continuation() const           { return _jsr_continuation;  }
 144     void set_jsr_continuation(BlockBegin* cont)    { _jsr_continuation = cont;  }
 145 
 146     int num_returns();
 147     void incr_num_returns();
 148 
 149     void set_inline_cleanup_info(BlockBegin* block,
 150                                  Instruction* return_prev,
 151                                  ValueStack* return_state);
 152     BlockBegin*  inline_cleanup_block() const      { return _cleanup_block; }
 153     Instruction* inline_cleanup_return_prev() const{ return _cleanup_return_prev; }
 154     ValueStack*  inline_cleanup_state() const      { return _cleanup_state; }
 155   };
 156 
 157   // for all GraphBuilders
 158   static bool       _can_trap[Bytecodes::number_of_java_codes];
 159 
 160   // for each instance of GraphBuilder
 161   ScopeData*        _scope_data;                 // Per-scope data; used for inlining
 162   Compilation*      _compilation;                // the current compilation
 163   ValueMap*         _vmap;                       // the map of values encountered (for CSE)
 164   MemoryBuffer*     _memory;
 165   const char*       _inline_bailout_msg;         // non-null if most recent inline attempt failed
 166   int               _instruction_count;          // for bailing out in pathological jsr/ret cases
 167   BlockBegin*       _start;                      // the start block
 168   BlockBegin*       _osr_entry;                  // the osr entry block block
 169   ValueStack*       _initial_state;              // The state for the start block
 170 
 171   // for each call to connect_to_end; can also be set by inliner
 172   BlockBegin*       _block;                      // the current block
 173   ValueStack*       _state;                      // the current execution state
 174   Instruction*      _last;                       // the last instruction added
 175   bool              _skip_block;                 // skip processing of the rest of this block
 176 
 177   // accessors
 178   ScopeData*        scope_data() const           { return _scope_data; }
 179   Compilation*      compilation() const          { return _compilation; }
 180   BlockList*        bci2block() const            { return scope_data()->bci2block(); }
 181   ValueMap*         vmap() const                 { assert(UseLocalValueNumbering, "should not access otherwise"); return _vmap; }
 182   bool              has_handler() const          { return scope_data()->has_handler(); }
 183 
 184   BlockBegin*       block() const                { return _block; }
 185   ValueStack*       state() const                { return _state; }
 186   void              set_state(ValueStack* state) { _state = state; }
 187   IRScope*          scope() const                { return scope_data()->scope(); }
 188   ciMethod*         method() const               { return scope()->method(); }
 189   ciBytecodeStream* stream() const               { return scope_data()->stream(); }
 190   Instruction*      last() const                 { return _last; }
 191   Bytecodes::Code   code() const                 { return stream()->cur_bc(); }
 192   int               bci() const                  { return stream()->cur_bci(); }
 193   int               next_bci() const             { return stream()->next_bci(); }
 194 
 195   // unified bailout support
 196   void bailout(const char* msg) const            { compilation()->bailout(msg); }
 197   bool bailed_out() const                        { return compilation()->bailed_out(); }
 198 
 199   // stack manipulation helpers
 200   void ipush(Value t) const                      { state()->ipush(t); }
 201   void lpush(Value t) const                      { state()->lpush(t); }
 202   void fpush(Value t) const                      { state()->fpush(t); }
 203   void dpush(Value t) const                      { state()->dpush(t); }
 204   void apush(Value t) const                      { state()->apush(t); }
 205   void  push(ValueType* type, Value t) const     { state()-> push(type, t); }
 206 
 207   Value ipop()                                   { return state()->ipop(); }
 208   Value lpop()                                   { return state()->lpop(); }
 209   Value fpop()                                   { return state()->fpop(); }
 210   Value dpop()                                   { return state()->dpop(); }
 211   Value apop()                                   { return state()->apop(); }
 212   Value  pop(ValueType* type)                    { return state()-> pop(type); }
 213 
 214   // instruction helpers
 215   void load_constant();
 216   void load_local(ValueType* type, int index);
 217   void store_local(ValueType* type, int index);
 218   void store_local(ValueStack* state, Value value, ValueType* type, int index);
 219   void load_indexed (BasicType type);
 220   void store_indexed(BasicType type);
 221   void stack_op(Bytecodes::Code code);
 222   void arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* state_before = NULL);
 223   void negate_op(ValueType* type);
 224   void shift_op(ValueType* type, Bytecodes::Code code);
 225   void logic_op(ValueType* type, Bytecodes::Code code);
 226   void compare_op(ValueType* type, Bytecodes::Code code);
 227   void convert(Bytecodes::Code op, BasicType from, BasicType to);
 228   void increment();
 229   void _goto(int from_bci, int to_bci);
 230   void if_node(Value x, If::Condition cond, Value y, ValueStack* stack_before);
 231   void if_zero(ValueType* type, If::Condition cond);
 232   void if_null(ValueType* type, If::Condition cond);
 233   void if_same(ValueType* type, If::Condition cond);
 234   void jsr(int dest);
 235   void ret(int local_index);
 236   void table_switch();
 237   void lookup_switch();
 238   void method_return(Value x);
 239   void call_register_finalizer();
 240   void access_field(Bytecodes::Code code);
 241   void invoke(Bytecodes::Code code);
 242   void new_instance(int klass_index);
 243   void new_type_array();
 244   void new_object_array();
 245   void check_cast(int klass_index);
 246   void instance_of(int klass_index);
 247   void monitorenter(Value x, int bci);
 248   void monitorexit(Value x, int bci);
 249   void new_multi_array(int dimensions);
 250   void throw_op(int bci);
 251   Value round_fp(Value fp_value);
 252 
 253   // stack/code manipulation helpers
 254   Instruction* append_with_bci(Instruction* instr, int bci);
 255   Instruction* append(Instruction* instr);
 256   Instruction* append_split(StateSplit* instr);
 257 
 258   // other helpers
 259   BlockBegin* block_at(int bci)                  { return scope_data()->block_at(bci); }
 260   XHandlers* handle_exception(Instruction* instruction);
 261   void connect_to_end(BlockBegin* beg);
 262   void null_check(Value value);
 263   void eliminate_redundant_phis(BlockBegin* start);
 264   BlockEnd* iterate_bytecodes_for_block(int bci);
 265   void iterate_all_blocks(bool start_in_current_block_for_inlining = false);
 266   Dependencies* dependency_recorder() const; // = compilation()->dependencies()
 267   bool direct_compare(ciKlass* k);
 268 
 269   void kill_all();
 270 
 271   // use of state copy routines (try to minimize unnecessary state
 272   // object allocations):
 273 
 274   // - if the instruction unconditionally needs a full copy of the
 275   // state (for patching for example), then use copy_state_before*
 276 
 277   // - if the instruction needs a full copy of the state only for
 278   // handler generation (Instruction::needs_exception_state() returns
 279   // false) then use copy_state_exhandling*
 280 
 281   // - if the instruction needs either a full copy of the state for
 282   // handler generation and a least a minimal copy of the state (as
 283   // returned by Instruction::exception_state()) for debug info
 284   // generation (that is when Instruction::needs_exception_state()
 285   // returns true) then use copy_state_for_exception*
 286 
 287   ValueStack* copy_state_before_with_bci(int bci);
 288   ValueStack* copy_state_before();
 289   ValueStack* copy_state_exhandling_with_bci(int bci);
 290   ValueStack* copy_state_exhandling();
 291   ValueStack* copy_state_for_exception_with_bci(int bci);
 292   ValueStack* copy_state_for_exception();
 293 
 294   //
 295   // Inlining support
 296   //
 297 
 298   // accessors
 299   bool parsing_jsr() const                               { return scope_data()->parsing_jsr();           }
 300   BlockBegin* continuation() const                       { return scope_data()->continuation();          }
 301   BlockBegin* jsr_continuation() const                   { return scope_data()->jsr_continuation();      }
 302   void set_continuation(BlockBegin* continuation)        { scope_data()->set_continuation(continuation); }
 303   void set_inline_cleanup_info(BlockBegin* block,
 304                                Instruction* return_prev,
 305                                ValueStack* return_state) { scope_data()->set_inline_cleanup_info(block,
 306                                                                                                   return_prev,
 307                                                                                                   return_state); }
 308   BlockBegin*  inline_cleanup_block() const              { return scope_data()->inline_cleanup_block();  }
 309   Instruction* inline_cleanup_return_prev() const        { return scope_data()->inline_cleanup_return_prev(); }
 310   ValueStack*  inline_cleanup_state() const              { return scope_data()->inline_cleanup_state();  }
 311   void incr_num_returns()                                { scope_data()->incr_num_returns();             }
 312   int  num_returns() const                               { return scope_data()->num_returns();           }
 313   intx max_inline_size() const                           { return scope_data()->max_inline_size();       }
 314   int  inline_level() const                              { return scope()->level();                      }
 315   int  recursive_inline_level(ciMethod* callee) const;
 316 
 317   // inlining of synchronized methods
 318   void inline_sync_entry(Value lock, BlockBegin* sync_handler);
 319   void fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler = false);
 320 
 321   // inliners
 322   bool try_inline(ciMethod* callee, bool holder_known);
 323   bool try_inline_intrinsics(ciMethod* callee);
 324   bool try_inline_full      (ciMethod* callee, bool holder_known);
 325   bool try_inline_jsr(int jsr_dest_bci);
 326 
 327   // helpers
 328   void inline_bailout(const char* msg);
 329   BlockBegin* header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state);
 330   BlockBegin* setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* init_state);
 331   void setup_osr_entry_block();
 332   void clear_inline_bailout();
 333   ValueStack* state_at_entry();
 334   void push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start);
 335   void push_scope(ciMethod* callee, BlockBegin* continuation);
 336   void push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci);
 337   void pop_scope();
 338   void pop_scope_for_jsr();
 339 
 340   bool append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile);
 341   bool append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile);
 342   bool append_unsafe_get_raw(ciMethod* callee, BasicType t);
 343   bool append_unsafe_put_raw(ciMethod* callee, BasicType t);
 344   bool append_unsafe_prefetch(ciMethod* callee, bool is_store, bool is_static);
 345   void append_unsafe_CAS(ciMethod* callee);
 346 
 347   NOT_PRODUCT(void print_inline_result(ciMethod* callee, bool res);)
 348 
 349   void profile_call(Value recv, ciKlass* predicted_holder);
 350   void profile_invocation(ciMethod* inlinee, ValueStack* state, int bci);
 351 
 352   // Shortcuts to profiling control.
 353   bool is_profiling()          { return _compilation->is_profiling();          }
 354   bool count_invocations()     { return _compilation->count_invocations();     }
 355   bool count_backedges()       { return _compilation->count_backedges();       }
 356   bool profile_branches()      { return _compilation->profile_branches();      }
 357   bool profile_calls()         { return _compilation->profile_calls();         }
 358   bool profile_inlined_calls() { return _compilation->profile_inlined_calls(); }
 359   bool profile_checkcasts()    { return _compilation->profile_checkcasts();    }
 360 
 361  public:
 362   NOT_PRODUCT(void print_stats();)
 363 
 364   // initialization
 365   static void initialize();
 366 
 367   // public
 368   static bool can_trap(ciMethod* method, Bytecodes::Code code) {
 369     assert(0 <= code && code < Bytecodes::number_of_java_codes, "illegal bytecode");
 370     if (_can_trap[code]) return true;
 371     // special handling for finalizer registration
 372     return code == Bytecodes::_return && method->intrinsic_id() == vmIntrinsics::_Object_init;
 373   }
 374 
 375   // creation
 376   GraphBuilder(Compilation* compilation, IRScope* scope);
 377   static void sort_top_into_worklist(BlockList* worklist, BlockBegin* top);
 378 
 379   BlockBegin* start() const                      { return _start; }
 380 };