blob: fce6ab0a336c905e2c5ecefca3b93e547a197ddb [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
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010017#include <fstream>
Nicolas Geoffray787c3072014-03-17 10:20:19 +000018#include <stdint.h>
19
20#include "builder.h"
21#include "code_generator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000022#include "compilers.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000023#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "driver/dex_compilation_unit.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010025#include "graph_visualizer.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026#include "nodes.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010027#include "register_allocator.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010028#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000030#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000031
32namespace art {
33
Nicolas Geoffray787c3072014-03-17 10:20:19 +000034/**
35 * Used by the code generator, to allocate the code in a vector.
36 */
37class CodeVectorAllocator FINAL : public CodeAllocator {
38 public:
39 CodeVectorAllocator() { }
40
41 virtual uint8_t* Allocate(size_t size) {
42 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000043 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000044 return &memory_[0];
45 }
46
47 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000048 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049
50 private:
51 std::vector<uint8_t> memory_;
52 size_t size_;
53
54 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
55};
56
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010057/**
58 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
59 */
60static bool kIsVisualizerEnabled = false;
61
62/**
63 * Filter to apply to the visualizer. Methods whose name contain that filter will
64 * be in the file.
65 */
66static const char* kStringFilter = "";
67
68OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : QuickCompiler(driver) {
69 if (kIsVisualizerEnabled) {
70 visualizer_output_.reset(new std::ofstream("art.cfg"));
71 }
72}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073
Ian Rogers72d32622014-05-06 16:20:11 -070074CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000075 uint32_t access_flags,
76 InvokeType invoke_type,
77 uint16_t class_def_idx,
78 uint32_t method_idx,
79 jobject class_loader,
80 const DexFile& dex_file) const {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +010081 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +010082 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
83 // overflow checks) assume thumb2.
84 if (instruction_set == kArm) {
85 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +010086 }
87
88 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray8d486732014-07-16 16:23:40 +010089 if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kThumb2) {
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +010090 return nullptr;
91 }
92
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000093 DexCompilationUnit dex_compilation_unit(
94 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -070095 class_def_idx, method_idx, access_flags,
96 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000097
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000098 // For testing purposes, we put a special marker on method names that should be compiled
99 // with this compiler. This makes sure we're not regressing.
100 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100101 bool shouldOptimize =
102 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000103
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 ArenaPool pool;
105 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100106 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100107
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108 HGraph* graph = builder.BuildGraph(*code_item);
109 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000110 if (shouldCompile) {
111 LOG(FATAL) << "Could not build graph in optimizing compiler";
112 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 return nullptr;
114 }
115
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
117 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000118 if (shouldCompile) {
119 LOG(FATAL) << "Could not find code generator for optimizing compiler";
120 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 return nullptr;
122 }
123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124 HGraphVisualizer visualizer(
125 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
126 visualizer.DumpGraph("builder");
127
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000128 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100129
130 if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
131 graph->BuildDominatorTree();
132 graph->TransformToSSA();
133 visualizer.DumpGraph("ssa");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100134 graph->FindNaturalLoops();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100135
136 SsaRedundantPhiElimination(graph).Run();
137 SsaDeadPhiElimination(graph).Run();
138
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100139 SsaLivenessAnalysis liveness(*graph, codegen);
140 liveness.Analyze();
141 visualizer.DumpGraph(kLivenessPassName);
142
143 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
144 register_allocator.AllocateRegisters();
145
146 visualizer.DumpGraph(kRegisterAllocatorPassName);
147 codegen->CompileOptimized(&allocator);
148 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
149 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
150 } else {
151 codegen->CompileBaseline(&allocator);
152
153 // Run these phases to get some test coverage.
154 graph->BuildDominatorTree();
155 graph->TransformToSSA();
156 visualizer.DumpGraph("ssa");
157 graph->FindNaturalLoops();
158 SsaLivenessAnalysis liveness(*graph, codegen);
159 liveness.Analyze();
160 visualizer.DumpGraph(kLivenessPassName);
161 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162
163 std::vector<uint8_t> mapping_table;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700164 SrcMap src_mapping_table;
165 codegen->BuildMappingTable(&mapping_table,
166 GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
167 &src_mapping_table : nullptr);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168 std::vector<uint8_t> vmap_table;
169 codegen->BuildVMapTable(&vmap_table);
170 std::vector<uint8_t> gc_map;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000171 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172
Ian Rogers72d32622014-05-06 16:20:11 -0700173 return new CompiledMethod(GetCompilerDriver(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000174 instruction_set,
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000175 allocator.GetMemory(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000176 codegen->GetFrameSize(),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000177 codegen->GetCoreSpillMask(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000178 0, /* FPR spill mask, unused */
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700179 &src_mapping_table,
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000180 mapping_table,
181 vmap_table,
182 gc_map,
183 nullptr);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000184}
185
186} // namespace art