blob: ee5091bbb5df342ecef85bf436851bb6e206f6ce [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 Levillainbf9cd7b2014-09-30 16:15:14 +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 Geoffrayf635e632014-05-14 09:43:38 +010029#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010030#include "gvn.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010031#include "instruction_simplifier.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000032#include "nodes.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010033#include "register_allocator.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010034#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010035#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000036#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000037
38namespace art {
39
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040/**
41 * Used by the code generator, to allocate the code in a vector.
42 */
43class CodeVectorAllocator FINAL : public CodeAllocator {
44 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010045 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046
47 virtual uint8_t* Allocate(size_t size) {
48 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000049 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050 return &memory_[0];
51 }
52
53 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000054 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000055
56 private:
57 std::vector<uint8_t> memory_;
58 size_t size_;
59
60 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
61};
62
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010063/**
64 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
65 */
66static bool kIsVisualizerEnabled = false;
67
68/**
69 * Filter to apply to the visualizer. Methods whose name contain that filter will
70 * be in the file.
71 */
72static const char* kStringFilter = "";
73
Andreas Gampe53c913b2014-08-12 23:19:23 -070074class OptimizingCompiler FINAL : public Compiler {
75 public:
76 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010077 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070078
79 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
80 OVERRIDE;
81
82 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
83 uint32_t access_flags,
84 InvokeType invoke_type,
85 uint16_t class_def_idx,
86 uint32_t method_idx,
87 jobject class_loader,
88 const DexFile& dex_file) const OVERRIDE;
89
90 CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
91 uint32_t access_flags,
92 InvokeType invoke_type,
93 uint16_t class_def_idx,
94 uint32_t method_idx,
95 jobject class_loader,
96 const DexFile& dex_file) const;
97
98 // For the following methods we will use the fallback. This is a delegation pattern.
99 CompiledMethod* JniCompile(uint32_t access_flags,
100 uint32_t method_idx,
101 const DexFile& dex_file) const OVERRIDE;
102
103 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
105
106 bool WriteElf(art::File* file,
107 OatWriter* oat_writer,
108 const std::vector<const art::DexFile*>& dex_files,
109 const std::string& android_root,
110 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111
112 Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE;
113
114 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
115
116 void Init() const OVERRIDE;
117
118 void UnInit() const OVERRIDE;
119
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_;
124 mutable AtomicInteger total_compiled_methods_;
125 mutable AtomicInteger unoptimized_compiled_methods_;
126 mutable AtomicInteger optimized_compiled_methods_;
127
Andreas Gampe53c913b2014-08-12 23:19:23 -0700128 std::unique_ptr<std::ostream> visualizer_output_;
129
130 // Delegate to another compiler in case the optimizing compiler cannot compile a method.
131 // Currently the fallback is the quick compiler.
132 std::unique_ptr<Compiler> delegate_;
133
134 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
135};
136
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100137static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
138
139OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
140 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
141 run_optimizations_(
142 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
143 total_compiled_methods_(0),
144 unoptimized_compiled_methods_(0),
145 optimized_compiled_methods_(0),
146 delegate_(Create(driver, Compiler::Kind::kQuick)) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100147 if (kIsVisualizerEnabled) {
148 visualizer_output_.reset(new std::ofstream("art.cfg"));
149 }
150}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000151
Andreas Gampe53c913b2014-08-12 23:19:23 -0700152void OptimizingCompiler::Init() const {
153 delegate_->Init();
154}
155
156void OptimizingCompiler::UnInit() const {
157 delegate_->UnInit();
158}
159
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100160OptimizingCompiler::~OptimizingCompiler() {
Nicolas Geoffrayce71ae72014-09-18 11:31:32 +0100161 if (total_compiled_methods_ == 0) {
162 LOG(INFO) << "Did not compile any method.";
163 } else {
164 size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_);
165 size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_);
166 LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: "
167 << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, "
168 << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized.";
169 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100170}
171
Andreas Gampe53c913b2014-08-12 23:19:23 -0700172bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
173 CompilationUnit* cu) const {
174 return delegate_->CanCompileMethod(method_idx, dex_file, cu);
175}
176
177CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
178 uint32_t method_idx,
179 const DexFile& dex_file) const {
180 return delegate_->JniCompile(access_flags, method_idx, dex_file);
181}
182
183uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
184 return delegate_->GetEntryPointOf(method);
185}
186
187bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
188 const std::vector<const art::DexFile*>& dex_files,
189 const std::string& android_root, bool is_host) const {
190 return delegate_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
191}
192
193Backend* OptimizingCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
194 return delegate_->GetCodeGenerator(cu, compilation_unit);
195}
196
197void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
198 delegate_->InitCompilationUnit(cu);
199}
200
Ian Rogers72d32622014-05-06 16:20:11 -0700201CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000202 uint32_t access_flags,
203 InvokeType invoke_type,
204 uint16_t class_def_idx,
205 uint32_t method_idx,
206 jobject class_loader,
207 const DexFile& dex_file) const {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100208 total_compiled_methods_++;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100209 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100210 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
211 // overflow checks) assume thumb2.
212 if (instruction_set == kArm) {
213 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100214 }
215
216 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100217 if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kThumb2) {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100218 return nullptr;
219 }
220
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000221 DexCompilationUnit dex_compilation_unit(
222 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700223 class_def_idx, method_idx, access_flags,
224 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000225
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000226 // For testing purposes, we put a special marker on method names that should be compiled
227 // with this compiler. This makes sure we're not regressing.
228 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100229 bool shouldOptimize =
230 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000231
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000232 ArenaPool pool;
233 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100234 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100235
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000236 HGraph* graph = builder.BuildGraph(*code_item);
237 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000238 if (shouldCompile) {
239 LOG(FATAL) << "Could not build graph in optimizing compiler";
240 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000241 return nullptr;
242 }
243
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000244 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
245 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000246 if (shouldCompile) {
247 LOG(FATAL) << "Could not find code generator for optimizing compiler";
248 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000249 return nullptr;
250 }
251
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100252 HGraphVisualizer visualizer(
253 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
254 visualizer.DumpGraph("builder");
255
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000256 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100257
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100258 if (run_optimizations_ && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
259 optimized_compiled_methods_++;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100260 graph->BuildDominatorTree();
261 graph->TransformToSSA();
262 visualizer.DumpGraph("ssa");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100263 graph->FindNaturalLoops();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100264
Roland Levillainbf9cd7b2014-09-30 16:15:14 +0100265 HDeadCodeElimination(graph, visualizer).Execute();
266 HConstantFolding(graph, visualizer).Execute();
267
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100268 SsaRedundantPhiElimination(graph).Run();
269 SsaDeadPhiElimination(graph).Run();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100270 InstructionSimplifier(graph).Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100271 GlobalValueNumberer(graph->GetArena(), graph).Run();
272 visualizer.DumpGraph(kGVNPassName);
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100273
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100274 SsaLivenessAnalysis liveness(*graph, codegen);
275 liveness.Analyze();
276 visualizer.DumpGraph(kLivenessPassName);
277
278 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
279 register_allocator.AllocateRegisters();
280
281 visualizer.DumpGraph(kRegisterAllocatorPassName);
282 codegen->CompileOptimized(&allocator);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100283
284 std::vector<uint8_t> mapping_table;
285 SrcMap src_mapping_table;
286 codegen->BuildMappingTable(&mapping_table,
287 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
288 &src_mapping_table : nullptr);
289
290 std::vector<uint8_t> stack_map;
291 codegen->BuildStackMaps(&stack_map);
292
293 return new CompiledMethod(GetCompilerDriver(),
294 instruction_set,
295 allocator.GetMemory(),
296 codegen->GetFrameSize(),
297 codegen->GetCoreSpillMask(),
298 0, /* FPR spill mask, unused */
299 mapping_table,
300 stack_map);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100301 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
302 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Nicolas Geoffray39468442014-09-02 15:17:15 +0100303 return nullptr;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100304 } else {
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100305 unoptimized_compiled_methods_++;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100306 codegen->CompileBaseline(&allocator);
307
308 // Run these phases to get some test coverage.
309 graph->BuildDominatorTree();
310 graph->TransformToSSA();
311 visualizer.DumpGraph("ssa");
312 graph->FindNaturalLoops();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100313 SsaRedundantPhiElimination(graph).Run();
314 SsaDeadPhiElimination(graph).Run();
315 GlobalValueNumberer(graph->GetArena(), graph).Run();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100316 SsaLivenessAnalysis liveness(*graph, codegen);
317 liveness.Analyze();
318 visualizer.DumpGraph(kLivenessPassName);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100319
320 std::vector<uint8_t> mapping_table;
321 SrcMap src_mapping_table;
322 codegen->BuildMappingTable(&mapping_table,
323 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
324 &src_mapping_table : nullptr);
325 std::vector<uint8_t> vmap_table;
326 codegen->BuildVMapTable(&vmap_table);
327 std::vector<uint8_t> gc_map;
328 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
329
330 return new CompiledMethod(GetCompilerDriver(),
331 instruction_set,
332 allocator.GetMemory(),
333 codegen->GetFrameSize(),
334 codegen->GetCoreSpillMask(),
335 0, /* FPR spill mask, unused */
336 &src_mapping_table,
337 mapping_table,
338 vmap_table,
339 gc_map,
340 nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100341 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000342}
343
Andreas Gampe53c913b2014-08-12 23:19:23 -0700344CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
345 uint32_t access_flags,
346 InvokeType invoke_type,
347 uint16_t class_def_idx,
348 uint32_t method_idx,
349 jobject class_loader,
350 const DexFile& dex_file) const {
351 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
352 method_idx, class_loader, dex_file);
353 if (method != nullptr) {
354 return method;
355 }
356
357 return delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
358 class_loader, dex_file);
359}
360
361Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
362 return new OptimizingCompiler(driver);
363}
364
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000365} // namespace art