comparison src/share/vm/shark/sharkCompiler.cpp @ 7212:291ffc492eb6

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/
author Doug Simon <doug.simon@oracle.com>
date Fri, 14 Dec 2012 14:35:13 +0100
parents 5d0bb7d52783 2cd5e15048e6
children 3ac7d10a6572
comparison
equal deleted inserted replaced
7163:2ed8d74e5984 7212:291ffc492eb6
46 46
47 #include <fnmatch.h> 47 #include <fnmatch.h>
48 48
49 using namespace llvm; 49 using namespace llvm;
50 50
51 #if SHARK_LLVM_VERSION >= 27
52 namespace { 51 namespace {
53 cl::opt<std::string> 52 cl::opt<std::string>
54 MCPU("mcpu"); 53 MCPU("mcpu");
55 54
56 cl::list<std::string> 55 cl::list<std::string>
57 MAttrs("mattr", 56 MAttrs("mattr",
58 cl::CommaSeparated); 57 cl::CommaSeparated);
59 } 58 }
60 #endif
61 59
62 SharkCompiler::SharkCompiler() 60 SharkCompiler::SharkCompiler()
63 : AbstractCompiler(shark) { 61 : AbstractCompiler(shark) {
64 // Create the lock to protect the memory manager and execution engine 62 // Create the lock to protect the memory manager and execution engine
65 _execution_engine_lock = new Monitor(Mutex::leaf, "SharkExecutionEngineLock"); 63 _execution_engine_lock = new Monitor(Mutex::leaf, "SharkExecutionEngineLock");
70 fatal("llvm_start_multithreaded() failed"); 68 fatal("llvm_start_multithreaded() failed");
71 69
72 // Initialize the native target 70 // Initialize the native target
73 InitializeNativeTarget(); 71 InitializeNativeTarget();
74 72
73 // MCJIT require a native AsmPrinter
74 InitializeNativeTargetAsmPrinter();
75
75 // Create the two contexts which we'll use 76 // Create the two contexts which we'll use
76 _normal_context = new SharkContext("normal"); 77 _normal_context = new SharkContext("normal");
77 _native_context = new SharkContext("native"); 78 _native_context = new SharkContext("native");
78 79
79 // Create the memory manager 80 // Create the memory manager
80 _memory_manager = new SharkMemoryManager(); 81 _memory_manager = new SharkMemoryManager();
81 82
82 #if SHARK_LLVM_VERSION >= 27
83 // Finetune LLVM for the current host CPU. 83 // Finetune LLVM for the current host CPU.
84 StringMap<bool> Features; 84 StringMap<bool> Features;
85 bool gotCpuFeatures = llvm::sys::getHostCPUFeatures(Features); 85 bool gotCpuFeatures = llvm::sys::getHostCPUFeatures(Features);
86 std::string cpu("-mcpu=" + llvm::sys::getHostCPUName()); 86 std::string cpu("-mcpu=" + llvm::sys::getHostCPUName());
87 87
111 builder.setMCPU(MCPU); 111 builder.setMCPU(MCPU);
112 builder.setMAttrs(MAttrs); 112 builder.setMAttrs(MAttrs);
113 builder.setJITMemoryManager(memory_manager()); 113 builder.setJITMemoryManager(memory_manager());
114 builder.setEngineKind(EngineKind::JIT); 114 builder.setEngineKind(EngineKind::JIT);
115 builder.setErrorStr(&ErrorMsg); 115 builder.setErrorStr(&ErrorMsg);
116 if (! fnmatch(SharkOptimizationLevel, "None", 0)) {
117 tty->print_cr("Shark optimization level set to: None");
118 builder.setOptLevel(llvm::CodeGenOpt::None);
119 } else if (! fnmatch(SharkOptimizationLevel, "Less", 0)) {
120 tty->print_cr("Shark optimization level set to: Less");
121 builder.setOptLevel(llvm::CodeGenOpt::Less);
122 } else if (! fnmatch(SharkOptimizationLevel, "Aggressive", 0)) {
123 tty->print_cr("Shark optimization level set to: Aggressive");
124 builder.setOptLevel(llvm::CodeGenOpt::Aggressive);
125 } // else Default is selected by, well, default :-)
116 _execution_engine = builder.create(); 126 _execution_engine = builder.create();
117 127
118 if (!execution_engine()) { 128 if (!execution_engine()) {
119 if (!ErrorMsg.empty()) 129 if (!ErrorMsg.empty())
120 printf("Error while creating Shark JIT: %s\n",ErrorMsg.c_str()); 130 printf("Error while creating Shark JIT: %s\n",ErrorMsg.c_str());
123 exit(1); 133 exit(1);
124 } 134 }
125 135
126 execution_engine()->addModule( 136 execution_engine()->addModule(
127 _native_context->module()); 137 _native_context->module());
128 #else
129 _execution_engine = ExecutionEngine::createJIT(
130 _normal_context->module_provider(),
131 NULL, memory_manager(), CodeGenOpt::Default);
132 execution_engine()->addModuleProvider(
133 _native_context->module_provider());
134 #endif
135 138
136 // All done 139 // All done
137 mark_initialized(); 140 mark_initialized();
138 } 141 }
139 142
259 if (SharkPrintBitcodeOf != NULL) { 262 if (SharkPrintBitcodeOf != NULL) {
260 if (!fnmatch(SharkPrintBitcodeOf, name, 0)) 263 if (!fnmatch(SharkPrintBitcodeOf, name, 0))
261 function->dump(); 264 function->dump();
262 } 265 }
263 266
267 if (SharkVerifyFunction != NULL) {
268 if (!fnmatch(SharkVerifyFunction, name, 0)) {
269 verifyFunction(*function);
270 }
271 }
272
264 // Compile to native code 273 // Compile to native code
265 address code = NULL; 274 address code = NULL;
266 context()->add_function(function); 275 context()->add_function(function);
267 { 276 {
268 MutexLocker locker(execution_engine_lock()); 277 MutexLocker locker(execution_engine_lock());
269 free_queued_methods(); 278 free_queued_methods();
270 279
280 #ifndef NDEBUG
281 #if SHARK_LLVM_VERSION <= 31
282 #define setCurrentDebugType SetCurrentDebugType
283 #endif
271 if (SharkPrintAsmOf != NULL) { 284 if (SharkPrintAsmOf != NULL) {
272 #if SHARK_LLVM_VERSION >= 27
273 #ifndef NDEBUG
274 if (!fnmatch(SharkPrintAsmOf, name, 0)) { 285 if (!fnmatch(SharkPrintAsmOf, name, 0)) {
275 llvm::SetCurrentDebugType(X86_ONLY("x86-emitter") NOT_X86("jit")); 286 llvm::setCurrentDebugType(X86_ONLY("x86-emitter") NOT_X86("jit"));
276 llvm::DebugFlag = true; 287 llvm::DebugFlag = true;
277 } 288 }
278 else { 289 else {
279 llvm::SetCurrentDebugType(""); 290 llvm::setCurrentDebugType("");
280 llvm::DebugFlag = false; 291 llvm::DebugFlag = false;
281 } 292 }
293 }
294 #ifdef setCurrentDebugType
295 #undef setCurrentDebugType
296 #endif
282 #endif // !NDEBUG 297 #endif // !NDEBUG
283 #else
284 // NB you need to patch LLVM with http://tinyurl.com/yf3baln for this
285 std::vector<const char*> args;
286 args.push_back(""); // program name
287 if (!fnmatch(SharkPrintAsmOf, name, 0))
288 args.push_back("-debug-only=x86-emitter");
289 else
290 args.push_back("-debug-only=none");
291 args.push_back(0); // terminator
292 cl::ParseCommandLineOptions(args.size() - 1, (char **) &args[0]);
293 #endif // SHARK_LLVM_VERSION
294 }
295 memory_manager()->set_entry_for_function(function, entry); 298 memory_manager()->set_entry_for_function(function, entry);
296 code = (address) execution_engine()->getPointerToFunction(function); 299 code = (address) execution_engine()->getPointerToFunction(function);
297 } 300 }
301 assert(code != NULL, "code must be != NULL");
298 entry->set_entry_point(code); 302 entry->set_entry_point(code);
299 entry->set_function(function); 303 entry->set_function(function);
300 entry->set_context(context()); 304 entry->set_context(context());
301 address code_start = entry->code_start(); 305 address code_start = entry->code_start();
302 address code_limit = entry->code_limit(); 306 address code_limit = entry->code_limit();
317 // This method may only be called when the VM is at a safepoint. 321 // This method may only be called when the VM is at a safepoint.
318 // All _thread_in_vm threads will be waiting for the safepoint to 322 // All _thread_in_vm threads will be waiting for the safepoint to
319 // finish with the exception of the VM thread, so we can consider 323 // finish with the exception of the VM thread, so we can consider
320 // ourself the owner of the execution engine lock even though we 324 // ourself the owner of the execution engine lock even though we
321 // can't actually acquire it at this time. 325 // can't actually acquire it at this time.
322 assert(Thread::current()->is_VM_thread(), "must be called by VM thread"); 326 assert(Thread::current()->is_Compiler_thread(), "must be called by compiler thread");
323 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 327 assert_locked_or_safepoint(CodeCache_lock);
324 328
325 SharkEntry *entry = (SharkEntry *) code; 329 SharkEntry *entry = (SharkEntry *) code;
326 entry->context()->push_to_free_queue(entry->function()); 330 entry->context()->push_to_free_queue(entry->function());
327 } 331 }
328 332