blob: 933a8a005c9c1dce7a7a14fae6b66e8e98e4b2fd [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"
Mingyao Yangf384f882014-10-22 16:08:18 -070025#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026#include "builder.h"
27#include "code_generator.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070028#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010029#include "constant_folding.h"
30#include "dead_code_elimination.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080031#include "dex/quick/dex_file_to_method_inliner_map.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000032#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000033#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000034#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010035#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010036#include "gvn.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000037#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010038#include "instruction_simplifier.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080039#include "intrinsics.h"
Nicolas Geoffray82091da2015-01-26 10:02:45 +000040#include "licm.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000041#include "jni/quick/jni_compiler.h"
42#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000043#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010044#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010045#include "register_allocator.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000046#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000047#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010048#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010049#include "ssa_liveness_analysis.h"
Calin Juravle10e244f2015-01-26 18:54:32 +000050#include "reference_type_propagation.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000051
52namespace art {
53
Nicolas Geoffray787c3072014-03-17 10:20:19 +000054/**
55 * Used by the code generator, to allocate the code in a vector.
56 */
57class CodeVectorAllocator FINAL : public CodeAllocator {
58 public:
Andreas Gampe7c3952f2015-02-19 18:21:24 -080059 CodeVectorAllocator() : size_(0) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060
61 virtual uint8_t* Allocate(size_t size) {
62 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000063 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000064 return &memory_[0];
65 }
66
67 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000068 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000069
70 private:
71 std::vector<uint8_t> memory_;
72 size_t size_;
73
74 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
75};
76
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010077/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010078 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000079 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010080 */
81static const char* kStringFilter = "";
82
David Brazdil809658e2015-02-05 11:34:02 +000083class PassInfo;
84
David Brazdil5e8b1372015-01-23 14:39:08 +000085class PassInfoPrinter : public ValueObject {
86 public:
87 PassInfoPrinter(HGraph* graph,
88 const char* method_name,
89 const CodeGenerator& codegen,
90 std::ostream* visualizer_output,
David Brazdil809658e2015-02-05 11:34:02 +000091 CompilerDriver* compiler_driver)
David Brazdil5e8b1372015-01-23 14:39:08 +000092 : method_name_(method_name),
David Brazdil809658e2015-02-05 11:34:02 +000093 timing_logger_enabled_(compiler_driver->GetDumpPasses()),
David Brazdil5e8b1372015-01-23 14:39:08 +000094 timing_logger_(method_name, true, true),
David Brazdil809658e2015-02-05 11:34:02 +000095 visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
David Brazdil5e8b1372015-01-23 14:39:08 +000096 visualizer_(visualizer_output, graph, codegen, method_name_) {
97 if (strstr(method_name, kStringFilter) == nullptr) {
98 timing_logger_enabled_ = visualizer_enabled_ = false;
99 }
100 }
101
David Brazdil5e8b1372015-01-23 14:39:08 +0000102 ~PassInfoPrinter() {
103 if (timing_logger_enabled_) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000104 LOG(INFO) << "TIMINGS " << method_name_;
105 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
106 }
107 }
108
109 private:
David Brazdil809658e2015-02-05 11:34:02 +0000110 void StartPass(const char* pass_name) {
111 // Dump graph first, then start timer.
112 if (visualizer_enabled_) {
113 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false);
114 }
115 if (timing_logger_enabled_) {
116 timing_logger_.StartTiming(pass_name);
117 }
118 }
119
120 void EndPass(const char* pass_name) {
121 // Pause timer first, then dump graph.
122 if (timing_logger_enabled_) {
123 timing_logger_.EndTiming();
124 }
125 if (visualizer_enabled_) {
126 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true);
127 }
128 }
129
David Brazdil5e8b1372015-01-23 14:39:08 +0000130 const char* method_name_;
131
132 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000133 TimingLogger timing_logger_;
134
135 bool visualizer_enabled_;
136 HGraphVisualizer visualizer_;
137
David Brazdil809658e2015-02-05 11:34:02 +0000138 friend PassInfo;
139
David Brazdil5e8b1372015-01-23 14:39:08 +0000140 DISALLOW_COPY_AND_ASSIGN(PassInfoPrinter);
141};
142
David Brazdil809658e2015-02-05 11:34:02 +0000143class PassInfo : public ValueObject {
144 public:
145 PassInfo(const char *pass_name, PassInfoPrinter* pass_info_printer)
146 : pass_name_(pass_name),
147 pass_info_printer_(pass_info_printer) {
148 pass_info_printer_->StartPass(pass_name_);
149 }
150
151 ~PassInfo() {
152 pass_info_printer_->EndPass(pass_name_);
153 }
154
155 private:
156 const char* const pass_name_;
157 PassInfoPrinter* const pass_info_printer_;
158};
159
Andreas Gampe53c913b2014-08-12 23:19:23 -0700160class OptimizingCompiler FINAL : public Compiler {
161 public:
162 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100163 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -0700164
165 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
166 OVERRIDE;
167
168 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
169 uint32_t access_flags,
170 InvokeType invoke_type,
171 uint16_t class_def_idx,
172 uint32_t method_idx,
173 jobject class_loader,
174 const DexFile& dex_file) const OVERRIDE;
175
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000176 CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
177 uint32_t access_flags,
178 InvokeType invoke_type,
179 uint16_t class_def_idx,
180 uint32_t method_idx,
181 jobject class_loader,
182 const DexFile& dex_file) const;
183
Andreas Gampe53c913b2014-08-12 23:19:23 -0700184 CompiledMethod* JniCompile(uint32_t access_flags,
185 uint32_t method_idx,
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000186 const DexFile& dex_file) const OVERRIDE {
187 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
188 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700189
190 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000191 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
192 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
193 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
194 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700195
196 bool WriteElf(art::File* file,
197 OatWriter* oat_writer,
198 const std::vector<const art::DexFile*>& dex_files,
199 const std::string& android_root,
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000200 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
201 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
202 *GetCompilerDriver());
203 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700204
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000205 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700206
David Brazdilee690a32014-12-01 17:04:16 +0000207 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700208
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000209 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700210
211 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100212 // Whether we should run any optimization or register allocation. If false, will
213 // just run the code generation after the graph was built.
214 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000215
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000216 // Optimize and compile `graph`.
217 CompiledMethod* CompileOptimized(HGraph* graph,
218 CodeGenerator* codegen,
219 CompilerDriver* driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000220 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000221 const DexCompilationUnit& dex_compilation_unit,
David Brazdil5e8b1372015-01-23 14:39:08 +0000222 PassInfoPrinter* pass_info) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000223
224 // Just compile without doing optimizations.
225 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
226 CompilerDriver* driver,
227 const DexCompilationUnit& dex_compilation_unit) const;
228
Calin Juravle48c2b032014-12-09 18:11:36 +0000229 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100230
Andreas Gampe53c913b2014-08-12 23:19:23 -0700231 std::unique_ptr<std::ostream> visualizer_output_;
232
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000233 // Delegate to Quick in case the optimizing compiler cannot compile a method.
234 std::unique_ptr<Compiler> delegate_;
235
Andreas Gampe53c913b2014-08-12 23:19:23 -0700236 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
237};
238
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100239static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
240
241OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
242 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
243 run_optimizations_(
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000244 (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
245 && !driver->GetCompilerOptions().GetDebuggable()),
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000246 compilation_stats_(),
247 delegate_(Create(driver, Compiler::Kind::kQuick)) {}
David Brazdilee690a32014-12-01 17:04:16 +0000248
249void OptimizingCompiler::Init() {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000250 delegate_->Init();
David Brazdilee690a32014-12-01 17:04:16 +0000251 // Enable C1visualizer output. Must be done in Init() because the compiler
252 // driver is not fully initialized when passed to the compiler's constructor.
253 CompilerDriver* driver = GetCompilerDriver();
David Brazdil866c0312015-01-13 21:21:31 +0000254 const std::string cfg_file_name = driver->GetDumpCfgFileName();
255 if (!cfg_file_name.empty()) {
David Brazdilee690a32014-12-01 17:04:16 +0000256 CHECK_EQ(driver->GetThreadCount(), 1U)
257 << "Graph visualizer requires the compiler to run single-threaded. "
258 << "Invoke the compiler with '-j1'.";
David Brazdil866c0312015-01-13 21:21:31 +0000259 visualizer_output_.reset(new std::ofstream(cfg_file_name));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100260 }
261}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000262
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000263void OptimizingCompiler::UnInit() const {
264 delegate_->UnInit();
265}
266
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100267OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000268 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100269}
270
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000271void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
272 delegate_->InitCompilationUnit(cu);
273}
274
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000275bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
276 const DexFile& dex_file ATTRIBUTE_UNUSED,
277 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
278 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700279}
280
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000281static bool IsInstructionSetSupported(InstructionSet instruction_set) {
282 return instruction_set == kArm64
283 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
284 || instruction_set == kX86
285 || instruction_set == kX86_64;
286}
287
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000288static bool CanOptimize(const DexFile::CodeItem& code_item) {
289 // TODO: We currently cannot optimize methods with try/catch.
290 return code_item.tries_size_ == 0;
291}
292
Calin Juravle10e244f2015-01-26 18:54:32 +0000293static void RunOptimizations(HOptimization* optimizations[],
294 size_t length,
David Brazdil809658e2015-02-05 11:34:02 +0000295 PassInfoPrinter* pass_info_printer) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000296 for (size_t i = 0; i < length; ++i) {
297 HOptimization* optimization = optimizations[i];
David Brazdil809658e2015-02-05 11:34:02 +0000298 {
299 PassInfo pass_info(optimization->GetPassName(), pass_info_printer);
300 optimization->Run();
301 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000302 optimization->Check();
303 }
304}
305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000306static void RunOptimizations(HGraph* graph,
307 CompilerDriver* driver,
308 OptimizingCompilerStats* stats,
Calin Juravleacf735c2015-02-12 15:25:22 +0000309 const DexFile& dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000310 const DexCompilationUnit& dex_compilation_unit,
Calin Juravleacf735c2015-02-12 15:25:22 +0000311 PassInfoPrinter* pass_info_printer,
312 StackHandleScopeCollection* handles) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000313 HDeadCodeElimination dce(graph);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000314 HConstantFolding fold1(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000315 InstructionSimplifier simplify1(graph, stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000316
317 HInliner inliner(graph, dex_compilation_unit, driver, stats);
318
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000319 HConstantFolding fold2(graph);
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000320 SideEffectsAnalysis side_effects(graph);
321 GVNOptimization gvn(graph, side_effects);
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000322 LICM licm(graph, side_effects);
Mingyao Yangf384f882014-10-22 16:08:18 -0700323 BoundsCheckElimination bce(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000324 ReferenceTypePropagation type_propagation(graph, dex_file, dex_compilation_unit, handles);
325 InstructionSimplifier simplify2(graph, stats, "instruction_simplifier_after_types");
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000326
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800327 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
328
Nicolas Geoffray31596742014-11-24 15:28:45 +0000329 HOptimization* optimizations[] = {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800330 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000331 &dce,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000332 &fold1,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000333 &simplify1,
334 &inliner,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000335 &fold2,
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000336 &side_effects,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000337 &gvn,
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000338 &licm,
Mingyao Yangf384f882014-10-22 16:08:18 -0700339 &bce,
Calin Juravle10e244f2015-01-26 18:54:32 +0000340 &type_propagation,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000341 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000342 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000343
David Brazdil809658e2015-02-05 11:34:02 +0000344 RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000345}
346
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000347// The stack map we generate must be 4-byte aligned on ARM. Since existing
348// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800349static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000350 size_t size = vector.size();
351 size_t aligned_size = RoundUp(size, 4);
352 for (; size < aligned_size; ++size) {
353 vector.push_back(0);
354 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800355 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000356}
357
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000358
359CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
360 CodeGenerator* codegen,
361 CompilerDriver* compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000362 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000363 const DexCompilationUnit& dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000364 PassInfoPrinter* pass_info_printer) const {
Calin Juravleacf735c2015-02-12 15:25:22 +0000365 StackHandleScopeCollection handles(Thread::Current());
366 RunOptimizations(graph, compiler_driver, &compilation_stats_,
367 dex_file, dex_compilation_unit, pass_info_printer, &handles);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000368
369 PrepareForRegisterAllocation(graph).Run();
370 SsaLivenessAnalysis liveness(*graph, codegen);
David Brazdil809658e2015-02-05 11:34:02 +0000371 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800372 PassInfo pass_info(SsaLivenessAnalysis::kLivenessPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000373 liveness.Analyze();
374 }
375 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800376 PassInfo pass_info(RegisterAllocator::kRegisterAllocatorPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000377 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
378 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000379
380 CodeVectorAllocator allocator;
381 codegen->CompileOptimized(&allocator);
382
383 std::vector<uint8_t> stack_map;
384 codegen->BuildStackMaps(&stack_map);
385
386 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
387
388 return CompiledMethod::SwapAllocCompiledMethodStackMap(
389 compiler_driver,
390 codegen->GetInstructionSet(),
391 ArrayRef<const uint8_t>(allocator.GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000392 // Follow Quick's behavior and set the frame size to zero if it is
393 // considered "empty" (see the definition of
394 // art::CodeGenerator::HasEmptyFrame).
395 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000396 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000397 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000398 ArrayRef<const uint8_t>(stack_map));
399}
400
401
402CompiledMethod* OptimizingCompiler::CompileBaseline(
403 CodeGenerator* codegen,
404 CompilerDriver* compiler_driver,
405 const DexCompilationUnit& dex_compilation_unit) const {
406 CodeVectorAllocator allocator;
407 codegen->CompileBaseline(&allocator);
408
409 std::vector<uint8_t> mapping_table;
410 DefaultSrcMap src_mapping_table;
411 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
412 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
413 std::vector<uint8_t> vmap_table;
414 codegen->BuildVMapTable(&vmap_table);
415 std::vector<uint8_t> gc_map;
416 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
417
418 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000419 return CompiledMethod::SwapAllocCompiledMethod(
420 compiler_driver,
421 codegen->GetInstructionSet(),
422 ArrayRef<const uint8_t>(allocator.GetMemory()),
423 // Follow Quick's behavior and set the frame size to zero if it is
424 // considered "empty" (see the definition of
425 // art::CodeGenerator::HasEmptyFrame).
426 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
427 codegen->GetCoreSpillMask(),
428 codegen->GetFpuSpillMask(),
429 &src_mapping_table,
430 AlignVectorSize(mapping_table),
431 AlignVectorSize(vmap_table),
432 AlignVectorSize(gc_map),
433 ArrayRef<const uint8_t>());
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000434}
435
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000436CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
437 uint32_t access_flags,
438 InvokeType invoke_type,
439 uint16_t class_def_idx,
440 uint32_t method_idx,
441 jobject class_loader,
442 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700443 UNUSED(invoke_type);
David Brazdil5e8b1372015-01-23 14:39:08 +0000444 std::string method_name = PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000445 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000446 CompilerDriver* compiler_driver = GetCompilerDriver();
447 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100448 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
449 // overflow checks) assume thumb2.
450 if (instruction_set == kArm) {
451 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100452 }
453
454 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000455 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000456 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100457 return nullptr;
458 }
459
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000460 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000461 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000462 return nullptr;
463 }
464
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000465 // Implementation of the space filter: do not compile a code item whose size in
466 // code units is bigger than 256.
467 static constexpr size_t kSpaceFilterOptimizingThreshold = 256;
468 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
469 if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
470 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
471 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
472 return nullptr;
473 }
474
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000475 DexCompilationUnit dex_compilation_unit(
476 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700477 class_def_idx, method_idx, access_flags,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000478 compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000479
David Brazdil5e8b1372015-01-23 14:39:08 +0000480 ArenaPool pool;
481 ArenaAllocator arena(&pool);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000482 HGraph* graph = new (&arena) HGraph(
483 &arena, compiler_driver->GetCompilerOptions().GetDebuggable());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000484
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000485 // For testing purposes, we put a special marker on method names that should be compiled
486 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000487 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000488 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000489
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000490 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000491 CodeGenerator::Create(graph,
492 instruction_set,
493 *compiler_driver->GetInstructionSetFeatures(),
494 compiler_driver->GetCompilerOptions()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000495 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800496 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000497 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000498 return nullptr;
499 }
500
David Brazdil809658e2015-02-05 11:34:02 +0000501 PassInfoPrinter pass_info_printer(graph,
502 method_name.c_str(),
503 *codegen.get(),
504 visualizer_output_.get(),
505 compiler_driver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000506
507 HGraphBuilder builder(graph,
508 &dex_compilation_unit,
509 &dex_compilation_unit,
510 &dex_file,
511 compiler_driver,
512 &compilation_stats_);
513
514 VLOG(compiler) << "Building " << method_name;
515
David Brazdil809658e2015-02-05 11:34:02 +0000516 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800517 PassInfo pass_info(HGraphBuilder::kBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000518 if (!builder.BuildGraph(*code_item)) {
519 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
520 return nullptr;
521 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000522 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100523
Calin Juravle48c2b032014-12-09 18:11:36 +0000524 bool can_optimize = CanOptimize(*code_item);
525 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000526
527 // `run_optimizations_` is set explicitly (either through a compiler filter
528 // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
529 // to Quick.
530 bool can_use_baseline = !run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000531 if (run_optimizations_ && can_optimize && can_allocate_registers) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000532 VLOG(compiler) << "Optimizing " << method_name;
533
David Brazdil809658e2015-02-05 11:34:02 +0000534 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800535 PassInfo pass_info(SsaBuilder::kSsaBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000536 if (!graph->TryBuildingSsa()) {
537 // We could not transform the graph to SSA, bailout.
538 LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
539 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
540 return nullptr;
541 }
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000542 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000543
544 return CompileOptimized(graph,
545 codegen.get(),
546 compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000547 dex_file,
David Brazdil5e8b1372015-01-23 14:39:08 +0000548 dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000549 &pass_info_printer);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000550 } else if (shouldOptimize && can_allocate_registers) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100551 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800552 UNREACHABLE();
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000553 } else if (can_use_baseline) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000554 VLOG(compiler) << "Compile baseline " << method_name;
Calin Juravle48c2b032014-12-09 18:11:36 +0000555
556 if (!run_optimizations_) {
557 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
558 } else if (!can_optimize) {
559 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
560 } else if (!can_allocate_registers) {
561 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
562 }
563
David Brazdil5e8b1372015-01-23 14:39:08 +0000564 return CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000565 } else {
566 return nullptr;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100567 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000568}
569
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000570CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
571 uint32_t access_flags,
572 InvokeType invoke_type,
573 uint16_t class_def_idx,
574 uint32_t method_idx,
575 jobject class_loader,
576 const DexFile& dex_file) const {
577 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
578 method_idx, class_loader, dex_file);
579 if (method != nullptr) {
580 return method;
581 }
582 return delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
583 class_loader, dex_file);
584}
585
Andreas Gampe53c913b2014-08-12 23:19:23 -0700586Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
587 return new OptimizingCompiler(driver);
588}
589
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000590} // namespace art