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> |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 23 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 24 | #include "dex_cache_array_fixups_arm.h" |
| 25 | #endif |
| 26 | |
| 27 | #ifdef ART_ENABLE_CODEGEN_arm64 |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 28 | #include "instruction_simplifier_arm64.h" |
| 29 | #endif |
| 30 | |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 31 | #ifdef ART_ENABLE_CODEGEN_x86 |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 32 | #include "pc_relative_fixups_x86.h" |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 33 | #endif |
| 34 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 35 | #include "art_method-inl.h" |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 36 | #include "base/arena_allocator.h" |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 37 | #include "base/arena_containers.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 38 | #include "base/dumpable.h" |
Vladimir Marko | c90d7c7 | 2015-10-06 17:30:45 +0000 | [diff] [blame] | 39 | #include "base/macros.h" |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 40 | #include "base/timing_logger.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 41 | #include "bounds_check_elimination.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 42 | #include "builder.h" |
| 43 | #include "code_generator.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 44 | #include "compiled_method.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 45 | #include "compiler.h" |
Roland Levillain | 75be283 | 2014-10-17 17:02:00 +0100 | [diff] [blame] | 46 | #include "constant_folding.h" |
| 47 | #include "dead_code_elimination.h" |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 48 | #include "debug/elf_debug_writer.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 49 | #include "debug/method_debug_info.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 50 | #include "dex/quick/dex_file_to_method_inliner_map.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 51 | #include "dex/verification_results.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 52 | #include "dex/verified_method.h" |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 53 | #include "driver/compiler_driver-inl.h" |
Vladimir Marko | 20f8559 | 2015-03-19 10:07:02 +0000 | [diff] [blame] | 54 | #include "driver/compiler_options.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 55 | #include "driver/dex_compilation_unit.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 56 | #include "elf_writer_quick.h" |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 57 | #include "graph_checker.h" |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 58 | #include "graph_visualizer.h" |
Nicolas Geoffray | d31cf3d | 2014-09-08 17:30:24 +0100 | [diff] [blame] | 59 | #include "gvn.h" |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 60 | #include "induction_var_analysis.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 61 | #include "inliner.h" |
Nicolas Geoffray | 3c04974 | 2014-09-24 18:10:46 +0100 | [diff] [blame] | 62 | #include "instruction_simplifier.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 63 | #include "intrinsics.h" |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 64 | #include "jit/debugger_interface.h" |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 65 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 66 | #include "jni/quick/jni_compiler.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 67 | #include "licm.h" |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 68 | #include "load_store_elimination.h" |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 69 | #include "nodes.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 70 | #include "oat_quick_method_header.h" |
Nicolas Geoffray | 26a25ef | 2014-09-30 13:54:09 +0100 | [diff] [blame] | 71 | #include "prepare_for_register_allocation.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 72 | #include "reference_type_propagation.h" |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 73 | #include "register_allocator.h" |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 74 | #include "select_generator.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 75 | #include "sharpening.h" |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 76 | #include "side_effects_analysis.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 77 | #include "ssa_builder.h" |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 78 | #include "ssa_liveness_analysis.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 79 | #include "ssa_phi_elimination.h" |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 80 | #include "utils/assembler.h" |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 81 | #include "verifier/method_verifier.h" |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 82 | |
| 83 | namespace art { |
| 84 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Used by the code generator, to allocate the code in a vector. |
| 87 | */ |
| 88 | class CodeVectorAllocator FINAL : public CodeAllocator { |
| 89 | public: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 90 | explicit CodeVectorAllocator(ArenaAllocator* arena) |
| 91 | : memory_(arena->Adapter(kArenaAllocCodeBuffer)), |
| 92 | size_(0) {} |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 93 | |
| 94 | virtual uint8_t* Allocate(size_t size) { |
| 95 | size_ = size; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 96 | memory_.resize(size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 97 | return &memory_[0]; |
| 98 | } |
| 99 | |
| 100 | size_t GetSize() const { return size_; } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 101 | const ArenaVector<uint8_t>& GetMemory() const { return memory_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 102 | |
| 103 | private: |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 104 | ArenaVector<uint8_t> memory_; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 105 | size_t size_; |
| 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); |
| 108 | }; |
| 109 | |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 110 | /** |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 111 | * 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] | 112 | * be dumped. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 113 | */ |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 114 | static constexpr const char kStringFilter[] = ""; |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 115 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 116 | class PassScope; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 117 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 118 | class PassObserver : public ValueObject { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 119 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 120 | PassObserver(HGraph* graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 121 | CodeGenerator* codegen, |
| 122 | std::ostream* visualizer_output, |
| 123 | CompilerDriver* compiler_driver) |
| 124 | : graph_(graph), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 125 | cached_method_name_(), |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 126 | timing_logger_enabled_(compiler_driver->GetDumpPasses()), |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 127 | timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true), |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 128 | disasm_info_(graph->GetArena()), |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 129 | visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 130 | visualizer_(visualizer_output, graph, *codegen), |
| 131 | graph_in_bad_state_(false) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 132 | if (timing_logger_enabled_ || visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 133 | if (!IsVerboseMethod(compiler_driver, GetMethodName())) { |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 134 | timing_logger_enabled_ = visualizer_enabled_ = false; |
| 135 | } |
| 136 | if (visualizer_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 137 | visualizer_.PrintHeader(GetMethodName()); |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 138 | codegen->SetDisassemblyInformation(&disasm_info_); |
| 139 | } |
David Brazdil | 62e074f | 2015-04-07 18:09:37 +0100 | [diff] [blame] | 140 | } |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 143 | ~PassObserver() { |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 144 | if (timing_logger_enabled_) { |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 145 | LOG(INFO) << "TIMINGS " << GetMethodName(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 146 | LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); |
| 147 | } |
| 148 | } |
| 149 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 150 | void DumpDisassembly() const { |
| 151 | if (visualizer_enabled_) { |
| 152 | visualizer_.DumpGraphWithDisassembly(); |
| 153 | } |
| 154 | } |
| 155 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 156 | void SetGraphInBadState() { graph_in_bad_state_ = true; } |
| 157 | |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 158 | const char* GetMethodName() { |
| 159 | // PrettyMethod() is expensive, so we delay calling it until we actually have to. |
| 160 | if (cached_method_name_.empty()) { |
| 161 | cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile()); |
| 162 | } |
| 163 | return cached_method_name_.c_str(); |
| 164 | } |
| 165 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 166 | private: |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 167 | void StartPass(const char* pass_name) { |
| 168 | // Dump graph first, then start timer. |
| 169 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 170 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 171 | } |
| 172 | if (timing_logger_enabled_) { |
| 173 | timing_logger_.StartTiming(pass_name); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void EndPass(const char* pass_name) { |
| 178 | // Pause timer first, then dump graph. |
| 179 | if (timing_logger_enabled_) { |
| 180 | timing_logger_.EndTiming(); |
| 181 | } |
| 182 | if (visualizer_enabled_) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 183 | visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 184 | } |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 185 | |
| 186 | // Validate the HGraph if running in debug mode. |
| 187 | if (kIsDebugBuild) { |
| 188 | if (!graph_in_bad_state_) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 189 | GraphChecker checker(graph_); |
| 190 | checker.Run(); |
| 191 | if (!checker.IsValid()) { |
| 192 | LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Andreas Gampe | 53fcd0f | 2015-07-22 12:10:13 -0700 | [diff] [blame] | 198 | static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { |
| 199 | // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an |
| 200 | // empty kStringFilter matching all methods. |
| 201 | if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { |
| 202 | return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); |
| 203 | } |
| 204 | |
| 205 | // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code |
| 206 | // warning when the string is empty. |
| 207 | constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; |
| 208 | if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 215 | HGraph* const graph_; |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 216 | |
| 217 | std::string cached_method_name_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 218 | |
| 219 | bool timing_logger_enabled_; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 220 | TimingLogger timing_logger_; |
| 221 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 222 | DisassemblyInformation disasm_info_; |
| 223 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 224 | bool visualizer_enabled_; |
| 225 | HGraphVisualizer visualizer_; |
| 226 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 227 | // Flag to be set by the compiler if the pass failed and the graph is not |
| 228 | // expected to validate. |
| 229 | bool graph_in_bad_state_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 230 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 231 | friend PassScope; |
| 232 | |
| 233 | DISALLOW_COPY_AND_ASSIGN(PassObserver); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 236 | class PassScope : public ValueObject { |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 237 | public: |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 238 | PassScope(const char *pass_name, PassObserver* pass_observer) |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 239 | : pass_name_(pass_name), |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 240 | pass_observer_(pass_observer) { |
| 241 | pass_observer_->StartPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 242 | } |
| 243 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 244 | ~PassScope() { |
| 245 | pass_observer_->EndPass(pass_name_); |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private: |
| 249 | const char* const pass_name_; |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 250 | PassObserver* const pass_observer_; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 253 | class OptimizingCompiler FINAL : public Compiler { |
| 254 | public: |
| 255 | explicit OptimizingCompiler(CompilerDriver* driver); |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 256 | ~OptimizingCompiler(); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 257 | |
| 258 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const |
| 259 | OVERRIDE; |
| 260 | |
| 261 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 262 | uint32_t access_flags, |
| 263 | InvokeType invoke_type, |
| 264 | uint16_t class_def_idx, |
| 265 | uint32_t method_idx, |
| 266 | jobject class_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 267 | const DexFile& dex_file, |
| 268 | Handle<mirror::DexCache> dex_cache) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 269 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 270 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 271 | uint32_t method_idx, |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 272 | const DexFile& dex_file) const OVERRIDE { |
| 273 | return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); |
| 274 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 275 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 276 | uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 277 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 278 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( |
| 279 | InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); |
| 280 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 281 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 282 | void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 283 | |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 284 | void Init() OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 285 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 286 | void UnInit() const OVERRIDE; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 287 | |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 288 | void MaybeRecordStat(MethodCompilationStat compilation_stat) const { |
| 289 | if (compilation_stats_.get() != nullptr) { |
| 290 | compilation_stats_->RecordStat(compilation_stat); |
| 291 | } |
| 292 | } |
| 293 | |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 294 | bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr) |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 295 | OVERRIDE |
| 296 | SHARED_REQUIRES(Locks::mutator_lock_); |
| 297 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 298 | private: |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 299 | // Create a 'CompiledMethod' for an optimized graph. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 300 | CompiledMethod* Emit(ArenaAllocator* arena, |
| 301 | CodeVectorAllocator* code_allocator, |
| 302 | CodeGenerator* codegen, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 303 | CompilerDriver* driver, |
| 304 | const DexFile::CodeItem* item) const; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 305 | |
| 306 | // Try compiling a method and return the code generator used for |
| 307 | // compiling it. |
| 308 | // This method: |
| 309 | // 1) Builds the graph. Returns null if it failed to build it. |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 310 | // 2) Transforms the graph to SSA. Returns null if it failed. |
| 311 | // 3) Runs optimizations on the graph, including register allocator. |
| 312 | // 4) Generates code with the `code_allocator` provided. |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 313 | CodeGenerator* TryCompile(ArenaAllocator* arena, |
| 314 | CodeVectorAllocator* code_allocator, |
| 315 | const DexFile::CodeItem* code_item, |
| 316 | uint32_t access_flags, |
| 317 | InvokeType invoke_type, |
| 318 | uint16_t class_def_idx, |
| 319 | uint32_t method_idx, |
| 320 | jobject class_loader, |
| 321 | const DexFile& dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 322 | Handle<mirror::DexCache> dex_cache, |
| 323 | bool osr) const; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 324 | |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 325 | std::unique_ptr<OptimizingCompilerStats> compilation_stats_; |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 326 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 327 | std::unique_ptr<std::ostream> visualizer_output_; |
| 328 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 329 | DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); |
| 330 | }; |
| 331 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 332 | static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ |
| 333 | |
| 334 | OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 335 | : Compiler(driver, kMaximumCompilationTimeBeforeWarning) {} |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 336 | |
| 337 | void OptimizingCompiler::Init() { |
| 338 | // Enable C1visualizer output. Must be done in Init() because the compiler |
| 339 | // driver is not fully initialized when passed to the compiler's constructor. |
| 340 | CompilerDriver* driver = GetCompilerDriver(); |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 341 | const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName(); |
David Brazdil | 866c031 | 2015-01-13 21:21:31 +0000 | [diff] [blame] | 342 | if (!cfg_file_name.empty()) { |
David Brazdil | ee690a3 | 2014-12-01 17:04:16 +0000 | [diff] [blame] | 343 | CHECK_EQ(driver->GetThreadCount(), 1U) |
| 344 | << "Graph visualizer requires the compiler to run single-threaded. " |
| 345 | << "Invoke the compiler with '-j1'."; |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 346 | std::ios_base::openmode cfg_file_mode = |
Nicolas Geoffray | c903b6a | 2016-01-18 12:56:06 +0000 | [diff] [blame] | 347 | driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; |
Calin Juravle | 87000a9 | 2015-08-24 15:34:44 +0100 | [diff] [blame] | 348 | visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 349 | } |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 350 | if (driver->GetDumpStats()) { |
| 351 | compilation_stats_.reset(new OptimizingCompilerStats()); |
| 352 | } |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 353 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 354 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 355 | void OptimizingCompiler::UnInit() const { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 358 | OptimizingCompiler::~OptimizingCompiler() { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 359 | if (compilation_stats_.get() != nullptr) { |
| 360 | compilation_stats_->Log(); |
| 361 | } |
Nicolas Geoffray | 88157ef | 2014-09-12 10:29:53 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Nicolas Geoffray | da3a55b | 2015-10-02 11:56:50 +0100 | [diff] [blame] | 364 | void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Nicolas Geoffray | e2dc6fa | 2014-11-17 12:55:12 +0000 | [diff] [blame] | 367 | bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, |
| 368 | const DexFile& dex_file ATTRIBUTE_UNUSED, |
| 369 | CompilationUnit* cu ATTRIBUTE_UNUSED) const { |
| 370 | return true; |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 373 | static bool IsInstructionSetSupported(InstructionSet instruction_set) { |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 374 | return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) |
| 375 | || instruction_set == kArm64 |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 376 | || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) |
Goran Jakovljevic | f652cec | 2015-08-25 16:11:42 +0200 | [diff] [blame] | 377 | || instruction_set == kMips |
Alexey Frunze | 4dda337 | 2015-06-01 18:31:49 -0700 | [diff] [blame] | 378 | || instruction_set == kMips64 |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 379 | || instruction_set == kX86 |
| 380 | || instruction_set == kX86_64; |
| 381 | } |
| 382 | |
Roland Levillain | 22ccc3a | 2015-11-24 13:10:05 +0000 | [diff] [blame] | 383 | // Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment. |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 384 | // TODO: Add support for other architectures and remove this function |
| 385 | static bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) { |
Roland Levillain | 22ccc3a | 2015-11-24 13:10:05 +0000 | [diff] [blame] | 386 | return instruction_set == kArm64 |
| 387 | || instruction_set == kThumb2 |
Roland Levillain | 3b359c7 | 2015-11-17 19:35:12 +0000 | [diff] [blame] | 388 | || instruction_set == kX86 |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 389 | || instruction_set == kX86_64; |
| 390 | } |
| 391 | |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 392 | static void RunOptimizations(HOptimization* optimizations[], |
| 393 | size_t length, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 394 | PassObserver* pass_observer) { |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 395 | for (size_t i = 0; i < length; ++i) { |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 396 | PassScope scope(optimizations[i]->GetPassName(), pass_observer); |
| 397 | optimizations[i]->Run(); |
Calin Juravle | 10e244f | 2015-01-26 18:54:32 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 401 | static void MaybeRunInliner(HGraph* graph, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 402 | CodeGenerator* codegen, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 403 | CompilerDriver* driver, |
| 404 | OptimizingCompilerStats* stats, |
| 405 | const DexCompilationUnit& dex_compilation_unit, |
| 406 | PassObserver* pass_observer, |
| 407 | StackHandleScopeCollection* handles) { |
| 408 | const CompilerOptions& compiler_options = driver->GetCompilerOptions(); |
| 409 | bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) |
| 410 | && (compiler_options.GetInlineMaxCodeUnits() > 0); |
| 411 | if (!should_inline) { |
| 412 | return; |
| 413 | } |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 414 | size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 415 | HInliner* inliner = new (graph->GetArena()) HInliner( |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 416 | graph, |
| 417 | graph, |
| 418 | codegen, |
| 419 | dex_compilation_unit, |
| 420 | dex_compilation_unit, |
| 421 | driver, |
| 422 | handles, |
| 423 | stats, |
| 424 | number_of_dex_registers, |
| 425 | /* depth */ 0); |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 426 | HOptimization* optimizations[] = { inliner }; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 427 | |
| 428 | RunOptimizations(optimizations, arraysize(optimizations), pass_observer); |
| 429 | } |
| 430 | |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 431 | static void RunArchOptimizations(InstructionSet instruction_set, |
| 432 | HGraph* graph, |
| 433 | OptimizingCompilerStats* stats, |
| 434 | PassObserver* pass_observer) { |
| 435 | ArenaAllocator* arena = graph->GetArena(); |
| 436 | switch (instruction_set) { |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 437 | #ifdef ART_ENABLE_CODEGEN_arm |
| 438 | case kThumb2: |
| 439 | case kArm: { |
| 440 | arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats); |
| 441 | HOptimization* arm_optimizations[] = { |
Nicolas Geoffray | 6b5afdd | 2016-01-22 09:31:52 +0000 | [diff] [blame] | 442 | fixups |
Vladimir Marko | b4536b7 | 2015-11-24 13:45:23 +0000 | [diff] [blame] | 443 | }; |
| 444 | RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer); |
| 445 | break; |
| 446 | } |
| 447 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 448 | #ifdef ART_ENABLE_CODEGEN_arm64 |
| 449 | case kArm64: { |
| 450 | arm64::InstructionSimplifierArm64* simplifier = |
| 451 | new (arena) arm64::InstructionSimplifierArm64(graph, stats); |
| 452 | SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); |
| 453 | GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); |
| 454 | HOptimization* arm64_optimizations[] = { |
| 455 | simplifier, |
| 456 | side_effects, |
| 457 | gvn |
| 458 | }; |
| 459 | RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); |
| 460 | break; |
| 461 | } |
| 462 | #endif |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 463 | #ifdef ART_ENABLE_CODEGEN_x86 |
| 464 | case kX86: { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 465 | x86::PcRelativeFixups* pc_relative_fixups = new (arena) x86::PcRelativeFixups(graph, stats); |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 466 | HOptimization* x86_optimizations[] = { |
Vladimir Marko | 0f7dca4 | 2015-11-02 14:36:43 +0000 | [diff] [blame] | 467 | pc_relative_fixups |
Mark Mendell | 0616ae0 | 2015-04-17 12:49:27 -0400 | [diff] [blame] | 468 | }; |
| 469 | RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); |
| 470 | break; |
| 471 | } |
| 472 | #endif |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 473 | default: |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 478 | NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. |
| 479 | static void AllocateRegisters(HGraph* graph, |
| 480 | CodeGenerator* codegen, |
| 481 | PassObserver* pass_observer) { |
| 482 | PrepareForRegisterAllocation(graph).Run(); |
| 483 | SsaLivenessAnalysis liveness(graph, codegen); |
| 484 | { |
| 485 | PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); |
| 486 | liveness.Analyze(); |
| 487 | } |
| 488 | { |
| 489 | PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); |
| 490 | RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); |
| 491 | } |
| 492 | } |
| 493 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 494 | static void RunOptimizations(HGraph* graph, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 495 | CodeGenerator* codegen, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 496 | CompilerDriver* driver, |
| 497 | OptimizingCompilerStats* stats, |
| 498 | const DexCompilationUnit& dex_compilation_unit, |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 499 | PassObserver* pass_observer, |
| 500 | StackHandleScopeCollection* handles) { |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 501 | ArenaAllocator* arena = graph->GetArena(); |
| 502 | HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( |
| 503 | graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); |
| 504 | HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( |
| 505 | graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 506 | HConstantFolding* fold1 = new (arena) HConstantFolding(graph); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 507 | InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 508 | HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 509 | HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 510 | HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce"); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 511 | SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); |
| 512 | GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); |
| 513 | LICM* licm = new (arena) LICM(graph, *side_effects); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 514 | LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 515 | HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 516 | BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 517 | HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 518 | InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 519 | graph, stats, "instruction_simplifier_after_bce"); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 520 | InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( |
Nicolas Geoffray | b2bdfce | 2015-06-18 15:46:47 +0100 | [diff] [blame] | 521 | graph, stats, "instruction_simplifier_before_codegen"); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 522 | IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver); |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 523 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 524 | HOptimization* optimizations1[] = { |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 525 | intrinsics, |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 526 | sharpening, |
Vladimir Marko | a3a3c59 | 2015-06-12 14:30:53 +0100 | [diff] [blame] | 527 | fold1, |
| 528 | simplify1, |
Nicolas Geoffray | 18e6873 | 2015-06-17 23:09:05 +0100 | [diff] [blame] | 529 | dce1, |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 530 | }; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 531 | RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); |
| 532 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 533 | MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles); |
David Brazdil | 9517798 | 2015-10-30 12:56:58 -0500 | [diff] [blame] | 534 | |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 535 | HOptimization* optimizations2[] = { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 536 | // SelectGenerator depends on the InstructionSimplifier removing |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 537 | // redundant suspend checks to recognize empty blocks. |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 538 | select_generator, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 539 | fold2, // TODO: if we don't inline we can also skip fold2. |
| 540 | side_effects, |
| 541 | gvn, |
| 542 | licm, |
| 543 | induction, |
| 544 | bce, |
| 545 | fold3, // evaluates code generated by dynamic bce |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 546 | simplify2, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 547 | lse, |
| 548 | dce2, |
| 549 | // The codegen has a few assumptions that only the instruction simplifier |
| 550 | // can satisfy. For example, the code generator does not expect to see a |
| 551 | // HTypeConversion from a type to the same type. |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 552 | simplify3, |
David Brazdil | 8993caf | 2015-12-07 10:04:40 +0000 | [diff] [blame] | 553 | }; |
| 554 | RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); |
David Brazdil | bbd733e | 2015-08-18 17:48:17 +0100 | [diff] [blame] | 555 | |
Alexandre Rames | 44b9cf9 | 2015-08-19 15:39:06 +0100 | [diff] [blame] | 556 | RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 557 | AllocateRegisters(graph, codegen, pass_observer); |
Nicolas Geoffray | 5e6916c | 2014-11-18 16:53:35 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 560 | static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { |
| 561 | ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); |
| 562 | codegen->EmitLinkerPatches(&linker_patches); |
| 563 | |
| 564 | // Sort patches by literal offset. Required for .oat_patches encoding. |
| 565 | std::sort(linker_patches.begin(), linker_patches.end(), |
| 566 | [](const LinkerPatch& lhs, const LinkerPatch& rhs) { |
| 567 | return lhs.LiteralOffset() < rhs.LiteralOffset(); |
| 568 | }); |
| 569 | |
| 570 | return linker_patches; |
| 571 | } |
| 572 | |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 573 | CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena, |
| 574 | CodeVectorAllocator* code_allocator, |
| 575 | CodeGenerator* codegen, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 576 | CompilerDriver* compiler_driver, |
| 577 | const DexFile::CodeItem* code_item) const { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 578 | ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 579 | ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 580 | stack_map.resize(codegen->ComputeStackMapsSize()); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 581 | codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 582 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 583 | CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 584 | compiler_driver, |
| 585 | codegen->GetInstructionSet(), |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 586 | ArrayRef<const uint8_t>(code_allocator->GetMemory()), |
Roland Levillain | aa9b7c4 | 2015-02-17 15:40:09 +0000 | [diff] [blame] | 587 | // Follow Quick's behavior and set the frame size to zero if it is |
| 588 | // considered "empty" (see the definition of |
| 589 | // art::CodeGenerator::HasEmptyFrame). |
| 590 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 591 | codegen->GetCoreSpillMask(), |
Nicolas Geoffray | d97dc40 | 2015-01-22 13:50:01 +0000 | [diff] [blame] | 592 | codegen->GetFpuSpillMask(), |
David Srbecky | f71b3ad | 2015-12-08 15:05:08 +0000 | [diff] [blame] | 593 | ArrayRef<const SrcMapElem>(), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 594 | ArrayRef<const uint8_t>(), // mapping_table. |
| 595 | ArrayRef<const uint8_t>(stack_map), |
| 596 | ArrayRef<const uint8_t>(), // native_gc_map. |
| 597 | ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 598 | ArrayRef<const LinkerPatch>(linker_patches)); |
Mathieu Chartier | ed15000 | 2015-08-28 11:16:54 -0700 | [diff] [blame] | 599 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 600 | return compiled_method; |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 603 | CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, |
| 604 | CodeVectorAllocator* code_allocator, |
| 605 | const DexFile::CodeItem* code_item, |
| 606 | uint32_t access_flags, |
| 607 | InvokeType invoke_type, |
| 608 | uint16_t class_def_idx, |
| 609 | uint32_t method_idx, |
| 610 | jobject class_loader, |
| 611 | const DexFile& dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 612 | Handle<mirror::DexCache> dex_cache, |
| 613 | bool osr) const { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 614 | MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 615 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 616 | InstructionSet instruction_set = compiler_driver->GetInstructionSet(); |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 617 | |
Roland Levillain | 3b359c7 | 2015-11-17 19:35:12 +0000 | [diff] [blame] | 618 | // Always use the Thumb-2 assembler: some runtime functionality |
| 619 | // (like implicit stack overflow checks) assume Thumb-2. |
Nicolas Geoffray | 8d48673 | 2014-07-16 16:23:40 +0100 | [diff] [blame] | 620 | if (instruction_set == kArm) { |
| 621 | instruction_set = kThumb2; |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // Do not attempt to compile on architectures we do not support. |
Nicolas Geoffray | 1ba0f59 | 2014-10-27 15:14:55 +0000 | [diff] [blame] | 625 | if (!IsInstructionSetSupported(instruction_set)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 626 | MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); |
Nicolas Geoffray | 8fb5ce3 | 2014-07-04 09:43:26 +0100 | [diff] [blame] | 627 | return nullptr; |
| 628 | } |
| 629 | |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 630 | // When read barriers are enabled, do not attempt to compile for |
| 631 | // instruction sets that have no read barrier support. |
| 632 | if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) { |
| 633 | return nullptr; |
| 634 | } |
| 635 | |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 636 | if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 637 | MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); |
Nicolas Geoffray | b5f62b3 | 2014-10-30 10:58:41 +0000 | [diff] [blame] | 638 | return nullptr; |
| 639 | } |
| 640 | |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 641 | // 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] | 642 | // code units is bigger than 128. |
| 643 | static constexpr size_t kSpaceFilterOptimizingThreshold = 128; |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 644 | const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); |
| 645 | if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) |
| 646 | && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 647 | MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); |
Nicolas Geoffray | 36540cb | 2015-03-23 14:45:53 +0000 | [diff] [blame] | 648 | return nullptr; |
| 649 | } |
| 650 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 651 | DexCompilationUnit dex_compilation_unit( |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 652 | nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 653 | class_def_idx, method_idx, access_flags, |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame^] | 654 | nullptr, dex_cache); |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 655 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 656 | bool requires_barrier = dex_compilation_unit.IsConstructor() |
| 657 | && compiler_driver->RequiresConstructorBarrier(Thread::Current(), |
| 658 | dex_compilation_unit.GetDexFile(), |
| 659 | dex_compilation_unit.GetClassDefIndex()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 660 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 661 | HGraph* graph = new (arena) HGraph( |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 662 | arena, |
| 663 | dex_file, |
| 664 | method_idx, |
| 665 | requires_barrier, |
| 666 | compiler_driver->GetInstructionSet(), |
| 667 | kInvalidInvokeType, |
| 668 | compiler_driver->GetCompilerOptions().GetDebuggable(), |
| 669 | osr); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 670 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 671 | const uint8_t* interpreter_metadata = nullptr; |
| 672 | { |
| 673 | ScopedObjectAccess soa(Thread::Current()); |
| 674 | StackHandleScope<1> hs(soa.Self()); |
| 675 | Handle<mirror::ClassLoader> loader(hs.NewHandle( |
| 676 | soa.Decode<mirror::ClassLoader*>(class_loader))); |
| 677 | ArtMethod* art_method = compiler_driver->ResolveMethod( |
| 678 | soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); |
| 679 | // We may not get a method, for example if its class is erroneous. |
| 680 | if (art_method != nullptr) { |
| 681 | graph->SetArtMethod(art_method); |
| 682 | interpreter_metadata = art_method->GetQuickenedInfo(); |
| 683 | } |
| 684 | } |
| 685 | |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 686 | std::unique_ptr<CodeGenerator> codegen( |
Calin Juravle | cd6dffe | 2015-01-08 17:35:35 +0000 | [diff] [blame] | 687 | CodeGenerator::Create(graph, |
| 688 | instruction_set, |
| 689 | *compiler_driver->GetInstructionSetFeatures(), |
| 690 | compiler_driver->GetCompilerOptions())); |
Nicolas Geoffray | 12df9eb | 2015-01-09 14:53:50 +0000 | [diff] [blame] | 691 | if (codegen.get() == nullptr) { |
Calin Juravle | 2be39e0 | 2015-04-21 13:56:34 +0100 | [diff] [blame] | 692 | MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 693 | return nullptr; |
| 694 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 695 | codegen->GetAssembler()->cfi().SetEnabled( |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 696 | compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 697 | |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 698 | PassObserver pass_observer(graph, |
David Brazdil | 69ba7b7 | 2015-06-23 18:27:30 +0100 | [diff] [blame] | 699 | codegen.get(), |
| 700 | visualizer_output_.get(), |
| 701 | compiler_driver); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 702 | |
Vladimir Marko | 0d1caa5 | 2015-11-20 13:02:22 +0000 | [diff] [blame] | 703 | VLOG(compiler) << "Building " << pass_observer.GetMethodName(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 704 | |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 705 | { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 706 | ScopedObjectAccess soa(Thread::Current()); |
| 707 | StackHandleScopeCollection handles(soa.Self()); |
| 708 | // Do not hold `mutator_lock_` between optimizations. |
| 709 | ScopedThreadSuspension sts(soa.Self(), kNative); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 710 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 711 | { |
| 712 | PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); |
| 713 | HGraphBuilder builder(graph, |
| 714 | &dex_compilation_unit, |
| 715 | &dex_compilation_unit, |
| 716 | &dex_file, |
| 717 | compiler_driver, |
| 718 | compilation_stats_.get(), |
| 719 | interpreter_metadata, |
| 720 | dex_cache); |
| 721 | GraphAnalysisResult result = builder.BuildGraph(*code_item, &handles); |
| 722 | if (result != kAnalysisSuccess) { |
| 723 | switch (result) { |
| 724 | case kAnalysisInvalidBytecode: |
| 725 | break; |
| 726 | case kAnalysisFailThrowCatchLoop: |
| 727 | MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop); |
| 728 | break; |
| 729 | case kAnalysisFailAmbiguousArrayOp: |
| 730 | MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp); |
| 731 | break; |
| 732 | case kAnalysisSuccess: |
| 733 | UNREACHABLE(); |
| 734 | } |
| 735 | pass_observer.SetGraphInBadState(); |
| 736 | return nullptr; |
David Brazdil | 809658e | 2015-02-05 11:34:02 +0000 | [diff] [blame] | 737 | } |
Nicolas Geoffray | f537012 | 2014-12-02 11:51:19 +0000 | [diff] [blame] | 738 | } |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 739 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 740 | RunOptimizations(graph, |
| 741 | codegen.get(), |
| 742 | compiler_driver, |
| 743 | compilation_stats_.get(), |
| 744 | dex_compilation_unit, |
| 745 | &pass_observer, |
| 746 | &handles); |
| 747 | |
| 748 | codegen->Compile(code_allocator); |
| 749 | pass_observer.DumpDisassembly(); |
| 750 | } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 751 | |
| 752 | if (kArenaAllocatorCountAllocations) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 753 | if (arena->BytesAllocated() > 4 * MB) { |
| 754 | MemStats mem_stats(arena->GetMemStats()); |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 755 | LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); |
| 756 | } |
| 757 | } |
| 758 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 759 | return codegen.release(); |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Calin Juravle | 07380a2 | 2015-09-17 14:15:12 +0100 | [diff] [blame] | 762 | static bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { |
| 763 | // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). |
| 764 | uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS |
| 765 | | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS |
| 766 | | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD |
| 767 | | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 768 | return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; |
| 769 | } |
| 770 | |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 771 | CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, |
| 772 | uint32_t access_flags, |
| 773 | InvokeType invoke_type, |
| 774 | uint16_t class_def_idx, |
| 775 | uint32_t method_idx, |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 776 | jobject jclass_loader, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 777 | const DexFile& dex_file, |
| 778 | Handle<mirror::DexCache> dex_cache) const { |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 779 | CompilerDriver* compiler_driver = GetCompilerDriver(); |
| 780 | CompiledMethod* method = nullptr; |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 781 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 782 | const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); |
| 783 | DCHECK(!verified_method->HasRuntimeThrow()); |
| 784 | if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) |
| 785 | || CanHandleVerificationFailure(verified_method)) { |
| 786 | ArenaAllocator arena(Runtime::Current()->GetArenaPool()); |
| 787 | CodeVectorAllocator code_allocator(&arena); |
| 788 | std::unique_ptr<CodeGenerator> codegen( |
| 789 | TryCompile(&arena, |
| 790 | &code_allocator, |
| 791 | code_item, |
| 792 | access_flags, |
| 793 | invoke_type, |
| 794 | class_def_idx, |
| 795 | method_idx, |
| 796 | jclass_loader, |
| 797 | dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 798 | dex_cache, |
| 799 | /* osr */ false)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 800 | if (codegen.get() != nullptr) { |
Calin Juravle | ad54338 | 2015-11-19 17:26:29 +0000 | [diff] [blame] | 801 | MaybeRecordStat(MethodCompilationStat::kCompiled); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 802 | method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item); |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 803 | } |
Nicolas Geoffray | 0c3c266 | 2015-10-15 13:53:04 +0100 | [diff] [blame] | 804 | } else { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 805 | if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { |
| 806 | MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); |
| 807 | } else { |
Calin Juravle | ad54338 | 2015-11-19 17:26:29 +0000 | [diff] [blame] | 808 | MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 809 | } |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 810 | } |
| 811 | |
Calin Juravle | cff8cc7 | 2015-10-09 12:03:24 +0100 | [diff] [blame] | 812 | if (kIsDebugBuild && |
| 813 | IsCompilingWithCoreImage() && |
Roland Levillain | 0d5a281 | 2015-11-13 10:07:31 +0000 | [diff] [blame] | 814 | IsInstructionSetSupported(compiler_driver->GetInstructionSet()) && |
| 815 | (!kEmitCompilerReadBarrier || |
| 816 | InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) { |
| 817 | // For testing purposes, we put a special marker on method names |
| 818 | // that should be compiled with this compiler (when the the |
| 819 | // instruction set is supported -- and has support for read |
| 820 | // barriers, if they are enabled). This makes sure we're not |
| 821 | // regressing. |
Calin Juravle | 09b1d6f | 2015-10-07 12:08:54 +0100 | [diff] [blame] | 822 | std::string method_name = PrettyMethod(method_idx, dex_file); |
| 823 | bool shouldCompile = method_name.find("$opt$") != std::string::npos; |
| 824 | DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; |
| 825 | } |
| 826 | |
Nicolas Geoffray | 12be74e | 2015-03-30 13:29:08 +0100 | [diff] [blame] | 827 | return method; |
Nicolas Geoffray | 216eaa2 | 2015-03-17 17:09:30 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 830 | Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { |
| 831 | return new OptimizingCompiler(driver); |
| 832 | } |
| 833 | |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 834 | bool IsCompilingWithCoreImage() { |
| 835 | const std::string& image = Runtime::Current()->GetImageLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 836 | // TODO: This is under-approximating... |
| 837 | if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) { |
| 838 | return true; |
| 839 | } |
| 840 | return false; |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 843 | bool OptimizingCompiler::JitCompile(Thread* self, |
| 844 | jit::JitCodeCache* code_cache, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 845 | ArtMethod* method, |
| 846 | bool osr) { |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 847 | StackHandleScope<2> hs(self); |
| 848 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 849 | method->GetDeclaringClass()->GetClassLoader())); |
| 850 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); |
| 851 | |
| 852 | jobject jclass_loader = class_loader.ToJObject(); |
| 853 | const DexFile* dex_file = method->GetDexFile(); |
| 854 | const uint16_t class_def_idx = method->GetClassDefIndex(); |
| 855 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
| 856 | const uint32_t method_idx = method->GetDexMethodIndex(); |
| 857 | const uint32_t access_flags = method->GetAccessFlags(); |
| 858 | const InvokeType invoke_type = method->GetInvokeType(); |
| 859 | |
| 860 | ArenaAllocator arena(Runtime::Current()->GetArenaPool()); |
| 861 | CodeVectorAllocator code_allocator(&arena); |
| 862 | std::unique_ptr<CodeGenerator> codegen; |
| 863 | { |
| 864 | // Go to native so that we don't block GC during compilation. |
| 865 | ScopedThreadSuspension sts(self, kNative); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 866 | codegen.reset( |
| 867 | TryCompile(&arena, |
| 868 | &code_allocator, |
| 869 | code_item, |
| 870 | access_flags, |
| 871 | invoke_type, |
| 872 | class_def_idx, |
| 873 | method_idx, |
| 874 | jclass_loader, |
| 875 | *dex_file, |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 876 | dex_cache, |
| 877 | osr)); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 878 | if (codegen.get() == nullptr) { |
| 879 | return false; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | size_t stack_map_size = codegen->ComputeStackMapsSize(); |
| 884 | uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size); |
| 885 | if (stack_map_data == nullptr) { |
| 886 | return false; |
| 887 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 888 | MaybeRecordStat(MethodCompilationStat::kCompiled); |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 889 | codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 890 | const void* code = code_cache->CommitCode( |
| 891 | self, |
| 892 | method, |
| 893 | nullptr, |
| 894 | stack_map_data, |
| 895 | nullptr, |
| 896 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
| 897 | codegen->GetCoreSpillMask(), |
| 898 | codegen->GetFpuSpillMask(), |
| 899 | code_allocator.GetMemory().data(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 900 | code_allocator.GetSize(), |
| 901 | osr); |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 902 | |
| 903 | if (code == nullptr) { |
| 904 | code_cache->ClearData(self, stack_map_data); |
| 905 | return false; |
| 906 | } |
| 907 | |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 908 | if (GetCompilerDriver()->GetCompilerOptions().GetGenerateDebugInfo()) { |
| 909 | const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); |
| 910 | const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); |
| 911 | CompiledMethod compiled_method( |
| 912 | GetCompilerDriver(), |
| 913 | codegen->GetInstructionSet(), |
| 914 | ArrayRef<const uint8_t>(code_allocator.GetMemory()), |
| 915 | codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), |
| 916 | codegen->GetCoreSpillMask(), |
| 917 | codegen->GetFpuSpillMask(), |
| 918 | ArrayRef<const SrcMapElem>(), |
| 919 | ArrayRef<const uint8_t>(), // mapping_table. |
| 920 | ArrayRef<const uint8_t>(stack_map_data, stack_map_size), |
| 921 | ArrayRef<const uint8_t>(), // native_gc_map. |
| 922 | ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), |
| 923 | ArrayRef<const LinkerPatch>()); |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 924 | debug::MethodDebugInfo method_debug_info { |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 925 | dex_file, |
| 926 | class_def_idx, |
| 927 | method_idx, |
| 928 | access_flags, |
| 929 | code_item, |
| 930 | false, // deduped. |
| 931 | code_address, |
| 932 | code_address + code_allocator.GetSize(), |
| 933 | &compiled_method |
| 934 | }; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 935 | ArrayRef<const uint8_t> elf_file = debug::WriteDebugElfFileForMethod(method_debug_info); |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 936 | CreateJITCodeEntryForAddress(code_address, |
| 937 | std::unique_ptr<const uint8_t[]>(elf_file.data()), |
| 938 | elf_file.size()); |
| 939 | } |
| 940 | |
Nicolas Geoffray | d28b969 | 2015-11-04 14:36:55 +0000 | [diff] [blame] | 941 | return true; |
| 942 | } |
| 943 | |
Nicolas Geoffray | b34f69a | 2014-03-07 15:28:39 +0000 | [diff] [blame] | 944 | } // namespace art |