blob: e474c49121ec4b7a0fd8588b34680960806a2ba0 [file] [log] [blame]
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001/*
2 * Copyright (C) 2014 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 "optimizing_compiler.h"
18
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010019#include <fstream>
Nicolas Geoffray787c3072014-03-17 10:20:19 +000020#include <stdint.h>
21
Mathieu Chartierb666f482015-02-18 14:33:14 -080022#include "base/arena_allocator.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000023#include "base/dumpable.h"
24#include "base/timing_logger.h"
David Brazdil46e2a392015-03-16 17:31:52 +000025#include "boolean_simplifier.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070026#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000027#include "builder.h"
28#include "code_generator.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000029#include "compiled_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070030#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010031#include "constant_folding.h"
32#include "dead_code_elimination.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080033#include "dex/quick/dex_file_to_method_inliner_map.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000034#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000035#include "driver/compiler_options.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000036#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000037#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010038#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010039#include "gvn.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000040#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010041#include "instruction_simplifier.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080042#include "intrinsics.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000043#include "licm.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000044#include "jni/quick/jni_compiler.h"
45#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010047#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010048#include "register_allocator.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000049#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000050#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010051#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010052#include "ssa_liveness_analysis.h"
Calin Juravle10e244f2015-01-26 18:54:32 +000053#include "reference_type_propagation.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000054
55namespace art {
56
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057/**
58 * Used by the code generator, to allocate the code in a vector.
59 */
60class CodeVectorAllocator FINAL : public CodeAllocator {
61 public:
Andreas Gampe7c3952f2015-02-19 18:21:24 -080062 CodeVectorAllocator() : size_(0) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000063
64 virtual uint8_t* Allocate(size_t size) {
65 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000066 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067 return &memory_[0];
68 }
69
70 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000071 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072
73 private:
74 std::vector<uint8_t> memory_;
75 size_t size_;
76
77 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
78};
79
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010080/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010081 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000082 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010083 */
84static const char* kStringFilter = "";
85
David Brazdil809658e2015-02-05 11:34:02 +000086class PassInfo;
87
David Brazdil5e8b1372015-01-23 14:39:08 +000088class PassInfoPrinter : public ValueObject {
89 public:
90 PassInfoPrinter(HGraph* graph,
91 const char* method_name,
92 const CodeGenerator& codegen,
93 std::ostream* visualizer_output,
David Brazdil809658e2015-02-05 11:34:02 +000094 CompilerDriver* compiler_driver)
David Brazdil5e8b1372015-01-23 14:39:08 +000095 : method_name_(method_name),
David Brazdil809658e2015-02-05 11:34:02 +000096 timing_logger_enabled_(compiler_driver->GetDumpPasses()),
David Brazdil5e8b1372015-01-23 14:39:08 +000097 timing_logger_(method_name, true, true),
David Brazdil809658e2015-02-05 11:34:02 +000098 visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
David Brazdil5e8b1372015-01-23 14:39:08 +000099 visualizer_(visualizer_output, graph, codegen, method_name_) {
100 if (strstr(method_name, kStringFilter) == nullptr) {
101 timing_logger_enabled_ = visualizer_enabled_ = false;
102 }
103 }
104
David Brazdil5e8b1372015-01-23 14:39:08 +0000105 ~PassInfoPrinter() {
106 if (timing_logger_enabled_) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000107 LOG(INFO) << "TIMINGS " << method_name_;
108 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
109 }
110 }
111
112 private:
David Brazdil809658e2015-02-05 11:34:02 +0000113 void StartPass(const char* pass_name) {
114 // Dump graph first, then start timer.
115 if (visualizer_enabled_) {
116 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false);
117 }
118 if (timing_logger_enabled_) {
119 timing_logger_.StartTiming(pass_name);
120 }
121 }
122
123 void EndPass(const char* pass_name) {
124 // Pause timer first, then dump graph.
125 if (timing_logger_enabled_) {
126 timing_logger_.EndTiming();
127 }
128 if (visualizer_enabled_) {
129 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true);
130 }
131 }
132
David Brazdil5e8b1372015-01-23 14:39:08 +0000133 const char* method_name_;
134
135 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000136 TimingLogger timing_logger_;
137
138 bool visualizer_enabled_;
139 HGraphVisualizer visualizer_;
140
David Brazdil809658e2015-02-05 11:34:02 +0000141 friend PassInfo;
142
David Brazdil5e8b1372015-01-23 14:39:08 +0000143 DISALLOW_COPY_AND_ASSIGN(PassInfoPrinter);
144};
145
David Brazdil809658e2015-02-05 11:34:02 +0000146class PassInfo : public ValueObject {
147 public:
148 PassInfo(const char *pass_name, PassInfoPrinter* pass_info_printer)
149 : pass_name_(pass_name),
150 pass_info_printer_(pass_info_printer) {
151 pass_info_printer_->StartPass(pass_name_);
152 }
153
154 ~PassInfo() {
155 pass_info_printer_->EndPass(pass_name_);
156 }
157
158 private:
159 const char* const pass_name_;
160 PassInfoPrinter* const pass_info_printer_;
161};
162
Andreas Gampe53c913b2014-08-12 23:19:23 -0700163class OptimizingCompiler FINAL : public Compiler {
164 public:
165 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100166 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -0700167
168 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
169 OVERRIDE;
170
171 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
172 uint32_t access_flags,
173 InvokeType invoke_type,
174 uint16_t class_def_idx,
175 uint32_t method_idx,
176 jobject class_loader,
177 const DexFile& dex_file) const OVERRIDE;
178
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000179 CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
180 uint32_t access_flags,
181 InvokeType invoke_type,
182 uint16_t class_def_idx,
183 uint32_t method_idx,
184 jobject class_loader,
185 const DexFile& dex_file) const;
186
Andreas Gampe53c913b2014-08-12 23:19:23 -0700187 CompiledMethod* JniCompile(uint32_t access_flags,
188 uint32_t method_idx,
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000189 const DexFile& dex_file) const OVERRIDE {
190 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
191 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700192
193 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
195 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
196 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
197 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700198
199 bool WriteElf(art::File* file,
200 OatWriter* oat_writer,
201 const std::vector<const art::DexFile*>& dex_files,
202 const std::string& android_root,
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000203 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
204 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
205 *GetCompilerDriver());
206 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700207
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000208 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700209
David Brazdilee690a32014-12-01 17:04:16 +0000210 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700211
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000212 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700213
214 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100215 // Whether we should run any optimization or register allocation. If false, will
216 // just run the code generation after the graph was built.
217 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000218
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000219 // Optimize and compile `graph`.
220 CompiledMethod* CompileOptimized(HGraph* graph,
221 CodeGenerator* codegen,
222 CompilerDriver* driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000223 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000224 const DexCompilationUnit& dex_compilation_unit,
David Brazdil5e8b1372015-01-23 14:39:08 +0000225 PassInfoPrinter* pass_info) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000226
227 // Just compile without doing optimizations.
228 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
229 CompilerDriver* driver,
230 const DexCompilationUnit& dex_compilation_unit) const;
231
Calin Juravle48c2b032014-12-09 18:11:36 +0000232 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100233
Andreas Gampe53c913b2014-08-12 23:19:23 -0700234 std::unique_ptr<std::ostream> visualizer_output_;
235
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000236 // Delegate to Quick in case the optimizing compiler cannot compile a method.
237 std::unique_ptr<Compiler> delegate_;
238
Andreas Gampe53c913b2014-08-12 23:19:23 -0700239 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
240};
241
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100242static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
243
244OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
245 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
246 run_optimizations_(
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000247 (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
248 && !driver->GetCompilerOptions().GetDebuggable()),
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000249 compilation_stats_(),
250 delegate_(Create(driver, Compiler::Kind::kQuick)) {}
David Brazdilee690a32014-12-01 17:04:16 +0000251
252void OptimizingCompiler::Init() {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000253 delegate_->Init();
David Brazdilee690a32014-12-01 17:04:16 +0000254 // Enable C1visualizer output. Must be done in Init() because the compiler
255 // driver is not fully initialized when passed to the compiler's constructor.
256 CompilerDriver* driver = GetCompilerDriver();
David Brazdil866c0312015-01-13 21:21:31 +0000257 const std::string cfg_file_name = driver->GetDumpCfgFileName();
258 if (!cfg_file_name.empty()) {
David Brazdilee690a32014-12-01 17:04:16 +0000259 CHECK_EQ(driver->GetThreadCount(), 1U)
260 << "Graph visualizer requires the compiler to run single-threaded. "
261 << "Invoke the compiler with '-j1'.";
David Brazdil866c0312015-01-13 21:21:31 +0000262 visualizer_output_.reset(new std::ofstream(cfg_file_name));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100263 }
264}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000265
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000266void OptimizingCompiler::UnInit() const {
267 delegate_->UnInit();
268}
269
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100270OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000271 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100272}
273
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000274void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
275 delegate_->InitCompilationUnit(cu);
276}
277
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000278bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
279 const DexFile& dex_file ATTRIBUTE_UNUSED,
280 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
281 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700282}
283
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000284static bool IsInstructionSetSupported(InstructionSet instruction_set) {
285 return instruction_set == kArm64
286 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
287 || instruction_set == kX86
288 || instruction_set == kX86_64;
289}
290
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000291static bool CanOptimize(const DexFile::CodeItem& code_item) {
292 // TODO: We currently cannot optimize methods with try/catch.
293 return code_item.tries_size_ == 0;
294}
295
Calin Juravle10e244f2015-01-26 18:54:32 +0000296static void RunOptimizations(HOptimization* optimizations[],
297 size_t length,
David Brazdil809658e2015-02-05 11:34:02 +0000298 PassInfoPrinter* pass_info_printer) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000299 for (size_t i = 0; i < length; ++i) {
300 HOptimization* optimization = optimizations[i];
David Brazdil809658e2015-02-05 11:34:02 +0000301 {
302 PassInfo pass_info(optimization->GetPassName(), pass_info_printer);
303 optimization->Run();
304 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000305 optimization->Check();
306 }
307}
308
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000309static void RunOptimizations(HGraph* graph,
310 CompilerDriver* driver,
311 OptimizingCompilerStats* stats,
Calin Juravleacf735c2015-02-12 15:25:22 +0000312 const DexFile& dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000313 const DexCompilationUnit& dex_compilation_unit,
Calin Juravleacf735c2015-02-12 15:25:22 +0000314 PassInfoPrinter* pass_info_printer,
315 StackHandleScopeCollection* handles) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000316 HDeadCodeElimination dce(graph);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000317 HConstantFolding fold1(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000318 InstructionSimplifier simplify1(graph, stats);
David Brazdil46e2a392015-03-16 17:31:52 +0000319 HBooleanSimplifier boolean_not(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000320
Nicolas Geoffray9437b782015-03-25 10:08:51 +0000321 HInliner inliner(graph, dex_compilation_unit, dex_compilation_unit, driver, stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000322
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000323 HConstantFolding fold2(graph);
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000324 SideEffectsAnalysis side_effects(graph);
325 GVNOptimization gvn(graph, side_effects);
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000326 LICM licm(graph, side_effects);
Mingyao Yangf384f882014-10-22 16:08:18 -0700327 BoundsCheckElimination bce(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000328 ReferenceTypePropagation type_propagation(graph, dex_file, dex_compilation_unit, handles);
329 InstructionSimplifier simplify2(graph, stats, "instruction_simplifier_after_types");
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000330
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800331 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
332
Nicolas Geoffray31596742014-11-24 15:28:45 +0000333 HOptimization* optimizations[] = {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800334 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000335 &dce,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000336 &fold1,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000337 &simplify1,
David Brazdil46e2a392015-03-16 17:31:52 +0000338 // BooleanSimplifier depends on the InstructionSimplifier removing redundant
339 // suspend checks to recognize empty blocks.
340 &boolean_not,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000341 &inliner,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000342 &fold2,
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000343 &side_effects,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000344 &gvn,
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000345 &licm,
Mingyao Yangf384f882014-10-22 16:08:18 -0700346 &bce,
Calin Juravle10e244f2015-01-26 18:54:32 +0000347 &type_propagation,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000348 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000349 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000350
David Brazdil809658e2015-02-05 11:34:02 +0000351 RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000352}
353
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000354// The stack map we generate must be 4-byte aligned on ARM. Since existing
355// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800356static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000357 size_t size = vector.size();
358 size_t aligned_size = RoundUp(size, 4);
359 for (; size < aligned_size; ++size) {
360 vector.push_back(0);
361 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800362 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000363}
364
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000365
366CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
367 CodeGenerator* codegen,
368 CompilerDriver* compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000369 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000370 const DexCompilationUnit& dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000371 PassInfoPrinter* pass_info_printer) const {
Calin Juravleacf735c2015-02-12 15:25:22 +0000372 StackHandleScopeCollection handles(Thread::Current());
373 RunOptimizations(graph, compiler_driver, &compilation_stats_,
374 dex_file, dex_compilation_unit, pass_info_printer, &handles);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000375
376 PrepareForRegisterAllocation(graph).Run();
377 SsaLivenessAnalysis liveness(*graph, codegen);
David Brazdil809658e2015-02-05 11:34:02 +0000378 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800379 PassInfo pass_info(SsaLivenessAnalysis::kLivenessPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000380 liveness.Analyze();
381 }
382 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800383 PassInfo pass_info(RegisterAllocator::kRegisterAllocatorPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000384 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
385 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000386
387 CodeVectorAllocator allocator;
388 codegen->CompileOptimized(&allocator);
389
390 std::vector<uint8_t> stack_map;
391 codegen->BuildStackMaps(&stack_map);
392
393 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
394
395 return CompiledMethod::SwapAllocCompiledMethodStackMap(
396 compiler_driver,
397 codegen->GetInstructionSet(),
398 ArrayRef<const uint8_t>(allocator.GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000399 // Follow Quick's behavior and set the frame size to zero if it is
400 // considered "empty" (see the definition of
401 // art::CodeGenerator::HasEmptyFrame).
402 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000403 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000404 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000405 ArrayRef<const uint8_t>(stack_map));
406}
407
408
409CompiledMethod* OptimizingCompiler::CompileBaseline(
410 CodeGenerator* codegen,
411 CompilerDriver* compiler_driver,
412 const DexCompilationUnit& dex_compilation_unit) const {
413 CodeVectorAllocator allocator;
414 codegen->CompileBaseline(&allocator);
415
416 std::vector<uint8_t> mapping_table;
417 DefaultSrcMap src_mapping_table;
418 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
419 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
420 std::vector<uint8_t> vmap_table;
421 codegen->BuildVMapTable(&vmap_table);
422 std::vector<uint8_t> gc_map;
423 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
424
425 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000426 return CompiledMethod::SwapAllocCompiledMethod(
427 compiler_driver,
428 codegen->GetInstructionSet(),
429 ArrayRef<const uint8_t>(allocator.GetMemory()),
430 // Follow Quick's behavior and set the frame size to zero if it is
431 // considered "empty" (see the definition of
432 // art::CodeGenerator::HasEmptyFrame).
433 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
434 codegen->GetCoreSpillMask(),
435 codegen->GetFpuSpillMask(),
436 &src_mapping_table,
437 AlignVectorSize(mapping_table),
438 AlignVectorSize(vmap_table),
439 AlignVectorSize(gc_map),
440 ArrayRef<const uint8_t>());
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000441}
442
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000443CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
444 uint32_t access_flags,
445 InvokeType invoke_type,
446 uint16_t class_def_idx,
447 uint32_t method_idx,
448 jobject class_loader,
449 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700450 UNUSED(invoke_type);
David Brazdil5e8b1372015-01-23 14:39:08 +0000451 std::string method_name = PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000452 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000453 CompilerDriver* compiler_driver = GetCompilerDriver();
454 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100455 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
456 // overflow checks) assume thumb2.
457 if (instruction_set == kArm) {
458 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100459 }
460
461 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000462 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000463 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100464 return nullptr;
465 }
466
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000467 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000468 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000469 return nullptr;
470 }
471
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000472 // Implementation of the space filter: do not compile a code item whose size in
473 // code units is bigger than 256.
474 static constexpr size_t kSpaceFilterOptimizingThreshold = 256;
475 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
476 if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
477 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
478 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
479 return nullptr;
480 }
481
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000482 DexCompilationUnit dex_compilation_unit(
483 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700484 class_def_idx, method_idx, access_flags,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000485 compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000486
Nicolas Geoffray579ea7d2015-03-24 17:28:38 +0000487 ArenaAllocator arena(Runtime::Current()->GetArenaPool());
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000488 HGraph* graph = new (&arena) HGraph(
489 &arena, compiler_driver->GetCompilerOptions().GetDebuggable());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000490
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000491 // For testing purposes, we put a special marker on method names that should be compiled
492 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000493 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000494 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000495
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000496 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000497 CodeGenerator::Create(graph,
498 instruction_set,
499 *compiler_driver->GetInstructionSetFeatures(),
500 compiler_driver->GetCompilerOptions()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000501 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800502 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000503 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000504 return nullptr;
505 }
506
David Brazdil809658e2015-02-05 11:34:02 +0000507 PassInfoPrinter pass_info_printer(graph,
508 method_name.c_str(),
509 *codegen.get(),
510 visualizer_output_.get(),
511 compiler_driver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000512
513 HGraphBuilder builder(graph,
514 &dex_compilation_unit,
515 &dex_compilation_unit,
516 &dex_file,
517 compiler_driver,
518 &compilation_stats_);
519
520 VLOG(compiler) << "Building " << method_name;
521
David Brazdil809658e2015-02-05 11:34:02 +0000522 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800523 PassInfo pass_info(HGraphBuilder::kBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000524 if (!builder.BuildGraph(*code_item)) {
525 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
526 return nullptr;
527 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000528 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100529
Calin Juravle48c2b032014-12-09 18:11:36 +0000530 bool can_optimize = CanOptimize(*code_item);
531 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000532
533 // `run_optimizations_` is set explicitly (either through a compiler filter
534 // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
535 // to Quick.
536 bool can_use_baseline = !run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000537 if (run_optimizations_ && can_optimize && can_allocate_registers) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000538 VLOG(compiler) << "Optimizing " << method_name;
539
David Brazdil809658e2015-02-05 11:34:02 +0000540 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800541 PassInfo pass_info(SsaBuilder::kSsaBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000542 if (!graph->TryBuildingSsa()) {
543 // We could not transform the graph to SSA, bailout.
544 LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
545 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
546 return nullptr;
547 }
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000548 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000549
550 return CompileOptimized(graph,
551 codegen.get(),
552 compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000553 dex_file,
David Brazdil5e8b1372015-01-23 14:39:08 +0000554 dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000555 &pass_info_printer);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000556 } else if (shouldOptimize && can_allocate_registers) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100557 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800558 UNREACHABLE();
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000559 } else if (can_use_baseline) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000560 VLOG(compiler) << "Compile baseline " << method_name;
Calin Juravle48c2b032014-12-09 18:11:36 +0000561
562 if (!run_optimizations_) {
563 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
564 } else if (!can_optimize) {
565 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
566 } else if (!can_allocate_registers) {
567 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
568 }
569
David Brazdil5e8b1372015-01-23 14:39:08 +0000570 return CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000571 } else {
572 return nullptr;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100573 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000574}
575
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000576CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
577 uint32_t access_flags,
578 InvokeType invoke_type,
579 uint16_t class_def_idx,
580 uint32_t method_idx,
581 jobject class_loader,
582 const DexFile& dex_file) const {
583 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
584 method_idx, class_loader, dex_file);
585 if (method != nullptr) {
586 return method;
587 }
Nicolas Geoffray12be74e2015-03-30 13:29:08 +0100588 method = delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
589 class_loader, dex_file);
590
591 if (method != nullptr) {
592 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledQuick);
593 }
594 return method;
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000595}
596
Andreas Gampe53c913b2014-08-12 23:19:23 -0700597Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
598 return new OptimizingCompiler(driver);
599}
600
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000601} // namespace art