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 | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 28 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 29 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 30 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Used by the code generator, to allocate the code in a vector. |
| 36 | */ |
| 37 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 38 | public: |
| 39 | CodeVectorAllocator() { } |
| 40 | |
| 41 | virtual uint8_t* Allocate(size_t size) { |
| 42 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 43 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 44 | return &memory_[0]; |
| 45 | } |
| 46 | |
| 47 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 48 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | |
| 50 | private: |
| 51 | std::vector<uint8_t> memory_; |
| 52 | size_t size_; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 55 | }; |
| 56 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 57 | /** |
| 58 | * If set to true, generates a file suitable for the c1visualizer tool and IRHydra. |
| 59 | */ |
| 60 | static 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 | */ |
| 66 | static const char* kStringFilter = ""; |
| 67 | |
| 68 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) : QuickCompiler(driver) { |
| 69 | if (kIsVisualizerEnabled) { |
| 70 | visualizer_output_.reset(new std::ofstream("art.cfg")); |
| 71 | } |
| 72 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 73 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 74 | CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item, |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 75 | 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 Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 81 | InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 82 | // 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 Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 89 | if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kThumb2) { |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 90 | return nullptr; |
| 91 | } |
| 92 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 93 | DexCompilationUnit dex_compilation_unit( |
| 94 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 95 | class_def_idx, method_idx, access_flags, |
| 96 | GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 97 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 98 | // 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 Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 101 | bool shouldOptimize = |
| 102 | dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 103 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 104 | ArenaPool pool; |
| 105 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 106 | HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver()); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 107 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 108 | HGraph* graph = builder.BuildGraph(*code_item); |
| 109 | if (graph == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 110 | if (shouldCompile) { |
| 111 | LOG(FATAL) << "Could not build graph in optimizing compiler"; |
| 112 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 113 | return nullptr; |
| 114 | } |
| 115 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 116 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 117 | if (codegen == nullptr) { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 118 | if (shouldCompile) { |
| 119 | LOG(FATAL) << "Could not find code generator for optimizing compiler"; |
| 120 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |
| 123 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 124 | HGraphVisualizer visualizer( |
| 125 | visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit); |
| 126 | visualizer.DumpGraph("builder"); |
| 127 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 128 | CodeVectorAllocator allocator; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 129 | |
| 130 | if (RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) { |
| 131 | graph->BuildDominatorTree(); |
| 132 | graph->TransformToSSA(); |
| 133 | visualizer.DumpGraph("ssa"); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 134 | graph->FindNaturalLoops(); |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 135 | |
| 136 | SsaRedundantPhiElimination(graph).Run(); |
| 137 | SsaDeadPhiElimination(graph).Run(); |
| 138 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 139 | 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 Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 162 | |
| 163 | std::vector<uint8_t> mapping_table; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 164 | SrcMap src_mapping_table; |
| 165 | codegen->BuildMappingTable(&mapping_table, |
| 166 | GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? |
| 167 | &src_mapping_table : nullptr); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 168 | std::vector<uint8_t> vmap_table; |
| 169 | codegen->BuildVMapTable(&vmap_table); |
| 170 | std::vector<uint8_t> gc_map; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 171 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 172 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 173 | return new CompiledMethod(GetCompilerDriver(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 174 | instruction_set, |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 175 | allocator.GetMemory(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 176 | codegen->GetFrameSize(), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 177 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 178 | 0, /* FPR spill mask, unused */ |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 179 | &src_mapping_table, |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 180 | mapping_table, |
| 181 | vmap_table, |
| 182 | gc_map, |
| 183 | nullptr); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } // namespace art |