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 BlockBegin;
  26 class CompilationResourceObj;
  27 class XHandlers;
  28 class ExceptionInfo;
  29 class DebugInformationRecorder;
  30 class FrameMap;
  31 class IR;
  32 class IRScope;
  33 class Instruction;
  34 class LinearScan;
  35 class OopMap;
  36 class LIR_Emitter;
  37 class LIR_Assembler;
  38 class CodeEmitInfo;
  39 class ciEnv;
  40 class ciMethod;
  41 class ValueStack;
  42 class LIR_OprDesc;
  43 class C1_MacroAssembler;
  44 class CFGPrinter;
  45 typedef LIR_OprDesc* LIR_Opr;
  46 
  47 
  48 define_array(BasicTypeArray, BasicType)
  49 define_stack(BasicTypeList, BasicTypeArray)
  50 
  51 define_array(ExceptionInfoArray, ExceptionInfo*)
  52 define_stack(ExceptionInfoList,  ExceptionInfoArray)
  53 
  54 class Compilation: public StackObj {
  55   friend class CompilationResourceObj;
  56  private:
  57   // compilation specifics
  58   Arena* _arena;
  59   int _next_id;
  60   int _next_block_id;
  61   AbstractCompiler*  _compiler;
  62   ciEnv*             _env;
  63   ciMethod*          _method;
  64   int                _osr_bci;
  65   IR*                _hir;
  66   int                _max_spills;
  67   FrameMap*          _frame_map;
  68   C1_MacroAssembler* _masm;
  69   bool               _has_exception_handlers;
  70   bool               _has_fpu_code;
  71   bool               _has_unsafe_access;
  72   bool               _would_profile;
  73   bool               _has_method_handle_invokes;  // True if this method has MethodHandle invokes.
  74   const char*        _bailout_msg;
  75   ExceptionInfoList* _exception_info_list;
  76   ExceptionHandlerTable _exception_handler_table;
  77   ImplicitExceptionTable _implicit_exception_table;
  78   LinearScan*        _allocator;
  79   CodeOffsets        _offsets;
  80   CodeBuffer         _code;
  81 
  82   // compilation helpers
  83   void initialize();
  84   void build_hir();
  85   void emit_lir();
  86 
  87   void emit_code_epilog(LIR_Assembler* assembler);
  88   int  emit_code_body();
  89 
  90   int  compile_java_method();
  91   void install_code(int frame_size);
  92   void compile_method();
  93 
  94   void generate_exception_handler_table();
  95 
  96   ExceptionInfoList* exception_info_list() const { return _exception_info_list; }
  97   ExceptionHandlerTable* exception_handler_table() { return &_exception_handler_table; }
  98 
  99   LinearScan* allocator()                          { return _allocator;      }
 100   void        set_allocator(LinearScan* allocator) { _allocator = allocator; }
 101 
 102   Instruction*       _current_instruction;       // the instruction currently being processed
 103 #ifndef PRODUCT
 104   Instruction*       _last_instruction_printed;  // the last instruction printed during traversal
 105 #endif // PRODUCT
 106 
 107  public:
 108   // creation
 109   Compilation(AbstractCompiler* compiler, ciEnv* env, ciMethod* method,
 110               int osr_bci, BufferBlob* buffer_blob);
 111   ~Compilation();
 112 
 113 
 114   static Compilation* current() {
 115     return (Compilation*) ciEnv::current()->compiler_data();
 116   }
 117 
 118   // accessors
 119   ciEnv* env() const                             { return _env; }
 120   AbstractCompiler* compiler() const             { return _compiler; }
 121   bool has_exception_handlers() const            { return _has_exception_handlers; }
 122   bool has_fpu_code() const                      { return _has_fpu_code; }
 123   bool has_unsafe_access() const                 { return _has_unsafe_access; }
 124   ciMethod* method() const                       { return _method; }
 125   int osr_bci() const                            { return _osr_bci; }
 126   bool is_osr_compile() const                    { return osr_bci() >= 0; }
 127   IR* hir() const                                { return _hir; }
 128   int max_spills() const                         { return _max_spills; }
 129   FrameMap* frame_map() const                    { return _frame_map; }
 130   CodeBuffer* code()                             { return &_code; }
 131   C1_MacroAssembler* masm() const                { return _masm; }
 132   CodeOffsets* offsets()                         { return &_offsets; }
 133   Arena* arena()                                 { return _arena; }
 134 
 135   // Instruction ids
 136   int get_next_id()                              { return _next_id++; }
 137   int number_of_instructions() const             { return _next_id; }
 138 
 139   // BlockBegin ids
 140   int get_next_block_id()                        { return _next_block_id++; }
 141   int number_of_blocks() const                   { return _next_block_id; }
 142 
 143   // setters
 144   void set_has_exception_handlers(bool f)        { _has_exception_handlers = f; }
 145   void set_has_fpu_code(bool f)                  { _has_fpu_code = f; }
 146   void set_has_unsafe_access(bool f)             { _has_unsafe_access = f; }
 147   void set_would_profile(bool f)                 { _would_profile = f; }
 148   // Add a set of exception handlers covering the given PC offset
 149   void add_exception_handlers_for_pco(int pco, XHandlers* exception_handlers);
 150   // Statistics gathering
 151   void notice_inlined_method(ciMethod* method);
 152 
 153   // JSR 292
 154   bool     has_method_handle_invokes() const { return _has_method_handle_invokes;     }
 155   void set_has_method_handle_invokes(bool z) {        _has_method_handle_invokes = z; }
 156 
 157   DebugInformationRecorder* debug_info_recorder() const; // = _env->debug_info();
 158   Dependencies* dependency_recorder() const; // = _env->dependencies()
 159   ImplicitExceptionTable* implicit_exception_table()     { return &_implicit_exception_table; }
 160 
 161   Instruction* current_instruction() const       { return _current_instruction; }
 162   Instruction* set_current_instruction(Instruction* instr) {
 163     Instruction* previous = _current_instruction;
 164     _current_instruction = instr;
 165     return previous;
 166   }
 167 
 168 #ifndef PRODUCT
 169   void maybe_print_current_instruction();
 170 #endif // PRODUCT
 171 
 172   // error handling
 173   void bailout(const char* msg);
 174   bool bailed_out() const                        { return _bailout_msg != NULL; }
 175   const char* bailout_msg() const                { return _bailout_msg; }
 176 
 177   static int desired_max_code_buffer_size() {
 178 #ifndef PPC
 179     return (int) NMethodSizeLimit;  // default 256K or 512K
 180 #else
 181     // conditional branches on PPC are restricted to 16 bit signed
 182     return MAX2((unsigned int)NMethodSizeLimit,32*K);
 183 #endif
 184   }
 185   static int desired_max_constant_size() {
 186 #ifndef PPC
 187     return (int) NMethodSizeLimit / 10;  // about 25K
 188 #else
 189     return (MAX2((unsigned int)NMethodSizeLimit, 32*K)) / 10;
 190 #endif
 191   }
 192 
 193   static void setup_code_buffer(CodeBuffer* cb, int call_stub_estimate);
 194 
 195   // timers
 196   static void print_timers();
 197 
 198 #ifndef PRODUCT
 199   // debugging support.
 200   // produces a file named c1compileonly in the current directory with
 201   // directives to compile only the current method and it's inlines.
 202   // The file can be passed to the command line option -XX:Flags=<filename>
 203   void compile_only_this_method();
 204   void compile_only_this_scope(outputStream* st, IRScope* scope);
 205   void exclude_this_method();
 206 #endif // PRODUCT
 207 
 208   bool is_profiling() {
 209     return env()->comp_level() == CompLevel_full_profile ||
 210            env()->comp_level() == CompLevel_limited_profile;
 211   }
 212   bool count_invocations() { return is_profiling(); }
 213   bool count_backedges()   { return is_profiling(); }
 214 
 215   // Helpers for generation of profile information
 216   bool profile_branches() {
 217     return env()->comp_level() == CompLevel_full_profile &&
 218       C1UpdateMethodData && C1ProfileBranches;
 219   }
 220   bool profile_calls() {
 221     return env()->comp_level() == CompLevel_full_profile &&
 222       C1UpdateMethodData && C1ProfileCalls;
 223   }
 224   bool profile_inlined_calls() {
 225     return profile_calls() && C1ProfileInlinedCalls;
 226   }
 227   bool profile_checkcasts() {
 228     return env()->comp_level() == CompLevel_full_profile &&
 229       C1UpdateMethodData && C1ProfileCheckcasts;
 230   }
 231 };
 232 
 233 
 234 // Macro definitions for unified bailout-support
 235 // The methods bailout() and bailed_out() are present in all classes
 236 // that might bailout, but forward all calls to Compilation
 237 #define BAILOUT(msg)               { bailout(msg); return;              }
 238 #define BAILOUT_(msg, res)         { bailout(msg); return res;          }
 239 
 240 #define CHECK_BAILOUT()            { if (bailed_out()) return;          }
 241 #define CHECK_BAILOUT_(res)        { if (bailed_out()) return res;      }
 242 
 243 
 244 class InstructionMark: public StackObj {
 245  private:
 246   Compilation* _compilation;
 247   Instruction*  _previous;
 248 
 249  public:
 250   InstructionMark(Compilation* compilation, Instruction* instr) {
 251     _compilation = compilation;
 252     _previous = _compilation->set_current_instruction(instr);
 253   }
 254   ~InstructionMark() {
 255     _compilation->set_current_instruction(_previous);
 256   }
 257 };
 258 
 259 
 260 //----------------------------------------------------------------------
 261 // Base class for objects allocated by the compiler in the compilation arena
 262 class CompilationResourceObj ALLOCATION_SUPER_CLASS_SPEC {
 263  public:
 264   void* operator new(size_t size) { return Compilation::current()->arena()->Amalloc(size); }
 265   void* operator new(size_t size, Arena* arena) {
 266     return arena->Amalloc(size);
 267   }
 268   void  operator delete(void* p) {} // nothing to do
 269 };
 270 
 271 
 272 //----------------------------------------------------------------------
 273 // Class for aggregating exception handler information.
 274 
 275 // Effectively extends XHandlers class with PC offset of
 276 // potentially exception-throwing instruction.
 277 // This class is used at the end of the compilation to build the
 278 // ExceptionHandlerTable.
 279 class ExceptionInfo: public CompilationResourceObj {
 280  private:
 281   int             _pco;                // PC of potentially exception-throwing instruction
 282   XHandlers*      _exception_handlers; // flat list of exception handlers covering this PC
 283 
 284  public:
 285   ExceptionInfo(int pco, XHandlers* exception_handlers)
 286     : _pco(pco)
 287     , _exception_handlers(exception_handlers)
 288   { }
 289 
 290   int pco()                                      { return _pco; }
 291   XHandlers* exception_handlers()                { return _exception_handlers; }
 292 };