blob: 2e21d052ec3a4f0f0f17292306cbb0fc0c009685 [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
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "frontend.h"
18
Andreas Gampe0d8ea462014-07-17 18:04:32 -070019#include <cstdint>
20
Andreas Gampe53c913b2014-08-12 23:19:23 -070021#include "backend.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000022#include "compiler.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "compiler_internals.h"
24#include "driver/compiler_driver.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080025#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mirror/object.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070027#include "pass_driver_me_opts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "runtime.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070030#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080031#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000032#include "dex/quick/dex_file_to_method_inliner_map.h"
33
Brian Carlstrom7940e442013-07-12 13:46:57 -070034namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070035
Brian Carlstrom7940e442013-07-12 13:46:57 -070036/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070037static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Serban Constantinescu63999682014-07-15 17:44:21 +010038 // (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070039 // (1 << kLoadHoisting) |
40 // (1 << kSuppressLoads) |
41 // (1 << kNullCheckElimination) |
Vladimir Markobfea9c22014-01-17 17:49:33 +000042 // (1 << kClassInitCheckElimination) |
Vladimir Marko95a05972014-05-30 10:01:32 +010043 // (1 << kGlobalValueNumbering) |
Vladimir Marko415ac882014-09-30 18:09:14 +010044 // (1 << kLocalValueNumbering) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070045 // (1 << kPromoteRegs) |
buzbee30adc732014-05-09 15:10:18 -070046 // (1 << kTrackLiveTemps) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070047 // (1 << kSafeOptimizations) |
48 // (1 << kBBOpt) |
49 // (1 << kMatch) |
50 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080051 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000052 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 0;
54
55static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070056 // (1 << kDebugDisplayMissingTargets) |
57 // (1 << kDebugVerbose) |
58 // (1 << kDebugDumpCFG) |
59 // (1 << kDebugSlowFieldPath) |
60 // (1 << kDebugSlowInvokePath) |
61 // (1 << kDebugSlowStringPath) |
62 // (1 << kDebugSlowestFieldPath) |
63 // (1 << kDebugSlowestStringPath) |
64 // (1 << kDebugExerciseResolveMethod) |
65 // (1 << kDebugVerifyDataflow) |
66 // (1 << kDebugShowMemoryUsage) |
67 // (1 << kDebugShowNops) |
68 // (1 << kDebugCountOpcodes) |
69 // (1 << kDebugDumpCheckStats) |
70 // (1 << kDebugDumpBitcodeFile) |
71 // (1 << kDebugVerifyBitcode) |
72 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070073 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070074 // (1 << kDebugTimings) |
buzbeeb01bf152014-05-13 15:59:07 -070075 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 0;
77
Ian Rogers3d504072014-03-01 09:16:49 -080078static CompiledMethod* CompileMethod(CompilerDriver& driver,
Andreas Gampe53c913b2014-08-12 23:19:23 -070079 const Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 const DexFile::CodeItem* code_item,
81 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070082 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000083 jobject class_loader, const DexFile& dex_file,
84 void* llvm_compilation_unit) {
Ian Rogers1ff3c982014-08-12 02:30:58 -070085 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbee497d62e2014-09-17 13:23:58 -070086 /*
87 * Skip compilation for pathologically large methods - either by instruction count or num vregs.
88 * Dalvik uses 16-bit uints for instruction and register counts. We'll limit to a quarter
89 * of that, which also guarantees we cannot overflow our 16-bit internal SSA name space.
90 */
91 if (code_item->insns_size_in_code_units_ >= UINT16_MAX / 4) {
92 LOG(INFO) << "Method exceeds compiler instruction limit: "
93 << code_item->insns_size_in_code_units_
Ian Rogers1ff3c982014-08-12 02:30:58 -070094 << " in " << PrettyMethod(method_idx, dex_file);
buzbeeb48819d2013-09-14 16:15:25 -070095 return NULL;
96 }
buzbee497d62e2014-09-17 13:23:58 -070097 if (code_item->registers_size_ >= UINT16_MAX / 4) {
98 LOG(INFO) << "Method exceeds compiler virtual register limit: "
99 << code_item->registers_size_ << " in " << PrettyMethod(method_idx, dex_file);
100 return NULL;
101 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102
Jeff Hao4a200f52014-04-01 14:58:49 -0700103 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000104 return nullptr;
105 }
106
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800108 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109
Ian Rogers3d504072014-03-01 09:16:49 -0800110 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700111 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800112 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700113 if (cu.instruction_set == kArm) {
114 cu.instruction_set = kThumb2;
115 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700116 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000117 cu.compiler = compiler;
Andreas Gampe00caeed2014-07-10 01:43:08 -0700118 // TODO: Mips64 is not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700119 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100120 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700121 (cu.instruction_set == kX86) ||
122 (cu.instruction_set == kX86_64) ||
123 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700126 cu.compiler_flip_match = false;
127 bool use_match = !cu.compiler_method_match.empty();
128 bool match = use_match && (cu.compiler_flip_match ^
Ian Rogers1ff3c982014-08-12 02:30:58 -0700129 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) != std::string::npos));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700131 cu.disable_opt = kCompilerOptimizerDisableFlags;
132 cu.enable_debug = kCompilerDebugFlags;
133 cu.verbose = VLOG_IS_ON(compiler) ||
134 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 }
136
Mingyao Yang42d65c52014-04-18 16:49:39 -0700137 if (gVerboseMethods.size() != 0) {
138 cu.verbose = false;
139 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700140 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
Mingyao Yang42d65c52014-04-18 16:49:39 -0700141 != std::string::npos) {
142 cu.verbose = true;
143 break;
144 }
145 }
146 }
147
buzbeeb01bf152014-05-13 15:59:07 -0700148 if (cu.verbose) {
149 cu.enable_debug |= (1 << kDebugCodegenDump);
150 }
151
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 /*
153 * TODO: rework handling of optimization and debug flags. Should we split out
154 * MIR and backend flags? Need command-line setting as well.
155 */
156
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000157 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158
buzbeea61f4952013-08-23 14:27:06 -0700159 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700160 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800162 /*
163 * After creation of the MIR graph, also create the code generator.
164 * The reason we do this is that optimizations on the MIR graph may need to get information
165 * that is only available if a CG exists.
166 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000167 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800168
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 /* Gathering opcode stats? */
170 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700171 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 }
173
174 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700175 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700176 class_loader, dex_file);
177
Andreas Gampe53c913b2014-08-12 23:19:23 -0700178 if (!compiler->CanCompileMethod(method_idx, dex_file, &cu)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700179 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : "
180 << PrettyMethod(method_idx, dex_file);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100181 return nullptr;
182 }
183
buzbee1da1e2f2013-11-15 13:37:01 -0800184 cu.NewTimingSplit("MIROpt:CheckFilters");
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700185 std::string skip_message;
186 if (cu.mir_graph->SkipCompilation(&skip_message)) {
187 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 << PrettyMethod(method_idx, dex_file) << " Reason = " << skip_message;
buzbee33ae5582014-06-12 14:56:32 -0700189 return nullptr;
buzbeeee17e0a2013-07-31 10:47:37 -0700190 }
191
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800192 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700193 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800194 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195
Calin Juravle38a09042014-06-23 14:40:31 +0100196 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
197 if (cu.compiler_driver->ProfilePresent()
198 && !cu.mir_graph->MethodIsLeaf()
Ian Rogers1ff3c982014-08-12 02:30:58 -0700199 && cu.mir_graph->SkipCompilationByName(PrettyMethod(method_idx, dex_file))) {
Calin Juravle38a09042014-06-23 14:40:31 +0100200 return nullptr;
201 }
202
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700203 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
204 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 }
206
207 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700208 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 }
210
buzbee1da1e2f2013-11-15 13:37:01 -0800211 /* Reassociate sreg names with original Dalvik vreg names. */
212 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000214 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
215 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100216 if (cu.arena_stack.PeakBytesAllocated() > 1 * 1024 * 1024) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000217 MemStats stack_stats(cu.arena_stack.GetPeakStats());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700218 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000219 }
220 }
221 cu.arena_stack.Reset();
222
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 CompiledMethod* result = NULL;
224
buzbee8c7a02a2014-06-14 12:33:09 -0700225 if (cu.mir_graph->PuntToInterpreter()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700226 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: "
227 << PrettyMethod(method_idx, dex_file);
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700228 return nullptr;
buzbee8c7a02a2014-06-14 12:33:09 -0700229 }
230
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700231 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232
buzbee1da1e2f2013-11-15 13:37:01 -0800233 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700234 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800235 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236
237 if (result) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700238 VLOG(compiler) << cu.instruction_set << ": Compiled " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700239 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700240 VLOG(compiler) << cu.instruction_set << ": Deferred " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 }
242
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700243 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000244 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000245 MemStats mem_stats(cu.arena.GetMemStats());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700246 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 }
248 }
249
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700250 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
251 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Ian Rogers1ff3c982014-08-12 02:30:58 -0700252 << " " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 }
254
buzbeea61f4952013-08-23 14:27:06 -0700255 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800256 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700257 return result;
258}
259
Andreas Gampe53c913b2014-08-12 23:19:23 -0700260CompiledMethod* CompileOneMethod(CompilerDriver* driver,
261 const Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 const DexFile::CodeItem* code_item,
263 uint32_t access_flags,
264 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700265 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700266 uint32_t method_idx,
267 jobject class_loader,
268 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000269 void* compilation_unit) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700270 return CompileMethod(*driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000271 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272}
273
274} // namespace art