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