blob: 79ac6b9b9d440fb14619ad648e09311309c0ff0c [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_allocator.h"
Vladimir Markof9f64412015-09-02 14:05:49 +010027#include "base/arena_containers.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000028#include "base/dumpable.h"
Vladimir Markoc90d7c72015-10-06 17:30:45 +000029#include "base/macros.h"
Andreas Gampea0d81af2016-10-27 12:04:57 -070030#include "base/mutex.h"
Vladimir Markoca6fff82017-10-03 14:49:14 +010031#include "base/scoped_arena_allocator.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000032#include "base/timing_logger.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000033#include "builder.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010034#include "class_root.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000035#include "code_generator.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000036#include "compiled_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070037#include "compiler.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000038#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000039#include "debug/method_debug_info.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/dex_file_types.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010041#include "dex/verification_results.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000042#include "dex/verified_method.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000043#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000044#include "driver/compiler_options.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000045#include "driver/dex_compilation_unit.h"
David Brazdil69ba7b72015-06-23 18:27:30 +010046#include "graph_checker.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010047#include "graph_visualizer.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000048#include "inliner.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000049#include "jit/debugger_interface.h"
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000050#include "jit/jit.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000051#include "jit/jit_code_cache.h"
Nicolas Geoffray01db5f72017-07-19 15:05:49 +010052#include "jit/jit_logger.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000053#include "jni/quick/jni_compiler.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010054#include "linker/linker_patch.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000055#include "nodes.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000056#include "oat_quick_method_header.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010057#include "prepare_for_register_allocation.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010058#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070059#include "register_allocator_linear_scan.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070060#include "select_generator.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000061#include "ssa_builder.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010062#include "ssa_liveness_analysis.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000063#include "ssa_phi_elimination.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010064#include "utils/assembler.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070065#include "verifier/verifier_compiler_binding.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000066
67namespace art {
68
Vladimir Marko3a40bf22016-03-22 16:26:33 +000069static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
70
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070071static constexpr const char* kPassNameSeparator = "$";
72
Nicolas Geoffray787c3072014-03-17 10:20:19 +000073/**
74 * Used by the code generator, to allocate the code in a vector.
75 */
76class CodeVectorAllocator FINAL : public CodeAllocator {
77 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010078 explicit CodeVectorAllocator(ArenaAllocator* allocator)
Vladimir Markoca1e0382018-04-11 09:58:41 +000079 : memory_(allocator->Adapter(kArenaAllocCodeBuffer)) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080
81 virtual uint8_t* Allocate(size_t size) {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000082 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 return &memory_[0];
84 }
85
Vladimir Markoca1e0382018-04-11 09:58:41 +000086 ArrayRef<const uint8_t> GetMemory() const OVERRIDE { return ArrayRef<const uint8_t>(memory_); }
Nicolas Geoffray132d8362016-11-16 09:19:42 +000087 uint8_t* GetData() { return memory_.data(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088
89 private:
Vladimir Markof9f64412015-09-02 14:05:49 +010090 ArenaVector<uint8_t> memory_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091
92 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
93};
94
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010095/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010096 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000097 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010098 */
Andreas Gampe53fcd0f2015-07-22 12:10:13 -070099static constexpr const char kStringFilter[] = "";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100100
David Brazdil69ba7b72015-06-23 18:27:30 +0100101class PassScope;
David Brazdil809658e2015-02-05 11:34:02 +0000102
David Brazdil69ba7b72015-06-23 18:27:30 +0100103class PassObserver : public ValueObject {
David Brazdil5e8b1372015-01-23 14:39:08 +0000104 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100105 PassObserver(HGraph* graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100106 CodeGenerator* codegen,
107 std::ostream* visualizer_output,
Andreas Gampea0d81af2016-10-27 12:04:57 -0700108 CompilerDriver* compiler_driver,
109 Mutex& dump_mutex)
David Brazdil69ba7b72015-06-23 18:27:30 +0100110 : graph_(graph),
Aart Bika8360cd2018-05-02 16:07:51 -0700111 last_seen_graph_size_(0),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000112 cached_method_name_(),
Vladimir Marko2da52b02018-05-08 16:31:34 +0100113 timing_logger_enabled_(compiler_driver->GetCompilerOptions().GetDumpPassTimings()),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000114 timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100115 disasm_info_(graph->GetAllocator()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700116 visualizer_oss_(),
117 visualizer_output_(visualizer_output),
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000118 visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700119 visualizer_(&visualizer_oss_, graph, *codegen),
120 visualizer_dump_mutex_(dump_mutex),
David Brazdil69ba7b72015-06-23 18:27:30 +0100121 graph_in_bad_state_(false) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700122 if (timing_logger_enabled_ || visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000123 if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700124 timing_logger_enabled_ = visualizer_enabled_ = false;
125 }
126 if (visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000127 visualizer_.PrintHeader(GetMethodName());
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700128 codegen->SetDisassemblyInformation(&disasm_info_);
129 }
David Brazdil62e074f2015-04-07 18:09:37 +0100130 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000131 }
132
David Brazdil69ba7b72015-06-23 18:27:30 +0100133 ~PassObserver() {
David Brazdil5e8b1372015-01-23 14:39:08 +0000134 if (timing_logger_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000135 LOG(INFO) << "TIMINGS " << GetMethodName();
David Brazdil5e8b1372015-01-23 14:39:08 +0000136 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
137 }
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000138 DCHECK(visualizer_oss_.str().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000139 }
140
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000141 void DumpDisassembly() REQUIRES(!visualizer_dump_mutex_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100142 if (visualizer_enabled_) {
143 visualizer_.DumpGraphWithDisassembly();
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000144 FlushVisualizer();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100145 }
146 }
147
David Brazdil69ba7b72015-06-23 18:27:30 +0100148 void SetGraphInBadState() { graph_in_bad_state_ = true; }
149
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000150 const char* GetMethodName() {
151 // PrettyMethod() is expensive, so we delay calling it until we actually have to.
152 if (cached_method_name_.empty()) {
David Sehr709b0702016-10-13 09:12:37 -0700153 cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx());
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000154 }
155 return cached_method_name_.c_str();
156 }
157
David Brazdil5e8b1372015-01-23 14:39:08 +0000158 private:
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000159 void StartPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
Wojciech Staszkiewicze7060702016-08-16 17:31:19 -0700160 VLOG(compiler) << "Starting pass: " << pass_name;
David Brazdil809658e2015-02-05 11:34:02 +0000161 // Dump graph first, then start timer.
162 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100163 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000164 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000165 }
166 if (timing_logger_enabled_) {
167 timing_logger_.StartTiming(pass_name);
168 }
169 }
170
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000171 void FlushVisualizer() REQUIRES(!visualizer_dump_mutex_) {
172 MutexLock mu(Thread::Current(), visualizer_dump_mutex_);
173 *visualizer_output_ << visualizer_oss_.str();
174 visualizer_output_->flush();
175 visualizer_oss_.str("");
176 visualizer_oss_.clear();
177 }
178
Aart Bika8360cd2018-05-02 16:07:51 -0700179 void EndPass(const char* pass_name, bool pass_change) REQUIRES(!visualizer_dump_mutex_) {
David Brazdil809658e2015-02-05 11:34:02 +0000180 // Pause timer first, then dump graph.
181 if (timing_logger_enabled_) {
182 timing_logger_.EndTiming();
183 }
184 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100185 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000186 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000187 }
David Brazdil69ba7b72015-06-23 18:27:30 +0100188
189 // Validate the HGraph if running in debug mode.
190 if (kIsDebugBuild) {
191 if (!graph_in_bad_state_) {
David Brazdilbadd8262016-02-02 16:28:56 +0000192 GraphChecker checker(graph_);
Aart Bika8360cd2018-05-02 16:07:51 -0700193 last_seen_graph_size_ = checker.Run(pass_change, last_seen_graph_size_);
David Brazdilbadd8262016-02-02 16:28:56 +0000194 if (!checker.IsValid()) {
195 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
David Brazdil69ba7b72015-06-23 18:27:30 +0100196 }
197 }
198 }
David Brazdil809658e2015-02-05 11:34:02 +0000199 }
200
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700201 static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
202 // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
203 // empty kStringFilter matching all methods.
204 if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
205 return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
206 }
207
208 // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
209 // warning when the string is empty.
210 constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
211 if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
212 return true;
213 }
214
215 return false;
216 }
217
David Brazdil69ba7b72015-06-23 18:27:30 +0100218 HGraph* const graph_;
Aart Bika8360cd2018-05-02 16:07:51 -0700219 size_t last_seen_graph_size_;
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000220
221 std::string cached_method_name_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000222
223 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000224 TimingLogger timing_logger_;
225
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100226 DisassemblyInformation disasm_info_;
227
Andreas Gampea0d81af2016-10-27 12:04:57 -0700228 std::ostringstream visualizer_oss_;
229 std::ostream* visualizer_output_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000230 bool visualizer_enabled_;
231 HGraphVisualizer visualizer_;
Andreas Gampea0d81af2016-10-27 12:04:57 -0700232 Mutex& visualizer_dump_mutex_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000233
David Brazdil69ba7b72015-06-23 18:27:30 +0100234 // Flag to be set by the compiler if the pass failed and the graph is not
235 // expected to validate.
236 bool graph_in_bad_state_;
David Brazdil809658e2015-02-05 11:34:02 +0000237
David Brazdil69ba7b72015-06-23 18:27:30 +0100238 friend PassScope;
239
240 DISALLOW_COPY_AND_ASSIGN(PassObserver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000241};
242
David Brazdil69ba7b72015-06-23 18:27:30 +0100243class PassScope : public ValueObject {
David Brazdil809658e2015-02-05 11:34:02 +0000244 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100245 PassScope(const char *pass_name, PassObserver* pass_observer)
David Brazdil809658e2015-02-05 11:34:02 +0000246 : pass_name_(pass_name),
Aart Bika8360cd2018-05-02 16:07:51 -0700247 pass_change_(true), // assume change
David Brazdil69ba7b72015-06-23 18:27:30 +0100248 pass_observer_(pass_observer) {
249 pass_observer_->StartPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000250 }
251
Aart Bika8360cd2018-05-02 16:07:51 -0700252 void SetPassNotChanged() {
253 pass_change_ = false;
254 }
255
David Brazdil69ba7b72015-06-23 18:27:30 +0100256 ~PassScope() {
Aart Bika8360cd2018-05-02 16:07:51 -0700257 pass_observer_->EndPass(pass_name_, pass_change_);
David Brazdil809658e2015-02-05 11:34:02 +0000258 }
259
260 private:
261 const char* const pass_name_;
Aart Bika8360cd2018-05-02 16:07:51 -0700262 bool pass_change_;
David Brazdil69ba7b72015-06-23 18:27:30 +0100263 PassObserver* const pass_observer_;
David Brazdil809658e2015-02-05 11:34:02 +0000264};
265
Andreas Gampe53c913b2014-08-12 23:19:23 -0700266class OptimizingCompiler FINAL : public Compiler {
267 public:
268 explicit OptimizingCompiler(CompilerDriver* driver);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700269 ~OptimizingCompiler() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700270
Vladimir Markodf739842016-03-23 16:59:07 +0000271 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700272
273 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
274 uint32_t access_flags,
275 InvokeType invoke_type,
276 uint16_t class_def_idx,
277 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000278 Handle<mirror::ClassLoader> class_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700279 const DexFile& dex_file,
280 Handle<mirror::DexCache> dex_cache) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700281
Andreas Gampe53c913b2014-08-12 23:19:23 -0700282 CompiledMethod* JniCompile(uint32_t access_flags,
283 uint32_t method_idx,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000284 const DexFile& dex_file,
285 Handle<mirror::DexCache> dex_cache) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700286
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287 uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700288 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000289 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
290 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
291 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700292
David Brazdilee690a32014-12-01 17:04:16 +0000293 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700294
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000295 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700296
Nicolas Geoffray01db5f72017-07-19 15:05:49 +0100297 bool JitCompile(Thread* self,
298 jit::JitCodeCache* code_cache,
299 ArtMethod* method,
300 bool osr,
301 jit::JitLogger* jit_logger)
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000302 OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700303 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000304
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700305 private:
Aart Bik24773202018-04-26 10:28:51 -0700306 bool RunOptimizations(HGraph* graph,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700307 CodeGenerator* codegen,
Aart Bik2ca10eb2017-11-15 15:17:53 -0800308 const DexCompilationUnit& dex_compilation_unit,
309 PassObserver* pass_observer,
310 VariableSizedHandleScope* handles,
311 const OptimizationDef definitions[],
312 size_t length) const {
313 // Convert definitions to optimization passes.
314 ArenaVector<HOptimization*> optimizations = ConstructOptimizations(
315 definitions,
316 length,
317 graph->GetAllocator(),
318 graph,
319 compilation_stats_.get(),
320 codegen,
321 GetCompilerDriver(),
322 dex_compilation_unit,
323 handles);
324 DCHECK_EQ(length, optimizations.size());
Aart Bik2e148682018-04-18 16:11:12 -0700325 // Run the optimization passes one by one. Any "depends_on" pass refers back to
326 // the most recent occurrence of that pass, skipped or executed.
327 std::bitset<static_cast<size_t>(OptimizationPass::kLast) + 1u> pass_changes;
328 pass_changes[static_cast<size_t>(OptimizationPass::kNone)] = true;
Aart Bik24773202018-04-26 10:28:51 -0700329 bool change = false;
Aart Bik2ca10eb2017-11-15 15:17:53 -0800330 for (size_t i = 0; i < length; ++i) {
Aart Bik2e148682018-04-18 16:11:12 -0700331 if (pass_changes[static_cast<size_t>(definitions[i].depends_on)]) {
332 // Execute the pass and record whether it changed anything.
333 PassScope scope(optimizations[i]->GetPassName(), pass_observer);
334 bool pass_change = optimizations[i]->Run();
335 pass_changes[static_cast<size_t>(definitions[i].pass)] = pass_change;
Aart Bika8360cd2018-05-02 16:07:51 -0700336 if (pass_change) {
337 change = true;
338 } else {
339 scope.SetPassNotChanged();
340 }
Aart Bik2e148682018-04-18 16:11:12 -0700341 } else {
342 // Skip the pass and record that nothing changed.
343 pass_changes[static_cast<size_t>(definitions[i].pass)] = false;
344 }
Aart Bik2ca10eb2017-11-15 15:17:53 -0800345 }
Aart Bik24773202018-04-26 10:28:51 -0700346 return change;
Aart Bik2ca10eb2017-11-15 15:17:53 -0800347 }
348
Aart Bik24773202018-04-26 10:28:51 -0700349 template <size_t length> bool RunOptimizations(
Aart Bik2ca10eb2017-11-15 15:17:53 -0800350 HGraph* graph,
351 CodeGenerator* codegen,
352 const DexCompilationUnit& dex_compilation_unit,
353 PassObserver* pass_observer,
354 VariableSizedHandleScope* handles,
355 const OptimizationDef (&definitions)[length]) const {
Aart Bik24773202018-04-26 10:28:51 -0700356 return RunOptimizations(
Aart Bik2ca10eb2017-11-15 15:17:53 -0800357 graph, codegen, dex_compilation_unit, pass_observer, handles, definitions, length);
358 }
359
360 void RunOptimizations(HGraph* graph,
361 CodeGenerator* codegen,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700362 const DexCompilationUnit& dex_compilation_unit,
363 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700364 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700365
Andreas Gampe53c913b2014-08-12 23:19:23 -0700366 private:
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000367 // Create a 'CompiledMethod' for an optimized graph.
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100368 CompiledMethod* Emit(ArenaAllocator* allocator,
David Brazdil58282f42016-01-14 12:45:10 +0000369 CodeVectorAllocator* code_allocator,
370 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000371 const DexFile::CodeItem* item) const;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000372
373 // Try compiling a method and return the code generator used for
374 // compiling it.
375 // This method:
376 // 1) Builds the graph. Returns null if it failed to build it.
David Brazdil58282f42016-01-14 12:45:10 +0000377 // 2) Transforms the graph to SSA. Returns null if it failed.
378 // 3) Runs optimizations on the graph, including register allocator.
379 // 4) Generates code with the `code_allocator` provided.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100380 CodeGenerator* TryCompile(ArenaAllocator* allocator,
381 ArenaStack* arena_stack,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000382 CodeVectorAllocator* code_allocator,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000383 const DexCompilationUnit& dex_compilation_unit,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000384 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000385 bool osr,
386 VariableSizedHandleScope* handles) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000387
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000388 CodeGenerator* TryCompileIntrinsic(ArenaAllocator* allocator,
389 ArenaStack* arena_stack,
390 CodeVectorAllocator* code_allocator,
391 const DexCompilationUnit& dex_compilation_unit,
392 ArtMethod* method,
393 VariableSizedHandleScope* handles) const;
394
Aart Bik24773202018-04-26 10:28:51 -0700395 bool RunArchOptimizations(HGraph* graph,
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700396 CodeGenerator* codegen,
Aart Bik2ca10eb2017-11-15 15:17:53 -0800397 const DexCompilationUnit& dex_compilation_unit,
398 PassObserver* pass_observer,
399 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700400
David Srbeckyc9e02082018-01-24 16:44:02 +0000401 void GenerateJitDebugInfo(ArtMethod* method, debug::MethodDebugInfo method_debug_info)
402 REQUIRES_SHARED(Locks::mutator_lock_);
David Srbeckyc684f332018-01-19 17:38:06 +0000403
Calin Juravle2be39e02015-04-21 13:56:34 +0100404 std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100405
Andreas Gampe53c913b2014-08-12 23:19:23 -0700406 std::unique_ptr<std::ostream> visualizer_output_;
407
Andreas Gampea0d81af2016-10-27 12:04:57 -0700408 mutable Mutex dump_mutex_; // To synchronize visualizer writing.
409
Andreas Gampe53c913b2014-08-12 23:19:23 -0700410 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
411};
412
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100413static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
414
415OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
Andreas Gampea0d81af2016-10-27 12:04:57 -0700416 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
417 dump_mutex_("Visualizer dump lock") {}
David Brazdilee690a32014-12-01 17:04:16 +0000418
419void OptimizingCompiler::Init() {
420 // Enable C1visualizer output. Must be done in Init() because the compiler
421 // driver is not fully initialized when passed to the compiler's constructor.
422 CompilerDriver* driver = GetCompilerDriver();
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000423 const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
David Brazdil866c0312015-01-13 21:21:31 +0000424 if (!cfg_file_name.empty()) {
Calin Juravle87000a92015-08-24 15:34:44 +0100425 std::ios_base::openmode cfg_file_mode =
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000426 driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
Calin Juravle87000a92015-08-24 15:34:44 +0100427 visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100428 }
Nicolas Geoffray2d8801f2017-11-28 15:50:07 +0000429 if (driver->GetCompilerOptions().GetDumpStats()) {
Calin Juravle2be39e02015-04-21 13:56:34 +0100430 compilation_stats_.reset(new OptimizingCompilerStats());
431 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100432}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000433
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000434void OptimizingCompiler::UnInit() const {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000435}
436
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100437OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle2be39e02015-04-21 13:56:34 +0100438 if (compilation_stats_.get() != nullptr) {
439 compilation_stats_->Log();
440 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100441}
442
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000443bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
Vladimir Markodf739842016-03-23 16:59:07 +0000444 const DexFile& dex_file ATTRIBUTE_UNUSED) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000445 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700446}
447
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000448static bool IsInstructionSetSupported(InstructionSet instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +0000449 return instruction_set == InstructionSet::kArm
450 || instruction_set == InstructionSet::kArm64
451 || instruction_set == InstructionSet::kThumb2
452 || instruction_set == InstructionSet::kMips
453 || instruction_set == InstructionSet::kMips64
454 || instruction_set == InstructionSet::kX86
455 || instruction_set == InstructionSet::kX86_64;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000456}
457
Aart Bik24773202018-04-26 10:28:51 -0700458bool OptimizingCompiler::RunArchOptimizations(HGraph* graph,
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700459 CodeGenerator* codegen,
Aart Bik2ca10eb2017-11-15 15:17:53 -0800460 const DexCompilationUnit& dex_compilation_unit,
461 PassObserver* pass_observer,
462 VariableSizedHandleScope* handles) const {
463 switch (GetCompilerDriver()->GetInstructionSet()) {
Artem Serov2bbc9532016-10-21 11:51:50 +0100464#if defined(ART_ENABLE_CODEGEN_arm)
Vladimir Marko33bff252017-11-01 14:35:42 +0000465 case InstructionSet::kThumb2:
466 case InstructionSet::kArm: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800467 OptimizationDef arm_optimizations[] = {
468 OptDef(OptimizationPass::kInstructionSimplifierArm),
469 OptDef(OptimizationPass::kSideEffectsAnalysis),
470 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"),
471 OptDef(OptimizationPass::kScheduling)
Vladimir Markob4536b72015-11-24 13:45:23 +0000472 };
Aart Bik24773202018-04-26 10:28:51 -0700473 return RunOptimizations(graph,
474 codegen,
475 dex_compilation_unit,
476 pass_observer,
477 handles,
478 arm_optimizations);
Vladimir Markob4536b72015-11-24 13:45:23 +0000479 }
480#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100481#ifdef ART_ENABLE_CODEGEN_arm64
Vladimir Marko33bff252017-11-01 14:35:42 +0000482 case InstructionSet::kArm64: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800483 OptimizationDef arm64_optimizations[] = {
484 OptDef(OptimizationPass::kInstructionSimplifierArm64),
485 OptDef(OptimizationPass::kSideEffectsAnalysis),
486 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"),
487 OptDef(OptimizationPass::kScheduling)
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100488 };
Aart Bik24773202018-04-26 10:28:51 -0700489 return RunOptimizations(graph,
490 codegen,
491 dex_compilation_unit,
492 pass_observer,
493 handles,
494 arm64_optimizations);
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100495 }
496#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700497#ifdef ART_ENABLE_CODEGEN_mips
Vladimir Marko33bff252017-11-01 14:35:42 +0000498 case InstructionSet::kMips: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800499 OptimizationDef mips_optimizations[] = {
500 OptDef(OptimizationPass::kInstructionSimplifierMips),
501 OptDef(OptimizationPass::kSideEffectsAnalysis),
502 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"),
503 OptDef(OptimizationPass::kPcRelativeFixupsMips)
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700504 };
Aart Bik24773202018-04-26 10:28:51 -0700505 return RunOptimizations(graph,
506 codegen,
507 dex_compilation_unit,
508 pass_observer,
509 handles,
510 mips_optimizations);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700511 }
512#endif
Lena Djokicb8e9c352017-09-20 15:11:13 +0200513#ifdef ART_ENABLE_CODEGEN_mips64
Vladimir Marko33bff252017-11-01 14:35:42 +0000514 case InstructionSet::kMips64: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800515 OptimizationDef mips64_optimizations[] = {
516 OptDef(OptimizationPass::kSideEffectsAnalysis),
517 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch")
Lena Djokicb8e9c352017-09-20 15:11:13 +0200518 };
Aart Bik24773202018-04-26 10:28:51 -0700519 return RunOptimizations(graph,
520 codegen,
521 dex_compilation_unit,
522 pass_observer,
523 handles,
524 mips64_optimizations);
Lena Djokicb8e9c352017-09-20 15:11:13 +0200525 }
526#endif
Mark Mendell0616ae02015-04-17 12:49:27 -0400527#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Marko33bff252017-11-01 14:35:42 +0000528 case InstructionSet::kX86: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800529 OptimizationDef x86_optimizations[] = {
530 OptDef(OptimizationPass::kSideEffectsAnalysis),
531 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"),
532 OptDef(OptimizationPass::kPcRelativeFixupsX86),
533 OptDef(OptimizationPass::kX86MemoryOperandGeneration)
Mark Mendell0616ae02015-04-17 12:49:27 -0400534 };
Aart Bik24773202018-04-26 10:28:51 -0700535 return RunOptimizations(graph,
536 codegen,
537 dex_compilation_unit,
538 pass_observer,
539 handles,
540 x86_optimizations);
Mark Mendell0616ae02015-04-17 12:49:27 -0400541 }
542#endif
Mark Mendellee8d9712016-07-12 11:13:15 -0400543#ifdef ART_ENABLE_CODEGEN_x86_64
Vladimir Marko33bff252017-11-01 14:35:42 +0000544 case InstructionSet::kX86_64: {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800545 OptimizationDef x86_64_optimizations[] = {
546 OptDef(OptimizationPass::kSideEffectsAnalysis),
547 OptDef(OptimizationPass::kGlobalValueNumbering, "GVN$after_arch"),
548 OptDef(OptimizationPass::kX86MemoryOperandGeneration)
Mark Mendellee8d9712016-07-12 11:13:15 -0400549 };
Aart Bik24773202018-04-26 10:28:51 -0700550 return RunOptimizations(graph,
551 codegen,
552 dex_compilation_unit,
553 pass_observer,
554 handles,
555 x86_64_optimizations);
Mark Mendellee8d9712016-07-12 11:13:15 -0400556 }
557#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100558 default:
Aart Bik24773202018-04-26 10:28:51 -0700559 return false;
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100560 }
561}
562
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000563NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects.
564static void AllocateRegisters(HGraph* graph,
565 CodeGenerator* codegen,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700566 PassObserver* pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -0700567 RegisterAllocator::Strategy strategy,
568 OptimizingCompilerStats* stats) {
Mingyao Yang700347e2016-03-02 14:59:32 -0800569 {
570 PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
571 pass_observer);
Igor Murashkin6ef45672017-08-08 13:59:55 -0700572 PrepareForRegisterAllocation(graph, stats).Run();
Mingyao Yang700347e2016-03-02 14:59:32 -0800573 }
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100574 // Use local allocator shared by SSA liveness analysis and register allocator.
575 // (Register allocator creates new objects in the liveness data.)
576 ScopedArenaAllocator local_allocator(graph->GetArenaStack());
577 SsaLivenessAnalysis liveness(graph, codegen, &local_allocator);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000578 {
579 PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
580 liveness.Analyze();
581 }
582 {
583 PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100584 std::unique_ptr<RegisterAllocator> register_allocator =
585 RegisterAllocator::Create(&local_allocator, codegen, liveness, strategy);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100586 register_allocator->AllocateRegisters();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000587 }
588}
589
Aart Bik2ca10eb2017-11-15 15:17:53 -0800590// Strip pass name suffix to get optimization name.
591static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) {
592 size_t pos = pass_name.find(kPassNameSeparator);
593 return pos == std::string::npos ? pass_name : pass_name.substr(0, pos);
594}
595
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700596void OptimizingCompiler::RunOptimizations(HGraph* graph,
597 CodeGenerator* codegen,
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700598 const DexCompilationUnit& dex_compilation_unit,
599 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700600 VariableSizedHandleScope* handles) const {
Aart Bik2ca10eb2017-11-15 15:17:53 -0800601 const std::vector<std::string>* pass_names =
602 GetCompilerDriver()->GetCompilerOptions().GetPassesToRun();
603 if (pass_names != nullptr) {
604 // If passes were defined on command-line, build the optimization
605 // passes and run these instead of the built-in optimizations.
Aart Bik2e148682018-04-18 16:11:12 -0700606 // TODO: a way to define depends_on via command-line?
Aart Bik2ca10eb2017-11-15 15:17:53 -0800607 const size_t length = pass_names->size();
608 std::vector<OptimizationDef> optimizations;
609 for (const std::string& pass_name : *pass_names) {
610 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
611 optimizations.push_back(OptDef(OptimizationPassByName(opt_name.c_str()), pass_name.c_str()));
612 }
613 RunOptimizations(graph,
614 codegen,
615 dex_compilation_unit,
616 pass_observer,
617 handles,
618 optimizations.data(),
619 length);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700620 return;
621 }
622
Aart Bik24773202018-04-26 10:28:51 -0700623 OptimizationDef optimizations[] = {
Aart Bik2e148682018-04-18 16:11:12 -0700624 // Initial optimizations.
Aart Bik2ca10eb2017-11-15 15:17:53 -0800625 OptDef(OptimizationPass::kIntrinsicsRecognizer),
626 OptDef(OptimizationPass::kSharpening),
627 OptDef(OptimizationPass::kConstantFolding),
628 OptDef(OptimizationPass::kInstructionSimplifier),
Aart Bik2e148682018-04-18 16:11:12 -0700629 OptDef(OptimizationPass::kDeadCodeElimination,
630 "dead_code_elimination$initial"),
631 // Inlining.
Aart Bik24773202018-04-26 10:28:51 -0700632 OptDef(OptimizationPass::kInliner),
Aart Bik2e148682018-04-18 16:11:12 -0700633 // Simplification (only if inlining occurred).
634 OptDef(OptimizationPass::kConstantFolding,
635 "constant_folding$after_inlining",
636 OptimizationPass::kInliner),
637 OptDef(OptimizationPass::kInstructionSimplifier,
638 "instruction_simplifier$after_inlining",
639 OptimizationPass::kInliner),
640 OptDef(OptimizationPass::kDeadCodeElimination,
641 "dead_code_elimination$after_inlining",
642 OptimizationPass::kInliner),
643 // GVN.
644 OptDef(OptimizationPass::kSideEffectsAnalysis,
645 "side_effects$before_gvn"),
Aart Bik6d057002018-04-09 15:39:58 -0700646 OptDef(OptimizationPass::kGlobalValueNumbering),
Aart Bik2e148682018-04-18 16:11:12 -0700647 // Simplification (TODO: only if GVN occurred).
Aart Bik2ca10eb2017-11-15 15:17:53 -0800648 OptDef(OptimizationPass::kSelectGenerator),
Aart Bik2e148682018-04-18 16:11:12 -0700649 OptDef(OptimizationPass::kConstantFolding,
650 "constant_folding$after_gvn"),
651 OptDef(OptimizationPass::kInstructionSimplifier,
652 "instruction_simplifier$after_gvn"),
653 OptDef(OptimizationPass::kDeadCodeElimination,
654 "dead_code_elimination$after_gvn"),
655 // High-level optimizations.
656 OptDef(OptimizationPass::kSideEffectsAnalysis,
657 "side_effects$before_licm"),
Aart Bik2ca10eb2017-11-15 15:17:53 -0800658 OptDef(OptimizationPass::kInvariantCodeMotion),
659 OptDef(OptimizationPass::kInductionVarAnalysis),
660 OptDef(OptimizationPass::kBoundsCheckElimination),
661 OptDef(OptimizationPass::kLoopOptimization),
Aart Bik2e148682018-04-18 16:11:12 -0700662 // Simplification.
663 OptDef(OptimizationPass::kConstantFolding,
664 "constant_folding$after_bce"),
665 OptDef(OptimizationPass::kInstructionSimplifier,
666 "instruction_simplifier$after_bce"),
667 // Other high-level optimizations.
668 OptDef(OptimizationPass::kSideEffectsAnalysis,
669 "side_effects$before_lse"),
Aart Bik2ca10eb2017-11-15 15:17:53 -0800670 OptDef(OptimizationPass::kLoadStoreAnalysis),
671 OptDef(OptimizationPass::kLoadStoreElimination),
672 OptDef(OptimizationPass::kCHAGuardOptimization),
Aart Bik2e148682018-04-18 16:11:12 -0700673 OptDef(OptimizationPass::kDeadCodeElimination,
674 "dead_code_elimination$final"),
Aart Bik2ca10eb2017-11-15 15:17:53 -0800675 OptDef(OptimizationPass::kCodeSinking),
David Brazdil8993caf2015-12-07 10:04:40 +0000676 // The codegen has a few assumptions that only the instruction simplifier
677 // can satisfy. For example, the code generator does not expect to see a
678 // HTypeConversion from a type to the same type.
Aart Bik2e148682018-04-18 16:11:12 -0700679 OptDef(OptimizationPass::kInstructionSimplifier,
680 "instruction_simplifier$before_codegen"),
Aart Bik2ca10eb2017-11-15 15:17:53 -0800681 // Eliminate constructor fences after code sinking to avoid
682 // complicated sinking logic to split a fence with many inputs.
683 OptDef(OptimizationPass::kConstructorFenceRedundancyElimination)
David Brazdil8993caf2015-12-07 10:04:40 +0000684 };
Aart Bik2ca10eb2017-11-15 15:17:53 -0800685 RunOptimizations(graph,
686 codegen,
687 dex_compilation_unit,
688 pass_observer,
689 handles,
Aart Bik24773202018-04-26 10:28:51 -0700690 optimizations);
David Brazdilbbd733e2015-08-18 17:48:17 +0100691
Aart Bik2ca10eb2017-11-15 15:17:53 -0800692 RunArchOptimizations(graph, codegen, dex_compilation_unit, pass_observer, handles);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000693}
694
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100695static ArenaVector<linker::LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100696 ArenaVector<linker::LinkerPatch> linker_patches(codegen->GetGraph()->GetAllocator()->Adapter());
Vladimir Marko58155012015-08-19 12:49:41 +0000697 codegen->EmitLinkerPatches(&linker_patches);
698
699 // Sort patches by literal offset. Required for .oat_patches encoding.
700 std::sort(linker_patches.begin(), linker_patches.end(),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100701 [](const linker::LinkerPatch& lhs, const linker::LinkerPatch& rhs) {
Vladimir Marko58155012015-08-19 12:49:41 +0000702 return lhs.LiteralOffset() < rhs.LiteralOffset();
703 });
704
705 return linker_patches;
706}
707
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100708CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* allocator,
David Brazdil58282f42016-01-14 12:45:10 +0000709 CodeVectorAllocator* code_allocator,
710 CodeGenerator* codegen,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000711 const DexFile::CodeItem* code_item_for_osr_check) const {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100712 ArenaVector<linker::LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100713 ArenaVector<uint8_t> stack_map(allocator->Adapter(kArenaAllocStackMaps));
714 ArenaVector<uint8_t> method_info(allocator->Adapter(kArenaAllocStackMaps));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700715 size_t stack_map_size = 0;
716 size_t method_info_size = 0;
717 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
718 stack_map.resize(stack_map_size);
719 method_info.resize(method_info_size);
720 codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()),
721 MemoryRegion(method_info.data(), method_info.size()),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000722 code_item_for_osr_check);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000723
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100724 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
Aart Bik2ca10eb2017-11-15 15:17:53 -0800725 GetCompilerDriver(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000726 codegen->GetInstructionSet(),
Vladimir Markoca1e0382018-04-11 09:58:41 +0000727 code_allocator->GetMemory(),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000728 // Follow Quick's behavior and set the frame size to zero if it is
729 // considered "empty" (see the definition of
730 // art::CodeGenerator::HasEmptyFrame).
731 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000732 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000733 codegen->GetFpuSpillMask(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700734 ArrayRef<const uint8_t>(method_info),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100735 ArrayRef<const uint8_t>(stack_map),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100736 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100737 ArrayRef<const linker::LinkerPatch>(linker_patches));
Mathieu Chartiered150002015-08-28 11:16:54 -0700738
Vladimir Markoca1e0382018-04-11 09:58:41 +0000739 CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage();
740 for (const linker::LinkerPatch& patch : linker_patches) {
741 if (codegen->NeedsThunkCode(patch) && storage->GetThunkCode(patch).empty()) {
742 ArenaVector<uint8_t> code(allocator->Adapter());
743 std::string debug_name;
744 codegen->EmitThunkCode(patch, &code, &debug_name);
745 storage->SetThunkCode(patch, ArrayRef<const uint8_t>(code), debug_name);
746 }
747 }
748
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100749 return compiled_method;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000750}
751
Vladimir Markoca6fff82017-10-03 14:49:14 +0100752CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* allocator,
753 ArenaStack* arena_stack,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000754 CodeVectorAllocator* code_allocator,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000755 const DexCompilationUnit& dex_compilation_unit,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000756 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000757 bool osr,
758 VariableSizedHandleScope* handles) const {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000759 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kAttemptBytecodeCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000760 CompilerDriver* compiler_driver = GetCompilerDriver();
761 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000762 const DexFile& dex_file = *dex_compilation_unit.GetDexFile();
763 uint32_t method_idx = dex_compilation_unit.GetDexMethodIndex();
764 const DexFile::CodeItem* code_item = dex_compilation_unit.GetCodeItem();
Calin Juravlecff8cc72015-10-09 12:03:24 +0100765
Roland Levillain3b359c72015-11-17 19:35:12 +0000766 // Always use the Thumb-2 assembler: some runtime functionality
767 // (like implicit stack overflow checks) assume Thumb-2.
Vladimir Marko33bff252017-11-01 14:35:42 +0000768 DCHECK_NE(instruction_set, InstructionSet::kArm);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100769
770 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000771 if (!IsInstructionSetSupported(instruction_set)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700772 MaybeRecordStat(compilation_stats_.get(),
773 MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100774 return nullptr;
775 }
776
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000777 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000778 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000779 return nullptr;
780 }
781
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000782 // Implementation of the space filter: do not compile a code item whose size in
Nicolas Geoffray432bf3d2015-07-17 11:11:09 +0100783 // code units is bigger than 128.
784 static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000785 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000786 if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800787 && (CodeItemInstructionAccessor(dex_file, code_item).InsnsSizeInCodeUnits() >
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800788 kSpaceFilterOptimizingThreshold)) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000789 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledSpaceFilter);
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000790 return nullptr;
791 }
792
Mathieu Chartier8892c6b2018-01-09 15:10:17 -0800793 CodeItemDebugInfoAccessor code_item_accessor(dex_file, code_item, method_idx);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100794 HGraph* graph = new (allocator) HGraph(
795 allocator,
796 arena_stack,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000797 dex_file,
798 method_idx,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000799 compiler_driver->GetInstructionSet(),
800 kInvalidInvokeType,
801 compiler_driver->GetCompilerOptions().GetDebuggable(),
802 osr);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000803
Mathieu Chartier210531f2018-01-12 10:15:51 -0800804 ArrayRef<const uint8_t> interpreter_metadata;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000805 // For AOT compilation, we may not get a method, for example if its class is erroneous.
806 // JIT should always have a method.
807 DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr);
808 if (method != nullptr) {
809 graph->SetArtMethod(method);
810 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray8eaa8e52017-11-13 17:47:50 +0000811 interpreter_metadata = method->GetQuickenedInfo();
David Brazdilbadd8262016-02-02 16:28:56 +0000812 }
813
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000814 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000815 CodeGenerator::Create(graph,
816 instruction_set,
817 *compiler_driver->GetInstructionSetFeatures(),
Calin Juravle2ae48182016-03-16 14:05:09 +0000818 compiler_driver->GetCompilerOptions(),
819 compilation_stats_.get()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000820 if (codegen.get() == nullptr) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000821 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000822 return nullptr;
823 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100824 codegen->GetAssembler()->cfi().SetEnabled(
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000825 compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000826
David Brazdil69ba7b72015-06-23 18:27:30 +0100827 PassObserver pass_observer(graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100828 codegen.get(),
829 visualizer_output_.get(),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700830 compiler_driver,
831 dump_mutex_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000832
David Brazdil809658e2015-02-05 11:34:02 +0000833 {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000834 VLOG(compiler) << "Building " << pass_observer.GetMethodName();
835 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
836 HGraphBuilder builder(graph,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800837 code_item_accessor,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000838 &dex_compilation_unit,
839 &dex_compilation_unit,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000840 compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000841 codegen.get(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000842 compilation_stats_.get(),
843 interpreter_metadata,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000844 handles);
845 GraphAnalysisResult result = builder.BuildGraph();
846 if (result != kAnalysisSuccess) {
847 switch (result) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700848 case kAnalysisSkipped: {
849 MaybeRecordStat(compilation_stats_.get(),
850 MethodCompilationStat::kNotCompiledSkipped);
851 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000852 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -0700853 case kAnalysisInvalidBytecode: {
854 MaybeRecordStat(compilation_stats_.get(),
855 MethodCompilationStat::kNotCompiledInvalidBytecode);
856 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000857 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -0700858 case kAnalysisFailThrowCatchLoop: {
859 MaybeRecordStat(compilation_stats_.get(),
860 MethodCompilationStat::kNotCompiledThrowCatchLoop);
861 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000862 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -0700863 case kAnalysisFailAmbiguousArrayOp: {
864 MaybeRecordStat(compilation_stats_.get(),
865 MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
866 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000867 break;
868 case kAnalysisSuccess:
869 UNREACHABLE();
David Brazdil809658e2015-02-05 11:34:02 +0000870 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000871 pass_observer.SetGraphInBadState();
872 return nullptr;
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000873 }
David Brazdilbadd8262016-02-02 16:28:56 +0000874 }
Vladimir Markof9f64412015-09-02 14:05:49 +0100875
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000876 RunOptimizations(graph,
877 codegen.get(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000878 dex_compilation_unit,
879 &pass_observer,
880 handles);
881
882 RegisterAllocator::Strategy regalloc_strategy =
883 compiler_options.GetRegisterAllocationStrategy();
Igor Murashkin1e065a52017-08-09 13:20:34 -0700884 AllocateRegisters(graph,
885 codegen.get(),
886 &pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -0700887 regalloc_strategy,
888 compilation_stats_.get());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000889
890 codegen->Compile(code_allocator);
891 pass_observer.DumpDisassembly();
892
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000893 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledBytecode);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000894 return codegen.release();
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000895}
896
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000897CodeGenerator* OptimizingCompiler::TryCompileIntrinsic(
898 ArenaAllocator* allocator,
899 ArenaStack* arena_stack,
900 CodeVectorAllocator* code_allocator,
901 const DexCompilationUnit& dex_compilation_unit,
902 ArtMethod* method,
903 VariableSizedHandleScope* handles) const {
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000904 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kAttemptIntrinsicCompilation);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000905 CompilerDriver* compiler_driver = GetCompilerDriver();
906 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
907 const DexFile& dex_file = *dex_compilation_unit.GetDexFile();
908 uint32_t method_idx = dex_compilation_unit.GetDexMethodIndex();
909
910 // Always use the Thumb-2 assembler: some runtime functionality
911 // (like implicit stack overflow checks) assume Thumb-2.
912 DCHECK_NE(instruction_set, InstructionSet::kArm);
913
914 // Do not attempt to compile on architectures we do not support.
915 if (!IsInstructionSetSupported(instruction_set)) {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000916 return nullptr;
917 }
918
919 HGraph* graph = new (allocator) HGraph(
920 allocator,
921 arena_stack,
922 dex_file,
923 method_idx,
924 compiler_driver->GetInstructionSet(),
925 kInvalidInvokeType,
926 compiler_driver->GetCompilerOptions().GetDebuggable(),
927 /* osr */ false);
928
929 DCHECK(Runtime::Current()->IsAotCompiler());
930 DCHECK(method != nullptr);
931 graph->SetArtMethod(method);
932
933 std::unique_ptr<CodeGenerator> codegen(
934 CodeGenerator::Create(graph,
935 instruction_set,
936 *compiler_driver->GetInstructionSetFeatures(),
937 compiler_driver->GetCompilerOptions(),
938 compilation_stats_.get()));
939 if (codegen.get() == nullptr) {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000940 return nullptr;
941 }
942 codegen->GetAssembler()->cfi().SetEnabled(
943 compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
944
945 PassObserver pass_observer(graph,
946 codegen.get(),
947 visualizer_output_.get(),
948 compiler_driver,
949 dump_mutex_);
950
951 {
952 VLOG(compiler) << "Building intrinsic graph " << pass_observer.GetMethodName();
953 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
954 HGraphBuilder builder(graph,
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800955 CodeItemDebugInfoAccessor(), // Null code item.
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000956 &dex_compilation_unit,
957 &dex_compilation_unit,
958 compiler_driver,
959 codegen.get(),
960 compilation_stats_.get(),
Mathieu Chartier210531f2018-01-12 10:15:51 -0800961 /* interpreter_metadata */ ArrayRef<const uint8_t>(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000962 handles);
963 builder.BuildIntrinsicGraph(method);
964 }
965
Aart Bik2ca10eb2017-11-15 15:17:53 -0800966 OptimizationDef optimizations[] = {
967 OptDef(OptimizationPass::kIntrinsicsRecognizer),
968 // Some intrinsics are converted to HIR by the simplifier and the codegen also
969 // has a few assumptions that only the instruction simplifier can satisfy.
970 OptDef(OptimizationPass::kInstructionSimplifier),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000971 };
Aart Bik2ca10eb2017-11-15 15:17:53 -0800972 RunOptimizations(graph,
973 codegen.get(),
974 dex_compilation_unit,
975 &pass_observer,
976 handles,
977 optimizations);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000978
Aart Bik2ca10eb2017-11-15 15:17:53 -0800979 RunArchOptimizations(graph, codegen.get(), dex_compilation_unit, &pass_observer, handles);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000980
981 AllocateRegisters(graph,
982 codegen.get(),
983 &pass_observer,
984 compiler_driver->GetCompilerOptions().GetRegisterAllocationStrategy(),
985 compilation_stats_.get());
986 if (!codegen->IsLeafMethod()) {
987 VLOG(compiler) << "Intrinsic method is not leaf: " << method->GetIntrinsic()
988 << " " << graph->PrettyMethod();
989 return nullptr;
990 }
991
992 codegen->Compile(code_allocator);
993 pass_observer.DumpDisassembly();
994
995 VLOG(compiler) << "Compiled intrinsic: " << method->GetIntrinsic()
996 << " " << graph->PrettyMethod();
Vladimir Markocd09e1f2017-11-24 15:02:40 +0000997 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledIntrinsic);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000998 return codegen.release();
999}
1000
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001001CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
1002 uint32_t access_flags,
1003 InvokeType invoke_type,
1004 uint16_t class_def_idx,
1005 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001006 Handle<mirror::ClassLoader> jclass_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001007 const DexFile& dex_file,
1008 Handle<mirror::DexCache> dex_cache) const {
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001009 CompilerDriver* compiler_driver = GetCompilerDriver();
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001010 CompiledMethod* compiled_method = nullptr;
1011 Runtime* runtime = Runtime::Current();
1012 DCHECK(runtime->IsAotCompiler());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001013 const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
1014 DCHECK(!verified_method->HasRuntimeThrow());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) ||
1016 verifier::CanCompilerHandleVerificationFailure(
1017 verified_method->GetEncounteredVerificationFailures())) {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001018 ArenaAllocator allocator(runtime->GetArenaPool());
1019 ArenaStack arena_stack(runtime->GetArenaPool());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001020 CodeVectorAllocator code_allocator(&allocator);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001021 std::unique_ptr<CodeGenerator> codegen;
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001022 bool compiled_intrinsic = false;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001023 {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001024 DexCompilationUnit dex_compilation_unit(
1025 jclass_loader,
1026 runtime->GetClassLinker(),
1027 dex_file,
1028 code_item,
1029 class_def_idx,
1030 method_idx,
1031 access_flags,
1032 /* verified_method */ nullptr, // Not needed by the Optimizing compiler.
1033 dex_cache);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001034 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001035 ArtMethod* method = compiler_driver->ResolveMethod(
1036 soa, dex_cache, jclass_loader, &dex_compilation_unit, method_idx, invoke_type);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001037 VariableSizedHandleScope handles(soa.Self());
1038 // Go to native so that we don't block GC during compilation.
1039 ScopedThreadSuspension sts(soa.Self(), kNative);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001040 if (method != nullptr && UNLIKELY(method->IsIntrinsic())) {
1041 DCHECK(compiler_driver->GetCompilerOptions().IsBootImage());
1042 codegen.reset(
1043 TryCompileIntrinsic(&allocator,
1044 &arena_stack,
1045 &code_allocator,
1046 dex_compilation_unit,
1047 method,
1048 &handles));
1049 if (codegen != nullptr) {
1050 compiled_intrinsic = true;
1051 }
1052 }
1053 if (codegen == nullptr) {
1054 codegen.reset(
1055 TryCompile(&allocator,
1056 &arena_stack,
1057 &code_allocator,
1058 dex_compilation_unit,
1059 method,
1060 /* osr */ false,
1061 &handles));
1062 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001063 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001064 if (codegen.get() != nullptr) {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001065 compiled_method = Emit(&allocator,
1066 &code_allocator,
1067 codegen.get(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001068 compiled_intrinsic ? nullptr : code_item);
1069 if (compiled_intrinsic) {
1070 compiled_method->MarkAsIntrinsic();
1071 }
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001072
1073 if (kArenaAllocatorCountAllocations) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001074 codegen.reset(); // Release codegen's ScopedArenaAllocator for memory accounting.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001075 size_t total_allocated = allocator.BytesAllocated() + arena_stack.PeakBytesAllocated();
1076 if (total_allocated > kArenaAllocatorMemoryReportThreshold) {
1077 MemStats mem_stats(allocator.GetMemStats());
1078 MemStats peak_stats(arena_stack.GetPeakStats());
Vladimir Marko69d310e2017-10-09 14:12:23 +01001079 LOG(INFO) << "Used " << total_allocated << " bytes of arena memory for compiling "
1080 << dex_file.PrettyMethod(method_idx)
Vladimir Markoca6fff82017-10-03 14:49:14 +01001081 << "\n" << Dumpable<MemStats>(mem_stats)
1082 << "\n" << Dumpable<MemStats>(peak_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001083 }
1084 }
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001085 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001086 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001087 MethodCompilationStat method_stat;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001088 if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001089 method_stat = MethodCompilationStat::kNotCompiledVerifyAtRuntime;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001090 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001091 method_stat = MethodCompilationStat::kNotCompiledVerificationError;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001092 }
Igor Murashkin1e065a52017-08-09 13:20:34 -07001093 MaybeRecordStat(compilation_stats_.get(), method_stat);
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001094 }
1095
Calin Juravlecff8cc72015-10-09 12:03:24 +01001096 if (kIsDebugBuild &&
1097 IsCompilingWithCoreImage() &&
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001098 IsInstructionSetSupported(compiler_driver->GetInstructionSet())) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001099 // For testing purposes, we put a special marker on method names
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001100 // that should be compiled with this compiler (when the
1101 // instruction set is supported). This makes sure we're not
Roland Levillain0d5a2812015-11-13 10:07:31 +00001102 // regressing.
David Sehr709b0702016-10-13 09:12:37 -07001103 std::string method_name = dex_file.PrettyMethod(method_idx);
Calin Juravle09b1d6f2015-10-07 12:08:54 +01001104 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001105 DCHECK((compiled_method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
Calin Juravle09b1d6f2015-10-07 12:08:54 +01001106 }
1107
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001108 return compiled_method;
1109}
1110
1111CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
1112 uint32_t method_idx,
1113 const DexFile& dex_file,
1114 Handle<mirror::DexCache> dex_cache) const {
1115 if (GetCompilerDriver()->GetCompilerOptions().IsBootImage()) {
1116 ScopedObjectAccess soa(Thread::Current());
1117 Runtime* runtime = Runtime::Current();
1118 ArtMethod* method = runtime->GetClassLinker()->LookupResolvedMethod(
1119 method_idx, dex_cache.Get(), /* class_loader */ nullptr);
1120 if (method != nullptr && UNLIKELY(method->IsIntrinsic())) {
1121 ScopedNullHandle<mirror::ClassLoader> class_loader; // null means boot class path loader.
1122 DexCompilationUnit dex_compilation_unit(
1123 class_loader,
1124 runtime->GetClassLinker(),
1125 dex_file,
1126 /* code_item */ nullptr,
1127 /* class_def_idx */ DexFile::kDexNoIndex16,
1128 method_idx,
1129 access_flags,
1130 /* verified_method */ nullptr,
1131 dex_cache);
1132 ArenaAllocator allocator(runtime->GetArenaPool());
1133 ArenaStack arena_stack(runtime->GetArenaPool());
1134 CodeVectorAllocator code_allocator(&allocator);
1135 VariableSizedHandleScope handles(soa.Self());
1136 // Go to native so that we don't block GC during compilation.
1137 ScopedThreadSuspension sts(soa.Self(), kNative);
1138 std::unique_ptr<CodeGenerator> codegen(
1139 TryCompileIntrinsic(&allocator,
1140 &arena_stack,
1141 &code_allocator,
1142 dex_compilation_unit,
1143 method,
1144 &handles));
1145 if (codegen != nullptr) {
1146 CompiledMethod* compiled_method = Emit(&allocator,
1147 &code_allocator,
1148 codegen.get(),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001149 /* code_item_for_osr_check */ nullptr);
1150 compiled_method->MarkAsIntrinsic();
1151 return compiled_method;
1152 }
1153 }
1154 }
1155
Vladimir Marko7bdc6e72017-11-28 12:37:13 +00001156 JniCompiledMethod jni_compiled_method = ArtQuickJniCompileMethod(
1157 GetCompilerDriver(), access_flags, method_idx, dex_file);
Vladimir Markocd09e1f2017-11-24 15:02:40 +00001158 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiledNativeStub);
Vladimir Marko7bdc6e72017-11-28 12:37:13 +00001159 return CompiledMethod::SwapAllocCompiledMethod(
1160 GetCompilerDriver(),
1161 jni_compiled_method.GetInstructionSet(),
1162 jni_compiled_method.GetCode(),
1163 jni_compiled_method.GetFrameSize(),
1164 jni_compiled_method.GetCoreSpillMask(),
1165 jni_compiled_method.GetFpSpillMask(),
1166 /* method_info */ ArrayRef<const uint8_t>(),
1167 /* vmap_table */ ArrayRef<const uint8_t>(),
1168 jni_compiled_method.GetCfi(),
1169 /* patches */ ArrayRef<const linker::LinkerPatch>());
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001170}
1171
Andreas Gampe53c913b2014-08-12 23:19:23 -07001172Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
1173 return new OptimizingCompiler(driver);
1174}
1175
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001176bool IsCompilingWithCoreImage() {
1177 const std::string& image = Runtime::Current()->GetImageLocation();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001178 return CompilerDriver::IsCoreImageFilename(image);
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001179}
1180
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00001181bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) {
1182 // Note: the runtime is null only for unit testing.
1183 return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler();
1184}
1185
1186bool CanEncodeInlinedMethodInStackMap(const DexFile& caller_dex_file, ArtMethod* callee) {
1187 if (!Runtime::Current()->IsAotCompiler()) {
1188 // JIT can always encode methods in stack maps.
1189 return true;
1190 }
1191 if (IsSameDexFile(caller_dex_file, *callee->GetDexFile())) {
1192 return true;
1193 }
1194 // TODO(ngeoffray): Support more AOT cases for inlining:
1195 // - methods in multidex
1196 // - methods in boot image for on-device non-PIC compilation.
1197 return false;
1198}
1199
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001200bool OptimizingCompiler::JitCompile(Thread* self,
1201 jit::JitCodeCache* code_cache,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001202 ArtMethod* method,
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001203 bool osr,
1204 jit::JitLogger* jit_logger) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001205 StackHandleScope<3> hs(self);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001206 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1207 method->GetDeclaringClass()->GetClassLoader()));
1208 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001209 DCHECK(method->IsCompilable());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001210
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001211 const DexFile* dex_file = method->GetDexFile();
1212 const uint16_t class_def_idx = method->GetClassDefIndex();
1213 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1214 const uint32_t method_idx = method->GetDexMethodIndex();
1215 const uint32_t access_flags = method->GetAccessFlags();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001216
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001217 Runtime* runtime = Runtime::Current();
1218 ArenaAllocator allocator(runtime->GetJitArenaPool());
Vladimir Marko2196c652017-11-30 16:16:07 +00001219
1220 if (UNLIKELY(method->IsNative())) {
1221 JniCompiledMethod jni_compiled_method = ArtQuickJniCompileMethod(
1222 GetCompilerDriver(), access_flags, method_idx, *dex_file);
1223 ScopedNullHandle<mirror::ObjectArray<mirror::Object>> roots;
1224 ArenaSet<ArtMethod*, std::less<ArtMethod*>> cha_single_implementation_list(
1225 allocator.Adapter(kArenaAllocCHA));
1226 const void* code = code_cache->CommitCode(
1227 self,
1228 method,
1229 /* stack_map_data */ nullptr,
1230 /* method_info_data */ nullptr,
1231 /* roots_data */ nullptr,
1232 jni_compiled_method.GetFrameSize(),
1233 jni_compiled_method.GetCoreSpillMask(),
1234 jni_compiled_method.GetFpSpillMask(),
1235 jni_compiled_method.GetCode().data(),
1236 jni_compiled_method.GetCode().size(),
1237 /* data_size */ 0u,
1238 osr,
1239 roots,
1240 /* has_should_deoptimize_flag */ false,
1241 cha_single_implementation_list);
1242 if (code == nullptr) {
1243 return false;
1244 }
1245
1246 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
David Srbeckyf4886df2017-12-11 16:06:29 +00001247 if (compiler_options.GenerateAnyDebugInfo()) {
Vladimir Marko2196c652017-11-30 16:16:07 +00001248 const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
1249 const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
1250 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +00001251 DCHECK(info.custom_name.empty());
Vladimir Marko2196c652017-11-30 16:16:07 +00001252 info.dex_file = dex_file;
1253 info.class_def_index = class_def_idx;
1254 info.dex_method_index = method_idx;
1255 info.access_flags = access_flags;
1256 info.code_item = code_item;
1257 info.isa = jni_compiled_method.GetInstructionSet();
1258 info.deduped = false;
1259 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
1260 info.is_optimized = true;
1261 info.is_code_address_text_relative = false;
1262 info.code_address = code_address;
1263 info.code_size = jni_compiled_method.GetCode().size();
1264 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
1265 info.code_info = nullptr;
1266 info.cfi = jni_compiled_method.GetCfi();
David Srbeckyc9e02082018-01-24 16:44:02 +00001267 GenerateJitDebugInfo(method, info);
Vladimir Marko2196c652017-11-30 16:16:07 +00001268 }
1269
1270 Runtime::Current()->GetJit()->AddMemoryUsage(method, allocator.BytesUsed());
1271 if (jit_logger != nullptr) {
1272 jit_logger->WriteLog(code, jni_compiled_method.GetCode().size(), method);
1273 }
1274 return true;
1275 }
1276
1277 ArenaStack arena_stack(runtime->GetJitArenaPool());
Vladimir Markoca6fff82017-10-03 14:49:14 +01001278 CodeVectorAllocator code_allocator(&allocator);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001279 VariableSizedHandleScope handles(self);
1280
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001281 std::unique_ptr<CodeGenerator> codegen;
1282 {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001283 DexCompilationUnit dex_compilation_unit(
1284 class_loader,
1285 runtime->GetClassLinker(),
1286 *dex_file,
1287 code_item,
1288 class_def_idx,
1289 method_idx,
1290 access_flags,
1291 /* verified_method */ nullptr,
1292 dex_cache);
1293
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001294 // Go to native so that we don't block GC during compilation.
1295 ScopedThreadSuspension sts(self, kNative);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001296 codegen.reset(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001297 TryCompile(&allocator,
1298 &arena_stack,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001299 &code_allocator,
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001300 dex_compilation_unit,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +00001301 method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001302 osr,
1303 &handles));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001304 if (codegen.get() == nullptr) {
1305 return false;
1306 }
1307 }
1308
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001309 size_t stack_map_size = 0;
1310 size_t method_info_size = 0;
1311 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001312 size_t number_of_roots = codegen->GetNumberOfJitRoots();
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001313 // We allocate an object array to ensure the JIT roots that we will collect in EmitJitRoots
1314 // will be visible by the GC between EmitLiterals and CommitCode. Once CommitCode is
1315 // executed, this array is not needed.
1316 Handle<mirror::ObjectArray<mirror::Object>> roots(
1317 hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc(
Vladimir Markob4eb1b12018-05-24 11:09:38 +01001318 self, GetClassRoot<mirror::ObjectArray<mirror::Object>>(), number_of_roots)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001319 if (roots == nullptr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001320 // Out of memory, just clear the exception to avoid any Java exception uncaught problems.
Vladimir Markocd09e1f2017-11-24 15:02:40 +00001321 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001322 DCHECK(self->IsExceptionPending());
1323 self->ClearException();
1324 return false;
1325 }
1326 uint8_t* stack_map_data = nullptr;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001327 uint8_t* method_info_data = nullptr;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001328 uint8_t* roots_data = nullptr;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001329 uint32_t data_size = code_cache->ReserveData(self,
1330 stack_map_size,
1331 method_info_size,
1332 number_of_roots,
1333 method,
1334 &stack_map_data,
1335 &method_info_data,
1336 &roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001337 if (stack_map_data == nullptr || roots_data == nullptr) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +00001338 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001339 return false;
1340 }
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001341 codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size),
1342 MemoryRegion(method_info_data, method_info_size),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +00001343 code_item);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00001344 codegen->EmitJitRoots(code_allocator.GetData(), roots, roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001345
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001346 const void* code = code_cache->CommitCode(
1347 self,
1348 method,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001349 stack_map_data,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001350 method_info_data,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001351 roots_data,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001352 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
1353 codegen->GetCoreSpillMask(),
1354 codegen->GetFpuSpillMask(),
1355 code_allocator.GetMemory().data(),
Vladimir Markoca1e0382018-04-11 09:58:41 +00001356 code_allocator.GetMemory().size(),
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001357 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001358 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -07001359 roots,
1360 codegen->GetGraph()->HasShouldDeoptimizeFlag(),
1361 codegen->GetGraph()->GetCHASingleImplementationList());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001362
1363 if (code == nullptr) {
Vladimir Markocd09e1f2017-11-24 15:02:40 +00001364 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kJitOutOfMemoryForCommit);
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001365 code_cache->ClearData(self, stack_map_data, roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001366 return false;
1367 }
1368
David Srbecky91cc06c2016-03-07 16:13:58 +00001369 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
David Srbeckyf4886df2017-12-11 16:06:29 +00001370 if (compiler_options.GenerateAnyDebugInfo()) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001371 const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
1372 const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
Vladimir Marko1b404a82017-09-01 13:35:26 +01001373 debug::MethodDebugInfo info = {};
David Srbeckyc684f332018-01-19 17:38:06 +00001374 DCHECK(info.custom_name.empty());
David Srbecky197160d2016-03-07 17:33:57 +00001375 info.dex_file = dex_file;
1376 info.class_def_index = class_def_idx;
1377 info.dex_method_index = method_idx;
1378 info.access_flags = access_flags;
1379 info.code_item = code_item;
1380 info.isa = codegen->GetInstructionSet();
1381 info.deduped = false;
1382 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
1383 info.is_optimized = true;
1384 info.is_code_address_text_relative = false;
1385 info.code_address = code_address;
Vladimir Markoca1e0382018-04-11 09:58:41 +00001386 info.code_size = code_allocator.GetMemory().size();
David Srbecky197160d2016-03-07 17:33:57 +00001387 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
1388 info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
1389 info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
David Srbeckyc9e02082018-01-24 16:44:02 +00001390 GenerateJitDebugInfo(method, info);
David Srbecky5cc349f2015-12-18 15:04:48 +00001391 }
1392
Vladimir Markoca6fff82017-10-03 14:49:14 +01001393 Runtime::Current()->GetJit()->AddMemoryUsage(method, allocator.BytesUsed());
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001394 if (jit_logger != nullptr) {
Vladimir Markoca1e0382018-04-11 09:58:41 +00001395 jit_logger->WriteLog(code, code_allocator.GetMemory().size(), method);
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001396 }
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001397
Vladimir Marko174b2e22017-10-12 13:34:49 +01001398 if (kArenaAllocatorCountAllocations) {
1399 codegen.reset(); // Release codegen's ScopedArenaAllocator for memory accounting.
1400 size_t total_allocated = allocator.BytesAllocated() + arena_stack.PeakBytesAllocated();
1401 if (total_allocated > kArenaAllocatorMemoryReportThreshold) {
1402 MemStats mem_stats(allocator.GetMemStats());
1403 MemStats peak_stats(arena_stack.GetPeakStats());
1404 LOG(INFO) << "Used " << total_allocated << " bytes of arena memory for compiling "
1405 << dex_file->PrettyMethod(method_idx)
1406 << "\n" << Dumpable<MemStats>(mem_stats)
1407 << "\n" << Dumpable<MemStats>(peak_stats);
1408 }
1409 }
1410
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001411 return true;
1412}
1413
David Srbeckyc9e02082018-01-24 16:44:02 +00001414void OptimizingCompiler::GenerateJitDebugInfo(ArtMethod* method, debug::MethodDebugInfo info) {
David Srbeckyc684f332018-01-19 17:38:06 +00001415 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
1416 DCHECK(compiler_options.GenerateAnyDebugInfo());
1417
1418 // If both flags are passed, generate full debug info.
1419 const bool mini_debug_info = !compiler_options.GetGenerateDebugInfo();
1420
1421 // Create entry for the single method that we just compiled.
1422 std::vector<uint8_t> elf_file = debug::MakeElfFileForJIT(
1423 GetCompilerDriver()->GetInstructionSet(),
1424 GetCompilerDriver()->GetInstructionSetFeatures(),
1425 mini_debug_info,
1426 ArrayRef<const debug::MethodDebugInfo>(&info, 1));
David Srbeckyfb3de3d2018-01-29 16:11:49 +00001427 MutexLock mu(Thread::Current(), *Locks::native_debug_interface_lock_);
David Srbecky440a9b32018-02-15 17:47:29 +00001428 AddNativeDebugInfoForJit(reinterpret_cast<const void*>(info.code_address), elf_file);
David Srbeckyc9e02082018-01-24 16:44:02 +00001429
1430 VLOG(jit)
1431 << "JIT mini-debug-info added for " << ArtMethod::PrettyMethod(method)
1432 << " size=" << PrettySize(elf_file.size())
David Srbecky440a9b32018-02-15 17:47:29 +00001433 << " total_size=" << PrettySize(GetJitNativeDebugInfoMemUsage());
David Srbeckyc684f332018-01-19 17:38:06 +00001434}
1435
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001436} // namespace art