blob: 83a1e11a98196ee849ae18994b0de50a5faa0855 [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
22#include "builder.h"
23#include "code_generator.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070024#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010025#include "constant_folding.h"
26#include "dead_code_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000027#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000028#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000029#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010030#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010031#include "gvn.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "instruction_simplifier.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000033#include "jni/quick/jni_compiler.h"
34#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000035#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010036#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010037#include "register_allocator.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010038#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010039#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000041
42namespace art {
43
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044/**
45 * Used by the code generator, to allocate the code in a vector.
46 */
47class CodeVectorAllocator FINAL : public CodeAllocator {
48 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010049 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050
51 virtual uint8_t* Allocate(size_t size) {
52 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000053 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000054 return &memory_[0];
55 }
56
57 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000058 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000059
60 private:
61 std::vector<uint8_t> memory_;
62 size_t size_;
63
64 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
65};
66
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010067/**
68 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
69 */
70static bool kIsVisualizerEnabled = false;
71
72/**
73 * Filter to apply to the visualizer. Methods whose name contain that filter will
74 * be in the file.
75 */
76static const char* kStringFilter = "";
77
Andreas Gampe53c913b2014-08-12 23:19:23 -070078class OptimizingCompiler FINAL : public Compiler {
79 public:
80 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010081 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070082
83 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
84 OVERRIDE;
85
86 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
87 uint32_t access_flags,
88 InvokeType invoke_type,
89 uint16_t class_def_idx,
90 uint32_t method_idx,
91 jobject class_loader,
92 const DexFile& dex_file) const OVERRIDE;
93
Andreas Gampe53c913b2014-08-12 23:19:23 -070094 CompiledMethod* JniCompile(uint32_t access_flags,
95 uint32_t method_idx,
96 const DexFile& dex_file) const OVERRIDE;
97
98 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100
101 bool WriteElf(art::File* file,
102 OatWriter* oat_writer,
103 const std::vector<const art::DexFile*>& dex_files,
104 const std::string& android_root,
105 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000107 Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
108 void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
109 return nullptr;
110 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700111
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000112 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700113
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000114 void Init() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700115
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000116 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700117
118 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100119 // Whether we should run any optimization or register allocation. If false, will
120 // just run the code generation after the graph was built.
121 const bool run_optimizations_;
122 mutable AtomicInteger total_compiled_methods_;
123 mutable AtomicInteger unoptimized_compiled_methods_;
124 mutable AtomicInteger optimized_compiled_methods_;
125
Andreas Gampe53c913b2014-08-12 23:19:23 -0700126 std::unique_ptr<std::ostream> visualizer_output_;
127
Andreas Gampe53c913b2014-08-12 23:19:23 -0700128 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
129};
130
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100131static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
132
133OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
134 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
135 run_optimizations_(
136 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
137 total_compiled_methods_(0),
138 unoptimized_compiled_methods_(0),
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000139 optimized_compiled_methods_(0) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100140 if (kIsVisualizerEnabled) {
141 visualizer_output_.reset(new std::ofstream("art.cfg"));
142 }
143}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000144
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100145OptimizingCompiler::~OptimizingCompiler() {
Nicolas Geoffrayce71ae72014-09-18 11:31:32 +0100146 if (total_compiled_methods_ == 0) {
147 LOG(INFO) << "Did not compile any method.";
148 } else {
149 size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_);
150 size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_);
151 LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: "
152 << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, "
153 << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized.";
154 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100155}
156
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000157bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
158 const DexFile& dex_file ATTRIBUTE_UNUSED,
159 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
160 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700161}
162
163CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
164 uint32_t method_idx,
165 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000166 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700167}
168
169uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000170 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700171}
172
173bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
174 const std::vector<const art::DexFile*>& dex_files,
175 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000176 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
177 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700178}
179
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000180static bool IsInstructionSetSupported(InstructionSet instruction_set) {
181 return instruction_set == kArm64
182 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
183 || instruction_set == kX86
184 || instruction_set == kX86_64;
185}
186
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000187static bool CanOptimize(const DexFile::CodeItem& code_item) {
188 // TODO: We currently cannot optimize methods with try/catch.
189 return code_item.tries_size_ == 0;
190}
191
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000192static void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) {
193 HDeadCodeElimination opt1(graph);
194 HConstantFolding opt2(graph);
195 SsaRedundantPhiElimination opt3(graph);
196 SsaDeadPhiElimination opt4(graph);
197 InstructionSimplifier opt5(graph);
198 GlobalValueNumberer opt6(graph->GetArena(), graph);
199 InstructionSimplifier opt7(graph);
200
201 HOptimization* optimizations[] = { &opt1, &opt2, &opt3, &opt4, &opt5, &opt6, &opt7 };
202
203 for (size_t i = 0; i < arraysize(optimizations); ++i) {
204 HOptimization* optimization = optimizations[i];
205 optimization->Run();
206 optimization->Check();
207 visualizer.DumpGraph(optimization->GetPassName());
208 }
209}
210
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000211CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
212 uint32_t access_flags,
213 InvokeType invoke_type,
214 uint16_t class_def_idx,
215 uint32_t method_idx,
216 jobject class_loader,
217 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700218 UNUSED(invoke_type);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100219 total_compiled_methods_++;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100220 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100221 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
222 // overflow checks) assume thumb2.
223 if (instruction_set == kArm) {
224 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100225 }
226
227 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000228 if (!IsInstructionSetSupported(instruction_set)) {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100229 return nullptr;
230 }
231
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
233 return nullptr;
234 }
235
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000236 DexCompilationUnit dex_compilation_unit(
237 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700238 class_def_idx, method_idx, access_flags,
239 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000240
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000241 // For testing purposes, we put a special marker on method names that should be compiled
242 // with this compiler. This makes sure we're not regressing.
243 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100244 bool shouldOptimize =
245 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000246
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000247 ArenaPool pool;
248 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100249 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100250
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000251 HGraph* graph = builder.BuildGraph(*code_item);
252 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800253 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000254 return nullptr;
255 }
256
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000257 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
258 if (codegen == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800259 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000260 return nullptr;
261 }
262
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100263 HGraphVisualizer visualizer(
264 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
265 visualizer.DumpGraph("builder");
266
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000267 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100268
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000269 if (run_optimizations_
270 && CanOptimize(*code_item)
271 && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100272 optimized_compiled_methods_++;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100273 graph->BuildDominatorTree();
274 graph->TransformToSSA();
275 visualizer.DumpGraph("ssa");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100276 graph->FindNaturalLoops();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100277
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000278 RunOptimizations(graph, visualizer);
Roland Levillain75be2832014-10-17 17:02:00 +0100279
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100280 PrepareForRegisterAllocation(graph).Run();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100281 SsaLivenessAnalysis liveness(*graph, codegen);
282 liveness.Analyze();
283 visualizer.DumpGraph(kLivenessPassName);
284
285 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
286 register_allocator.AllocateRegisters();
287
288 visualizer.DumpGraph(kRegisterAllocatorPassName);
289 codegen->CompileOptimized(&allocator);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100290
291 std::vector<uint8_t> mapping_table;
292 SrcMap src_mapping_table;
293 codegen->BuildMappingTable(&mapping_table,
294 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
295 &src_mapping_table : nullptr);
296
297 std::vector<uint8_t> stack_map;
298 codegen->BuildStackMaps(&stack_map);
299
300 return new CompiledMethod(GetCompilerDriver(),
301 instruction_set,
302 allocator.GetMemory(),
303 codegen->GetFrameSize(),
304 codegen->GetCoreSpillMask(),
305 0, /* FPR spill mask, unused */
306 mapping_table,
307 stack_map);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100308 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
309 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800310 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100311 } else {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100312 unoptimized_compiled_methods_++;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100313 codegen->CompileBaseline(&allocator);
314
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000315 if (CanOptimize(*code_item)) {
316 // Run these phases to get some test coverage.
317 graph->BuildDominatorTree();
318 graph->TransformToSSA();
319 visualizer.DumpGraph("ssa");
320 graph->FindNaturalLoops();
321 SsaRedundantPhiElimination(graph).Run();
322 SsaDeadPhiElimination(graph).Run();
323 GlobalValueNumberer(graph->GetArena(), graph).Run();
324 SsaLivenessAnalysis liveness(*graph, codegen);
325 liveness.Analyze();
326 visualizer.DumpGraph(kLivenessPassName);
327 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100328
329 std::vector<uint8_t> mapping_table;
330 SrcMap src_mapping_table;
331 codegen->BuildMappingTable(&mapping_table,
332 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
333 &src_mapping_table : nullptr);
334 std::vector<uint8_t> vmap_table;
335 codegen->BuildVMapTable(&vmap_table);
336 std::vector<uint8_t> gc_map;
337 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
338
339 return new CompiledMethod(GetCompilerDriver(),
340 instruction_set,
341 allocator.GetMemory(),
342 codegen->GetFrameSize(),
343 codegen->GetCoreSpillMask(),
344 0, /* FPR spill mask, unused */
345 &src_mapping_table,
346 mapping_table,
347 vmap_table,
348 gc_map,
349 nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100350 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000351}
352
Andreas Gampe53c913b2014-08-12 23:19:23 -0700353Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
354 return new OptimizingCompiler(driver);
355}
356
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000357} // namespace art