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