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> |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 20 | #include <memory> |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 21 | #include <sstream> |
| 22 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 25 | #ifdef ART_ENABLE_CODEGEN_arm |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 26 | #include "dex_cache_array_fixups_arm.h" |
| 27 | #endif |
| 28 | |
| 29 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 30 | #include "instruction_simplifier_arm64.h" |
| 31 | #endif |
| 32 | |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 33 | #ifdef ART_ENABLE_CODEGEN_mips |
| 34 | #include "dex_cache_array_fixups_mips.h" |
| 35 | #include "pc_relative_fixups_mips.h" |
| 36 | #endif |
| 37 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 38 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 39 | #include "pc_relative_fixups_x86.h" |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 40 | #endif |
| 41 | |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 42 | #if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64) |
| 43 | #include "x86_memory_gen.h" |
| 44 | #endif |
| 45 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 46 | #include "art_method-inl.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 47 | #include "base/arena_allocator.h" |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 48 | #include "base/arena_containers.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 49 | #include "base/dumpable.h" |
Vladimir Marko | c90d7c7 | 2015-10-06 17:30:45 +0000 | [diff] [blame] | 50 | #include "base/macros.h" |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 51 | #include "base/mutex.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 52 | #include "base/timing_logger.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 53 | #include "bounds_check_elimination.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 54 | #include "builder.h" |
| 55 | #include "code_generator.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 56 | #include "compiled_method.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 57 | #include "compiler.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 58 | #include "constant_folding.h" |
| 59 | #include "dead_code_elimination.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 60 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 61 | #include "debug/method_debug_info.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 62 | #include "dex/verification_results.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 63 | #include "dex/verified_method.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 64 | #include "driver/compiler_driver-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 65 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 66 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 67 | #include "elf_writer_quick.h" |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 68 | #include "graph_checker.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 69 | #include "graph_visualizer.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 70 | #include "gvn.h" |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 71 | #include "induction_var_analysis.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 72 | #include "inliner.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 73 | #include "instruction_simplifier.h" |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 74 | #include "instruction_simplifier_arm.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 75 | #include "intrinsics.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 76 | #include "jit/debugger_interface.h" |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 77 | #include "jit/jit.h" |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 78 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 79 | #include "jni/quick/jni_compiler.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 80 | #include "licm.h" |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 81 | #include "load_store_elimination.h" |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 82 | #include "loop_optimization.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 83 | #include "nodes.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 84 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 85 | #include "prepare_for_register_allocation.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 86 | #include "reference_type_propagation.h" |
Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 87 | #include "register_allocator_linear_scan.h" |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 88 | #include "select_generator.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 89 | #include "sharpening.h" |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 90 | #include "side_effects_analysis.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 91 | #include "ssa_builder.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 92 | #include "ssa_liveness_analysis.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 93 | #include "ssa_phi_elimination.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 94 | #include "utils/assembler.h" |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 95 | #include "verifier/method_verifier.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 96 | |
| 97 | namespace art { |
| 98 | |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 99 | static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB; |
| 100 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 101 | static constexpr const char* kPassNameSeparator = "$"; |
| 102 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 103 | /** |
| 104 | * Used by the code generator, to allocate the code in a vector. |
| 105 | */ |
| 106 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 107 | public: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 108 | explicit CodeVectorAllocator(ArenaAllocator* arena) |
| 109 | : memory_(arena->Adapter(kArenaAllocCodeBuffer)), |
| 110 | size_(0) {} |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 111 | |
| 112 | virtual uint8_t* Allocate(size_t size) { |
| 113 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 114 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 115 | return &memory_[0]; |
| 116 | } |
| 117 | |
| 118 | size_t GetSize() const { return size_; } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 119 | const ArenaVector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 120 | |
| 121 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 122 | ArenaVector<uint8_t> memory_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 123 | size_t size_; |
| 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 126 | }; |
| 127 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 128 | /** |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 129 | * 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] | 130 | * be dumped. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 131 | */ |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 132 | static constexpr const char kStringFilter[] = ""; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 133 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 134 | class PassScope; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 135 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 136 | class PassObserver : public ValueObject { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 137 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 138 | PassObserver(HGraph* graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 139 | CodeGenerator* codegen, |
| 140 | std::ostream* visualizer_output, |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 141 | CompilerDriver* compiler_driver, |
| 142 | Mutex& dump_mutex) |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 143 | : graph_(graph), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 144 | cached_method_name_(), |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 145 | timing_logger_enabled_(compiler_driver->GetDumpPasses()), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 146 | timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true), |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 147 | disasm_info_(graph->GetArena()), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 148 | visualizer_oss_(), |
| 149 | visualizer_output_(visualizer_output), |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 150 | visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 151 | visualizer_(&visualizer_oss_, graph, *codegen), |
| 152 | visualizer_dump_mutex_(dump_mutex), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 153 | graph_in_bad_state_(false) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 154 | if (timing_logger_enabled_ || visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 155 | if (!IsVerboseMethod(compiler_driver, GetMethodName())) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 156 | timing_logger_enabled_ = visualizer_enabled_ = false; |
| 157 | } |
| 158 | if (visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 159 | visualizer_.PrintHeader(GetMethodName()); |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 160 | codegen->SetDisassemblyInformation(&disasm_info_); |
| 161 | } |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 162 | } |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 163 | } |
| 164 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 165 | ~PassObserver() { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 166 | if (timing_logger_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 167 | LOG(INFO) << "TIMINGS " << GetMethodName(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 168 | LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); |
| 169 | } |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 170 | if (visualizer_enabled_) { |
| 171 | MutexLock mu(Thread::Current(), visualizer_dump_mutex_); |
| 172 | *visualizer_output_ << visualizer_oss_.str(); |
| 173 | } |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 176 | void DumpDisassembly() const { |
| 177 | if (visualizer_enabled_) { |
| 178 | visualizer_.DumpGraphWithDisassembly(); |
| 179 | } |
| 180 | } |
| 181 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 182 | void SetGraphInBadState() { graph_in_bad_state_ = true; } |
| 183 | |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 184 | const char* GetMethodName() { |
| 185 | // PrettyMethod() is expensive, so we delay calling it until we actually have to. |
| 186 | if (cached_method_name_.empty()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 187 | cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx()); |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 188 | } |
| 189 | return cached_method_name_.c_str(); |
| 190 | } |
| 191 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 192 | private: |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 193 | void StartPass(const char* pass_name) { |
Wojciech Staszkiewicz | e706070 | 2016-08-16 17:31:19 -0700 | [diff] [blame] | 194 | VLOG(compiler) << "Starting pass: " << pass_name; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 195 | // Dump graph first, then start timer. |
| 196 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 197 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 198 | } |
| 199 | if (timing_logger_enabled_) { |
| 200 | timing_logger_.StartTiming(pass_name); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void EndPass(const char* pass_name) { |
| 205 | // Pause timer first, then dump graph. |
| 206 | if (timing_logger_enabled_) { |
| 207 | timing_logger_.EndTiming(); |
| 208 | } |
| 209 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 210 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 211 | } |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 212 | |
| 213 | // Validate the HGraph if running in debug mode. |
| 214 | if (kIsDebugBuild) { |
| 215 | if (!graph_in_bad_state_) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 216 | GraphChecker checker(graph_); |
| 217 | checker.Run(); |
| 218 | if (!checker.IsValid()) { |
| 219 | LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 225 | static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { |
| 226 | // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an |
| 227 | // empty kStringFilter matching all methods. |
| 228 | if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { |
| 229 | return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); |
| 230 | } |
| 231 | |
| 232 | // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code |
| 233 | // warning when the string is empty. |
| 234 | constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; |
| 235 | if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 242 | HGraph* const graph_; |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 243 | |
| 244 | std::string cached_method_name_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 245 | |
| 246 | bool timing_logger_enabled_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 247 | TimingLogger timing_logger_; |
| 248 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 249 | DisassemblyInformation disasm_info_; |
| 250 | |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 251 | std::ostringstream visualizer_oss_; |
| 252 | std::ostream* visualizer_output_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 253 | bool visualizer_enabled_; |
| 254 | HGraphVisualizer visualizer_; |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 255 | Mutex& visualizer_dump_mutex_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 256 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 257 | // Flag to be set by the compiler if the pass failed and the graph is not |
| 258 | // expected to validate. |
| 259 | bool graph_in_bad_state_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 260 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 261 | friend PassScope; |
| 262 | |
| 263 | DISALLOW_COPY_AND_ASSIGN(PassObserver); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 264 | }; |
| 265 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 266 | class PassScope : public ValueObject { |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 267 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 268 | PassScope(const char *pass_name, PassObserver* pass_observer) |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 269 | : pass_name_(pass_name), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 270 | pass_observer_(pass_observer) { |
| 271 | pass_observer_->StartPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 272 | } |
| 273 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 274 | ~PassScope() { |
| 275 | pass_observer_->EndPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | private: |
| 279 | const char* const pass_name_; |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 280 | PassObserver* const pass_observer_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 283 | class OptimizingCompiler FINAL : public Compiler { |
| 284 | public: |
| 285 | explicit OptimizingCompiler(CompilerDriver* driver); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 286 | ~OptimizingCompiler() OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 287 | |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 288 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 289 | |
| 290 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 291 | uint32_t access_flags, |
| 292 | InvokeType invoke_type, |
| 293 | uint16_t class_def_idx, |
| 294 | uint32_t method_idx, |
| 295 | jobject class_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 296 | const DexFile& dex_file, |
| 297 | Handle<mirror::DexCache> dex_cache) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 298 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 299 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 300 | uint32_t method_idx, |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame] | 301 | const DexFile& dex_file, |
| 302 | JniOptimizationFlags optimization_flags) const OVERRIDE { |
| 303 | return ArtQuickJniCompileMethod(GetCompilerDriver(), |
| 304 | access_flags, |
| 305 | method_idx, |
| 306 | dex_file, |
| 307 | optimization_flags); |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 308 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 309 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 310 | uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 311 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 312 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 313 | InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); |
| 314 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 315 | |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 316 | void Init() OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 317 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 318 | void UnInit() const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 319 | |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 320 | void MaybeRecordStat(MethodCompilationStat compilation_stat) const { |
| 321 | if (compilation_stats_.get() != nullptr) { |
| 322 | compilation_stats_->RecordStat(compilation_stat); |
| 323 | } |
| 324 | } |
| 325 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 326 | bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr) |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 327 | OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 328 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 329 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 330 | private: |
| 331 | void RunOptimizations(HGraph* graph, |
| 332 | CodeGenerator* codegen, |
| 333 | CompilerDriver* driver, |
| 334 | const DexCompilationUnit& dex_compilation_unit, |
| 335 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 336 | VariableSizedHandleScope* handles) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 337 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 338 | void RunOptimizations(HOptimization* optimizations[], |
| 339 | size_t length, |
| 340 | PassObserver* pass_observer) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 341 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 342 | private: |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 343 | // Create a 'CompiledMethod' for an optimized graph. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 344 | CompiledMethod* Emit(ArenaAllocator* arena, |
| 345 | CodeVectorAllocator* code_allocator, |
| 346 | CodeGenerator* codegen, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 347 | CompilerDriver* driver, |
| 348 | const DexFile::CodeItem* item) const; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 349 | |
| 350 | // Try compiling a method and return the code generator used for |
| 351 | // compiling it. |
| 352 | // This method: |
| 353 | // 1) Builds the graph. Returns null if it failed to build it. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 354 | // 2) Transforms the graph to SSA. Returns null if it failed. |
| 355 | // 3) Runs optimizations on the graph, including register allocator. |
| 356 | // 4) Generates code with the `code_allocator` provided. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 357 | CodeGenerator* TryCompile(ArenaAllocator* arena, |
| 358 | CodeVectorAllocator* code_allocator, |
| 359 | const DexFile::CodeItem* code_item, |
| 360 | uint32_t access_flags, |
| 361 | InvokeType invoke_type, |
| 362 | uint16_t class_def_idx, |
| 363 | uint32_t method_idx, |
| 364 | jobject class_loader, |
| 365 | const DexFile& dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 366 | Handle<mirror::DexCache> dex_cache, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 367 | ArtMethod* method, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 368 | bool osr) const; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 369 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 370 | void MaybeRunInliner(HGraph* graph, |
| 371 | CodeGenerator* codegen, |
| 372 | CompilerDriver* driver, |
| 373 | const DexCompilationUnit& dex_compilation_unit, |
| 374 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 375 | VariableSizedHandleScope* handles) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 376 | |
| 377 | void RunArchOptimizations(InstructionSet instruction_set, |
| 378 | HGraph* graph, |
| 379 | CodeGenerator* codegen, |
| 380 | PassObserver* pass_observer) const; |
| 381 | |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 382 | std::unique_ptr<OptimizingCompilerStats> compilation_stats_; |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 383 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 384 | std::unique_ptr<std::ostream> visualizer_output_; |
| 385 | |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 386 | mutable Mutex dump_mutex_; // To synchronize visualizer writing. |
| 387 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 388 | DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); |
| 389 | }; |
| 390 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 391 | static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ |
| 392 | |
| 393 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 394 | : Compiler(driver, kMaximumCompilationTimeBeforeWarning), |
| 395 | dump_mutex_("Visualizer dump lock") {} |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 396 | |
| 397 | void OptimizingCompiler::Init() { |
| 398 | // Enable C1visualizer output. Must be done in Init() because the compiler |
| 399 | // driver is not fully initialized when passed to the compiler's constructor. |
| 400 | CompilerDriver* driver = GetCompilerDriver(); |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 401 | const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName(); |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 402 | if (!cfg_file_name.empty()) { |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 403 | std::ios_base::openmode cfg_file_mode = |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 404 | driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 405 | visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 406 | } |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 407 | if (driver->GetDumpStats()) { |
| 408 | compilation_stats_.reset(new OptimizingCompilerStats()); |
| 409 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 410 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 411 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 412 | void OptimizingCompiler::UnInit() const { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 415 | OptimizingCompiler::~OptimizingCompiler() { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 416 | if (compilation_stats_.get() != nullptr) { |
| 417 | compilation_stats_->Log(); |
| 418 | } |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 421 | bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 422 | const DexFile& dex_file ATTRIBUTE_UNUSED) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 423 | return true; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 426 | static bool IsInstructionSetSupported(InstructionSet instruction_set) { |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 427 | return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) |
| 428 | || instruction_set == kArm64 |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 429 | || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 430 | || instruction_set == kMips |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 431 | || instruction_set == kMips64 |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 432 | || instruction_set == kX86 |
| 433 | || instruction_set == kX86_64; |
| 434 | } |
| 435 | |
Roland Levillain | 22ccc3a | 2015-11-24 13:10:05 +0000 | [diff] [blame] | 436 | // Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 437 | // TODO: Add support for other architectures and remove this function |
| 438 | static bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) { |
Roland Levillain | 22ccc3a | 2015-11-24 13:10:05 +0000 | [diff] [blame] | 439 | return instruction_set == kArm64 |
| 440 | || instruction_set == kThumb2 |
Roland Levillain | 3b359c7 | 2015-11-17 19:35:12 +0000 | [diff] [blame] | 441 | || instruction_set == kX86 |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 442 | || instruction_set == kX86_64; |
| 443 | } |
| 444 | |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 445 | // Strip pass name suffix to get optimization name. |
| 446 | static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) { |
| 447 | size_t pos = pass_name.find(kPassNameSeparator); |
| 448 | return pos == std::string::npos ? pass_name : pass_name.substr(0, pos); |
| 449 | } |
| 450 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 451 | static HOptimization* BuildOptimization( |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 452 | const std::string& pass_name, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 453 | ArenaAllocator* arena, |
| 454 | HGraph* graph, |
| 455 | OptimizingCompilerStats* stats, |
| 456 | CodeGenerator* codegen, |
| 457 | CompilerDriver* driver, |
| 458 | const DexCompilationUnit& dex_compilation_unit, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 459 | VariableSizedHandleScope* handles, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 460 | SideEffectsAnalysis* most_recent_side_effects, |
| 461 | HInductionVarAnalysis* most_recent_induction) { |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 462 | std::string opt_name = ConvertPassNameToOptimizationName(pass_name); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 463 | if (opt_name == BoundsCheckElimination::kBoundsCheckEliminationPassName) { |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 464 | CHECK(most_recent_side_effects != nullptr && most_recent_induction != nullptr); |
| 465 | return new (arena) BoundsCheckElimination(graph, |
| 466 | *most_recent_side_effects, |
| 467 | most_recent_induction); |
| 468 | } else if (opt_name == GVNOptimization::kGlobalValueNumberingPassName) { |
| 469 | CHECK(most_recent_side_effects != nullptr); |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 470 | return new (arena) GVNOptimization(graph, *most_recent_side_effects, pass_name.c_str()); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 471 | } else if (opt_name == HConstantFolding::kConstantFoldingPassName) { |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 472 | return new (arena) HConstantFolding(graph, pass_name.c_str()); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 473 | } else if (opt_name == HDeadCodeElimination::kDeadCodeEliminationPassName) { |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 474 | return new (arena) HDeadCodeElimination(graph, stats, pass_name.c_str()); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 475 | } else if (opt_name == HInliner::kInlinerPassName) { |
| 476 | size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; |
| 477 | return new (arena) HInliner(graph, // outer_graph |
| 478 | graph, // outermost_graph |
| 479 | codegen, |
| 480 | dex_compilation_unit, // outer_compilation_unit |
| 481 | dex_compilation_unit, // outermost_compilation_unit |
| 482 | driver, |
| 483 | handles, |
| 484 | stats, |
| 485 | number_of_dex_registers, |
| 486 | /* depth */ 0); |
| 487 | } else if (opt_name == HSharpening::kSharpeningPassName) { |
| 488 | return new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); |
| 489 | } else if (opt_name == HSelectGenerator::kSelectGeneratorPassName) { |
| 490 | return new (arena) HSelectGenerator(graph, stats); |
| 491 | } else if (opt_name == HInductionVarAnalysis::kInductionPassName) { |
| 492 | return new (arena) HInductionVarAnalysis(graph); |
| 493 | } else if (opt_name == InstructionSimplifier::kInstructionSimplifierPassName) { |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 494 | return new (arena) InstructionSimplifier(graph, stats, pass_name.c_str()); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 495 | } else if (opt_name == IntrinsicsRecognizer::kIntrinsicsRecognizerPassName) { |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 496 | return new (arena) IntrinsicsRecognizer(graph, stats); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 497 | } else if (opt_name == LICM::kLoopInvariantCodeMotionPassName) { |
| 498 | CHECK(most_recent_side_effects != nullptr); |
| 499 | return new (arena) LICM(graph, *most_recent_side_effects, stats); |
| 500 | } else if (opt_name == LoadStoreElimination::kLoadStoreEliminationPassName) { |
| 501 | CHECK(most_recent_side_effects != nullptr); |
| 502 | return new (arena) LoadStoreElimination(graph, *most_recent_side_effects); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 503 | } else if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) { |
| 504 | return new (arena) SideEffectsAnalysis(graph); |
Nicolas Geoffray | ebe1674 | 2016-10-05 09:55:42 +0100 | [diff] [blame] | 505 | } else if (opt_name == HLoopOptimization::kLoopOptimizationPassName) { |
| 506 | return new (arena) HLoopOptimization(graph, most_recent_induction); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 507 | #ifdef ART_ENABLE_CODEGEN_arm |
| 508 | } else if (opt_name == arm::DexCacheArrayFixups::kDexCacheArrayFixupsArmPassName) { |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 509 | return new (arena) arm::DexCacheArrayFixups(graph, codegen, stats); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 510 | } else if (opt_name == arm::InstructionSimplifierArm::kInstructionSimplifierArmPassName) { |
| 511 | return new (arena) arm::InstructionSimplifierArm(graph, stats); |
| 512 | #endif |
| 513 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 514 | } else if (opt_name == arm64::InstructionSimplifierArm64::kInstructionSimplifierArm64PassName) { |
| 515 | return new (arena) arm64::InstructionSimplifierArm64(graph, stats); |
| 516 | #endif |
| 517 | #ifdef ART_ENABLE_CODEGEN_mips |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 518 | } else if (opt_name == mips::DexCacheArrayFixups::kDexCacheArrayFixupsMipsPassName) { |
| 519 | return new (arena) mips::DexCacheArrayFixups(graph, codegen, stats); |
| 520 | } else if (opt_name == mips::PcRelativeFixups::kPcRelativeFixupsMipsPassName) { |
| 521 | return new (arena) mips::PcRelativeFixups(graph, codegen, stats); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 522 | #endif |
| 523 | #ifdef ART_ENABLE_CODEGEN_x86 |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 524 | } else if (opt_name == x86::PcRelativeFixups::kPcRelativeFixupsX86PassName) { |
| 525 | return new (arena) x86::PcRelativeFixups(graph, codegen, stats); |
| 526 | } else if (opt_name == x86::X86MemoryOperandGeneration::kX86MemoryOperandGenerationPassName) { |
| 527 | return new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats); |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 528 | #endif |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 529 | } |
| 530 | return nullptr; |
| 531 | } |
| 532 | |
| 533 | static ArenaVector<HOptimization*> BuildOptimizations( |
| 534 | const std::vector<std::string>& pass_names, |
| 535 | ArenaAllocator* arena, |
| 536 | HGraph* graph, |
| 537 | OptimizingCompilerStats* stats, |
| 538 | CodeGenerator* codegen, |
| 539 | CompilerDriver* driver, |
| 540 | const DexCompilationUnit& dex_compilation_unit, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 541 | VariableSizedHandleScope* handles) { |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 542 | // Few HOptimizations constructors require SideEffectsAnalysis or HInductionVarAnalysis |
| 543 | // instances. This method assumes that each of them expects the nearest instance preceeding it |
| 544 | // in the pass name list. |
| 545 | SideEffectsAnalysis* most_recent_side_effects = nullptr; |
| 546 | HInductionVarAnalysis* most_recent_induction = nullptr; |
| 547 | ArenaVector<HOptimization*> ret(arena->Adapter()); |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 548 | for (const std::string& pass_name : pass_names) { |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 549 | HOptimization* opt = BuildOptimization( |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 550 | pass_name, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 551 | arena, |
| 552 | graph, |
| 553 | stats, |
| 554 | codegen, |
| 555 | driver, |
| 556 | dex_compilation_unit, |
| 557 | handles, |
| 558 | most_recent_side_effects, |
| 559 | most_recent_induction); |
| 560 | CHECK(opt != nullptr) << "Couldn't build optimization: \"" << pass_name << "\""; |
| 561 | ret.push_back(opt); |
| 562 | |
Wojciech Staszkiewicz | b6a12832 | 2016-08-16 16:33:00 -0700 | [diff] [blame] | 563 | std::string opt_name = ConvertPassNameToOptimizationName(pass_name); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 564 | if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) { |
| 565 | most_recent_side_effects = down_cast<SideEffectsAnalysis*>(opt); |
| 566 | } else if (opt_name == HInductionVarAnalysis::kInductionPassName) { |
| 567 | most_recent_induction = down_cast<HInductionVarAnalysis*>(opt); |
| 568 | } |
| 569 | } |
| 570 | return ret; |
| 571 | } |
| 572 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 573 | void OptimizingCompiler::RunOptimizations(HOptimization* optimizations[], |
| 574 | size_t length, |
| 575 | PassObserver* pass_observer) const { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 576 | for (size_t i = 0; i < length; ++i) { |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 577 | PassScope scope(optimizations[i]->GetPassName(), pass_observer); |
| 578 | optimizations[i]->Run(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 582 | void OptimizingCompiler::MaybeRunInliner(HGraph* graph, |
| 583 | CodeGenerator* codegen, |
| 584 | CompilerDriver* driver, |
| 585 | const DexCompilationUnit& dex_compilation_unit, |
| 586 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 587 | VariableSizedHandleScope* handles) const { |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 588 | OptimizingCompilerStats* stats = compilation_stats_.get(); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 589 | const CompilerOptions& compiler_options = driver->GetCompilerOptions(); |
| 590 | bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) |
| 591 | && (compiler_options.GetInlineMaxCodeUnits() > 0); |
| 592 | if (!should_inline) { |
| 593 | return; |
| 594 | } |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 595 | size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 596 | HInliner* inliner = new (graph->GetArena()) HInliner( |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 597 | graph, // outer_graph |
| 598 | graph, // outermost_graph |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 599 | codegen, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 600 | dex_compilation_unit, // outer_compilation_unit |
| 601 | dex_compilation_unit, // outermost_compilation_unit |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 602 | driver, |
| 603 | handles, |
| 604 | stats, |
| 605 | number_of_dex_registers, |
| 606 | /* depth */ 0); |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 607 | HOptimization* optimizations[] = { inliner }; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 608 | |
| 609 | RunOptimizations(optimizations, arraysize(optimizations), pass_observer); |
| 610 | } |
| 611 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 612 | void OptimizingCompiler::RunArchOptimizations(InstructionSet instruction_set, |
| 613 | HGraph* graph, |
| 614 | CodeGenerator* codegen, |
| 615 | PassObserver* pass_observer) const { |
Wojciech Staszkiewicz | e28c7d0 | 2016-08-17 19:15:51 -0700 | [diff] [blame] | 616 | UNUSED(codegen); // To avoid compilation error when compiling for svelte |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 617 | OptimizingCompilerStats* stats = compilation_stats_.get(); |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 618 | ArenaAllocator* arena = graph->GetArena(); |
Scott Wakeling | a7812ae | 2016-10-17 10:03:36 +0100 | [diff] [blame] | 619 | #ifdef ART_USE_VIXL_ARM_BACKEND |
| 620 | UNUSED(arena); |
| 621 | UNUSED(pass_observer); |
| 622 | UNUSED(stats); |
| 623 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 624 | switch (instruction_set) { |
Scott Wakeling | a7812ae | 2016-10-17 10:03:36 +0100 | [diff] [blame] | 625 | #if defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_USE_VIXL_ARM_BACKEND) |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 626 | case kThumb2: |
| 627 | case kArm: { |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 628 | arm::DexCacheArrayFixups* fixups = |
| 629 | new (arena) arm::DexCacheArrayFixups(graph, codegen, stats); |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 630 | arm::InstructionSimplifierArm* simplifier = |
| 631 | new (arena) arm::InstructionSimplifierArm(graph, stats); |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 632 | SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 633 | GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch"); |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 634 | HOptimization* arm_optimizations[] = { |
Artem Udovichenko | 4a0dad6 | 2016-01-26 12:28:31 +0300 | [diff] [blame] | 635 | simplifier, |
Artem Serov | 328429f | 2016-07-06 16:23:04 +0100 | [diff] [blame] | 636 | side_effects, |
| 637 | gvn, |
Nicolas Geoffray | 6b5afdd | 2016-01-22 09:31:52 +0000 | [diff] [blame] | 638 | fixups |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 639 | }; |
| 640 | RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer); |
| 641 | break; |
| 642 | } |
| 643 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 644 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 645 | case kArm64: { |
| 646 | arm64::InstructionSimplifierArm64* simplifier = |
| 647 | new (arena) arm64::InstructionSimplifierArm64(graph, stats); |
| 648 | SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 649 | GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch"); |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 650 | HOptimization* arm64_optimizations[] = { |
| 651 | simplifier, |
| 652 | side_effects, |
| 653 | gvn |
| 654 | }; |
| 655 | RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); |
| 656 | break; |
| 657 | } |
| 658 | #endif |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 659 | #ifdef ART_ENABLE_CODEGEN_mips |
| 660 | case kMips: { |
| 661 | mips::PcRelativeFixups* pc_relative_fixups = |
| 662 | new (arena) mips::PcRelativeFixups(graph, codegen, stats); |
| 663 | mips::DexCacheArrayFixups* dex_cache_array_fixups = |
Alexey Frunze | 06a46c4 | 2016-07-19 15:00:40 -0700 | [diff] [blame] | 664 | new (arena) mips::DexCacheArrayFixups(graph, codegen, stats); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 665 | HOptimization* mips_optimizations[] = { |
| 666 | pc_relative_fixups, |
| 667 | dex_cache_array_fixups |
| 668 | }; |
| 669 | RunOptimizations(mips_optimizations, arraysize(mips_optimizations), pass_observer); |
| 670 | break; |
| 671 | } |
| 672 | #endif |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 673 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 674 | case kX86: { |
Aart Bik | d1c4045 | 2016-03-02 16:06:13 -0800 | [diff] [blame] | 675 | x86::PcRelativeFixups* pc_relative_fixups = |
| 676 | new (arena) x86::PcRelativeFixups(graph, codegen, stats); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 677 | x86::X86MemoryOperandGeneration* memory_gen = |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 678 | new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats); |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 679 | HOptimization* x86_optimizations[] = { |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 680 | pc_relative_fixups, |
| 681 | memory_gen |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 682 | }; |
| 683 | RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); |
| 684 | break; |
| 685 | } |
| 686 | #endif |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 687 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
| 688 | case kX86_64: { |
| 689 | x86::X86MemoryOperandGeneration* memory_gen = |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 690 | new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 691 | HOptimization* x86_64_optimizations[] = { |
| 692 | memory_gen |
| 693 | }; |
| 694 | RunOptimizations(x86_64_optimizations, arraysize(x86_64_optimizations), pass_observer); |
| 695 | break; |
| 696 | } |
| 697 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 703 | NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. |
| 704 | static void AllocateRegisters(HGraph* graph, |
| 705 | CodeGenerator* codegen, |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 706 | PassObserver* pass_observer, |
| 707 | RegisterAllocator::Strategy strategy) { |
Mingyao Yang | 700347e | 2016-03-02 14:59:32 -0800 | [diff] [blame] | 708 | { |
| 709 | PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName, |
| 710 | pass_observer); |
| 711 | PrepareForRegisterAllocation(graph).Run(); |
| 712 | } |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 713 | SsaLivenessAnalysis liveness(graph, codegen); |
| 714 | { |
| 715 | PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); |
| 716 | liveness.Analyze(); |
| 717 | } |
| 718 | { |
| 719 | PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 720 | RegisterAllocator::Create(graph->GetArena(), codegen, liveness, strategy)->AllocateRegisters(); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 724 | void OptimizingCompiler::RunOptimizations(HGraph* graph, |
| 725 | CodeGenerator* codegen, |
| 726 | CompilerDriver* driver, |
| 727 | const DexCompilationUnit& dex_compilation_unit, |
| 728 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 729 | VariableSizedHandleScope* handles) const { |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 730 | OptimizingCompilerStats* stats = compilation_stats_.get(); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 731 | ArenaAllocator* arena = graph->GetArena(); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 732 | if (driver->GetCompilerOptions().GetPassesToRun() != nullptr) { |
| 733 | ArenaVector<HOptimization*> optimizations = BuildOptimizations( |
| 734 | *driver->GetCompilerOptions().GetPassesToRun(), |
| 735 | arena, |
| 736 | graph, |
| 737 | stats, |
| 738 | codegen, |
| 739 | driver, |
| 740 | dex_compilation_unit, |
| 741 | handles); |
| 742 | RunOptimizations(&optimizations[0], optimizations.size(), pass_observer); |
| 743 | return; |
| 744 | } |
| 745 | |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 746 | HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 747 | graph, stats, "dead_code_elimination$initial"); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 748 | HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 749 | graph, stats, "dead_code_elimination$final"); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 750 | HConstantFolding* fold1 = new (arena) HConstantFolding(graph); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 751 | InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 752 | HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 753 | HConstantFolding* fold2 = new (arena) HConstantFolding( |
| 754 | graph, "constant_folding$after_inlining"); |
| 755 | HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding$after_bce"); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 756 | SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); |
| 757 | GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 758 | LICM* licm = new (arena) LICM(graph, *side_effects, stats); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 759 | LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 760 | HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 761 | BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction); |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 762 | HLoopOptimization* loop = new (arena) HLoopOptimization(graph, induction); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 763 | HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 764 | InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 765 | graph, stats, "instruction_simplifier$after_inlining"); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 766 | InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 767 | graph, stats, "instruction_simplifier$after_bce"); |
| 768 | InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier( |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 769 | graph, stats, "instruction_simplifier$before_codegen"); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 770 | IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, stats); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 771 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 772 | HOptimization* optimizations1[] = { |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 773 | intrinsics, |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 774 | sharpening, |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 775 | fold1, |
| 776 | simplify1, |
Nicolas Geoffray | 18e6873 | 2015-06-17 23:09:05 +0100 | [diff] [blame] | 777 | dce1, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 778 | }; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 779 | RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); |
| 780 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 781 | MaybeRunInliner(graph, codegen, driver, dex_compilation_unit, pass_observer, handles); |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 782 | |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 783 | HOptimization* optimizations2[] = { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 784 | // SelectGenerator depends on the InstructionSimplifier removing |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 785 | // redundant suspend checks to recognize empty blocks. |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 786 | select_generator, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 787 | fold2, // TODO: if we don't inline we can also skip fold2. |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 788 | simplify2, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 789 | side_effects, |
| 790 | gvn, |
| 791 | licm, |
| 792 | induction, |
| 793 | bce, |
Aart Bik | 281c681 | 2016-08-26 11:31:48 -0700 | [diff] [blame] | 794 | loop, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 795 | fold3, // evaluates code generated by dynamic bce |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 796 | simplify3, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 797 | lse, |
| 798 | dce2, |
| 799 | // The codegen has a few assumptions that only the instruction simplifier |
| 800 | // can satisfy. For example, the code generator does not expect to see a |
| 801 | // HTypeConversion from a type to the same type. |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 802 | simplify4, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 803 | }; |
| 804 | RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 805 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 806 | RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, pass_observer); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 809 | static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { |
| 810 | ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); |
| 811 | codegen->EmitLinkerPatches(&linker_patches); |
| 812 | |
| 813 | // Sort patches by literal offset. Required for .oat_patches encoding. |
| 814 | std::sort(linker_patches.begin(), linker_patches.end(), |
| 815 | [](const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 816 | return lhs.LiteralOffset() < rhs.LiteralOffset(); |
| 817 | }); |
| 818 | |
| 819 | return linker_patches; |
| 820 | } |
| 821 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 822 | CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena, |
| 823 | CodeVectorAllocator* code_allocator, |
| 824 | CodeGenerator* codegen, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 825 | CompilerDriver* compiler_driver, |
| 826 | const DexFile::CodeItem* code_item) const { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 827 | ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 828 | ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 829 | stack_map.resize(codegen->ComputeStackMapsSize()); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 830 | codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 831 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 832 | CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 833 | compiler_driver, |
| 834 | codegen->GetInstructionSet(), |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 835 | ArrayRef<const uint8_t>(code_allocator->GetMemory()), |
Roland Levillain | aa9b7c4 | 2015-02-17 15:40:09 +0000 | [diff] [blame] | 836 | // Follow Quick's behavior and set the frame size to zero if it is |
| 837 | // considered "empty" (see the definition of |
| 838 | // art::CodeGenerator::HasEmptyFrame). |
| 839 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 840 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 841 | codegen->GetFpuSpillMask(), |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 842 | ArrayRef<const SrcMapElem>(), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 843 | ArrayRef<const uint8_t>(stack_map), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 844 | ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 845 | ArrayRef<const LinkerPatch>(linker_patches)); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 846 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 847 | return compiled_method; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 850 | CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, |
| 851 | CodeVectorAllocator* code_allocator, |
| 852 | const DexFile::CodeItem* code_item, |
| 853 | uint32_t access_flags, |
| 854 | InvokeType invoke_type, |
| 855 | uint16_t class_def_idx, |
| 856 | uint32_t method_idx, |
| 857 | jobject class_loader, |
| 858 | const DexFile& dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 859 | Handle<mirror::DexCache> dex_cache, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 860 | ArtMethod* method, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 861 | bool osr) const { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 862 | MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 863 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 864 | InstructionSet instruction_set = compiler_driver->GetInstructionSet(); |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 865 | |
Roland Levillain | 3b359c7 | 2015-11-17 19:35:12 +0000 | [diff] [blame] | 866 | // Always use the Thumb-2 assembler: some runtime functionality |
| 867 | // (like implicit stack overflow checks) assume Thumb-2. |
Artem Serov | ba6b679 | 2016-08-15 14:22:07 +0100 | [diff] [blame] | 868 | DCHECK_NE(instruction_set, kArm); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 869 | |
| 870 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 871 | if (!IsInstructionSetSupported(instruction_set)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 872 | MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 873 | return nullptr; |
| 874 | } |
| 875 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 876 | // When read barriers are enabled, do not attempt to compile for |
| 877 | // instruction sets that have no read barrier support. |
| 878 | if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) { |
| 879 | return nullptr; |
| 880 | } |
| 881 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 882 | if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 883 | MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 884 | return nullptr; |
| 885 | } |
| 886 | |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 887 | // Implementation of the space filter: do not compile a code item whose size in |
Nicolas Geoffray | 432bf3d | 2015-07-17 11:11:09 +0100 | [diff] [blame] | 888 | // code units is bigger than 128. |
| 889 | static constexpr size_t kSpaceFilterOptimizingThreshold = 128; |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 890 | const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 891 | if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace) |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 892 | && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 893 | MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 894 | return nullptr; |
| 895 | } |
| 896 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 897 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 898 | DexCompilationUnit dex_compilation_unit( |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 899 | class_loader, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 900 | class_linker, |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 901 | dex_file, |
| 902 | code_item, |
| 903 | class_def_idx, |
| 904 | method_idx, |
| 905 | access_flags, |
| 906 | /* verified_method */ nullptr, |
| 907 | dex_cache); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 908 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 909 | bool requires_barrier = dex_compilation_unit.IsConstructor() |
| 910 | && compiler_driver->RequiresConstructorBarrier(Thread::Current(), |
| 911 | dex_compilation_unit.GetDexFile(), |
| 912 | dex_compilation_unit.GetClassDefIndex()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 913 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 914 | HGraph* graph = new (arena) HGraph( |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 915 | arena, |
| 916 | dex_file, |
| 917 | method_idx, |
| 918 | requires_barrier, |
| 919 | compiler_driver->GetInstructionSet(), |
| 920 | kInvalidInvokeType, |
| 921 | compiler_driver->GetCompilerOptions().GetDebuggable(), |
| 922 | osr); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 923 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 924 | const uint8_t* interpreter_metadata = nullptr; |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 925 | if (method == nullptr) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 926 | ScopedObjectAccess soa(Thread::Current()); |
| 927 | StackHandleScope<1> hs(soa.Self()); |
| 928 | Handle<mirror::ClassLoader> loader(hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 929 | soa.Decode<mirror::ClassLoader>(class_loader))); |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 930 | method = compiler_driver->ResolveMethod( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 931 | soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 932 | } |
| 933 | // For AOT compilation, we may not get a method, for example if its class is erroneous. |
| 934 | // JIT should always have a method. |
| 935 | DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr); |
| 936 | if (method != nullptr) { |
| 937 | graph->SetArtMethod(method); |
| 938 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 939 | interpreter_metadata = method->GetQuickenedInfo(class_linker->GetImagePointerSize()); |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 940 | uint16_t type_index = method->GetDeclaringClass()->GetDexTypeIndex(); |
| 941 | |
| 942 | // Update the dex cache if the type is not in it yet. Note that under AOT, |
| 943 | // the verifier must have set it, but under JIT, there's no guarantee, as we |
| 944 | // don't necessarily run the verifier. |
| 945 | // The compiler and the compiler driver assume the compiling class is |
| 946 | // in the dex cache. |
| 947 | if (dex_cache->GetResolvedType(type_index) == nullptr) { |
| 948 | dex_cache->SetResolvedType(type_index, method->GetDeclaringClass()); |
Nicolas Geoffray | 12abcbd | 2016-06-06 15:51:58 +0000 | [diff] [blame] | 949 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 952 | std::unique_ptr<CodeGenerator> codegen( |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 953 | CodeGenerator::Create(graph, |
| 954 | instruction_set, |
| 955 | *compiler_driver->GetInstructionSetFeatures(), |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 956 | compiler_driver->GetCompilerOptions(), |
| 957 | compilation_stats_.get())); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 958 | if (codegen.get() == nullptr) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 959 | MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 960 | return nullptr; |
| 961 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 962 | codegen->GetAssembler()->cfi().SetEnabled( |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 963 | compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 964 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 965 | PassObserver pass_observer(graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 966 | codegen.get(), |
| 967 | visualizer_output_.get(), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame^] | 968 | compiler_driver, |
| 969 | dump_mutex_); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 970 | |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 971 | VLOG(compiler) << "Building " << pass_observer.GetMethodName(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 972 | |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 973 | { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 974 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 975 | VariableSizedHandleScope handles(soa.Self()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 976 | // Do not hold `mutator_lock_` between optimizations. |
| 977 | ScopedThreadSuspension sts(soa.Self(), kNative); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 978 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 979 | { |
| 980 | PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); |
| 981 | HGraphBuilder builder(graph, |
| 982 | &dex_compilation_unit, |
| 983 | &dex_compilation_unit, |
| 984 | &dex_file, |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 985 | *code_item, |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 986 | compiler_driver, |
| 987 | compilation_stats_.get(), |
| 988 | interpreter_metadata, |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 989 | dex_cache, |
| 990 | &handles); |
| 991 | GraphAnalysisResult result = builder.BuildGraph(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 992 | if (result != kAnalysisSuccess) { |
| 993 | switch (result) { |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 994 | case kAnalysisSkipped: |
| 995 | MaybeRecordStat(MethodCompilationStat::kNotCompiledSkipped); |
| 996 | break; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 997 | case kAnalysisInvalidBytecode: |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 998 | MaybeRecordStat(MethodCompilationStat::kNotCompiledInvalidBytecode); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case kAnalysisFailThrowCatchLoop: |
| 1001 | MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop); |
| 1002 | break; |
| 1003 | case kAnalysisFailAmbiguousArrayOp: |
| 1004 | MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp); |
| 1005 | break; |
| 1006 | case kAnalysisSuccess: |
| 1007 | UNREACHABLE(); |
| 1008 | } |
| 1009 | pass_observer.SetGraphInBadState(); |
| 1010 | return nullptr; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 1011 | } |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 1012 | } |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 1013 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1014 | RunOptimizations(graph, |
| 1015 | codegen.get(), |
| 1016 | compiler_driver, |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1017 | dex_compilation_unit, |
| 1018 | &pass_observer, |
| 1019 | &handles); |
| 1020 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 1021 | RegisterAllocator::Strategy regalloc_strategy = |
| 1022 | compiler_options.GetRegisterAllocationStrategy(); |
| 1023 | AllocateRegisters(graph, codegen.get(), &pass_observer, regalloc_strategy); |
| 1024 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1025 | codegen->Compile(code_allocator); |
| 1026 | pass_observer.DumpDisassembly(); |
| 1027 | } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 1028 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1029 | return codegen.release(); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 1032 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
| 1033 | uint32_t access_flags, |
| 1034 | InvokeType invoke_type, |
| 1035 | uint16_t class_def_idx, |
| 1036 | uint32_t method_idx, |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1037 | jobject jclass_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 1038 | const DexFile& dex_file, |
| 1039 | Handle<mirror::DexCache> dex_cache) const { |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1040 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 1041 | CompiledMethod* method = nullptr; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1042 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 1043 | const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); |
| 1044 | DCHECK(!verified_method->HasRuntimeThrow()); |
| 1045 | if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 1046 | || verifier::MethodVerifier::CanCompilerHandleVerificationFailure( |
| 1047 | verified_method->GetEncounteredVerificationFailures())) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1048 | ArenaAllocator arena(Runtime::Current()->GetArenaPool()); |
| 1049 | CodeVectorAllocator code_allocator(&arena); |
| 1050 | std::unique_ptr<CodeGenerator> codegen( |
| 1051 | TryCompile(&arena, |
| 1052 | &code_allocator, |
| 1053 | code_item, |
| 1054 | access_flags, |
| 1055 | invoke_type, |
| 1056 | class_def_idx, |
| 1057 | method_idx, |
| 1058 | jclass_loader, |
| 1059 | dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1060 | dex_cache, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 1061 | nullptr, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1062 | /* osr */ false)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1063 | if (codegen.get() != nullptr) { |
Calin Juravle | ad54338 | 2015-11-19 17:26:29 +0000 | [diff] [blame] | 1064 | MaybeRecordStat(MethodCompilationStat::kCompiled); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1065 | method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item); |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (kArenaAllocatorCountAllocations) { |
| 1068 | if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) { |
| 1069 | MemStats mem_stats(arena.GetMemStats()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1070 | LOG(INFO) << dex_file.PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats); |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1073 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 1074 | } else { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1075 | if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { |
| 1076 | MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); |
| 1077 | } else { |
Calin Juravle | ad54338 | 2015-11-19 17:26:29 +0000 | [diff] [blame] | 1078 | MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1079 | } |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1080 | } |
| 1081 | |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 1082 | if (kIsDebugBuild && |
| 1083 | IsCompilingWithCoreImage() && |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1084 | IsInstructionSetSupported(compiler_driver->GetInstructionSet()) && |
| 1085 | (!kEmitCompilerReadBarrier || |
| 1086 | InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) { |
| 1087 | // For testing purposes, we put a special marker on method names |
| 1088 | // that should be compiled with this compiler (when the the |
| 1089 | // instruction set is supported -- and has support for read |
| 1090 | // barriers, if they are enabled). This makes sure we're not |
| 1091 | // regressing. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1092 | std::string method_name = dex_file.PrettyMethod(method_idx); |
Calin Juravle | 09b1d6f | 2015-10-07 12:08:54 +0100 | [diff] [blame] | 1093 | bool shouldCompile = method_name.find("$opt$") != std::string::npos; |
| 1094 | DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; |
| 1095 | } |
| 1096 | |
Nicolas Geoffray | 12be74e | 2015-03-30 13:29:08 +0100 | [diff] [blame] | 1097 | return method; |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 1100 | Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { |
| 1101 | return new OptimizingCompiler(driver); |
| 1102 | } |
| 1103 | |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 1104 | bool IsCompilingWithCoreImage() { |
| 1105 | const std::string& image = Runtime::Current()->GetImageLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1106 | // TODO: This is under-approximating... |
| 1107 | if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) { |
| 1108 | return true; |
| 1109 | } |
| 1110 | return false; |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 1111 | } |
| 1112 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1113 | bool OptimizingCompiler::JitCompile(Thread* self, |
| 1114 | jit::JitCodeCache* code_cache, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1115 | ArtMethod* method, |
| 1116 | bool osr) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1117 | StackHandleScope<2> hs(self); |
| 1118 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 1119 | method->GetDeclaringClass()->GetClassLoader())); |
| 1120 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 1121 | DCHECK(method->IsCompilable()); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1122 | |
| 1123 | jobject jclass_loader = class_loader.ToJObject(); |
| 1124 | const DexFile* dex_file = method->GetDexFile(); |
| 1125 | const uint16_t class_def_idx = method->GetClassDefIndex(); |
| 1126 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
| 1127 | const uint32_t method_idx = method->GetDexMethodIndex(); |
| 1128 | const uint32_t access_flags = method->GetAccessFlags(); |
| 1129 | const InvokeType invoke_type = method->GetInvokeType(); |
| 1130 | |
Nicolas Geoffray | 25e0456 | 2016-03-01 13:17:58 +0000 | [diff] [blame] | 1131 | ArenaAllocator arena(Runtime::Current()->GetJitArenaPool()); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1132 | CodeVectorAllocator code_allocator(&arena); |
| 1133 | std::unique_ptr<CodeGenerator> codegen; |
| 1134 | { |
| 1135 | // Go to native so that we don't block GC during compilation. |
| 1136 | ScopedThreadSuspension sts(self, kNative); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1137 | codegen.reset( |
| 1138 | TryCompile(&arena, |
| 1139 | &code_allocator, |
| 1140 | code_item, |
| 1141 | access_flags, |
| 1142 | invoke_type, |
| 1143 | class_def_idx, |
| 1144 | method_idx, |
| 1145 | jclass_loader, |
| 1146 | *dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1147 | dex_cache, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 1148 | method, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1149 | osr)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1150 | if (codegen.get() == nullptr) { |
| 1151 | return false; |
| 1152 | } |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (kArenaAllocatorCountAllocations) { |
| 1155 | if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) { |
| 1156 | MemStats mem_stats(arena.GetMemStats()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1157 | LOG(INFO) << dex_file->PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats); |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | size_t stack_map_size = codegen->ComputeStackMapsSize(); |
Nicolas Geoffray | 933330a | 2016-03-16 14:20:06 +0000 | [diff] [blame] | 1163 | uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size, method); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1164 | if (stack_map_data == nullptr) { |
| 1165 | return false; |
| 1166 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1167 | MaybeRecordStat(MethodCompilationStat::kCompiled); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1168 | codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1169 | const void* code = code_cache->CommitCode( |
| 1170 | self, |
| 1171 | method, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1172 | stack_map_data, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1173 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
| 1174 | codegen->GetCoreSpillMask(), |
| 1175 | codegen->GetFpuSpillMask(), |
| 1176 | code_allocator.GetMemory().data(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1177 | code_allocator.GetSize(), |
| 1178 | osr); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1179 | |
| 1180 | if (code == nullptr) { |
| 1181 | code_cache->ClearData(self, stack_map_data); |
| 1182 | return false; |
| 1183 | } |
| 1184 | |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 1185 | const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions(); |
| 1186 | if (compiler_options.GetGenerateDebugInfo()) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1187 | const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); |
| 1188 | const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); |
David Srbecky | 09c2a6b | 2016-03-11 17:11:44 +0000 | [diff] [blame] | 1189 | debug::MethodDebugInfo info = debug::MethodDebugInfo(); |
| 1190 | info.trampoline_name = nullptr; |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 1191 | info.dex_file = dex_file; |
| 1192 | info.class_def_index = class_def_idx; |
| 1193 | info.dex_method_index = method_idx; |
| 1194 | info.access_flags = access_flags; |
| 1195 | info.code_item = code_item; |
| 1196 | info.isa = codegen->GetInstructionSet(); |
| 1197 | info.deduped = false; |
| 1198 | info.is_native_debuggable = compiler_options.GetNativeDebuggable(); |
| 1199 | info.is_optimized = true; |
| 1200 | info.is_code_address_text_relative = false; |
| 1201 | info.code_address = code_address; |
| 1202 | info.code_size = code_allocator.GetSize(); |
| 1203 | info.frame_size_in_bytes = method_header->GetFrameSizeInBytes(); |
| 1204 | info.code_info = stack_map_size == 0 ? nullptr : stack_map_data; |
| 1205 | info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 1206 | std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods( |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 1207 | GetCompilerDriver()->GetInstructionSet(), |
| 1208 | GetCompilerDriver()->GetInstructionSetFeatures(), |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 1209 | ArrayRef<const debug::MethodDebugInfo>(&info, 1)); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 1210 | CreateJITCodeEntryForAddress(code_address, std::move(elf_file)); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 1213 | Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed()); |
| 1214 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1215 | return true; |
| 1216 | } |
| 1217 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1218 | } // namespace art |