blob: b621e510f35124f7665ed8b3e2cd053af26b8d3d [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();
82 // The optimizing compiler currently does not have a Thumb2 assembler.
83 if (instruction_set == kThumb2) {
84 instruction_set = kArm;
85 }
86
87 // Do not attempt to compile on architectures we do not support.
88 if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kArm) {
89 return nullptr;
90 }
91
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000092 DexCompilationUnit dex_compilation_unit(
93 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -070094 class_def_idx, method_idx, access_flags,
95 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000096
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000097 // For testing purposes, we put a special marker on method names that should be compiled
98 // with this compiler. This makes sure we're not regressing.
99 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100100 bool shouldOptimize =
101 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 ArenaPool pool;
104 ArenaAllocator arena(&pool);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100105 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 HGraph* graph = builder.BuildGraph(*code_item);
108 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000109 if (shouldCompile) {
110 LOG(FATAL) << "Could not build graph in optimizing compiler";
111 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112 return nullptr;
113 }
114
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000115 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
116 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000117 if (shouldCompile) {
118 LOG(FATAL) << "Could not find code generator for optimizing compiler";
119 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 return nullptr;
121 }
122
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100123 HGraphVisualizer visualizer(
124 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
125 visualizer.DumpGraph("builder");
126
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100128
129 if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
130 graph->BuildDominatorTree();
131 graph->TransformToSSA();
132 visualizer.DumpGraph("ssa");
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100133 graph->FindNaturalLoops();
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100134
135 SsaRedundantPhiElimination(graph).Run();
136 SsaDeadPhiElimination(graph).Run();
137
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100138 SsaLivenessAnalysis liveness(*graph, codegen);
139 liveness.Analyze();
140 visualizer.DumpGraph(kLivenessPassName);
141
142 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
143 register_allocator.AllocateRegisters();
144
145 visualizer.DumpGraph(kRegisterAllocatorPassName);
146 codegen->CompileOptimized(&allocator);
147 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
148 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
149 } else {
150 codegen->CompileBaseline(&allocator);
151
152 // Run these phases to get some test coverage.
153 graph->BuildDominatorTree();
154 graph->TransformToSSA();
155 visualizer.DumpGraph("ssa");
156 graph->FindNaturalLoops();
157 SsaLivenessAnalysis liveness(*graph, codegen);
158 liveness.Analyze();
159 visualizer.DumpGraph(kLivenessPassName);
160 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000161
162 std::vector<uint8_t> mapping_table;
163 codegen->BuildMappingTable(&mapping_table);
164 std::vector<uint8_t> vmap_table;
165 codegen->BuildVMapTable(&vmap_table);
166 std::vector<uint8_t> gc_map;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000167 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000168
Ian Rogers72d32622014-05-06 16:20:11 -0700169 return new CompiledMethod(GetCompilerDriver(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000170 instruction_set,
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000171 allocator.GetMemory(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000172 codegen->GetFrameSize(),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000173 codegen->GetCoreSpillMask(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000174 0, /* FPR spill mask, unused */
175 mapping_table,
176 vmap_table,
177 gc_map,
178 nullptr);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000179}
180
181} // namespace art