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