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 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 17 | #include "optimizing_compiler.h" |
| 18 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 19 | #include <fstream> |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 22 | #include "bounds_check_elimination.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 23 | #include "builder.h" |
| 24 | #include "code_generator.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 25 | #include "compiler.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 26 | #include "constant_folding.h" |
| 27 | #include "dead_code_elimination.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 28 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 29 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 30 | #include "elf_writer_quick.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 31 | #include "graph_visualizer.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 32 | #include "gvn.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 33 | #include "instruction_simplifier.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 34 | #include "jni/quick/jni_compiler.h" |
| 35 | #include "mirror/art_method-inl.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 36 | #include "nodes.h" |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 37 | #include "prepare_for_register_allocation.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 38 | #include "register_allocator.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 39 | #include "ssa_builder.h" |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 40 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 41 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 42 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Used by the code generator, to allocate the code in a vector. |
| 48 | */ |
| 49 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 50 | public: |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 51 | CodeVectorAllocator() {} |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 52 | |
| 53 | virtual uint8_t* Allocate(size_t size) { |
| 54 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 55 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 56 | return &memory_[0]; |
| 57 | } |
| 58 | |
| 59 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 60 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | std::vector<uint8_t> memory_; |
| 64 | size_t size_; |
| 65 | |
| 66 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 67 | }; |
| 68 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 69 | /** |
| 70 | * If set to true, generates a file suitable for the c1visualizer tool and IRHydra. |
| 71 | */ |
| 72 | static bool kIsVisualizerEnabled = false; |
| 73 | |
| 74 | /** |
| 75 | * Filter to apply to the visualizer. Methods whose name contain that filter will |
| 76 | * be in the file. |
| 77 | */ |
| 78 | static const char* kStringFilter = ""; |
| 79 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 80 | class OptimizingCompiler FINAL : public Compiler { |
| 81 | public: |
| 82 | explicit OptimizingCompiler(CompilerDriver* driver); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 83 | ~OptimizingCompiler(); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 84 | |
| 85 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const |
| 86 | OVERRIDE; |
| 87 | |
| 88 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 89 | uint32_t access_flags, |
| 90 | InvokeType invoke_type, |
| 91 | uint16_t class_def_idx, |
| 92 | uint32_t method_idx, |
| 93 | jobject class_loader, |
| 94 | const DexFile& dex_file) const OVERRIDE; |
| 95 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 96 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 97 | uint32_t method_idx, |
| 98 | const DexFile& dex_file) const OVERRIDE; |
| 99 | |
| 100 | uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE |
| 101 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 102 | |
| 103 | bool WriteElf(art::File* file, |
| 104 | OatWriter* oat_writer, |
| 105 | const std::vector<const art::DexFile*>& dex_files, |
| 106 | const std::string& android_root, |
| 107 | bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 108 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 109 | Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED, |
| 110 | void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE { |
| 111 | return nullptr; |
| 112 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 113 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 114 | void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {} |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 115 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 116 | void Init() const OVERRIDE {} |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 117 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 118 | void UnInit() const OVERRIDE {} |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 119 | |
| 120 | private: |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 121 | // Whether we should run any optimization or register allocation. If false, will |
| 122 | // just run the code generation after the graph was built. |
| 123 | const bool run_optimizations_; |
| 124 | mutable AtomicInteger total_compiled_methods_; |
| 125 | mutable AtomicInteger unoptimized_compiled_methods_; |
| 126 | mutable AtomicInteger optimized_compiled_methods_; |
| 127 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 128 | std::unique_ptr<std::ostream> visualizer_output_; |
| 129 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 130 | DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); |
| 131 | }; |
| 132 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 133 | static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ |
| 134 | |
| 135 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) |
| 136 | : Compiler(driver, kMaximumCompilationTimeBeforeWarning), |
| 137 | run_optimizations_( |
| 138 | driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime), |
| 139 | total_compiled_methods_(0), |
| 140 | unoptimized_compiled_methods_(0), |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 141 | optimized_compiled_methods_(0) { |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 142 | if (kIsVisualizerEnabled) { |
| 143 | visualizer_output_.reset(new std::ofstream("art.cfg")); |
| 144 | } |
| 145 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 146 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 147 | OptimizingCompiler::~OptimizingCompiler() { |
Nicolas Geoffray | ce71ae7 | 2014-09-18 11:31:32 +0100 | [diff] [blame] | 148 | if (total_compiled_methods_ == 0) { |
| 149 | LOG(INFO) << "Did not compile any method."; |
| 150 | } else { |
| 151 | size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_); |
| 152 | size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_); |
| 153 | LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: " |
| 154 | << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, " |
| 155 | << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized."; |
| 156 | } |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 159 | bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, |
| 160 | const DexFile& dex_file ATTRIBUTE_UNUSED, |
| 161 | CompilationUnit* cu ATTRIBUTE_UNUSED) const { |
| 162 | return true; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, |
| 166 | uint32_t method_idx, |
| 167 | const DexFile& dex_file) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 168 | return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { |
Nicolas Geoffray | 4179cc1 | 2014-11-19 08:35:17 +0000 | [diff] [blame] | 172 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 173 | InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer, |
| 177 | const std::vector<const art::DexFile*>& dex_files, |
| 178 | const std::string& android_root, bool is_host) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 179 | return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host, |
| 180 | *GetCompilerDriver()); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 183 | static bool IsInstructionSetSupported(InstructionSet instruction_set) { |
| 184 | return instruction_set == kArm64 |
| 185 | || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) |
| 186 | || instruction_set == kX86 |
| 187 | || instruction_set == kX86_64; |
| 188 | } |
| 189 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 190 | static bool CanOptimize(const DexFile::CodeItem& code_item) { |
| 191 | // TODO: We currently cannot optimize methods with try/catch. |
| 192 | return code_item.tries_size_ == 0; |
| 193 | } |
| 194 | |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 195 | static void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) { |
| 196 | HDeadCodeElimination opt1(graph); |
| 197 | HConstantFolding opt2(graph); |
| 198 | SsaRedundantPhiElimination opt3(graph); |
| 199 | SsaDeadPhiElimination opt4(graph); |
| 200 | InstructionSimplifier opt5(graph); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 201 | GVNOptimization opt6(graph); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 202 | BoundsCheckElimination bce(graph); |
| 203 | InstructionSimplifier opt8(graph); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 204 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 205 | HOptimization* optimizations[] = { |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 206 | &opt1, |
| 207 | &opt2, |
| 208 | &opt3, |
| 209 | &opt4, |
| 210 | &opt5, |
| 211 | &opt6, |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 212 | &bce, |
| 213 | &opt8 |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 214 | }; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 215 | |
| 216 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 217 | HOptimization* optimization = optimizations[i]; |
| 218 | optimization->Run(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 219 | visualizer.DumpGraph(optimization->GetPassName()); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 220 | optimization->Check(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 224 | static bool TryBuildingSsa(HGraph* graph, |
| 225 | const DexCompilationUnit& dex_compilation_unit, |
| 226 | const HGraphVisualizer& visualizer) { |
| 227 | graph->BuildDominatorTree(); |
| 228 | graph->TransformToSSA(); |
| 229 | |
| 230 | if (!graph->AnalyzeNaturalLoops()) { |
| 231 | LOG(INFO) << "Skipping compilation of " |
| 232 | << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(), |
| 233 | *dex_compilation_unit.GetDexFile()) |
| 234 | << ": it contains a non natural loop"; |
| 235 | return false; |
| 236 | } |
| 237 | visualizer.DumpGraph("ssa transform"); |
| 238 | return true; |
| 239 | } |
| 240 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 241 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
| 242 | uint32_t access_flags, |
| 243 | InvokeType invoke_type, |
| 244 | uint16_t class_def_idx, |
| 245 | uint32_t method_idx, |
| 246 | jobject class_loader, |
| 247 | const DexFile& dex_file) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 248 | UNUSED(invoke_type); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 249 | total_compiled_methods_++; |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 250 | InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 251 | // Always use the thumb2 assembler: some runtime functionality (like implicit stack |
| 252 | // overflow checks) assume thumb2. |
| 253 | if (instruction_set == kArm) { |
| 254 | instruction_set = kThumb2; |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 258 | if (!IsInstructionSetSupported(instruction_set)) { |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 259 | return nullptr; |
| 260 | } |
| 261 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 262 | if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 266 | DexCompilationUnit dex_compilation_unit( |
| 267 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 268 | class_def_idx, method_idx, access_flags, |
| 269 | GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 270 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 271 | // For testing purposes, we put a special marker on method names that should be compiled |
| 272 | // with this compiler. This makes sure we're not regressing. |
| 273 | bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 274 | bool shouldOptimize = |
| 275 | dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 276 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 277 | ArenaPool pool; |
| 278 | ArenaAllocator arena(&pool); |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 279 | HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver()); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 280 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 281 | HGraph* graph = builder.BuildGraph(*code_item); |
| 282 | if (graph == nullptr) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 283 | CHECK(!shouldCompile) << "Could not build graph in optimizing compiler"; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 284 | return nullptr; |
| 285 | } |
| 286 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 287 | CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); |
| 288 | if (codegen == nullptr) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 289 | CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 290 | return nullptr; |
| 291 | } |
| 292 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 293 | HGraphVisualizer visualizer( |
| 294 | visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit); |
| 295 | visualizer.DumpGraph("builder"); |
| 296 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 297 | CodeVectorAllocator allocator; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 298 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 299 | if (run_optimizations_ |
| 300 | && CanOptimize(*code_item) |
| 301 | && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 302 | VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 303 | optimized_compiled_methods_++; |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 304 | if (!TryBuildingSsa(graph, dex_compilation_unit, visualizer)) { |
| 305 | // We could not transform the graph to SSA, bailout. |
| 306 | return nullptr; |
| 307 | } |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 308 | RunOptimizations(graph, visualizer); |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 309 | |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 310 | PrepareForRegisterAllocation(graph).Run(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 311 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 312 | liveness.Analyze(); |
| 313 | visualizer.DumpGraph(kLivenessPassName); |
| 314 | |
| 315 | RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); |
| 316 | register_allocator.AllocateRegisters(); |
| 317 | |
| 318 | visualizer.DumpGraph(kRegisterAllocatorPassName); |
| 319 | codegen->CompileOptimized(&allocator); |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 320 | |
| 321 | std::vector<uint8_t> mapping_table; |
| 322 | SrcMap src_mapping_table; |
| 323 | codegen->BuildMappingTable(&mapping_table, |
| 324 | GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? |
| 325 | &src_mapping_table : nullptr); |
| 326 | |
| 327 | std::vector<uint8_t> stack_map; |
| 328 | codegen->BuildStackMaps(&stack_map); |
| 329 | |
| 330 | return new CompiledMethod(GetCompilerDriver(), |
| 331 | instruction_set, |
| 332 | allocator.GetMemory(), |
| 333 | codegen->GetFrameSize(), |
| 334 | codegen->GetCoreSpillMask(), |
| 335 | 0, /* FPR spill mask, unused */ |
| 336 | mapping_table, |
| 337 | stack_map); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 338 | } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { |
| 339 | LOG(FATAL) << "Could not allocate registers in optimizing compiler"; |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 340 | UNREACHABLE(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 341 | } else { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 342 | VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 343 | unoptimized_compiled_methods_++; |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 344 | codegen->CompileBaseline(&allocator); |
| 345 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 346 | std::vector<uint8_t> mapping_table; |
| 347 | SrcMap src_mapping_table; |
| 348 | codegen->BuildMappingTable(&mapping_table, |
| 349 | GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? |
| 350 | &src_mapping_table : nullptr); |
| 351 | std::vector<uint8_t> vmap_table; |
| 352 | codegen->BuildVMapTable(&vmap_table); |
| 353 | std::vector<uint8_t> gc_map; |
| 354 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
| 355 | |
| 356 | return new CompiledMethod(GetCompilerDriver(), |
| 357 | instruction_set, |
| 358 | allocator.GetMemory(), |
| 359 | codegen->GetFrameSize(), |
| 360 | codegen->GetCoreSpillMask(), |
| 361 | 0, /* FPR spill mask, unused */ |
| 362 | &src_mapping_table, |
| 363 | mapping_table, |
| 364 | vmap_table, |
| 365 | gc_map, |
| 366 | nullptr); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 367 | } |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 370 | Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { |
| 371 | return new OptimizingCompiler(driver); |
| 372 | } |
| 373 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 374 | } // namespace art |