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