blob: d8533eb8bfe1c76977a34dcaadb38553d29322a8 [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 Geoffray31596742014-11-24 15:28:45 +000038#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010039#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010040#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000041#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000042
43namespace art {
44
Nicolas Geoffray787c3072014-03-17 10:20:19 +000045/**
46 * Used by the code generator, to allocate the code in a vector.
47 */
48class CodeVectorAllocator FINAL : public CodeAllocator {
49 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010050 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051
52 virtual uint8_t* Allocate(size_t size) {
53 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000054 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000055 return &memory_[0];
56 }
57
58 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000059 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000060
61 private:
62 std::vector<uint8_t> memory_;
63 size_t size_;
64
65 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
66};
67
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010068/**
69 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
70 */
71static bool kIsVisualizerEnabled = false;
72
73/**
74 * Filter to apply to the visualizer. Methods whose name contain that filter will
75 * be in the file.
76 */
77static const char* kStringFilter = "";
78
Andreas Gampe53c913b2014-08-12 23:19:23 -070079class OptimizingCompiler FINAL : public Compiler {
80 public:
81 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010082 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070083
84 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
85 OVERRIDE;
86
87 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
88 uint32_t access_flags,
89 InvokeType invoke_type,
90 uint16_t class_def_idx,
91 uint32_t method_idx,
92 jobject class_loader,
93 const DexFile& dex_file) const OVERRIDE;
94
Andreas Gampe53c913b2014-08-12 23:19:23 -070095 CompiledMethod* JniCompile(uint32_t access_flags,
96 uint32_t method_idx,
97 const DexFile& dex_file) const OVERRIDE;
98
99 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
101
102 bool WriteElf(art::File* file,
103 OatWriter* oat_writer,
104 const std::vector<const art::DexFile*>& dex_files,
105 const std::string& android_root,
106 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
107
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000108 Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
109 void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
110 return nullptr;
111 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700112
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000113 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700114
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000115 void Init() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700116
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000117 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700118
119 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100120 // Whether we should run any optimization or register allocation. If false, will
121 // just run the code generation after the graph was built.
122 const bool run_optimizations_;
123 mutable AtomicInteger total_compiled_methods_;
124 mutable AtomicInteger unoptimized_compiled_methods_;
125 mutable AtomicInteger optimized_compiled_methods_;
126
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),
138 total_compiled_methods_(0),
139 unoptimized_compiled_methods_(0),
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000140 optimized_compiled_methods_(0) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100141 if (kIsVisualizerEnabled) {
142 visualizer_output_.reset(new std::ofstream("art.cfg"));
143 }
144}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000145
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100146OptimizingCompiler::~OptimizingCompiler() {
Nicolas Geoffrayce71ae72014-09-18 11:31:32 +0100147 if (total_compiled_methods_ == 0) {
148 LOG(INFO) << "Did not compile any method.";
149 } else {
150 size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_);
151 size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_);
152 LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: "
153 << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, "
154 << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized.";
155 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100156}
157
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000158bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
159 const DexFile& dex_file ATTRIBUTE_UNUSED,
160 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
161 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700162}
163
164CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
165 uint32_t method_idx,
166 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000167 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700168}
169
170uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000171 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
172 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700173}
174
175bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
176 const std::vector<const art::DexFile*>& dex_files,
177 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000178 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
179 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700180}
181
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000182static bool IsInstructionSetSupported(InstructionSet instruction_set) {
183 return instruction_set == kArm64
184 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
185 || instruction_set == kX86
186 || instruction_set == kX86_64;
187}
188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000189static bool CanOptimize(const DexFile::CodeItem& code_item) {
190 // TODO: We currently cannot optimize methods with try/catch.
191 return code_item.tries_size_ == 0;
192}
193
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000194static void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) {
Nicolas Geoffray31596742014-11-24 15:28:45 +0000195 TransformToSsa ssa(graph);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000196 HDeadCodeElimination opt1(graph);
197 HConstantFolding opt2(graph);
198 SsaRedundantPhiElimination opt3(graph);
199 SsaDeadPhiElimination opt4(graph);
200 InstructionSimplifier opt5(graph);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000201 GVNOptimization opt6(graph);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000202 InstructionSimplifier opt7(graph);
203
Nicolas Geoffray31596742014-11-24 15:28:45 +0000204 HOptimization* optimizations[] = {
205 &ssa,
206 &opt1,
207 &opt2,
208 &opt3,
209 &opt4,
210 &opt5,
211 &opt6,
212 &opt7
213 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000214
215 for (size_t i = 0; i < arraysize(optimizations); ++i) {
216 HOptimization* optimization = optimizations[i];
217 optimization->Run();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000218 visualizer.DumpGraph(optimization->GetPassName());
Nicolas Geoffray31596742014-11-24 15:28:45 +0000219 optimization->Check();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000220 }
221}
222
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000223CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
224 uint32_t access_flags,
225 InvokeType invoke_type,
226 uint16_t class_def_idx,
227 uint32_t method_idx,
228 jobject class_loader,
229 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230 UNUSED(invoke_type);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100231 total_compiled_methods_++;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100232 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100233 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
234 // overflow checks) assume thumb2.
235 if (instruction_set == kArm) {
236 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100237 }
238
239 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000240 if (!IsInstructionSetSupported(instruction_set)) {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100241 return nullptr;
242 }
243
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000244 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
245 return nullptr;
246 }
247
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000248 DexCompilationUnit dex_compilation_unit(
249 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700250 class_def_idx, method_idx, access_flags,
251 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000252
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000253 // For testing purposes, we put a special marker on method names that should be compiled
254 // with this compiler. This makes sure we're not regressing.
255 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100256 bool shouldOptimize =
257 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000258
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000259 ArenaPool pool;
260 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100262
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000263 HGraph* graph = builder.BuildGraph(*code_item);
264 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800265 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000266 return nullptr;
267 }
268
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000269 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
270 if (codegen == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800271 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 return nullptr;
273 }
274
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 HGraphVisualizer visualizer(
276 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
277 visualizer.DumpGraph("builder");
278
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000279 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100280
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000281 if (run_optimizations_
282 && CanOptimize(*code_item)
283 && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100284 optimized_compiled_methods_++;
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000285 RunOptimizations(graph, visualizer);
Roland Levillain75be2832014-10-17 17:02:00 +0100286
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +0100287 PrepareForRegisterAllocation(graph).Run();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100288 SsaLivenessAnalysis liveness(*graph, codegen);
289 liveness.Analyze();
290 visualizer.DumpGraph(kLivenessPassName);
291
292 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
293 register_allocator.AllocateRegisters();
294
295 visualizer.DumpGraph(kRegisterAllocatorPassName);
296 codegen->CompileOptimized(&allocator);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100297
298 std::vector<uint8_t> mapping_table;
299 SrcMap src_mapping_table;
300 codegen->BuildMappingTable(&mapping_table,
301 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
302 &src_mapping_table : nullptr);
303
304 std::vector<uint8_t> stack_map;
305 codegen->BuildStackMaps(&stack_map);
306
307 return new CompiledMethod(GetCompilerDriver(),
308 instruction_set,
309 allocator.GetMemory(),
310 codegen->GetFrameSize(),
311 codegen->GetCoreSpillMask(),
312 0, /* FPR spill mask, unused */
313 mapping_table,
314 stack_map);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100315 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
316 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800317 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100318 } else {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100319 unoptimized_compiled_methods_++;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100320 codegen->CompileBaseline(&allocator);
321
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000322 if (CanOptimize(*code_item)) {
323 // Run these phases to get some test coverage.
324 graph->BuildDominatorTree();
325 graph->TransformToSSA();
326 visualizer.DumpGraph("ssa");
327 graph->FindNaturalLoops();
328 SsaRedundantPhiElimination(graph).Run();
329 SsaDeadPhiElimination(graph).Run();
Nicolas Geoffray31596742014-11-24 15:28:45 +0000330 GVNOptimization(graph).Run();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000331 SsaLivenessAnalysis liveness(*graph, codegen);
332 liveness.Analyze();
333 visualizer.DumpGraph(kLivenessPassName);
334 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100335
336 std::vector<uint8_t> mapping_table;
337 SrcMap src_mapping_table;
338 codegen->BuildMappingTable(&mapping_table,
339 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
340 &src_mapping_table : nullptr);
341 std::vector<uint8_t> vmap_table;
342 codegen->BuildVMapTable(&vmap_table);
343 std::vector<uint8_t> gc_map;
344 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
345
346 return new CompiledMethod(GetCompilerDriver(),
347 instruction_set,
348 allocator.GetMemory(),
349 codegen->GetFrameSize(),
350 codegen->GetCoreSpillMask(),
351 0, /* FPR spill mask, unused */
352 &src_mapping_table,
353 mapping_table,
354 vmap_table,
355 gc_map,
356 nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100357 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000358}
359
Andreas Gampe53c913b2014-08-12 23:19:23 -0700360Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
361 return new OptimizingCompiler(driver);
362}
363
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000364} // namespace art