blob: 243395ad3d48ea5533f61bcc51ab354620d96ddb [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -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
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000017#include "compiler_backend.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include "compiler_internals.h"
19#include "driver/compiler_driver.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080020#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dataflow_iterator-inl.h"
22#include "leb128.h"
23#include "mirror/object.h"
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -080024#include "pass_driver.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "runtime.h"
26#include "backend.h"
27#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070028#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080029#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000030#include "dex/quick/dex_file_to_method_inliner_map.h"
31
Brian Carlstrom7940e442013-07-12 13:46:57 -070032namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070033
Vladimir Marko867a2b32013-12-10 13:01:13 +000034extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& driver) {
35 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070036}
37
Vladimir Marko867a2b32013-12-10 13:01:13 +000038extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& driver) {
Vladimir Marko5816ed42013-11-27 17:04:20 +000039 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070040}
41
42/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070043static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070044 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070045 // (1 << kLoadHoisting) |
46 // (1 << kSuppressLoads) |
47 // (1 << kNullCheckElimination) |
48 // (1 << kPromoteRegs) |
49 // (1 << kTrackLiveTemps) |
50 // (1 << kSafeOptimizations) |
51 // (1 << kBBOpt) |
52 // (1 << kMatch) |
53 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080054 // (1 << kSuppressExceptionEdges) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 0;
56
57static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070058 // (1 << kDebugDisplayMissingTargets) |
59 // (1 << kDebugVerbose) |
60 // (1 << kDebugDumpCFG) |
61 // (1 << kDebugSlowFieldPath) |
62 // (1 << kDebugSlowInvokePath) |
63 // (1 << kDebugSlowStringPath) |
64 // (1 << kDebugSlowestFieldPath) |
65 // (1 << kDebugSlowestStringPath) |
66 // (1 << kDebugExerciseResolveMethod) |
67 // (1 << kDebugVerifyDataflow) |
68 // (1 << kDebugShowMemoryUsage) |
69 // (1 << kDebugShowNops) |
70 // (1 << kDebugCountOpcodes) |
71 // (1 << kDebugDumpCheckStats) |
72 // (1 << kDebugDumpBitcodeFile) |
73 // (1 << kDebugVerifyBitcode) |
74 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070075 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070076 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 0;
78
Vladimir Marko25724ef2013-11-12 15:09:20 +000079CompilationUnit::CompilationUnit(ArenaPool* pool)
80 : compiler_driver(NULL),
81 class_linker(NULL),
82 dex_file(NULL),
83 class_loader(NULL),
84 class_def_idx(0),
85 method_idx(0),
86 code_item(NULL),
87 access_flags(0),
88 invoke_type(kDirect),
89 shorty(NULL),
90 disable_opt(0),
91 enable_debug(0),
92 verbose(false),
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000093 compiler_backend(NULL),
Vladimir Marko25724ef2013-11-12 15:09:20 +000094 instruction_set(kNone),
95 num_dalvik_registers(0),
96 insns(NULL),
97 num_ins(0),
98 num_outs(0),
99 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000100 compiler_flip_match(false),
101 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000102 arena_stack(pool),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000103 mir_graph(NULL),
104 cg(NULL),
105 timings("QuickCompiler", true, false) {
106}
107
108CompilationUnit::~CompilationUnit() {
109}
110
buzbeea61f4952013-08-23 14:27:06 -0700111void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000112 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700113 timings.StartSplit(label);
114 }
115}
116
117void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000118 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700119 timings.NewSplit(label);
120 }
121}
122
123void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000124 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700125 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000126 if (enable_debug & (1 << kDebugTimings)) {
127 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
128 LOG(INFO) << Dumpable<TimingLogger>(timings);
129 }
buzbeea61f4952013-08-23 14:27:06 -0700130 }
131}
132
Ian Rogers3d504072014-03-01 09:16:49 -0800133static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000134 CompilerBackend* compiler_backend,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 const DexFile::CodeItem* code_item,
136 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700137 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000138 jobject class_loader, const DexFile& dex_file,
139 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700141 if (code_item->insns_size_in_code_units_ >= 0x10000) {
142 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
143 << " in " << PrettyMethod(method_idx, dex_file);
144 return NULL;
145 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146
147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800148 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149
Ian Rogers3d504072014-03-01 09:16:49 -0800150 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700151 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800152 cu.instruction_set = driver.GetInstructionSet();
Andreas Gampe2da88232014-02-27 12:26:20 -0800153 cu.target64 = cu.instruction_set == kX86_64;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700154 cu.compiler_backend = compiler_backend;
Andreas Gampe2da88232014-02-27 12:26:20 -0800155 // TODO: x86_64 is not yet implemented.
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700156 DCHECK((cu.instruction_set == kThumb2) ||
157 (cu.instruction_set == kX86) ||
158 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700159
160
161 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700162 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700164 cu.compiler_flip_match = false;
165 bool use_match = !cu.compiler_method_match.empty();
166 bool match = use_match && (cu.compiler_flip_match ^
167 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 std::string::npos));
169 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700170 cu.disable_opt = kCompilerOptimizerDisableFlags;
171 cu.enable_debug = kCompilerDebugFlags;
172 cu.verbose = VLOG_IS_ON(compiler) ||
173 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 }
175
176 /*
177 * TODO: rework handling of optimization and debug flags. Should we split out
178 * MIR and backend flags? Need command-line setting as well.
179 */
180
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000181 compiler_backend->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700183 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700185 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 (1 << kLoadStoreElimination) |
187 (1 << kLoadHoisting) |
188 (1 << kSuppressLoads) |
189 (1 << kNullCheckElimination) |
190 (1 << kPromoteRegs) |
191 (1 << kTrackLiveTemps) |
192 (1 << kSafeOptimizations) |
193 (1 << kBBOpt) |
194 (1 << kMatch) |
195 (1 << kPromoteCompilerTemps));
196 }
197
buzbeea61f4952013-08-23 14:27:06 -0700198 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700199 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800201 /*
202 * After creation of the MIR graph, also create the code generator.
203 * The reason we do this is that optimizations on the MIR graph may need to get information
204 * that is only available if a CG exists.
205 */
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000206 cu.cg.reset(compiler_backend->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800207
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 /* Gathering opcode stats? */
209 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700210 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 }
212
Dave Allison39c3bfb2014-01-28 18:33:52 -0800213 const CompilerOptions& compiler_options = cu.compiler_driver->GetCompilerOptions();
214 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter();
215
216 // Check early if we should skip this compilation if using the profiled filter.
217 if (cu.compiler_driver->ProfilePresent()) {
218 std::string methodname = PrettyMethod(method_idx, dex_file);
219 if (cu.mir_graph->SkipCompilation(methodname)) {
220 return NULL;
221 }
222 }
223
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700225 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 class_loader, dex_file);
227
buzbee1da1e2f2013-11-15 13:37:01 -0800228 cu.NewTimingSplit("MIROpt:CheckFilters");
Dave Allison39c3bfb2014-01-28 18:33:52 -0800229 if (compiler_filter != CompilerOptions::kInterpretOnly) {
230 if (cu.mir_graph->SkipCompilation()) {
231 return NULL;
232 }
buzbeeee17e0a2013-07-31 10:47:37 -0700233 }
234
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800235 /* Create the pass driver and launch it */
Ian Rogers3d504072014-03-01 09:16:49 -0800236 PassDriver pass_driver(&cu);
237 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700239 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
240 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
242
243 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700244 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 }
246
buzbee1da1e2f2013-11-15 13:37:01 -0800247 /* Reassociate sreg names with original Dalvik vreg names. */
248 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249
250 CompiledMethod* result = NULL;
251
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700252 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253
buzbee1da1e2f2013-11-15 13:37:01 -0800254 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700255 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800256 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257
258 if (result) {
259 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
260 } else {
261 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
262 }
263
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700264 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000265 if (cu.arena.BytesAllocated() > (1 * 1024 *1024) ||
266 cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
267 MemStats mem_stats(cu.arena.GetMemStats());
268 MemStats peak_stats(cu.arena_stack.GetPeakStats());
269 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats)
270 << Dumpable<MemStats>(peak_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 }
272 }
273
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700274 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
275 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 << " " << PrettyMethod(method_idx, dex_file);
277 }
278
buzbeea61f4952013-08-23 14:27:06 -0700279 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800280 driver.GetTimingsLogger()->Start();
281 driver.GetTimingsLogger()->AddLogger(cu.timings);
282 driver.GetTimingsLogger()->End();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 return result;
284}
285
286CompiledMethod* CompileOneMethod(CompilerDriver& compiler,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000287 CompilerBackend* backend,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 const DexFile::CodeItem* code_item,
289 uint32_t access_flags,
290 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700291 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 uint32_t method_idx,
293 jobject class_loader,
294 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000295 void* compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700296 return CompileMethod(compiler, backend, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000297 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298}
299
300} // namespace art
301
302extern "C" art::CompiledMethod*
303 ArtQuickCompileMethod(art::CompilerDriver& compiler,
304 const art::DexFile::CodeItem* code_item,
305 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 const art::DexFile& dex_file) {
308 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000309 art::CompilerBackend* backend = compiler.GetCompilerBackend();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 return art::CompileOneMethod(compiler, backend, code_item, access_flags, invoke_type,
311 class_def_idx, method_idx, class_loader, dex_file,
312 NULL /* use thread llvm_info */);
313}