blob: 1e06ea86a2d04eeaa3d82b469d2a2aa47ac98cf7 [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
Lena Djokica2901602017-09-21 13:50:52 +020030#include "instruction_simplifier_mips.h"
Alexey Frunzee3fb2452016-05-10 16:08:05 -070031#include "pc_relative_fixups_mips.h"
32#endif
33
Mark Mendell0616ae02015-04-17 12:49:27 -040034#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Marko0f7dca42015-11-02 14:36:43 +000035#include "pc_relative_fixups_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -040036#endif
37
Mark Mendellee8d9712016-07-12 11:13:15 -040038#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
39#include "x86_memory_gen.h"
40#endif
41
Mathieu Chartiere401d142015-04-22 13:56:20 -070042#include "art_method-inl.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080043#include "base/arena_allocator.h"
Vladimir Markof9f64412015-09-02 14:05:49 +010044#include "base/arena_containers.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000045#include "base/dumpable.h"
Vladimir Markoc90d7c72015-10-06 17:30:45 +000046#include "base/macros.h"
Andreas Gampea0d81af2016-10-27 12:04:57 -070047#include "base/mutex.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000048#include "base/timing_logger.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070049#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050#include "builder.h"
Mingyao Yangb0b051a2016-11-17 09:04:53 -080051#include "cha_guard_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000052#include "code_generator.h"
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +000053#include "code_sinking.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000054#include "compiled_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070055#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010056#include "constant_folding.h"
Igor Murashkindd018df2017-08-09 10:38:31 -070057#include "constructor_fence_redundancy_elimination.h"
Roland Levillain75be2832014-10-17 17:02:00 +010058#include "dead_code_elimination.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000059#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000060#include "debug/method_debug_info.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010061#include "dex/verification_results.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000062#include "dex/verified_method.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080063#include "dex_file_types.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000064#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000065#include "driver/compiler_options.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000066#include "driver/dex_compilation_unit.h"
David Brazdil69ba7b72015-06-23 18:27:30 +010067#include "graph_checker.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010068#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010069#include "gvn.h"
Aart Bik22af3be2015-09-10 12:50:58 -070070#include "induction_var_analysis.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000071#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010072#include "instruction_simplifier.h"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030073#include "instruction_simplifier_arm.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080074#include "intrinsics.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000075#include "jit/debugger_interface.h"
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000076#include "jit/jit.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000077#include "jit/jit_code_cache.h"
Nicolas Geoffray01db5f72017-07-19 15:05:49 +010078#include "jit/jit_logger.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000079#include "jni/quick/jni_compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000080#include "licm.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010081#include "linker/linker_patch.h"
xueliang.zhongc239a2b2017-04-27 15:31:37 +010082#include "load_store_analysis.h"
Mingyao Yang8df69d42015-10-22 15:40:58 -070083#include "load_store_elimination.h"
Aart Bik281c6812016-08-26 11:31:48 -070084#include "loop_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000085#include "nodes.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000086#include "oat_quick_method_header.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010087#include "prepare_for_register_allocation.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010088#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070089#include "register_allocator_linear_scan.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010090#include "scheduler.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070091#include "select_generator.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010092#include "sharpening.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000093#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000094#include "ssa_builder.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095#include "ssa_liveness_analysis.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000096#include "ssa_phi_elimination.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010097#include "utils/assembler.h"
Andreas Gampe6d7abbd2017-04-24 13:19:09 -070098#include "verifier/verifier_compiler_binding.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000099
100namespace art {
101
Vladimir Marko3a40bf22016-03-22 16:26:33 +0000102static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
103
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700104static constexpr const char* kPassNameSeparator = "$";
105
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106/**
107 * Used by the code generator, to allocate the code in a vector.
108 */
109class CodeVectorAllocator FINAL : public CodeAllocator {
110 public:
Vladimir Markof9f64412015-09-02 14:05:49 +0100111 explicit CodeVectorAllocator(ArenaAllocator* arena)
112 : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
113 size_(0) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114
115 virtual uint8_t* Allocate(size_t size) {
116 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000117 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 return &memory_[0];
119 }
120
121 size_t GetSize() const { return size_; }
Vladimir Markof9f64412015-09-02 14:05:49 +0100122 const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000123 uint8_t* GetData() { return memory_.data(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000124
125 private:
Vladimir Markof9f64412015-09-02 14:05:49 +0100126 ArenaVector<uint8_t> memory_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000127 size_t size_;
128
129 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
130};
131
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100132/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100133 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +0000134 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100135 */
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700136static constexpr const char kStringFilter[] = "";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100137
David Brazdil69ba7b72015-06-23 18:27:30 +0100138class PassScope;
David Brazdil809658e2015-02-05 11:34:02 +0000139
David Brazdil69ba7b72015-06-23 18:27:30 +0100140class PassObserver : public ValueObject {
David Brazdil5e8b1372015-01-23 14:39:08 +0000141 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100142 PassObserver(HGraph* graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100143 CodeGenerator* codegen,
144 std::ostream* visualizer_output,
Andreas Gampea0d81af2016-10-27 12:04:57 -0700145 CompilerDriver* compiler_driver,
146 Mutex& dump_mutex)
David Brazdil69ba7b72015-06-23 18:27:30 +0100147 : graph_(graph),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000148 cached_method_name_(),
David Brazdil809658e2015-02-05 11:34:02 +0000149 timing_logger_enabled_(compiler_driver->GetDumpPasses()),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000150 timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100151 disasm_info_(graph->GetArena()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700152 visualizer_oss_(),
153 visualizer_output_(visualizer_output),
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000154 visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700155 visualizer_(&visualizer_oss_, graph, *codegen),
156 visualizer_dump_mutex_(dump_mutex),
David Brazdil69ba7b72015-06-23 18:27:30 +0100157 graph_in_bad_state_(false) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700158 if (timing_logger_enabled_ || visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000159 if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700160 timing_logger_enabled_ = visualizer_enabled_ = false;
161 }
162 if (visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000163 visualizer_.PrintHeader(GetMethodName());
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700164 codegen->SetDisassemblyInformation(&disasm_info_);
165 }
David Brazdil62e074f2015-04-07 18:09:37 +0100166 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000167 }
168
David Brazdil69ba7b72015-06-23 18:27:30 +0100169 ~PassObserver() {
David Brazdil5e8b1372015-01-23 14:39:08 +0000170 if (timing_logger_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000171 LOG(INFO) << "TIMINGS " << GetMethodName();
David Brazdil5e8b1372015-01-23 14:39:08 +0000172 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
173 }
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000174 DCHECK(visualizer_oss_.str().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000175 }
176
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000177 void DumpDisassembly() REQUIRES(!visualizer_dump_mutex_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100178 if (visualizer_enabled_) {
179 visualizer_.DumpGraphWithDisassembly();
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000180 FlushVisualizer();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100181 }
182 }
183
David Brazdil69ba7b72015-06-23 18:27:30 +0100184 void SetGraphInBadState() { graph_in_bad_state_ = true; }
185
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000186 const char* GetMethodName() {
187 // PrettyMethod() is expensive, so we delay calling it until we actually have to.
188 if (cached_method_name_.empty()) {
David Sehr709b0702016-10-13 09:12:37 -0700189 cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx());
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000190 }
191 return cached_method_name_.c_str();
192 }
193
David Brazdil5e8b1372015-01-23 14:39:08 +0000194 private:
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000195 void StartPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
Wojciech Staszkiewicze7060702016-08-16 17:31:19 -0700196 VLOG(compiler) << "Starting pass: " << pass_name;
David Brazdil809658e2015-02-05 11:34:02 +0000197 // Dump graph first, then start timer.
198 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100199 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000200 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000201 }
202 if (timing_logger_enabled_) {
203 timing_logger_.StartTiming(pass_name);
204 }
205 }
206
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000207 void FlushVisualizer() REQUIRES(!visualizer_dump_mutex_) {
208 MutexLock mu(Thread::Current(), visualizer_dump_mutex_);
209 *visualizer_output_ << visualizer_oss_.str();
210 visualizer_output_->flush();
211 visualizer_oss_.str("");
212 visualizer_oss_.clear();
213 }
214
215 void EndPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
David Brazdil809658e2015-02-05 11:34:02 +0000216 // Pause timer first, then dump graph.
217 if (timing_logger_enabled_) {
218 timing_logger_.EndTiming();
219 }
220 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100221 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000222 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000223 }
David Brazdil69ba7b72015-06-23 18:27:30 +0100224
225 // Validate the HGraph if running in debug mode.
226 if (kIsDebugBuild) {
227 if (!graph_in_bad_state_) {
David Brazdilbadd8262016-02-02 16:28:56 +0000228 GraphChecker checker(graph_);
229 checker.Run();
230 if (!checker.IsValid()) {
231 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
David Brazdil69ba7b72015-06-23 18:27:30 +0100232 }
233 }
234 }
David Brazdil809658e2015-02-05 11:34:02 +0000235 }
236
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700237 static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
238 // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
239 // empty kStringFilter matching all methods.
240 if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
241 return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
242 }
243
244 // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
245 // warning when the string is empty.
246 constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
247 if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
248 return true;
249 }
250
251 return false;
252 }
253
David Brazdil69ba7b72015-06-23 18:27:30 +0100254 HGraph* const graph_;
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000255
256 std::string cached_method_name_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000257
258 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000259 TimingLogger timing_logger_;
260
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100261 DisassemblyInformation disasm_info_;
262
Andreas Gampea0d81af2016-10-27 12:04:57 -0700263 std::ostringstream visualizer_oss_;
264 std::ostream* visualizer_output_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000265 bool visualizer_enabled_;
266 HGraphVisualizer visualizer_;
Andreas Gampea0d81af2016-10-27 12:04:57 -0700267 Mutex& visualizer_dump_mutex_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000268
David Brazdil69ba7b72015-06-23 18:27:30 +0100269 // Flag to be set by the compiler if the pass failed and the graph is not
270 // expected to validate.
271 bool graph_in_bad_state_;
David Brazdil809658e2015-02-05 11:34:02 +0000272
David Brazdil69ba7b72015-06-23 18:27:30 +0100273 friend PassScope;
274
275 DISALLOW_COPY_AND_ASSIGN(PassObserver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000276};
277
David Brazdil69ba7b72015-06-23 18:27:30 +0100278class PassScope : public ValueObject {
David Brazdil809658e2015-02-05 11:34:02 +0000279 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100280 PassScope(const char *pass_name, PassObserver* pass_observer)
David Brazdil809658e2015-02-05 11:34:02 +0000281 : pass_name_(pass_name),
David Brazdil69ba7b72015-06-23 18:27:30 +0100282 pass_observer_(pass_observer) {
283 pass_observer_->StartPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000284 }
285
David Brazdil69ba7b72015-06-23 18:27:30 +0100286 ~PassScope() {
287 pass_observer_->EndPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000288 }
289
290 private:
291 const char* const pass_name_;
David Brazdil69ba7b72015-06-23 18:27:30 +0100292 PassObserver* const pass_observer_;
David Brazdil809658e2015-02-05 11:34:02 +0000293};
294
Andreas Gampe53c913b2014-08-12 23:19:23 -0700295class OptimizingCompiler FINAL : public Compiler {
296 public:
297 explicit OptimizingCompiler(CompilerDriver* driver);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700298 ~OptimizingCompiler() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700299
Vladimir Markodf739842016-03-23 16:59:07 +0000300 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700301
302 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
303 uint32_t access_flags,
304 InvokeType invoke_type,
305 uint16_t class_def_idx,
306 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000307 Handle<mirror::ClassLoader> class_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700308 const DexFile& dex_file,
309 Handle<mirror::DexCache> dex_cache) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700310
Andreas Gampe53c913b2014-08-12 23:19:23 -0700311 CompiledMethod* JniCompile(uint32_t access_flags,
312 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700313 const DexFile& dex_file,
314 JniOptimizationFlags optimization_flags) const OVERRIDE {
315 return ArtQuickJniCompileMethod(GetCompilerDriver(),
316 access_flags,
317 method_idx,
318 dex_file,
319 optimization_flags);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000320 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700321
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700323 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000324 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
325 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
326 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700327
David Brazdilee690a32014-12-01 17:04:16 +0000328 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700329
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000330 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700331
Nicolas Geoffray01db5f72017-07-19 15:05:49 +0100332 bool JitCompile(Thread* self,
333 jit::JitCodeCache* code_cache,
334 ArtMethod* method,
335 bool osr,
336 jit::JitLogger* jit_logger)
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000337 OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700338 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000339
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700340 private:
341 void RunOptimizations(HGraph* graph,
342 CodeGenerator* codegen,
343 CompilerDriver* driver,
344 const DexCompilationUnit& dex_compilation_unit,
345 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700346 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700347
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700348 void RunOptimizations(HOptimization* optimizations[],
349 size_t length,
350 PassObserver* pass_observer) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700351
Andreas Gampe53c913b2014-08-12 23:19:23 -0700352 private:
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000353 // Create a 'CompiledMethod' for an optimized graph.
David Brazdil58282f42016-01-14 12:45:10 +0000354 CompiledMethod* Emit(ArenaAllocator* arena,
355 CodeVectorAllocator* code_allocator,
356 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000357 CompilerDriver* driver,
358 const DexFile::CodeItem* item) const;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000359
360 // Try compiling a method and return the code generator used for
361 // compiling it.
362 // This method:
363 // 1) Builds the graph. Returns null if it failed to build it.
David Brazdil58282f42016-01-14 12:45:10 +0000364 // 2) Transforms the graph to SSA. Returns null if it failed.
365 // 3) Runs optimizations on the graph, including register allocator.
366 // 4) Generates code with the `code_allocator` provided.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000367 CodeGenerator* TryCompile(ArenaAllocator* arena,
368 CodeVectorAllocator* code_allocator,
369 const DexFile::CodeItem* code_item,
370 uint32_t access_flags,
371 InvokeType invoke_type,
372 uint16_t class_def_idx,
373 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000374 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000375 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000376 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000377 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000378 bool osr,
379 VariableSizedHandleScope* handles) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000380
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700381 void MaybeRunInliner(HGraph* graph,
382 CodeGenerator* codegen,
383 CompilerDriver* driver,
384 const DexCompilationUnit& dex_compilation_unit,
385 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700386 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700387
388 void RunArchOptimizations(InstructionSet instruction_set,
389 HGraph* graph,
390 CodeGenerator* codegen,
391 PassObserver* pass_observer) const;
392
Calin Juravle2be39e02015-04-21 13:56:34 +0100393 std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100394
Andreas Gampe53c913b2014-08-12 23:19:23 -0700395 std::unique_ptr<std::ostream> visualizer_output_;
396
Andreas Gampea0d81af2016-10-27 12:04:57 -0700397 mutable Mutex dump_mutex_; // To synchronize visualizer writing.
398
Andreas Gampe53c913b2014-08-12 23:19:23 -0700399 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
400};
401
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100402static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
403
404OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
Andreas Gampea0d81af2016-10-27 12:04:57 -0700405 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
406 dump_mutex_("Visualizer dump lock") {}
David Brazdilee690a32014-12-01 17:04:16 +0000407
408void OptimizingCompiler::Init() {
409 // Enable C1visualizer output. Must be done in Init() because the compiler
410 // driver is not fully initialized when passed to the compiler's constructor.
411 CompilerDriver* driver = GetCompilerDriver();
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000412 const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
David Brazdil866c0312015-01-13 21:21:31 +0000413 if (!cfg_file_name.empty()) {
Calin Juravle87000a92015-08-24 15:34:44 +0100414 std::ios_base::openmode cfg_file_mode =
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000415 driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
Calin Juravle87000a92015-08-24 15:34:44 +0100416 visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100417 }
Calin Juravle2be39e02015-04-21 13:56:34 +0100418 if (driver->GetDumpStats()) {
419 compilation_stats_.reset(new OptimizingCompilerStats());
420 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100421}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000422
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000423void OptimizingCompiler::UnInit() const {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000424}
425
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100426OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle2be39e02015-04-21 13:56:34 +0100427 if (compilation_stats_.get() != nullptr) {
428 compilation_stats_->Log();
429 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100430}
431
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000432bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
Vladimir Markodf739842016-03-23 16:59:07 +0000433 const DexFile& dex_file ATTRIBUTE_UNUSED) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000434 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700435}
436
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000437static bool IsInstructionSetSupported(InstructionSet instruction_set) {
Andreas Gampe217d6d32017-09-18 12:48:20 -0700438 return instruction_set == kArm
Calin Juravlecff8cc72015-10-09 12:03:24 +0100439 || instruction_set == kArm64
Andreas Gampe217d6d32017-09-18 12:48:20 -0700440 || instruction_set == kThumb2
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441 || instruction_set == kMips
Alexey Frunze4dda3372015-06-01 18:31:49 -0700442 || instruction_set == kMips64
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000443 || instruction_set == kX86
444 || instruction_set == kX86_64;
445}
446
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700447// Strip pass name suffix to get optimization name.
448static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) {
449 size_t pos = pass_name.find(kPassNameSeparator);
450 return pos == std::string::npos ? pass_name : pass_name.substr(0, pos);
451}
452
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700453static HOptimization* BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700454 const std::string& pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700455 ArenaAllocator* arena,
456 HGraph* graph,
457 OptimizingCompilerStats* stats,
458 CodeGenerator* codegen,
459 CompilerDriver* driver,
460 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700461 VariableSizedHandleScope* handles,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700462 SideEffectsAnalysis* most_recent_side_effects,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100463 HInductionVarAnalysis* most_recent_induction,
464 LoadStoreAnalysis* most_recent_lsa) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700465 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700466 if (opt_name == BoundsCheckElimination::kBoundsCheckEliminationPassName) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700467 CHECK(most_recent_side_effects != nullptr && most_recent_induction != nullptr);
468 return new (arena) BoundsCheckElimination(graph,
469 *most_recent_side_effects,
470 most_recent_induction);
471 } else if (opt_name == GVNOptimization::kGlobalValueNumberingPassName) {
472 CHECK(most_recent_side_effects != nullptr);
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700473 return new (arena) GVNOptimization(graph, *most_recent_side_effects, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700474 } else if (opt_name == HConstantFolding::kConstantFoldingPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700475 return new (arena) HConstantFolding(graph, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700476 } else if (opt_name == HDeadCodeElimination::kDeadCodeEliminationPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700477 return new (arena) HDeadCodeElimination(graph, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700478 } else if (opt_name == HInliner::kInlinerPassName) {
479 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
480 return new (arena) HInliner(graph, // outer_graph
481 graph, // outermost_graph
482 codegen,
483 dex_compilation_unit, // outer_compilation_unit
484 dex_compilation_unit, // outermost_compilation_unit
485 driver,
486 handles,
487 stats,
488 number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000489 /* total_number_of_instructions */ 0,
490 /* parent */ nullptr);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700491 } else if (opt_name == HSharpening::kSharpeningPassName) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000492 return new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver, handles);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700493 } else if (opt_name == HSelectGenerator::kSelectGeneratorPassName) {
Mads Ager16e52892017-07-14 13:11:37 +0200494 return new (arena) HSelectGenerator(graph, handles, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700495 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
496 return new (arena) HInductionVarAnalysis(graph);
497 } else if (opt_name == InstructionSimplifier::kInstructionSimplifierPassName) {
Vladimir Marko65979462017-05-19 17:25:12 +0100498 return new (arena) InstructionSimplifier(graph, codegen, driver, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700499 } else if (opt_name == IntrinsicsRecognizer::kIntrinsicsRecognizerPassName) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100500 return new (arena) IntrinsicsRecognizer(graph, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700501 } else if (opt_name == LICM::kLoopInvariantCodeMotionPassName) {
502 CHECK(most_recent_side_effects != nullptr);
503 return new (arena) LICM(graph, *most_recent_side_effects, stats);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100504 } else if (opt_name == LoadStoreAnalysis::kLoadStoreAnalysisPassName) {
505 return new (arena) LoadStoreAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700506 } else if (opt_name == LoadStoreElimination::kLoadStoreEliminationPassName) {
507 CHECK(most_recent_side_effects != nullptr);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100508 CHECK(most_recent_lsa != nullptr);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700509 return
510 new (arena) LoadStoreElimination(graph, *most_recent_side_effects, *most_recent_lsa, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700511 } else if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
512 return new (arena) SideEffectsAnalysis(graph);
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100513 } else if (opt_name == HLoopOptimization::kLoopOptimizationPassName) {
Aart Bikb92cc332017-09-06 15:53:17 -0700514 return new (arena) HLoopOptimization(graph, driver, most_recent_induction, stats);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800515 } else if (opt_name == CHAGuardOptimization::kCHAGuardOptimizationPassName) {
516 return new (arena) CHAGuardOptimization(graph);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000517 } else if (opt_name == CodeSinking::kCodeSinkingPassName) {
518 return new (arena) CodeSinking(graph, stats);
Igor Murashkindd018df2017-08-09 10:38:31 -0700519 } else if (opt_name == ConstructorFenceRedundancyElimination::kPassName) {
520 return new (arena) ConstructorFenceRedundancyElimination(graph, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700521#ifdef ART_ENABLE_CODEGEN_arm
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700522 } else if (opt_name == arm::InstructionSimplifierArm::kInstructionSimplifierArmPassName) {
523 return new (arena) arm::InstructionSimplifierArm(graph, stats);
524#endif
525#ifdef ART_ENABLE_CODEGEN_arm64
526 } else if (opt_name == arm64::InstructionSimplifierArm64::kInstructionSimplifierArm64PassName) {
527 return new (arena) arm64::InstructionSimplifierArm64(graph, stats);
528#endif
529#ifdef ART_ENABLE_CODEGEN_mips
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700530 } else if (opt_name == mips::PcRelativeFixups::kPcRelativeFixupsMipsPassName) {
531 return new (arena) mips::PcRelativeFixups(graph, codegen, stats);
Lena Djokica2901602017-09-21 13:50:52 +0200532 } else if (opt_name == mips::InstructionSimplifierMips::kInstructionSimplifierMipsPassName) {
533 return new (arena) mips::InstructionSimplifierMips(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700534#endif
535#ifdef ART_ENABLE_CODEGEN_x86
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700536 } else if (opt_name == x86::PcRelativeFixups::kPcRelativeFixupsX86PassName) {
537 return new (arena) x86::PcRelativeFixups(graph, codegen, stats);
538 } else if (opt_name == x86::X86MemoryOperandGeneration::kX86MemoryOperandGenerationPassName) {
539 return new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700540#endif
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700541 }
542 return nullptr;
543}
544
545static ArenaVector<HOptimization*> BuildOptimizations(
546 const std::vector<std::string>& pass_names,
547 ArenaAllocator* arena,
548 HGraph* graph,
549 OptimizingCompilerStats* stats,
550 CodeGenerator* codegen,
551 CompilerDriver* driver,
552 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700553 VariableSizedHandleScope* handles) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700554 // Few HOptimizations constructors require SideEffectsAnalysis or HInductionVarAnalysis
555 // instances. This method assumes that each of them expects the nearest instance preceeding it
556 // in the pass name list.
557 SideEffectsAnalysis* most_recent_side_effects = nullptr;
558 HInductionVarAnalysis* most_recent_induction = nullptr;
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100559 LoadStoreAnalysis* most_recent_lsa = nullptr;
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700560 ArenaVector<HOptimization*> ret(arena->Adapter());
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700561 for (const std::string& pass_name : pass_names) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700562 HOptimization* opt = BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700563 pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700564 arena,
565 graph,
566 stats,
567 codegen,
568 driver,
569 dex_compilation_unit,
570 handles,
571 most_recent_side_effects,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100572 most_recent_induction,
573 most_recent_lsa);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700574 CHECK(opt != nullptr) << "Couldn't build optimization: \"" << pass_name << "\"";
575 ret.push_back(opt);
576
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700577 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700578 if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
579 most_recent_side_effects = down_cast<SideEffectsAnalysis*>(opt);
580 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
581 most_recent_induction = down_cast<HInductionVarAnalysis*>(opt);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100582 } else if (opt_name == LoadStoreAnalysis::kLoadStoreAnalysisPassName) {
583 most_recent_lsa = down_cast<LoadStoreAnalysis*>(opt);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700584 }
585 }
586 return ret;
587}
588
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700589void OptimizingCompiler::RunOptimizations(HOptimization* optimizations[],
590 size_t length,
591 PassObserver* pass_observer) const {
Calin Juravle10e244f2015-01-26 18:54:32 +0000592 for (size_t i = 0; i < length; ++i) {
David Brazdil69ba7b72015-06-23 18:27:30 +0100593 PassScope scope(optimizations[i]->GetPassName(), pass_observer);
594 optimizations[i]->Run();
Calin Juravle10e244f2015-01-26 18:54:32 +0000595 }
596}
597
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700598void OptimizingCompiler::MaybeRunInliner(HGraph* graph,
599 CodeGenerator* codegen,
600 CompilerDriver* driver,
601 const DexCompilationUnit& dex_compilation_unit,
602 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700603 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700604 OptimizingCompilerStats* stats = compilation_stats_.get();
Calin Juravleec748352015-07-29 13:52:12 +0100605 const CompilerOptions& compiler_options = driver->GetCompilerOptions();
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000606 bool should_inline = (compiler_options.GetInlineMaxCodeUnits() > 0);
Calin Juravleec748352015-07-29 13:52:12 +0100607 if (!should_inline) {
608 return;
609 }
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000610 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000611 HInliner* inliner = new (graph->GetArena()) HInliner(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700612 graph, // outer_graph
613 graph, // outermost_graph
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000614 codegen,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700615 dex_compilation_unit, // outer_compilation_unit
616 dex_compilation_unit, // outermost_compilation_unit
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000617 driver,
618 handles,
619 stats,
620 number_of_dex_registers,
Nicolas Geoffrayf6d46682017-02-28 17:41:45 +0000621 /* total_number_of_instructions */ 0,
622 /* parent */ nullptr);
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000623 HOptimization* optimizations[] = { inliner };
Calin Juravleec748352015-07-29 13:52:12 +0100624
625 RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
626}
627
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700628void OptimizingCompiler::RunArchOptimizations(InstructionSet instruction_set,
629 HGraph* graph,
630 CodeGenerator* codegen,
631 PassObserver* pass_observer) const {
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700632 UNUSED(codegen); // To avoid compilation error when compiling for svelte
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700633 OptimizingCompilerStats* stats = compilation_stats_.get();
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100634 ArenaAllocator* arena = graph->GetArena();
635 switch (instruction_set) {
Artem Serov2bbc9532016-10-21 11:51:50 +0100636#if defined(ART_ENABLE_CODEGEN_arm)
Vladimir Markob4536b72015-11-24 13:45:23 +0000637 case kThumb2:
638 case kArm: {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300639 arm::InstructionSimplifierArm* simplifier =
640 new (arena) arm::InstructionSimplifierArm(graph, stats);
Artem Serov328429f2016-07-06 16:23:04 +0100641 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700642 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
xueliang.zhongf7caf682017-03-01 16:07:02 +0000643 HInstructionScheduling* scheduling =
644 new (arena) HInstructionScheduling(graph, instruction_set, codegen);
Vladimir Markob4536b72015-11-24 13:45:23 +0000645 HOptimization* arm_optimizations[] = {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300646 simplifier,
Artem Serov328429f2016-07-06 16:23:04 +0100647 side_effects,
648 gvn,
xueliang.zhongf7caf682017-03-01 16:07:02 +0000649 scheduling,
Vladimir Markob4536b72015-11-24 13:45:23 +0000650 };
651 RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
652 break;
653 }
654#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100655#ifdef ART_ENABLE_CODEGEN_arm64
656 case kArm64: {
657 arm64::InstructionSimplifierArm64* simplifier =
658 new (arena) arm64::InstructionSimplifierArm64(graph, stats);
659 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700660 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100661 HInstructionScheduling* scheduling =
662 new (arena) HInstructionScheduling(graph, instruction_set);
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100663 HOptimization* arm64_optimizations[] = {
664 simplifier,
665 side_effects,
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100666 gvn,
667 scheduling,
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100668 };
669 RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
670 break;
671 }
672#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700673#ifdef ART_ENABLE_CODEGEN_mips
674 case kMips: {
Lena Djokica2901602017-09-21 13:50:52 +0200675 mips::InstructionSimplifierMips* simplifier =
676 new (arena) mips::InstructionSimplifierMips(graph, codegen, stats);
Lena Djokicb8e9c352017-09-20 15:11:13 +0200677 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
678 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Aart Bik9e147872017-09-27 11:25:01 -0700679 mips::PcRelativeFixups* pc_relative_fixups =
680 new (arena) mips::PcRelativeFixups(graph, codegen, stats);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700681 HOptimization* mips_optimizations[] = {
Lena Djokica2901602017-09-21 13:50:52 +0200682 simplifier,
Lena Djokicb8e9c352017-09-20 15:11:13 +0200683 side_effects,
684 gvn,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700685 pc_relative_fixups,
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700686 };
687 RunOptimizations(mips_optimizations, arraysize(mips_optimizations), pass_observer);
688 break;
689 }
690#endif
Lena Djokicb8e9c352017-09-20 15:11:13 +0200691#ifdef ART_ENABLE_CODEGEN_mips64
692 case kMips64: {
693 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
694 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
695 HOptimization* mips64_optimizations[] = {
696 side_effects,
697 gvn,
698 };
699 RunOptimizations(mips64_optimizations, arraysize(mips64_optimizations), pass_observer);
700 break;
701 }
702#endif
Mark Mendell0616ae02015-04-17 12:49:27 -0400703#ifdef ART_ENABLE_CODEGEN_x86
704 case kX86: {
Aart Bik9e147872017-09-27 11:25:01 -0700705 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
706 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Aart Bikd1c40452016-03-02 16:06:13 -0800707 x86::PcRelativeFixups* pc_relative_fixups =
708 new (arena) x86::PcRelativeFixups(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400709 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700710 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendell0616ae02015-04-17 12:49:27 -0400711 HOptimization* x86_optimizations[] = {
Aart Bik9e147872017-09-27 11:25:01 -0700712 side_effects,
713 gvn,
Mark Mendellee8d9712016-07-12 11:13:15 -0400714 pc_relative_fixups,
715 memory_gen
Mark Mendell0616ae02015-04-17 12:49:27 -0400716 };
717 RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
718 break;
719 }
720#endif
Mark Mendellee8d9712016-07-12 11:13:15 -0400721#ifdef ART_ENABLE_CODEGEN_x86_64
722 case kX86_64: {
Aart Bik9e147872017-09-27 11:25:01 -0700723 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
724 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Mark Mendellee8d9712016-07-12 11:13:15 -0400725 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700726 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400727 HOptimization* x86_64_optimizations[] = {
Aart Bik9e147872017-09-27 11:25:01 -0700728 side_effects,
729 gvn,
Mark Mendellee8d9712016-07-12 11:13:15 -0400730 memory_gen
731 };
732 RunOptimizations(x86_64_optimizations, arraysize(x86_64_optimizations), pass_observer);
733 break;
734 }
735#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100736 default:
737 break;
738 }
739}
740
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000741NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects.
742static void AllocateRegisters(HGraph* graph,
743 CodeGenerator* codegen,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700744 PassObserver* pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -0700745 RegisterAllocator::Strategy strategy,
746 OptimizingCompilerStats* stats) {
Mingyao Yang700347e2016-03-02 14:59:32 -0800747 {
748 PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
749 pass_observer);
Igor Murashkin6ef45672017-08-08 13:59:55 -0700750 PrepareForRegisterAllocation(graph, stats).Run();
Mingyao Yang700347e2016-03-02 14:59:32 -0800751 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000752 SsaLivenessAnalysis liveness(graph, codegen);
753 {
754 PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
755 liveness.Analyze();
756 }
757 {
758 PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700759 RegisterAllocator::Create(graph->GetArena(), codegen, liveness, strategy)->AllocateRegisters();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000760 }
761}
762
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700763void OptimizingCompiler::RunOptimizations(HGraph* graph,
764 CodeGenerator* codegen,
765 CompilerDriver* driver,
766 const DexCompilationUnit& dex_compilation_unit,
767 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700768 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700769 OptimizingCompilerStats* stats = compilation_stats_.get();
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100770 ArenaAllocator* arena = graph->GetArena();
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700771 if (driver->GetCompilerOptions().GetPassesToRun() != nullptr) {
772 ArenaVector<HOptimization*> optimizations = BuildOptimizations(
773 *driver->GetCompilerOptions().GetPassesToRun(),
774 arena,
775 graph,
776 stats,
777 codegen,
778 driver,
779 dex_compilation_unit,
780 handles);
781 RunOptimizations(&optimizations[0], optimizations.size(), pass_observer);
782 return;
783 }
784
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100785 HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700786 graph, stats, "dead_code_elimination$initial");
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100787 HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
Aart Bik2767f4b2016-10-28 15:03:53 -0700788 graph, stats, "dead_code_elimination$after_inlining");
789 HDeadCodeElimination* dce3 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700790 graph, stats, "dead_code_elimination$final");
Andreas Gampeca620d72016-11-08 08:09:33 -0800791 HConstantFolding* fold1 = new (arena) HConstantFolding(graph, "constant_folding");
Vladimir Marko65979462017-05-19 17:25:12 +0100792 InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(
793 graph, codegen, driver, stats);
Mads Ager16e52892017-07-14 13:11:37 +0200794 HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, handles, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700795 HConstantFolding* fold2 = new (arena) HConstantFolding(
796 graph, "constant_folding$after_inlining");
797 HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding$after_bce");
Aart Bik92685a82017-03-06 11:13:43 -0800798 SideEffectsAnalysis* side_effects1 = new (arena) SideEffectsAnalysis(
799 graph, "side_effects$before_gvn");
800 SideEffectsAnalysis* side_effects2 = new (arena) SideEffectsAnalysis(
801 graph, "side_effects$before_lse");
802 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects1);
803 LICM* licm = new (arena) LICM(graph, *side_effects1, stats);
Aart Bik22af3be2015-09-10 12:50:58 -0700804 HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
Aart Bik92685a82017-03-06 11:13:43 -0800805 BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects1, induction);
Aart Bikb92cc332017-09-06 15:53:17 -0700806 HLoopOptimization* loop = new (arena) HLoopOptimization(graph, driver, induction, stats);
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100807 LoadStoreAnalysis* lsa = new (arena) LoadStoreAnalysis(graph);
Igor Murashkin1e065a52017-08-09 13:20:34 -0700808 LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects2, *lsa, stats);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000809 HSharpening* sharpening = new (arena) HSharpening(
810 graph, codegen, dex_compilation_unit, driver, handles);
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100811 InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100812 graph, codegen, driver, stats, "instruction_simplifier$after_inlining");
David Brazdil4833f5a2015-12-16 10:37:39 +0000813 InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100814 graph, codegen, driver, stats, "instruction_simplifier$after_bce");
Aart Bik639cc8c2016-10-18 13:03:31 -0700815 InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
Vladimir Marko65979462017-05-19 17:25:12 +0100816 graph, codegen, driver, stats, "instruction_simplifier$before_codegen");
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100817 IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, stats);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800818 CHAGuardOptimization* cha_guard = new (arena) CHAGuardOptimization(graph);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000819 CodeSinking* code_sinking = new (arena) CodeSinking(graph, stats);
Igor Murashkindd018df2017-08-09 10:38:31 -0700820 ConstructorFenceRedundancyElimination* cfre =
821 new (arena) ConstructorFenceRedundancyElimination(graph, stats);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800822
Calin Juravleec748352015-07-29 13:52:12 +0100823 HOptimization* optimizations1[] = {
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100824 intrinsics,
David Brazdil4833f5a2015-12-16 10:37:39 +0000825 sharpening,
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100826 fold1,
827 simplify1,
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100828 dce1,
Calin Juravleec748352015-07-29 13:52:12 +0100829 };
Calin Juravleec748352015-07-29 13:52:12 +0100830 RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
831
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700832 MaybeRunInliner(graph, codegen, driver, dex_compilation_unit, pass_observer, handles);
David Brazdil95177982015-10-30 12:56:58 -0500833
David Brazdil8993caf2015-12-07 10:04:40 +0000834 HOptimization* optimizations2[] = {
David Brazdil74eb1b22015-12-14 11:44:01 +0000835 // SelectGenerator depends on the InstructionSimplifier removing
David Brazdil8993caf2015-12-07 10:04:40 +0000836 // redundant suspend checks to recognize empty blocks.
David Brazdil74eb1b22015-12-14 11:44:01 +0000837 select_generator,
David Brazdil8993caf2015-12-07 10:04:40 +0000838 fold2, // TODO: if we don't inline we can also skip fold2.
Aart Bik639cc8c2016-10-18 13:03:31 -0700839 simplify2,
Aart Bik2767f4b2016-10-28 15:03:53 -0700840 dce2,
Aart Bik92685a82017-03-06 11:13:43 -0800841 side_effects1,
David Brazdil8993caf2015-12-07 10:04:40 +0000842 gvn,
843 licm,
844 induction,
845 bce,
Aart Bik281c6812016-08-26 11:31:48 -0700846 loop,
David Brazdil8993caf2015-12-07 10:04:40 +0000847 fold3, // evaluates code generated by dynamic bce
Aart Bik639cc8c2016-10-18 13:03:31 -0700848 simplify3,
Aart Bik92685a82017-03-06 11:13:43 -0800849 side_effects2,
xueliang.zhongc239a2b2017-04-27 15:31:37 +0100850 lsa,
David Brazdil8993caf2015-12-07 10:04:40 +0000851 lse,
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800852 cha_guard,
Aart Bik2767f4b2016-10-28 15:03:53 -0700853 dce3,
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000854 code_sinking,
David Brazdil8993caf2015-12-07 10:04:40 +0000855 // The codegen has a few assumptions that only the instruction simplifier
856 // can satisfy. For example, the code generator does not expect to see a
857 // HTypeConversion from a type to the same type.
Aart Bik639cc8c2016-10-18 13:03:31 -0700858 simplify4,
Igor Murashkindd018df2017-08-09 10:38:31 -0700859 cfre, // Eliminate constructor fences after code sinking to avoid
860 // complicated sinking logic to split a fence with many inputs.
David Brazdil8993caf2015-12-07 10:04:40 +0000861 };
862 RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
David Brazdilbbd733e2015-08-18 17:48:17 +0100863
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700864 RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, pass_observer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000865}
866
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100867static ArenaVector<linker::LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
868 ArenaVector<linker::LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
Vladimir Marko58155012015-08-19 12:49:41 +0000869 codegen->EmitLinkerPatches(&linker_patches);
870
871 // Sort patches by literal offset. Required for .oat_patches encoding.
872 std::sort(linker_patches.begin(), linker_patches.end(),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100873 [](const linker::LinkerPatch& lhs, const linker::LinkerPatch& rhs) {
Vladimir Marko58155012015-08-19 12:49:41 +0000874 return lhs.LiteralOffset() < rhs.LiteralOffset();
875 });
876
877 return linker_patches;
878}
879
David Brazdil58282f42016-01-14 12:45:10 +0000880CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
881 CodeVectorAllocator* code_allocator,
882 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000883 CompilerDriver* compiler_driver,
884 const DexFile::CodeItem* code_item) const {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100885 ArenaVector<linker::LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
Vladimir Markof9f64412015-09-02 14:05:49 +0100886 ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700887 ArenaVector<uint8_t> method_info(arena->Adapter(kArenaAllocStackMaps));
888 size_t stack_map_size = 0;
889 size_t method_info_size = 0;
890 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
891 stack_map.resize(stack_map_size);
892 method_info.resize(method_info_size);
893 codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()),
894 MemoryRegion(method_info.data(), method_info.size()),
895 *code_item);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000896
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100897 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000898 compiler_driver,
899 codegen->GetInstructionSet(),
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000900 ArrayRef<const uint8_t>(code_allocator->GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000901 // Follow Quick's behavior and set the frame size to zero if it is
902 // considered "empty" (see the definition of
903 // art::CodeGenerator::HasEmptyFrame).
904 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000905 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000906 codegen->GetFpuSpillMask(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700907 ArrayRef<const uint8_t>(method_info),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100908 ArrayRef<const uint8_t>(stack_map),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100909 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100910 ArrayRef<const linker::LinkerPatch>(linker_patches));
Mathieu Chartiered150002015-08-28 11:16:54 -0700911
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100912 return compiled_method;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000913}
914
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000915CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
916 CodeVectorAllocator* code_allocator,
917 const DexFile::CodeItem* code_item,
918 uint32_t access_flags,
919 InvokeType invoke_type,
920 uint16_t class_def_idx,
921 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000922 Handle<mirror::ClassLoader> class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000923 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000924 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000925 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000926 bool osr,
927 VariableSizedHandleScope* handles) const {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700928 MaybeRecordStat(compilation_stats_.get(),
929 MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000930 CompilerDriver* compiler_driver = GetCompilerDriver();
931 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Calin Juravlecff8cc72015-10-09 12:03:24 +0100932
Roland Levillain3b359c72015-11-17 19:35:12 +0000933 // Always use the Thumb-2 assembler: some runtime functionality
934 // (like implicit stack overflow checks) assume Thumb-2.
Artem Serovba6b6792016-08-15 14:22:07 +0100935 DCHECK_NE(instruction_set, kArm);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100936
937 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000938 if (!IsInstructionSetSupported(instruction_set)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700939 MaybeRecordStat(compilation_stats_.get(),
940 MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100941 return nullptr;
942 }
943
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000944 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700945 MaybeRecordStat(compilation_stats_.get(),
946 MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000947 return nullptr;
948 }
949
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000950 // Implementation of the space filter: do not compile a code item whose size in
Nicolas Geoffray432bf3d2015-07-17 11:11:09 +0100951 // code units is bigger than 128.
952 static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000953 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000954 if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000955 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
Igor Murashkin1e065a52017-08-09 13:20:34 -0700956 MaybeRecordStat(compilation_stats_.get(),
957 MethodCompilationStat::kNotCompiledSpaceFilter);
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000958 return nullptr;
959 }
960
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100961 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000962 DexCompilationUnit dex_compilation_unit(
Vladimir Markodf739842016-03-23 16:59:07 +0000963 class_loader,
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100964 class_linker,
Vladimir Markodf739842016-03-23 16:59:07 +0000965 dex_file,
966 code_item,
967 class_def_idx,
968 method_idx,
969 access_flags,
970 /* verified_method */ nullptr,
971 dex_cache);
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000972
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000973 HGraph* graph = new (arena) HGraph(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000974 arena,
975 dex_file,
976 method_idx,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000977 compiler_driver->GetInstructionSet(),
978 kInvalidInvokeType,
979 compiler_driver->GetCompilerOptions().GetDebuggable(),
980 osr);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000981
David Brazdilbadd8262016-02-02 16:28:56 +0000982 const uint8_t* interpreter_metadata = nullptr;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000983 if (method == nullptr) {
David Brazdilbadd8262016-02-02 16:28:56 +0000984 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000985 method = compiler_driver->ResolveMethod(
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000986 soa, dex_cache, class_loader, &dex_compilation_unit, method_idx, invoke_type);
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000987 }
988 // For AOT compilation, we may not get a method, for example if its class is erroneous.
989 // JIT should always have a method.
990 DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr);
991 if (method != nullptr) {
992 graph->SetArtMethod(method);
993 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100994 interpreter_metadata = method->GetQuickenedInfo(class_linker->GetImagePointerSize());
David Brazdilbadd8262016-02-02 16:28:56 +0000995 }
996
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000997 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000998 CodeGenerator::Create(graph,
999 instruction_set,
1000 *compiler_driver->GetInstructionSetFeatures(),
Calin Juravle2ae48182016-03-16 14:05:09 +00001001 compiler_driver->GetCompilerOptions(),
1002 compilation_stats_.get()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +00001003 if (codegen.get() == nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001004 MaybeRecordStat(compilation_stats_.get(),
1005 MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001006 return nullptr;
1007 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001008 codegen->GetAssembler()->cfi().SetEnabled(
David Srbecky5b1c2ca2016-01-25 17:32:41 +00001009 compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001010
David Brazdil69ba7b72015-06-23 18:27:30 +01001011 PassObserver pass_observer(graph,
David Brazdil69ba7b72015-06-23 18:27:30 +01001012 codegen.get(),
1013 visualizer_output_.get(),
Andreas Gampea0d81af2016-10-27 12:04:57 -07001014 compiler_driver,
1015 dump_mutex_);
David Brazdil5e8b1372015-01-23 14:39:08 +00001016
David Brazdil809658e2015-02-05 11:34:02 +00001017 {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001018 VLOG(compiler) << "Building " << pass_observer.GetMethodName();
1019 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
1020 HGraphBuilder builder(graph,
1021 &dex_compilation_unit,
1022 &dex_compilation_unit,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001023 compiler_driver,
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00001024 codegen.get(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001025 compilation_stats_.get(),
1026 interpreter_metadata,
1027 dex_cache,
1028 handles);
1029 GraphAnalysisResult result = builder.BuildGraph();
1030 if (result != kAnalysisSuccess) {
1031 switch (result) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001032 case kAnalysisSkipped: {
1033 MaybeRecordStat(compilation_stats_.get(),
1034 MethodCompilationStat::kNotCompiledSkipped);
1035 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001036 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001037 case kAnalysisInvalidBytecode: {
1038 MaybeRecordStat(compilation_stats_.get(),
1039 MethodCompilationStat::kNotCompiledInvalidBytecode);
1040 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001041 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001042 case kAnalysisFailThrowCatchLoop: {
1043 MaybeRecordStat(compilation_stats_.get(),
1044 MethodCompilationStat::kNotCompiledThrowCatchLoop);
1045 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001046 break;
Igor Murashkin1e065a52017-08-09 13:20:34 -07001047 case kAnalysisFailAmbiguousArrayOp: {
1048 MaybeRecordStat(compilation_stats_.get(),
1049 MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
1050 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001051 break;
1052 case kAnalysisSuccess:
1053 UNREACHABLE();
David Brazdil809658e2015-02-05 11:34:02 +00001054 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001055 pass_observer.SetGraphInBadState();
1056 return nullptr;
Nicolas Geoffrayf5370122014-12-02 11:51:19 +00001057 }
David Brazdilbadd8262016-02-02 16:28:56 +00001058 }
Vladimir Markof9f64412015-09-02 14:05:49 +01001059
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001060 RunOptimizations(graph,
1061 codegen.get(),
1062 compiler_driver,
1063 dex_compilation_unit,
1064 &pass_observer,
1065 handles);
1066
1067 RegisterAllocator::Strategy regalloc_strategy =
1068 compiler_options.GetRegisterAllocationStrategy();
Igor Murashkin1e065a52017-08-09 13:20:34 -07001069 AllocateRegisters(graph,
1070 codegen.get(),
1071 &pass_observer,
Igor Murashkin6ef45672017-08-08 13:59:55 -07001072 regalloc_strategy,
1073 compilation_stats_.get());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001074
1075 codegen->Compile(code_allocator);
1076 pass_observer.DumpDisassembly();
1077
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001078 return codegen.release();
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001079}
1080
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001081CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
1082 uint32_t access_flags,
1083 InvokeType invoke_type,
1084 uint16_t class_def_idx,
1085 uint32_t method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001086 Handle<mirror::ClassLoader> jclass_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001087 const DexFile& dex_file,
1088 Handle<mirror::DexCache> dex_cache) const {
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001089 CompilerDriver* compiler_driver = GetCompilerDriver();
1090 CompiledMethod* method = nullptr;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001091 DCHECK(Runtime::Current()->IsAotCompiler());
1092 const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
1093 DCHECK(!verified_method->HasRuntimeThrow());
1094 if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
Andreas Gampe6d7abbd2017-04-24 13:19:09 -07001095 || verifier::CanCompilerHandleVerificationFailure(
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001096 verified_method->GetEncounteredVerificationFailures())) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001097 ArenaAllocator arena(Runtime::Current()->GetArenaPool());
1098 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001099 std::unique_ptr<CodeGenerator> codegen;
1100 {
1101 ScopedObjectAccess soa(Thread::Current());
1102 VariableSizedHandleScope handles(soa.Self());
1103 // Go to native so that we don't block GC during compilation.
1104 ScopedThreadSuspension sts(soa.Self(), kNative);
1105 codegen.reset(
1106 TryCompile(&arena,
1107 &code_allocator,
1108 code_item,
1109 access_flags,
1110 invoke_type,
1111 class_def_idx,
1112 method_idx,
1113 jclass_loader,
1114 dex_file,
1115 dex_cache,
1116 nullptr,
1117 /* osr */ false,
1118 &handles));
1119 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001120 if (codegen.get() != nullptr) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001121 MaybeRecordStat(compilation_stats_.get(),
1122 MethodCompilationStat::kCompiled);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001123 method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001124
1125 if (kArenaAllocatorCountAllocations) {
1126 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1127 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001128 LOG(INFO) << dex_file.PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001129 }
1130 }
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001131 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001132 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001133 MethodCompilationStat method_stat;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001134 if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001135 method_stat = MethodCompilationStat::kNotCompiledVerifyAtRuntime;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001136 } else {
Igor Murashkin1e065a52017-08-09 13:20:34 -07001137 method_stat = MethodCompilationStat::kNotCompiledVerificationError;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001138 }
Igor Murashkin1e065a52017-08-09 13:20:34 -07001139 MaybeRecordStat(compilation_stats_.get(), method_stat);
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001140 }
1141
Calin Juravlecff8cc72015-10-09 12:03:24 +01001142 if (kIsDebugBuild &&
1143 IsCompilingWithCoreImage() &&
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001144 IsInstructionSetSupported(compiler_driver->GetInstructionSet())) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001145 // For testing purposes, we put a special marker on method names
Goran Jakovljevic9c4f0d8f2017-04-05 16:27:25 +02001146 // that should be compiled with this compiler (when the
1147 // instruction set is supported). This makes sure we're not
Roland Levillain0d5a2812015-11-13 10:07:31 +00001148 // regressing.
David Sehr709b0702016-10-13 09:12:37 -07001149 std::string method_name = dex_file.PrettyMethod(method_idx);
Calin Juravle09b1d6f2015-10-07 12:08:54 +01001150 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
1151 DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
1152 }
1153
Nicolas Geoffray12be74e2015-03-30 13:29:08 +01001154 return method;
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001155}
1156
Andreas Gampe53c913b2014-08-12 23:19:23 -07001157Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
1158 return new OptimizingCompiler(driver);
1159}
1160
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001161bool IsCompilingWithCoreImage() {
1162 const std::string& image = Runtime::Current()->GetImageLocation();
Roland Levillain2b03a1f2017-06-06 16:09:59 +01001163 return CompilerDriver::IsCoreImageFilename(image);
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001164}
1165
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00001166bool EncodeArtMethodInInlineInfo(ArtMethod* method ATTRIBUTE_UNUSED) {
1167 // Note: the runtime is null only for unit testing.
1168 return Runtime::Current() == nullptr || !Runtime::Current()->IsAotCompiler();
1169}
1170
1171bool CanEncodeInlinedMethodInStackMap(const DexFile& caller_dex_file, ArtMethod* callee) {
1172 if (!Runtime::Current()->IsAotCompiler()) {
1173 // JIT can always encode methods in stack maps.
1174 return true;
1175 }
1176 if (IsSameDexFile(caller_dex_file, *callee->GetDexFile())) {
1177 return true;
1178 }
1179 // TODO(ngeoffray): Support more AOT cases for inlining:
1180 // - methods in multidex
1181 // - methods in boot image for on-device non-PIC compilation.
1182 return false;
1183}
1184
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001185bool OptimizingCompiler::JitCompile(Thread* self,
1186 jit::JitCodeCache* code_cache,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001187 ArtMethod* method,
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001188 bool osr,
1189 jit::JitLogger* jit_logger) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001190 StackHandleScope<3> hs(self);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001191 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1192 method->GetDeclaringClass()->GetClassLoader()));
1193 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001194 DCHECK(method->IsCompilable());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001195
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001196 const DexFile* dex_file = method->GetDexFile();
1197 const uint16_t class_def_idx = method->GetClassDefIndex();
1198 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1199 const uint32_t method_idx = method->GetDexMethodIndex();
1200 const uint32_t access_flags = method->GetAccessFlags();
1201 const InvokeType invoke_type = method->GetInvokeType();
1202
Nicolas Geoffray25e04562016-03-01 13:17:58 +00001203 ArenaAllocator arena(Runtime::Current()->GetJitArenaPool());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001204 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001205 VariableSizedHandleScope handles(self);
1206
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001207 std::unique_ptr<CodeGenerator> codegen;
1208 {
1209 // Go to native so that we don't block GC during compilation.
1210 ScopedThreadSuspension sts(self, kNative);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001211 codegen.reset(
1212 TryCompile(&arena,
1213 &code_allocator,
1214 code_item,
1215 access_flags,
1216 invoke_type,
1217 class_def_idx,
1218 method_idx,
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001219 class_loader,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001220 *dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001221 dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +00001222 method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001223 osr,
1224 &handles));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001225 if (codegen.get() == nullptr) {
1226 return false;
1227 }
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001228
1229 if (kArenaAllocatorCountAllocations) {
1230 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1231 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001232 LOG(INFO) << dex_file->PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001233 }
1234 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001235 }
1236
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001237 size_t stack_map_size = 0;
1238 size_t method_info_size = 0;
1239 codegen->ComputeStackMapAndMethodInfoSize(&stack_map_size, &method_info_size);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001240 size_t number_of_roots = codegen->GetNumberOfJitRoots();
1241 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1242 // We allocate an object array to ensure the JIT roots that we will collect in EmitJitRoots
1243 // will be visible by the GC between EmitLiterals and CommitCode. Once CommitCode is
1244 // executed, this array is not needed.
1245 Handle<mirror::ObjectArray<mirror::Object>> roots(
1246 hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc(
1247 self, class_linker->GetClassRoot(ClassLinker::kObjectArrayClass), number_of_roots)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001248 if (roots == nullptr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001249 // Out of memory, just clear the exception to avoid any Java exception uncaught problems.
1250 DCHECK(self->IsExceptionPending());
1251 self->ClearException();
1252 return false;
1253 }
1254 uint8_t* stack_map_data = nullptr;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001255 uint8_t* method_info_data = nullptr;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001256 uint8_t* roots_data = nullptr;
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001257 uint32_t data_size = code_cache->ReserveData(self,
1258 stack_map_size,
1259 method_info_size,
1260 number_of_roots,
1261 method,
1262 &stack_map_data,
1263 &method_info_data,
1264 &roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001265 if (stack_map_data == nullptr || roots_data == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001266 return false;
1267 }
Igor Murashkin1e065a52017-08-09 13:20:34 -07001268 MaybeRecordStat(compilation_stats_.get(), MethodCompilationStat::kCompiled);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001269 codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size),
1270 MemoryRegion(method_info_data, method_info_size),
1271 *code_item);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00001272 codegen->EmitJitRoots(code_allocator.GetData(), roots, roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001273
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001274 const void* code = code_cache->CommitCode(
1275 self,
1276 method,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001277 stack_map_data,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -07001278 method_info_data,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001279 roots_data,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001280 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
1281 codegen->GetCoreSpillMask(),
1282 codegen->GetFpuSpillMask(),
1283 code_allocator.GetMemory().data(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001284 code_allocator.GetSize(),
Orion Hodsondbd05fe2017-08-10 11:41:35 +01001285 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001286 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -07001287 roots,
1288 codegen->GetGraph()->HasShouldDeoptimizeFlag(),
1289 codegen->GetGraph()->GetCHASingleImplementationList());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001290
1291 if (code == nullptr) {
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001292 code_cache->ClearData(self, stack_map_data, roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001293 return false;
1294 }
1295
David Srbecky91cc06c2016-03-07 16:13:58 +00001296 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
1297 if (compiler_options.GetGenerateDebugInfo()) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001298 const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
1299 const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
Vladimir Marko1b404a82017-09-01 13:35:26 +01001300 debug::MethodDebugInfo info = {};
1301 DCHECK(info.trampoline_name.empty());
David Srbecky197160d2016-03-07 17:33:57 +00001302 info.dex_file = dex_file;
1303 info.class_def_index = class_def_idx;
1304 info.dex_method_index = method_idx;
1305 info.access_flags = access_flags;
1306 info.code_item = code_item;
1307 info.isa = codegen->GetInstructionSet();
1308 info.deduped = false;
1309 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
1310 info.is_optimized = true;
1311 info.is_code_address_text_relative = false;
1312 info.code_address = code_address;
1313 info.code_size = code_allocator.GetSize();
1314 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
1315 info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
1316 info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
Vladimir Marko93205e32016-04-13 11:59:46 +01001317 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
David Srbecky5d811202016-03-08 13:21:22 +00001318 GetCompilerDriver()->GetInstructionSet(),
1319 GetCompilerDriver()->GetInstructionSetFeatures(),
David Srbeckyfe736b72016-03-09 11:44:44 +00001320 ArrayRef<const debug::MethodDebugInfo>(&info, 1));
Vladimir Marko93205e32016-04-13 11:59:46 +01001321 CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
David Srbecky5cc349f2015-12-18 15:04:48 +00001322 }
1323
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001324 Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed());
Nicolas Geoffray01db5f72017-07-19 15:05:49 +01001325 if (jit_logger != nullptr) {
1326 jit_logger->WriteLog(code, code_allocator.GetSize(), method);
1327 }
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001328
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001329 return true;
1330}
1331
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001332} // namespace art