1 /*
   2  * Copyright (c) 1999, 2014, 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 #include "precompiled.hpp"
  26 #include "opto/c2compiler.hpp"
  27 #include "opto/compile.hpp"
  28 #include "opto/optoreg.hpp"
  29 #include "opto/output.hpp"
  30 #include "opto/runtime.hpp"
  31 
  32 // register information defined by ADLC
  33 extern const char register_save_policy[];
  34 extern const int  register_save_type[];
  35 
  36 const char* C2Compiler::retry_no_subsuming_loads() {
  37   return "retry without subsuming loads";
  38 }
  39 const char* C2Compiler::retry_no_escape_analysis() {
  40   return "retry without escape analysis";
  41 }
  42 const char* C2Compiler::retry_class_loading_during_parsing() {
  43   return "retry class loading during parsing";
  44 }
  45 bool C2Compiler::init_c2_runtime() {
  46 
  47   // Check assumptions used while running ADLC
  48   Compile::adlc_verification();
  49   assert(REG_COUNT <= ConcreteRegisterImpl::number_of_registers, "incompatible register counts");
  50 
  51   for (int i = 0; i < ConcreteRegisterImpl::number_of_registers ; i++ ) {
  52       OptoReg::vm2opto[i] = OptoReg::Bad;
  53   }
  54 
  55   for( OptoReg::Name i=OptoReg::Name(0); i<OptoReg::Name(REG_COUNT); i = OptoReg::add(i,1) ) {
  56     VMReg r = OptoReg::as_VMReg(i);
  57     if (r->is_valid()) {
  58       OptoReg::vm2opto[r->value()] = i;
  59     }
  60   }
  61 
  62   // Check that runtime and architecture description agree on callee-saved-floats
  63   bool callee_saved_floats = false;
  64   for( OptoReg::Name i=OptoReg::Name(0); i<OptoReg::Name(_last_Mach_Reg); i = OptoReg::add(i,1) ) {
  65     // Is there a callee-saved float or double?
  66     if( register_save_policy[i] == 'E' /* callee-saved */ &&
  67        (register_save_type[i] == Op_RegF || register_save_type[i] == Op_RegD) ) {
  68       callee_saved_floats = true;
  69     }
  70   }
  71 
  72   DEBUG_ONLY( Node::init_NodeProperty(); )
  73 
  74   Compile::pd_compiler2_init();
  75 
  76   CompilerThread* thread = CompilerThread::current();
  77 
  78   HandleMark handle_mark(thread);
  79   return OptoRuntime::generate(thread->env());
  80 }
  81 
  82 void C2Compiler::initialize() {
  83   // The first compiler thread that gets here will initialize the
  84   // small amount of global state (and runtime stubs) that C2 needs.
  85 
  86   // There is a race possible once at startup and then we're fine
  87 
  88   // Note that this is being called from a compiler thread not the
  89   // main startup thread.
  90   if (should_perform_init()) {
  91     bool successful = C2Compiler::init_c2_runtime();
  92     int new_state = (successful) ? initialized : failed;
  93     set_state(new_state);
  94   }
  95 }
  96 
  97 void C2Compiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci) {
  98   assert(is_initialized(), "Compiler thread must be initialized");
  99 
 100   bool subsume_loads = SubsumeLoads;
 101   bool do_escape_analysis = DoEscapeAnalysis && !env->should_retain_local_variables();
 102   bool eliminate_boxing = EliminateAutoBox;
 103   while (!env->failing()) {
 104     // Attempt to compile while subsuming loads into machine instructions.
 105     Compile C(env, this, target, entry_bci, subsume_loads, do_escape_analysis, eliminate_boxing);
 106 
 107     // Check result and retry if appropriate.
 108     if (C.failure_reason() != NULL) {
 109       if (C.failure_reason_is(retry_class_loading_during_parsing())) {
 110         env->report_failure(C.failure_reason());
 111         continue;  // retry
 112       }
 113       if (C.failure_reason_is(retry_no_subsuming_loads())) {
 114         assert(subsume_loads, "must make progress");
 115         subsume_loads = false;
 116         env->report_failure(C.failure_reason());
 117         continue;  // retry
 118       }
 119       if (C.failure_reason_is(retry_no_escape_analysis())) {
 120         assert(do_escape_analysis, "must make progress");
 121         do_escape_analysis = false;
 122         env->report_failure(C.failure_reason());
 123         continue;  // retry
 124       }
 125       if (C.has_boxed_value()) {
 126         // Recompile without boxing elimination regardless failure reason.
 127         assert(eliminate_boxing, "must make progress");
 128         eliminate_boxing = false;
 129         env->report_failure(C.failure_reason());
 130         continue;  // retry
 131       }
 132       // Pass any other failure reason up to the ciEnv.
 133       // Note that serious, irreversible failures are already logged
 134       // on the ciEnv via env->record_method_not_compilable().
 135       env->record_failure(C.failure_reason());
 136     }
 137     if (StressRecompilation) {
 138       if (subsume_loads) {
 139         subsume_loads = false;
 140         continue;  // retry
 141       }
 142       if (do_escape_analysis) {
 143         do_escape_analysis = false;
 144         continue;  // retry
 145       }
 146     }
 147 
 148     // print inlining for last compilation only
 149     C.dump_print_inlining();
 150 
 151     // No retry; just break the loop.
 152     break;
 153   }
 154 }
 155 
 156 void C2Compiler::print_timers() {
 157   Compile::print_timers();
 158 }
 159 
 160 bool C2Compiler::is_intrinsic_available_for(methodHandle method, methodHandle compilation_context) {
 161   // Assume a non-virtual dispatch. A virtual dispatch is
 162   // possible for only a limited set of available intrinsics whereas
 163   // a non-virtual dispatch is possible for all available intrinsics.
 164   return Compile::is_intrinsic_available_for(method(), compilation_context(), false);
 165 }
 166 
 167 bool C2Compiler::is_intrinsic_available_for(methodHandle method) {
 168   return Compile::is_intrinsic_available_for(method(), NULL, false);
 169 }
 170 
 171 int C2Compiler::initial_code_buffer_size() {
 172   assert(SegmentedCodeCache, "Should be only used with a segmented code cache");
 173   return Compile::MAX_inst_size + Compile::MAX_locs_size + initial_const_capacity;
 174 }