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