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 #ifdef X86
  63 #ifdef _LP64
  64   if (UseAVX < 3) {
  65     int delta = XMMRegisterImpl::max_slots_per_register * XMMRegisterImpl::number_of_registers;
  66     int bottom = ConcreteRegisterImpl::max_fpr;
  67     int top = bottom + delta;
  68     int middle = bottom + (delta / 2);
  69     int xmm_slots = XMMRegisterImpl::max_slots_per_register;
  70     int lower = xmm_slots / 2;
  71     // mark bad every register that we cannot get to if AVX less than 3, we have all slots in the array
  72     // Note: vm2opto is allocated to ConcreteRegisterImpl::number_of_registers
  73     for (int i = bottom; i < middle; i+= xmm_slots) {
  74       for( OptoReg::Name j=OptoReg::Name(i + lower); j<OptoReg::Name(i + xmm_slots); j = OptoReg::add(j,1) ) {
  75         OptoReg::vm2opto[j] = OptoReg::Bad;
  76       }
  77     }
  78     // mark the upper zmm bank bad and all the mask registers bad in this case
  79     for( OptoReg::Name i=OptoReg::Name(middle); i<OptoReg::Name(_last_Mach_Reg-1); i = OptoReg::add(i,1) ) {
  80       OptoReg::vm2opto[i] = OptoReg::Bad;
  81     }
  82   }
  83 #endif
  84 #endif
  85 
  86   // Check that runtime and architecture description agree on callee-saved-floats
  87   bool callee_saved_floats = false;
  88   for( OptoReg::Name i=OptoReg::Name(0); i<OptoReg::Name(_last_Mach_Reg); i = OptoReg::add(i,1) ) {
  89     // Is there a callee-saved float or double?
  90     if( register_save_policy[i] == 'E' /* callee-saved */ &&
  91        (register_save_type[i] == Op_RegF || register_save_type[i] == Op_RegD) ) {
  92       callee_saved_floats = true;
  93     }
  94   }
  95 
  96   DEBUG_ONLY( Node::init_NodeProperty(); )
  97 
  98   Compile::pd_compiler2_init();
  99 
 100   CompilerThread* thread = CompilerThread::current();
 101 
 102   HandleMark handle_mark(thread);
 103   return OptoRuntime::generate(thread->env());
 104 }
 105 
 106 
 107 void C2Compiler::initialize() {
 108   // The first compiler thread that gets here will initialize the
 109   // small amount of global state (and runtime stubs) that C2 needs.
 110 
 111   // There is a race possible once at startup and then we're fine
 112 
 113   // Note that this is being called from a compiler thread not the
 114   // main startup thread.
 115   if (should_perform_init()) {
 116     bool successful = C2Compiler::init_c2_runtime();
 117     int new_state = (successful) ? initialized : failed;
 118     set_state(new_state);
 119   }
 120 }
 121 
 122 void C2Compiler::compile_method(ciEnv* env, ciMethod* target, int entry_bci) {
 123   assert(is_initialized(), "Compiler thread must be initialized");
 124 
 125   bool subsume_loads = SubsumeLoads;
 126   bool do_escape_analysis = DoEscapeAnalysis && !env->should_retain_local_variables();
 127   bool eliminate_boxing = EliminateAutoBox;
 128   while (!env->failing()) {
 129     // Attempt to compile while subsuming loads into machine instructions.
 130     Compile C(env, this, target, entry_bci, subsume_loads, do_escape_analysis, eliminate_boxing);
 131 
 132     // Check result and retry if appropriate.
 133     if (C.failure_reason() != NULL) {
 134       if (C.failure_reason_is(retry_class_loading_during_parsing())) {
 135         env->report_failure(C.failure_reason());
 136         continue;  // retry
 137       }
 138       if (C.failure_reason_is(retry_no_subsuming_loads())) {
 139         assert(subsume_loads, "must make progress");
 140         subsume_loads = false;
 141         env->report_failure(C.failure_reason());
 142         continue;  // retry
 143       }
 144       if (C.failure_reason_is(retry_no_escape_analysis())) {
 145         assert(do_escape_analysis, "must make progress");
 146         do_escape_analysis = false;
 147         env->report_failure(C.failure_reason());
 148         continue;  // retry
 149       }
 150       if (C.has_boxed_value()) {
 151         // Recompile without boxing elimination regardless failure reason.
 152         assert(eliminate_boxing, "must make progress");
 153         eliminate_boxing = false;
 154         env->report_failure(C.failure_reason());
 155         continue;  // retry
 156       }
 157       // Pass any other failure reason up to the ciEnv.
 158       // Note that serious, irreversible failures are already logged
 159       // on the ciEnv via env->record_method_not_compilable().
 160       env->record_failure(C.failure_reason());
 161     }
 162     if (StressRecompilation) {
 163       if (subsume_loads) {
 164         subsume_loads = false;
 165         continue;  // retry
 166       }
 167       if (do_escape_analysis) {
 168         do_escape_analysis = false;
 169         continue;  // retry
 170       }
 171     }
 172 
 173     // print inlining for last compilation only
 174     C.dump_print_inlining();
 175 
 176     // No retry; just break the loop.
 177     break;
 178   }
 179 }
 180 
 181 
 182 void C2Compiler::print_timers() {
 183   Compile::print_timers();
 184 }
 185 
 186 int C2Compiler::initial_code_buffer_size() {
 187   assert(SegmentedCodeCache, "Should be only used with a segmented code cache");
 188   return Compile::MAX_inst_size + Compile::MAX_locs_size + initial_const_capacity;
 189 }