blob: 56029aa30b906afb8b59e9b52b5bee68c274ed97 [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 Geoffray804d0932014-05-02 08:46:00 +010028#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000029#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000030
31namespace art {
32
Nicolas Geoffray787c3072014-03-17 10:20:19 +000033/**
34 * Used by the code generator, to allocate the code in a vector.
35 */
36class CodeVectorAllocator FINAL : public CodeAllocator {
37 public:
38 CodeVectorAllocator() { }
39
40 virtual uint8_t* Allocate(size_t size) {
41 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000042 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000043 return &memory_[0];
44 }
45
46 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000047 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000048
49 private:
50 std::vector<uint8_t> memory_;
51 size_t size_;
52
53 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
54};
55
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010056/**
57 * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
58 */
59static bool kIsVisualizerEnabled = false;
60
61/**
62 * Filter to apply to the visualizer. Methods whose name contain that filter will
63 * be in the file.
64 */
65static const char* kStringFilter = "";
66
67OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : QuickCompiler(driver) {
68 if (kIsVisualizerEnabled) {
69 visualizer_output_.reset(new std::ofstream("art.cfg"));
70 }
71}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000072
Ian Rogers72d32622014-05-06 16:20:11 -070073CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000074 uint32_t access_flags,
75 InvokeType invoke_type,
76 uint16_t class_def_idx,
77 uint32_t method_idx,
78 jobject class_loader,
79 const DexFile& dex_file) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000080 DexCompilationUnit dex_compilation_unit(
81 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -070082 class_def_idx, method_idx, access_flags,
83 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000084
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000085 // For testing purposes, we put a special marker on method names that should be compiled
86 // with this compiler. This makes sure we're not regressing.
87 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010088 bool shouldOptimize =
89 dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000090
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091 ArenaPool pool;
92 ArenaAllocator arena(&pool);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000093 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010094
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 HGraph* graph = builder.BuildGraph(*code_item);
96 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000097 if (shouldCompile) {
98 LOG(FATAL) << "Could not build graph in optimizing compiler";
99 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 return nullptr;
101 }
102
Ian Rogers72d32622014-05-06 16:20:11 -0700103 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000104 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
105 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000106 if (shouldCompile) {
107 LOG(FATAL) << "Could not find code generator for optimizing compiler";
108 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000109 return nullptr;
110 }
111
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100112 HGraphVisualizer visualizer(
113 visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
114 visualizer.DumpGraph("builder");
115
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 CodeVectorAllocator allocator;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100117
118 if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
119 graph->BuildDominatorTree();
120 graph->TransformToSSA();
121 visualizer.DumpGraph("ssa");
122
123 graph->FindNaturalLoops();
124 SsaLivenessAnalysis liveness(*graph, codegen);
125 liveness.Analyze();
126 visualizer.DumpGraph(kLivenessPassName);
127
128 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
129 register_allocator.AllocateRegisters();
130
131 visualizer.DumpGraph(kRegisterAllocatorPassName);
132 codegen->CompileOptimized(&allocator);
133 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
134 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
135 } else {
136 codegen->CompileBaseline(&allocator);
137
138 // Run these phases to get some test coverage.
139 graph->BuildDominatorTree();
140 graph->TransformToSSA();
141 visualizer.DumpGraph("ssa");
142 graph->FindNaturalLoops();
143 SsaLivenessAnalysis liveness(*graph, codegen);
144 liveness.Analyze();
145 visualizer.DumpGraph(kLivenessPassName);
146 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000147
148 std::vector<uint8_t> mapping_table;
149 codegen->BuildMappingTable(&mapping_table);
150 std::vector<uint8_t> vmap_table;
151 codegen->BuildVMapTable(&vmap_table);
152 std::vector<uint8_t> gc_map;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000153 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154
Ian Rogers72d32622014-05-06 16:20:11 -0700155 return new CompiledMethod(GetCompilerDriver(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000156 instruction_set,
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000157 allocator.GetMemory(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 codegen->GetFrameSize(),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000159 codegen->GetCoreSpillMask(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160 0, /* FPR spill mask, unused */
161 mapping_table,
162 vmap_table,
163 gc_map,
164 nullptr);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000165}
166
167} // namespace art