blob: e015645584b8e1faa48452282bd70e7ded2745d2 [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"
Ian Rogers8d3a1172013-06-04 01:13:28 -070021#include "dataflow_iterator-inl.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"
buzbee862a7602013-04-05 10:58:54 -070030#include "base/logging.h"
buzbee67bf8852011-08-17 17:51:35 -070031
buzbee4df2bbd2012-10-11 14:46:06 -070032namespace {
Ian Rogersc928de92013-02-27 14:30:44 -080033#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070034 pthread_once_t llvm_multi_init = PTHREAD_ONCE_INIT;
Shih-wei Liao215a9262012-10-12 10:29:46 -070035#endif
buzbee4df2bbd2012-10-11 14:46:06 -070036 void InitializeLLVMForQuick() {
Ian Rogers4c1c2832013-03-04 18:30:13 -080037 ::llvm::llvm_start_multithreaded();
buzbee4df2bbd2012-10-11 14:46:06 -070038 }
39}
buzbee4df2bbd2012-10-11 14:46:06 -070040
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080041namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080042namespace llvm {
43::llvm::Module* makeLLVMModuleContents(::llvm::Module* module);
Ian Rogers76ae4fe2013-02-27 16:03:41 -080044}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080045
buzbee4df2bbd2012-10-11 14:46:06 -070046LLVMInfo::LLVMInfo() {
Ian Rogersc928de92013-02-27 14:30:44 -080047#if !defined(ART_USE_PORTABLE_COMPILER)
buzbee4df2bbd2012-10-11 14:46:06 -070048 pthread_once(&llvm_multi_init, InitializeLLVMForQuick);
49#endif
buzbee692be802012-08-29 15:52:59 -070050 // Create context, module, intrinsic helper & ir builder
Ian Rogers4c1c2832013-03-04 18:30:13 -080051 llvm_context_.reset(new ::llvm::LLVMContext());
52 llvm_module_ = new ::llvm::Module("art", *llvm_context_);
53 ::llvm::StructType::create(*llvm_context_, "JavaObject");
54 art::llvm::makeLLVMModuleContents(llvm_module_);
55 intrinsic_helper_.reset( new art::llvm::IntrinsicHelper(*llvm_context_, *llvm_module_));
56 ir_builder_.reset(new art::llvm::IRBuilder(*llvm_context_, *llvm_module_, *intrinsic_helper_));
buzbee692be802012-08-29 15:52:59 -070057}
58
buzbee4df2bbd2012-10-11 14:46:06 -070059LLVMInfo::~LLVMInfo() {
buzbee692be802012-08-29 15:52:59 -070060}
61
Ian Rogers1212a022013-03-04 10:48:41 -080062extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee692be802012-08-29 15:52:59 -070063 CHECK(compiler.GetCompilerContext() == NULL);
buzbeefa57c472012-11-21 12:06:18 -080064 LLVMInfo* llvm_info = new LLVMInfo();
65 compiler.SetCompilerContext(llvm_info);
buzbee692be802012-08-29 15:52:59 -070066}
67
Ian Rogers1212a022013-03-04 10:48:41 -080068extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& compiler) {
buzbee4df2bbd2012-10-11 14:46:06 -070069 delete reinterpret_cast<LLVMInfo*>(compiler.GetCompilerContext());
buzbee692be802012-08-29 15:52:59 -070070 compiler.SetCompilerContext(NULL);
71}
buzbee692be802012-08-29 15:52:59 -070072
buzbeece302932011-10-04 14:32:18 -070073/* Default optimizer/debug setting for the compiler. */
Elliott Hughese52e49b2012-04-02 16:05:44 -070074static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee4ef3e452012-12-14 13:35:28 -080075 (1 << kLoadStoreElimination) |
Bill Buzbeea114add2012-05-03 15:00:40 -070076 //(1 << kLoadHoisting) |
77 //(1 << kSuppressLoads) |
78 //(1 << kNullCheckElimination) |
79 //(1 << kPromoteRegs) |
80 //(1 << kTrackLiveTemps) |
Bill Buzbeea114add2012-05-03 15:00:40 -070081 //(1 << kSafeOptimizations) |
82 //(1 << kBBOpt) |
83 //(1 << kMatch) |
84 //(1 << kPromoteCompilerTemps) |
85 0;
buzbeece302932011-10-04 14:32:18 -070086
Elliott Hughese52e49b2012-04-02 16:05:44 -070087static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Bill Buzbeea114add2012-05-03 15:00:40 -070088 //(1 << kDebugDisplayMissingTargets) |
89 //(1 << kDebugVerbose) |
90 //(1 << kDebugDumpCFG) |
91 //(1 << kDebugSlowFieldPath) |
92 //(1 << kDebugSlowInvokePath) |
93 //(1 << kDebugSlowStringPath) |
94 //(1 << kDebugSlowestFieldPath) |
95 //(1 << kDebugSlowestStringPath) |
96 //(1 << kDebugExerciseResolveMethod) |
97 //(1 << kDebugVerifyDataflow) |
98 //(1 << kDebugShowMemoryUsage) |
99 //(1 << kDebugShowNops) |
100 //(1 << kDebugCountOpcodes) |
buzbeed1643e42012-09-05 14:06:51 -0700101 //(1 << kDebugDumpCheckStats) |
buzbeead8f15e2012-06-18 14:49:45 -0700102 //(1 << kDebugDumpBitcodeFile) |
Bill Buzbeec9f40dd2012-08-15 11:35:25 -0700103 //(1 << kDebugVerifyBitcode) |
buzbeea5abf702013-04-12 14:39:29 -0700104 //(1 << kDebugShowSummaryMemoryUsage) |
Bill Buzbeea114add2012-05-03 15:00:40 -0700105 0;
buzbeece302932011-10-04 14:32:18 -0700106
Ian Rogers1212a022013-03-04 10:48:41 -0800107static CompiledMethod* CompileMethod(CompilerDriver& compiler,
buzbeefa57c472012-11-21 12:06:18 -0800108 const CompilerBackend compiler_backend,
buzbee52a77fc2012-11-20 19:50:46 -0800109 const DexFile::CodeItem* code_item,
110 uint32_t access_flags, InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800111 uint32_t class_def_idx, uint32_t method_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800112 jobject class_loader, const DexFile& dex_file
113#if defined(ART_USE_PORTABLE_COMPILER)
114 , llvm::LlvmCompilationUnit* llvm_compilation_unit
115#endif
116)
buzbee67bf8852011-08-17 17:51:35 -0700117{
Bill Buzbeea114add2012-05-03 15:00:40 -0700118 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Brian Carlstrom94496d32011-08-22 09:22:47 -0700119
Bill Buzbeea114add2012-05-03 15:00:40 -0700120 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
buzbeefa57c472012-11-21 12:06:18 -0800121 UniquePtr<CompilationUnit> cu(new CompilationUnit);
buzbeeba938cb2012-02-03 14:47:55 -0800122
Ian Rogers1212a022013-03-04 10:48:41 -0800123 cu->compiler_driver = &compiler;
buzbeefa57c472012-11-21 12:06:18 -0800124 cu->class_linker = class_linker;
buzbeefa57c472012-11-21 12:06:18 -0800125 cu->instruction_set = compiler.GetInstructionSet();
buzbee1fd33462013-03-25 13:40:45 -0700126 cu->compiler_backend = compiler_backend;
buzbeefa57c472012-11-21 12:06:18 -0800127 DCHECK((cu->instruction_set == kThumb2) ||
128 (cu->instruction_set == kX86) ||
129 (cu->instruction_set == kMips));
buzbee311ca162013-02-28 15:56:43 -0800130
buzbee311ca162013-02-28 15:56:43 -0800131
Bill Buzbeea114add2012-05-03 15:00:40 -0700132 /* Adjust this value accordingly once inlining is performed */
buzbeefa57c472012-11-21 12:06:18 -0800133 cu->num_dalvik_registers = code_item->registers_size_;
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 // TODO: set this from command line
buzbeefa57c472012-11-21 12:06:18 -0800135 cu->compiler_flip_match = false;
136 bool use_match = !cu->compiler_method_match.empty();
137 bool match = use_match && (cu->compiler_flip_match ^
138 (PrettyMethod(method_idx, dex_file).find(cu->compiler_method_match) !=
Bill Buzbeea114add2012-05-03 15:00:40 -0700139 std::string::npos));
buzbeefa57c472012-11-21 12:06:18 -0800140 if (!use_match || match) {
141 cu->disable_opt = kCompilerOptimizerDisableFlags;
142 cu->enable_debug = kCompilerDebugFlags;
143 cu->verbose = VLOG_IS_ON(compiler) ||
144 (cu->enable_debug & (1 << kDebugVerbose));
Bill Buzbeea114add2012-05-03 15:00:40 -0700145 }
buzbee311ca162013-02-28 15:56:43 -0800146
buzbee1fd33462013-03-25 13:40:45 -0700147 /*
148 * TODO: rework handling of optimization and debug flags. Should we split out
149 * MIR and backend flags? Need command-line setting as well.
150 */
151
152 if (compiler_backend == kPortable) {
153 // Fused long branches not currently usseful in bitcode.
154 cu->disable_opt |= (1 << kBranchFusing);
buzbee6969d502012-06-15 16:40:31 -0700155 }
buzbee9281f002012-10-24 12:17:24 -0700156
buzbeefa57c472012-11-21 12:06:18 -0800157 if (cu->instruction_set == kMips) {
jeffhao7fbee072012-08-24 17:56:54 -0700158 // Disable some optimizations for mips for now
buzbeefa57c472012-11-21 12:06:18 -0800159 cu->disable_opt |= (
jeffhao7fbee072012-08-24 17:56:54 -0700160 (1 << kLoadStoreElimination) |
161 (1 << kLoadHoisting) |
162 (1 << kSuppressLoads) |
163 (1 << kNullCheckElimination) |
164 (1 << kPromoteRegs) |
165 (1 << kTrackLiveTemps) |
jeffhao7fbee072012-08-24 17:56:54 -0700166 (1 << kSafeOptimizations) |
167 (1 << kBBOpt) |
168 (1 << kMatch) |
169 (1 << kPromoteCompilerTemps));
170 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700171
buzbee862a7602013-04-05 10:58:54 -0700172 cu->mir_graph.reset(new MIRGraph(cu.get(), &cu->arena));
buzbee311ca162013-02-28 15:56:43 -0800173
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 /* Gathering opcode stats? */
175 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
buzbee311ca162013-02-28 15:56:43 -0800176 cu->mir_graph->EnableOpcodeCounting();
Bill Buzbeea114add2012-05-03 15:00:40 -0700177 }
178
buzbee311ca162013-02-28 15:56:43 -0800179 /* Build the raw MIR graph */
180 cu->mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
181 class_loader, dex_file);
buzbee67bf8852011-08-17 17:51:35 -0700182
buzbee0967a252012-09-14 10:43:54 -0700183 /* Do a code layout pass */
buzbee311ca162013-02-28 15:56:43 -0800184 cu->mir_graph->CodeLayout();
buzbeed8506212012-12-20 14:15:05 -0800185
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 /* Perform SSA transformation for the whole method */
buzbee311ca162013-02-28 15:56:43 -0800187 cu->mir_graph->SSATransformation();
buzbeed8506212012-12-20 14:15:05 -0800188
buzbee2cfc6392012-05-07 14:51:40 -0700189 /* Do constant propagation */
buzbee311ca162013-02-28 15:56:43 -0800190 cu->mir_graph->PropagateConstants();
buzbee67bf8852011-08-17 17:51:35 -0700191
Bill Buzbeea114add2012-05-03 15:00:40 -0700192 /* Count uses */
buzbee311ca162013-02-28 15:56:43 -0800193 cu->mir_graph->MethodUseCount();
buzbee67bf8852011-08-17 17:51:35 -0700194
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 /* Perform null check elimination */
buzbee311ca162013-02-28 15:56:43 -0800196 cu->mir_graph->NullCheckElimination();
buzbeed8506212012-12-20 14:15:05 -0800197
buzbeed1643e42012-09-05 14:06:51 -0700198 /* Combine basic blocks where possible */
buzbee311ca162013-02-28 15:56:43 -0800199 cu->mir_graph->BasicBlockCombine();
buzbeed8506212012-12-20 14:15:05 -0800200
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 /* Do some basic block optimizations */
buzbee311ca162013-02-28 15:56:43 -0800202 cu->mir_graph->BasicBlockOptimization();
buzbeed8506212012-12-20 14:15:05 -0800203
buzbeefa57c472012-11-21 12:06:18 -0800204 if (cu->enable_debug & (1 << kDebugDumpCheckStats)) {
buzbee311ca162013-02-28 15:56:43 -0800205 cu->mir_graph->DumpCheckStats();
buzbeed1643e42012-09-05 14:06:51 -0700206 }
207
buzbee1fd33462013-03-25 13:40:45 -0700208 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
209 cu->mir_graph->ShowOpcodeStats();
210 }
211
buzbee311ca162013-02-28 15:56:43 -0800212 /* Set up regLocation[] array to describe values - one for each ssa_name. */
213 cu->mir_graph->BuildRegLocations();
buzbee2502e002012-12-31 16:05:53 -0800214
buzbee1fd33462013-03-25 13:40:45 -0700215 CompiledMethod* result = NULL;
216
Brian Carlstrom265091e2013-01-30 14:08:26 -0800217#if defined(ART_USE_PORTABLE_COMPILER)
buzbee1fd33462013-03-25 13:40:45 -0700218 if (compiler_backend == kPortable) {
buzbee862a7602013-04-05 10:58:54 -0700219 cu->cg.reset(PortableCodeGenerator(cu.get(), cu->mir_graph.get(), &cu->arena,
220 llvm_compilation_unit));
Brian Carlstrom265091e2013-01-30 14:08:26 -0800221 } else
222#endif
223 {
buzbee311ca162013-02-28 15:56:43 -0800224 switch (compiler.GetInstructionSet()) {
225 case kThumb2:
buzbee862a7602013-04-05 10:58:54 -0700226 cu->cg.reset(ArmCodeGenerator(cu.get(), cu->mir_graph.get(), &cu->arena)); break;
buzbee311ca162013-02-28 15:56:43 -0800227 case kMips:
buzbee862a7602013-04-05 10:58:54 -0700228 cu->cg.reset(MipsCodeGenerator(cu.get(), cu->mir_graph.get(), &cu->arena)); break;
buzbee311ca162013-02-28 15:56:43 -0800229 case kX86:
buzbee862a7602013-04-05 10:58:54 -0700230 cu->cg.reset(X86CodeGenerator(cu.get(), cu->mir_graph.get(), &cu->arena)); break;
buzbee311ca162013-02-28 15:56:43 -0800231 default:
232 LOG(FATAL) << "Unexpected instruction set: " << compiler.GetInstructionSet();
233 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700234 }
buzbee67bf8852011-08-17 17:51:35 -0700235
buzbee1fd33462013-03-25 13:40:45 -0700236 cu->cg->Materialize();
buzbee67bf8852011-08-17 17:51:35 -0700237
buzbee1fd33462013-03-25 13:40:45 -0700238 result = cu->cg->GetCompiledMethod();
buzbee67bf8852011-08-17 17:51:35 -0700239
buzbee1fd33462013-03-25 13:40:45 -0700240 if (result) {
241 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700242 } else {
buzbee1fd33462013-03-25 13:40:45 -0700243 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
Bill Buzbeea114add2012-05-03 15:00:40 -0700244 }
buzbee5abfa3e2012-01-31 17:01:43 -0800245
buzbeefa57c472012-11-21 12:06:18 -0800246 if (cu->enable_debug & (1 << kDebugShowMemoryUsage)) {
buzbee862a7602013-04-05 10:58:54 -0700247 if (cu->arena.BytesAllocated() > (5 * 1024 *1024)) {
248 MemStats mem_stats(cu->arena);
249 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
250 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700251 }
buzbee67bf8852011-08-17 17:51:35 -0700252
buzbeea5abf702013-04-12 14:39:29 -0700253 if (cu->enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
254 LOG(INFO) << "MEMINFO " << cu->arena.BytesAllocated() << " " << cu->mir_graph->GetNumBlocks()
255 << " " << PrettyMethod(method_idx, dex_file);
256 }
257
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 return result;
buzbee67bf8852011-08-17 17:51:35 -0700259}
260
Ian Rogers1212a022013-03-04 10:48:41 -0800261CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -0700262 const CompilerBackend backend,
buzbeeabc4c6b2012-08-23 08:17:15 -0700263 const DexFile::CodeItem* code_item,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800264 uint32_t access_flags,
265 InvokeType invoke_type,
266 uint32_t class_def_idx,
267 uint32_t method_idx,
268 jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -0700269 const DexFile& dex_file,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800270 llvm::LlvmCompilationUnit* llvm_compilation_unit)
buzbeeabc4c6b2012-08-23 08:17:15 -0700271{
Ian Rogersfffdb022013-01-04 15:14:08 -0800272 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800273 method_idx, class_loader, dex_file
274#if defined(ART_USE_PORTABLE_COMPILER)
275 , llvm_compilation_unit
276#endif
277 );
buzbeeabc4c6b2012-08-23 08:17:15 -0700278}
279
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800280} // namespace art
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800281
Bill Buzbeea114add2012-05-03 15:00:40 -0700282extern "C" art::CompiledMethod*
Ian Rogers1212a022013-03-04 10:48:41 -0800283 ArtQuickCompileMethod(art::CompilerDriver& compiler,
buzbeec531cef2012-10-18 07:09:20 -0700284 const art::DexFile::CodeItem* code_item,
285 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800286 uint32_t class_def_idx, uint32_t method_idx, jobject class_loader,
buzbeec531cef2012-10-18 07:09:20 -0700287 const art::DexFile& dex_file)
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800288{
buzbeec531cef2012-10-18 07:09:20 -0700289 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
290 art::CompilerBackend backend = compiler.GetCompilerBackend();
buzbee52a77fc2012-11-20 19:50:46 -0800291 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
Ian Rogersfffdb022013-01-04 15:14:08 -0800292 class_def_idx, method_idx, class_loader, dex_file,
293 NULL /* use thread llvm_info */);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800294}