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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method-inl.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 26 | #include "base/arena_allocator.h" |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 27 | #include "base/arena_containers.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 28 | #include "base/dumpable.h" |
Vladimir Marko | c90d7c7 | 2015-10-06 17:30:45 +0000 | [diff] [blame] | 29 | #include "base/macros.h" |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 30 | #include "base/mutex.h" |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 31 | #include "base/scoped_arena_allocator.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 32 | #include "base/timing_logger.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 33 | #include "builder.h" |
| 34 | #include "code_generator.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 35 | #include "compiled_method.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 36 | #include "compiler.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 37 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 38 | #include "debug/method_debug_info.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 39 | #include "dex/verification_results.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 40 | #include "dex/verified_method.h" |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 41 | #include "dex_file_types.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 42 | #include "driver/compiler_driver-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 43 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 44 | #include "driver/dex_compilation_unit.h" |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 45 | #include "graph_checker.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 46 | #include "graph_visualizer.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 47 | #include "inliner.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 48 | #include "jit/debugger_interface.h" |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 49 | #include "jit/jit.h" |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 50 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | 01db5f7 | 2017-07-19 15:05:49 +0100 | [diff] [blame] | 51 | #include "jit/jit_logger.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 52 | #include "jni/quick/jni_compiler.h" |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 53 | #include "linker/linker_patch.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 54 | #include "nodes.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 55 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 56 | #include "prepare_for_register_allocation.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 57 | #include "reference_type_propagation.h" |
Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 58 | #include "register_allocator_linear_scan.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 59 | #include "select_generator.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 60 | #include "ssa_builder.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 61 | #include "ssa_liveness_analysis.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 62 | #include "ssa_phi_elimination.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 63 | #include "utils/assembler.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 64 | #include "verifier/verifier_compiler_binding.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 65 | |
| 66 | namespace art { |
| 67 | |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 68 | static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB; |
| 69 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 70 | static constexpr const char* kPassNameSeparator = "$"; |
| 71 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Used by the code generator, to allocate the code in a vector. |
| 74 | */ |
| 75 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 76 | public: |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 77 | explicit CodeVectorAllocator(ArenaAllocator* allocator) |
| 78 | : memory_(allocator->Adapter(kArenaAllocCodeBuffer)), |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 79 | size_(0) {} |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 80 | |
| 81 | virtual uint8_t* Allocate(size_t size) { |
| 82 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 83 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 84 | return &memory_[0]; |
| 85 | } |
| 86 | |
| 87 | size_t GetSize() const { return size_; } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 88 | const ArenaVector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 89 | uint8_t* GetData() { return memory_.data(); } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 92 | ArenaVector<uint8_t> memory_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 93 | size_t size_; |
| 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 96 | }; |
| 97 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 98 | /** |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 99 | * 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] | 100 | * be dumped. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 101 | */ |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 102 | static constexpr const char kStringFilter[] = ""; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 103 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 104 | class PassScope; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 105 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 106 | class PassObserver : public ValueObject { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 107 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 108 | PassObserver(HGraph* graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 109 | CodeGenerator* codegen, |
| 110 | std::ostream* visualizer_output, |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 111 | CompilerDriver* compiler_driver, |
| 112 | Mutex& dump_mutex) |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 113 | : graph_(graph), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 114 | cached_method_name_(), |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 115 | timing_logger_enabled_(compiler_driver->GetDumpPasses()), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 116 | timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true), |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 117 | disasm_info_(graph->GetAllocator()), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 118 | visualizer_oss_(), |
| 119 | visualizer_output_(visualizer_output), |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 120 | visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 121 | visualizer_(&visualizer_oss_, graph, *codegen), |
| 122 | visualizer_dump_mutex_(dump_mutex), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 123 | graph_in_bad_state_(false) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 124 | if (timing_logger_enabled_ || visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 125 | if (!IsVerboseMethod(compiler_driver, GetMethodName())) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 126 | timing_logger_enabled_ = visualizer_enabled_ = false; |
| 127 | } |
| 128 | if (visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 129 | visualizer_.PrintHeader(GetMethodName()); |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 130 | codegen->SetDisassemblyInformation(&disasm_info_); |
| 131 | } |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 132 | } |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 135 | ~PassObserver() { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 136 | if (timing_logger_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 137 | LOG(INFO) << "TIMINGS " << GetMethodName(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 138 | LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); |
| 139 | } |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 140 | DCHECK(visualizer_oss_.str().empty()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 143 | void DumpDisassembly() REQUIRES(!visualizer_dump_mutex_) { |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 144 | if (visualizer_enabled_) { |
| 145 | visualizer_.DumpGraphWithDisassembly(); |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 146 | FlushVisualizer(); |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 150 | void SetGraphInBadState() { graph_in_bad_state_ = true; } |
| 151 | |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 152 | const char* GetMethodName() { |
| 153 | // PrettyMethod() is expensive, so we delay calling it until we actually have to. |
| 154 | if (cached_method_name_.empty()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 155 | cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx()); |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 156 | } |
| 157 | return cached_method_name_.c_str(); |
| 158 | } |
| 159 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 160 | private: |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 161 | void StartPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) { |
Wojciech Staszkiewicz | e706070 | 2016-08-16 17:31:19 -0700 | [diff] [blame] | 162 | VLOG(compiler) << "Starting pass: " << pass_name; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 163 | // Dump graph first, then start timer. |
| 164 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 165 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 166 | FlushVisualizer(); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 167 | } |
| 168 | if (timing_logger_enabled_) { |
| 169 | timing_logger_.StartTiming(pass_name); |
| 170 | } |
| 171 | } |
| 172 | |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 173 | void FlushVisualizer() REQUIRES(!visualizer_dump_mutex_) { |
| 174 | MutexLock mu(Thread::Current(), visualizer_dump_mutex_); |
| 175 | *visualizer_output_ << visualizer_oss_.str(); |
| 176 | visualizer_output_->flush(); |
| 177 | visualizer_oss_.str(""); |
| 178 | visualizer_oss_.clear(); |
| 179 | } |
| 180 | |
| 181 | void EndPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) { |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 182 | // Pause timer first, then dump graph. |
| 183 | if (timing_logger_enabled_) { |
| 184 | timing_logger_.EndTiming(); |
| 185 | } |
| 186 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 187 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); |
Nicolas Geoffray | 00c141a | 2016-11-10 15:19:15 +0000 | [diff] [blame] | 188 | FlushVisualizer(); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 189 | } |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 190 | |
| 191 | // Validate the HGraph if running in debug mode. |
| 192 | if (kIsDebugBuild) { |
| 193 | if (!graph_in_bad_state_) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 194 | GraphChecker checker(graph_); |
| 195 | checker.Run(); |
| 196 | if (!checker.IsValid()) { |
| 197 | LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 203 | static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { |
| 204 | // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an |
| 205 | // empty kStringFilter matching all methods. |
| 206 | if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { |
| 207 | return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); |
| 208 | } |
| 209 | |
| 210 | // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code |
| 211 | // warning when the string is empty. |
| 212 | constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; |
| 213 | if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 220 | HGraph* const graph_; |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 221 | |
| 222 | std::string cached_method_name_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 223 | |
| 224 | bool timing_logger_enabled_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 225 | TimingLogger timing_logger_; |
| 226 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 227 | DisassemblyInformation disasm_info_; |
| 228 | |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 229 | std::ostringstream visualizer_oss_; |
| 230 | std::ostream* visualizer_output_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 231 | bool visualizer_enabled_; |
| 232 | HGraphVisualizer visualizer_; |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 233 | Mutex& visualizer_dump_mutex_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 234 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 235 | // Flag to be set by the compiler if the pass failed and the graph is not |
| 236 | // expected to validate. |
| 237 | bool graph_in_bad_state_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 238 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 239 | friend PassScope; |
| 240 | |
| 241 | DISALLOW_COPY_AND_ASSIGN(PassObserver); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 244 | class PassScope : public ValueObject { |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 245 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 246 | PassScope(const char *pass_name, PassObserver* pass_observer) |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 247 | : pass_name_(pass_name), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 248 | pass_observer_(pass_observer) { |
| 249 | pass_observer_->StartPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 250 | } |
| 251 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 252 | ~PassScope() { |
| 253 | pass_observer_->EndPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | private: |
| 257 | const char* const pass_name_; |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 258 | PassObserver* const pass_observer_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 261 | class OptimizingCompiler FINAL : public Compiler { |
| 262 | public: |
| 263 | explicit OptimizingCompiler(CompilerDriver* driver); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 264 | ~OptimizingCompiler() OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 265 | |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 266 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 267 | |
| 268 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 269 | uint32_t access_flags, |
| 270 | InvokeType invoke_type, |
| 271 | uint16_t class_def_idx, |
| 272 | uint32_t method_idx, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 273 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 274 | const DexFile& dex_file, |
| 275 | Handle<mirror::DexCache> dex_cache) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 276 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 277 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 278 | uint32_t method_idx, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 279 | const DexFile& dex_file, |
| 280 | Handle<mirror::DexCache> dex_cache) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 281 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 282 | uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 283 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 284 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 285 | InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); |
| 286 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 287 | |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 288 | void Init() OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 289 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 290 | void UnInit() const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 291 | |
Nicolas Geoffray | 01db5f7 | 2017-07-19 15:05:49 +0100 | [diff] [blame] | 292 | bool JitCompile(Thread* self, |
| 293 | jit::JitCodeCache* code_cache, |
| 294 | ArtMethod* method, |
| 295 | bool osr, |
| 296 | jit::JitLogger* jit_logger) |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 297 | OVERRIDE |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 298 | REQUIRES_SHARED(Locks::mutator_lock_); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 299 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 300 | private: |
| 301 | void RunOptimizations(HGraph* graph, |
| 302 | CodeGenerator* codegen, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 303 | const DexCompilationUnit& dex_compilation_unit, |
| 304 | PassObserver* pass_observer, |
| 305 | VariableSizedHandleScope* handles, |
| 306 | const OptimizationDef definitions[], |
| 307 | size_t length) const { |
| 308 | // Convert definitions to optimization passes. |
| 309 | ArenaVector<HOptimization*> optimizations = ConstructOptimizations( |
| 310 | definitions, |
| 311 | length, |
| 312 | graph->GetAllocator(), |
| 313 | graph, |
| 314 | compilation_stats_.get(), |
| 315 | codegen, |
| 316 | GetCompilerDriver(), |
| 317 | dex_compilation_unit, |
| 318 | handles); |
| 319 | DCHECK_EQ(length, optimizations.size()); |
| 320 | // Run the optimization passes one by one. |
| 321 | for (size_t i = 0; i < length; ++i) { |
| 322 | PassScope scope(optimizations[i]->GetPassName(), pass_observer); |
| 323 | optimizations[i]->Run(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | template <size_t length> void RunOptimizations( |
| 328 | HGraph* graph, |
| 329 | CodeGenerator* codegen, |
| 330 | const DexCompilationUnit& dex_compilation_unit, |
| 331 | PassObserver* pass_observer, |
| 332 | VariableSizedHandleScope* handles, |
| 333 | const OptimizationDef (&definitions)[length]) const { |
| 334 | RunOptimizations( |
| 335 | graph, codegen, dex_compilation_unit, pass_observer, handles, definitions, length); |
| 336 | } |
| 337 | |
| 338 | void RunOptimizations(HGraph* graph, |
| 339 | CodeGenerator* codegen, |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 340 | const DexCompilationUnit& dex_compilation_unit, |
| 341 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 342 | VariableSizedHandleScope* handles) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 343 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 344 | private: |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 345 | // Create a 'CompiledMethod' for an optimized graph. |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 346 | CompiledMethod* Emit(ArenaAllocator* allocator, |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 347 | CodeVectorAllocator* code_allocator, |
| 348 | CodeGenerator* codegen, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 349 | const DexFile::CodeItem* item) const; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 350 | |
| 351 | // Try compiling a method and return the code generator used for |
| 352 | // compiling it. |
| 353 | // This method: |
| 354 | // 1) Builds the graph. Returns null if it failed to build it. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 355 | // 2) Transforms the graph to SSA. Returns null if it failed. |
| 356 | // 3) Runs optimizations on the graph, including register allocator. |
| 357 | // 4) Generates code with the `code_allocator` provided. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 358 | CodeGenerator* TryCompile(ArenaAllocator* allocator, |
| 359 | ArenaStack* arena_stack, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 360 | CodeVectorAllocator* code_allocator, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 361 | const DexCompilationUnit& dex_compilation_unit, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 362 | ArtMethod* method, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 363 | bool osr, |
| 364 | VariableSizedHandleScope* handles) const; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 365 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 366 | CodeGenerator* TryCompileIntrinsic(ArenaAllocator* allocator, |
| 367 | ArenaStack* arena_stack, |
| 368 | CodeVectorAllocator* code_allocator, |
| 369 | const DexCompilationUnit& dex_compilation_unit, |
| 370 | ArtMethod* method, |
| 371 | VariableSizedHandleScope* handles) const; |
| 372 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 373 | void MaybeRunInliner(HGraph* graph, |
| 374 | CodeGenerator* codegen, |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 375 | const DexCompilationUnit& dex_compilation_unit, |
| 376 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 377 | VariableSizedHandleScope* handles) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 378 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 379 | void RunArchOptimizations(HGraph* graph, |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 380 | CodeGenerator* codegen, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 381 | const DexCompilationUnit& dex_compilation_unit, |
| 382 | PassObserver* pass_observer, |
| 383 | VariableSizedHandleScope* handles) const; |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 384 | |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 385 | std::unique_ptr<OptimizingCompilerStats> compilation_stats_; |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 386 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 387 | std::unique_ptr<std::ostream> visualizer_output_; |
| 388 | |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 389 | mutable Mutex dump_mutex_; // To synchronize visualizer writing. |
| 390 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 391 | DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); |
| 392 | }; |
| 393 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 394 | static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ |
| 395 | |
| 396 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 397 | : Compiler(driver, kMaximumCompilationTimeBeforeWarning), |
| 398 | dump_mutex_("Visualizer dump lock") {} |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 399 | |
| 400 | void OptimizingCompiler::Init() { |
| 401 | // Enable C1visualizer output. Must be done in Init() because the compiler |
| 402 | // driver is not fully initialized when passed to the compiler's constructor. |
| 403 | CompilerDriver* driver = GetCompilerDriver(); |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 404 | const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName(); |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 405 | if (!cfg_file_name.empty()) { |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 406 | std::ios_base::openmode cfg_file_mode = |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 407 | driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 408 | visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 409 | } |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 410 | if (driver->GetDumpStats()) { |
| 411 | compilation_stats_.reset(new OptimizingCompilerStats()); |
| 412 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 413 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 414 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 415 | void OptimizingCompiler::UnInit() const { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 418 | OptimizingCompiler::~OptimizingCompiler() { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 419 | if (compilation_stats_.get() != nullptr) { |
| 420 | compilation_stats_->Log(); |
| 421 | } |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 422 | } |
| 423 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 424 | bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, |
Vladimir Marko | df73984 | 2016-03-23 16:59:07 +0000 | [diff] [blame] | 425 | const DexFile& dex_file ATTRIBUTE_UNUSED) const { |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 426 | return true; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 429 | static bool IsInstructionSetSupported(InstructionSet instruction_set) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 430 | return instruction_set == InstructionSet::kArm |
| 431 | || instruction_set == InstructionSet::kArm64 |
| 432 | || instruction_set == InstructionSet::kThumb2 |
| 433 | || instruction_set == InstructionSet::kMips |
| 434 | || instruction_set == InstructionSet::kMips64 |
| 435 | || instruction_set == InstructionSet::kX86 |
| 436 | || instruction_set == InstructionSet::kX86_64; |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 439 | void OptimizingCompiler::MaybeRunInliner(HGraph* graph, |
| 440 | CodeGenerator* codegen, |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 441 | const DexCompilationUnit& dex_compilation_unit, |
| 442 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 443 | VariableSizedHandleScope* handles) const { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 444 | const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions(); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 445 | bool should_inline = (compiler_options.GetInlineMaxCodeUnits() > 0); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 446 | if (!should_inline) { |
| 447 | return; |
| 448 | } |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 449 | OptimizationDef optimizations[] = { |
| 450 | OptDef(OptimizationPass::kInliner) |
| 451 | }; |
| 452 | RunOptimizations(graph, |
| 453 | codegen, |
| 454 | dex_compilation_unit, |
| 455 | pass_observer, |
| 456 | handles, |
| 457 | optimizations); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 460 | void OptimizingCompiler::RunArchOptimizations(HGraph* graph, |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 461 | CodeGenerator* codegen, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 462 | const DexCompilationUnit& dex_compilation_unit, |
| 463 | PassObserver* pass_observer, |
| 464 | VariableSizedHandleScope* handles) const { |
| 465 | switch (GetCompilerDriver()->GetInstructionSet()) { |
Artem Serov | 2bbc953 | 2016-10-21 11:51:50 +0100 | [diff] [blame] | 466 | #if defined(ART_ENABLE_CODEGEN_arm) |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 467 | case InstructionSet::kThumb2: |
| 468 | case InstructionSet::kArm: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 469 | OptimizationDef arm_optimizations[] = { |
| 470 | OptDef(OptimizationPass::kInstructionSimplifierArm), |
| 471 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 472 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"), |
| 473 | OptDef(OptimizationPass::kScheduling) |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 474 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 475 | RunOptimizations(graph, |
| 476 | codegen, |
| 477 | dex_compilation_unit, |
| 478 | pass_observer, |
| 479 | handles, |
| 480 | arm_optimizations); |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 484 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 485 | case InstructionSet::kArm64: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 486 | OptimizationDef arm64_optimizations[] = { |
| 487 | OptDef(OptimizationPass::kInstructionSimplifierArm64), |
| 488 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 489 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"), |
| 490 | OptDef(OptimizationPass::kScheduling) |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 491 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 492 | RunOptimizations(graph, |
| 493 | codegen, |
| 494 | dex_compilation_unit, |
| 495 | pass_observer, |
| 496 | handles, |
| 497 | arm64_optimizations); |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | #endif |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 501 | #ifdef ART_ENABLE_CODEGEN_mips |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 502 | case InstructionSet::kMips: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 503 | OptimizationDef mips_optimizations[] = { |
| 504 | OptDef(OptimizationPass::kInstructionSimplifierMips), |
| 505 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 506 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"), |
| 507 | OptDef(OptimizationPass::kPcRelativeFixupsMips) |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 508 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 509 | RunOptimizations(graph, |
| 510 | codegen, |
| 511 | dex_compilation_unit, |
| 512 | pass_observer, |
| 513 | handles, |
| 514 | mips_optimizations); |
Alexey Frunze | e3fb245 | 2016-05-10 16:08:05 -0700 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | #endif |
Lena Djokic | b8e9c35 | 2017-09-20 15:11:13 +0200 | [diff] [blame] | 518 | #ifdef ART_ENABLE_CODEGEN_mips64 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 519 | case InstructionSet::kMips64: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 520 | OptimizationDef mips64_optimizations[] = { |
| 521 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 522 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch") |
Lena Djokic | b8e9c35 | 2017-09-20 15:11:13 +0200 | [diff] [blame] | 523 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 524 | RunOptimizations(graph, |
| 525 | codegen, |
| 526 | dex_compilation_unit, |
| 527 | pass_observer, |
| 528 | handles, |
| 529 | mips64_optimizations); |
Lena Djokic | b8e9c35 | 2017-09-20 15:11:13 +0200 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | #endif |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 533 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 534 | case InstructionSet::kX86: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 535 | OptimizationDef x86_optimizations[] = { |
| 536 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 537 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"), |
| 538 | OptDef(OptimizationPass::kPcRelativeFixupsX86), |
| 539 | OptDef(OptimizationPass::kX86MemoryOperandGeneration) |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 540 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 541 | RunOptimizations(graph, |
| 542 | codegen, |
| 543 | dex_compilation_unit, |
| 544 | pass_observer, |
| 545 | handles, |
| 546 | x86_optimizations); |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | #endif |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 550 | #ifdef ART_ENABLE_CODEGEN_x86_64 |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 551 | case InstructionSet::kX86_64: { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 552 | OptimizationDef x86_64_optimizations[] = { |
| 553 | OptDef(OptimizationPass::kSideEffectsAnalysis), |
| 554 | OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"), |
| 555 | OptDef(OptimizationPass::kX86MemoryOperandGeneration) |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 556 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 557 | RunOptimizations(graph, |
| 558 | codegen, |
| 559 | dex_compilation_unit, |
| 560 | pass_observer, |
| 561 | handles, |
| 562 | x86_64_optimizations); |
Mark Mendell | ee8d971 | 2016-07-12 11:13:15 -0400 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 566 | default: |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 571 | NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. |
| 572 | static void AllocateRegisters(HGraph* graph, |
| 573 | CodeGenerator* codegen, |
Matthew Gharrity | 2cd05b7 | 2016-08-03 16:57:37 -0700 | [diff] [blame] | 574 | PassObserver* pass_observer, |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 575 | RegisterAllocator::Strategy strategy, |
| 576 | OptimizingCompilerStats* stats) { |
Mingyao Yang | 700347e | 2016-03-02 14:59:32 -0800 | [diff] [blame] | 577 | { |
| 578 | PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName, |
| 579 | pass_observer); |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 580 | PrepareForRegisterAllocation(graph, stats).Run(); |
Mingyao Yang | 700347e | 2016-03-02 14:59:32 -0800 | [diff] [blame] | 581 | } |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 582 | // Use local allocator shared by SSA liveness analysis and register allocator. |
| 583 | // (Register allocator creates new objects in the liveness data.) |
| 584 | ScopedArenaAllocator local_allocator(graph->GetArenaStack()); |
| 585 | SsaLivenessAnalysis liveness(graph, codegen, &local_allocator); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 586 | { |
| 587 | PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); |
| 588 | liveness.Analyze(); |
| 589 | } |
| 590 | { |
| 591 | PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 592 | std::unique_ptr<RegisterAllocator> register_allocator = |
| 593 | RegisterAllocator::Create(&local_allocator, codegen, liveness, strategy); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 594 | register_allocator->AllocateRegisters(); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 598 | // Strip pass name suffix to get optimization name. |
| 599 | static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) { |
| 600 | size_t pos = pass_name.find(kPassNameSeparator); |
| 601 | return pos == std::string::npos ? pass_name : pass_name.substr(0, pos); |
| 602 | } |
| 603 | |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 604 | void OptimizingCompiler::RunOptimizations(HGraph* graph, |
| 605 | CodeGenerator* codegen, |
Wojciech Staszkiewicz | f5fb090 | 2016-07-22 13:33:11 -0700 | [diff] [blame] | 606 | const DexCompilationUnit& dex_compilation_unit, |
| 607 | PassObserver* pass_observer, |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 608 | VariableSizedHandleScope* handles) const { |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 609 | const std::vector<std::string>* pass_names = |
| 610 | GetCompilerDriver()->GetCompilerOptions().GetPassesToRun(); |
| 611 | if (pass_names != nullptr) { |
| 612 | // If passes were defined on command-line, build the optimization |
| 613 | // passes and run these instead of the built-in optimizations. |
| 614 | const size_t length = pass_names->size(); |
| 615 | std::vector<OptimizationDef> optimizations; |
| 616 | for (const std::string& pass_name : *pass_names) { |
| 617 | std::string opt_name = ConvertPassNameToOptimizationName(pass_name); |
| 618 | optimizations.push_back(OptDef(OptimizationPassByName(opt_name.c_str()), pass_name.c_str())); |
| 619 | } |
| 620 | RunOptimizations(graph, |
| 621 | codegen, |
| 622 | dex_compilation_unit, |
| 623 | pass_observer, |
| 624 | handles, |
| 625 | optimizations.data(), |
| 626 | length); |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 627 | return; |
| 628 | } |
| 629 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 630 | OptimizationDef optimizations1[] = { |
| 631 | OptDef(OptimizationPass::kIntrinsicsRecognizer), |
| 632 | OptDef(OptimizationPass::kSharpening), |
| 633 | OptDef(OptimizationPass::kConstantFolding), |
| 634 | OptDef(OptimizationPass::kInstructionSimplifier), |
| 635 | OptDef(OptimizationPass::kDeadCodeElimination, "dead_code_elimination$initial") |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 636 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 637 | RunOptimizations(graph, |
| 638 | codegen, |
| 639 | dex_compilation_unit, |
| 640 | pass_observer, |
| 641 | handles, |
| 642 | optimizations1); |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 643 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 644 | MaybeRunInliner(graph, codegen, dex_compilation_unit, pass_observer, handles); |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 645 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 646 | OptimizationDef optimizations2[] = { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 647 | // SelectGenerator depends on the InstructionSimplifier removing |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 648 | // redundant suspend checks to recognize empty blocks. |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 649 | OptDef(OptimizationPass::kSelectGenerator), |
| 650 | // TODO: if we don't inline we can also skip fold2. |
| 651 | OptDef(OptimizationPass::kConstantFolding, "constant_folding$after_inlining"), |
| 652 | OptDef(OptimizationPass::kInstructionSimplifier, "instruction_simplifier$after_inlining"), |
| 653 | OptDef(OptimizationPass::kDeadCodeElimination, "dead_code_elimination$after_inlining"), |
| 654 | OptDef(OptimizationPass::kSideEffectsAnalysis, "side_effects$before_gvn"), |
| 655 | OptDef(OptimizationPass::kGlobalValueNumbering), |
| 656 | OptDef(OptimizationPass::kInvariantCodeMotion), |
| 657 | OptDef(OptimizationPass::kInductionVarAnalysis), |
| 658 | OptDef(OptimizationPass::kBoundsCheckElimination), |
| 659 | OptDef(OptimizationPass::kLoopOptimization), |
| 660 | // Evaluates code generated by dynamic bce. |
| 661 | OptDef(OptimizationPass::kConstantFolding, "constant_folding$after_bce"), |
| 662 | OptDef(OptimizationPass::kInstructionSimplifier, "instruction_simplifier$after_bce"), |
| 663 | OptDef(OptimizationPass::kSideEffectsAnalysis, "side_effects$before_lse"), |
| 664 | OptDef(OptimizationPass::kLoadStoreAnalysis), |
| 665 | OptDef(OptimizationPass::kLoadStoreElimination), |
| 666 | OptDef(OptimizationPass::kCHAGuardOptimization), |
| 667 | OptDef(OptimizationPass::kDeadCodeElimination, "dead_code_elimination$final"), |
| 668 | OptDef(OptimizationPass::kCodeSinking), |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 669 | // The codegen has a few assumptions that only the instruction simplifier |
| 670 | // can satisfy. For example, the code generator does not expect to see a |
| 671 | // HTypeConversion from a type to the same type. |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 672 | OptDef(OptimizationPass::kInstructionSimplifier, "instruction_simplifier$before_codegen"), |
| 673 | // Eliminate constructor fences after code sinking to avoid |
| 674 | // complicated sinking logic to split a fence with many inputs. |
| 675 | OptDef(OptimizationPass::kConstructorFenceRedundancyElimination) |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 676 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 677 | RunOptimizations(graph, |
| 678 | codegen, |
| 679 | dex_compilation_unit, |
| 680 | pass_observer, |
| 681 | handles, |
| 682 | optimizations2); |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 683 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 684 | RunArchOptimizations(graph, codegen, dex_compilation_unit, pass_observer, handles); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 687 | static ArenaVector<linker::LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 688 | ArenaVector<linker::LinkerPatch> linker_patches(codegen->GetGraph()->GetAllocator()->Adapter()); |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 689 | codegen->EmitLinkerPatches(&linker_patches); |
| 690 | |
| 691 | // Sort patches by literal offset. Required for .oat_patches encoding. |
| 692 | std::sort(linker_patches.begin(), linker_patches.end(), |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 693 | [](const linker::LinkerPatch& lhs, const linker::LinkerPatch& rhs) { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 694 | return lhs.LiteralOffset() < rhs.LiteralOffset(); |
| 695 | }); |
| 696 | |
| 697 | return linker_patches; |
| 698 | } |
| 699 | |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 700 | CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* allocator, |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 701 | CodeVectorAllocator* code_allocator, |
| 702 | CodeGenerator* codegen, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 703 | const DexFile::CodeItem* code_item_for_osr_check) const { |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 704 | ArenaVector<linker::LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); |
Vladimir Marko | e764d2e | 2017-10-05 14:35:55 +0100 | [diff] [blame] | 705 | ArenaVector<uint8_t> stack_map(allocator->Adapter(kArenaAllocStackMaps)); |
| 706 | ArenaVector<uint8_t> method_info(allocator->Adapter(kArenaAllocStackMaps)); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 707 | size_t stack_map_size = 0; |
| 708 | size_t method_info_size = 0; |
| 709 | codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size); |
| 710 | stack_map.resize(stack_map_size); |
| 711 | method_info.resize(method_info_size); |
| 712 | codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), |
| 713 | MemoryRegion(method_info.data(), method_info.size()), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 714 | code_item_for_osr_check); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 715 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 716 | CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 717 | GetCompilerDriver(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 718 | codegen->GetInstructionSet(), |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 719 | ArrayRef<const uint8_t>(code_allocator->GetMemory()), |
Roland Levillain | aa9b7c4 | 2015-02-17 15:40:09 +0000 | [diff] [blame] | 720 | // Follow Quick's behavior and set the frame size to zero if it is |
| 721 | // considered "empty" (see the definition of |
| 722 | // art::CodeGenerator::HasEmptyFrame). |
| 723 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 724 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 725 | codegen->GetFpuSpillMask(), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 726 | ArrayRef<const uint8_t>(method_info), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 727 | ArrayRef<const uint8_t>(stack_map), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 728 | ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 729 | ArrayRef<const linker::LinkerPatch>(linker_patches)); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 730 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 731 | return compiled_method; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 734 | CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* allocator, |
| 735 | ArenaStack* arena_stack, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 736 | CodeVectorAllocator* code_allocator, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 737 | const DexCompilationUnit& dex_compilation_unit, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 738 | ArtMethod* method, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 739 | bool osr, |
| 740 | VariableSizedHandleScope* handles) const { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 741 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kAttemptBytecodeCompilation); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 742 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 743 | InstructionSet instruction_set = compiler_driver->GetInstructionSet(); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 744 | const DexFile& dex_file = *dex_compilation_unit.GetDexFile(); |
| 745 | uint32_t method_idx = dex_compilation_unit.GetDexMethodIndex(); |
| 746 | const DexFile::CodeItem* code_item = dex_compilation_unit.GetCodeItem(); |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 747 | |
Roland Levillain | 3b359c7 | 2015-11-17 19:35:12 +0000 | [diff] [blame] | 748 | // Always use the Thumb-2 assembler: some runtime functionality |
| 749 | // (like implicit stack overflow checks) assume Thumb-2. |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 750 | DCHECK_NE(instruction_set, InstructionSet::kArm); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 751 | |
| 752 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 753 | if (!IsInstructionSetSupported(instruction_set)) { |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 754 | MaybeRecordStat(compilation_stats_.get(), |
| 755 | MethodCompilationStat::kNotCompiledUnsupportedIsa); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 756 | return nullptr; |
| 757 | } |
| 758 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 759 | if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 760 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledPathological); |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 761 | return nullptr; |
| 762 | } |
| 763 | |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 764 | // 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] | 765 | // code units is bigger than 128. |
| 766 | static constexpr size_t kSpaceFilterOptimizingThreshold = 128; |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 767 | const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); |
Andreas Gampe | 29d38e7 | 2016-03-23 15:31:51 +0000 | [diff] [blame] | 768 | if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace) |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 769 | && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 770 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledSpaceFilter); |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 771 | return nullptr; |
| 772 | } |
| 773 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 774 | HGraph* graph = new (allocator) HGraph( |
| 775 | allocator, |
| 776 | arena_stack, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 777 | dex_file, |
| 778 | method_idx, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 779 | compiler_driver->GetInstructionSet(), |
| 780 | kInvalidInvokeType, |
| 781 | compiler_driver->GetCompilerOptions().GetDebuggable(), |
| 782 | osr); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 783 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 784 | const uint8_t* interpreter_metadata = nullptr; |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 785 | // For AOT compilation, we may not get a method, for example if its class is erroneous. |
| 786 | // JIT should always have a method. |
| 787 | DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr); |
| 788 | if (method != nullptr) { |
| 789 | graph->SetArtMethod(method); |
| 790 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 8eaa8e5 | 2017-11-13 17:47:50 +0000 | [diff] [blame] | 791 | interpreter_metadata = method->GetQuickenedInfo(); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 794 | std::unique_ptr<CodeGenerator> codegen( |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 795 | CodeGenerator::Create(graph, |
| 796 | instruction_set, |
| 797 | *compiler_driver->GetInstructionSetFeatures(), |
Calin Juravle | 2ae4818 | 2016-03-16 14:05:09 +0000 | [diff] [blame] | 798 | compiler_driver->GetCompilerOptions(), |
| 799 | compilation_stats_.get())); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 800 | if (codegen.get() == nullptr) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 801 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledNoCodegen); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 802 | return nullptr; |
| 803 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 804 | codegen->GetAssembler()->cfi().SetEnabled( |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 805 | compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 806 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 807 | PassObserver pass_observer(graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 808 | codegen.get(), |
| 809 | visualizer_output_.get(), |
Andreas Gampe | a0d81af | 2016-10-27 12:04:57 -0700 | [diff] [blame] | 810 | compiler_driver, |
| 811 | dump_mutex_); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 812 | |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 813 | { |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 814 | VLOG(compiler) << "Building " << pass_observer.GetMethodName(); |
| 815 | PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); |
| 816 | HGraphBuilder builder(graph, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 817 | code_item, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 818 | &dex_compilation_unit, |
| 819 | &dex_compilation_unit, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 820 | compiler_driver, |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 821 | codegen.get(), |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 822 | compilation_stats_.get(), |
| 823 | interpreter_metadata, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 824 | handles); |
| 825 | GraphAnalysisResult result = builder.BuildGraph(); |
| 826 | if (result != kAnalysisSuccess) { |
| 827 | switch (result) { |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 828 | case kAnalysisSkipped: { |
| 829 | MaybeRecordStat(compilation_stats_.get(), |
| 830 | MethodCompilationStat::kNotCompiledSkipped); |
| 831 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 832 | break; |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 833 | case kAnalysisInvalidBytecode: { |
| 834 | MaybeRecordStat(compilation_stats_.get(), |
| 835 | MethodCompilationStat::kNotCompiledInvalidBytecode); |
| 836 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 837 | break; |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 838 | case kAnalysisFailThrowCatchLoop: { |
| 839 | MaybeRecordStat(compilation_stats_.get(), |
| 840 | MethodCompilationStat::kNotCompiledThrowCatchLoop); |
| 841 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 842 | break; |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 843 | case kAnalysisFailAmbiguousArrayOp: { |
| 844 | MaybeRecordStat(compilation_stats_.get(), |
| 845 | MethodCompilationStat::kNotCompiledAmbiguousArrayOp); |
| 846 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 847 | break; |
| 848 | case kAnalysisSuccess: |
| 849 | UNREACHABLE(); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 850 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 851 | pass_observer.SetGraphInBadState(); |
| 852 | return nullptr; |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 853 | } |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 854 | } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 855 | |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 856 | RunOptimizations(graph, |
| 857 | codegen.get(), |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 858 | dex_compilation_unit, |
| 859 | &pass_observer, |
| 860 | handles); |
| 861 | |
| 862 | RegisterAllocator::Strategy regalloc_strategy = |
| 863 | compiler_options.GetRegisterAllocationStrategy(); |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 864 | AllocateRegisters(graph, |
| 865 | codegen.get(), |
| 866 | &pass_observer, |
Igor Murashkin | 6ef4567 | 2017-08-08 13:59:55 -0700 | [diff] [blame] | 867 | regalloc_strategy, |
| 868 | compilation_stats_.get()); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 869 | |
| 870 | codegen->Compile(code_allocator); |
| 871 | pass_observer.DumpDisassembly(); |
| 872 | |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 873 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledBytecode); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 874 | return codegen.release(); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 877 | CodeGenerator* OptimizingCompiler::TryCompileIntrinsic( |
| 878 | ArenaAllocator* allocator, |
| 879 | ArenaStack* arena_stack, |
| 880 | CodeVectorAllocator* code_allocator, |
| 881 | const DexCompilationUnit& dex_compilation_unit, |
| 882 | ArtMethod* method, |
| 883 | VariableSizedHandleScope* handles) const { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 884 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kAttemptIntrinsicCompilation); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 885 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 886 | InstructionSet instruction_set = compiler_driver->GetInstructionSet(); |
| 887 | const DexFile& dex_file = *dex_compilation_unit.GetDexFile(); |
| 888 | uint32_t method_idx = dex_compilation_unit.GetDexMethodIndex(); |
| 889 | |
| 890 | // Always use the Thumb-2 assembler: some runtime functionality |
| 891 | // (like implicit stack overflow checks) assume Thumb-2. |
| 892 | DCHECK_NE(instruction_set, InstructionSet::kArm); |
| 893 | |
| 894 | // Do not attempt to compile on architectures we do not support. |
| 895 | if (!IsInstructionSetSupported(instruction_set)) { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 896 | return nullptr; |
| 897 | } |
| 898 | |
| 899 | HGraph* graph = new (allocator) HGraph( |
| 900 | allocator, |
| 901 | arena_stack, |
| 902 | dex_file, |
| 903 | method_idx, |
| 904 | compiler_driver->GetInstructionSet(), |
| 905 | kInvalidInvokeType, |
| 906 | compiler_driver->GetCompilerOptions().GetDebuggable(), |
| 907 | /* osr */ false); |
| 908 | |
| 909 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 910 | DCHECK(method != nullptr); |
| 911 | graph->SetArtMethod(method); |
| 912 | |
| 913 | std::unique_ptr<CodeGenerator> codegen( |
| 914 | CodeGenerator::Create(graph, |
| 915 | instruction_set, |
| 916 | *compiler_driver->GetInstructionSetFeatures(), |
| 917 | compiler_driver->GetCompilerOptions(), |
| 918 | compilation_stats_.get())); |
| 919 | if (codegen.get() == nullptr) { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 920 | return nullptr; |
| 921 | } |
| 922 | codegen->GetAssembler()->cfi().SetEnabled( |
| 923 | compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); |
| 924 | |
| 925 | PassObserver pass_observer(graph, |
| 926 | codegen.get(), |
| 927 | visualizer_output_.get(), |
| 928 | compiler_driver, |
| 929 | dump_mutex_); |
| 930 | |
| 931 | { |
| 932 | VLOG(compiler) << "Building intrinsic graph " << pass_observer.GetMethodName(); |
| 933 | PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); |
| 934 | HGraphBuilder builder(graph, |
| 935 | /* code_item */ nullptr, |
| 936 | &dex_compilation_unit, |
| 937 | &dex_compilation_unit, |
| 938 | compiler_driver, |
| 939 | codegen.get(), |
| 940 | compilation_stats_.get(), |
| 941 | /* interpreter_metadata */ nullptr, |
| 942 | handles); |
| 943 | builder.BuildIntrinsicGraph(method); |
| 944 | } |
| 945 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 946 | OptimizationDef optimizations[] = { |
| 947 | OptDef(OptimizationPass::kIntrinsicsRecognizer), |
| 948 | // Some intrinsics are converted to HIR by the simplifier and the codegen also |
| 949 | // has a few assumptions that only the instruction simplifier can satisfy. |
| 950 | OptDef(OptimizationPass::kInstructionSimplifier), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 951 | }; |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 952 | RunOptimizations(graph, |
| 953 | codegen.get(), |
| 954 | dex_compilation_unit, |
| 955 | &pass_observer, |
| 956 | handles, |
| 957 | optimizations); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 958 | |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 959 | RunArchOptimizations(graph, codegen.get(), dex_compilation_unit, &pass_observer, handles); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 960 | |
| 961 | AllocateRegisters(graph, |
| 962 | codegen.get(), |
| 963 | &pass_observer, |
| 964 | compiler_driver->GetCompilerOptions().GetRegisterAllocationStrategy(), |
| 965 | compilation_stats_.get()); |
| 966 | if (!codegen->IsLeafMethod()) { |
| 967 | VLOG(compiler) << "Intrinsic method is not leaf: " << method->GetIntrinsic() |
| 968 | << " " << graph->PrettyMethod(); |
| 969 | return nullptr; |
| 970 | } |
| 971 | |
| 972 | codegen->Compile(code_allocator); |
| 973 | pass_observer.DumpDisassembly(); |
| 974 | |
| 975 | VLOG(compiler) << "Compiled intrinsic: " << method->GetIntrinsic() |
| 976 | << " " << graph->PrettyMethod(); |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 977 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledIntrinsic); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 978 | return codegen.release(); |
| 979 | } |
| 980 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 981 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
| 982 | uint32_t access_flags, |
| 983 | InvokeType invoke_type, |
| 984 | uint16_t class_def_idx, |
| 985 | uint32_t method_idx, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 986 | Handle<mirror::ClassLoader> jclass_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 987 | const DexFile& dex_file, |
| 988 | Handle<mirror::DexCache> dex_cache) const { |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 989 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 990 | CompiledMethod* compiled_method = nullptr; |
| 991 | Runtime* runtime = Runtime::Current(); |
| 992 | DCHECK(runtime->IsAotCompiler()); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 993 | const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); |
| 994 | DCHECK(!verified_method->HasRuntimeThrow()); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 995 | if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) || |
| 996 | verifier::CanCompilerHandleVerificationFailure( |
| 997 | verified_method->GetEncounteredVerificationFailures())) { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 998 | ArenaAllocator allocator(runtime->GetArenaPool()); |
| 999 | ArenaStack arena_stack(runtime->GetArenaPool()); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1000 | CodeVectorAllocator code_allocator(&allocator); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1001 | std::unique_ptr<CodeGenerator> codegen; |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1002 | bool compiled_intrinsic = false; |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1003 | { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1004 | DexCompilationUnit dex_compilation_unit( |
| 1005 | jclass_loader, |
| 1006 | runtime->GetClassLinker(), |
| 1007 | dex_file, |
| 1008 | code_item, |
| 1009 | class_def_idx, |
| 1010 | method_idx, |
| 1011 | access_flags, |
| 1012 | /* verified_method */ nullptr, // Not needed by the Optimizing compiler. |
| 1013 | dex_cache); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1014 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1015 | ArtMethod* method = compiler_driver->ResolveMethod( |
| 1016 | soa, dex_cache, jclass_loader, &dex_compilation_unit, method_idx, invoke_type); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1017 | VariableSizedHandleScope handles(soa.Self()); |
| 1018 | // Go to native so that we don't block GC during compilation. |
| 1019 | ScopedThreadSuspension sts(soa.Self(), kNative); |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1020 | if (method != nullptr && UNLIKELY(method->IsIntrinsic())) { |
| 1021 | DCHECK(compiler_driver->GetCompilerOptions().IsBootImage()); |
| 1022 | codegen.reset( |
| 1023 | TryCompileIntrinsic(&allocator, |
| 1024 | &arena_stack, |
| 1025 | &code_allocator, |
| 1026 | dex_compilation_unit, |
| 1027 | method, |
| 1028 | &handles)); |
| 1029 | if (codegen != nullptr) { |
| 1030 | compiled_intrinsic = true; |
| 1031 | } |
| 1032 | } |
| 1033 | if (codegen == nullptr) { |
| 1034 | codegen.reset( |
| 1035 | TryCompile(&allocator, |
| 1036 | &arena_stack, |
| 1037 | &code_allocator, |
| 1038 | dex_compilation_unit, |
| 1039 | method, |
| 1040 | /* osr */ false, |
| 1041 | &handles)); |
| 1042 | } |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1043 | } |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1044 | if (codegen.get() != nullptr) { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1045 | compiled_method = Emit(&allocator, |
| 1046 | &code_allocator, |
| 1047 | codegen.get(), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1048 | compiled_intrinsic ? nullptr : code_item); |
| 1049 | if (compiled_intrinsic) { |
| 1050 | compiled_method->MarkAsIntrinsic(); |
| 1051 | } |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (kArenaAllocatorCountAllocations) { |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 1054 | codegen.reset(); // Release codegen's ScopedArenaAllocator for memory accounting. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1055 | size_t total_allocated = allocator.BytesAllocated() + arena_stack.PeakBytesAllocated(); |
| 1056 | if (total_allocated > kArenaAllocatorMemoryReportThreshold) { |
| 1057 | MemStats mem_stats(allocator.GetMemStats()); |
| 1058 | MemStats peak_stats(arena_stack.GetPeakStats()); |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 1059 | LOG(INFO) << "Used " << total_allocated << " bytes of arena memory for compiling " |
| 1060 | << dex_file.PrettyMethod(method_idx) |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1061 | << "\n" << Dumpable<MemStats>(mem_stats) |
| 1062 | << "\n" << Dumpable<MemStats>(peak_stats); |
Vladimir Marko | 3a40bf2 | 2016-03-22 16:26:33 +0000 | [diff] [blame] | 1063 | } |
| 1064 | } |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1065 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 1066 | } else { |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 1067 | MethodCompilationStat method_stat; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1068 | if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 1069 | method_stat = MethodCompilationStat::kNotCompiledVerifyAtRuntime; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1070 | } else { |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 1071 | method_stat = MethodCompilationStat::kNotCompiledVerificationError; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1072 | } |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 1073 | MaybeRecordStat(compilation_stats_.get(), method_stat); |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 1074 | } |
| 1075 | |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 1076 | if (kIsDebugBuild && |
| 1077 | IsCompilingWithCoreImage() && |
Goran Jakovljevic | 9c4f0d8f | 2017-04-05 16:27:25 +0200 | [diff] [blame] | 1078 | IsInstructionSetSupported(compiler_driver->GetInstructionSet())) { |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1079 | // For testing purposes, we put a special marker on method names |
Goran Jakovljevic | 9c4f0d8f | 2017-04-05 16:27:25 +0200 | [diff] [blame] | 1080 | // that should be compiled with this compiler (when the |
| 1081 | // instruction set is supported). This makes sure we're not |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 1082 | // regressing. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1083 | std::string method_name = dex_file.PrettyMethod(method_idx); |
Calin Juravle | 09b1d6f | 2015-10-07 12:08:54 +0100 | [diff] [blame] | 1084 | bool shouldCompile = method_name.find("$opt$") != std::string::npos; |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1085 | DCHECK((compiled_method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; |
Calin Juravle | 09b1d6f | 2015-10-07 12:08:54 +0100 | [diff] [blame] | 1086 | } |
| 1087 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1088 | return compiled_method; |
| 1089 | } |
| 1090 | |
| 1091 | CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, |
| 1092 | uint32_t method_idx, |
| 1093 | const DexFile& dex_file, |
| 1094 | Handle<mirror::DexCache> dex_cache) const { |
| 1095 | if (GetCompilerDriver()->GetCompilerOptions().IsBootImage()) { |
| 1096 | ScopedObjectAccess soa(Thread::Current()); |
| 1097 | Runtime* runtime = Runtime::Current(); |
| 1098 | ArtMethod* method = runtime->GetClassLinker()->LookupResolvedMethod( |
| 1099 | method_idx, dex_cache.Get(), /* class_loader */ nullptr); |
| 1100 | if (method != nullptr && UNLIKELY(method->IsIntrinsic())) { |
| 1101 | ScopedNullHandle<mirror::ClassLoader> class_loader; // null means boot class path loader. |
| 1102 | DexCompilationUnit dex_compilation_unit( |
| 1103 | class_loader, |
| 1104 | runtime->GetClassLinker(), |
| 1105 | dex_file, |
| 1106 | /* code_item */ nullptr, |
| 1107 | /* class_def_idx */ DexFile::kDexNoIndex16, |
| 1108 | method_idx, |
| 1109 | access_flags, |
| 1110 | /* verified_method */ nullptr, |
| 1111 | dex_cache); |
| 1112 | ArenaAllocator allocator(runtime->GetArenaPool()); |
| 1113 | ArenaStack arena_stack(runtime->GetArenaPool()); |
| 1114 | CodeVectorAllocator code_allocator(&allocator); |
| 1115 | VariableSizedHandleScope handles(soa.Self()); |
| 1116 | // Go to native so that we don't block GC during compilation. |
| 1117 | ScopedThreadSuspension sts(soa.Self(), kNative); |
| 1118 | std::unique_ptr<CodeGenerator> codegen( |
| 1119 | TryCompileIntrinsic(&allocator, |
| 1120 | &arena_stack, |
| 1121 | &code_allocator, |
| 1122 | dex_compilation_unit, |
| 1123 | method, |
| 1124 | &handles)); |
| 1125 | if (codegen != nullptr) { |
| 1126 | CompiledMethod* compiled_method = Emit(&allocator, |
| 1127 | &code_allocator, |
| 1128 | codegen.get(), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1129 | /* code_item_for_osr_check */ nullptr); |
| 1130 | compiled_method->MarkAsIntrinsic(); |
| 1131 | return compiled_method; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 1136 | CompiledMethod* compiled_method = ArtQuickJniCompileMethod(GetCompilerDriver(), |
| 1137 | access_flags, |
| 1138 | method_idx, |
| 1139 | dex_file); |
| 1140 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledNativeStub); |
| 1141 | return compiled_method; |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 1144 | Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { |
| 1145 | return new OptimizingCompiler(driver); |
| 1146 | } |
| 1147 | |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 1148 | bool IsCompilingWithCoreImage() { |
| 1149 | const std::string& image = Runtime::Current()->GetImageLocation(); |
Roland Levillain | 2b03a1f | 2017-06-06 16:09:59 +0100 | [diff] [blame] | 1150 | return CompilerDriver::IsCoreImageFilename(image); |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Nicolas Geoffray | fbdfa6d | 2017-02-03 10:43:13 +0000 | [diff] [blame] | 1153 | bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) { |
| 1154 | // Note: the runtime is null only for unit testing. |
| 1155 | return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler(); |
| 1156 | } |
| 1157 | |
| 1158 | bool CanEncodeInlinedMethodInStackMap(const DexFile& caller_dex_file, ArtMethod* callee) { |
| 1159 | if (!Runtime::Current()->IsAotCompiler()) { |
| 1160 | // JIT can always encode methods in stack maps. |
| 1161 | return true; |
| 1162 | } |
| 1163 | if (IsSameDexFile(caller_dex_file, *callee->GetDexFile())) { |
| 1164 | return true; |
| 1165 | } |
| 1166 | // TODO(ngeoffray): Support more AOT cases for inlining: |
| 1167 | // - methods in multidex |
| 1168 | // - methods in boot image for on-device non-PIC compilation. |
| 1169 | return false; |
| 1170 | } |
| 1171 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1172 | bool OptimizingCompiler::JitCompile(Thread* self, |
| 1173 | jit::JitCodeCache* code_cache, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1174 | ArtMethod* method, |
Nicolas Geoffray | 01db5f7 | 2017-07-19 15:05:49 +0100 | [diff] [blame] | 1175 | bool osr, |
| 1176 | jit::JitLogger* jit_logger) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1177 | StackHandleScope<3> hs(self); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1178 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 1179 | method->GetDeclaringClass()->GetClassLoader())); |
| 1180 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 1181 | DCHECK(method->IsCompilable()); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1182 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1183 | const DexFile* dex_file = method->GetDexFile(); |
| 1184 | const uint16_t class_def_idx = method->GetClassDefIndex(); |
| 1185 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
| 1186 | const uint32_t method_idx = method->GetDexMethodIndex(); |
| 1187 | const uint32_t access_flags = method->GetAccessFlags(); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1188 | |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1189 | Runtime* runtime = Runtime::Current(); |
| 1190 | ArenaAllocator allocator(runtime->GetJitArenaPool()); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1191 | ArenaStack arena_stack(Runtime::Current()->GetJitArenaPool()); |
| 1192 | CodeVectorAllocator code_allocator(&allocator); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1193 | VariableSizedHandleScope handles(self); |
| 1194 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1195 | std::unique_ptr<CodeGenerator> codegen; |
| 1196 | { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1197 | DexCompilationUnit dex_compilation_unit( |
| 1198 | class_loader, |
| 1199 | runtime->GetClassLinker(), |
| 1200 | *dex_file, |
| 1201 | code_item, |
| 1202 | class_def_idx, |
| 1203 | method_idx, |
| 1204 | access_flags, |
| 1205 | /* verified_method */ nullptr, |
| 1206 | dex_cache); |
| 1207 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1208 | // Go to native so that we don't block GC during compilation. |
| 1209 | ScopedThreadSuspension sts(self, kNative); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1210 | codegen.reset( |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1211 | TryCompile(&allocator, |
| 1212 | &arena_stack, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1213 | &code_allocator, |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1214 | dex_compilation_unit, |
Nicolas Geoffray | 3aaf964 | 2016-06-07 14:14:37 +0000 | [diff] [blame] | 1215 | method, |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1216 | osr, |
| 1217 | &handles)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1218 | if (codegen.get() == nullptr) { |
| 1219 | return false; |
| 1220 | } |
| 1221 | } |
| 1222 | |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1223 | size_t stack_map_size = 0; |
| 1224 | size_t method_info_size = 0; |
| 1225 | codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1226 | size_t number_of_roots = codegen->GetNumberOfJitRoots(); |
| 1227 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1228 | // We allocate an object array to ensure the JIT roots that we will collect in EmitJitRoots |
| 1229 | // will be visible by the GC between EmitLiterals and CommitCode. Once CommitCode is |
| 1230 | // executed, this array is not needed. |
| 1231 | Handle<mirror::ObjectArray<mirror::Object>> roots( |
| 1232 | hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc( |
| 1233 | self, class_linker->GetClassRoot(ClassLinker::kObjectArrayClass), number_of_roots))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1234 | if (roots == nullptr) { |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1235 | // Out of memory, just clear the exception to avoid any Java exception uncaught problems. |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 1236 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1237 | DCHECK(self->IsExceptionPending()); |
| 1238 | self->ClearException(); |
| 1239 | return false; |
| 1240 | } |
| 1241 | uint8_t* stack_map_data = nullptr; |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1242 | uint8_t* method_info_data = nullptr; |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1243 | uint8_t* roots_data = nullptr; |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 1244 | uint32_t data_size = code_cache->ReserveData(self, |
| 1245 | stack_map_size, |
| 1246 | method_info_size, |
| 1247 | number_of_roots, |
| 1248 | method, |
| 1249 | &stack_map_data, |
| 1250 | &method_info_data, |
| 1251 | &roots_data); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1252 | if (stack_map_data == nullptr || roots_data == nullptr) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 1253 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1254 | return false; |
| 1255 | } |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1256 | codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), |
| 1257 | MemoryRegion(method_info_data, method_info_size), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 1258 | code_item); |
Nicolas Geoffray | f0acfe7 | 2017-01-09 20:54:52 +0000 | [diff] [blame] | 1259 | codegen->EmitJitRoots(code_allocator.GetData(), roots, roots_data); |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1260 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1261 | const void* code = code_cache->CommitCode( |
| 1262 | self, |
| 1263 | method, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1264 | stack_map_data, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 1265 | method_info_data, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1266 | roots_data, |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1267 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
| 1268 | codegen->GetCoreSpillMask(), |
| 1269 | codegen->GetFpuSpillMask(), |
| 1270 | code_allocator.GetMemory().data(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1271 | code_allocator.GetSize(), |
Orion Hodson | dbd05fe | 2017-08-10 11:41:35 +0100 | [diff] [blame] | 1272 | data_size, |
Nicolas Geoffray | 132d836 | 2016-11-16 09:19:42 +0000 | [diff] [blame] | 1273 | osr, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1274 | roots, |
| 1275 | codegen->GetGraph()->HasShouldDeoptimizeFlag(), |
| 1276 | codegen->GetGraph()->GetCHASingleImplementationList()); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1277 | |
| 1278 | if (code == nullptr) { |
Vladimir Marko | cd09e1f | 2017-11-24 15:02:40 +0000 | [diff] [blame^] | 1279 | MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit); |
Nicolas Geoffray | f46501c | 2016-11-22 13:45:36 +0000 | [diff] [blame] | 1280 | code_cache->ClearData(self, stack_map_data, roots_data); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1281 | return false; |
| 1282 | } |
| 1283 | |
David Srbecky | 91cc06c | 2016-03-07 16:13:58 +0000 | [diff] [blame] | 1284 | const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions(); |
| 1285 | if (compiler_options.GetGenerateDebugInfo()) { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1286 | const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); |
| 1287 | const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); |
Vladimir Marko | 1b404a8 | 2017-09-01 13:35:26 +0100 | [diff] [blame] | 1288 | debug::MethodDebugInfo info = {}; |
| 1289 | DCHECK(info.trampoline_name.empty()); |
David Srbecky | 197160d | 2016-03-07 17:33:57 +0000 | [diff] [blame] | 1290 | info.dex_file = dex_file; |
| 1291 | info.class_def_index = class_def_idx; |
| 1292 | info.dex_method_index = method_idx; |
| 1293 | info.access_flags = access_flags; |
| 1294 | info.code_item = code_item; |
| 1295 | info.isa = codegen->GetInstructionSet(); |
| 1296 | info.deduped = false; |
| 1297 | info.is_native_debuggable = compiler_options.GetNativeDebuggable(); |
| 1298 | info.is_optimized = true; |
| 1299 | info.is_code_address_text_relative = false; |
| 1300 | info.code_address = code_address; |
| 1301 | info.code_size = code_allocator.GetSize(); |
| 1302 | info.frame_size_in_bytes = method_header->GetFrameSizeInBytes(); |
| 1303 | info.code_info = stack_map_size == 0 ? nullptr : stack_map_data; |
| 1304 | info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 1305 | std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods( |
David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 1306 | GetCompilerDriver()->GetInstructionSet(), |
| 1307 | GetCompilerDriver()->GetInstructionSetFeatures(), |
David Srbecky | fe736b7 | 2016-03-09 11:44:44 +0000 | [diff] [blame] | 1308 | ArrayRef<const debug::MethodDebugInfo>(&info, 1)); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 1309 | CreateJITCodeEntryForAddress(code_address, std::move(elf_file)); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 1312 | Runtime::Current()->GetJit()->AddMemoryUsage(method, allocator.BytesUsed()); |
Nicolas Geoffray | 01db5f7 | 2017-07-19 15:05:49 +0100 | [diff] [blame] | 1313 | if (jit_logger != nullptr) { |
| 1314 | jit_logger->WriteLog(code, code_allocator.GetSize(), method); |
| 1315 | } |
Nicolas Geoffray | a4f8154 | 2016-03-08 16:57:48 +0000 | [diff] [blame] | 1316 | |
Vladimir Marko | 174b2e2 | 2017-10-12 13:34:49 +0100 | [diff] [blame] | 1317 | if (kArenaAllocatorCountAllocations) { |
| 1318 | codegen.reset(); // Release codegen's ScopedArenaAllocator for memory accounting. |
| 1319 | size_t total_allocated = allocator.BytesAllocated() + arena_stack.PeakBytesAllocated(); |
| 1320 | if (total_allocated > kArenaAllocatorMemoryReportThreshold) { |
| 1321 | MemStats mem_stats(allocator.GetMemStats()); |
| 1322 | MemStats peak_stats(arena_stack.GetPeakStats()); |
| 1323 | LOG(INFO) << "Used " << total_allocated << " bytes of arena memory for compiling " |
| 1324 | << dex_file->PrettyMethod(method_idx) |
| 1325 | << "\n" << Dumpable<MemStats>(mem_stats) |
| 1326 | << "\n" << Dumpable<MemStats>(peak_stats); |
| 1327 | } |
| 1328 | } |
| 1329 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 1330 | return true; |
| 1331 | } |
| 1332 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 1333 | } // namespace art |