blob: a5031e0a7c9cf36534d6da94232fa4b100bcda89 [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 Geoffray787c3072014-03-17 10:20:19 +000017#include <stdint.h>
18
19#include "builder.h"
20#include "code_generator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000021#include "compilers.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000022#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000023#include "driver/dex_compilation_unit.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000024#include "nodes.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010025#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000026#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000027
28namespace art {
29
Nicolas Geoffray787c3072014-03-17 10:20:19 +000030/**
31 * Used by the code generator, to allocate the code in a vector.
32 */
33class CodeVectorAllocator FINAL : public CodeAllocator {
34 public:
35 CodeVectorAllocator() { }
36
37 virtual uint8_t* Allocate(size_t size) {
38 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000039 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040 return &memory_[0];
41 }
42
43 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000044 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000045
46 private:
47 std::vector<uint8_t> memory_;
48 size_t size_;
49
50 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
51};
52
53
Ian Rogers72d32622014-05-06 16:20:11 -070054CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000055 uint32_t access_flags,
56 InvokeType invoke_type,
57 uint16_t class_def_idx,
58 uint32_t method_idx,
59 jobject class_loader,
60 const DexFile& dex_file) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000061 DexCompilationUnit dex_compilation_unit(
62 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -070063 class_def_idx, method_idx, access_flags,
64 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000065
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000066 // For testing purposes, we put a special marker on method names that should be compiled
67 // with this compiler. This makes sure we're not regressing.
68 bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
69
Nicolas Geoffray787c3072014-03-17 10:20:19 +000070 ArenaPool pool;
71 ArenaAllocator arena(&pool);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000072 HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073 HGraph* graph = builder.BuildGraph(*code_item);
74 if (graph == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000075 if (shouldCompile) {
76 LOG(FATAL) << "Could not build graph in optimizing compiler";
77 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000078 return nullptr;
79 }
80
Ian Rogers72d32622014-05-06 16:20:11 -070081 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000082 // The optimizing compiler currently does not have a Thumb2 assembler.
83 if (instruction_set == kThumb2) {
84 instruction_set = kArm;
85 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
87 if (codegen == nullptr) {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000088 if (shouldCompile) {
89 LOG(FATAL) << "Could not find code generator for optimizing compiler";
90 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091 return nullptr;
92 }
93
94 CodeVectorAllocator allocator;
95 codegen->Compile(&allocator);
96
97 std::vector<uint8_t> mapping_table;
98 codegen->BuildMappingTable(&mapping_table);
99 std::vector<uint8_t> vmap_table;
100 codegen->BuildVMapTable(&vmap_table);
101 std::vector<uint8_t> gc_map;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000102 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100104 // Run these phases to get some test coverage.
105 graph->BuildDominatorTree();
106 graph->TransformToSSA();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100107 graph->FindNaturalLoops();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100108 SsaLivenessAnalysis(*graph).Analyze();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109
Ian Rogers72d32622014-05-06 16:20:11 -0700110 return new CompiledMethod(GetCompilerDriver(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000111 instruction_set,
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000112 allocator.GetMemory(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113 codegen->GetFrameSize(),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000114 codegen->GetCoreSpillMask(),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000115 0, /* FPR spill mask, unused */
116 mapping_table,
117 vmap_table,
118 gc_map,
119 nullptr);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000120}
121
122} // namespace art