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" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 28 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 29 | #include "driver/compiler_driver.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 30 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 31 | #include "elf_writer_quick.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 32 | #include "graph_visualizer.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 33 | #include "gvn.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 34 | #include "inliner.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 35 | #include "instruction_simplifier.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 36 | #include "intrinsics.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 37 | #include "jni/quick/jni_compiler.h" |
| 38 | #include "mirror/art_method-inl.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 39 | #include "nodes.h" |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 40 | #include "prepare_for_register_allocation.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 41 | #include "register_allocator.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 42 | #include "ssa_builder.h" |
Nicolas Geoffray | 7dc206a | 2014-07-11 09:49:49 +0100 | [diff] [blame] | 43 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 44 | #include "ssa_liveness_analysis.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 45 | #include "utils/arena_allocator.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 46 | |
| 47 | namespace art { |
| 48 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Used by the code generator, to allocate the code in a vector. |
| 51 | */ |
| 52 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 53 | public: |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 54 | CodeVectorAllocator() {} |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 55 | |
| 56 | virtual uint8_t* Allocate(size_t size) { |
| 57 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 58 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 59 | return &memory_[0]; |
| 60 | } |
| 61 | |
| 62 | size_t GetSize() const { return size_; } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 63 | const std::vector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | std::vector<uint8_t> memory_; |
| 67 | size_t size_; |
| 68 | |
| 69 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 70 | }; |
| 71 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 72 | /** |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 73 | * Filter to apply to the visualizer. Methods whose name contain that filter will |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 74 | * be dumped. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 75 | */ |
| 76 | static const char* kStringFilter = ""; |
| 77 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 78 | class OptimizingCompiler FINAL : public Compiler { |
| 79 | public: |
| 80 | explicit OptimizingCompiler(CompilerDriver* driver); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 81 | ~OptimizingCompiler(); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 82 | |
| 83 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const |
| 84 | OVERRIDE; |
| 85 | |
| 86 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 87 | uint32_t access_flags, |
| 88 | InvokeType invoke_type, |
| 89 | uint16_t class_def_idx, |
| 90 | uint32_t method_idx, |
| 91 | jobject class_loader, |
| 92 | const DexFile& dex_file) const OVERRIDE; |
| 93 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 94 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 95 | uint32_t method_idx, |
| 96 | const DexFile& dex_file) const OVERRIDE; |
| 97 | |
| 98 | uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE |
| 99 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 100 | |
| 101 | bool WriteElf(art::File* file, |
| 102 | OatWriter* oat_writer, |
| 103 | const std::vector<const art::DexFile*>& dex_files, |
| 104 | const std::string& android_root, |
| 105 | bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 106 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 107 | Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED, |
| 108 | void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE { |
| 109 | return nullptr; |
| 110 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 111 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 112 | void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {} |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 113 | |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 114 | void Init() 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 UnInit() const OVERRIDE {} |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 117 | |
| 118 | private: |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 119 | // Whether we should run any optimization or register allocation. If false, will |
| 120 | // just run the code generation after the graph was built. |
| 121 | const bool run_optimizations_; |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 122 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 123 | // Optimize and compile `graph`. |
| 124 | CompiledMethod* CompileOptimized(HGraph* graph, |
| 125 | CodeGenerator* codegen, |
| 126 | CompilerDriver* driver, |
| 127 | const DexCompilationUnit& dex_compilation_unit, |
| 128 | const HGraphVisualizer& visualizer) const; |
| 129 | |
| 130 | // Just compile without doing optimizations. |
| 131 | CompiledMethod* CompileBaseline(CodeGenerator* codegen, |
| 132 | CompilerDriver* driver, |
| 133 | const DexCompilationUnit& dex_compilation_unit) const; |
| 134 | |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 135 | mutable OptimizingCompilerStats compilation_stats_; |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 136 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 137 | std::unique_ptr<std::ostream> visualizer_output_; |
| 138 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 139 | DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); |
| 140 | }; |
| 141 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 142 | static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ |
| 143 | |
| 144 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) |
| 145 | : Compiler(driver, kMaximumCompilationTimeBeforeWarning), |
| 146 | run_optimizations_( |
| 147 | driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime), |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 148 | compilation_stats_() {} |
| 149 | |
| 150 | void OptimizingCompiler::Init() { |
| 151 | // Enable C1visualizer output. Must be done in Init() because the compiler |
| 152 | // driver is not fully initialized when passed to the compiler's constructor. |
| 153 | CompilerDriver* driver = GetCompilerDriver(); |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 154 | const std::string cfg_file_name = driver->GetDumpCfgFileName(); |
| 155 | if (!cfg_file_name.empty()) { |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 156 | CHECK_EQ(driver->GetThreadCount(), 1U) |
| 157 | << "Graph visualizer requires the compiler to run single-threaded. " |
| 158 | << "Invoke the compiler with '-j1'."; |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 159 | visualizer_output_.reset(new std::ofstream(cfg_file_name)); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 162 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 163 | OptimizingCompiler::~OptimizingCompiler() { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 164 | compilation_stats_.Log(); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 167 | bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, |
| 168 | const DexFile& dex_file ATTRIBUTE_UNUSED, |
| 169 | CompilationUnit* cu ATTRIBUTE_UNUSED) const { |
| 170 | return true; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, |
| 174 | uint32_t method_idx, |
| 175 | const DexFile& dex_file) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 176 | return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { |
Nicolas Geoffray | 4179cc1 | 2014-11-19 08:35:17 +0000 | [diff] [blame] | 180 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 181 | InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer, |
| 185 | const std::vector<const art::DexFile*>& dex_files, |
| 186 | const std::string& android_root, bool is_host) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 187 | return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host, |
| 188 | *GetCompilerDriver()); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 191 | static bool IsInstructionSetSupported(InstructionSet instruction_set) { |
| 192 | return instruction_set == kArm64 |
| 193 | || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) |
| 194 | || instruction_set == kX86 |
| 195 | || instruction_set == kX86_64; |
| 196 | } |
| 197 | |
Nicolas Geoffray | de58ab2 | 2014-11-05 12:46:03 +0000 | [diff] [blame] | 198 | static bool CanOptimize(const DexFile::CodeItem& code_item) { |
| 199 | // TODO: We currently cannot optimize methods with try/catch. |
| 200 | return code_item.tries_size_ == 0; |
| 201 | } |
| 202 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 203 | static void RunOptimizations(HGraph* graph, |
| 204 | CompilerDriver* driver, |
| 205 | OptimizingCompilerStats* stats, |
| 206 | const DexCompilationUnit& dex_compilation_unit, |
| 207 | const HGraphVisualizer& visualizer) { |
| 208 | SsaRedundantPhiElimination redundant_phi(graph); |
| 209 | SsaDeadPhiElimination dead_phi(graph); |
| 210 | HDeadCodeElimination dce(graph); |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 211 | HConstantFolding fold1(graph); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 212 | InstructionSimplifier simplify1(graph); |
| 213 | |
| 214 | HInliner inliner(graph, dex_compilation_unit, driver, stats); |
| 215 | |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 216 | HConstantFolding fold2(graph); |
Nicolas Geoffray | 86dde16 | 2015-01-26 12:54:33 +0000 | [diff] [blame] | 217 | SideEffectsAnalysis side_effects(graph); |
| 218 | GVNOptimization gvn(graph, side_effects); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 219 | BoundsCheckElimination bce(graph); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 220 | InstructionSimplifier simplify2(graph); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 221 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 222 | IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver); |
| 223 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 224 | HOptimization* optimizations[] = { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 225 | &redundant_phi, |
| 226 | &dead_phi, |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 227 | &intrinsics, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 228 | &dce, |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 229 | &fold1, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 230 | &simplify1, |
| 231 | &inliner, |
Nicolas Geoffray | 9ee6618 | 2015-01-16 12:35:40 +0000 | [diff] [blame] | 232 | &fold2, |
Nicolas Geoffray | 86dde16 | 2015-01-26 12:54:33 +0000 | [diff] [blame] | 233 | &side_effects, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 234 | &gvn, |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 235 | &bce, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 236 | &simplify2 |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 237 | }; |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 238 | |
| 239 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 240 | HOptimization* optimization = optimizations[i]; |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 241 | visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 242 | optimization->Run(); |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 243 | visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true); |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 244 | optimization->Check(); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 248 | // The stack map we generate must be 4-byte aligned on ARM. Since existing |
| 249 | // maps are generated alongside these stack maps, we must also align them. |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 250 | static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) { |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 251 | size_t size = vector.size(); |
| 252 | size_t aligned_size = RoundUp(size, 4); |
| 253 | for (; size < aligned_size; ++size) { |
| 254 | vector.push_back(0); |
| 255 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 256 | return ArrayRef<const uint8_t>(vector); |
Nicolas Geoffray | 376b2bb | 2014-12-09 14:26:32 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 259 | |
| 260 | CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph, |
| 261 | CodeGenerator* codegen, |
| 262 | CompilerDriver* compiler_driver, |
| 263 | const DexCompilationUnit& dex_compilation_unit, |
| 264 | const HGraphVisualizer& visualizer) const { |
| 265 | RunOptimizations( |
| 266 | graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer); |
| 267 | |
| 268 | PrepareForRegisterAllocation(graph).Run(); |
| 269 | SsaLivenessAnalysis liveness(*graph, codegen); |
| 270 | liveness.Analyze(); |
| 271 | visualizer.DumpGraph(kLivenessPassName); |
| 272 | |
| 273 | RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); |
| 274 | register_allocator.AllocateRegisters(); |
| 275 | visualizer.DumpGraph(kRegisterAllocatorPassName); |
| 276 | |
| 277 | CodeVectorAllocator allocator; |
| 278 | codegen->CompileOptimized(&allocator); |
| 279 | |
| 280 | std::vector<uint8_t> stack_map; |
| 281 | codegen->BuildStackMaps(&stack_map); |
| 282 | |
| 283 | compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized); |
| 284 | |
| 285 | return CompiledMethod::SwapAllocCompiledMethodStackMap( |
| 286 | compiler_driver, |
| 287 | codegen->GetInstructionSet(), |
| 288 | ArrayRef<const uint8_t>(allocator.GetMemory()), |
| 289 | codegen->GetFrameSize(), |
| 290 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 291 | codegen->GetFpuSpillMask(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 292 | ArrayRef<const uint8_t>(stack_map)); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | CompiledMethod* OptimizingCompiler::CompileBaseline( |
| 297 | CodeGenerator* codegen, |
| 298 | CompilerDriver* compiler_driver, |
| 299 | const DexCompilationUnit& dex_compilation_unit) const { |
| 300 | CodeVectorAllocator allocator; |
| 301 | codegen->CompileBaseline(&allocator); |
| 302 | |
| 303 | std::vector<uint8_t> mapping_table; |
| 304 | DefaultSrcMap src_mapping_table; |
| 305 | bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols(); |
| 306 | codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr); |
| 307 | std::vector<uint8_t> vmap_table; |
| 308 | codegen->BuildVMapTable(&vmap_table); |
| 309 | std::vector<uint8_t> gc_map; |
| 310 | codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); |
| 311 | |
| 312 | compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline); |
| 313 | return CompiledMethod::SwapAllocCompiledMethod(compiler_driver, |
| 314 | codegen->GetInstructionSet(), |
| 315 | ArrayRef<const uint8_t>(allocator.GetMemory()), |
| 316 | codegen->GetFrameSize(), |
| 317 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 318 | codegen->GetFpuSpillMask(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 319 | &src_mapping_table, |
| 320 | AlignVectorSize(mapping_table), |
| 321 | AlignVectorSize(vmap_table), |
| 322 | AlignVectorSize(gc_map), |
| 323 | ArrayRef<const uint8_t>()); |
| 324 | } |
| 325 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 326 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
| 327 | uint32_t access_flags, |
| 328 | InvokeType invoke_type, |
| 329 | uint16_t class_def_idx, |
| 330 | uint32_t method_idx, |
| 331 | jobject class_loader, |
| 332 | const DexFile& dex_file) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 333 | UNUSED(invoke_type); |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 334 | compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 335 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 336 | InstructionSet instruction_set = compiler_driver->GetInstructionSet(); |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 337 | // Always use the thumb2 assembler: some runtime functionality (like implicit stack |
| 338 | // overflow checks) assume thumb2. |
| 339 | if (instruction_set == kArm) { |
| 340 | instruction_set = kThumb2; |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 344 | if (!IsInstructionSetSupported(instruction_set)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 345 | compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 346 | return nullptr; |
| 347 | } |
| 348 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 349 | if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 350 | compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological); |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 351 | return nullptr; |
| 352 | } |
| 353 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 354 | DexCompilationUnit dex_compilation_unit( |
| 355 | nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 356 | class_def_idx, method_idx, access_flags, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 357 | compiler_driver->GetVerifiedMethod(&dex_file, method_idx)); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 358 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 359 | std::string method_name = PrettyMethod(method_idx, dex_file); |
| 360 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 361 | // For testing purposes, we put a special marker on method names that should be compiled |
| 362 | // with this compiler. This makes sure we're not regressing. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 363 | bool shouldCompile = method_name.find("$opt$") != std::string::npos; |
| 364 | bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 365 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 366 | ArenaPool pool; |
| 367 | ArenaAllocator arena(&pool); |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 368 | HGraphBuilder builder(&arena, |
| 369 | &dex_compilation_unit, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 370 | &dex_compilation_unit, |
| 371 | &dex_file, |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 372 | compiler_driver, |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 373 | &compilation_stats_); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 374 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 375 | VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 376 | HGraph* graph = builder.BuildGraph(*code_item); |
| 377 | if (graph == nullptr) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 378 | CHECK(!shouldCompile) << "Could not build graph in optimizing compiler"; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 379 | return nullptr; |
| 380 | } |
| 381 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 382 | std::unique_ptr<CodeGenerator> codegen( |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 383 | CodeGenerator::Create(graph, |
| 384 | instruction_set, |
| 385 | *compiler_driver->GetInstructionSetFeatures(), |
| 386 | compiler_driver->GetCompilerOptions())); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 387 | if (codegen.get() == nullptr) { |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 388 | CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 389 | compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 390 | return nullptr; |
| 391 | } |
| 392 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 393 | HGraphVisualizer visualizer( |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 394 | visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 395 | visualizer.DumpGraph("builder"); |
| 396 | |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 397 | bool can_optimize = CanOptimize(*code_item); |
| 398 | bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 399 | CompiledMethod* result = nullptr; |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 400 | if (run_optimizations_ && can_optimize && can_allocate_registers) { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 401 | VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 402 | if (!graph->TryBuildingSsa()) { |
| 403 | LOG(INFO) << "Skipping compilation of " |
| 404 | << PrettyMethod(method_idx, dex_file) |
| 405 | << ": it contains a non natural loop"; |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 406 | // We could not transform the graph to SSA, bailout. |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 407 | compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 408 | } else { |
| 409 | result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer); |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 410 | } |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 411 | } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { |
| 412 | LOG(FATAL) << "Could not allocate registers in optimizing compiler"; |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 413 | UNREACHABLE(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 414 | } else { |
Nicolas Geoffray | 43a539f | 2014-12-02 10:19:51 +0000 | [diff] [blame] | 415 | VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file); |
Calin Juravle | 48c2b03 | 2014-12-09 18:11:36 +0000 | [diff] [blame] | 416 | |
| 417 | if (!run_optimizations_) { |
| 418 | compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled); |
| 419 | } else if (!can_optimize) { |
| 420 | compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch); |
| 421 | } else if (!can_allocate_registers) { |
| 422 | compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator); |
| 423 | } |
| 424 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 425 | result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 426 | } |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 427 | return result; |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 430 | Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { |
| 431 | return new OptimizingCompiler(driver); |
| 432 | } |
| 433 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 434 | } // namespace art |