blob: a8abb0198f612cdbcb989ed34d38ff8969b316ba [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
Mingyao Yangf384f882014-10-22 16:08:18 -070022#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000023#include "builder.h"
24#include "code_generator.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070025#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010026#include "constant_folding.h"
27#include "dead_code_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000028#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000029#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000030#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010031#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010032#include "gvn.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010033#include "instruction_simplifier.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000034#include "jni/quick/jni_compiler.h"
35#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000036#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010037#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010038#include "register_allocator.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000039#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010040#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010041#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000042#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000043
44namespace art {
45
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046/**
47 * Used by the code generator, to allocate the code in a vector.
48 */
49class CodeVectorAllocator FINAL : public CodeAllocator {
50 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010051 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000052
53 virtual uint8_t* Allocate(size_t size) {
54 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000055 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000056 return &memory_[0];
57 }
58
59 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000060 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000061
62 private:
63 std::vector<uint8_t> memory_;
64 size_t size_;
65
66 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
67};
68
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010069/**
70 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
71 */
72static bool kIsVisualizerEnabled = false;
73
74/**
75 * Filter to apply to the visualizer. Methods whose name contain that filter will
76 * be in the file.
77 */
78static const char* kStringFilter = "";
79
Andreas Gampe53c913b2014-08-12 23:19:23 -070080class OptimizingCompiler FINAL : public Compiler {
81 public:
82 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010083 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070084
85 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
86 OVERRIDE;
87
88 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
89 uint32_t access_flags,
90 InvokeType invoke_type,
91 uint16_t class_def_idx,
92 uint32_t method_idx,
93 jobject class_loader,
94 const DexFile& dex_file) const OVERRIDE;
95
Andreas Gampe53c913b2014-08-12 23:19:23 -070096 CompiledMethod* JniCompile(uint32_t access_flags,
97 uint32_t method_idx,
98 const DexFile& dex_file) const OVERRIDE;
99
100 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103 bool WriteElf(art::File* file,
104 OatWriter* oat_writer,
105 const std::vector<const art::DexFile*>& dex_files,
106 const std::string& android_root,
107 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000109 Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
110 void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
111 return nullptr;
112 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700113
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000114 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700115
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000116 void Init() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700117
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000118 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700119
120 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100121 // Whether we should run any optimization or register allocation. If false, will
122 // just run the code generation after the graph was built.
123 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000124
125 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100126
Andreas Gampe53c913b2014-08-12 23:19:23 -0700127 std::unique_ptr<std::ostream> visualizer_output_;
128
Andreas Gampe53c913b2014-08-12 23:19:23 -0700129 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
130};
131
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100132static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
133
134OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
135 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
136 run_optimizations_(
137 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
Calin Juravle48c2b032014-12-09 18:11:36 +0000138 compilation_stats_() {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100139 if (kIsVisualizerEnabled) {
140 visualizer_output_.reset(new std::ofstream("art.cfg"));
141 }
142}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100144OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000145 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100146}
147
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000148bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
149 const DexFile& dex_file ATTRIBUTE_UNUSED,
150 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
151 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700152}
153
154CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
155 uint32_t method_idx,
156 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000157 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700158}
159
160uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000161 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
162 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700163}
164
165bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
166 const std::vector<const art::DexFile*>& dex_files,
167 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000168 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
169 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700170}
171
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000172static bool IsInstructionSetSupported(InstructionSet instruction_set) {
173 return instruction_set == kArm64
174 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
175 || instruction_set == kX86
176 || instruction_set == kX86_64;
177}
178
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000179static bool CanOptimize(const DexFile::CodeItem& code_item) {
180 // TODO: We currently cannot optimize methods with try/catch.
181 return code_item.tries_size_ == 0;
182}
183
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000184static void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) {
185 HDeadCodeElimination opt1(graph);
186 HConstantFolding opt2(graph);
187 SsaRedundantPhiElimination opt3(graph);
188 SsaDeadPhiElimination opt4(graph);
189 InstructionSimplifier opt5(graph);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000190 GVNOptimization opt6(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700191 BoundsCheckElimination bce(graph);
192 InstructionSimplifier opt8(graph);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000193
Nicolas Geoffray31596742014-11-24 15:28:45 +0000194 HOptimization* optimizations[] = {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000195 &opt1,
196 &opt2,
197 &opt3,
198 &opt4,
199 &opt5,
200 &opt6,
Mingyao Yangf384f882014-10-22 16:08:18 -0700201 &bce,
202 &opt8
Nicolas Geoffray31596742014-11-24 15:28:45 +0000203 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000204
205 for (size_t i = 0; i < arraysize(optimizations); ++i) {
206 HOptimization* optimization = optimizations[i];
207 optimization->Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000208 visualizer.DumpGraph(optimization->GetPassName());
Nicolas Geoffray31596742014-11-24 15:28:45 +0000209 optimization->Check();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000210 }
211}
212
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000213static bool TryBuildingSsa(HGraph* graph,
214 const DexCompilationUnit& dex_compilation_unit,
215 const HGraphVisualizer& visualizer) {
216 graph->BuildDominatorTree();
217 graph->TransformToSSA();
218
219 if (!graph->AnalyzeNaturalLoops()) {
220 LOG(INFO) << "Skipping compilation of "
221 << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
222 *dex_compilation_unit.GetDexFile())
223 << ": it contains a non natural loop";
224 return false;
225 }
226 visualizer.DumpGraph("ssa transform");
227 return true;
228}
229
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000230CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
231 uint32_t access_flags,
232 InvokeType invoke_type,
233 uint16_t class_def_idx,
234 uint32_t method_idx,
235 jobject class_loader,
236 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700237 UNUSED(invoke_type);
Calin Juravle48c2b032014-12-09 18:11:36 +0000238 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100239 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100240 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
241 // overflow checks) assume thumb2.
242 if (instruction_set == kArm) {
243 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100244 }
245
246 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000247 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000248 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100249 return nullptr;
250 }
251
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000252 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000253 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000254 return nullptr;
255 }
256
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000257 DexCompilationUnit dex_compilation_unit(
258 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700259 class_def_idx, method_idx, access_flags,
260 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000261
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000262 // For testing purposes, we put a special marker on method names that should be compiled
263 // with this compiler. This makes sure we're not regressing.
264 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100265 bool shouldOptimize =
266 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000267
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000268 ArenaPool pool;
269 ArenaAllocator arena(&pool);
Calin Juravle48c2b032014-12-09 18:11:36 +0000270 HGraphBuilder builder(&arena,
271 &dex_compilation_unit,
272 &dex_file, GetCompilerDriver(),
273 &compilation_stats_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100274
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000275 HGraph* graph = builder.BuildGraph(*code_item);
276 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800277 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000278 return nullptr;
279 }
280
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000281 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
282 if (codegen == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800283 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000284 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 return nullptr;
286 }
287
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100288 HGraphVisualizer visualizer(
289 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
290 visualizer.DumpGraph("builder");
291
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000292 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100293
Calin Juravle48c2b032014-12-09 18:11:36 +0000294 bool can_optimize = CanOptimize(*code_item);
295 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
296 if (run_optimizations_ && can_optimize && can_allocate_registers) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000297 VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000298 if (!TryBuildingSsa(graph, dex_compilation_unit, visualizer)) {
299 // We could not transform the graph to SSA, bailout.
Calin Juravle48c2b032014-12-09 18:11:36 +0000300 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000301 return nullptr;
302 }
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000303 RunOptimizations(graph, visualizer);
Roland Levillain75be2832014-10-17 17:02:00 +0100304
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100305 PrepareForRegisterAllocation(graph).Run();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100306 SsaLivenessAnalysis liveness(*graph, codegen);
307 liveness.Analyze();
308 visualizer.DumpGraph(kLivenessPassName);
309
310 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
311 register_allocator.AllocateRegisters();
312
313 visualizer.DumpGraph(kRegisterAllocatorPassName);
314 codegen->CompileOptimized(&allocator);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100315
316 std::vector<uint8_t> mapping_table;
317 SrcMap src_mapping_table;
318 codegen->BuildMappingTable(&mapping_table,
319 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
320 &src_mapping_table : nullptr);
321
322 std::vector<uint8_t> stack_map;
323 codegen->BuildStackMaps(&stack_map);
324
Calin Juravle48c2b032014-12-09 18:11:36 +0000325 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100326 return new CompiledMethod(GetCompilerDriver(),
327 instruction_set,
328 allocator.GetMemory(),
329 codegen->GetFrameSize(),
330 codegen->GetCoreSpillMask(),
331 0, /* FPR spill mask, unused */
332 mapping_table,
333 stack_map);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100334 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
335 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800336 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100337 } else {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000338 VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000339
340 if (!run_optimizations_) {
341 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
342 } else if (!can_optimize) {
343 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
344 } else if (!can_allocate_registers) {
345 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
346 }
347
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100348 codegen->CompileBaseline(&allocator);
349
Nicolas Geoffray39468442014-09-02 15:17:15 +0100350 std::vector<uint8_t> mapping_table;
351 SrcMap src_mapping_table;
352 codegen->BuildMappingTable(&mapping_table,
353 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
354 &src_mapping_table : nullptr);
355 std::vector<uint8_t> vmap_table;
356 codegen->BuildVMapTable(&vmap_table);
357 std::vector<uint8_t> gc_map;
358 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
359
Calin Juravle48c2b032014-12-09 18:11:36 +0000360 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100361 return new CompiledMethod(GetCompilerDriver(),
362 instruction_set,
363 allocator.GetMemory(),
364 codegen->GetFrameSize(),
365 codegen->GetCoreSpillMask(),
366 0, /* FPR spill mask, unused */
367 &src_mapping_table,
368 mapping_table,
369 vmap_table,
370 gc_map,
371 nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100372 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000373}
374
Andreas Gampe53c913b2014-08-12 23:19:23 -0700375Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
376 return new OptimizingCompiler(driver);
377}
378
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000379} // namespace art