blob: 75f41557e2dcc8097ca1c85fd3d74274a8a6d4dd [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"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000025#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000026#include "driver/dex_compilation_unit.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010027#include "graph_visualizer.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000028#include "nodes.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010029#include "register_allocator.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010030#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010031#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000032#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000033
34namespace art {
35
Nicolas Geoffray787c3072014-03-17 10:20:19 +000036/**
37 * Used by the code generator, to allocate the code in a vector.
38 */
39class CodeVectorAllocator FINAL : public CodeAllocator {
40 public:
41 CodeVectorAllocator() { }
42
43 virtual uint8_t* Allocate(size_t size) {
44 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000045 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046 return &memory_[0];
47 }
48
49 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000050 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051
52 private:
53 std::vector<uint8_t> memory_;
54 size_t size_;
55
56 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
57};
58
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010059/**
60 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
61 */
62static bool kIsVisualizerEnabled = false;
63
64/**
65 * Filter to apply to the visualizer. Methods whose name contain that filter will
66 * be in the file.
67 */
68static const char* kStringFilter = "";
69
Andreas Gampe53c913b2014-08-12 23:19:23 -070070class OptimizingCompiler FINAL : public Compiler {
71 public:
72 explicit OptimizingCompiler(CompilerDriver* driver);
73
74 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
75 OVERRIDE;
76
77 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
78 uint32_t access_flags,
79 InvokeType invoke_type,
80 uint16_t class_def_idx,
81 uint32_t method_idx,
82 jobject class_loader,
83 const DexFile& dex_file) const OVERRIDE;
84
85 CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
86 uint32_t access_flags,
87 InvokeType invoke_type,
88 uint16_t class_def_idx,
89 uint32_t method_idx,
90 jobject class_loader,
91 const DexFile& dex_file) const;
92
93 // For the following methods we will use the fallback. This is a delegation pattern.
94 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
107 Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE;
108
109 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
110
111 void Init() const OVERRIDE;
112
113 void UnInit() const OVERRIDE;
114
115 private:
116 std::unique_ptr<std::ostream> visualizer_output_;
117
118 // Delegate to another compiler in case the optimizing compiler cannot compile a method.
119 // Currently the fallback is the quick compiler.
120 std::unique_ptr<Compiler> delegate_;
121
122 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
123};
124
125OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : Compiler(driver, 100),
126 delegate_(Create(driver, Compiler::Kind::kQuick)) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100127 if (kIsVisualizerEnabled) {
128 visualizer_output_.reset(new std::ofstream("art.cfg"));
129 }
130}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000131
Andreas Gampe53c913b2014-08-12 23:19:23 -0700132void OptimizingCompiler::Init() const {
133 delegate_->Init();
134}
135
136void OptimizingCompiler::UnInit() const {
137 delegate_->UnInit();
138}
139
140bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
141 CompilationUnit* cu) const {
142 return delegate_->CanCompileMethod(method_idx, dex_file, cu);
143}
144
145CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
146 uint32_t method_idx,
147 const DexFile& dex_file) const {
148 return delegate_->JniCompile(access_flags, method_idx, dex_file);
149}
150
151uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
152 return delegate_->GetEntryPointOf(method);
153}
154
155bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
156 const std::vector<const art::DexFile*>& dex_files,
157 const std::string& android_root, bool is_host) const {
158 return delegate_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
159}
160
161Backend* OptimizingCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
162 return delegate_->GetCodeGenerator(cu, compilation_unit);
163}
164
165void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
166 delegate_->InitCompilationUnit(cu);
167}
168
Ian Rogers72d32622014-05-06 16:20:11 -0700169CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000170 uint32_t access_flags,
171 InvokeType invoke_type,
172 uint16_t class_def_idx,
173 uint32_t method_idx,
174 jobject class_loader,
175 const DexFile& dex_file) const {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100176 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100177 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
178 // overflow checks) assume thumb2.
179 if (instruction_set == kArm) {
180 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100181 }
182
183 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100184 if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kThumb2) {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100185 return nullptr;
186 }
187
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000188 DexCompilationUnit dex_compilation_unit(
189 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700190 class_def_idx, method_idx, access_flags,
191 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000192
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000193 // For testing purposes, we put a special marker on method names that should be compiled
194 // with this compiler. This makes sure we're not regressing.
195 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100196 bool shouldOptimize =
197 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000198
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000199 ArenaPool pool;
200 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100201 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100202
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000203 HGraph* graph = builder.BuildGraph(*code_item);
204 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000205 if (shouldCompile) {
206 LOG(FATAL) << "Could not build graph in optimizing compiler";
207 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000208 return nullptr;
209 }
210
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000211 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
212 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000213 if (shouldCompile) {
214 LOG(FATAL) << "Could not find code generator for optimizing compiler";
215 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000216 return nullptr;
217 }
218
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100219 HGraphVisualizer visualizer(
220 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
221 visualizer.DumpGraph("builder");
222
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000223 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100224
225 if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
226 graph->BuildDominatorTree();
227 graph->TransformToSSA();
228 visualizer.DumpGraph("ssa");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100229 graph->FindNaturalLoops();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100230
231 SsaRedundantPhiElimination(graph).Run();
232 SsaDeadPhiElimination(graph).Run();
233
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100234 SsaLivenessAnalysis liveness(*graph, codegen);
235 liveness.Analyze();
236 visualizer.DumpGraph(kLivenessPassName);
237
238 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
239 register_allocator.AllocateRegisters();
240
241 visualizer.DumpGraph(kRegisterAllocatorPassName);
242 codegen->CompileOptimized(&allocator);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100243
244 std::vector<uint8_t> mapping_table;
245 SrcMap src_mapping_table;
246 codegen->BuildMappingTable(&mapping_table,
247 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
248 &src_mapping_table : nullptr);
249
250 std::vector<uint8_t> stack_map;
251 codegen->BuildStackMaps(&stack_map);
252
253 return new CompiledMethod(GetCompilerDriver(),
254 instruction_set,
255 allocator.GetMemory(),
256 codegen->GetFrameSize(),
257 codegen->GetCoreSpillMask(),
258 0, /* FPR spill mask, unused */
259 mapping_table,
260 stack_map);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100261 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
262 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Nicolas Geoffray39468442014-09-02 15:17:15 +0100263 return nullptr;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100264 } else {
265 codegen->CompileBaseline(&allocator);
266
267 // Run these phases to get some test coverage.
268 graph->BuildDominatorTree();
269 graph->TransformToSSA();
270 visualizer.DumpGraph("ssa");
271 graph->FindNaturalLoops();
272 SsaLivenessAnalysis liveness(*graph, codegen);
273 liveness.Analyze();
274 visualizer.DumpGraph(kLivenessPassName);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100275
276 std::vector<uint8_t> mapping_table;
277 SrcMap src_mapping_table;
278 codegen->BuildMappingTable(&mapping_table,
279 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
280 &src_mapping_table : nullptr);
281 std::vector<uint8_t> vmap_table;
282 codegen->BuildVMapTable(&vmap_table);
283 std::vector<uint8_t> gc_map;
284 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
285
286 return new CompiledMethod(GetCompilerDriver(),
287 instruction_set,
288 allocator.GetMemory(),
289 codegen->GetFrameSize(),
290 codegen->GetCoreSpillMask(),
291 0, /* FPR spill mask, unused */
292 &src_mapping_table,
293 mapping_table,
294 vmap_table,
295 gc_map,
296 nullptr);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100297 }
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000298}
299
Andreas Gampe53c913b2014-08-12 23:19:23 -0700300CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
301 uint32_t access_flags,
302 InvokeType invoke_type,
303 uint16_t class_def_idx,
304 uint32_t method_idx,
305 jobject class_loader,
306 const DexFile& dex_file) const {
307 CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
308 method_idx, class_loader, dex_file);
309 if (method != nullptr) {
310 return method;
311 }
312
313 return delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
314 class_loader, dex_file);
315}
316
317Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
318 return new OptimizingCompiler(driver);
319}
320
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000321} // namespace art