blob: e48e5bf12280371acd825e02fd619fbca60f1251 [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 Geoffrayb34f69a2014-03-07 15:28:39 +000017#include "compiler.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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070027#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080028#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000029#include "dex/quick/dex_file_to_method_inliner_map.h"
30
Brian Carlstrom7940e442013-07-12 13:46:57 -070031namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
Vladimir Marko867a2b32013-12-10 13:01:13 +000033extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver& driver) {
34 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070035}
36
Vladimir Marko867a2b32013-12-10 13:01:13 +000037extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver& driver) {
Vladimir Marko5816ed42013-11-27 17:04:20 +000038 CHECK(driver.GetCompilerContext() == NULL);
Brian Carlstrom7940e442013-07-12 13:46:57 -070039}
40
41/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070042static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070044 // (1 << kLoadHoisting) |
45 // (1 << kSuppressLoads) |
46 // (1 << kNullCheckElimination) |
Vladimir Markobfea9c22014-01-17 17:49:33 +000047 // (1 << kClassInitCheckElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070048 // (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) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000055 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 0;
57
58static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059 // (1 << kDebugDisplayMissingTargets) |
60 // (1 << kDebugVerbose) |
61 // (1 << kDebugDumpCFG) |
62 // (1 << kDebugSlowFieldPath) |
63 // (1 << kDebugSlowInvokePath) |
64 // (1 << kDebugSlowStringPath) |
65 // (1 << kDebugSlowestFieldPath) |
66 // (1 << kDebugSlowestStringPath) |
67 // (1 << kDebugExerciseResolveMethod) |
68 // (1 << kDebugVerifyDataflow) |
69 // (1 << kDebugShowMemoryUsage) |
70 // (1 << kDebugShowNops) |
71 // (1 << kDebugCountOpcodes) |
72 // (1 << kDebugDumpCheckStats) |
73 // (1 << kDebugDumpBitcodeFile) |
74 // (1 << kDebugVerifyBitcode) |
75 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070076 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070077 // (1 << kDebugTimings) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 0;
79
Vladimir Marko25724ef2013-11-12 15:09:20 +000080CompilationUnit::CompilationUnit(ArenaPool* pool)
81 : compiler_driver(NULL),
82 class_linker(NULL),
83 dex_file(NULL),
84 class_loader(NULL),
85 class_def_idx(0),
86 method_idx(0),
87 code_item(NULL),
88 access_flags(0),
89 invoke_type(kDirect),
90 shorty(NULL),
91 disable_opt(0),
92 enable_debug(0),
93 verbose(false),
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000094 compiler(NULL),
Vladimir Marko25724ef2013-11-12 15:09:20 +000095 instruction_set(kNone),
96 num_dalvik_registers(0),
97 insns(NULL),
98 num_ins(0),
99 num_outs(0),
100 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000101 compiler_flip_match(false),
102 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000103 arena_stack(pool),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000104 mir_graph(NULL),
105 cg(NULL),
106 timings("QuickCompiler", true, false) {
107}
108
109CompilationUnit::~CompilationUnit() {
110}
111
buzbeea61f4952013-08-23 14:27:06 -0700112void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000113 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700114 timings.StartSplit(label);
115 }
116}
117
118void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000119 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700120 timings.NewSplit(label);
121 }
122}
123
124void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000125 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700126 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000127 if (enable_debug & (1 << kDebugTimings)) {
128 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
129 LOG(INFO) << Dumpable<TimingLogger>(timings);
130 }
buzbeea61f4952013-08-23 14:27:06 -0700131 }
132}
133
Ian Rogers3d504072014-03-01 09:16:49 -0800134static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000135 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 const DexFile::CodeItem* code_item,
137 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700138 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000139 jobject class_loader, const DexFile& dex_file,
140 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700142 if (code_item->insns_size_in_code_units_ >= 0x10000) {
143 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
144 << " in " << PrettyMethod(method_idx, dex_file);
145 return NULL;
146 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147
Jeff Hao4a200f52014-04-01 14:58:49 -0700148 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000149 return nullptr;
150 }
151
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800153 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154
Ian Rogers3d504072014-03-01 09:16:49 -0800155 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700156 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800157 cu.instruction_set = driver.GetInstructionSet();
Stuart Monteithb95a5342014-03-12 13:32:32 +0000158 cu.target64 = (cu.instruction_set == kX86_64) || (cu.instruction_set == kArm64);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000159 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000160 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700161 DCHECK((cu.instruction_set == kThumb2) ||
162 (cu.instruction_set == kX86) ||
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700163 (cu.instruction_set == kX86_64) ||
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700164 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165
166
167 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700168 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700170 cu.compiler_flip_match = false;
171 bool use_match = !cu.compiler_method_match.empty();
172 bool match = use_match && (cu.compiler_flip_match ^
173 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 std::string::npos));
175 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700176 cu.disable_opt = kCompilerOptimizerDisableFlags;
177 cu.enable_debug = kCompilerDebugFlags;
178 cu.verbose = VLOG_IS_ON(compiler) ||
179 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 }
181
182 /*
183 * TODO: rework handling of optimization and debug flags. Should we split out
184 * MIR and backend flags? Need command-line setting as well.
185 */
186
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000187 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700189 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700191 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 (1 << kLoadStoreElimination) |
193 (1 << kLoadHoisting) |
194 (1 << kSuppressLoads) |
195 (1 << kNullCheckElimination) |
196 (1 << kPromoteRegs) |
197 (1 << kTrackLiveTemps) |
198 (1 << kSafeOptimizations) |
199 (1 << kBBOpt) |
200 (1 << kMatch) |
201 (1 << kPromoteCompilerTemps));
202 }
203
buzbeea61f4952013-08-23 14:27:06 -0700204 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700205 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800207 /*
208 * After creation of the MIR graph, also create the code generator.
209 * The reason we do this is that optimizations on the MIR graph may need to get information
210 * that is only available if a CG exists.
211 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000212 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800213
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 /* Gathering opcode stats? */
215 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700216 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 }
218
Dave Allison39c3bfb2014-01-28 18:33:52 -0800219 // Check early if we should skip this compilation if using the profiled filter.
220 if (cu.compiler_driver->ProfilePresent()) {
221 std::string methodname = PrettyMethod(method_idx, dex_file);
222 if (cu.mir_graph->SkipCompilation(methodname)) {
223 return NULL;
224 }
225 }
226
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700228 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 class_loader, dex_file);
230
buzbee1da1e2f2013-11-15 13:37:01 -0800231 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700232 if (cu.mir_graph->SkipCompilation()) {
233 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700234 }
235
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800236 /* Create the pass driver and launch it */
Ian Rogers3d504072014-03-01 09:16:49 -0800237 PassDriver pass_driver(&cu);
238 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700240 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
241 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 }
243
244 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700245 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 }
247
buzbee1da1e2f2013-11-15 13:37:01 -0800248 /* Reassociate sreg names with original Dalvik vreg names. */
249 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000251 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
252 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
253 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
254 MemStats stack_stats(cu.arena_stack.GetPeakStats());
255 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
256 }
257 }
258 cu.arena_stack.Reset();
259
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 CompiledMethod* result = NULL;
261
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700262 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263
buzbee1da1e2f2013-11-15 13:37:01 -0800264 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700265 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800266 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267
268 if (result) {
269 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
270 } else {
271 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
272 }
273
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700274 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000275 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000276 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000277 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 }
279 }
280
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700281 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
282 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 << " " << PrettyMethod(method_idx, dex_file);
284 }
285
buzbeea61f4952013-08-23 14:27:06 -0700286 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800287 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 return result;
289}
290
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000291CompiledMethod* CompileOneMethod(CompilerDriver& driver,
292 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 const DexFile::CodeItem* code_item,
294 uint32_t access_flags,
295 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700296 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 uint32_t method_idx,
298 jobject class_loader,
299 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000300 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000301 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000302 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303}
304
305} // namespace art
306
307extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000308 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 const art::DexFile::CodeItem* code_item,
310 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700311 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 const art::DexFile& dex_file) {
313 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000314 art::Compiler* compiler = driver.GetCompiler();
315 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 class_def_idx, method_idx, class_loader, dex_file,
317 NULL /* use thread llvm_info */);
318}