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