1 /*
   2  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2008, 2009, 2010, 2011 Red Hat, Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "ci/ciEnv.hpp"
  28 #include "ci/ciMethod.hpp"
  29 #include "code/debugInfoRec.hpp"
  30 #include "code/dependencies.hpp"
  31 #include "code/exceptionHandlerTable.hpp"
  32 #include "code/oopRecorder.hpp"
  33 #include "compiler/abstractCompiler.hpp"
  34 #include "compiler/oopMap.hpp"
  35 #include "shark/llvmHeaders.hpp"
  36 #include "shark/sharkBuilder.hpp"
  37 #include "shark/sharkCodeBuffer.hpp"
  38 #include "shark/sharkCompiler.hpp"
  39 #include "shark/sharkContext.hpp"
  40 #include "shark/sharkEntry.hpp"
  41 #include "shark/sharkFunction.hpp"
  42 #include "shark/sharkMemoryManager.hpp"
  43 #include "shark/sharkNativeWrapper.hpp"
  44 #include "shark/shark_globals.hpp"
  45 #include "utilities/debug.hpp"
  46 
  47 #include <fnmatch.h>
  48 
  49 using namespace llvm;
  50 
  51 namespace {
  52   cl::opt<std::string>
  53   MCPU("mcpu");
  54 
  55   cl::list<std::string>
  56   MAttrs("mattr",
  57          cl::CommaSeparated);
  58 }
  59 
  60 SharkCompiler::SharkCompiler()
  61   : AbstractCompiler() {
  62   // Create the lock to protect the memory manager and execution engine
  63   _execution_engine_lock = new Monitor(Mutex::leaf, "SharkExecutionEngineLock");
  64   MutexLocker locker(execution_engine_lock());
  65 
  66   // Make LLVM safe for multithreading
  67   if (!llvm_start_multithreaded())
  68     fatal("llvm_start_multithreaded() failed");
  69 
  70   // Initialize the native target
  71   InitializeNativeTarget();
  72 
  73   // MCJIT require a native AsmPrinter
  74   InitializeNativeTargetAsmPrinter();
  75 
  76   // Create the two contexts which we'll use
  77   _normal_context = new SharkContext("normal");
  78   _native_context = new SharkContext("native");
  79 
  80   // Create the memory manager
  81   _memory_manager = new SharkMemoryManager();
  82 
  83   // Finetune LLVM for the current host CPU.
  84   StringMap<bool> Features;
  85   bool gotCpuFeatures = llvm::sys::getHostCPUFeatures(Features);
  86   std::string cpu("-mcpu=" + llvm::sys::getHostCPUName());
  87 
  88   std::vector<const char*> args;
  89   args.push_back(""); // program name
  90   args.push_back(cpu.c_str());
  91 
  92   std::string mattr("-mattr=");
  93   if(gotCpuFeatures){
  94     for(StringMap<bool>::iterator I = Features.begin(),
  95       E = Features.end(); I != E; ++I){
  96       if(I->second){
  97         std::string attr(I->first());
  98         mattr+="+"+attr+",";
  99       }
 100     }
 101     args.push_back(mattr.c_str());
 102   }
 103 
 104   args.push_back(0);  // terminator
 105   cl::ParseCommandLineOptions(args.size() - 1, (char **) &args[0]);
 106 
 107   // Create the JIT
 108   std::string ErrorMsg;
 109 
 110   EngineBuilder builder(_normal_context->module());
 111   builder.setMCPU(MCPU);
 112   builder.setMAttrs(MAttrs);
 113   builder.setJITMemoryManager(memory_manager());
 114   builder.setEngineKind(EngineKind::JIT);
 115   builder.setErrorStr(&ErrorMsg);
 116   _execution_engine = builder.create();
 117 
 118   if (!execution_engine()) {
 119     if (!ErrorMsg.empty())
 120       printf("Error while creating Shark JIT: %s\n",ErrorMsg.c_str());
 121     else
 122       printf("Unknown error while creating Shark JIT\n");
 123     exit(1);
 124   }
 125 
 126   execution_engine()->addModule(
 127     _native_context->module());
 128 
 129   // All done
 130   mark_initialized();
 131 }
 132 
 133 void SharkCompiler::initialize() {
 134   ShouldNotCallThis();
 135 }
 136 
 137 void SharkCompiler::compile_method(ciEnv*    env,
 138                                    ciMethod* target,
 139                                    int       entry_bci) {
 140   assert(is_initialized(), "should be");
 141   ResourceMark rm;
 142   const char *name = methodname(
 143     target->holder()->name()->as_utf8(), target->name()->as_utf8());
 144 
 145   if (SharkShowCompiledMethods) {
 146     tty->print_cr("Shark compiling method: %s", name);
 147   }
 148 
 149   // Do the typeflow analysis
 150   ciTypeFlow *flow;
 151   if (entry_bci == InvocationEntryBci)
 152     flow = target->get_flow_analysis();
 153   else
 154     flow = target->get_osr_flow_analysis(entry_bci);
 155   if (flow->failing())
 156     return;
 157   if (SharkPrintTypeflowOf != NULL) {
 158     if (!fnmatch(SharkPrintTypeflowOf, name, 0))
 159       flow->print_on(tty);
 160   }
 161 
 162   // Create the recorders
 163   Arena arena;
 164   env->set_oop_recorder(new OopRecorder(&arena));
 165   OopMapSet oopmaps;
 166   env->set_debug_info(new DebugInformationRecorder(env->oop_recorder()));
 167   env->debug_info()->set_oopmaps(&oopmaps);
 168   env->set_dependencies(new Dependencies(env));
 169 
 170   // Create the code buffer and builder
 171   CodeBuffer hscb("Shark", 256 * K, 64 * K);
 172   hscb.initialize_oop_recorder(env->oop_recorder());
 173   MacroAssembler *masm = new MacroAssembler(&hscb);
 174   SharkCodeBuffer cb(masm);
 175   SharkBuilder builder(&cb);
 176 
 177   // Emit the entry point
 178   SharkEntry *entry = (SharkEntry *) cb.malloc(sizeof(SharkEntry));
 179 
 180   // Build the LLVM IR for the method
 181   Function *function = SharkFunction::build(env, &builder, flow, name);
 182   if (SharkVerifyFunctions) {
 183     verifyFunction(*function);
 184   }
 185 
 186   // Generate native code.  It's unpleasant that we have to drop into
 187   // the VM to do this -- it blocks safepoints -- but I can't see any
 188   // other way to handle the locking.
 189   {
 190     ThreadInVMfromNative tiv(JavaThread::current());
 191     generate_native_code(entry, function, name);
 192   }
 193 
 194   // Install the method into the VM
 195   CodeOffsets offsets;
 196   offsets.set_value(CodeOffsets::Deopt, 0);
 197   offsets.set_value(CodeOffsets::Exceptions, 0);
 198   offsets.set_value(CodeOffsets::Verified_Entry,
 199                     target->is_static() ? 0 : wordSize);
 200 
 201   ExceptionHandlerTable handler_table;
 202   ImplicitExceptionTable inc_table;
 203 
 204   env->register_method(target,
 205                        entry_bci,
 206                        &offsets,
 207                        0,
 208                        &hscb,
 209                        0,
 210                        &oopmaps,
 211                        &handler_table,
 212                        &inc_table,
 213                        this,
 214                        env->comp_level(),
 215                        false,
 216                        false);
 217 }
 218 
 219 nmethod* SharkCompiler::generate_native_wrapper(MacroAssembler* masm,
 220                                                 methodHandle    target,
 221                                                 int             compile_id,
 222                                                 BasicType*      arg_types,
 223                                                 BasicType       return_type) {
 224   assert(is_initialized(), "should be");
 225   ResourceMark rm;
 226   const char *name = methodname(
 227     target->klass_name()->as_utf8(), target->name()->as_utf8());
 228 
 229   // Create the code buffer and builder
 230   SharkCodeBuffer cb(masm);
 231   SharkBuilder builder(&cb);
 232 
 233   // Emit the entry point
 234   SharkEntry *entry = (SharkEntry *) cb.malloc(sizeof(SharkEntry));
 235 
 236   // Build the LLVM IR for the method
 237   SharkNativeWrapper *wrapper = SharkNativeWrapper::build(
 238     &builder, target, name, arg_types, return_type);
 239 
 240   // Generate native code
 241   generate_native_code(entry, wrapper->function(), name);
 242 
 243   // Return the nmethod for installation in the VM
 244   return nmethod::new_native_nmethod(target,
 245                                      compile_id,
 246                                      masm->code(),
 247                                      0,
 248                                      0,
 249                                      wrapper->frame_size(),
 250                                      wrapper->receiver_offset(),
 251                                      wrapper->lock_offset(),
 252                                      wrapper->oop_maps());
 253 }
 254 
 255 void SharkCompiler::generate_native_code(SharkEntry* entry,
 256                                          Function*   function,
 257                                          const char* name) {
 258   // Print the LLVM bitcode, if requested
 259   if (SharkPrintBitcodeOf != NULL) {
 260     if (!fnmatch(SharkPrintBitcodeOf, name, 0))
 261       function->dump();
 262   }
 263 
 264   // Compile to native code
 265   address code = NULL;
 266   context()->add_function(function);
 267   {
 268     MutexLocker locker(execution_engine_lock());
 269     free_queued_methods();
 270 
 271     if (SharkPrintAsmOf != NULL) {
 272 #ifndef NDEBUG
 273       if (!fnmatch(SharkPrintAsmOf, name, 0)) {
 274         llvm::SetCurrentDebugType(X86_ONLY("x86-emitter") NOT_X86("jit"));
 275         llvm::DebugFlag = true;
 276       }
 277       else {
 278         llvm::SetCurrentDebugType("");
 279         llvm::DebugFlag = false;
 280       }
 281 #endif // !NDEBUG
 282     }
 283     memory_manager()->set_entry_for_function(function, entry);
 284     code = (address) execution_engine()->getPointerToFunction(function);
 285   }
 286   assert(code != NULL, "code must be != NULL");
 287   entry->set_entry_point(code);
 288   entry->set_function(function);
 289   entry->set_context(context());
 290   address code_start = entry->code_start();
 291   address code_limit = entry->code_limit();
 292 
 293   // Register generated code for profiling, etc
 294   if (JvmtiExport::should_post_dynamic_code_generated())
 295     JvmtiExport::post_dynamic_code_generated(name, code_start, code_limit);
 296 
 297   // Print debug information, if requested
 298   if (SharkTraceInstalls) {
 299     tty->print_cr(
 300       " [%p-%p): %s (%d bytes code)",
 301       code_start, code_limit, name, code_limit - code_start);
 302   }
 303 }
 304 
 305 void SharkCompiler::free_compiled_method(address code) {
 306   // This method may only be called when the VM is at a safepoint.
 307   // All _thread_in_vm threads will be waiting for the safepoint to
 308   // finish with the exception of the VM thread, so we can consider
 309   // ourself the owner of the execution engine lock even though we
 310   // can't actually acquire it at this time.
 311   assert(Thread::current()->is_Compiler_thread(), "must be called by compiler thread");
 312   assert_locked_or_safepoint(CodeCache_lock);
 313 
 314   SharkEntry *entry = (SharkEntry *) code;
 315   entry->context()->push_to_free_queue(entry->function());
 316 }
 317 
 318 void SharkCompiler::free_queued_methods() {
 319   // The free queue is protected by the execution engine lock
 320   assert(execution_engine_lock()->owned_by_self(), "should be");
 321 
 322   while (true) {
 323     Function *function = context()->pop_from_free_queue();
 324     if (function == NULL)
 325       break;
 326 
 327     execution_engine()->freeMachineCodeForFunction(function);
 328     function->eraseFromParent();
 329   }
 330 }
 331 
 332 const char* SharkCompiler::methodname(const char* klass, const char* method) {
 333   char *buf = NEW_RESOURCE_ARRAY(char, strlen(klass) + 2 + strlen(method) + 1);
 334 
 335   char *dst = buf;
 336   for (const char *c = klass; *c; c++) {
 337     if (*c == '/')
 338       *(dst++) = '.';
 339     else
 340       *(dst++) = *c;
 341   }
 342   *(dst++) = ':';
 343   *(dst++) = ':';
 344   for (const char *c = method; *c; c++) {
 345     *(dst++) = *c;
 346   }
 347   *(dst++) = '\0';
 348   return buf;
 349 }