blob: e47b4f61b259f101bca38390b99cd90c301ca44e [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
Andreas Gampe53c913b2014-08-12 23:19:23 -0700176 CompiledMethod* JniCompile(uint32_t access_flags,
177 uint32_t method_idx,
178 const DexFile& dex_file) const OVERRIDE;
179
180 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
182
183 bool WriteElf(art::File* file,
184 OatWriter* oat_writer,
185 const std::vector<const art::DexFile*>& dex_files,
186 const std::string& android_root,
187 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
188
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000189 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700190
David Brazdilee690a32014-12-01 17:04:16 +0000191 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700192
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000193 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700194
195 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100196 // Whether we should run any optimization or register allocation. If false, will
197 // just run the code generation after the graph was built.
198 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000199
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000200 // Optimize and compile `graph`.
201 CompiledMethod* CompileOptimized(HGraph* graph,
202 CodeGenerator* codegen,
203 CompilerDriver* driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000204 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000205 const DexCompilationUnit& dex_compilation_unit,
David Brazdil5e8b1372015-01-23 14:39:08 +0000206 PassInfoPrinter* pass_info) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000207
208 // Just compile without doing optimizations.
209 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
210 CompilerDriver* driver,
211 const DexCompilationUnit& dex_compilation_unit) const;
212
Calin Juravle48c2b032014-12-09 18:11:36 +0000213 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100214
Andreas Gampe53c913b2014-08-12 23:19:23 -0700215 std::unique_ptr<std::ostream> visualizer_output_;
216
Andreas Gampe53c913b2014-08-12 23:19:23 -0700217 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
218};
219
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100220static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
221
222OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
223 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
224 run_optimizations_(
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000225 (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
226 && !driver->GetCompilerOptions().GetDebuggable()),
David Brazdilee690a32014-12-01 17:04:16 +0000227 compilation_stats_() {}
228
229void OptimizingCompiler::Init() {
230 // Enable C1visualizer output. Must be done in Init() because the compiler
231 // driver is not fully initialized when passed to the compiler's constructor.
232 CompilerDriver* driver = GetCompilerDriver();
David Brazdil866c0312015-01-13 21:21:31 +0000233 const std::string cfg_file_name = driver->GetDumpCfgFileName();
234 if (!cfg_file_name.empty()) {
David Brazdilee690a32014-12-01 17:04:16 +0000235 CHECK_EQ(driver->GetThreadCount(), 1U)
236 << "Graph visualizer requires the compiler to run single-threaded. "
237 << "Invoke the compiler with '-j1'.";
David Brazdil866c0312015-01-13 21:21:31 +0000238 visualizer_output_.reset(new std::ofstream(cfg_file_name));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100239 }
240}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000241
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100242OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000243 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100244}
245
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000246bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
247 const DexFile& dex_file ATTRIBUTE_UNUSED,
248 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
249 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700250}
251
252CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
253 uint32_t method_idx,
254 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000255 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700256}
257
258uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000259 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
260 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700261}
262
263bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
264 const std::vector<const art::DexFile*>& dex_files,
265 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000266 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
267 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700268}
269
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000270static bool IsInstructionSetSupported(InstructionSet instruction_set) {
271 return instruction_set == kArm64
272 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
273 || instruction_set == kX86
274 || instruction_set == kX86_64;
275}
276
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000277static bool CanOptimize(const DexFile::CodeItem& code_item) {
278 // TODO: We currently cannot optimize methods with try/catch.
279 return code_item.tries_size_ == 0;
280}
281
Calin Juravle10e244f2015-01-26 18:54:32 +0000282static void RunOptimizations(HOptimization* optimizations[],
283 size_t length,
David Brazdil809658e2015-02-05 11:34:02 +0000284 PassInfoPrinter* pass_info_printer) {
Calin Juravle10e244f2015-01-26 18:54:32 +0000285 for (size_t i = 0; i < length; ++i) {
286 HOptimization* optimization = optimizations[i];
David Brazdil809658e2015-02-05 11:34:02 +0000287 {
288 PassInfo pass_info(optimization->GetPassName(), pass_info_printer);
289 optimization->Run();
290 }
Calin Juravle10e244f2015-01-26 18:54:32 +0000291 optimization->Check();
292 }
293}
294
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000295static void RunOptimizations(HGraph* graph,
296 CompilerDriver* driver,
297 OptimizingCompilerStats* stats,
Calin Juravleacf735c2015-02-12 15:25:22 +0000298 const DexFile& dex_file,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000299 const DexCompilationUnit& dex_compilation_unit,
Calin Juravleacf735c2015-02-12 15:25:22 +0000300 PassInfoPrinter* pass_info_printer,
301 StackHandleScopeCollection* handles) {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000302 HDeadCodeElimination dce(graph);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000303 HConstantFolding fold1(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000304 InstructionSimplifier simplify1(graph, stats);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000305
306 HInliner inliner(graph, dex_compilation_unit, driver, stats);
307
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000308 HConstantFolding fold2(graph);
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000309 SideEffectsAnalysis side_effects(graph);
310 GVNOptimization gvn(graph, side_effects);
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000311 LICM licm(graph, side_effects);
Mingyao Yangf384f882014-10-22 16:08:18 -0700312 BoundsCheckElimination bce(graph);
Calin Juravleacf735c2015-02-12 15:25:22 +0000313 ReferenceTypePropagation type_propagation(graph, dex_file, dex_compilation_unit, handles);
314 InstructionSimplifier simplify2(graph, stats, "instruction_simplifier_after_types");
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000315
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800316 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
317
Nicolas Geoffray31596742014-11-24 15:28:45 +0000318 HOptimization* optimizations[] = {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800319 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000320 &dce,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000321 &fold1,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000322 &simplify1,
323 &inliner,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000324 &fold2,
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000325 &side_effects,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000326 &gvn,
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000327 &licm,
Mingyao Yangf384f882014-10-22 16:08:18 -0700328 &bce,
Calin Juravle10e244f2015-01-26 18:54:32 +0000329 &type_propagation,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000330 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000331 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000332
David Brazdil809658e2015-02-05 11:34:02 +0000333 RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000334}
335
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000336// The stack map we generate must be 4-byte aligned on ARM. Since existing
337// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800338static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000339 size_t size = vector.size();
340 size_t aligned_size = RoundUp(size, 4);
341 for (; size < aligned_size; ++size) {
342 vector.push_back(0);
343 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800344 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000345}
346
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000347
348CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
349 CodeGenerator* codegen,
350 CompilerDriver* compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000351 const DexFile& dex_file,
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000352 const DexCompilationUnit& dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000353 PassInfoPrinter* pass_info_printer) const {
Calin Juravleacf735c2015-02-12 15:25:22 +0000354 StackHandleScopeCollection handles(Thread::Current());
355 RunOptimizations(graph, compiler_driver, &compilation_stats_,
356 dex_file, dex_compilation_unit, pass_info_printer, &handles);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000357
358 PrepareForRegisterAllocation(graph).Run();
359 SsaLivenessAnalysis liveness(*graph, codegen);
David Brazdil809658e2015-02-05 11:34:02 +0000360 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800361 PassInfo pass_info(SsaLivenessAnalysis::kLivenessPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000362 liveness.Analyze();
363 }
364 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800365 PassInfo pass_info(RegisterAllocator::kRegisterAllocatorPassName, pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000366 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
367 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000368
369 CodeVectorAllocator allocator;
370 codegen->CompileOptimized(&allocator);
371
372 std::vector<uint8_t> stack_map;
373 codegen->BuildStackMaps(&stack_map);
374
375 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
376
377 return CompiledMethod::SwapAllocCompiledMethodStackMap(
378 compiler_driver,
379 codegen->GetInstructionSet(),
380 ArrayRef<const uint8_t>(allocator.GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000381 // Follow Quick's behavior and set the frame size to zero if it is
382 // considered "empty" (see the definition of
383 // art::CodeGenerator::HasEmptyFrame).
384 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000385 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000386 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000387 ArrayRef<const uint8_t>(stack_map));
388}
389
390
391CompiledMethod* OptimizingCompiler::CompileBaseline(
392 CodeGenerator* codegen,
393 CompilerDriver* compiler_driver,
394 const DexCompilationUnit& dex_compilation_unit) const {
395 CodeVectorAllocator allocator;
396 codegen->CompileBaseline(&allocator);
397
398 std::vector<uint8_t> mapping_table;
399 DefaultSrcMap src_mapping_table;
400 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
401 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
402 std::vector<uint8_t> vmap_table;
403 codegen->BuildVMapTable(&vmap_table);
404 std::vector<uint8_t> gc_map;
405 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
406
407 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000408 return CompiledMethod::SwapAllocCompiledMethod(
409 compiler_driver,
410 codegen->GetInstructionSet(),
411 ArrayRef<const uint8_t>(allocator.GetMemory()),
412 // Follow Quick's behavior and set the frame size to zero if it is
413 // considered "empty" (see the definition of
414 // art::CodeGenerator::HasEmptyFrame).
415 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
416 codegen->GetCoreSpillMask(),
417 codegen->GetFpuSpillMask(),
418 &src_mapping_table,
419 AlignVectorSize(mapping_table),
420 AlignVectorSize(vmap_table),
421 AlignVectorSize(gc_map),
422 ArrayRef<const uint8_t>());
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000423}
424
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000425CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
426 uint32_t access_flags,
427 InvokeType invoke_type,
428 uint16_t class_def_idx,
429 uint32_t method_idx,
430 jobject class_loader,
431 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700432 UNUSED(invoke_type);
David Brazdil5e8b1372015-01-23 14:39:08 +0000433 std::string method_name = PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000434 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000435 CompilerDriver* compiler_driver = GetCompilerDriver();
436 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100437 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
438 // overflow checks) assume thumb2.
439 if (instruction_set == kArm) {
440 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100441 }
442
443 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000444 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000445 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100446 return nullptr;
447 }
448
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000449 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000450 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000451 return nullptr;
452 }
453
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000454 DexCompilationUnit dex_compilation_unit(
455 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700456 class_def_idx, method_idx, access_flags,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000457 compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000458
David Brazdil5e8b1372015-01-23 14:39:08 +0000459 ArenaPool pool;
460 ArenaAllocator arena(&pool);
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000461 HGraph* graph = new (&arena) HGraph(
462 &arena, compiler_driver->GetCompilerOptions().GetDebuggable());
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000463
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000464 // For testing purposes, we put a special marker on method names that should be compiled
465 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000466 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
Nicolas Geoffraya3d90fb2015-03-16 13:55:40 +0000467 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000468
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000469 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000470 CodeGenerator::Create(graph,
471 instruction_set,
472 *compiler_driver->GetInstructionSetFeatures(),
473 compiler_driver->GetCompilerOptions()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000474 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800475 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000476 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000477 return nullptr;
478 }
479
David Brazdil809658e2015-02-05 11:34:02 +0000480 PassInfoPrinter pass_info_printer(graph,
481 method_name.c_str(),
482 *codegen.get(),
483 visualizer_output_.get(),
484 compiler_driver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000485
486 HGraphBuilder builder(graph,
487 &dex_compilation_unit,
488 &dex_compilation_unit,
489 &dex_file,
490 compiler_driver,
491 &compilation_stats_);
492
493 VLOG(compiler) << "Building " << method_name;
494
David Brazdil809658e2015-02-05 11:34:02 +0000495 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800496 PassInfo pass_info(HGraphBuilder::kBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000497 if (!builder.BuildGraph(*code_item)) {
498 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
499 return nullptr;
500 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000501 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100502
Calin Juravle48c2b032014-12-09 18:11:36 +0000503 bool can_optimize = CanOptimize(*code_item);
504 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
505 if (run_optimizations_ && can_optimize && can_allocate_registers) {
David Brazdil5e8b1372015-01-23 14:39:08 +0000506 VLOG(compiler) << "Optimizing " << method_name;
507
David Brazdil809658e2015-02-05 11:34:02 +0000508 {
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800509 PassInfo pass_info(SsaBuilder::kSsaBuilderPassName, &pass_info_printer);
David Brazdil809658e2015-02-05 11:34:02 +0000510 if (!graph->TryBuildingSsa()) {
511 // We could not transform the graph to SSA, bailout.
512 LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
513 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
514 return nullptr;
515 }
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000516 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000517
518 return CompileOptimized(graph,
519 codegen.get(),
520 compiler_driver,
Calin Juravleacf735c2015-02-12 15:25:22 +0000521 dex_file,
David Brazdil5e8b1372015-01-23 14:39:08 +0000522 dex_compilation_unit,
David Brazdil809658e2015-02-05 11:34:02 +0000523 &pass_info_printer);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000524 } else if (shouldOptimize && can_allocate_registers) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100525 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800526 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100527 } else {
David Brazdil5e8b1372015-01-23 14:39:08 +0000528 VLOG(compiler) << "Compile baseline " << method_name;
Calin Juravle48c2b032014-12-09 18:11:36 +0000529
530 if (!run_optimizations_) {
531 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
532 } else if (!can_optimize) {
533 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
534 } else if (!can_allocate_registers) {
535 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
536 }
537
David Brazdil5e8b1372015-01-23 14:39:08 +0000538 return CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100539 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000540}
541
Andreas Gampe53c913b2014-08-12 23:19:23 -0700542Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
543 return new OptimizingCompiler(driver);
544}
545
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000546} // namespace art