blob: 3f6231cb1f4b073dc93077dc8b683791e7c1b3a4 [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"
Ian Rogersc7dd2952014-10-21 23:31:19 -070022#include "base/dumpable.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000023#include "compiler.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "compiler_internals.h"
25#include "driver/compiler_driver.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080026#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "mirror/object.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070028#include "pass_driver_me_opts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "runtime.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070031#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080032#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000033#include "dex/quick/dex_file_to_method_inliner_map.h"
34
Brian Carlstrom7940e442013-07-12 13:46:57 -070035namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070036
Brian Carlstrom7940e442013-07-12 13:46:57 -070037/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070038static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
Serban Constantinescu63999682014-07-15 17:44:21 +010039 // (1 << kLoadStoreElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070040 // (1 << kLoadHoisting) |
41 // (1 << kSuppressLoads) |
42 // (1 << kNullCheckElimination) |
Vladimir Markobfea9c22014-01-17 17:49:33 +000043 // (1 << kClassInitCheckElimination) |
Vladimir Marko95a05972014-05-30 10:01:32 +010044 // (1 << kGlobalValueNumbering) |
Vladimir Marko415ac882014-09-30 18:09:14 +010045 // (1 << kLocalValueNumbering) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070046 // (1 << kPromoteRegs) |
buzbee30adc732014-05-09 15:10:18 -070047 // (1 << kTrackLiveTemps) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070048 // (1 << kSafeOptimizations) |
49 // (1 << kBBOpt) |
50 // (1 << kMatch) |
51 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080052 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000053 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 0;
55
56static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070057 // (1 << kDebugDisplayMissingTargets) |
58 // (1 << kDebugVerbose) |
59 // (1 << kDebugDumpCFG) |
60 // (1 << kDebugSlowFieldPath) |
61 // (1 << kDebugSlowInvokePath) |
62 // (1 << kDebugSlowStringPath) |
63 // (1 << kDebugSlowestFieldPath) |
64 // (1 << kDebugSlowestStringPath) |
65 // (1 << kDebugExerciseResolveMethod) |
66 // (1 << kDebugVerifyDataflow) |
67 // (1 << kDebugShowMemoryUsage) |
68 // (1 << kDebugShowNops) |
69 // (1 << kDebugCountOpcodes) |
70 // (1 << kDebugDumpCheckStats) |
71 // (1 << kDebugDumpBitcodeFile) |
72 // (1 << kDebugVerifyBitcode) |
73 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070074 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070075 // (1 << kDebugTimings) |
buzbeeb01bf152014-05-13 15:59:07 -070076 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 0;
78
Ian Rogers3d504072014-03-01 09:16:49 -080079static CompiledMethod* CompileMethod(CompilerDriver& driver,
Andreas Gampe53c913b2014-08-12 23:19:23 -070080 const Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 const DexFile::CodeItem* code_item,
82 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070083 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +000084 jobject class_loader, const DexFile& dex_file,
85 void* llvm_compilation_unit) {
Ian Rogers1ff3c982014-08-12 02:30:58 -070086 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +000087 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
88 return nullptr;
buzbee497d62e2014-09-17 13:23:58 -070089 }
Brian Carlstrom7940e442013-07-12 13:46:57 -070090
Ian Rogersaaf29b32014-11-07 17:05:19 -080091 DCHECK(driver.GetCompilerOptions().IsCompilationEnabled());
Ian Rogersa03de6d2014-03-08 23:37:07 +000092
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -080094 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -070095
Ian Rogers3d504072014-03-01 09:16:49 -080096 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070097 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -080098 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -070099 if (cu.instruction_set == kArm) {
100 cu.instruction_set = kThumb2;
101 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700102 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000103 cu.compiler = compiler;
Andreas Gampe00caeed2014-07-10 01:43:08 -0700104 // TODO: Mips64 is not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700105 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100106 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700107 (cu.instruction_set == kX86) ||
108 (cu.instruction_set == kX86_64) ||
109 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700112 cu.compiler_flip_match = false;
113 bool use_match = !cu.compiler_method_match.empty();
114 bool match = use_match && (cu.compiler_flip_match ^
Ian Rogers1ff3c982014-08-12 02:30:58 -0700115 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) != std::string::npos));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700117 cu.disable_opt = kCompilerOptimizerDisableFlags;
118 cu.enable_debug = kCompilerDebugFlags;
119 cu.verbose = VLOG_IS_ON(compiler) ||
120 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 }
122
Ian Rogersc7dd2952014-10-21 23:31:19 -0700123 if (driver.GetCompilerOptions().HasVerboseMethods()) {
124 cu.verbose = driver.GetCompilerOptions().IsVerboseMethod(PrettyMethod(method_idx, dex_file));
Mingyao Yang42d65c52014-04-18 16:49:39 -0700125 }
126
buzbeeb01bf152014-05-13 15:59:07 -0700127 if (cu.verbose) {
128 cu.enable_debug |= (1 << kDebugCodegenDump);
129 }
130
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131 /*
132 * TODO: rework handling of optimization and debug flags. Should we split out
133 * MIR and backend flags? Need command-line setting as well.
134 */
135
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000136 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137
buzbeea61f4952013-08-23 14:27:06 -0700138 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700139 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800141 /*
142 * After creation of the MIR graph, also create the code generator.
143 * The reason we do this is that optimizations on the MIR graph may need to get information
144 * that is only available if a CG exists.
145 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000146 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800147
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 /* Gathering opcode stats? */
149 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700150 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 }
152
153 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700154 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 class_loader, dex_file);
156
Andreas Gampe53c913b2014-08-12 23:19:23 -0700157 if (!compiler->CanCompileMethod(method_idx, dex_file, &cu)) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700158 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : "
159 << PrettyMethod(method_idx, dex_file);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100160 return nullptr;
161 }
162
buzbee1da1e2f2013-11-15 13:37:01 -0800163 cu.NewTimingSplit("MIROpt:CheckFilters");
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700164 std::string skip_message;
165 if (cu.mir_graph->SkipCompilation(&skip_message)) {
166 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700167 << PrettyMethod(method_idx, dex_file) << " Reason = " << skip_message;
buzbee33ae5582014-06-12 14:56:32 -0700168 return nullptr;
buzbeeee17e0a2013-07-31 10:47:37 -0700169 }
170
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800171 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700172 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800173 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174
Calin Juravle38a09042014-06-23 14:40:31 +0100175 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
176 if (cu.compiler_driver->ProfilePresent()
177 && !cu.mir_graph->MethodIsLeaf()
Ian Rogers1ff3c982014-08-12 02:30:58 -0700178 && cu.mir_graph->SkipCompilationByName(PrettyMethod(method_idx, dex_file))) {
Calin Juravle38a09042014-06-23 14:40:31 +0100179 return nullptr;
180 }
181
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700182 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
183 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 }
185
186 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700187 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 }
189
buzbee1da1e2f2013-11-15 13:37:01 -0800190 /* Reassociate sreg names with original Dalvik vreg names. */
191 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000193 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
194 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Markob19955d2014-07-29 12:04:10 +0100195 if (cu.arena_stack.PeakBytesAllocated() > 1 * 1024 * 1024) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000196 MemStats stack_stats(cu.arena_stack.GetPeakStats());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000198 }
199 }
200 cu.arena_stack.Reset();
201
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 CompiledMethod* result = NULL;
203
buzbee8c7a02a2014-06-14 12:33:09 -0700204 if (cu.mir_graph->PuntToInterpreter()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700205 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: "
206 << PrettyMethod(method_idx, dex_file);
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700207 return nullptr;
buzbee8c7a02a2014-06-14 12:33:09 -0700208 }
209
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700210 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211
buzbee1da1e2f2013-11-15 13:37:01 -0800212 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700213 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800214 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
216 if (result) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 VLOG(compiler) << cu.instruction_set << ": Compiled " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700219 VLOG(compiler) << cu.instruction_set << ": Deferred " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 }
221
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700222 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000223 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000224 MemStats mem_stats(cu.arena.GetMemStats());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700225 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 }
227 }
228
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700229 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
230 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Ian Rogers1ff3c982014-08-12 02:30:58 -0700231 << " " << PrettyMethod(method_idx, dex_file);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 }
233
buzbeea61f4952013-08-23 14:27:06 -0700234 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800235 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 return result;
237}
238
Andreas Gampe53c913b2014-08-12 23:19:23 -0700239CompiledMethod* CompileOneMethod(CompilerDriver* driver,
240 const Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 const DexFile::CodeItem* code_item,
242 uint32_t access_flags,
243 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700244 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 uint32_t method_idx,
246 jobject class_loader,
247 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000248 void* compilation_unit) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700249 return CompileMethod(*driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000250 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251}
252
253} // namespace art