blob: 1ab671022bd01b8cb1a652d5a6e85031b7a5b8fb [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
Andreas Gampe9186ced2016-12-12 14:28:21 -080025#include "android-base/strings.h"
26
Aart Bikd1c40452016-03-02 16:06:13 -080027#ifdef ART_ENABLE_CODEGEN_arm
Vladimir Markob4536b72015-11-24 13:45:23 +000028#include "dex_cache_array_fixups_arm.h"
29#endif
30
31#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames44b9cf92015-08-19 15:39:06 +010032#include "instruction_simplifier_arm64.h"
33#endif
34
Alexey Frunzee3fb2452016-05-10 16:08:05 -070035#ifdef ART_ENABLE_CODEGEN_mips
36#include "dex_cache_array_fixups_mips.h"
37#include "pc_relative_fixups_mips.h"
38#endif
39
Mark Mendell0616ae02015-04-17 12:49:27 -040040#ifdef ART_ENABLE_CODEGEN_x86
Vladimir Marko0f7dca42015-11-02 14:36:43 +000041#include "pc_relative_fixups_x86.h"
Mark Mendell0616ae02015-04-17 12:49:27 -040042#endif
43
Mark Mendellee8d9712016-07-12 11:13:15 -040044#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
45#include "x86_memory_gen.h"
46#endif
47
Mathieu Chartiere401d142015-04-22 13:56:20 -070048#include "art_method-inl.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080049#include "base/arena_allocator.h"
Vladimir Markof9f64412015-09-02 14:05:49 +010050#include "base/arena_containers.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000051#include "base/dumpable.h"
Vladimir Markoc90d7c72015-10-06 17:30:45 +000052#include "base/macros.h"
Andreas Gampea0d81af2016-10-27 12:04:57 -070053#include "base/mutex.h"
David Brazdil5e8b1372015-01-23 14:39:08 +000054#include "base/timing_logger.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070055#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000056#include "builder.h"
Mingyao Yangb0b051a2016-11-17 09:04:53 -080057#include "cha_guard_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058#include "code_generator.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000059#include "compiled_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070060#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010061#include "constant_folding.h"
62#include "dead_code_elimination.h"
David Srbeckyc5bfa972016-02-05 15:49:10 +000063#include "debug/elf_debug_writer.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000064#include "debug/method_debug_info.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010065#include "dex/verification_results.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000066#include "dex/verified_method.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080067#include "dex_file_types.h"
Nicolas Geoffray9523a3e2015-07-17 11:51:28 +000068#include "driver/compiler_driver-inl.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000069#include "driver/compiler_options.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000070#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000071#include "elf_writer_quick.h"
David Brazdil69ba7b72015-06-23 18:27:30 +010072#include "graph_checker.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010073#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010074#include "gvn.h"
Aart Bik22af3be2015-09-10 12:50:58 -070075#include "induction_var_analysis.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000076#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010077#include "instruction_simplifier.h"
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030078#include "instruction_simplifier_arm.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080079#include "intrinsics.h"
David Srbecky5cc349f2015-12-18 15:04:48 +000080#include "jit/debugger_interface.h"
Nicolas Geoffraya4f81542016-03-08 16:57:48 +000081#include "jit/jit.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000082#include "jit/jit_code_cache.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000083#include "jni/quick/jni_compiler.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000084#include "licm.h"
Mingyao Yang8df69d42015-10-22 15:40:58 -070085#include "load_store_elimination.h"
Aart Bik281c6812016-08-26 11:31:48 -070086#include "loop_optimization.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000087#include "nodes.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000088#include "oat_quick_method_header.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010089#include "prepare_for_register_allocation.h"
Calin Juravlef1c6d9e2015-04-13 18:42:21 +010090#include "reference_type_propagation.h"
Matthew Gharritye9288852016-07-14 14:08:16 -070091#include "register_allocator_linear_scan.h"
David Brazdil74eb1b22015-12-14 11:44:01 +000092#include "select_generator.h"
Alexandre Rames22aa54b2016-10-18 09:32:29 +010093#include "scheduler.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010094#include "sharpening.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000095#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000096#include "ssa_builder.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097#include "ssa_liveness_analysis.h"
David Srbecky4fda4eb2016-02-05 13:34:46 +000098#include "ssa_phi_elimination.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010099#include "utils/assembler.h"
Calin Juravle175dc732015-08-25 15:42:32 +0100100#include "verifier/method_verifier.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000101
102namespace art {
103
Vladimir Marko3a40bf22016-03-22 16:26:33 +0000104static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
105
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700106static constexpr const char* kPassNameSeparator = "$";
107
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000108/**
109 * Used by the code generator, to allocate the code in a vector.
110 */
111class CodeVectorAllocator FINAL : public CodeAllocator {
112 public:
Vladimir Markof9f64412015-09-02 14:05:49 +0100113 explicit CodeVectorAllocator(ArenaAllocator* arena)
114 : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
115 size_(0) {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116
117 virtual uint8_t* Allocate(size_t size) {
118 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000119 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000120 return &memory_[0];
121 }
122
123 size_t GetSize() const { return size_; }
Vladimir Markof9f64412015-09-02 14:05:49 +0100124 const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000125 uint8_t* GetData() { return memory_.data(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126
127 private:
Vladimir Markof9f64412015-09-02 14:05:49 +0100128 ArenaVector<uint8_t> memory_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000129 size_t size_;
130
131 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
132};
133
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100134/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100135 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +0000136 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100137 */
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700138static constexpr const char kStringFilter[] = "";
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100139
David Brazdil69ba7b72015-06-23 18:27:30 +0100140class PassScope;
David Brazdil809658e2015-02-05 11:34:02 +0000141
David Brazdil69ba7b72015-06-23 18:27:30 +0100142class PassObserver : public ValueObject {
David Brazdil5e8b1372015-01-23 14:39:08 +0000143 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100144 PassObserver(HGraph* graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100145 CodeGenerator* codegen,
146 std::ostream* visualizer_output,
Andreas Gampea0d81af2016-10-27 12:04:57 -0700147 CompilerDriver* compiler_driver,
148 Mutex& dump_mutex)
David Brazdil69ba7b72015-06-23 18:27:30 +0100149 : graph_(graph),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000150 cached_method_name_(),
David Brazdil809658e2015-02-05 11:34:02 +0000151 timing_logger_enabled_(compiler_driver->GetDumpPasses()),
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000152 timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100153 disasm_info_(graph->GetArena()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700154 visualizer_oss_(),
155 visualizer_output_(visualizer_output),
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000156 visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700157 visualizer_(&visualizer_oss_, graph, *codegen),
158 visualizer_dump_mutex_(dump_mutex),
David Brazdil69ba7b72015-06-23 18:27:30 +0100159 graph_in_bad_state_(false) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700160 if (timing_logger_enabled_ || visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000161 if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700162 timing_logger_enabled_ = visualizer_enabled_ = false;
163 }
164 if (visualizer_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000165 visualizer_.PrintHeader(GetMethodName());
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700166 codegen->SetDisassemblyInformation(&disasm_info_);
167 }
David Brazdil62e074f2015-04-07 18:09:37 +0100168 }
David Brazdil5e8b1372015-01-23 14:39:08 +0000169 }
170
David Brazdil69ba7b72015-06-23 18:27:30 +0100171 ~PassObserver() {
David Brazdil5e8b1372015-01-23 14:39:08 +0000172 if (timing_logger_enabled_) {
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000173 LOG(INFO) << "TIMINGS " << GetMethodName();
David Brazdil5e8b1372015-01-23 14:39:08 +0000174 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
175 }
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000176 DCHECK(visualizer_oss_.str().empty());
David Brazdil5e8b1372015-01-23 14:39:08 +0000177 }
178
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000179 void DumpDisassembly() REQUIRES(!visualizer_dump_mutex_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100180 if (visualizer_enabled_) {
181 visualizer_.DumpGraphWithDisassembly();
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000182 FlushVisualizer();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100183 }
184 }
185
David Brazdil69ba7b72015-06-23 18:27:30 +0100186 void SetGraphInBadState() { graph_in_bad_state_ = true; }
187
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000188 const char* GetMethodName() {
189 // PrettyMethod() is expensive, so we delay calling it until we actually have to.
190 if (cached_method_name_.empty()) {
David Sehr709b0702016-10-13 09:12:37 -0700191 cached_method_name_ = graph_->GetDexFile().PrettyMethod(graph_->GetMethodIdx());
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000192 }
193 return cached_method_name_.c_str();
194 }
195
David Brazdil5e8b1372015-01-23 14:39:08 +0000196 private:
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000197 void StartPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
Wojciech Staszkiewicze7060702016-08-16 17:31:19 -0700198 VLOG(compiler) << "Starting pass: " << pass_name;
David Brazdil809658e2015-02-05 11:34:02 +0000199 // Dump graph first, then start timer.
200 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100201 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000202 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000203 }
204 if (timing_logger_enabled_) {
205 timing_logger_.StartTiming(pass_name);
206 }
207 }
208
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000209 void FlushVisualizer() REQUIRES(!visualizer_dump_mutex_) {
210 MutexLock mu(Thread::Current(), visualizer_dump_mutex_);
211 *visualizer_output_ << visualizer_oss_.str();
212 visualizer_output_->flush();
213 visualizer_oss_.str("");
214 visualizer_oss_.clear();
215 }
216
217 void EndPass(const char* pass_name) REQUIRES(!visualizer_dump_mutex_) {
David Brazdil809658e2015-02-05 11:34:02 +0000218 // Pause timer first, then dump graph.
219 if (timing_logger_enabled_) {
220 timing_logger_.EndTiming();
221 }
222 if (visualizer_enabled_) {
David Brazdilffee3d32015-07-06 11:48:53 +0100223 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
Nicolas Geoffray00c141a2016-11-10 15:19:15 +0000224 FlushVisualizer();
David Brazdil809658e2015-02-05 11:34:02 +0000225 }
David Brazdil69ba7b72015-06-23 18:27:30 +0100226
227 // Validate the HGraph if running in debug mode.
228 if (kIsDebugBuild) {
229 if (!graph_in_bad_state_) {
David Brazdilbadd8262016-02-02 16:28:56 +0000230 GraphChecker checker(graph_);
231 checker.Run();
232 if (!checker.IsValid()) {
233 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
David Brazdil69ba7b72015-06-23 18:27:30 +0100234 }
235 }
236 }
David Brazdil809658e2015-02-05 11:34:02 +0000237 }
238
Andreas Gampe53fcd0f2015-07-22 12:10:13 -0700239 static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
240 // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
241 // empty kStringFilter matching all methods.
242 if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
243 return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
244 }
245
246 // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
247 // warning when the string is empty.
248 constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
249 if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
250 return true;
251 }
252
253 return false;
254 }
255
David Brazdil69ba7b72015-06-23 18:27:30 +0100256 HGraph* const graph_;
Vladimir Marko0d1caa52015-11-20 13:02:22 +0000257
258 std::string cached_method_name_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000259
260 bool timing_logger_enabled_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000261 TimingLogger timing_logger_;
262
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100263 DisassemblyInformation disasm_info_;
264
Andreas Gampea0d81af2016-10-27 12:04:57 -0700265 std::ostringstream visualizer_oss_;
266 std::ostream* visualizer_output_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000267 bool visualizer_enabled_;
268 HGraphVisualizer visualizer_;
Andreas Gampea0d81af2016-10-27 12:04:57 -0700269 Mutex& visualizer_dump_mutex_;
David Brazdil5e8b1372015-01-23 14:39:08 +0000270
David Brazdil69ba7b72015-06-23 18:27:30 +0100271 // Flag to be set by the compiler if the pass failed and the graph is not
272 // expected to validate.
273 bool graph_in_bad_state_;
David Brazdil809658e2015-02-05 11:34:02 +0000274
David Brazdil69ba7b72015-06-23 18:27:30 +0100275 friend PassScope;
276
277 DISALLOW_COPY_AND_ASSIGN(PassObserver);
David Brazdil5e8b1372015-01-23 14:39:08 +0000278};
279
David Brazdil69ba7b72015-06-23 18:27:30 +0100280class PassScope : public ValueObject {
David Brazdil809658e2015-02-05 11:34:02 +0000281 public:
David Brazdil69ba7b72015-06-23 18:27:30 +0100282 PassScope(const char *pass_name, PassObserver* pass_observer)
David Brazdil809658e2015-02-05 11:34:02 +0000283 : pass_name_(pass_name),
David Brazdil69ba7b72015-06-23 18:27:30 +0100284 pass_observer_(pass_observer) {
285 pass_observer_->StartPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000286 }
287
David Brazdil69ba7b72015-06-23 18:27:30 +0100288 ~PassScope() {
289 pass_observer_->EndPass(pass_name_);
David Brazdil809658e2015-02-05 11:34:02 +0000290 }
291
292 private:
293 const char* const pass_name_;
David Brazdil69ba7b72015-06-23 18:27:30 +0100294 PassObserver* const pass_observer_;
David Brazdil809658e2015-02-05 11:34:02 +0000295};
296
Andreas Gampe53c913b2014-08-12 23:19:23 -0700297class OptimizingCompiler FINAL : public Compiler {
298 public:
299 explicit OptimizingCompiler(CompilerDriver* driver);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700300 ~OptimizingCompiler() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700301
Vladimir Markodf739842016-03-23 16:59:07 +0000302 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700303
304 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
305 uint32_t access_flags,
306 InvokeType invoke_type,
307 uint16_t class_def_idx,
308 uint32_t method_idx,
309 jobject class_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -0700310 const DexFile& dex_file,
311 Handle<mirror::DexCache> dex_cache) const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700312
Andreas Gampe53c913b2014-08-12 23:19:23 -0700313 CompiledMethod* JniCompile(uint32_t access_flags,
314 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700315 const DexFile& dex_file,
316 JniOptimizationFlags optimization_flags) const OVERRIDE {
317 return ArtQuickJniCompileMethod(GetCompilerDriver(),
318 access_flags,
319 method_idx,
320 dex_file,
321 optimization_flags);
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000322 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700323
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700325 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000326 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
327 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
328 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700329
David Brazdilee690a32014-12-01 17:04:16 +0000330 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700331
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000332 void UnInit() const OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700333
Calin Juravle2be39e02015-04-21 13:56:34 +0100334 void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
335 if (compilation_stats_.get() != nullptr) {
336 compilation_stats_->RecordStat(compilation_stat);
337 }
338 }
339
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000340 bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr)
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000341 OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700342 REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000343
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700344 private:
345 void RunOptimizations(HGraph* graph,
346 CodeGenerator* codegen,
347 CompilerDriver* driver,
348 const DexCompilationUnit& dex_compilation_unit,
349 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700350 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700351
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700352 void RunOptimizations(HOptimization* optimizations[],
353 size_t length,
354 PassObserver* pass_observer) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700355
Andreas Gampe53c913b2014-08-12 23:19:23 -0700356 private:
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000357 // Create a 'CompiledMethod' for an optimized graph.
David Brazdil58282f42016-01-14 12:45:10 +0000358 CompiledMethod* Emit(ArenaAllocator* arena,
359 CodeVectorAllocator* code_allocator,
360 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000361 CompilerDriver* driver,
362 const DexFile::CodeItem* item) const;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000363
364 // Try compiling a method and return the code generator used for
365 // compiling it.
366 // This method:
367 // 1) Builds the graph. Returns null if it failed to build it.
David Brazdil58282f42016-01-14 12:45:10 +0000368 // 2) Transforms the graph to SSA. Returns null if it failed.
369 // 3) Runs optimizations on the graph, including register allocator.
370 // 4) Generates code with the `code_allocator` provided.
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000371 CodeGenerator* TryCompile(ArenaAllocator* arena,
372 CodeVectorAllocator* code_allocator,
373 const DexFile::CodeItem* code_item,
374 uint32_t access_flags,
375 InvokeType invoke_type,
376 uint16_t class_def_idx,
377 uint32_t method_idx,
378 jobject class_loader,
379 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000380 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000381 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000382 bool osr,
383 VariableSizedHandleScope* handles) const;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000384
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700385 void MaybeRunInliner(HGraph* graph,
386 CodeGenerator* codegen,
387 CompilerDriver* driver,
388 const DexCompilationUnit& dex_compilation_unit,
389 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700390 VariableSizedHandleScope* handles) const;
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700391
392 void RunArchOptimizations(InstructionSet instruction_set,
393 HGraph* graph,
394 CodeGenerator* codegen,
395 PassObserver* pass_observer) const;
396
Calin Juravle2be39e02015-04-21 13:56:34 +0100397 std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100398
Andreas Gampe53c913b2014-08-12 23:19:23 -0700399 std::unique_ptr<std::ostream> visualizer_output_;
400
Andreas Gampea0d81af2016-10-27 12:04:57 -0700401 mutable Mutex dump_mutex_; // To synchronize visualizer writing.
402
Andreas Gampe53c913b2014-08-12 23:19:23 -0700403 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
404};
405
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100406static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
407
408OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
Andreas Gampea0d81af2016-10-27 12:04:57 -0700409 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
410 dump_mutex_("Visualizer dump lock") {}
David Brazdilee690a32014-12-01 17:04:16 +0000411
412void OptimizingCompiler::Init() {
413 // Enable C1visualizer output. Must be done in Init() because the compiler
414 // driver is not fully initialized when passed to the compiler's constructor.
415 CompilerDriver* driver = GetCompilerDriver();
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000416 const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
David Brazdil866c0312015-01-13 21:21:31 +0000417 if (!cfg_file_name.empty()) {
Calin Juravle87000a92015-08-24 15:34:44 +0100418 std::ios_base::openmode cfg_file_mode =
Nicolas Geoffrayc903b6a2016-01-18 12:56:06 +0000419 driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
Calin Juravle87000a92015-08-24 15:34:44 +0100420 visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100421 }
Calin Juravle2be39e02015-04-21 13:56:34 +0100422 if (driver->GetDumpStats()) {
423 compilation_stats_.reset(new OptimizingCompilerStats());
424 }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100425}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000426
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000427void OptimizingCompiler::UnInit() const {
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000428}
429
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100430OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle2be39e02015-04-21 13:56:34 +0100431 if (compilation_stats_.get() != nullptr) {
432 compilation_stats_->Log();
433 }
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100434}
435
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000436bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
Vladimir Markodf739842016-03-23 16:59:07 +0000437 const DexFile& dex_file ATTRIBUTE_UNUSED) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000438 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700439}
440
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000441static bool IsInstructionSetSupported(InstructionSet instruction_set) {
Calin Juravlecff8cc72015-10-09 12:03:24 +0100442 return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
443 || instruction_set == kArm64
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000444 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 || instruction_set == kMips
Alexey Frunze4dda3372015-06-01 18:31:49 -0700446 || instruction_set == kMips64
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000447 || instruction_set == kX86
448 || instruction_set == kX86_64;
449}
450
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000451// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000452// TODO: Add support for other architectures and remove this function
453static bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000454 return instruction_set == kArm64
455 || instruction_set == kThumb2
Roland Levillain3b359c72015-11-17 19:35:12 +0000456 || instruction_set == kX86
Roland Levillain0d5a2812015-11-13 10:07:31 +0000457 || instruction_set == kX86_64;
458}
459
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700460// Strip pass name suffix to get optimization name.
461static std::string ConvertPassNameToOptimizationName(const std::string& pass_name) {
462 size_t pos = pass_name.find(kPassNameSeparator);
463 return pos == std::string::npos ? pass_name : pass_name.substr(0, pos);
464}
465
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700466static HOptimization* BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700467 const std::string& pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700468 ArenaAllocator* arena,
469 HGraph* graph,
470 OptimizingCompilerStats* stats,
471 CodeGenerator* codegen,
472 CompilerDriver* driver,
473 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700474 VariableSizedHandleScope* handles,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700475 SideEffectsAnalysis* most_recent_side_effects,
476 HInductionVarAnalysis* most_recent_induction) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700477 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700478 if (opt_name == BoundsCheckElimination::kBoundsCheckEliminationPassName) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700479 CHECK(most_recent_side_effects != nullptr && most_recent_induction != nullptr);
480 return new (arena) BoundsCheckElimination(graph,
481 *most_recent_side_effects,
482 most_recent_induction);
483 } else if (opt_name == GVNOptimization::kGlobalValueNumberingPassName) {
484 CHECK(most_recent_side_effects != nullptr);
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700485 return new (arena) GVNOptimization(graph, *most_recent_side_effects, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700486 } else if (opt_name == HConstantFolding::kConstantFoldingPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700487 return new (arena) HConstantFolding(graph, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700488 } else if (opt_name == HDeadCodeElimination::kDeadCodeEliminationPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700489 return new (arena) HDeadCodeElimination(graph, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700490 } else if (opt_name == HInliner::kInlinerPassName) {
491 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
492 return new (arena) HInliner(graph, // outer_graph
493 graph, // outermost_graph
494 codegen,
495 dex_compilation_unit, // outer_compilation_unit
496 dex_compilation_unit, // outermost_compilation_unit
497 driver,
498 handles,
499 stats,
500 number_of_dex_registers,
501 /* depth */ 0);
502 } else if (opt_name == HSharpening::kSharpeningPassName) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000503 return new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver, handles);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700504 } else if (opt_name == HSelectGenerator::kSelectGeneratorPassName) {
505 return new (arena) HSelectGenerator(graph, stats);
506 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
507 return new (arena) HInductionVarAnalysis(graph);
508 } else if (opt_name == InstructionSimplifier::kInstructionSimplifierPassName) {
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700509 return new (arena) InstructionSimplifier(graph, stats, pass_name.c_str());
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700510 } else if (opt_name == IntrinsicsRecognizer::kIntrinsicsRecognizerPassName) {
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100511 return new (arena) IntrinsicsRecognizer(graph, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700512 } else if (opt_name == LICM::kLoopInvariantCodeMotionPassName) {
513 CHECK(most_recent_side_effects != nullptr);
514 return new (arena) LICM(graph, *most_recent_side_effects, stats);
515 } else if (opt_name == LoadStoreElimination::kLoadStoreEliminationPassName) {
516 CHECK(most_recent_side_effects != nullptr);
517 return new (arena) LoadStoreElimination(graph, *most_recent_side_effects);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700518 } else if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
519 return new (arena) SideEffectsAnalysis(graph);
Nicolas Geoffrayebe16742016-10-05 09:55:42 +0100520 } else if (opt_name == HLoopOptimization::kLoopOptimizationPassName) {
521 return new (arena) HLoopOptimization(graph, most_recent_induction);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800522 } else if (opt_name == CHAGuardOptimization::kCHAGuardOptimizationPassName) {
523 return new (arena) CHAGuardOptimization(graph);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700524#ifdef ART_ENABLE_CODEGEN_arm
525 } else if (opt_name == arm::DexCacheArrayFixups::kDexCacheArrayFixupsArmPassName) {
Vladimir Marko68c981f2016-08-26 13:13:33 +0100526 return new (arena) arm::DexCacheArrayFixups(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700527 } else if (opt_name == arm::InstructionSimplifierArm::kInstructionSimplifierArmPassName) {
528 return new (arena) arm::InstructionSimplifierArm(graph, stats);
529#endif
530#ifdef ART_ENABLE_CODEGEN_arm64
531 } else if (opt_name == arm64::InstructionSimplifierArm64::kInstructionSimplifierArm64PassName) {
532 return new (arena) arm64::InstructionSimplifierArm64(graph, stats);
533#endif
534#ifdef ART_ENABLE_CODEGEN_mips
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700535 } else if (opt_name == mips::DexCacheArrayFixups::kDexCacheArrayFixupsMipsPassName) {
536 return new (arena) mips::DexCacheArrayFixups(graph, codegen, stats);
537 } else if (opt_name == mips::PcRelativeFixups::kPcRelativeFixupsMipsPassName) {
538 return new (arena) mips::PcRelativeFixups(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700539#endif
540#ifdef ART_ENABLE_CODEGEN_x86
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700541 } else if (opt_name == x86::PcRelativeFixups::kPcRelativeFixupsX86PassName) {
542 return new (arena) x86::PcRelativeFixups(graph, codegen, stats);
543 } else if (opt_name == x86::X86MemoryOperandGeneration::kX86MemoryOperandGenerationPassName) {
544 return new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700545#endif
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700546 }
547 return nullptr;
548}
549
550static ArenaVector<HOptimization*> BuildOptimizations(
551 const std::vector<std::string>& pass_names,
552 ArenaAllocator* arena,
553 HGraph* graph,
554 OptimizingCompilerStats* stats,
555 CodeGenerator* codegen,
556 CompilerDriver* driver,
557 const DexCompilationUnit& dex_compilation_unit,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700558 VariableSizedHandleScope* handles) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700559 // Few HOptimizations constructors require SideEffectsAnalysis or HInductionVarAnalysis
560 // instances. This method assumes that each of them expects the nearest instance preceeding it
561 // in the pass name list.
562 SideEffectsAnalysis* most_recent_side_effects = nullptr;
563 HInductionVarAnalysis* most_recent_induction = nullptr;
564 ArenaVector<HOptimization*> ret(arena->Adapter());
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700565 for (const std::string& pass_name : pass_names) {
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700566 HOptimization* opt = BuildOptimization(
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700567 pass_name,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700568 arena,
569 graph,
570 stats,
571 codegen,
572 driver,
573 dex_compilation_unit,
574 handles,
575 most_recent_side_effects,
576 most_recent_induction);
577 CHECK(opt != nullptr) << "Couldn't build optimization: \"" << pass_name << "\"";
578 ret.push_back(opt);
579
Wojciech Staszkiewiczb6a128322016-08-16 16:33:00 -0700580 std::string opt_name = ConvertPassNameToOptimizationName(pass_name);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700581 if (opt_name == SideEffectsAnalysis::kSideEffectsAnalysisPassName) {
582 most_recent_side_effects = down_cast<SideEffectsAnalysis*>(opt);
583 } else if (opt_name == HInductionVarAnalysis::kInductionPassName) {
584 most_recent_induction = down_cast<HInductionVarAnalysis*>(opt);
585 }
586 }
587 return ret;
588}
589
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700590void OptimizingCompiler::RunOptimizations(HOptimization* optimizations[],
591 size_t length,
592 PassObserver* pass_observer) const {
Calin Juravle10e244f2015-01-26 18:54:32 +0000593 for (size_t i = 0; i < length; ++i) {
David Brazdil69ba7b72015-06-23 18:27:30 +0100594 PassScope scope(optimizations[i]->GetPassName(), pass_observer);
595 optimizations[i]->Run();
Calin Juravle10e244f2015-01-26 18:54:32 +0000596 }
597}
598
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700599void OptimizingCompiler::MaybeRunInliner(HGraph* graph,
600 CodeGenerator* codegen,
601 CompilerDriver* driver,
602 const DexCompilationUnit& dex_compilation_unit,
603 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700604 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700605 OptimizingCompilerStats* stats = compilation_stats_.get();
Calin Juravleec748352015-07-29 13:52:12 +0100606 const CompilerOptions& compiler_options = driver->GetCompilerOptions();
607 bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
608 && (compiler_options.GetInlineMaxCodeUnits() > 0);
609 if (!should_inline) {
610 return;
611 }
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000612 size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000613 HInliner* inliner = new (graph->GetArena()) HInliner(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700614 graph, // outer_graph
615 graph, // outermost_graph
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000616 codegen,
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700617 dex_compilation_unit, // outer_compilation_unit
618 dex_compilation_unit, // outermost_compilation_unit
Nicolas Geoffray5949fa02015-12-18 10:57:10 +0000619 driver,
620 handles,
621 stats,
622 number_of_dex_registers,
623 /* depth */ 0);
Calin Juravlecdfed3d2015-10-26 14:05:01 +0000624 HOptimization* optimizations[] = { inliner };
Calin Juravleec748352015-07-29 13:52:12 +0100625
626 RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
627}
628
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700629void OptimizingCompiler::RunArchOptimizations(InstructionSet instruction_set,
630 HGraph* graph,
631 CodeGenerator* codegen,
632 PassObserver* pass_observer) const {
Wojciech Staszkiewicze28c7d02016-08-17 19:15:51 -0700633 UNUSED(codegen); // To avoid compilation error when compiling for svelte
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700634 OptimizingCompilerStats* stats = compilation_stats_.get();
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100635 ArenaAllocator* arena = graph->GetArena();
636 switch (instruction_set) {
Artem Serov2bbc9532016-10-21 11:51:50 +0100637#if defined(ART_ENABLE_CODEGEN_arm)
Vladimir Markob4536b72015-11-24 13:45:23 +0000638 case kThumb2:
639 case kArm: {
Vladimir Marko68c981f2016-08-26 13:13:33 +0100640 arm::DexCacheArrayFixups* fixups =
641 new (arena) arm::DexCacheArrayFixups(graph, codegen, stats);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300642 arm::InstructionSimplifierArm* simplifier =
643 new (arena) arm::InstructionSimplifierArm(graph, stats);
Artem Serov328429f2016-07-06 16:23:04 +0100644 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700645 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Vladimir Markob4536b72015-11-24 13:45:23 +0000646 HOptimization* arm_optimizations[] = {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300647 simplifier,
Artem Serov328429f2016-07-06 16:23:04 +0100648 side_effects,
649 gvn,
Nicolas Geoffray6b5afdd2016-01-22 09:31:52 +0000650 fixups
Vladimir Markob4536b72015-11-24 13:45:23 +0000651 };
652 RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
653 break;
654 }
655#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100656#ifdef ART_ENABLE_CODEGEN_arm64
657 case kArm64: {
658 arm64::InstructionSimplifierArm64* simplifier =
659 new (arena) arm64::InstructionSimplifierArm64(graph, stats);
660 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700661 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN$after_arch");
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100662 HInstructionScheduling* scheduling =
663 new (arena) HInstructionScheduling(graph, instruction_set);
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100664 HOptimization* arm64_optimizations[] = {
665 simplifier,
666 side_effects,
Alexandre Rames22aa54b2016-10-18 09:32:29 +0100667 gvn,
668 scheduling,
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100669 };
670 RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
671 break;
672 }
673#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700674#ifdef ART_ENABLE_CODEGEN_mips
675 case kMips: {
676 mips::PcRelativeFixups* pc_relative_fixups =
677 new (arena) mips::PcRelativeFixups(graph, codegen, stats);
678 mips::DexCacheArrayFixups* dex_cache_array_fixups =
Alexey Frunze06a46c42016-07-19 15:00:40 -0700679 new (arena) mips::DexCacheArrayFixups(graph, codegen, stats);
Alexey Frunzee3fb2452016-05-10 16:08:05 -0700680 HOptimization* mips_optimizations[] = {
681 pc_relative_fixups,
682 dex_cache_array_fixups
683 };
684 RunOptimizations(mips_optimizations, arraysize(mips_optimizations), pass_observer);
685 break;
686 }
687#endif
Mark Mendell0616ae02015-04-17 12:49:27 -0400688#ifdef ART_ENABLE_CODEGEN_x86
689 case kX86: {
Aart Bikd1c40452016-03-02 16:06:13 -0800690 x86::PcRelativeFixups* pc_relative_fixups =
691 new (arena) x86::PcRelativeFixups(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400692 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700693 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendell0616ae02015-04-17 12:49:27 -0400694 HOptimization* x86_optimizations[] = {
Mark Mendellee8d9712016-07-12 11:13:15 -0400695 pc_relative_fixups,
696 memory_gen
Mark Mendell0616ae02015-04-17 12:49:27 -0400697 };
698 RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
699 break;
700 }
701#endif
Mark Mendellee8d9712016-07-12 11:13:15 -0400702#ifdef ART_ENABLE_CODEGEN_x86_64
703 case kX86_64: {
704 x86::X86MemoryOperandGeneration* memory_gen =
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700705 new (arena) x86::X86MemoryOperandGeneration(graph, codegen, stats);
Mark Mendellee8d9712016-07-12 11:13:15 -0400706 HOptimization* x86_64_optimizations[] = {
707 memory_gen
708 };
709 RunOptimizations(x86_64_optimizations, arraysize(x86_64_optimizations), pass_observer);
710 break;
711 }
712#endif
Alexandre Rames44b9cf92015-08-19 15:39:06 +0100713 default:
714 break;
715 }
716}
717
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000718NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects.
719static void AllocateRegisters(HGraph* graph,
720 CodeGenerator* codegen,
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700721 PassObserver* pass_observer,
722 RegisterAllocator::Strategy strategy) {
Mingyao Yang700347e2016-03-02 14:59:32 -0800723 {
724 PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
725 pass_observer);
726 PrepareForRegisterAllocation(graph).Run();
727 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000728 SsaLivenessAnalysis liveness(graph, codegen);
729 {
730 PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
731 liveness.Analyze();
732 }
733 {
734 PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
Matthew Gharrity2cd05b72016-08-03 16:57:37 -0700735 RegisterAllocator::Create(graph->GetArena(), codegen, liveness, strategy)->AllocateRegisters();
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000736 }
737}
738
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700739void OptimizingCompiler::RunOptimizations(HGraph* graph,
740 CodeGenerator* codegen,
741 CompilerDriver* driver,
742 const DexCompilationUnit& dex_compilation_unit,
743 PassObserver* pass_observer,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700744 VariableSizedHandleScope* handles) const {
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700745 OptimizingCompilerStats* stats = compilation_stats_.get();
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100746 ArenaAllocator* arena = graph->GetArena();
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700747 if (driver->GetCompilerOptions().GetPassesToRun() != nullptr) {
748 ArenaVector<HOptimization*> optimizations = BuildOptimizations(
749 *driver->GetCompilerOptions().GetPassesToRun(),
750 arena,
751 graph,
752 stats,
753 codegen,
754 driver,
755 dex_compilation_unit,
756 handles);
757 RunOptimizations(&optimizations[0], optimizations.size(), pass_observer);
758 return;
759 }
760
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100761 HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700762 graph, stats, "dead_code_elimination$initial");
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100763 HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
Aart Bik2767f4b2016-10-28 15:03:53 -0700764 graph, stats, "dead_code_elimination$after_inlining");
765 HDeadCodeElimination* dce3 = new (arena) HDeadCodeElimination(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700766 graph, stats, "dead_code_elimination$final");
Andreas Gampeca620d72016-11-08 08:09:33 -0800767 HConstantFolding* fold1 = new (arena) HConstantFolding(graph, "constant_folding");
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100768 InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100769 HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats);
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700770 HConstantFolding* fold2 = new (arena) HConstantFolding(
771 graph, "constant_folding$after_inlining");
772 HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding$after_bce");
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100773 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
774 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100775 LICM* licm = new (arena) LICM(graph, *side_effects, stats);
Mingyao Yang8df69d42015-10-22 15:40:58 -0700776 LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
Aart Bik22af3be2015-09-10 12:50:58 -0700777 HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
Aart Bik4a342772015-11-30 10:17:46 -0800778 BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction);
Aart Bik281c6812016-08-26 11:31:48 -0700779 HLoopOptimization* loop = new (arena) HLoopOptimization(graph, induction);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000780 HSharpening* sharpening = new (arena) HSharpening(
781 graph, codegen, dex_compilation_unit, driver, handles);
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100782 InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
Aart Bik639cc8c2016-10-18 13:03:31 -0700783 graph, stats, "instruction_simplifier$after_inlining");
David Brazdil4833f5a2015-12-16 10:37:39 +0000784 InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
Aart Bik639cc8c2016-10-18 13:03:31 -0700785 graph, stats, "instruction_simplifier$after_bce");
786 InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -0700787 graph, stats, "instruction_simplifier$before_codegen");
Nicolas Geoffray762869d2016-07-15 15:28:35 +0100788 IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, stats);
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800789 CHAGuardOptimization* cha_guard = new (arena) CHAGuardOptimization(graph);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800790
Calin Juravleec748352015-07-29 13:52:12 +0100791 HOptimization* optimizations1[] = {
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100792 intrinsics,
David Brazdil4833f5a2015-12-16 10:37:39 +0000793 sharpening,
Vladimir Markoa3a3c592015-06-12 14:30:53 +0100794 fold1,
795 simplify1,
Nicolas Geoffray18e68732015-06-17 23:09:05 +0100796 dce1,
Calin Juravleec748352015-07-29 13:52:12 +0100797 };
Calin Juravleec748352015-07-29 13:52:12 +0100798 RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
799
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700800 MaybeRunInliner(graph, codegen, driver, dex_compilation_unit, pass_observer, handles);
David Brazdil95177982015-10-30 12:56:58 -0500801
David Brazdil8993caf2015-12-07 10:04:40 +0000802 HOptimization* optimizations2[] = {
David Brazdil74eb1b22015-12-14 11:44:01 +0000803 // SelectGenerator depends on the InstructionSimplifier removing
David Brazdil8993caf2015-12-07 10:04:40 +0000804 // redundant suspend checks to recognize empty blocks.
David Brazdil74eb1b22015-12-14 11:44:01 +0000805 select_generator,
David Brazdil8993caf2015-12-07 10:04:40 +0000806 fold2, // TODO: if we don't inline we can also skip fold2.
Aart Bik639cc8c2016-10-18 13:03:31 -0700807 simplify2,
Aart Bik2767f4b2016-10-28 15:03:53 -0700808 dce2,
David Brazdil8993caf2015-12-07 10:04:40 +0000809 side_effects,
810 gvn,
811 licm,
812 induction,
813 bce,
Aart Bik281c6812016-08-26 11:31:48 -0700814 loop,
David Brazdil8993caf2015-12-07 10:04:40 +0000815 fold3, // evaluates code generated by dynamic bce
Aart Bik639cc8c2016-10-18 13:03:31 -0700816 simplify3,
David Brazdil8993caf2015-12-07 10:04:40 +0000817 lse,
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800818 cha_guard,
Aart Bik2767f4b2016-10-28 15:03:53 -0700819 dce3,
David Brazdil8993caf2015-12-07 10:04:40 +0000820 // The codegen has a few assumptions that only the instruction simplifier
821 // can satisfy. For example, the code generator does not expect to see a
822 // HTypeConversion from a type to the same type.
Aart Bik639cc8c2016-10-18 13:03:31 -0700823 simplify4,
David Brazdil8993caf2015-12-07 10:04:40 +0000824 };
825 RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
David Brazdilbbd733e2015-08-18 17:48:17 +0100826
Wojciech Staszkiewiczf5fb0902016-07-22 13:33:11 -0700827 RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, pass_observer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000828}
829
Vladimir Marko58155012015-08-19 12:49:41 +0000830static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
831 ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
832 codegen->EmitLinkerPatches(&linker_patches);
833
834 // Sort patches by literal offset. Required for .oat_patches encoding.
835 std::sort(linker_patches.begin(), linker_patches.end(),
836 [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
837 return lhs.LiteralOffset() < rhs.LiteralOffset();
838 });
839
840 return linker_patches;
841}
842
David Brazdil58282f42016-01-14 12:45:10 +0000843CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
844 CodeVectorAllocator* code_allocator,
845 CodeGenerator* codegen,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000846 CompilerDriver* compiler_driver,
847 const DexFile::CodeItem* code_item) const {
Vladimir Marko58155012015-08-19 12:49:41 +0000848 ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
Vladimir Markof9f64412015-09-02 14:05:49 +0100849 ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000850 stack_map.resize(codegen->ComputeStackMapsSize());
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000851 codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000852
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100853 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000854 compiler_driver,
855 codegen->GetInstructionSet(),
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000856 ArrayRef<const uint8_t>(code_allocator->GetMemory()),
Roland Levillainaa9b7c42015-02-17 15:40:09 +0000857 // Follow Quick's behavior and set the frame size to zero if it is
858 // considered "empty" (see the definition of
859 // art::CodeGenerator::HasEmptyFrame).
860 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000861 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000862 codegen->GetFpuSpillMask(),
David Srbeckyf71b3ad2015-12-08 15:05:08 +0000863 ArrayRef<const SrcMapElem>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100864 ArrayRef<const uint8_t>(stack_map),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100865 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
Vladimir Marko58155012015-08-19 12:49:41 +0000866 ArrayRef<const LinkerPatch>(linker_patches));
Mathieu Chartiered150002015-08-28 11:16:54 -0700867
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100868 return compiled_method;
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000869}
870
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000871CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
872 CodeVectorAllocator* code_allocator,
873 const DexFile::CodeItem* code_item,
874 uint32_t access_flags,
875 InvokeType invoke_type,
876 uint16_t class_def_idx,
877 uint32_t method_idx,
878 jobject class_loader,
879 const DexFile& dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000880 Handle<mirror::DexCache> dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000881 ArtMethod* method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000882 bool osr,
883 VariableSizedHandleScope* handles) const {
Calin Juravle2be39e02015-04-21 13:56:34 +0100884 MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000885 CompilerDriver* compiler_driver = GetCompilerDriver();
886 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Calin Juravlecff8cc72015-10-09 12:03:24 +0100887
Roland Levillain3b359c72015-11-17 19:35:12 +0000888 // Always use the Thumb-2 assembler: some runtime functionality
889 // (like implicit stack overflow checks) assume Thumb-2.
Artem Serovba6b6792016-08-15 14:22:07 +0100890 DCHECK_NE(instruction_set, kArm);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100891
892 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000893 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle2be39e02015-04-21 13:56:34 +0100894 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100895 return nullptr;
896 }
897
Roland Levillain0d5a2812015-11-13 10:07:31 +0000898 // When read barriers are enabled, do not attempt to compile for
899 // instruction sets that have no read barrier support.
900 if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) {
901 return nullptr;
902 }
903
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000904 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle2be39e02015-04-21 13:56:34 +0100905 MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000906 return nullptr;
907 }
908
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000909 // Implementation of the space filter: do not compile a code item whose size in
Nicolas Geoffray432bf3d2015-07-17 11:11:09 +0100910 // code units is bigger than 128.
911 static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000912 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
Andreas Gampe29d38e72016-03-23 15:31:51 +0000913 if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000914 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
Calin Juravle2be39e02015-04-21 13:56:34 +0100915 MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
Nicolas Geoffray36540cb2015-03-23 14:45:53 +0000916 return nullptr;
917 }
918
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100919 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000920 DexCompilationUnit dex_compilation_unit(
Vladimir Markodf739842016-03-23 16:59:07 +0000921 class_loader,
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100922 class_linker,
Vladimir Markodf739842016-03-23 16:59:07 +0000923 dex_file,
924 code_item,
925 class_def_idx,
926 method_idx,
927 access_flags,
928 /* verified_method */ nullptr,
929 dex_cache);
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000930
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100931 bool requires_barrier = dex_compilation_unit.IsConstructor()
932 && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
933 dex_compilation_unit.GetDexFile(),
934 dex_compilation_unit.GetClassDefIndex());
David Brazdilbadd8262016-02-02 16:28:56 +0000935
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000936 HGraph* graph = new (arena) HGraph(
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000937 arena,
938 dex_file,
939 method_idx,
940 requires_barrier,
941 compiler_driver->GetInstructionSet(),
942 kInvalidInvokeType,
943 compiler_driver->GetCompilerOptions().GetDebuggable(),
944 osr);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000945
David Brazdilbadd8262016-02-02 16:28:56 +0000946 const uint8_t* interpreter_metadata = nullptr;
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000947 if (method == nullptr) {
David Brazdilbadd8262016-02-02 16:28:56 +0000948 ScopedObjectAccess soa(Thread::Current());
949 StackHandleScope<1> hs(soa.Self());
950 Handle<mirror::ClassLoader> loader(hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700951 soa.Decode<mirror::ClassLoader>(class_loader)));
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000952 method = compiler_driver->ResolveMethod(
David Brazdilbadd8262016-02-02 16:28:56 +0000953 soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000954 }
955 // For AOT compilation, we may not get a method, for example if its class is erroneous.
956 // JIT should always have a method.
957 DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr);
958 if (method != nullptr) {
959 graph->SetArtMethod(method);
960 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko97d7e1c2016-10-04 14:44:28 +0100961 interpreter_metadata = method->GetQuickenedInfo(class_linker->GetImagePointerSize());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800962 dex::TypeIndex type_index = method->GetDeclaringClass()->GetDexTypeIndex();
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +0000963
964 // Update the dex cache if the type is not in it yet. Note that under AOT,
965 // the verifier must have set it, but under JIT, there's no guarantee, as we
966 // don't necessarily run the verifier.
967 // The compiler and the compiler driver assume the compiling class is
968 // in the dex cache.
969 if (dex_cache->GetResolvedType(type_index) == nullptr) {
970 dex_cache->SetResolvedType(type_index, method->GetDeclaringClass());
Nicolas Geoffray12abcbd2016-06-06 15:51:58 +0000971 }
David Brazdilbadd8262016-02-02 16:28:56 +0000972 }
973
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000974 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000975 CodeGenerator::Create(graph,
976 instruction_set,
977 *compiler_driver->GetInstructionSetFeatures(),
Calin Juravle2ae48182016-03-16 14:05:09 +0000978 compiler_driver->GetCompilerOptions(),
979 compilation_stats_.get()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000980 if (codegen.get() == nullptr) {
Calin Juravle2be39e02015-04-21 13:56:34 +0100981 MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982 return nullptr;
983 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100984 codegen->GetAssembler()->cfi().SetEnabled(
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000985 compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986
David Brazdil69ba7b72015-06-23 18:27:30 +0100987 PassObserver pass_observer(graph,
David Brazdil69ba7b72015-06-23 18:27:30 +0100988 codegen.get(),
989 visualizer_output_.get(),
Andreas Gampea0d81af2016-10-27 12:04:57 -0700990 compiler_driver,
991 dump_mutex_);
David Brazdil5e8b1372015-01-23 14:39:08 +0000992
David Brazdil809658e2015-02-05 11:34:02 +0000993 {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000994 VLOG(compiler) << "Building " << pass_observer.GetMethodName();
995 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
996 HGraphBuilder builder(graph,
997 &dex_compilation_unit,
998 &dex_compilation_unit,
999 &dex_file,
1000 *code_item,
1001 compiler_driver,
1002 compilation_stats_.get(),
1003 interpreter_metadata,
1004 dex_cache,
1005 handles);
1006 GraphAnalysisResult result = builder.BuildGraph();
1007 if (result != kAnalysisSuccess) {
1008 switch (result) {
1009 case kAnalysisSkipped:
1010 MaybeRecordStat(MethodCompilationStat::kNotCompiledSkipped);
1011 break;
1012 case kAnalysisInvalidBytecode:
1013 MaybeRecordStat(MethodCompilationStat::kNotCompiledInvalidBytecode);
1014 break;
1015 case kAnalysisFailThrowCatchLoop:
1016 MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop);
1017 break;
1018 case kAnalysisFailAmbiguousArrayOp:
1019 MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
1020 break;
1021 case kAnalysisSuccess:
1022 UNREACHABLE();
David Brazdil809658e2015-02-05 11:34:02 +00001023 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001024 pass_observer.SetGraphInBadState();
1025 return nullptr;
Nicolas Geoffrayf5370122014-12-02 11:51:19 +00001026 }
David Brazdilbadd8262016-02-02 16:28:56 +00001027 }
Vladimir Markof9f64412015-09-02 14:05:49 +01001028
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001029 RunOptimizations(graph,
1030 codegen.get(),
1031 compiler_driver,
1032 dex_compilation_unit,
1033 &pass_observer,
1034 handles);
1035
1036 RegisterAllocator::Strategy regalloc_strategy =
1037 compiler_options.GetRegisterAllocationStrategy();
1038 AllocateRegisters(graph, codegen.get(), &pass_observer, regalloc_strategy);
1039
1040 codegen->Compile(code_allocator);
1041 pass_observer.DumpDisassembly();
1042
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001043 return codegen.release();
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001044}
1045
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001046CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
1047 uint32_t access_flags,
1048 InvokeType invoke_type,
1049 uint16_t class_def_idx,
1050 uint32_t method_idx,
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001051 jobject jclass_loader,
Mathieu Chartier736b5602015-09-02 14:54:11 -07001052 const DexFile& dex_file,
1053 Handle<mirror::DexCache> dex_cache) const {
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001054 CompilerDriver* compiler_driver = GetCompilerDriver();
1055 CompiledMethod* method = nullptr;
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001056 DCHECK(Runtime::Current()->IsAotCompiler());
1057 const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
1058 DCHECK(!verified_method->HasRuntimeThrow());
1059 if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001060 || verifier::MethodVerifier::CanCompilerHandleVerificationFailure(
1061 verified_method->GetEncounteredVerificationFailures())) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001062 ArenaAllocator arena(Runtime::Current()->GetArenaPool());
1063 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001064 std::unique_ptr<CodeGenerator> codegen;
1065 {
1066 ScopedObjectAccess soa(Thread::Current());
1067 VariableSizedHandleScope handles(soa.Self());
1068 // Go to native so that we don't block GC during compilation.
1069 ScopedThreadSuspension sts(soa.Self(), kNative);
1070 codegen.reset(
1071 TryCompile(&arena,
1072 &code_allocator,
1073 code_item,
1074 access_flags,
1075 invoke_type,
1076 class_def_idx,
1077 method_idx,
1078 jclass_loader,
1079 dex_file,
1080 dex_cache,
1081 nullptr,
1082 /* osr */ false,
1083 &handles));
1084 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001085 if (codegen.get() != nullptr) {
Calin Juravlead543382015-11-19 17:26:29 +00001086 MaybeRecordStat(MethodCompilationStat::kCompiled);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001087 method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001088
1089 if (kArenaAllocatorCountAllocations) {
1090 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1091 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001092 LOG(INFO) << dex_file.PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001093 }
1094 }
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001095 }
Nicolas Geoffray0c3c2662015-10-15 13:53:04 +01001096 } else {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001097 if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
1098 MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
1099 } else {
Calin Juravlead543382015-11-19 17:26:29 +00001100 MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001101 }
Calin Juravlef1c6d9e2015-04-13 18:42:21 +01001102 }
1103
Calin Juravlecff8cc72015-10-09 12:03:24 +01001104 if (kIsDebugBuild &&
1105 IsCompilingWithCoreImage() &&
Roland Levillain0d5a2812015-11-13 10:07:31 +00001106 IsInstructionSetSupported(compiler_driver->GetInstructionSet()) &&
1107 (!kEmitCompilerReadBarrier ||
1108 InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) {
1109 // For testing purposes, we put a special marker on method names
1110 // that should be compiled with this compiler (when the the
1111 // instruction set is supported -- and has support for read
1112 // barriers, if they are enabled). This makes sure we're not
1113 // regressing.
David Sehr709b0702016-10-13 09:12:37 -07001114 std::string method_name = dex_file.PrettyMethod(method_idx);
Calin Juravle09b1d6f2015-10-07 12:08:54 +01001115 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
1116 DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
1117 }
1118
Nicolas Geoffray12be74e2015-03-30 13:29:08 +01001119 return method;
Nicolas Geoffray216eaa22015-03-17 17:09:30 +00001120}
1121
Andreas Gampe53c913b2014-08-12 23:19:23 -07001122Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
1123 return new OptimizingCompiler(driver);
1124}
1125
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001126bool IsCompilingWithCoreImage() {
1127 const std::string& image = Runtime::Current()->GetImageLocation();
Jeff Haodcdc85b2015-12-04 14:06:18 -08001128 // TODO: This is under-approximating...
Andreas Gampe9186ced2016-12-12 14:28:21 -08001129 if (android::base::EndsWith(image, "core.art") ||
1130 android::base::EndsWith(image, "core-optimizing.art")) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08001131 return true;
1132 }
1133 return false;
Nicolas Geoffray335005e2015-06-25 10:01:47 +01001134}
1135
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001136bool OptimizingCompiler::JitCompile(Thread* self,
1137 jit::JitCodeCache* code_cache,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001138 ArtMethod* method,
1139 bool osr) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001140 StackHandleScope<3> hs(self);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001141 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
1142 method->GetDeclaringClass()->GetClassLoader()));
1143 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
Nicolas Geoffray250a3782016-04-20 16:27:53 +01001144 DCHECK(method->IsCompilable());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001145
1146 jobject jclass_loader = class_loader.ToJObject();
1147 const DexFile* dex_file = method->GetDexFile();
1148 const uint16_t class_def_idx = method->GetClassDefIndex();
1149 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1150 const uint32_t method_idx = method->GetDexMethodIndex();
1151 const uint32_t access_flags = method->GetAccessFlags();
1152 const InvokeType invoke_type = method->GetInvokeType();
1153
Nicolas Geoffray25e04562016-03-01 13:17:58 +00001154 ArenaAllocator arena(Runtime::Current()->GetJitArenaPool());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001155 CodeVectorAllocator code_allocator(&arena);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001156 VariableSizedHandleScope handles(self);
1157
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001158 std::unique_ptr<CodeGenerator> codegen;
1159 {
1160 // Go to native so that we don't block GC during compilation.
1161 ScopedThreadSuspension sts(self, kNative);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001162 codegen.reset(
1163 TryCompile(&arena,
1164 &code_allocator,
1165 code_item,
1166 access_flags,
1167 invoke_type,
1168 class_def_idx,
1169 method_idx,
1170 jclass_loader,
1171 *dex_file,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001172 dex_cache,
Nicolas Geoffray3aaf9642016-06-07 14:14:37 +00001173 method,
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001174 osr,
1175 &handles));
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001176 if (codegen.get() == nullptr) {
1177 return false;
1178 }
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001179
1180 if (kArenaAllocatorCountAllocations) {
1181 if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
1182 MemStats mem_stats(arena.GetMemStats());
David Sehr709b0702016-10-13 09:12:37 -07001183 LOG(INFO) << dex_file->PrettyMethod(method_idx) << " " << Dumpable<MemStats>(mem_stats);
Vladimir Marko3a40bf22016-03-22 16:26:33 +00001184 }
1185 }
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001186 }
1187
1188 size_t stack_map_size = codegen->ComputeStackMapsSize();
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001189 size_t number_of_roots = codegen->GetNumberOfJitRoots();
1190 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1191 // We allocate an object array to ensure the JIT roots that we will collect in EmitJitRoots
1192 // will be visible by the GC between EmitLiterals and CommitCode. Once CommitCode is
1193 // executed, this array is not needed.
1194 Handle<mirror::ObjectArray<mirror::Object>> roots(
1195 hs.NewHandle(mirror::ObjectArray<mirror::Object>::Alloc(
1196 self, class_linker->GetClassRoot(ClassLinker::kObjectArrayClass), number_of_roots)));
1197 if (roots.Get() == nullptr) {
1198 // Out of memory, just clear the exception to avoid any Java exception uncaught problems.
1199 DCHECK(self->IsExceptionPending());
1200 self->ClearException();
1201 return false;
1202 }
1203 uint8_t* stack_map_data = nullptr;
1204 uint8_t* roots_data = nullptr;
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001205 uint32_t data_size = code_cache->ReserveData(
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001206 self, stack_map_size, number_of_roots, method, &stack_map_data, &roots_data);
1207 if (stack_map_data == nullptr || roots_data == nullptr) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001208 return false;
1209 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01001210 MaybeRecordStat(MethodCompilationStat::kCompiled);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001211 codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00001212 codegen->EmitJitRoots(code_allocator.GetData(), roots, roots_data);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001213
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001214 const void* code = code_cache->CommitCode(
1215 self,
1216 method,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001217 stack_map_data,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001218 roots_data,
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001219 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
1220 codegen->GetCoreSpillMask(),
1221 codegen->GetFpuSpillMask(),
1222 code_allocator.GetMemory().data(),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +00001223 code_allocator.GetSize(),
Nicolas Geoffrayed015ac2016-12-15 17:58:48 +00001224 data_size,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001225 osr,
Mingyao Yang063fc772016-08-02 11:02:54 -07001226 roots,
1227 codegen->GetGraph()->HasShouldDeoptimizeFlag(),
1228 codegen->GetGraph()->GetCHASingleImplementationList());
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001229
1230 if (code == nullptr) {
Nicolas Geoffrayf46501c2016-11-22 13:45:36 +00001231 code_cache->ClearData(self, stack_map_data, roots_data);
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001232 return false;
1233 }
1234
David Srbecky91cc06c2016-03-07 16:13:58 +00001235 const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
1236 if (compiler_options.GetGenerateDebugInfo()) {
David Srbecky5cc349f2015-12-18 15:04:48 +00001237 const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
1238 const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
David Srbecky09c2a6b2016-03-11 17:11:44 +00001239 debug::MethodDebugInfo info = debug::MethodDebugInfo();
1240 info.trampoline_name = nullptr;
David Srbecky197160d2016-03-07 17:33:57 +00001241 info.dex_file = dex_file;
1242 info.class_def_index = class_def_idx;
1243 info.dex_method_index = method_idx;
1244 info.access_flags = access_flags;
1245 info.code_item = code_item;
1246 info.isa = codegen->GetInstructionSet();
1247 info.deduped = false;
1248 info.is_native_debuggable = compiler_options.GetNativeDebuggable();
1249 info.is_optimized = true;
1250 info.is_code_address_text_relative = false;
1251 info.code_address = code_address;
1252 info.code_size = code_allocator.GetSize();
1253 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
1254 info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
1255 info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
Vladimir Marko93205e32016-04-13 11:59:46 +01001256 std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
David Srbecky5d811202016-03-08 13:21:22 +00001257 GetCompilerDriver()->GetInstructionSet(),
1258 GetCompilerDriver()->GetInstructionSetFeatures(),
David Srbeckyfe736b72016-03-09 11:44:44 +00001259 ArrayRef<const debug::MethodDebugInfo>(&info, 1));
Vladimir Marko93205e32016-04-13 11:59:46 +01001260 CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
David Srbecky5cc349f2015-12-18 15:04:48 +00001261 }
1262
Nicolas Geoffraya4f81542016-03-08 16:57:48 +00001263 Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed());
1264
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +00001265 return true;
1266}
1267
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001268} // namespace art