blob: e99c1968c826864c563d10e514dcf8cb0383b3f4 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstrom265091e2013-01-30 14:08:26 -080017#include <llvm/Support/Threading.h>
18
Ian Rogers1212a022013-03-04 10:48:41 -080019#include "compiler/driver/compiler_driver.h"
buzbeeefc63692012-11-14 16:31:52 -080020#include "compiler_internals.h"
buzbee311ca162013-02-28 15:56:43 -080021#include "dataflow_iterator.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080022#if defined(ART_USE_PORTABLE_COMPILER)
23#include "compiler/llvm/llvm_compilation_unit.h"
buzbee1fd33462013-03-25 13:40:45 -070024#include "compiler/dex/portable/mir_to_gbc.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080025#endif
Ian Rogers0571d352011-11-03 19:51:38 -070026#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "runtime.h"
buzbee1fd33462013-03-25 13:40:45 -070029#include "backend.h"
buzbee67bf8852011-08-17 17:51:35 -070030
buzbee4df2bbd2012-10-11 14:46:06 -070031namespace {
Ian Rogersc928de92013-02-27 14:30:44 -080032#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070033 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
Shih-wei Liao215a9262012-10-12 10:29:46 -070034#endif
buzbee4df2bbd2012-10-11 14:46:06 -070035 void InitializeLLVMForQuick() {
Ian Rogers4c1c2832013-03-04 18:30:13 -080036 ::llvm::llvm_start_multithreaded();
buzbee4df2bbd2012-10-11 14:46:06 -070037 }
38}
buzbee4df2bbd2012-10-11 14:46:06 -070039
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080040namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080041namespace llvm {
42::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Ian Rogers76ae4fe2013-02-27 16:03:41 -080043}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080044
buzbee4df2bbd2012-10-11 14:46:06 -070045LLVMInfo::LLVMInfo() {
Ian Rogersc928de92013-02-27 14:30:44 -080046#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070047 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
48#endif
buzbee692be802012-08-29 15:52:59 -070049 // Create context, module, intrinsic helper & ir builder
Ian Rogers4c1c2832013-03-04 18:30:13 -080050 llvm_context_.reset(new ::llvm::LLVMContext());
51 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
52 ::llvm::StructType::create(*llvm_context_, "JavaObject");
53 art::llvm::makeLLVMModuleContents(llvm_module_);
54 intrinsic_helper_.reset( new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
55 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
buzbee692be802012-08-29 15:52:59 -070056}
57
buzbee4df2bbd2012-10-11 14:46:06 -070058LLVMInfo::~LLVMInfo() {
buzbee692be802012-08-29 15:52:59 -070059}
60
Ian Rogers1212a022013-03-04 10:48:41 -080061extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee692be802012-08-29 15:52:59 -070062 CHECK(compiler.GetCompilerContext() == NULL);
buzbeefa57c472012-11-21 12:06:18 -080063 LLVMInfo* llvm_info = new LLVMInfo();
64 compiler.SetCompilerContext(llvm_info);
buzbee692be802012-08-29 15:52:59 -070065}
66
Ian Rogers1212a022013-03-04 10:48:41 -080067extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee4df2bbd2012-10-11 14:46:06 -070068 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
buzbee692be802012-08-29 15:52:59 -070069 compiler.SetCompilerContext(NULL);
70}
buzbee692be802012-08-29 15:52:59 -070071
buzbeece302932011-10-04 14:32:18 -070072/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070073static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee4ef3e452012-12-14 13:35:28 -080074 (1 << kLoadStoreElimination) |
Bill Buzbeea114add2012-05-03 15:00:40 -070075 //(1 << kLoadHoisting) |
76 //(1 << kSuppressLoads) |
77 //(1 << kNullCheckElimination) |
78 //(1 << kPromoteRegs) |
79 //(1 << kTrackLiveTemps) |
Bill Buzbeea114add2012-05-03 15:00:40 -070080 //(1 << kSafeOptimizations) |
81 //(1 << kBBOpt) |
82 //(1 << kMatch) |
83 //(1 << kPromoteCompilerTemps) |
84 0;
buzbeece302932011-10-04 14:32:18 -070085
Elliott Hughese52e49b2012-04-02 16:05:44 -070086static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070087 //(1 << kDebugDisplayMissingTargets) |
88 //(1 << kDebugVerbose) |
89 //(1 << kDebugDumpCFG) |
90 //(1 << kDebugSlowFieldPath) |
91 //(1 << kDebugSlowInvokePath) |
92 //(1 << kDebugSlowStringPath) |
93 //(1 << kDebugSlowestFieldPath) |
94 //(1 << kDebugSlowestStringPath) |
95 //(1 << kDebugExerciseResolveMethod) |
96 //(1 << kDebugVerifyDataflow) |
97 //(1 << kDebugShowMemoryUsage) |
98 //(1 << kDebugShowNops) |
99 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -0700100 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -0700101 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700102 //(1 << kDebugVerifyBitcode) |
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 0;
buzbeece302932011-10-04 14:32:18 -0700104
Ian Rogers1212a022013-03-04 10:48:41 -0800105static CompiledMethod* CompileMethod(CompilerDriver& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800106 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800107 const DexFile::CodeItem* code_item,
108 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800109 uint32_t class_def_idx, uint32_t method_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800110 jobject class_loader, const DexFile& dex_file
111#if defined(ART_USE_PORTABLE_COMPILER)
112 , llvm::LlvmCompilationUnit* llvm_compilation_unit
113#endif
114)
buzbee67bf8852011-08-17 17:51:35 -0700115{
Bill Buzbeea114add2012-05-03 15:00:40 -0700116 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700117
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800119 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800120
buzbee311ca162013-02-28 15:56:43 -0800121 if (!HeapInit(cu.get())) {
122 LOG(FATAL) << "Failed to initialize compiler heap";
123 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800124
Ian Rogers1212a022013-03-04 10:48:41 -0800125 cu->compiler_driver = &compiler;
buzbeefa57c472012-11-21 12:06:18 -0800126 cu->class_linker = class_linker;
buzbeefa57c472012-11-21 12:06:18 -0800127 cu->instruction_set = compiler.GetInstructionSet();
buzbee1fd33462013-03-25 13:40:45 -0700128 cu->compiler_backend = compiler_backend;
buzbeefa57c472012-11-21 12:06:18 -0800129 DCHECK((cu->instruction_set == kThumb2) ||
130 (cu->instruction_set == kX86) ||
131 (cu->instruction_set == kMips));
buzbee311ca162013-02-28 15:56:43 -0800132
buzbee311ca162013-02-28 15:56:43 -0800133
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 /* Adjust this value accordingly once inlining is performed */
buzbeefa57c472012-11-21 12:06:18 -0800135 cu->num_dalvik_registers = code_item->registers_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 // TODO: set this from command line
buzbeefa57c472012-11-21 12:06:18 -0800137 cu->compiler_flip_match = false;
138 bool use_match = !cu->compiler_method_match.empty();
139 bool match = use_match && (cu->compiler_flip_match ^
140 (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) !=
Bill Buzbeea114add2012-05-03 15:00:40 -0700141 std::string::npos));
buzbeefa57c472012-11-21 12:06:18 -0800142 if (!use_match || match) {
143 cu->disable_opt = kCompilerOptimizerDisableFlags;
144 cu->enable_debug = kCompilerDebugFlags;
145 cu->verbose = VLOG_IS_ON(compiler) ||
146 (cu->enable_debug & (1 << kDebugVerbose));
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 }
buzbee311ca162013-02-28 15:56:43 -0800148
buzbee1fd33462013-03-25 13:40:45 -0700149 /*
150 * TODO: rework handling of optimization and debug flags. Should we split out
151 * MIR and backend flags? Need command-line setting as well.
152 */
153
154 if (compiler_backend == kPortable) {
155 // Fused long branches not currently usseful in bitcode.
156 cu->disable_opt |= (1 << kBranchFusing);
buzbee6969d502012-06-15 16:40:31 -0700157 }
buzbee9281f002012-10-24 12:17:24 -0700158
buzbeefa57c472012-11-21 12:06:18 -0800159 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700160 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800161 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700162 (1 << kLoadStoreElimination) |
163 (1 << kLoadHoisting) |
164 (1 << kSuppressLoads) |
165 (1 << kNullCheckElimination) |
166 (1 << kPromoteRegs) |
167 (1 << kTrackLiveTemps) |
jeffhao7fbee072012-08-24 17:56:54 -0700168 (1 << kSafeOptimizations) |
169 (1 << kBBOpt) |
170 (1 << kMatch) |
171 (1 << kPromoteCompilerTemps));
172 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700173
buzbee311ca162013-02-28 15:56:43 -0800174 cu->mir_graph.reset(new MIRGraph(cu.get()));
175
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 /* Gathering opcode stats? */
177 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbee311ca162013-02-28 15:56:43 -0800178 cu->mir_graph->EnableOpcodeCounting();
Bill Buzbeea114add2012-05-03 15:00:40 -0700179 }
180
buzbee311ca162013-02-28 15:56:43 -0800181 /* Build the raw MIR graph */
182 cu->mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
183 class_loader, dex_file);
buzbee67bf8852011-08-17 17:51:35 -0700184
buzbee0967a252012-09-14 10:43:54 -0700185 /* Do a code layout pass */
buzbee311ca162013-02-28 15:56:43 -0800186 cu->mir_graph->CodeLayout();
buzbeed8506212012-12-20 14:15:05 -0800187
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 /* Perform SSA transformation for the whole method */
buzbee311ca162013-02-28 15:56:43 -0800189 cu->mir_graph->SSATransformation();
buzbeed8506212012-12-20 14:15:05 -0800190
buzbee2cfc6392012-05-07 14:51:40 -0700191 /* Do constant propagation */
buzbee311ca162013-02-28 15:56:43 -0800192 cu->mir_graph->PropagateConstants();
buzbee67bf8852011-08-17 17:51:35 -0700193
Bill Buzbeea114add2012-05-03 15:00:40 -0700194 /* Count uses */
buzbee311ca162013-02-28 15:56:43 -0800195 cu->mir_graph->MethodUseCount();
buzbee67bf8852011-08-17 17:51:35 -0700196
Bill Buzbeea114add2012-05-03 15:00:40 -0700197 /* Perform null check elimination */
buzbee311ca162013-02-28 15:56:43 -0800198 cu->mir_graph->NullCheckElimination();
buzbeed8506212012-12-20 14:15:05 -0800199
buzbeed1643e42012-09-05 14:06:51 -0700200 /* Combine basic blocks where possible */
buzbee311ca162013-02-28 15:56:43 -0800201 cu->mir_graph->BasicBlockCombine();
buzbeed8506212012-12-20 14:15:05 -0800202
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 /* Do some basic block optimizations */
buzbee311ca162013-02-28 15:56:43 -0800204 cu->mir_graph->BasicBlockOptimization();
buzbeed8506212012-12-20 14:15:05 -0800205
buzbeefa57c472012-11-21 12:06:18 -0800206 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
buzbee311ca162013-02-28 15:56:43 -0800207 cu->mir_graph->DumpCheckStats();
buzbeed1643e42012-09-05 14:06:51 -0700208 }
209
buzbee1fd33462013-03-25 13:40:45 -0700210 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
211 cu->mir_graph->ShowOpcodeStats();
212 }
213
buzbee311ca162013-02-28 15:56:43 -0800214 /* Set up regLocation[] array to describe values - one for each ssa_name. */
215 cu->mir_graph->BuildRegLocations();
buzbee2502e002012-12-31 16:05:53 -0800216
buzbee1fd33462013-03-25 13:40:45 -0700217 CompiledMethod* result = NULL;
218
Brian Carlstrom265091e2013-01-30 14:08:26 -0800219#if defined(ART_USE_PORTABLE_COMPILER)
buzbee1fd33462013-03-25 13:40:45 -0700220 if (compiler_backend == kPortable) {
221 cu->cg.reset(PortableCodeGenerator(cu.get(), cu->mir_graph.get(), llvm_compilation_unit));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800222 } else
223#endif
224 {
buzbee311ca162013-02-28 15:56:43 -0800225 switch (compiler.GetInstructionSet()) {
226 case kThumb2:
buzbee1fd33462013-03-25 13:40:45 -0700227 cu->cg.reset(ArmCodeGenerator(cu.get(), cu->mir_graph.get())); break;
buzbee311ca162013-02-28 15:56:43 -0800228 case kMips:
buzbee1fd33462013-03-25 13:40:45 -0700229 cu->cg.reset(MipsCodeGenerator(cu.get(), cu->mir_graph.get())); break;
buzbee311ca162013-02-28 15:56:43 -0800230 case kX86:
buzbee1fd33462013-03-25 13:40:45 -0700231 cu->cg.reset(X86CodeGenerator(cu.get(), cu->mir_graph.get())); break;
buzbee311ca162013-02-28 15:56:43 -0800232 default:
233 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
234 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700235 }
buzbee67bf8852011-08-17 17:51:35 -0700236
buzbee1fd33462013-03-25 13:40:45 -0700237 cu->cg->Materialize();
buzbee67bf8852011-08-17 17:51:35 -0700238
buzbee1fd33462013-03-25 13:40:45 -0700239 result = cu->cg->GetCompiledMethod();
buzbee67bf8852011-08-17 17:51:35 -0700240
buzbee1fd33462013-03-25 13:40:45 -0700241 if (result) {
242 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 } else {
buzbee1fd33462013-03-25 13:40:45 -0700244 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 }
buzbee5abfa3e2012-01-31 17:01:43 -0800246
247#ifdef WITH_MEMSTATS
buzbeefa57c472012-11-21 12:06:18 -0800248 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
249 DumpMemStats(cu.get());
Bill Buzbeea114add2012-05-03 15:00:40 -0700250 }
buzbee5abfa3e2012-01-31 17:01:43 -0800251#endif
buzbee67bf8852011-08-17 17:51:35 -0700252
buzbeefa57c472012-11-21 12:06:18 -0800253 ArenaReset(cu.get());
buzbeeba938cb2012-02-03 14:47:55 -0800254
Bill Buzbeea114add2012-05-03 15:00:40 -0700255 return result;
buzbee67bf8852011-08-17 17:51:35 -0700256}
257
Ian Rogers1212a022013-03-04 10:48:41 -0800258CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -0700259 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -0700260 const DexFile::CodeItem* code_item,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800261 uint32_t access_flags,
262 InvokeType invoke_type,
263 uint32_t class_def_idx,
264 uint32_t method_idx,
265 jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -0700266 const DexFile& dex_file,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800267 llvm::LlvmCompilationUnit* llvm_compilation_unit)
buzbeeabc4c6b2012-08-23 08:17:15 -0700268{
Ian Rogersfffdb022013-01-04 15:14:08 -0800269 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800270 method_idx, class_loader, dex_file
271#if defined(ART_USE_PORTABLE_COMPILER)
272 , llvm_compilation_unit
273#endif
274 );
buzbeeabc4c6b2012-08-23 08:17:15 -0700275}
276
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800277} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800278
Bill Buzbeea114add2012-05-03 15:00:40 -0700279extern "C" art::CompiledMethod*
Ian Rogers1212a022013-03-04 10:48:41 -0800280 ArtQuickCompileMethod(art::CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -0700281 const art::DexFile::CodeItem* code_item,
282 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800283 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -0700284 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800285{
buzbeec531cef2012-10-18 07:09:20 -0700286 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
287 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -0800288 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800289 class_def_idx, method_idx, class_loader, dex_file,
290 NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800291}