blob: 12185866cd19786cd935500de7d14159991e21f4 [file] [log] [blame]
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001/*
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 Gampe53c913b2014-08-12 23:19:23 -070017#include "optimizing_compiler.h"
18
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010019#include <fstream>
David Srbecky5cc349f2015-12-18 15:04:48 +000020#include <memory>
Andreas Gampea0d81af2016-10-27 12:04:57 -070021#include <sstream>
22
Nicolas Geoffray787c3072014-03-17 10:20:19 +000023#include <stdint.h>
24
Vladimir Markob4536b72015-11-24 13:45:23 +000025#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames44b9cf92015-08-19 15:39:06 +010026#include "instruction_simplifier_arm64.h"
27#endif
28
Alexey Frunzee3fb2452016-05-10 16:08:05 -070029#ifdef ART_ENABLE_CODEGEN_mips
Alexey Frunzee3fb2452016-05-10 16:08:05 -070030#include "pc_relative_fixups_mips.h"
31#endif
32
Mark Mendell0616ae02015-04-17 12:49:27 -040033#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Marko0f7dca42015-11-02 14:36:43 +000034#include "pc_relative_fixups_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -040035#endif
36
Mark Mendellee8d9712016-07-12 11:13:15 -040037#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
38#include "x86_memory_gen.h"
39#endif
40
Mathieu Chartiere401d142015-04-22 13:56:20 -070041#include "art_method-inl.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080042#include "base/arena_allocator.h"
Vladimir Markof9f64412015-09-02 14:05:49 +010043#include "base/arena_containers.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000044#include "base/dumpable.h"
Vladimir Markoc90d7c72015-10-06 17:30:45 +000045#include "base/macros.h"
Andreas Gampea0d81af2016-10-27 12:04:57 -070046#include "base/mutex.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000047#include "base/timing_logger.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070048#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049#include "builder.h"
Mingyao Yangb0b051a2016-11-17 09:04:53 -080050#include "cha_guard_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051#include "code_generator.h"
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +000052#include "code_sinking.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000053#include "compiled_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070054#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010055#include "constant_folding.h"
Igor Murashkindd018df2017-08-09 10:38:31 -070056#include "constructor_fence_redundancy_elimination.h"
Roland Levillain75be2832014-10-17 17:02:00 +010057#include "dead_code_elimination.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000058#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000059#include "debug/method_debug_info.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010060#include "dex/verification_results.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000061#include "dex/verified_method.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080062#include "dex_file_types.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000063#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000064#include "driver/compiler_options.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000065#include "driver/dex_compilation_unit.h"
David Brazdil69ba7b72015-06-23 18:27:30 +010066#include "graph_checker.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010067#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010068#include "gvn.h"
Aart Bik22af3be2015-09-10 12:50:58 -070069#include "induction_var_analysis.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000070#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010071#include "instruction_simplifier.h"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030072#include "instruction_simplifier_arm.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080073#include "intrinsics.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000074#include "jit/debugger_interface.h"
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000075#include "jit/jit.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000076#include "jit/jit_code_cache.h"
Nicolas Geoffray01db5f72017-07-19 15:05:49 +010077#include "jit/jit_logger.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000078#include "jni/quick/jni_compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000079#include "licm.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010080#include "linker/linker_patch.h"
xueliang.zhongc239a2b2017-04-27 15:31:37 +010081#include "load_store_analysis.h"
Mingyao Yang8df69d42015-10-22 15:40:58 -070082#include "load_store_elimination.h"
Aart Bik281c6812016-08-26 11:31:48 -070083#include "loop_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084#include "nodes.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000085#include "oat_quick_method_header.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010086#include "prepare_for_register_allocation.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010087#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070088#include "register_allocator_linear_scan.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010089#include "scheduler.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070090#include "select_generator.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010091#include "sharpening.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000092#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000093#include "ssa_builder.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010094#include "ssa_liveness_analysis.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000095#include "ssa_phi_elimination.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010096#include "utils/assembler.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070097#include "verifier/verifier_compiler_binding.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000098
99namespace art {
100
Vladimir Marko3a40bf22016-03-22 16:26:33 +0000101static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
102
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700103static constexpr const char* kPassNameSeparator = "$";
104
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000105/**
106 * Used by the code generator, to allocate the code in a vector.
107 */
108class CodeVectorAllocator FINAL : public CodeAllocator {
109 public:
Vladimir Markof9f64412015-09-02 14:05:49 +0100110 explicit CodeVectorAllocator(ArenaAllocator* arena)
111 : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
112 size_(0) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000113
114 virtual uint8_t* Allocate(size_t size) {
115 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000116 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000117 return &memory_[0];
118 }
119
120 size_t GetSize() const { return size_; }
Vladimir Markof9f64412015-09-02 14:05:49 +0100121 const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000122 uint8_t* GetData() { return memory_.data(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000123
124 private:
Vladimir Markof9f64412015-09-02 14:05:49 +0100125 ArenaVector<uint8_t> memory_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126 size_t size_;
127
128 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
129};
130
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100131/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100132 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +0000133 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100134 */
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700135static constexpr const char kStringFilter[] = "";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100136
David Brazdil69ba7b72015-06-23 18:27:30 +0100137class PassScope;
David Brazdil809658e2015-02-05 11:34:02 +0000138
David Brazdil69ba7b72015-06-23 18:27:30 +0100139class PassObserver : public ValueObject {
David Brazdil5e8b1372015-01-23 14:39:08 +0000140 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100141 PassObserver(HGraph* graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100142 CodeGenerator* codegen,
143 std::ostream* visualizer_output,
Andreas Gampea0d81af2016-10-27 12:04:57 -0700144 CompilerDriver* compiler_driver,
145 Mutex& dump_mutex)
David Brazdil69ba7b72015-06-23 18:27:30 +0100146 : graph_(graph),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000147 cached_method_name_(),
David Brazdil809658e2015-02-05 11:34:02 +0000148 timing_logger_enabled_(compiler_driver->GetDumpPasses()),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000149 timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100150 disasm_info_(graph->GetArena()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700151 visualizer_oss_(),
152 visualizer_output_(visualizer_output),
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000153 visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700154 visualizer_(&visualizer_oss_, graph, *codegen),
155 visualizer_dump_mutex_(dump_mutex),
David Brazdil69ba7b72015-06-23 18:27:30 +0100156 graph_in_bad_state_(false) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700157 if (timing_logger_enabled_ || visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000158 if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700159 timing_logger_enabled_ = visualizer_enabled_ = false;
160 }
161 if (visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000162 visualizer_.PrintHeader(GetMethodName());
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700163 codegen->SetDisassemblyInformation(&disasm_info_);
164 }
David Brazdil62e074f2015-04-07 18:09:37 +0100165 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000166 }
167
David Brazdil69ba7b72015-06-23 18:27:30 +0100168 ~PassObserver() {
David Brazdil5e8b1372015-01-23 14:39:08 +0000169 if (timing_logger_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000170 LOG(INFO) << "TIMINGS " << GetMethodName();
David Brazdil5e8b1372015-01-23 14:39:08 +0000171 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
172 }
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000173 DCHECK(visualizer_oss_.str().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000174 }
175
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000176 void DumpDisassembly() REQUIRES(!visualizer_dump_mutex_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100177 if (visualizer_enabled_) {
178 visualizer_.DumpGraphWithDisassembly();
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000179 FlushVisualizer();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100180 }
181 }
182
David Brazdil69ba7b72015-06-23 18:27:30 +0100183 void SetGraphInBadState() { graph_in_bad_state_ = true; }
184
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000185 const char* GetMethodName() {
186 // PrettyMethod() is expensive, so we delay calling it until we actually have to.
187 if (cached_method_name_.empty()) {
David Sehr709b0702016-10-13 09:12:37 -0700188 cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx());
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000189 }
190 return cached_method_name_.c_str();
191 }
192
David Brazdil5e8b1372015-01-23 14:39:08 +0000193 private:
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000194 void StartPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
Wojciech Staszkiewicze7060702016-08-16 17:31:19 -0700195 VLOG(compiler) << "Starting pass: " << pass_name;
David Brazdil809658e2015-02-05 11:34:02 +0000196 // Dump graph first, then start timer.
197 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100198 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000199 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000200 }
201 if (timing_logger_enabled_) {
202 timing_logger_.StartTiming(pass_name);
203 }
204 }
205
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000206 void FlushVisualizer() REQUIRES(!visualizer_dump_mutex_) {
207 MutexLock mu(Thread::Current(), visualizer_dump_mutex_);
208 *visualizer_output_ << visualizer_oss_.str();
209 visualizer_output_->flush();
210 visualizer_oss_.str("");
211 visualizer_oss_.clear();
212 }
213
214 void EndPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
David Brazdil809658e2015-02-05 11:34:02 +0000215 // Pause timer first, then dump graph.
216 if (timing_logger_enabled_) {
217 timing_logger_.EndTiming();
218 }
219 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100220 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000221 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000222 }
David Brazdil69ba7b72015-06-23 18:27:30 +0100223
224 // Validate the HGraph if running in debug mode.
225 if (kIsDebugBuild) {
226 if (!graph_in_bad_state_) {
David Brazdilbadd8262016-02-02 16:28:56 +0000227 GraphChecker checker(graph_);
228 checker.Run();
229 if (!checker.IsValid()) {
230 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
David Brazdil69ba7b72015-06-23 18:27:30 +0100231 }
232 }
233 }
David Brazdil809658e2015-02-05 11:34:02 +0000234 }
235
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700236 static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
237 // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
238 // empty kStringFilter matching all methods.
239 if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
240 return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
241 }
242
243 // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
244 // warning when the string is empty.
245 constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
246 if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
247 return true;
248 }
249
250 return false;
251 }
252
David Brazdil69ba7b72015-06-23 18:27:30 +0100253 HGraph* const graph_;
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000254
255 std::string cached_method_name_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000256
257 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000258 TimingLogger timing_logger_;
259
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100260 DisassemblyInformation disasm_info_;
261
Andreas Gampea0d81af2016-10-27 12:04:57 -0700262 std::ostringstream visualizer_oss_;
263 std::ostream* visualizer_output_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000264 bool visualizer_enabled_;
265 HGraphVisualizer visualizer_;
Andreas Gampea0d81af2016-10-27 12:04:57 -0700266 Mutex& visualizer_dump_mutex_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000267
David Brazdil69ba7b72015-06-23 18:27:30 +0100268 // Flag to be set by the compiler if the pass failed and the graph is not
269 // expected to validate.
270 bool graph_in_bad_state_;
David Brazdil809658e2015-02-05 11:34:02 +0000271
David Brazdil69ba7b72015-06-23 18:27:30 +0100272 friend PassScope;
273
274 DISALLOW_COPY_AND_ASSIGN(PassObserver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000275};
276
David Brazdil69ba7b72015-06-23 18:27:30 +0100277class PassScope : public ValueObject {
David Brazdil809658e2015-02-05 11:34:02 +0000278 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100279 PassScope(const char *pass_name, PassObserver* pass_observer)
David Brazdil809658e2015-02-05 11:34:02 +0000280 : pass_name_(pass_name),
David Brazdil69ba7b72015-06-23 18:27:30 +0100281 pass_observer_(pass_observer) {
282 pass_observer_->StartPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000283 }
284
David Brazdil69ba7b72015-06-23 18:27:30 +0100285 ~PassScope() {
286 pass_observer_->EndPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000287 }
288
289 private:
290 const char* const pass_name_;
David Brazdil69ba7b72015-06-23 18:27:30 +0100291 PassObserver* const pass_observer_;
David Brazdil809658e2015-02-05 11:34:02 +0000292};
293
Andreas Gampe53c913b2014-08-12 23:19:23 -0700294class OptimizingCompiler FINAL : public Compiler {
295 public:
296 explicit OptimizingCompiler(CompilerDriver* driver);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700297 ~OptimizingCompiler() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700298
Vladimir Markodf739842016-03-23 16:59:07 +0000299 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700300
301 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
302 uint32_t access_flags,
303 InvokeType invoke_type,
304 uint16_t class_def_idx,
305 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000306 Handle<mirror::ClassLoader> class_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700307 const DexFile& dex_file,
308 Handle<mirror::DexCache> dex_cache) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700309
Andreas Gampe53c913b2014-08-12 23:19:23 -0700310 CompiledMethod* JniCompile(uint32_t access_flags,
311 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700312 const DexFile& dex_file,
313 JniOptimizationFlags optimization_flags) const OVERRIDE {
314 return ArtQuickJniCompileMethod(GetCompilerDriver(),
315 access_flags,
316 method_idx,
317 dex_file,
318 optimization_flags);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000319 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700320
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000323 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
324 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
325 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700326
David Brazdilee690a32014-12-01 17:04:16 +0000327 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700328
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000329 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700330
Nicolas Geoffray01db5f72017-07-19 15:05:49 +0100331 bool JitCompile(Thread* self,
332 jit::JitCodeCache* code_cache,
333 ArtMethod* method,
334 bool osr,
335 jit::JitLogger* jit_logger)
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000336 OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000338
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700339 private:
340 void RunOptimizations(HGraph* graph,
341 CodeGenerator* codegen,
342 CompilerDriver* driver,
343 const DexCompilationUnit& dex_compilation_unit,
344 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700345 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700346
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700347 void RunOptimizations(HOptimization* optimizations[],
348 size_t length,
349 PassObserver* pass_observer) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700350
Andreas Gampe53c913b2014-08-12 23:19:23 -0700351 private:
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000352 // Create a 'CompiledMethod' for an optimized graph.
David Brazdil58282f42016-01-14 12:45:10 +0000353 CompiledMethod* Emit(ArenaAllocator* arena,
354 CodeVectorAllocator* code_allocator,
355 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000356 CompilerDriver* driver,
357 const DexFile::CodeItem* item) const;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000358
359 // Try compiling a method and return the code generator used for
360 // compiling it.
361 // This method:
362 // 1) Builds the graph. Returns null if it failed to build it.
David Brazdil58282f42016-01-14 12:45:10 +0000363 // 2) Transforms the graph to SSA. Returns null if it failed.
364 // 3) Runs optimizations on the graph, including register allocator.
365 // 4) Generates code with the `code_allocator` provided.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000366 CodeGenerator* TryCompile(ArenaAllocator* arena,
367 CodeVectorAllocator* code_allocator,
368 const DexFile::CodeItem* code_item,
369 uint32_t access_flags,
370 InvokeType invoke_type,
371 uint16_t class_def_idx,
372 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000373 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000374 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000375 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000376 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000377 bool osr,
378 VariableSizedHandleScope* handles) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000379
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700380 void MaybeRunInliner(HGraph* graph,
381 CodeGenerator* codegen,
382 CompilerDriver* driver,
383 const DexCompilationUnit& dex_compilation_unit,
384 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700385 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700386
387 void RunArchOptimizations(InstructionSet instruction_set,
388 HGraph* graph,
389 CodeGenerator* codegen,
390 PassObserver* pass_observer) const;
391
Calin Juravle2be39e02015-04-21 13:56:34 +0100392 std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100393
Andreas Gampe53c913b2014-08-12 23:19:23 -0700394 std::unique_ptr<std::ostream> visualizer_output_;
395
Andreas Gampea0d81af2016-10-27 12:04:57 -0700396 mutable Mutex dump_mutex_; // To synchronize visualizer writing.
397
Andreas Gampe53c913b2014-08-12 23:19:23 -0700398 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
399};
400
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100401static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
402
403OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
Andreas Gampea0d81af2016-10-27 12:04:57 -0700404 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
405 dump_mutex_("Visualizer dump lock") {}
David Brazdilee690a32014-12-01 17:04:16 +0000406
407void OptimizingCompiler::Init() {
408 // Enable C1visualizer output. Must be done in Init() because the compiler
409 // driver is not fully initialized when passed to the compiler's constructor.
410 CompilerDriver* driver = GetCompilerDriver();
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000411 const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
David Brazdil866c0312015-01-13 21:21:31 +0000412 if (!cfg_file_name.empty()) {
Calin Juravle87000a92015-08-24 15:34:44 +0100413 std::ios_base::openmode cfg_file_mode =
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000414 driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
Calin Juravle87000a92015-08-24 15:34:44 +0100415 visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100416 }
Calin Juravle2be39e02015-04-21 13:56:34 +0100417 if (driver->GetDumpStats()) {
418 compilation_stats_.reset(new OptimizingCompilerStats());
419 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100420}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000421
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000422void OptimizingCompiler::UnInit() const {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000423}
424
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100425OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle2be39e02015-04-21 13:56:34 +0100426 if (compilation_stats_.get() != nullptr) {
427 compilation_stats_->Log();
428 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100429}
430
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000431bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
Vladimir Markodf739842016-03-23 16:59:07 +0000432 const DexFile& dex_file ATTRIBUTE_UNUSED) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000433 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700434}
435
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000436static bool IsInstructionSetSupported(InstructionSet instruction_set) {
Andreas Gampe217d6d32017-09-18 12:48:20 -0700437 return instruction_set == kArm
Calin Juravlecff8cc72015-10-09 12:03:24 +0100438 || instruction_set == kArm64
Andreas Gampe217d6d32017-09-18 12:48:20 -0700439 || instruction_set == kThumb2
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 || instruction_set == kMips
Alexey Frunze4dda3372015-06-01 18:31:49 -0700441 || instruction_set == kMips64
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000442 || instruction_set == kX86
443 || instruction_set == kX86_64;
444}
445
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700446// Strip pass name suffix to get optimization name.
447static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) {
448 size_t pos = pass_name.find(kPassNameSeparator);
449 return pos == std::string::npos ? pass_name : pass_name.substr(0, pos);
450}
451
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700452static HOptimization* BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700453 const std::string& pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700454 ArenaAllocator* arena,
455 HGraph* graph,
456 OptimizingCompilerStats* stats,
457 CodeGenerator* codegen,
458 CompilerDriver* driver,
459 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700460 VariableSizedHandleScope* handles,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700461 SideEffectsAnalysis* most_recent_side_effects,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100462 HInductionVarAnalysis* most_recent_induction,
463 LoadStoreAnalysis* most_recent_lsa) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700464 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700465 if (opt_name == BoundsCheckElimination::kBoundsCheckEliminationPassName) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700466 CHECK(most_recent_side_effects != nullptr && most_recent_induction != nullptr);
467 return new (arena) BoundsCheckElimination(graph,
468 *most_recent_side_effects,
469 most_recent_induction);
470 } else if (opt_name == GVNOptimization::kGlobalValueNumberingPassName) {
471 CHECK(most_recent_side_effects != nullptr);
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700472 return new (arena) GVNOptimization(graph, *most_recent_side_effects, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700473 } else if (opt_name == HConstantFolding::kConstantFoldingPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700474 return new (arena) HConstantFolding(graph, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700475 } else if (opt_name == HDeadCodeElimination::kDeadCodeEliminationPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700476 return new (arena) HDeadCodeElimination(graph, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700477 } else if (opt_name == HInliner::kInlinerPassName) {
478 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
479 return new (arena) HInliner(graph, // outer_graph
480 graph, // outermost_graph
481 codegen,
482 dex_compilation_unit, // outer_compilation_unit
483 dex_compilation_unit, // outermost_compilation_unit
484 driver,
485 handles,
486 stats,
487 number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000488 /* total_number_of_instructions */ 0,
489 /* parent */ nullptr);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700490 } else if (opt_name == HSharpening::kSharpeningPassName) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000491 return new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver, handles);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700492 } else if (opt_name == HSelectGenerator::kSelectGeneratorPassName) {
Mads Ager16e52892017-07-14 13:11:37 +0200493 return new (arena) HSelectGenerator(graph, handles, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700494 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
495 return new (arena) HInductionVarAnalysis(graph);
496 } else if (opt_name == InstructionSimplifier::kInstructionSimplifierPassName) {
Vladimir Marko65979462017-05-19 17:25:12 +0100497 return new (arena) InstructionSimplifier(graph, codegen, driver, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700498 } else if (opt_name == IntrinsicsRecognizer::kIntrinsicsRecognizerPassName) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100499 return new (arena) IntrinsicsRecognizer(graph, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700500 } else if (opt_name == LICM::kLoopInvariantCodeMotionPassName) {
501 CHECK(most_recent_side_effects != nullptr);
502 return new (arena) LICM(graph, *most_recent_side_effects, stats);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100503 } else if (opt_name == LoadStoreAnalysis::kLoadStoreAnalysisPassName) {
504 return new (arena) LoadStoreAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700505 } else if (opt_name == LoadStoreElimination::kLoadStoreEliminationPassName) {
506 CHECK(most_recent_side_effects != nullptr);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100507 CHECK(most_recent_lsa != nullptr);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700508 return
509 new (arena) LoadStoreElimination(graph, *most_recent_side_effects, *most_recent_lsa, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700510 } else if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
511 return new (arena) SideEffectsAnalysis(graph);
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100512 } else if (opt_name == HLoopOptimization::kLoopOptimizationPassName) {
Aart Bikb92cc332017-09-06 15:53:17 -0700513 return new (arena) HLoopOptimization(graph, driver, most_recent_induction, stats);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800514 } else if (opt_name == CHAGuardOptimization::kCHAGuardOptimizationPassName) {
515 return new (arena) CHAGuardOptimization(graph);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000516 } else if (opt_name == CodeSinking::kCodeSinkingPassName) {
517 return new (arena) CodeSinking(graph, stats);
Igor Murashkindd018df2017-08-09 10:38:31 -0700518 } else if (opt_name == ConstructorFenceRedundancyElimination::kPassName) {
519 return new (arena) ConstructorFenceRedundancyElimination(graph, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700520#ifdef ART_ENABLE_CODEGEN_arm
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700521 } else if (opt_name == arm::InstructionSimplifierArm::kInstructionSimplifierArmPassName) {
522 return new (arena) arm::InstructionSimplifierArm(graph, stats);
523#endif
524#ifdef ART_ENABLE_CODEGEN_arm64
525 } else if (opt_name == arm64::InstructionSimplifierArm64::kInstructionSimplifierArm64PassName) {
526 return new (arena) arm64::InstructionSimplifierArm64(graph, stats);
527#endif
528#ifdef ART_ENABLE_CODEGEN_mips
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700529 } else if (opt_name == mips::PcRelativeFixups::kPcRelativeFixupsMipsPassName) {
530 return new (arena) mips::PcRelativeFixups(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700531#endif
532#ifdef ART_ENABLE_CODEGEN_x86
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700533 } else if (opt_name == x86::PcRelativeFixups::kPcRelativeFixupsX86PassName) {
534 return new (arena) x86::PcRelativeFixups(graph, codegen, stats);
535 } else if (opt_name == x86::X86MemoryOperandGeneration::kX86MemoryOperandGenerationPassName) {
536 return new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700537#endif
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700538 }
539 return nullptr;
540}
541
542static ArenaVector<HOptimization*> BuildOptimizations(
543 const std::vector<std::string>& pass_names,
544 ArenaAllocator* arena,
545 HGraph* graph,
546 OptimizingCompilerStats* stats,
547 CodeGenerator* codegen,
548 CompilerDriver* driver,
549 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700550 VariableSizedHandleScope* handles) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700551 // Few HOptimizations constructors require SideEffectsAnalysis or HInductionVarAnalysis
552 // instances. This method assumes that each of them expects the nearest instance preceeding it
553 // in the pass name list.
554 SideEffectsAnalysis* most_recent_side_effects = nullptr;
555 HInductionVarAnalysis* most_recent_induction = nullptr;
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100556 LoadStoreAnalysis* most_recent_lsa = nullptr;
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700557 ArenaVector<HOptimization*> ret(arena->Adapter());
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700558 for (const std::string& pass_name : pass_names) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700559 HOptimization* opt = BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700560 pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700561 arena,
562 graph,
563 stats,
564 codegen,
565 driver,
566 dex_compilation_unit,
567 handles,
568 most_recent_side_effects,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100569 most_recent_induction,
570 most_recent_lsa);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700571 CHECK(opt != nullptr) << "Couldn't build optimization: \"" << pass_name << "\"";
572 ret.push_back(opt);
573
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700574 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700575 if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
576 most_recent_side_effects = down_cast<SideEffectsAnalysis*>(opt);
577 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
578 most_recent_induction = down_cast<HInductionVarAnalysis*>(opt);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100579 } else if (opt_name == LoadStoreAnalysis::kLoadStoreAnalysisPassName) {
580 most_recent_lsa = down_cast<LoadStoreAnalysis*>(opt);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700581 }
582 }
583 return ret;
584}
585
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700586void OptimizingCompiler::RunOptimizations(HOptimization* optimizations[],
587 size_t length,
588 PassObserver* pass_observer) const {
Calin Juravle10e244f2015-01-26 18:54:32 +0000589 for (size_t i = 0; i < length; ++i) {
David Brazdil69ba7b72015-06-23 18:27:30 +0100590 PassScope scope(optimizations[i]->GetPassName(), pass_observer);
591 optimizations[i]->Run();
Calin Juravle10e244f2015-01-26 18:54:32 +0000592 }
593}
594
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700595void OptimizingCompiler::MaybeRunInliner(HGraph* graph,
596 CodeGenerator* codegen,
597 CompilerDriver* driver,
598 const DexCompilationUnit& dex_compilation_unit,
599 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700600 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700601 OptimizingCompilerStats* stats = compilation_stats_.get();
Calin Juravleec748352015-07-29 13:52:12 +0100602 const CompilerOptions& compiler_options = driver->GetCompilerOptions();
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000603 bool should_inline = (compiler_options.GetInlineMaxCodeUnits() > 0);
Calin Juravleec748352015-07-29 13:52:12 +0100604 if (!should_inline) {
605 return;
606 }
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000607 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000608 HInliner* inliner = new (graph->GetArena()) HInliner(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700609 graph, // outer_graph
610 graph, // outermost_graph
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000611 codegen,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700612 dex_compilation_unit, // outer_compilation_unit
613 dex_compilation_unit, // outermost_compilation_unit
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000614 driver,
615 handles,
616 stats,
617 number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000618 /* total_number_of_instructions */ 0,
619 /* parent */ nullptr);
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000620 HOptimization* optimizations[] = { inliner };
Calin Juravleec748352015-07-29 13:52:12 +0100621
622 RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
623}
624
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700625void OptimizingCompiler::RunArchOptimizations(InstructionSet instruction_set,
626 HGraph* graph,
627 CodeGenerator* codegen,
628 PassObserver* pass_observer) const {
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700629 UNUSED(codegen); // To avoid compilation error when compiling for svelte
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700630 OptimizingCompilerStats* stats = compilation_stats_.get();
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100631 ArenaAllocator* arena = graph->GetArena();
632 switch (instruction_set) {
Artem Serov2bbc9532016-10-21 11:51:50 +0100633#if defined(ART_ENABLE_CODEGEN_arm)
Vladimir Markob4536b72015-11-24 13:45:23 +0000634 case kThumb2:
635 case kArm: {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300636 arm::InstructionSimplifierArm* simplifier =
637 new (arena) arm::InstructionSimplifierArm(graph, stats);
Artem Serov328429f2016-07-06 16:23:04 +0100638 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700639 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
xueliang.zhongf7caf682017-03-01 16:07:02 +0000640 HInstructionScheduling* scheduling =
641 new (arena) HInstructionScheduling(graph, instruction_set, codegen);
Vladimir Markob4536b72015-11-24 13:45:23 +0000642 HOptimization* arm_optimizations[] = {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300643 simplifier,
Artem Serov328429f2016-07-06 16:23:04 +0100644 side_effects,
645 gvn,
xueliang.zhongf7caf682017-03-01 16:07:02 +0000646 scheduling,
Vladimir Markob4536b72015-11-24 13:45:23 +0000647 };
648 RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
649 break;
650 }
651#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100652#ifdef ART_ENABLE_CODEGEN_arm64
653 case kArm64: {
654 arm64::InstructionSimplifierArm64* simplifier =
655 new (arena) arm64::InstructionSimplifierArm64(graph, stats);
656 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700657 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100658 HInstructionScheduling* scheduling =
659 new (arena) HInstructionScheduling(graph, instruction_set);
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100660 HOptimization* arm64_optimizations[] = {
661 simplifier,
662 side_effects,
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100663 gvn,
664 scheduling,
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100665 };
666 RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
667 break;
668 }
669#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700670#ifdef ART_ENABLE_CODEGEN_mips
671 case kMips: {
Lena Djokicb8e9c352017-09-20 15:11:13 +0200672 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
673 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Aart Bik9e147872017-09-27 11:25:01 -0700674 mips::PcRelativeFixups* pc_relative_fixups =
675 new (arena) mips::PcRelativeFixups(graph, codegen, stats);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700676 HOptimization* mips_optimizations[] = {
Lena Djokicb8e9c352017-09-20 15:11:13 +0200677 side_effects,
678 gvn,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700679 pc_relative_fixups,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700680 };
681 RunOptimizations(mips_optimizations, arraysize(mips_optimizations), pass_observer);
682 break;
683 }
684#endif
Lena Djokicb8e9c352017-09-20 15:11:13 +0200685#ifdef ART_ENABLE_CODEGEN_mips64
686 case kMips64: {
687 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
688 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
689 HOptimization* mips64_optimizations[] = {
690 side_effects,
691 gvn,
692 };
693 RunOptimizations(mips64_optimizations, arraysize(mips64_optimizations), pass_observer);
694 break;
695 }
696#endif
Mark Mendell0616ae02015-04-17 12:49:27 -0400697#ifdef ART_ENABLE_CODEGEN_x86
698 case kX86: {
Aart Bik9e147872017-09-27 11:25:01 -0700699 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
700 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Aart Bikd1c40452016-03-02 16:06:13 -0800701 x86::PcRelativeFixups* pc_relative_fixups =
702 new (arena) x86::PcRelativeFixups(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400703 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700704 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendell0616ae02015-04-17 12:49:27 -0400705 HOptimization* x86_optimizations[] = {
Aart Bik9e147872017-09-27 11:25:01 -0700706 side_effects,
707 gvn,
Mark Mendellee8d9712016-07-12 11:13:15 -0400708 pc_relative_fixups,
709 memory_gen
Mark Mendell0616ae02015-04-17 12:49:27 -0400710 };
711 RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
712 break;
713 }
714#endif
Mark Mendellee8d9712016-07-12 11:13:15 -0400715#ifdef ART_ENABLE_CODEGEN_x86_64
716 case kX86_64: {
Aart Bik9e147872017-09-27 11:25:01 -0700717 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
718 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Mark Mendellee8d9712016-07-12 11:13:15 -0400719 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700720 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400721 HOptimization* x86_64_optimizations[] = {
Aart Bik9e147872017-09-27 11:25:01 -0700722 side_effects,
723 gvn,
Mark Mendellee8d9712016-07-12 11:13:15 -0400724 memory_gen
725 };
726 RunOptimizations(x86_64_optimizations, arraysize(x86_64_optimizations), pass_observer);
727 break;
728 }
729#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100730 default:
731 break;
732 }
733}
734
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000735NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects.
736static void AllocateRegisters(HGraph* graph,
737 CodeGenerator* codegen,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700738 PassObserver* pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -0700739 RegisterAllocator::Strategy strategy,
740 OptimizingCompilerStats* stats) {
Mingyao Yang700347e2016-03-02 14:59:32 -0800741 {
742 PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
743 pass_observer);
Igor Murashkin6ef45672017-08-08 13:59:55 -0700744 PrepareForRegisterAllocation(graph, stats).Run();
Mingyao Yang700347e2016-03-02 14:59:32 -0800745 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000746 SsaLivenessAnalysis liveness(graph, codegen);
747 {
748 PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
749 liveness.Analyze();
750 }
751 {
752 PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700753 RegisterAllocator::Create(graph->GetArena(), codegen, liveness, strategy)->AllocateRegisters();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000754 }
755}
756
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700757void OptimizingCompiler::RunOptimizations(HGraph* graph,
758 CodeGenerator* codegen,
759 CompilerDriver* driver,
760 const DexCompilationUnit& dex_compilation_unit,
761 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700762 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700763 OptimizingCompilerStats* stats = compilation_stats_.get();
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100764 ArenaAllocator* arena = graph->GetArena();
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700765 if (driver->GetCompilerOptions().GetPassesToRun() != nullptr) {
766 ArenaVector<HOptimization*> optimizations = BuildOptimizations(
767 *driver->GetCompilerOptions().GetPassesToRun(),
768 arena,
769 graph,
770 stats,
771 codegen,
772 driver,
773 dex_compilation_unit,
774 handles);
775 RunOptimizations(&optimizations[0], optimizations.size(), pass_observer);
776 return;
777 }
778
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100779 HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700780 graph, stats, "dead_code_elimination$initial");
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100781 HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
Aart Bik2767f4b2016-10-28 15:03:53 -0700782 graph, stats, "dead_code_elimination$after_inlining");
783 HDeadCodeElimination* dce3 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700784 graph, stats, "dead_code_elimination$final");
Andreas Gampeca620d72016-11-08 08:09:33 -0800785 HConstantFolding* fold1 = new (arena) HConstantFolding(graph, "constant_folding");
Vladimir Marko65979462017-05-19 17:25:12 +0100786 InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(
787 graph, codegen, driver, stats);
Mads Ager16e52892017-07-14 13:11:37 +0200788 HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, handles, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700789 HConstantFolding* fold2 = new (arena) HConstantFolding(
790 graph, "constant_folding$after_inlining");
791 HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding$after_bce");
Aart Bik92685a82017-03-06 11:13:43 -0800792 SideEffectsAnalysis* side_effects1 = new (arena) SideEffectsAnalysis(
793 graph, "side_effects$before_gvn");
794 SideEffectsAnalysis* side_effects2 = new (arena) SideEffectsAnalysis(
795 graph, "side_effects$before_lse");
796 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects1);
797 LICM* licm = new (arena) LICM(graph, *side_effects1, stats);
Aart Bik22af3be2015-09-10 12:50:58 -0700798 HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
Aart Bik92685a82017-03-06 11:13:43 -0800799 BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects1, induction);
Aart Bikb92cc332017-09-06 15:53:17 -0700800 HLoopOptimization* loop = new (arena) HLoopOptimization(graph, driver, induction, stats);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100801 LoadStoreAnalysis* lsa = new (arena) LoadStoreAnalysis(graph);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700802 LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects2, *lsa, stats);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000803 HSharpening* sharpening = new (arena) HSharpening(
804 graph, codegen, dex_compilation_unit, driver, handles);
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100805 InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100806 graph, codegen, driver, stats, "instruction_simplifier$after_inlining");
David Brazdil4833f5a2015-12-16 10:37:39 +0000807 InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100808 graph, codegen, driver, stats, "instruction_simplifier$after_bce");
Aart Bik639cc8c2016-10-18 13:03:31 -0700809 InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100810 graph, codegen, driver, stats, "instruction_simplifier$before_codegen");
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100811 IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, stats);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800812 CHAGuardOptimization* cha_guard = new (arena) CHAGuardOptimization(graph);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000813 CodeSinking* code_sinking = new (arena) CodeSinking(graph, stats);
Igor Murashkindd018df2017-08-09 10:38:31 -0700814 ConstructorFenceRedundancyElimination* cfre =
815 new (arena) ConstructorFenceRedundancyElimination(graph, stats);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800816
Calin Juravleec748352015-07-29 13:52:12 +0100817 HOptimization* optimizations1[] = {
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100818 intrinsics,
David Brazdil4833f5a2015-12-16 10:37:39 +0000819 sharpening,
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100820 fold1,
821 simplify1,
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100822 dce1,
Calin Juravleec748352015-07-29 13:52:12 +0100823 };
Calin Juravleec748352015-07-29 13:52:12 +0100824 RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
825
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700826 MaybeRunInliner(graph, codegen, driver, dex_compilation_unit, pass_observer, handles);
David Brazdil95177982015-10-30 12:56:58 -0500827
David Brazdil8993caf2015-12-07 10:04:40 +0000828 HOptimization* optimizations2[] = {
David Brazdil74eb1b22015-12-14 11:44:01 +0000829 // SelectGenerator depends on the InstructionSimplifier removing
David Brazdil8993caf2015-12-07 10:04:40 +0000830 // redundant suspend checks to recognize empty blocks.
David Brazdil74eb1b22015-12-14 11:44:01 +0000831 select_generator,
David Brazdil8993caf2015-12-07 10:04:40 +0000832 fold2, // TODO: if we don't inline we can also skip fold2.
Aart Bik639cc8c2016-10-18 13:03:31 -0700833 simplify2,
Aart Bik2767f4b2016-10-28 15:03:53 -0700834 dce2,
Aart Bik92685a82017-03-06 11:13:43 -0800835 side_effects1,
David Brazdil8993caf2015-12-07 10:04:40 +0000836 gvn,
837 licm,
838 induction,
839 bce,
Aart Bik281c6812016-08-26 11:31:48 -0700840 loop,
David Brazdil8993caf2015-12-07 10:04:40 +0000841 fold3, // evaluates code generated by dynamic bce
Aart Bik639cc8c2016-10-18 13:03:31 -0700842 simplify3,
Aart Bik92685a82017-03-06 11:13:43 -0800843 side_effects2,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100844 lsa,
David Brazdil8993caf2015-12-07 10:04:40 +0000845 lse,
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800846 cha_guard,
Aart Bik2767f4b2016-10-28 15:03:53 -0700847 dce3,
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000848 code_sinking,
David Brazdil8993caf2015-12-07 10:04:40 +0000849 // The codegen has a few assumptions that only the instruction simplifier
850 // can satisfy. For example, the code generator does not expect to see a
851 // HTypeConversion from a type to the same type.
Aart Bik639cc8c2016-10-18 13:03:31 -0700852 simplify4,
Igor Murashkindd018df2017-08-09 10:38:31 -0700853 cfre, // Eliminate constructor fences after code sinking to avoid
854 // complicated sinking logic to split a fence with many inputs.
David Brazdil8993caf2015-12-07 10:04:40 +0000855 };
856 RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
David Brazdilbbd733e2015-08-18 17:48:17 +0100857
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700858 RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, pass_observer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000859}
860
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100861static ArenaVector<linker::LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
862 ArenaVector<linker::LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
Vladimir Marko58155012015-08-19 12:49:41 +0000863 codegen->EmitLinkerPatches(&linker_patches);
864
865 // Sort patches by literal offset. Required for .oat_patches encoding.
866 std::sort(linker_patches.begin(), linker_patches.end(),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100867 [](const linker::LinkerPatch& lhs, const linker::LinkerPatch& rhs) {
Vladimir Marko58155012015-08-19 12:49:41 +0000868 return lhs.LiteralOffset() < rhs.LiteralOffset();
869 });
870
871 return linker_patches;
872}
873
David Brazdil58282f42016-01-14 12:45:10 +0000874CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
875 CodeVectorAllocator* code_allocator,
876 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000877 CompilerDriver* compiler_driver,
878 const DexFile::CodeItem* code_item) const {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100879 ArenaVector<linker::LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
Vladimir Markof9f64412015-09-02 14:05:49 +0100880 ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700881 ArenaVector<uint8_t> method_info(arena->Adapter(kArenaAllocStackMaps));
882 size_t stack_map_size = 0;
883 size_t method_info_size = 0;
884 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
885 stack_map.resize(stack_map_size);
886 method_info.resize(method_info_size);
887 codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()),
888 MemoryRegion(method_info.data(), method_info.size()),
889 *code_item);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000890
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100891 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000892 compiler_driver,
893 codegen->GetInstructionSet(),
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000894 ArrayRef<const uint8_t>(code_allocator->GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000895 // Follow Quick's behavior and set the frame size to zero if it is
896 // considered "empty" (see the definition of
897 // art::CodeGenerator::HasEmptyFrame).
898 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000899 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000900 codegen->GetFpuSpillMask(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700901 ArrayRef<const uint8_t>(method_info),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100902 ArrayRef<const uint8_t>(stack_map),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100903 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100904 ArrayRef<const linker::LinkerPatch>(linker_patches));
Mathieu Chartiered150002015-08-28 11:16:54 -0700905
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100906 return compiled_method;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000907}
908
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000909CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
910 CodeVectorAllocator* code_allocator,
911 const DexFile::CodeItem* code_item,
912 uint32_t access_flags,
913 InvokeType invoke_type,
914 uint16_t class_def_idx,
915 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000916 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000917 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000918 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000919 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000920 bool osr,
921 VariableSizedHandleScope* handles) const {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700922 MaybeRecordStat(compilation_stats_.get(),
923 MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000924 CompilerDriver* compiler_driver = GetCompilerDriver();
925 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Calin Juravlecff8cc72015-10-09 12:03:24 +0100926
Roland Levillain3b359c72015-11-17 19:35:12 +0000927 // Always use the Thumb-2 assembler: some runtime functionality
928 // (like implicit stack overflow checks) assume Thumb-2.
Artem Serovba6b6792016-08-15 14:22:07 +0100929 DCHECK_NE(instruction_set, kArm);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100930
931 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000932 if (!IsInstructionSetSupported(instruction_set)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700933 MaybeRecordStat(compilation_stats_.get(),
934 MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100935 return nullptr;
936 }
937
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000938 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700939 MaybeRecordStat(compilation_stats_.get(),
940 MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000941 return nullptr;
942 }
943
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000944 // Implementation of the space filter: do not compile a code item whose size in
Nicolas Geoffray432bf3d2015-07-17 11:11:09 +0100945 // code units is bigger than 128.
946 static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000947 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000948 if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000949 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700950 MaybeRecordStat(compilation_stats_.get(),
951 MethodCompilationStat::kNotCompiledSpaceFilter);
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000952 return nullptr;
953 }
954
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100955 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000956 DexCompilationUnit dex_compilation_unit(
Vladimir Markodf739842016-03-23 16:59:07 +0000957 class_loader,
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100958 class_linker,
Vladimir Markodf739842016-03-23 16:59:07 +0000959 dex_file,
960 code_item,
961 class_def_idx,
962 method_idx,
963 access_flags,
964 /* verified_method */ nullptr,
965 dex_cache);
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000966
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000967 HGraph* graph = new (arena) HGraph(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000968 arena,
969 dex_file,
970 method_idx,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000971 compiler_driver->GetInstructionSet(),
972 kInvalidInvokeType,
973 compiler_driver->GetCompilerOptions().GetDebuggable(),
974 osr);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000975
David Brazdilbadd8262016-02-02 16:28:56 +0000976 const uint8_t* interpreter_metadata = nullptr;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000977 if (method == nullptr) {
David Brazdilbadd8262016-02-02 16:28:56 +0000978 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000979 method = compiler_driver->ResolveMethod(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000980 soa, dex_cache, class_loader, &dex_compilation_unit, method_idx, invoke_type);
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000981 }
982 // For AOT compilation, we may not get a method, for example if its class is erroneous.
983 // JIT should always have a method.
984 DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr);
985 if (method != nullptr) {
986 graph->SetArtMethod(method);
987 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100988 interpreter_metadata = method->GetQuickenedInfo(class_linker->GetImagePointerSize());
David Brazdilbadd8262016-02-02 16:28:56 +0000989 }
990
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000991 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000992 CodeGenerator::Create(graph,
993 instruction_set,
994 *compiler_driver->GetInstructionSetFeatures(),
Calin Juravle2ae48182016-03-16 14:05:09 +0000995 compiler_driver->GetCompilerOptions(),
996 compilation_stats_.get()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000997 if (codegen.get() == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700998 MaybeRecordStat(compilation_stats_.get(),
999 MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001000 return nullptr;
1001 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001002 codegen->GetAssembler()->cfi().SetEnabled(
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001003 compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001004
David Brazdil69ba7b72015-06-23 18:27:30 +01001005 PassObserver pass_observer(graph,
David Brazdil69ba7b72015-06-23 18:27:30 +01001006 codegen.get(),
1007 visualizer_output_.get(),
Andreas Gampea0d81af2016-10-27 12:04:57 -07001008 compiler_driver,
1009 dump_mutex_);
David Brazdil5e8b1372015-01-23 14:39:08 +00001010
David Brazdil809658e2015-02-05 11:34:02 +00001011 {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001012 VLOG(compiler) << "Building " << pass_observer.GetMethodName();
1013 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
1014 HGraphBuilder builder(graph,
1015 &dex_compilation_unit,
1016 &dex_compilation_unit,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001017 compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001018 codegen.get(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001019 compilation_stats_.get(),
1020 interpreter_metadata,
1021 dex_cache,
1022 handles);
1023 GraphAnalysisResult result = builder.BuildGraph();
1024 if (result != kAnalysisSuccess) {
1025 switch (result) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001026 case kAnalysisSkipped: {
1027 MaybeRecordStat(compilation_stats_.get(),
1028 MethodCompilationStat::kNotCompiledSkipped);
1029 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001030 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001031 case kAnalysisInvalidBytecode: {
1032 MaybeRecordStat(compilation_stats_.get(),
1033 MethodCompilationStat::kNotCompiledInvalidBytecode);
1034 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001035 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001036 case kAnalysisFailThrowCatchLoop: {
1037 MaybeRecordStat(compilation_stats_.get(),
1038 MethodCompilationStat::kNotCompiledThrowCatchLoop);
1039 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001040 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001041 case kAnalysisFailAmbiguousArrayOp: {
1042 MaybeRecordStat(compilation_stats_.get(),
1043 MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
1044 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001045 break;
1046 case kAnalysisSuccess:
1047 UNREACHABLE();
David Brazdil809658e2015-02-05 11:34:02 +00001048 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001049 pass_observer.SetGraphInBadState();
1050 return nullptr;
Nicolas Geoffrayf5370122014-12-02 11:51:19 +00001051 }
David Brazdilbadd8262016-02-02 16:28:56 +00001052 }
Vladimir Markof9f64412015-09-02 14:05:49 +01001053
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001054 RunOptimizations(graph,
1055 codegen.get(),
1056 compiler_driver,
1057 dex_compilation_unit,
1058 &pass_observer,
1059 handles);
1060
1061 RegisterAllocator::Strategy regalloc_strategy =
1062 compiler_options.GetRegisterAllocationStrategy();
Igor Murashkin1e065a52017-08-09 13:20:34 -07001063 AllocateRegisters(graph,
1064 codegen.get(),
1065 &pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -07001066 regalloc_strategy,
1067 compilation_stats_.get());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001068
1069 codegen->Compile(code_allocator);
1070 pass_observer.DumpDisassembly();
1071
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001072 return codegen.release();
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001073}
1074
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001075CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
1076 uint32_t access_flags,
1077 InvokeType invoke_type,
1078 uint16_t class_def_idx,
1079 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001080 Handle<mirror::ClassLoader> jclass_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001081 const DexFile& dex_file,
1082 Handle<mirror::DexCache> dex_cache) const {
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001083 CompilerDriver* compiler_driver = GetCompilerDriver();
1084 CompiledMethod* method = nullptr;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001085 DCHECK(Runtime::Current()->IsAotCompiler());
1086 const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
1087 DCHECK(!verified_method->HasRuntimeThrow());
1088 if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001089 || verifier::CanCompilerHandleVerificationFailure(
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001090 verified_method->GetEncounteredVerificationFailures())) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001091 ArenaAllocator arena(Runtime::Current()->GetArenaPool());
1092 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001093 std::unique_ptr<CodeGenerator> codegen;
1094 {
1095 ScopedObjectAccess soa(Thread::Current());
1096 VariableSizedHandleScope handles(soa.Self());
1097 // Go to native so that we don't block GC during compilation.
1098 ScopedThreadSuspension sts(soa.Self(), kNative);
1099 codegen.reset(
1100 TryCompile(&arena,
1101 &code_allocator,
1102 code_item,
1103 access_flags,
1104 invoke_type,
1105 class_def_idx,
1106 method_idx,
1107 jclass_loader,
1108 dex_file,
1109 dex_cache,
1110 nullptr,
1111 /* osr */ false,
1112 &handles));
1113 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001114 if (codegen.get() != nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001115 MaybeRecordStat(compilation_stats_.get(),
1116 MethodCompilationStat::kCompiled);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001117 method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001118
1119 if (kArenaAllocatorCountAllocations) {
1120 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1121 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001122 LOG(INFO) << dex_file.PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001123 }
1124 }
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001125 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001126 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001127 MethodCompilationStat method_stat;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001128 if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001129 method_stat = MethodCompilationStat::kNotCompiledVerifyAtRuntime;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001130 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001131 method_stat = MethodCompilationStat::kNotCompiledVerificationError;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001132 }
Igor Murashkin1e065a52017-08-09 13:20:34 -07001133 MaybeRecordStat(compilation_stats_.get(), method_stat);
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001134 }
1135
Calin Juravlecff8cc72015-10-09 12:03:24 +01001136 if (kIsDebugBuild &&
1137 IsCompilingWithCoreImage() &&
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001138 IsInstructionSetSupported(compiler_driver->GetInstructionSet())) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001139 // For testing purposes, we put a special marker on method names
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001140 // that should be compiled with this compiler (when the
1141 // instruction set is supported). This makes sure we're not
Roland Levillain0d5a2812015-11-13 10:07:31 +00001142 // regressing.
David Sehr709b0702016-10-13 09:12:37 -07001143 std::string method_name = dex_file.PrettyMethod(method_idx);
Calin Juravle09b1d6f2015-10-07 12:08:54 +01001144 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
1145 DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
1146 }
1147
Nicolas Geoffray12be74e2015-03-30 13:29:08 +01001148 return method;
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001149}
1150
Andreas Gampe53c913b2014-08-12 23:19:23 -07001151Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
1152 return new OptimizingCompiler(driver);
1153}
1154
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001155bool IsCompilingWithCoreImage() {
1156 const std::string& image = Runtime::Current()->GetImageLocation();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001157 return CompilerDriver::IsCoreImageFilename(image);
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001158}
1159
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00001160bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) {
1161 // Note: the runtime is null only for unit testing.
1162 return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler();
1163}
1164
1165bool CanEncodeInlinedMethodInStackMap(const DexFile& caller_dex_file, ArtMethod* callee) {
1166 if (!Runtime::Current()->IsAotCompiler()) {
1167 // JIT can always encode methods in stack maps.
1168 return true;
1169 }
1170 if (IsSameDexFile(caller_dex_file, *callee->GetDexFile())) {
1171 return true;
1172 }
1173 // TODO(ngeoffray): Support more AOT cases for inlining:
1174 // - methods in multidex
1175 // - methods in boot image for on-device non-PIC compilation.
1176 return false;
1177}
1178
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001179bool OptimizingCompiler::JitCompile(Thread* self,
1180 jit::JitCodeCache* code_cache,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001181 ArtMethod* method,
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001182 bool osr,
1183 jit::JitLogger* jit_logger) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001184 StackHandleScope<3> hs(self);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001185 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1186 method->GetDeclaringClass()->GetClassLoader()));
1187 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001188 DCHECK(method->IsCompilable());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001189
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001190 const DexFile* dex_file = method->GetDexFile();
1191 const uint16_t class_def_idx = method->GetClassDefIndex();
1192 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1193 const uint32_t method_idx = method->GetDexMethodIndex();
1194 const uint32_t access_flags = method->GetAccessFlags();
1195 const InvokeType invoke_type = method->GetInvokeType();
1196
Nicolas Geoffray25e04562016-03-01 13:17:58 +00001197 ArenaAllocator arena(Runtime::Current()->GetJitArenaPool());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001198 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001199 VariableSizedHandleScope handles(self);
1200
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001201 std::unique_ptr<CodeGenerator> codegen;
1202 {
1203 // Go to native so that we don't block GC during compilation.
1204 ScopedThreadSuspension sts(self, kNative);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001205 codegen.reset(
1206 TryCompile(&arena,
1207 &code_allocator,
1208 code_item,
1209 access_flags,
1210 invoke_type,
1211 class_def_idx,
1212 method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001213 class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001214 *dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001215 dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +00001216 method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001217 osr,
1218 &handles));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001219 if (codegen.get() == nullptr) {
1220 return false;
1221 }
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001222
1223 if (kArenaAllocatorCountAllocations) {
1224 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1225 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001226 LOG(INFO) << dex_file->PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001227 }
1228 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001229 }
1230
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001231 size_t stack_map_size = 0;
1232 size_t method_info_size = 0;
1233 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001234 size_t number_of_roots = codegen->GetNumberOfJitRoots();
1235 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1236 // We allocate an object array to ensure the JIT roots that we will collect in EmitJitRoots
1237 // will be visible by the GC between EmitLiterals and CommitCode. Once CommitCode is
1238 // executed, this array is not needed.
1239 Handle<mirror::ObjectArray<mirror::Object>> roots(
1240 hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc(
1241 self, class_linker->GetClassRoot(ClassLinker::kObjectArrayClass), number_of_roots)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001242 if (roots == nullptr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001243 // Out of memory, just clear the exception to avoid any Java exception uncaught problems.
1244 DCHECK(self->IsExceptionPending());
1245 self->ClearException();
1246 return false;
1247 }
1248 uint8_t* stack_map_data = nullptr;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001249 uint8_t* method_info_data = nullptr;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001250 uint8_t* roots_data = nullptr;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001251 uint32_t data_size = code_cache->ReserveData(self,
1252 stack_map_size,
1253 method_info_size,
1254 number_of_roots,
1255 method,
1256 &stack_map_data,
1257 &method_info_data,
1258 &roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001259 if (stack_map_data == nullptr || roots_data == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001260 return false;
1261 }
Igor Murashkin1e065a52017-08-09 13:20:34 -07001262 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiled);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001263 codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size),
1264 MemoryRegion(method_info_data, method_info_size),
1265 *code_item);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00001266 codegen->EmitJitRoots(code_allocator.GetData(), roots, roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001267
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001268 const void* code = code_cache->CommitCode(
1269 self,
1270 method,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001271 stack_map_data,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001272 method_info_data,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001273 roots_data,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001274 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
1275 codegen->GetCoreSpillMask(),
1276 codegen->GetFpuSpillMask(),
1277 code_allocator.GetMemory().data(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001278 code_allocator.GetSize(),
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001279 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001280 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -07001281 roots,
1282 codegen->GetGraph()->HasShouldDeoptimizeFlag(),
1283 codegen->GetGraph()->GetCHASingleImplementationList());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001284
1285 if (code == nullptr) {
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001286 code_cache->ClearData(self, stack_map_data, roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001287 return false;
1288 }
1289
David Srbecky91cc06c2016-03-07 16:13:58 +00001290 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
1291 if (compiler_options.GetGenerateDebugInfo()) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001292 const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
1293 const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
Vladimir Marko1b404a82017-09-01 13:35:26 +01001294 debug::MethodDebugInfo info = {};
1295 DCHECK(info.trampoline_name.empty());
David Srbecky197160d2016-03-07 17:33:57 +00001296 info.dex_file = dex_file;
1297 info.class_def_index = class_def_idx;
1298 info.dex_method_index = method_idx;
1299 info.access_flags = access_flags;
1300 info.code_item = code_item;
1301 info.isa = codegen->GetInstructionSet();
1302 info.deduped = false;
1303 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
1304 info.is_optimized = true;
1305 info.is_code_address_text_relative = false;
1306 info.code_address = code_address;
1307 info.code_size = code_allocator.GetSize();
1308 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
1309 info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
1310 info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
Vladimir Marko93205e32016-04-13 11:59:46 +01001311 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
David Srbecky5d811202016-03-08 13:21:22 +00001312 GetCompilerDriver()->GetInstructionSet(),
1313 GetCompilerDriver()->GetInstructionSetFeatures(),
David Srbeckyfe736b72016-03-09 11:44:44 +00001314 ArrayRef<const debug::MethodDebugInfo>(&info, 1));
Vladimir Marko93205e32016-04-13 11:59:46 +01001315 CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
David Srbecky5cc349f2015-12-18 15:04:48 +00001316 }
1317
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001318 Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed());
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001319 if (jit_logger != nullptr) {
1320 jit_logger->WriteLog(code, code_allocator.GetSize(), method);
1321 }
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001322
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001323 return true;
1324}
1325
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001326} // namespace art