Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1 | /* |
| 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 Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 17 | #include <fstream> |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | |
| 20 | #include "builder.h" |
| 21 | #include "code_generator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 22 | #include "compilers.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 23 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 24 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 25 | #include "graph_visualizer.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 26 | #include "nodes.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 27 | #include "register_allocator.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 28 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 29 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 33 | /** |
| 34 | * Used by the code generator, to allocate the code in a vector. |
| 35 | */ |
| 36 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 37 | public: |
| 38 | CodeVectorAllocator() { } |
| 39 | |
| 40 | virtual uint8_t* Allocate(size_t size) { |
| 41 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 42 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 43 | return &memory_[0]; |
| 44 | } |
| 45 | |
| 46 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 47 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | std::vector<uint8_t> memory_; |
| 51 | size_t size_; |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 54 | }; |
| 55 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 56 | /** |
| 57 | * If set to true, generates a file suitable for the c1visualizer tool and IRHydra. |
| 58 | */ |
| 59 | static 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 | */ |
| 65 | static const char* kStringFilter = ""; |
| 66 | |
| 67 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : QuickCompiler(driver) { |
| 68 | if (kIsVisualizerEnabled) { |
| 69 | visualizer_output_.reset(new std::ofstream("art.cfg")); |
| 70 | } |
| 71 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 72 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 73 | CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 74 | 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 Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 80 | InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); |
| 81 | // The optimizing compiler currently does not have a Thumb2 assembler. |
| 82 | if (instruction_set == kThumb2) { |
| 83 | instruction_set = kArm; |
| 84 | } |
| 85 | |
| 86 | // Do not attempt to compile on architectures we do not support. |
| 87 | if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kArm) { |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 91 | DexCompilationUnit dex_compilation_unit( |
| 92 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 93 | class_def_idx, method_idx, access_flags, |
| 94 | GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 95 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 96 | // For testing purposes, we put a special marker on method names that should be compiled |
| 97 | // with this compiler. This makes sure we're not regressing. |
| 98 | bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 99 | bool shouldOptimize = |
| 100 | dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 101 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 102 | ArenaPool pool; |
| 103 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame^] | 104 | HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver()); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 105 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 106 | HGraph* graph = builder.BuildGraph(*code_item); |
| 107 | if (graph == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 108 | if (shouldCompile) { |
| 109 | LOG(FATAL) << "Could not build graph in optimizing compiler"; |
| 110 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 111 | return nullptr; |
| 112 | } |
| 113 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 114 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 115 | if (codegen == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 116 | if (shouldCompile) { |
| 117 | LOG(FATAL) << "Could not find code generator for optimizing compiler"; |
| 118 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 119 | return nullptr; |
| 120 | } |
| 121 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 122 | HGraphVisualizer visualizer( |
| 123 | visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit); |
| 124 | visualizer.DumpGraph("builder"); |
| 125 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 126 | CodeVectorAllocator allocator; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 127 | |
| 128 | if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) { |
| 129 | graph->BuildDominatorTree(); |
| 130 | graph->TransformToSSA(); |
| 131 | visualizer.DumpGraph("ssa"); |
| 132 | |
| 133 | graph->FindNaturalLoops(); |
| 134 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 135 | liveness.Analyze(); |
| 136 | visualizer.DumpGraph(kLivenessPassName); |
| 137 | |
| 138 | RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); |
| 139 | register_allocator.AllocateRegisters(); |
| 140 | |
| 141 | visualizer.DumpGraph(kRegisterAllocatorPassName); |
| 142 | codegen->CompileOptimized(&allocator); |
| 143 | } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { |
| 144 | LOG(FATAL) << "Could not allocate registers in optimizing compiler"; |
| 145 | } else { |
| 146 | codegen->CompileBaseline(&allocator); |
| 147 | |
| 148 | // Run these phases to get some test coverage. |
| 149 | graph->BuildDominatorTree(); |
| 150 | graph->TransformToSSA(); |
| 151 | visualizer.DumpGraph("ssa"); |
| 152 | graph->FindNaturalLoops(); |
| 153 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 154 | liveness.Analyze(); |
| 155 | visualizer.DumpGraph(kLivenessPassName); |
| 156 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 157 | |
| 158 | std::vector<uint8_t> mapping_table; |
| 159 | codegen->BuildMappingTable(&mapping_table); |
| 160 | std::vector<uint8_t> vmap_table; |
| 161 | codegen->BuildVMapTable(&vmap_table); |
| 162 | std::vector<uint8_t> gc_map; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 163 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 164 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 165 | return new CompiledMethod(GetCompilerDriver(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 166 | instruction_set, |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 167 | allocator.GetMemory(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 168 | codegen->GetFrameSize(), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 169 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 170 | 0, /* FPR spill mask, unused */ |
| 171 | mapping_table, |
| 172 | vmap_table, |
| 173 | gc_map, |
| 174 | nullptr); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | } // namespace art |