blob: a37ea1e9a310a1eec9d135243068bb306e9300ce [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
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
17#include "code_generator_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010023#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080024#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010025#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "intrinsics.h"
27#include "intrinsics_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000030#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010031#include "thread.h"
32#include "utils/arm64/assembler_arm64.h"
33#include "utils/assembler.h"
34#include "utils/stack_checks.h"
35
36
37using namespace vixl; // NOLINT(build/namespaces)
38
39#ifdef __
40#error "ARM64 Codegen VIXL macro-assembler macro already defined."
41#endif
42
Alexandre Rames5319def2014-10-23 10:03:10 +010043namespace art {
44
Roland Levillain22ccc3a2015-11-24 13:10:05 +000045template<class MirrorType>
46class GcRoot;
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace arm64 {
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050using helpers::CPURegisterFrom;
51using helpers::DRegisterFrom;
52using helpers::FPRegisterFrom;
53using helpers::HeapOperand;
54using helpers::HeapOperandFrom;
55using helpers::InputCPURegisterAt;
56using helpers::InputFPRegisterAt;
57using helpers::InputRegisterAt;
58using helpers::InputOperandAt;
59using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080060using helpers::LocationFrom;
61using helpers::OperandFromMemOperand;
62using helpers::OutputCPURegister;
63using helpers::OutputFPRegister;
64using helpers::OutputRegister;
65using helpers::RegisterFrom;
66using helpers::StackOperandFrom;
67using helpers::VIXLRegCodeFromART;
68using helpers::WRegisterFrom;
69using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000070using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080071using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
Alexandre Rames5319def2014-10-23 10:03:10 +010073static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000074// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080075// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
76// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000077static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010078
Alexandre Rames5319def2014-10-23 10:03:10 +010079inline Condition ARM64Condition(IfCondition cond) {
80 switch (cond) {
81 case kCondEQ: return eq;
82 case kCondNE: return ne;
83 case kCondLT: return lt;
84 case kCondLE: return le;
85 case kCondGT: return gt;
86 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070087 case kCondB: return lo;
88 case kCondBE: return ls;
89 case kCondA: return hi;
90 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010091 }
Roland Levillain7f63c522015-07-13 15:54:55 +000092 LOG(FATAL) << "Unreachable";
93 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010094}
95
Vladimir Markod6e069b2016-01-18 11:11:01 +000096inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
97 // The ARM64 condition codes can express all the necessary branches, see the
98 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
99 // There is no dex instruction or HIR that would need the missing conditions
100 // "equal or unordered" or "not equal".
101 switch (cond) {
102 case kCondEQ: return eq;
103 case kCondNE: return ne /* unordered */;
104 case kCondLT: return gt_bias ? cc : lt /* unordered */;
105 case kCondLE: return gt_bias ? ls : le /* unordered */;
106 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
107 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
108 default:
109 LOG(FATAL) << "UNREACHABLE";
110 UNREACHABLE();
111 }
112}
113
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000115 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
116 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
117 // but we use the exact registers for clarity.
118 if (return_type == Primitive::kPrimFloat) {
119 return LocationFrom(s0);
120 } else if (return_type == Primitive::kPrimDouble) {
121 return LocationFrom(d0);
122 } else if (return_type == Primitive::kPrimLong) {
123 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100124 } else if (return_type == Primitive::kPrimVoid) {
125 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000126 } else {
127 return LocationFrom(w0);
128 }
129}
130
Alexandre Rames5319def2014-10-23 10:03:10 +0100131Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000132 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100133}
134
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700135// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
136#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Alexandre Rames67555f72014-11-18 10:55:16 +0000137#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100138
Zheng Xuda403092015-04-24 17:35:39 +0800139// Calculate memory accessing operand for save/restore live registers.
140static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
141 RegisterSet* register_set,
142 int64_t spill_offset,
143 bool is_save) {
144 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
145 codegen->GetNumberOfCoreRegisters(),
146 register_set->GetFloatingPointRegisters(),
147 codegen->GetNumberOfFloatingPointRegisters()));
148
149 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
150 register_set->GetCoreRegisters() & (~callee_saved_core_registers.list()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000151 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
152 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.list()));
Zheng Xuda403092015-04-24 17:35:39 +0800153
154 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
155 UseScratchRegisterScope temps(masm);
156
157 Register base = masm->StackPointer();
158 int64_t core_spill_size = core_list.TotalSizeInBytes();
159 int64_t fp_spill_size = fp_list.TotalSizeInBytes();
160 int64_t reg_size = kXRegSizeInBytes;
161 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
162 uint32_t ls_access_size = WhichPowerOf2(reg_size);
163 if (((core_list.Count() > 1) || (fp_list.Count() > 1)) &&
164 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
165 // If the offset does not fit in the instruction's immediate field, use an alternate register
166 // to compute the base address(float point registers spill base address).
167 Register new_base = temps.AcquireSameSizeAs(base);
168 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
169 base = new_base;
170 spill_offset = -core_spill_size;
171 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
172 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
173 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
174 }
175
176 if (is_save) {
177 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
178 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
179 } else {
180 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
181 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
182 }
183}
184
185void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
186 RegisterSet* register_set = locations->GetLiveRegisters();
187 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
188 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
189 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
190 // If the register holds an object, update the stack mask.
191 if (locations->RegisterContainsObject(i)) {
192 locations->SetStackBit(stack_offset / kVRegSize);
193 }
194 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_core_stack_offsets_[i] = stack_offset;
197 stack_offset += kXRegSizeInBytes;
198 }
199 }
200
201 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
202 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
203 register_set->ContainsFloatingPointRegister(i)) {
204 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
205 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
206 saved_fpu_stack_offsets_[i] = stack_offset;
207 stack_offset += kDRegSizeInBytes;
208 }
209 }
210
211 SaveRestoreLiveRegistersHelper(codegen, register_set,
212 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
213}
214
215void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
216 RegisterSet* register_set = locations->GetLiveRegisters();
217 SaveRestoreLiveRegistersHelper(codegen, register_set,
218 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
219}
220
Alexandre Rames5319def2014-10-23 10:03:10 +0100221class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
222 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100224
Alexandre Rames67555f72014-11-18 10:55:16 +0000225 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100226 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000227 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100228
Alexandre Rames5319def2014-10-23 10:03:10 +0100229 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000230 if (instruction_->CanThrowIntoCatchBlock()) {
231 // Live registers will be restored in the catch block if caught.
232 SaveLiveRegisters(codegen, instruction_->GetLocations());
233 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000234 // We're moving two locations to locations that could overlap, so we need a parallel
235 // move resolver.
236 InvokeRuntimeCallingConvention calling_convention;
237 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100238 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
239 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000240 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000241 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800242 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100243 }
244
Alexandre Rames8158f282015-08-07 10:26:17 +0100245 bool IsFatal() const OVERRIDE { return true; }
246
Alexandre Rames9931f312015-06-19 14:47:01 +0100247 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
248
Alexandre Rames5319def2014-10-23 10:03:10 +0100249 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100250 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
251};
252
Alexandre Rames67555f72014-11-18 10:55:16 +0000253class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
254 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000255 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000256
257 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
258 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
259 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000260 if (instruction_->CanThrowIntoCatchBlock()) {
261 // Live registers will be restored in the catch block if caught.
262 SaveLiveRegisters(codegen, instruction_->GetLocations());
263 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000264 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000265 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800266 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000267 }
268
Alexandre Rames8158f282015-08-07 10:26:17 +0100269 bool IsFatal() const OVERRIDE { return true; }
270
Alexandre Rames9931f312015-06-19 14:47:01 +0100271 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
272
Alexandre Rames67555f72014-11-18 10:55:16 +0000273 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000274 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
275};
276
277class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
278 public:
279 LoadClassSlowPathARM64(HLoadClass* cls,
280 HInstruction* at,
281 uint32_t dex_pc,
282 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000283 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000284 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
285 }
286
287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
288 LocationSummary* locations = at_->GetLocations();
289 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
290
291 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000292 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000293
294 InvokeRuntimeCallingConvention calling_convention;
295 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
297 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000298 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800299 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100300 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000304
305 // Move the class to the desired location.
306 Location out = locations->Out();
307 if (out.IsValid()) {
308 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
309 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000310 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000311 }
312
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000313 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 __ B(GetExitLabel());
315 }
316
Alexandre Rames9931f312015-06-19 14:47:01 +0100317 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
318
Alexandre Rames67555f72014-11-18 10:55:16 +0000319 private:
320 // The class this slow path will load.
321 HLoadClass* const cls_;
322
323 // The instruction where this slow path is happening.
324 // (Might be the load class or an initialization check).
325 HInstruction* const at_;
326
327 // The dex PC of `at_`.
328 const uint32_t dex_pc_;
329
330 // Whether to initialize the class.
331 const bool do_clinit_;
332
333 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
334};
335
336class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
337 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000338 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
340 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
341 LocationSummary* locations = instruction_->GetLocations();
342 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
343 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
344
345 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000346 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000347
348 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000349 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
350 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000351 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000352 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100353 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358 __ B(GetExitLabel());
359 }
360
Alexandre Rames9931f312015-06-19 14:47:01 +0100361 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
362
Alexandre Rames67555f72014-11-18 10:55:16 +0000363 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000364 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
365};
366
Alexandre Rames5319def2014-10-23 10:03:10 +0100367class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
368 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000369 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100370
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
372 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000374 if (instruction_->CanThrowIntoCatchBlock()) {
375 // Live registers will be restored in the catch block if caught.
376 SaveLiveRegisters(codegen, instruction_->GetLocations());
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000379 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800380 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100381 }
382
Alexandre Rames8158f282015-08-07 10:26:17 +0100383 bool IsFatal() const OVERRIDE { return true; }
384
Alexandre Rames9931f312015-06-19 14:47:01 +0100385 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
386
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100388 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
389};
390
391class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
392 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100393 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000394 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100395
Alexandre Rames67555f72014-11-18 10:55:16 +0000396 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
397 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100398 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000399 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000400 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000401 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800402 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000403 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000404 if (successor_ == nullptr) {
405 __ B(GetReturnLabel());
406 } else {
407 __ B(arm64_codegen->GetLabelOf(successor_));
408 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100409 }
410
411 vixl::Label* GetReturnLabel() {
412 DCHECK(successor_ == nullptr);
413 return &return_label_;
414 }
415
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100416 HBasicBlock* GetSuccessor() const {
417 return successor_;
418 }
419
Alexandre Rames9931f312015-06-19 14:47:01 +0100420 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
421
Alexandre Rames5319def2014-10-23 10:03:10 +0100422 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100423 // If not null, the block to branch to after the suspend check.
424 HBasicBlock* const successor_;
425
426 // If `successor_` is null, the label to branch to after the suspend check.
427 vixl::Label return_label_;
428
429 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
430};
431
Alexandre Rames67555f72014-11-18 10:55:16 +0000432class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
433 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000434 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000435 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000436
437 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000438 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100439 Location class_to_check = locations->InAt(1);
440 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
441 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000442 DCHECK(instruction_->IsCheckCast()
443 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
444 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100445 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000446
Alexandre Rames67555f72014-11-18 10:55:16 +0000447 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000449 if (!is_fatal_) {
450 SaveLiveRegisters(codegen, locations);
451 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000452
453 // We're moving two locations to locations that could overlap, so we need a parallel
454 // move resolver.
455 InvokeRuntimeCallingConvention calling_convention;
456 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100457 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
458 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000459
460 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000461 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100462 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000463 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
464 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000465 Primitive::Type ret_type = instruction_->GetType();
466 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
467 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
468 } else {
469 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100470 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800471 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000472 }
473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000474 if (!is_fatal_) {
475 RestoreLiveRegisters(codegen, locations);
476 __ B(GetExitLabel());
477 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000478 }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100482
Alexandre Rames67555f72014-11-18 10:55:16 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000485
Alexandre Rames67555f72014-11-18 10:55:16 +0000486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
487};
488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700489class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000492 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
497 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800498 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
499 instruction_,
500 instruction_->GetDexPc(),
501 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000502 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 }
504
Alexandre Rames9931f312015-06-19 14:47:01 +0100505 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
506
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700507 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700508 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
509};
510
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100511class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
512 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000513 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 LocationSummary* locations = instruction_->GetLocations();
517 __ Bind(GetEntryLabel());
518 SaveLiveRegisters(codegen, locations);
519
520 InvokeRuntimeCallingConvention calling_convention;
521 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
522 parallel_move.AddMove(
523 locations->InAt(0),
524 LocationFrom(calling_convention.GetRegisterAt(0)),
525 Primitive::kPrimNot,
526 nullptr);
527 parallel_move.AddMove(
528 locations->InAt(1),
529 LocationFrom(calling_convention.GetRegisterAt(1)),
530 Primitive::kPrimInt,
531 nullptr);
532 parallel_move.AddMove(
533 locations->InAt(2),
534 LocationFrom(calling_convention.GetRegisterAt(2)),
535 Primitive::kPrimNot,
536 nullptr);
537 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
538
539 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
540 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
541 instruction_,
542 instruction_->GetDexPc(),
543 this);
544 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
545 RestoreLiveRegisters(codegen, locations);
546 __ B(GetExitLabel());
547 }
548
549 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
550
551 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100552 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
553};
554
Zheng Xu3927c8b2015-11-18 17:46:25 +0800555void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
556 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000557 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800558
559 // We are about to use the assembler to place literals directly. Make sure we have enough
560 // underlying code buffer and we have generated the jump table with right size.
561 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
562 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
563
564 __ Bind(&table_start_);
565 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
566 for (uint32_t i = 0; i < num_entries; i++) {
567 vixl::Label* target_label = codegen->GetLabelOf(successors[i]);
568 DCHECK(target_label->IsBound());
569 ptrdiff_t jump_offset = target_label->location() - table_start_.location();
570 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
571 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
572 Literal<int32_t> literal(jump_offset);
573 __ place(&literal);
574 }
575}
576
Roland Levillain44015862016-01-22 11:47:17 +0000577// Slow path marking an object during a read barrier.
578class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
579 public:
580 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000581 : SlowPathCodeARM64(instruction), out_(out), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000582 DCHECK(kEmitCompilerReadBarrier);
583 }
584
585 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
588 LocationSummary* locations = instruction_->GetLocations();
589 Primitive::Type type = Primitive::kPrimNot;
590 DCHECK(locations->CanCall());
591 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
592 DCHECK(instruction_->IsInstanceFieldGet() ||
593 instruction_->IsStaticFieldGet() ||
594 instruction_->IsArrayGet() ||
595 instruction_->IsLoadClass() ||
596 instruction_->IsLoadString() ||
597 instruction_->IsInstanceOf() ||
598 instruction_->IsCheckCast())
599 << "Unexpected instruction in read barrier marking slow path: "
600 << instruction_->DebugName();
601
602 __ Bind(GetEntryLabel());
603 SaveLiveRegisters(codegen, locations);
604
605 InvokeRuntimeCallingConvention calling_convention;
606 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
607 arm64_codegen->MoveLocation(LocationFrom(calling_convention.GetRegisterAt(0)), obj_, type);
608 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
609 instruction_,
610 instruction_->GetDexPc(),
611 this);
612 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
613 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
614
615 RestoreLiveRegisters(codegen, locations);
616 __ B(GetExitLabel());
617 }
618
619 private:
Roland Levillain44015862016-01-22 11:47:17 +0000620 const Location out_;
621 const Location obj_;
622
623 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
624};
625
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000626// Slow path generating a read barrier for a heap reference.
627class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
628 public:
629 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
630 Location out,
631 Location ref,
632 Location obj,
633 uint32_t offset,
634 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000635 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000636 out_(out),
637 ref_(ref),
638 obj_(obj),
639 offset_(offset),
640 index_(index) {
641 DCHECK(kEmitCompilerReadBarrier);
642 // If `obj` is equal to `out` or `ref`, it means the initial object
643 // has been overwritten by (or after) the heap object reference load
644 // to be instrumented, e.g.:
645 //
646 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000647 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000648 //
649 // In that case, we have lost the information about the original
650 // object, and the emitted read barrier cannot work properly.
651 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
652 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
653 }
654
655 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
656 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
657 LocationSummary* locations = instruction_->GetLocations();
658 Primitive::Type type = Primitive::kPrimNot;
659 DCHECK(locations->CanCall());
660 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
661 DCHECK(!instruction_->IsInvoke() ||
662 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain44015862016-01-22 11:47:17 +0000663 instruction_->GetLocations()->Intrinsified()))
664 << "Unexpected instruction in read barrier for heap reference slow path: "
665 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000666 // The read barrier instrumentation does not support the
667 // HArm64IntermediateAddress instruction yet.
668 DCHECK(!(instruction_->IsArrayGet() &&
669 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000670
671 __ Bind(GetEntryLabel());
672
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000673 SaveLiveRegisters(codegen, locations);
674
675 // We may have to change the index's value, but as `index_` is a
676 // constant member (like other "inputs" of this slow path),
677 // introduce a copy of it, `index`.
678 Location index = index_;
679 if (index_.IsValid()) {
680 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
681 if (instruction_->IsArrayGet()) {
682 // Compute the actual memory offset and store it in `index`.
683 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
684 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
685 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
686 // We are about to change the value of `index_reg` (see the
687 // calls to vixl::MacroAssembler::Lsl and
688 // vixl::MacroAssembler::Mov below), but it has
689 // not been saved by the previous call to
690 // art::SlowPathCode::SaveLiveRegisters, as it is a
691 // callee-save register --
692 // art::SlowPathCode::SaveLiveRegisters does not consider
693 // callee-save registers, as it has been designed with the
694 // assumption that callee-save registers are supposed to be
695 // handled by the called function. So, as a callee-save
696 // register, `index_reg` _would_ eventually be saved onto
697 // the stack, but it would be too late: we would have
698 // changed its value earlier. Therefore, we manually save
699 // it here into another freely available register,
700 // `free_reg`, chosen of course among the caller-save
701 // registers (as a callee-save `free_reg` register would
702 // exhibit the same problem).
703 //
704 // Note we could have requested a temporary register from
705 // the register allocator instead; but we prefer not to, as
706 // this is a slow path, and we know we can find a
707 // caller-save register that is available.
708 Register free_reg = FindAvailableCallerSaveRegister(codegen);
709 __ Mov(free_reg.W(), index_reg);
710 index_reg = free_reg;
711 index = LocationFrom(index_reg);
712 } else {
713 // The initial register stored in `index_` has already been
714 // saved in the call to art::SlowPathCode::SaveLiveRegisters
715 // (as it is not a callee-save register), so we can freely
716 // use it.
717 }
718 // Shifting the index value contained in `index_reg` by the scale
719 // factor (2) cannot overflow in practice, as the runtime is
720 // unable to allocate object arrays with a size larger than
721 // 2^26 - 1 (that is, 2^28 - 4 bytes).
722 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
723 static_assert(
724 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
725 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
726 __ Add(index_reg, index_reg, Operand(offset_));
727 } else {
728 DCHECK(instruction_->IsInvoke());
729 DCHECK(instruction_->GetLocations()->Intrinsified());
730 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
731 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
732 << instruction_->AsInvoke()->GetIntrinsic();
733 DCHECK_EQ(offset_, 0U);
734 DCHECK(index_.IsRegisterPair());
735 // UnsafeGet's offset location is a register pair, the low
736 // part contains the correct offset.
737 index = index_.ToLow();
738 }
739 }
740
741 // We're moving two or three locations to locations that could
742 // overlap, so we need a parallel move resolver.
743 InvokeRuntimeCallingConvention calling_convention;
744 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
745 parallel_move.AddMove(ref_,
746 LocationFrom(calling_convention.GetRegisterAt(0)),
747 type,
748 nullptr);
749 parallel_move.AddMove(obj_,
750 LocationFrom(calling_convention.GetRegisterAt(1)),
751 type,
752 nullptr);
753 if (index.IsValid()) {
754 parallel_move.AddMove(index,
755 LocationFrom(calling_convention.GetRegisterAt(2)),
756 Primitive::kPrimInt,
757 nullptr);
758 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
759 } else {
760 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
761 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
762 }
763 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
764 instruction_,
765 instruction_->GetDexPc(),
766 this);
767 CheckEntrypointTypes<
768 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
769 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
770
771 RestoreLiveRegisters(codegen, locations);
772
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000773 __ B(GetExitLabel());
774 }
775
776 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
777
778 private:
779 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
780 size_t ref = static_cast<int>(XRegisterFrom(ref_).code());
781 size_t obj = static_cast<int>(XRegisterFrom(obj_).code());
782 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
783 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
784 return Register(VIXLRegCodeFromART(i), kXRegSize);
785 }
786 }
787 // We shall never fail to find a free caller-save register, as
788 // there are more than two core caller-save registers on ARM64
789 // (meaning it is possible to find one which is different from
790 // `ref` and `obj`).
791 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
792 LOG(FATAL) << "Could not find a free register";
793 UNREACHABLE();
794 }
795
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000796 const Location out_;
797 const Location ref_;
798 const Location obj_;
799 const uint32_t offset_;
800 // An additional location containing an index to an array.
801 // Only used for HArrayGet and the UnsafeGetObject &
802 // UnsafeGetObjectVolatile intrinsics.
803 const Location index_;
804
805 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
806};
807
808// Slow path generating a read barrier for a GC root.
809class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
810 public:
811 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000812 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000813 DCHECK(kEmitCompilerReadBarrier);
814 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000815
816 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
817 LocationSummary* locations = instruction_->GetLocations();
818 Primitive::Type type = Primitive::kPrimNot;
819 DCHECK(locations->CanCall());
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000821 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
822 << "Unexpected instruction in read barrier for GC root slow path: "
823 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000824
825 __ Bind(GetEntryLabel());
826 SaveLiveRegisters(codegen, locations);
827
828 InvokeRuntimeCallingConvention calling_convention;
829 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
830 // The argument of the ReadBarrierForRootSlow is not a managed
831 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
832 // thus we need a 64-bit move here, and we cannot use
833 //
834 // arm64_codegen->MoveLocation(
835 // LocationFrom(calling_convention.GetRegisterAt(0)),
836 // root_,
837 // type);
838 //
839 // which would emit a 32-bit move, as `type` is a (32-bit wide)
840 // reference type (`Primitive::kPrimNot`).
841 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
842 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
843 instruction_,
844 instruction_->GetDexPc(),
845 this);
846 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
847 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
848
849 RestoreLiveRegisters(codegen, locations);
850 __ B(GetExitLabel());
851 }
852
853 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
854
855 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000856 const Location out_;
857 const Location root_;
858
859 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
860};
861
Alexandre Rames5319def2014-10-23 10:03:10 +0100862#undef __
863
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100864Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100865 Location next_location;
866 if (type == Primitive::kPrimVoid) {
867 LOG(FATAL) << "Unreachable type " << type;
868 }
869
Alexandre Rames542361f2015-01-29 16:57:31 +0000870 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100871 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
872 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000873 } else if (!Primitive::IsFloatingPointType(type) &&
874 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000875 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
876 } else {
877 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000878 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
879 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100880 }
881
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000882 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000883 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100884 return next_location;
885}
886
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100887Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100888 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100889}
890
Serban Constantinescu579885a2015-02-22 20:51:33 +0000891CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
892 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100893 const CompilerOptions& compiler_options,
894 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100895 : CodeGenerator(graph,
896 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000897 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000898 kNumberOfAllocatableRegisterPairs,
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000899 callee_saved_core_registers.list(),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000900 callee_saved_fp_registers.list(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100901 compiler_options,
902 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100903 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800904 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100905 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000906 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000907 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100908 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000909 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000910 uint32_literals_(std::less<uint32_t>(),
911 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100912 uint64_literals_(std::less<uint64_t>(),
913 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
914 method_patches_(MethodReferenceComparator(),
915 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
916 call_patches_(MethodReferenceComparator(),
917 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
918 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000919 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
920 boot_image_string_patches_(StringReferenceValueComparator(),
921 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
922 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
923 boot_image_address_patches_(std::less<uint32_t>(),
924 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000925 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000926 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000927}
Alexandre Rames5319def2014-10-23 10:03:10 +0100928
Alexandre Rames67555f72014-11-18 10:55:16 +0000929#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100930
Zheng Xu3927c8b2015-11-18 17:46:25 +0800931void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100932 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800933 jump_table->EmitTable(this);
934 }
935}
936
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000937void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800938 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000939 // Ensure we emit the literal pool.
940 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000941
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000942 CodeGenerator::Finalize(allocator);
943}
944
Zheng Xuad4450e2015-04-17 18:48:56 +0800945void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
946 // Note: There are 6 kinds of moves:
947 // 1. constant -> GPR/FPR (non-cycle)
948 // 2. constant -> stack (non-cycle)
949 // 3. GPR/FPR -> GPR/FPR
950 // 4. GPR/FPR -> stack
951 // 5. stack -> GPR/FPR
952 // 6. stack -> stack (non-cycle)
953 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
954 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
955 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
956 // dependency.
957 vixl_temps_.Open(GetVIXLAssembler());
958}
959
960void ParallelMoveResolverARM64::FinishEmitNativeCode() {
961 vixl_temps_.Close();
962}
963
964Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
965 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
966 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
967 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
968 Location scratch = GetScratchLocation(kind);
969 if (!scratch.Equals(Location::NoLocation())) {
970 return scratch;
971 }
972 // Allocate from VIXL temp registers.
973 if (kind == Location::kRegister) {
974 scratch = LocationFrom(vixl_temps_.AcquireX());
975 } else {
976 DCHECK(kind == Location::kFpuRegister);
977 scratch = LocationFrom(vixl_temps_.AcquireD());
978 }
979 AddScratchLocation(scratch);
980 return scratch;
981}
982
983void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
984 if (loc.IsRegister()) {
985 vixl_temps_.Release(XRegisterFrom(loc));
986 } else {
987 DCHECK(loc.IsFpuRegister());
988 vixl_temps_.Release(DRegisterFrom(loc));
989 }
990 RemoveScratchLocation(loc);
991}
992
Alexandre Rames3e69f162014-12-10 10:36:50 +0000993void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100994 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +0100995 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000996}
997
Alexandre Rames5319def2014-10-23 10:03:10 +0100998void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +0100999 MacroAssembler* masm = GetVIXLAssembler();
1000 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001001 __ Bind(&frame_entry_label_);
1002
Serban Constantinescu02164b32014-11-13 14:05:07 +00001003 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1004 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001005 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001006 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001007 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001008 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001009 __ Ldr(wzr, MemOperand(temp, 0));
1010 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001011 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001012
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001013 if (!HasEmptyFrame()) {
1014 int frame_size = GetFrameSize();
1015 // Stack layout:
1016 // sp[frame_size - 8] : lr.
1017 // ... : other preserved core registers.
1018 // ... : other preserved fp registers.
1019 // ... : reserved frame space.
1020 // sp[0] : current method.
1021 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001022 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001023 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1024 frame_size - GetCoreSpillSize());
1025 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1026 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001027 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001028}
1029
1030void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001031 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001032 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001033 if (!HasEmptyFrame()) {
1034 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001035 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1036 frame_size - FrameEntrySpillSize());
1037 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1038 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001039 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001040 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001041 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001042 __ Ret();
1043 GetAssembler()->cfi().RestoreState();
1044 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001045}
1046
Zheng Xuda403092015-04-24 17:35:39 +08001047vixl::CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
1048 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
1049 return vixl::CPURegList(vixl::CPURegister::kRegister, vixl::kXRegSize,
1050 core_spill_mask_);
1051}
1052
1053vixl::CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
1054 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1055 GetNumberOfFloatingPointRegisters()));
1056 return vixl::CPURegList(vixl::CPURegister::kFPRegister, vixl::kDRegSize,
1057 fpu_spill_mask_);
1058}
1059
Alexandre Rames5319def2014-10-23 10:03:10 +01001060void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1061 __ Bind(GetLabelOf(block));
1062}
1063
Calin Juravle175dc732015-08-25 15:42:32 +01001064void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1065 DCHECK(location.IsRegister());
1066 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1067}
1068
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1070 if (location.IsRegister()) {
1071 locations->AddTemp(location);
1072 } else {
1073 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1074 }
1075}
1076
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001077void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001078 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001079 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001080 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Alexandre Rames5319def2014-10-23 10:03:10 +01001081 vixl::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001082 if (value_can_be_null) {
1083 __ Cbz(value, &done);
1084 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001085 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1086 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001087 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001088 if (value_can_be_null) {
1089 __ Bind(&done);
1090 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001091}
1092
David Brazdil58282f42016-01-14 12:45:10 +00001093void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001094 // Blocked core registers:
1095 // lr : Runtime reserved.
1096 // tr : Runtime reserved.
1097 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1098 // ip1 : VIXL core temp.
1099 // ip0 : VIXL core temp.
1100 //
1101 // Blocked fp registers:
1102 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001103 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1104 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001105 while (!reserved_core_registers.IsEmpty()) {
1106 blocked_core_registers_[reserved_core_registers.PopLowestIndex().code()] = true;
1107 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001108
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001109 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001110 while (!reserved_fp_registers.IsEmpty()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001111 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().code()] = true;
1112 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001113
David Brazdil58282f42016-01-14 12:45:10 +00001114 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001115 // Stubs do not save callee-save floating point registers. If the graph
1116 // is debuggable, we need to deal with these registers differently. For
1117 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001118 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1119 while (!reserved_fp_registers_debuggable.IsEmpty()) {
1120 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().code()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001121 }
1122 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001123}
1124
Alexandre Rames3e69f162014-12-10 10:36:50 +00001125size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1126 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1127 __ Str(reg, MemOperand(sp, stack_index));
1128 return kArm64WordSize;
1129}
1130
1131size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1132 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1133 __ Ldr(reg, MemOperand(sp, stack_index));
1134 return kArm64WordSize;
1135}
1136
1137size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1138 FPRegister reg = FPRegister(reg_id, kDRegSize);
1139 __ Str(reg, MemOperand(sp, stack_index));
1140 return kArm64WordSize;
1141}
1142
1143size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1144 FPRegister reg = FPRegister(reg_id, kDRegSize);
1145 __ Ldr(reg, MemOperand(sp, stack_index));
1146 return kArm64WordSize;
1147}
1148
Alexandre Rames5319def2014-10-23 10:03:10 +01001149void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001150 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001151}
1152
1153void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001154 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001155}
1156
Alexandre Rames67555f72014-11-18 10:55:16 +00001157void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001158 if (constant->IsIntConstant()) {
1159 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1160 } else if (constant->IsLongConstant()) {
1161 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1162 } else if (constant->IsNullConstant()) {
1163 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001164 } else if (constant->IsFloatConstant()) {
1165 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1166 } else {
1167 DCHECK(constant->IsDoubleConstant());
1168 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1169 }
1170}
1171
Alexandre Rames3e69f162014-12-10 10:36:50 +00001172
1173static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1174 DCHECK(constant.IsConstant());
1175 HConstant* cst = constant.GetConstant();
1176 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001177 // Null is mapped to a core W register, which we associate with kPrimInt.
1178 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001179 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1180 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1181 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1182}
1183
Calin Juravlee460d1d2015-09-29 04:52:17 +01001184void CodeGeneratorARM64::MoveLocation(Location destination,
1185 Location source,
1186 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001187 if (source.Equals(destination)) {
1188 return;
1189 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001190
1191 // A valid move can always be inferred from the destination and source
1192 // locations. When moving from and to a register, the argument type can be
1193 // used to generate 32bit instead of 64bit moves. In debug mode we also
1194 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001195 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001196
1197 if (destination.IsRegister() || destination.IsFpuRegister()) {
1198 if (unspecified_type) {
1199 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1200 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001201 (src_cst != nullptr && (src_cst->IsIntConstant()
1202 || src_cst->IsFloatConstant()
1203 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001204 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001205 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001206 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001207 // If the source is a double stack slot or a 64bit constant, a 64bit
1208 // type is appropriate. Else the source is a register, and since the
1209 // type has not been specified, we chose a 64bit type to force a 64bit
1210 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001211 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001214 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1215 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1216 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001217 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1218 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1219 __ Ldr(dst, StackOperandFrom(source));
1220 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001221 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001222 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001223 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001225 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001226 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001227 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1229 ? Primitive::kPrimLong
1230 : Primitive::kPrimInt;
1231 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1232 }
1233 } else {
1234 DCHECK(source.IsFpuRegister());
1235 if (destination.IsRegister()) {
1236 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1237 ? Primitive::kPrimDouble
1238 : Primitive::kPrimFloat;
1239 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1240 } else {
1241 DCHECK(destination.IsFpuRegister());
1242 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001243 }
1244 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 } else { // The destination is not a register. It must be a stack slot.
1246 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1247 if (source.IsRegister() || source.IsFpuRegister()) {
1248 if (unspecified_type) {
1249 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001250 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001252 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001253 }
1254 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1256 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1257 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1260 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001261 UseScratchRegisterScope temps(GetVIXLAssembler());
1262 HConstant* src_cst = source.GetConstant();
1263 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001264 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001265 temp = temps.AcquireW();
1266 } else if (src_cst->IsLongConstant()) {
1267 temp = temps.AcquireX();
1268 } else if (src_cst->IsFloatConstant()) {
1269 temp = temps.AcquireS();
1270 } else {
1271 DCHECK(src_cst->IsDoubleConstant());
1272 temp = temps.AcquireD();
1273 }
1274 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001275 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001276 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001277 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001278 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001279 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001280 // There is generally less pressure on FP registers.
1281 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001282 __ Ldr(temp, StackOperandFrom(source));
1283 __ Str(temp, StackOperandFrom(destination));
1284 }
1285 }
1286}
1287
1288void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001289 CPURegister dst,
1290 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001291 switch (type) {
1292 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001293 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001294 break;
1295 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001296 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001297 break;
1298 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001299 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001300 break;
1301 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001303 break;
1304 case Primitive::kPrimInt:
1305 case Primitive::kPrimNot:
1306 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001307 case Primitive::kPrimFloat:
1308 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001309 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001310 __ Ldr(dst, src);
1311 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001312 case Primitive::kPrimVoid:
1313 LOG(FATAL) << "Unreachable type " << type;
1314 }
1315}
1316
Calin Juravle77520bc2015-01-12 18:45:46 +00001317void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001318 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001319 const MemOperand& src,
1320 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001321 MacroAssembler* masm = GetVIXLAssembler();
1322 BlockPoolsScope block_pools(masm);
1323 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001324 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001325 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001326
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001327 DCHECK(!src.IsPreIndex());
1328 DCHECK(!src.IsPostIndex());
1329
1330 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001331 __ Add(temp_base, src.base(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001332 MemOperand base = MemOperand(temp_base);
1333 switch (type) {
1334 case Primitive::kPrimBoolean:
1335 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001336 if (needs_null_check) {
1337 MaybeRecordImplicitNullCheck(instruction);
1338 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001339 break;
1340 case Primitive::kPrimByte:
1341 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001342 if (needs_null_check) {
1343 MaybeRecordImplicitNullCheck(instruction);
1344 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001345 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1346 break;
1347 case Primitive::kPrimChar:
1348 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001349 if (needs_null_check) {
1350 MaybeRecordImplicitNullCheck(instruction);
1351 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352 break;
1353 case Primitive::kPrimShort:
1354 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001355 if (needs_null_check) {
1356 MaybeRecordImplicitNullCheck(instruction);
1357 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1359 break;
1360 case Primitive::kPrimInt:
1361 case Primitive::kPrimNot:
1362 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001363 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001364 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001365 if (needs_null_check) {
1366 MaybeRecordImplicitNullCheck(instruction);
1367 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 break;
1369 case Primitive::kPrimFloat:
1370 case Primitive::kPrimDouble: {
1371 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001372 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001373
1374 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1375 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001376 if (needs_null_check) {
1377 MaybeRecordImplicitNullCheck(instruction);
1378 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001379 __ Fmov(FPRegister(dst), temp);
1380 break;
1381 }
1382 case Primitive::kPrimVoid:
1383 LOG(FATAL) << "Unreachable type " << type;
1384 }
1385}
1386
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001387void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001388 CPURegister src,
1389 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001390 switch (type) {
1391 case Primitive::kPrimBoolean:
1392 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001393 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001394 break;
1395 case Primitive::kPrimChar:
1396 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001397 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001398 break;
1399 case Primitive::kPrimInt:
1400 case Primitive::kPrimNot:
1401 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001402 case Primitive::kPrimFloat:
1403 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001404 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001405 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001406 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001407 case Primitive::kPrimVoid:
1408 LOG(FATAL) << "Unreachable type " << type;
1409 }
1410}
1411
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001412void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1413 CPURegister src,
1414 const MemOperand& dst) {
1415 UseScratchRegisterScope temps(GetVIXLAssembler());
1416 Register temp_base = temps.AcquireX();
1417
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001418 DCHECK(!dst.IsPreIndex());
1419 DCHECK(!dst.IsPostIndex());
1420
1421 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001422 Operand op = OperandFromMemOperand(dst);
1423 __ Add(temp_base, dst.base(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001424 MemOperand base = MemOperand(temp_base);
1425 switch (type) {
1426 case Primitive::kPrimBoolean:
1427 case Primitive::kPrimByte:
1428 __ Stlrb(Register(src), base);
1429 break;
1430 case Primitive::kPrimChar:
1431 case Primitive::kPrimShort:
1432 __ Stlrh(Register(src), base);
1433 break;
1434 case Primitive::kPrimInt:
1435 case Primitive::kPrimNot:
1436 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001437 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001438 __ Stlr(Register(src), base);
1439 break;
1440 case Primitive::kPrimFloat:
1441 case Primitive::kPrimDouble: {
1442 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001443 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001444
1445 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1446 __ Fmov(temp, FPRegister(src));
1447 __ Stlr(temp, base);
1448 break;
1449 }
1450 case Primitive::kPrimVoid:
1451 LOG(FATAL) << "Unreachable type " << type;
1452 }
1453}
1454
Calin Juravle175dc732015-08-25 15:42:32 +01001455void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1456 HInstruction* instruction,
1457 uint32_t dex_pc,
1458 SlowPathCode* slow_path) {
1459 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1460 instruction,
1461 dex_pc,
1462 slow_path);
1463}
1464
Alexandre Rames67555f72014-11-18 10:55:16 +00001465void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1466 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001467 uint32_t dex_pc,
1468 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001469 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001470 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001471 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1472 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001473 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001474}
1475
1476void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
1477 vixl::Register class_reg) {
1478 UseScratchRegisterScope temps(GetVIXLAssembler());
1479 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001480 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1481
Serban Constantinescu02164b32014-11-13 14:05:07 +00001482 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001483 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1484 __ Add(temp, class_reg, status_offset);
1485 __ Ldar(temp, HeapOperand(temp));
1486 __ Cmp(temp, mirror::Class::kStatusInitialized);
1487 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001488 __ Bind(slow_path->GetExitLabel());
1489}
Alexandre Rames5319def2014-10-23 10:03:10 +01001490
Roland Levillain44015862016-01-22 11:47:17 +00001491void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001492 BarrierType type = BarrierAll;
1493
1494 switch (kind) {
1495 case MemBarrierKind::kAnyAny:
1496 case MemBarrierKind::kAnyStore: {
1497 type = BarrierAll;
1498 break;
1499 }
1500 case MemBarrierKind::kLoadAny: {
1501 type = BarrierReads;
1502 break;
1503 }
1504 case MemBarrierKind::kStoreStore: {
1505 type = BarrierWrites;
1506 break;
1507 }
1508 default:
1509 LOG(FATAL) << "Unexpected memory barrier " << kind;
1510 }
1511 __ Dmb(InnerShareable, type);
1512}
1513
Serban Constantinescu02164b32014-11-13 14:05:07 +00001514void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1515 HBasicBlock* successor) {
1516 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001517 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1518 if (slow_path == nullptr) {
1519 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1520 instruction->SetSlowPath(slow_path);
1521 codegen_->AddSlowPath(slow_path);
1522 if (successor != nullptr) {
1523 DCHECK(successor->IsLoopHeader());
1524 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1525 }
1526 } else {
1527 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1528 }
1529
Serban Constantinescu02164b32014-11-13 14:05:07 +00001530 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1531 Register temp = temps.AcquireW();
1532
1533 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1534 if (successor == nullptr) {
1535 __ Cbnz(temp, slow_path->GetEntryLabel());
1536 __ Bind(slow_path->GetReturnLabel());
1537 } else {
1538 __ Cbz(temp, codegen_->GetLabelOf(successor));
1539 __ B(slow_path->GetEntryLabel());
1540 // slow_path will return to GetLabelOf(successor).
1541 }
1542}
1543
Alexandre Rames5319def2014-10-23 10:03:10 +01001544InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1545 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001546 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001547 assembler_(codegen->GetAssembler()),
1548 codegen_(codegen) {}
1549
1550#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001551 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001552
1553#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1554
1555enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001556 // Using a base helps identify when we hit such breakpoints.
1557 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001558#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1559 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1560#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1561};
1562
1563#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001565 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1566 } \
1567 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1569 locations->SetOut(Location::Any()); \
1570 }
1571 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1572#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1573
1574#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001575#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001576
Alexandre Rames67555f72014-11-18 10:55:16 +00001577void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001578 DCHECK_EQ(instr->InputCount(), 2U);
1579 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1580 Primitive::Type type = instr->GetResultType();
1581 switch (type) {
1582 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001583 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001584 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001585 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001587 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001588
1589 case Primitive::kPrimFloat:
1590 case Primitive::kPrimDouble:
1591 locations->SetInAt(0, Location::RequiresFpuRegister());
1592 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001593 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001594 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001595
Alexandre Rames5319def2014-10-23 10:03:10 +01001596 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001597 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001598 }
1599}
1600
Alexandre Rames09a99962015-04-15 11:47:56 +01001601void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001602 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1603
1604 bool object_field_get_with_read_barrier =
1605 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001606 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001607 new (GetGraph()->GetArena()) LocationSummary(instruction,
1608 object_field_get_with_read_barrier ?
1609 LocationSummary::kCallOnSlowPath :
1610 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001611 locations->SetInAt(0, Location::RequiresRegister());
1612 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1613 locations->SetOut(Location::RequiresFpuRegister());
1614 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001615 // The output overlaps for an object field get when read barriers
1616 // are enabled: we do not want the load to overwrite the object's
1617 // location, as we need it to emit the read barrier.
1618 locations->SetOut(
1619 Location::RequiresRegister(),
1620 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001621 }
1622}
1623
1624void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1625 const FieldInfo& field_info) {
1626 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001627 LocationSummary* locations = instruction->GetLocations();
1628 Location base_loc = locations->InAt(0);
1629 Location out = locations->Out();
1630 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001631 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001632 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001633 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001634
Roland Levillain44015862016-01-22 11:47:17 +00001635 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1636 // Object FieldGet with Baker's read barrier case.
1637 MacroAssembler* masm = GetVIXLAssembler();
1638 UseScratchRegisterScope temps(masm);
1639 // /* HeapReference<Object> */ out = *(base + offset)
1640 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1641 Register temp = temps.AcquireW();
1642 // Note that potential implicit null checks are handled in this
1643 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1644 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1645 instruction,
1646 out,
1647 base,
1648 offset,
1649 temp,
1650 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001651 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001652 } else {
1653 // General case.
1654 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001655 // Note that a potential implicit null check is handled in this
1656 // CodeGeneratorARM64::LoadAcquire call.
1657 // NB: LoadAcquire will record the pc info if needed.
1658 codegen_->LoadAcquire(
1659 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001660 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001661 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001662 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001663 }
Roland Levillain44015862016-01-22 11:47:17 +00001664 if (field_type == Primitive::kPrimNot) {
1665 // If read barriers are enabled, emit read barriers other than
1666 // Baker's using a slow path (and also unpoison the loaded
1667 // reference, if heap poisoning is enabled).
1668 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1669 }
Roland Levillain4d027112015-07-01 15:41:14 +01001670 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001671}
1672
1673void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1674 LocationSummary* locations =
1675 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1678 locations->SetInAt(1, Location::RequiresFpuRegister());
1679 } else {
1680 locations->SetInAt(1, Location::RequiresRegister());
1681 }
1682}
1683
1684void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001685 const FieldInfo& field_info,
1686 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001687 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001688 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001689
1690 Register obj = InputRegisterAt(instruction, 0);
1691 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001692 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001693 Offset offset = field_info.GetFieldOffset();
1694 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001695
Roland Levillain4d027112015-07-01 15:41:14 +01001696 {
1697 // We use a block to end the scratch scope before the write barrier, thus
1698 // freeing the temporary registers so they can be used in `MarkGCCard`.
1699 UseScratchRegisterScope temps(GetVIXLAssembler());
1700
1701 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1702 DCHECK(value.IsW());
1703 Register temp = temps.AcquireW();
1704 __ Mov(temp, value.W());
1705 GetAssembler()->PoisonHeapReference(temp.W());
1706 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001707 }
Roland Levillain4d027112015-07-01 15:41:14 +01001708
1709 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001710 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001712 } else {
1713 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1714 codegen_->MaybeRecordImplicitNullCheck(instruction);
1715 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001716 }
1717
1718 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001719 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001720 }
1721}
1722
Alexandre Rames67555f72014-11-18 10:55:16 +00001723void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001724 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001725
1726 switch (type) {
1727 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001728 case Primitive::kPrimLong: {
1729 Register dst = OutputRegister(instr);
1730 Register lhs = InputRegisterAt(instr, 0);
1731 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001732 if (instr->IsAdd()) {
1733 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001734 } else if (instr->IsAnd()) {
1735 __ And(dst, lhs, rhs);
1736 } else if (instr->IsOr()) {
1737 __ Orr(dst, lhs, rhs);
1738 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001739 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001740 } else if (instr->IsRor()) {
1741 if (rhs.IsImmediate()) {
1742 uint32_t shift = rhs.immediate() & (lhs.SizeInBits() - 1);
1743 __ Ror(dst, lhs, shift);
1744 } else {
1745 // Ensure shift distance is in the same size register as the result. If
1746 // we are rotating a long and the shift comes in a w register originally,
1747 // we don't need to sxtw for use as an x since the shift distances are
1748 // all & reg_bits - 1.
1749 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1750 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001751 } else {
1752 DCHECK(instr->IsXor());
1753 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001754 }
1755 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001756 }
1757 case Primitive::kPrimFloat:
1758 case Primitive::kPrimDouble: {
1759 FPRegister dst = OutputFPRegister(instr);
1760 FPRegister lhs = InputFPRegisterAt(instr, 0);
1761 FPRegister rhs = InputFPRegisterAt(instr, 1);
1762 if (instr->IsAdd()) {
1763 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001764 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001765 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001766 } else {
1767 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001768 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001769 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001770 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001772 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 }
1774}
1775
Serban Constantinescu02164b32014-11-13 14:05:07 +00001776void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1777 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1778
1779 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1780 Primitive::Type type = instr->GetResultType();
1781 switch (type) {
1782 case Primitive::kPrimInt:
1783 case Primitive::kPrimLong: {
1784 locations->SetInAt(0, Location::RequiresRegister());
1785 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1786 locations->SetOut(Location::RequiresRegister());
1787 break;
1788 }
1789 default:
1790 LOG(FATAL) << "Unexpected shift type " << type;
1791 }
1792}
1793
1794void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1795 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1796
1797 Primitive::Type type = instr->GetType();
1798 switch (type) {
1799 case Primitive::kPrimInt:
1800 case Primitive::kPrimLong: {
1801 Register dst = OutputRegister(instr);
1802 Register lhs = InputRegisterAt(instr, 0);
1803 Operand rhs = InputOperandAt(instr, 1);
1804 if (rhs.IsImmediate()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00001805 uint32_t shift_value = rhs.immediate() &
1806 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001807 if (instr->IsShl()) {
1808 __ Lsl(dst, lhs, shift_value);
1809 } else if (instr->IsShr()) {
1810 __ Asr(dst, lhs, shift_value);
1811 } else {
1812 __ Lsr(dst, lhs, shift_value);
1813 }
1814 } else {
1815 Register rhs_reg = dst.IsX() ? rhs.reg().X() : rhs.reg().W();
1816
1817 if (instr->IsShl()) {
1818 __ Lsl(dst, lhs, rhs_reg);
1819 } else if (instr->IsShr()) {
1820 __ Asr(dst, lhs, rhs_reg);
1821 } else {
1822 __ Lsr(dst, lhs, rhs_reg);
1823 }
1824 }
1825 break;
1826 }
1827 default:
1828 LOG(FATAL) << "Unexpected shift operation type " << type;
1829 }
1830}
1831
Alexandre Rames5319def2014-10-23 10:03:10 +01001832void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001833 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001834}
1835
1836void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001837 HandleBinaryOp(instruction);
1838}
1839
1840void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1841 HandleBinaryOp(instruction);
1842}
1843
1844void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1845 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001846}
1847
Artem Serov7fc63502016-02-09 17:15:29 +00001848void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001849 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1851 locations->SetInAt(0, Location::RequiresRegister());
1852 // There is no immediate variant of negated bitwise instructions in AArch64.
1853 locations->SetInAt(1, Location::RequiresRegister());
1854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1855}
1856
Artem Serov7fc63502016-02-09 17:15:29 +00001857void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001858 Register dst = OutputRegister(instr);
1859 Register lhs = InputRegisterAt(instr, 0);
1860 Register rhs = InputRegisterAt(instr, 1);
1861
1862 switch (instr->GetOpKind()) {
1863 case HInstruction::kAnd:
1864 __ Bic(dst, lhs, rhs);
1865 break;
1866 case HInstruction::kOr:
1867 __ Orn(dst, lhs, rhs);
1868 break;
1869 case HInstruction::kXor:
1870 __ Eon(dst, lhs, rhs);
1871 break;
1872 default:
1873 LOG(FATAL) << "Unreachable";
1874 }
1875}
1876
Alexandre Rames8626b742015-11-25 16:28:08 +00001877void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1878 HArm64DataProcWithShifterOp* instruction) {
1879 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1880 instruction->GetType() == Primitive::kPrimLong);
1881 LocationSummary* locations =
1882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1883 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1884 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1885 } else {
1886 locations->SetInAt(0, Location::RequiresRegister());
1887 }
1888 locations->SetInAt(1, Location::RequiresRegister());
1889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1890}
1891
1892void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1893 HArm64DataProcWithShifterOp* instruction) {
1894 Primitive::Type type = instruction->GetType();
1895 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1896 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1897 Register out = OutputRegister(instruction);
1898 Register left;
1899 if (kind != HInstruction::kNeg) {
1900 left = InputRegisterAt(instruction, 0);
1901 }
1902 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1903 // shifter operand operation, the IR generating `right_reg` (input to the type
1904 // conversion) can have a different type from the current instruction's type,
1905 // so we manually indicate the type.
1906 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001907 int64_t shift_amount = instruction->GetShiftAmount() &
1908 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001909
1910 Operand right_operand(0);
1911
1912 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1913 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1914 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1915 } else {
1916 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1917 }
1918
1919 // Logical binary operations do not support extension operations in the
1920 // operand. Note that VIXL would still manage if it was passed by generating
1921 // the extension as a separate instruction.
1922 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1923 DCHECK(!right_operand.IsExtendedRegister() ||
1924 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1925 kind != HInstruction::kNeg));
1926 switch (kind) {
1927 case HInstruction::kAdd:
1928 __ Add(out, left, right_operand);
1929 break;
1930 case HInstruction::kAnd:
1931 __ And(out, left, right_operand);
1932 break;
1933 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001934 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001935 __ Neg(out, right_operand);
1936 break;
1937 case HInstruction::kOr:
1938 __ Orr(out, left, right_operand);
1939 break;
1940 case HInstruction::kSub:
1941 __ Sub(out, left, right_operand);
1942 break;
1943 case HInstruction::kXor:
1944 __ Eor(out, left, right_operand);
1945 break;
1946 default:
1947 LOG(FATAL) << "Unexpected operation kind: " << kind;
1948 UNREACHABLE();
1949 }
1950}
1951
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001952void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001953 // The read barrier instrumentation does not support the
1954 // HArm64IntermediateAddress instruction yet.
1955 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001956 LocationSummary* locations =
1957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1958 locations->SetInAt(0, Location::RequiresRegister());
1959 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1960 locations->SetOut(Location::RequiresRegister());
1961}
1962
1963void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1964 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001965 // The read barrier instrumentation does not support the
1966 // HArm64IntermediateAddress instruction yet.
1967 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001968 __ Add(OutputRegister(instruction),
1969 InputRegisterAt(instruction, 0),
1970 Operand(InputOperandAt(instruction, 1)));
1971}
1972
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001973void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001974 LocationSummary* locations =
1975 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001976 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
1977 if (instr->GetOpKind() == HInstruction::kSub &&
1978 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00001979 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001980 // Don't allocate register for Mneg instruction.
1981 } else {
1982 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
1983 Location::RequiresRegister());
1984 }
1985 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
1986 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00001987 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1988}
1989
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001990void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00001991 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001992 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
1993 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00001994
1995 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
1996 // This fixup should be carried out for all multiply-accumulate instructions:
1997 // madd, msub, smaddl, smsubl, umaddl and umsubl.
1998 if (instr->GetType() == Primitive::kPrimLong &&
1999 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2000 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
2001 vixl::Instruction* prev = masm->GetCursorAddress<vixl::Instruction*>() - vixl::kInstructionSize;
2002 if (prev->IsLoadOrStore()) {
2003 // Make sure we emit only exactly one nop.
2004 vixl::CodeBufferCheckScope scope(masm,
2005 vixl::kInstructionSize,
2006 vixl::CodeBufferCheckScope::kCheck,
2007 vixl::CodeBufferCheckScope::kExactSize);
2008 __ nop();
2009 }
2010 }
2011
2012 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002013 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002014 __ Madd(res, mul_left, mul_right, accumulator);
2015 } else {
2016 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002017 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002018 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002019 __ Mneg(res, mul_left, mul_right);
2020 } else {
2021 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2022 __ Msub(res, mul_left, mul_right, accumulator);
2023 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002024 }
2025}
2026
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002028 bool object_array_get_with_read_barrier =
2029 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002030 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002031 new (GetGraph()->GetArena()) LocationSummary(instruction,
2032 object_array_get_with_read_barrier ?
2033 LocationSummary::kCallOnSlowPath :
2034 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002035 locations->SetInAt(0, Location::RequiresRegister());
2036 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002037 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2038 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2039 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002040 // The output overlaps in the case of an object array get with
2041 // read barriers enabled: we do not want the move to overwrite the
2042 // array's location, as we need it to emit the read barrier.
2043 locations->SetOut(
2044 Location::RequiresRegister(),
2045 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002046 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002047}
2048
2049void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002050 Primitive::Type type = instruction->GetType();
2051 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002052 LocationSummary* locations = instruction->GetLocations();
2053 Location index = locations->InAt(1);
2054 uint32_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(type)).Uint32Value();
Roland Levillain44015862016-01-22 11:47:17 +00002055 Location out = locations->Out();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002056
Alexandre Ramesd921d642015-04-16 15:07:16 +01002057 MacroAssembler* masm = GetVIXLAssembler();
2058 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002059 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002060 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061
Roland Levillain44015862016-01-22 11:47:17 +00002062 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2063 // Object ArrayGet with Baker's read barrier case.
2064 Register temp = temps.AcquireW();
2065 // The read barrier instrumentation does not support the
2066 // HArm64IntermediateAddress instruction yet.
2067 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2068 // Note that a potential implicit null check is handled in the
2069 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2070 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2071 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002072 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002073 // General case.
2074 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002076 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2077 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002078 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002079 Register temp = temps.AcquireSameSizeAs(obj);
2080 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2081 // The read barrier instrumentation does not support the
2082 // HArm64IntermediateAddress instruction yet.
2083 DCHECK(!kEmitCompilerReadBarrier);
2084 // We do not need to compute the intermediate address from the array: the
2085 // input instruction has done it already. See the comment in
2086 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2087 if (kIsDebugBuild) {
2088 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2089 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2090 }
2091 temp = obj;
2092 } else {
2093 __ Add(temp, obj, offset);
2094 }
2095 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2096 }
2097
2098 codegen_->Load(type, OutputCPURegister(instruction), source);
2099 codegen_->MaybeRecordImplicitNullCheck(instruction);
2100
2101 if (type == Primitive::kPrimNot) {
2102 static_assert(
2103 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2104 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2105 Location obj_loc = locations->InAt(0);
2106 if (index.IsConstant()) {
2107 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2108 } else {
2109 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2110 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002111 }
Roland Levillain4d027112015-07-01 15:41:14 +01002112 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002113}
2114
Alexandre Rames5319def2014-10-23 10:03:10 +01002115void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2117 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002118 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002119}
2120
2121void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002122 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002123 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002124 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002125 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002126}
2127
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002128void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002129 Primitive::Type value_type = instruction->GetComponentType();
2130
2131 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2132 bool object_array_set_with_read_barrier =
2133 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2135 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002136 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2137 LocationSummary::kCallOnSlowPath :
2138 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002139 locations->SetInAt(0, Location::RequiresRegister());
2140 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002141 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002142 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002143 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002144 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002145 }
2146}
2147
2148void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2149 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002150 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002151 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002152 bool needs_write_barrier =
2153 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002154
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002155 Register array = InputRegisterAt(instruction, 0);
2156 CPURegister value = InputCPURegisterAt(instruction, 2);
2157 CPURegister source = value;
2158 Location index = locations->InAt(1);
2159 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2160 MemOperand destination = HeapOperand(array);
2161 MacroAssembler* masm = GetVIXLAssembler();
2162 BlockPoolsScope block_pools(masm);
2163
2164 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002165 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002166 if (index.IsConstant()) {
2167 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2168 destination = HeapOperand(array, offset);
2169 } else {
2170 UseScratchRegisterScope temps(masm);
2171 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002172 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002173 // The read barrier instrumentation does not support the
2174 // HArm64IntermediateAddress instruction yet.
2175 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002176 // We do not need to compute the intermediate address from the array: the
2177 // input instruction has done it already. See the comment in
2178 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2179 if (kIsDebugBuild) {
2180 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2181 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2182 }
2183 temp = array;
2184 } else {
2185 __ Add(temp, array, offset);
2186 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002187 destination = HeapOperand(temp,
2188 XRegisterFrom(index),
2189 LSL,
2190 Primitive::ComponentSizeShift(value_type));
2191 }
2192 codegen_->Store(value_type, value, destination);
2193 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002194 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002195 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002196 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002197 vixl::Label done;
2198 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002199 {
2200 // We use a block to end the scratch scope before the write barrier, thus
2201 // freeing the temporary registers so they can be used in `MarkGCCard`.
2202 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002203 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002204 if (index.IsConstant()) {
2205 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002206 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002207 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002208 destination = HeapOperand(temp,
2209 XRegisterFrom(index),
2210 LSL,
2211 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002212 }
2213
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002214 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2215 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2216 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2217
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002218 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002219 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2220 codegen_->AddSlowPath(slow_path);
2221 if (instruction->GetValueCanBeNull()) {
2222 vixl::Label non_zero;
2223 __ Cbnz(Register(value), &non_zero);
2224 if (!index.IsConstant()) {
2225 __ Add(temp, array, offset);
2226 }
2227 __ Str(wzr, destination);
2228 codegen_->MaybeRecordImplicitNullCheck(instruction);
2229 __ B(&done);
2230 __ Bind(&non_zero);
2231 }
2232
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002233 if (kEmitCompilerReadBarrier) {
2234 // When read barriers are enabled, the type checking
2235 // instrumentation requires two read barriers:
2236 //
2237 // __ Mov(temp2, temp);
2238 // // /* HeapReference<Class> */ temp = temp->component_type_
2239 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002240 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002241 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2242 //
2243 // // /* HeapReference<Class> */ temp2 = value->klass_
2244 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002245 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002246 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2247 //
2248 // __ Cmp(temp, temp2);
2249 //
2250 // However, the second read barrier may trash `temp`, as it
2251 // is a temporary register, and as such would not be saved
2252 // along with live registers before calling the runtime (nor
2253 // restored afterwards). So in this case, we bail out and
2254 // delegate the work to the array set slow path.
2255 //
2256 // TODO: Extend the register allocator to support a new
2257 // "(locally) live temp" location so as to avoid always
2258 // going into the slow path when read barriers are enabled.
2259 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002260 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002261 Register temp2 = temps.AcquireSameSizeAs(array);
2262 // /* HeapReference<Class> */ temp = array->klass_
2263 __ Ldr(temp, HeapOperand(array, class_offset));
2264 codegen_->MaybeRecordImplicitNullCheck(instruction);
2265 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2266
2267 // /* HeapReference<Class> */ temp = temp->component_type_
2268 __ Ldr(temp, HeapOperand(temp, component_offset));
2269 // /* HeapReference<Class> */ temp2 = value->klass_
2270 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2271 // If heap poisoning is enabled, no need to unpoison `temp`
2272 // nor `temp2`, as we are comparing two poisoned references.
2273 __ Cmp(temp, temp2);
2274
2275 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2276 vixl::Label do_put;
2277 __ B(eq, &do_put);
2278 // If heap poisoning is enabled, the `temp` reference has
2279 // not been unpoisoned yet; unpoison it now.
2280 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2281
2282 // /* HeapReference<Class> */ temp = temp->super_class_
2283 __ Ldr(temp, HeapOperand(temp, super_offset));
2284 // If heap poisoning is enabled, no need to unpoison
2285 // `temp`, as we are comparing against null below.
2286 __ Cbnz(temp, slow_path->GetEntryLabel());
2287 __ Bind(&do_put);
2288 } else {
2289 __ B(ne, slow_path->GetEntryLabel());
2290 }
2291 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002292 }
2293 }
2294
2295 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002296 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002297 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002298 __ Mov(temp2, value.W());
2299 GetAssembler()->PoisonHeapReference(temp2);
2300 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002301 }
2302
2303 if (!index.IsConstant()) {
2304 __ Add(temp, array, offset);
2305 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002306 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002307
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002309 codegen_->MaybeRecordImplicitNullCheck(instruction);
2310 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002311 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002312
2313 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2314
2315 if (done.IsLinked()) {
2316 __ Bind(&done);
2317 }
2318
2319 if (slow_path != nullptr) {
2320 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002321 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002322 }
2323}
2324
Alexandre Rames67555f72014-11-18 10:55:16 +00002325void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002326 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2327 ? LocationSummary::kCallOnSlowPath
2328 : LocationSummary::kNoCall;
2329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002330 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002331 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002332 if (instruction->HasUses()) {
2333 locations->SetOut(Location::SameAsFirstInput());
2334 }
2335}
2336
2337void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002338 BoundsCheckSlowPathARM64* slow_path =
2339 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002340 codegen_->AddSlowPath(slow_path);
2341
2342 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2343 __ B(slow_path->GetEntryLabel(), hs);
2344}
2345
Alexandre Rames67555f72014-11-18 10:55:16 +00002346void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2347 LocationSummary* locations =
2348 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2349 locations->SetInAt(0, Location::RequiresRegister());
2350 if (check->HasUses()) {
2351 locations->SetOut(Location::SameAsFirstInput());
2352 }
2353}
2354
2355void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2356 // We assume the class is not null.
2357 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2358 check->GetLoadClass(), check, check->GetDexPc(), true);
2359 codegen_->AddSlowPath(slow_path);
2360 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2361}
2362
Roland Levillain1a653882016-03-18 18:05:57 +00002363static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2364 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2365 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2366}
2367
2368void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2369 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2370 Location rhs_loc = instruction->GetLocations()->InAt(1);
2371 if (rhs_loc.IsConstant()) {
2372 // 0.0 is the only immediate that can be encoded directly in
2373 // an FCMP instruction.
2374 //
2375 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2376 // specify that in a floating-point comparison, positive zero
2377 // and negative zero are considered equal, so we can use the
2378 // literal 0.0 for both cases here.
2379 //
2380 // Note however that some methods (Float.equal, Float.compare,
2381 // Float.compareTo, Double.equal, Double.compare,
2382 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2383 // StrictMath.min) consider 0.0 to be (strictly) greater than
2384 // -0.0. So if we ever translate calls to these methods into a
2385 // HCompare instruction, we must handle the -0.0 case with
2386 // care here.
2387 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2388 __ Fcmp(lhs_reg, 0.0);
2389 } else {
2390 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2391 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002392}
2393
Serban Constantinescu02164b32014-11-13 14:05:07 +00002394void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002395 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002396 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2397 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002398 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002399 case Primitive::kPrimBoolean:
2400 case Primitive::kPrimByte:
2401 case Primitive::kPrimShort:
2402 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002403 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002404 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002405 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002406 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2408 break;
2409 }
2410 case Primitive::kPrimFloat:
2411 case Primitive::kPrimDouble: {
2412 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002413 locations->SetInAt(1,
2414 IsFloatingPointZeroConstant(compare->InputAt(1))
2415 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2416 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002417 locations->SetOut(Location::RequiresRegister());
2418 break;
2419 }
2420 default:
2421 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2422 }
2423}
2424
2425void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2426 Primitive::Type in_type = compare->InputAt(0)->GetType();
2427
2428 // 0 if: left == right
2429 // 1 if: left > right
2430 // -1 if: left < right
2431 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002432 case Primitive::kPrimBoolean:
2433 case Primitive::kPrimByte:
2434 case Primitive::kPrimShort:
2435 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002436 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002437 case Primitive::kPrimLong: {
2438 Register result = OutputRegister(compare);
2439 Register left = InputRegisterAt(compare, 0);
2440 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002441 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002442 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2443 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002444 break;
2445 }
2446 case Primitive::kPrimFloat:
2447 case Primitive::kPrimDouble: {
2448 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002449 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002450 __ Cset(result, ne);
2451 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 break;
2453 }
2454 default:
2455 LOG(FATAL) << "Unimplemented compare type " << in_type;
2456 }
2457}
2458
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002459void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002461
2462 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2463 locations->SetInAt(0, Location::RequiresFpuRegister());
2464 locations->SetInAt(1,
2465 IsFloatingPointZeroConstant(instruction->InputAt(1))
2466 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2467 : Location::RequiresFpuRegister());
2468 } else {
2469 // Integer cases.
2470 locations->SetInAt(0, Location::RequiresRegister());
2471 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2472 }
2473
David Brazdilb3e773e2016-01-26 11:28:37 +00002474 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002476 }
2477}
2478
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002479void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002480 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002481 return;
2482 }
2483
2484 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002485 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002486 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002487
Roland Levillain7f63c522015-07-13 15:54:55 +00002488 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002489 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002490 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002491 } else {
2492 // Integer cases.
2493 Register lhs = InputRegisterAt(instruction, 0);
2494 Operand rhs = InputOperandAt(instruction, 1);
2495 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002496 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002497 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002498}
2499
2500#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2501 M(Equal) \
2502 M(NotEqual) \
2503 M(LessThan) \
2504 M(LessThanOrEqual) \
2505 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002506 M(GreaterThanOrEqual) \
2507 M(Below) \
2508 M(BelowOrEqual) \
2509 M(Above) \
2510 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002511#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002512void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2513void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002514FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002515#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002516#undef FOR_EACH_CONDITION_INSTRUCTION
2517
Zheng Xuc6667102015-05-15 16:08:45 +08002518void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2519 DCHECK(instruction->IsDiv() || instruction->IsRem());
2520
2521 LocationSummary* locations = instruction->GetLocations();
2522 Location second = locations->InAt(1);
2523 DCHECK(second.IsConstant());
2524
2525 Register out = OutputRegister(instruction);
2526 Register dividend = InputRegisterAt(instruction, 0);
2527 int64_t imm = Int64FromConstant(second.GetConstant());
2528 DCHECK(imm == 1 || imm == -1);
2529
2530 if (instruction->IsRem()) {
2531 __ Mov(out, 0);
2532 } else {
2533 if (imm == 1) {
2534 __ Mov(out, dividend);
2535 } else {
2536 __ Neg(out, dividend);
2537 }
2538 }
2539}
2540
2541void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2542 DCHECK(instruction->IsDiv() || instruction->IsRem());
2543
2544 LocationSummary* locations = instruction->GetLocations();
2545 Location second = locations->InAt(1);
2546 DCHECK(second.IsConstant());
2547
2548 Register out = OutputRegister(instruction);
2549 Register dividend = InputRegisterAt(instruction, 0);
2550 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002551 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002552 int ctz_imm = CTZ(abs_imm);
2553
2554 UseScratchRegisterScope temps(GetVIXLAssembler());
2555 Register temp = temps.AcquireSameSizeAs(out);
2556
2557 if (instruction->IsDiv()) {
2558 __ Add(temp, dividend, abs_imm - 1);
2559 __ Cmp(dividend, 0);
2560 __ Csel(out, temp, dividend, lt);
2561 if (imm > 0) {
2562 __ Asr(out, out, ctz_imm);
2563 } else {
2564 __ Neg(out, Operand(out, ASR, ctz_imm));
2565 }
2566 } else {
2567 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2568 __ Asr(temp, dividend, bits - 1);
2569 __ Lsr(temp, temp, bits - ctz_imm);
2570 __ Add(out, dividend, temp);
2571 __ And(out, out, abs_imm - 1);
2572 __ Sub(out, out, temp);
2573 }
2574}
2575
2576void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2577 DCHECK(instruction->IsDiv() || instruction->IsRem());
2578
2579 LocationSummary* locations = instruction->GetLocations();
2580 Location second = locations->InAt(1);
2581 DCHECK(second.IsConstant());
2582
2583 Register out = OutputRegister(instruction);
2584 Register dividend = InputRegisterAt(instruction, 0);
2585 int64_t imm = Int64FromConstant(second.GetConstant());
2586
2587 Primitive::Type type = instruction->GetResultType();
2588 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2589
2590 int64_t magic;
2591 int shift;
2592 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2593
2594 UseScratchRegisterScope temps(GetVIXLAssembler());
2595 Register temp = temps.AcquireSameSizeAs(out);
2596
2597 // temp = get_high(dividend * magic)
2598 __ Mov(temp, magic);
2599 if (type == Primitive::kPrimLong) {
2600 __ Smulh(temp, dividend, temp);
2601 } else {
2602 __ Smull(temp.X(), dividend, temp);
2603 __ Lsr(temp.X(), temp.X(), 32);
2604 }
2605
2606 if (imm > 0 && magic < 0) {
2607 __ Add(temp, temp, dividend);
2608 } else if (imm < 0 && magic > 0) {
2609 __ Sub(temp, temp, dividend);
2610 }
2611
2612 if (shift != 0) {
2613 __ Asr(temp, temp, shift);
2614 }
2615
2616 if (instruction->IsDiv()) {
2617 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2618 } else {
2619 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2620 // TODO: Strength reduction for msub.
2621 Register temp_imm = temps.AcquireSameSizeAs(out);
2622 __ Mov(temp_imm, imm);
2623 __ Msub(out, temp, temp_imm, dividend);
2624 }
2625}
2626
2627void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2628 DCHECK(instruction->IsDiv() || instruction->IsRem());
2629 Primitive::Type type = instruction->GetResultType();
2630 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2631
2632 LocationSummary* locations = instruction->GetLocations();
2633 Register out = OutputRegister(instruction);
2634 Location second = locations->InAt(1);
2635
2636 if (second.IsConstant()) {
2637 int64_t imm = Int64FromConstant(second.GetConstant());
2638
2639 if (imm == 0) {
2640 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2641 } else if (imm == 1 || imm == -1) {
2642 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002643 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002644 DivRemByPowerOfTwo(instruction);
2645 } else {
2646 DCHECK(imm <= -2 || imm >= 2);
2647 GenerateDivRemWithAnyConstant(instruction);
2648 }
2649 } else {
2650 Register dividend = InputRegisterAt(instruction, 0);
2651 Register divisor = InputRegisterAt(instruction, 1);
2652 if (instruction->IsDiv()) {
2653 __ Sdiv(out, dividend, divisor);
2654 } else {
2655 UseScratchRegisterScope temps(GetVIXLAssembler());
2656 Register temp = temps.AcquireSameSizeAs(out);
2657 __ Sdiv(temp, dividend, divisor);
2658 __ Msub(out, temp, divisor, dividend);
2659 }
2660 }
2661}
2662
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002663void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2664 LocationSummary* locations =
2665 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2666 switch (div->GetResultType()) {
2667 case Primitive::kPrimInt:
2668 case Primitive::kPrimLong:
2669 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002670 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2672 break;
2673
2674 case Primitive::kPrimFloat:
2675 case Primitive::kPrimDouble:
2676 locations->SetInAt(0, Location::RequiresFpuRegister());
2677 locations->SetInAt(1, Location::RequiresFpuRegister());
2678 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2679 break;
2680
2681 default:
2682 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2683 }
2684}
2685
2686void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2687 Primitive::Type type = div->GetResultType();
2688 switch (type) {
2689 case Primitive::kPrimInt:
2690 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002691 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002692 break;
2693
2694 case Primitive::kPrimFloat:
2695 case Primitive::kPrimDouble:
2696 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected div type " << type;
2701 }
2702}
2703
Alexandre Rames67555f72014-11-18 10:55:16 +00002704void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002705 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2706 ? LocationSummary::kCallOnSlowPath
2707 : LocationSummary::kNoCall;
2708 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002709 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2710 if (instruction->HasUses()) {
2711 locations->SetOut(Location::SameAsFirstInput());
2712 }
2713}
2714
2715void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2716 SlowPathCodeARM64* slow_path =
2717 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2718 codegen_->AddSlowPath(slow_path);
2719 Location value = instruction->GetLocations()->InAt(0);
2720
Alexandre Rames3e69f162014-12-10 10:36:50 +00002721 Primitive::Type type = instruction->GetType();
2722
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002723 if (!Primitive::IsIntegralType(type)) {
2724 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002725 return;
2726 }
2727
Alexandre Rames67555f72014-11-18 10:55:16 +00002728 if (value.IsConstant()) {
2729 int64_t divisor = Int64ConstantFrom(value);
2730 if (divisor == 0) {
2731 __ B(slow_path->GetEntryLabel());
2732 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002733 // A division by a non-null constant is valid. We don't need to perform
2734 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002735 }
2736 } else {
2737 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2738 }
2739}
2740
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002741void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2742 LocationSummary* locations =
2743 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2744 locations->SetOut(Location::ConstantLocation(constant));
2745}
2746
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002747void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2748 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002749 // Will be generated at use site.
2750}
2751
Alexandre Rames5319def2014-10-23 10:03:10 +01002752void LocationsBuilderARM64::VisitExit(HExit* exit) {
2753 exit->SetLocations(nullptr);
2754}
2755
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002756void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002757}
2758
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002759void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2760 LocationSummary* locations =
2761 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2762 locations->SetOut(Location::ConstantLocation(constant));
2763}
2764
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002765void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002766 // Will be generated at use site.
2767}
2768
David Brazdilfc6a86a2015-06-26 10:33:45 +00002769void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002770 DCHECK(!successor->IsExitBlock());
2771 HBasicBlock* block = got->GetBlock();
2772 HInstruction* previous = got->GetPrevious();
2773 HLoopInformation* info = block->GetLoopInformation();
2774
David Brazdil46e2a392015-03-16 17:31:52 +00002775 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002776 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2777 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2778 return;
2779 }
2780 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2781 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2782 }
2783 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002784 __ B(codegen_->GetLabelOf(successor));
2785 }
2786}
2787
David Brazdilfc6a86a2015-06-26 10:33:45 +00002788void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2789 got->SetLocations(nullptr);
2790}
2791
2792void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2793 HandleGoto(got, got->GetSuccessor());
2794}
2795
2796void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2797 try_boundary->SetLocations(nullptr);
2798}
2799
2800void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2801 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2802 if (!successor->IsExitBlock()) {
2803 HandleGoto(try_boundary, successor);
2804 }
2805}
2806
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002807void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002808 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002809 vixl::Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002810 vixl::Label* false_target) {
2811 // FP branching requires both targets to be explicit. If either of the targets
2812 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2813 vixl::Label fallthrough_target;
2814 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002815
David Brazdil0debae72015-11-12 18:37:00 +00002816 if (true_target == nullptr && false_target == nullptr) {
2817 // Nothing to do. The code always falls through.
2818 return;
2819 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002820 // Constant condition, statically compared against "true" (integer value 1).
2821 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002822 if (true_target != nullptr) {
2823 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002824 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002825 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002826 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002827 if (false_target != nullptr) {
2828 __ B(false_target);
2829 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002830 }
David Brazdil0debae72015-11-12 18:37:00 +00002831 return;
2832 }
2833
2834 // The following code generates these patterns:
2835 // (1) true_target == nullptr && false_target != nullptr
2836 // - opposite condition true => branch to false_target
2837 // (2) true_target != nullptr && false_target == nullptr
2838 // - condition true => branch to true_target
2839 // (3) true_target != nullptr && false_target != nullptr
2840 // - condition true => branch to true_target
2841 // - branch to false_target
2842 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002843 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002844 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002845 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002846 if (true_target == nullptr) {
2847 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2848 } else {
2849 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2850 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002851 } else {
2852 // The condition instruction has not been materialized, use its inputs as
2853 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002854 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002855
David Brazdil0debae72015-11-12 18:37:00 +00002856 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002857 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002858 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002859 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002860 IfCondition opposite_condition = condition->GetOppositeCondition();
2861 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002862 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002863 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002864 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002865 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002866 // Integer cases.
2867 Register lhs = InputRegisterAt(condition, 0);
2868 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002869
2870 Condition arm64_cond;
2871 vixl::Label* non_fallthrough_target;
2872 if (true_target == nullptr) {
2873 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2874 non_fallthrough_target = false_target;
2875 } else {
2876 arm64_cond = ARM64Condition(condition->GetCondition());
2877 non_fallthrough_target = true_target;
2878 }
2879
Aart Bik086d27e2016-01-20 17:02:00 -08002880 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
2881 rhs.IsImmediate() && (rhs.immediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002882 switch (arm64_cond) {
2883 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002884 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002885 break;
2886 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002887 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002888 break;
2889 case lt:
2890 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002891 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002892 break;
2893 case ge:
2894 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002895 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002896 break;
2897 default:
2898 // Without the `static_cast` the compiler throws an error for
2899 // `-Werror=sign-promo`.
2900 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2901 }
2902 } else {
2903 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002904 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002905 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002906 }
2907 }
David Brazdil0debae72015-11-12 18:37:00 +00002908
2909 // If neither branch falls through (case 3), the conditional branch to `true_target`
2910 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2911 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002912 __ B(false_target);
2913 }
David Brazdil0debae72015-11-12 18:37:00 +00002914
2915 if (fallthrough_target.IsLinked()) {
2916 __ Bind(&fallthrough_target);
2917 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002918}
2919
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002920void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002922 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002923 locations->SetInAt(0, Location::RequiresRegister());
2924 }
2925}
2926
2927void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002928 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2929 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2930 vixl::Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2931 nullptr : codegen_->GetLabelOf(true_successor);
2932 vixl::Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2933 nullptr : codegen_->GetLabelOf(false_successor);
2934 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002935}
2936
2937void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2938 LocationSummary* locations = new (GetGraph()->GetArena())
2939 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002940 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002941 locations->SetInAt(0, Location::RequiresRegister());
2942 }
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002946 SlowPathCodeARM64* slow_path =
2947 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002948 GenerateTestAndBranch(deoptimize,
2949 /* condition_input_index */ 0,
2950 slow_path->GetEntryLabel(),
2951 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002952}
2953
David Brazdilc0b601b2016-02-08 14:20:45 +00002954static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2955 return condition->IsCondition() &&
2956 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2957}
2958
Alexandre Rames880f1192016-06-13 16:04:50 +01002959static inline Condition GetConditionForSelect(HCondition* condition) {
2960 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002961 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2962 : ARM64Condition(cond);
2963}
2964
David Brazdil74eb1b22015-12-14 11:44:01 +00002965void LocationsBuilderARM64::VisitSelect(HSelect* select) {
2966 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01002967 if (Primitive::IsFloatingPointType(select->GetType())) {
2968 locations->SetInAt(0, Location::RequiresFpuRegister());
2969 locations->SetInAt(1, Location::RequiresFpuRegister());
2970 locations->SetOut(Location::RequiresFpuRegister());
2971 } else {
2972 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
2973 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
2974 bool is_true_value_constant = cst_true_value != nullptr;
2975 bool is_false_value_constant = cst_false_value != nullptr;
2976 // Ask VIXL whether we should synthesize constants in registers.
2977 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
2978 Operand true_op = is_true_value_constant ?
2979 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
2980 Operand false_op = is_false_value_constant ?
2981 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
2982 bool true_value_in_register = false;
2983 bool false_value_in_register = false;
2984 MacroAssembler::GetCselSynthesisInformation(
2985 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
2986 true_value_in_register |= !is_true_value_constant;
2987 false_value_in_register |= !is_false_value_constant;
2988
2989 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
2990 : Location::ConstantLocation(cst_true_value));
2991 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
2992 : Location::ConstantLocation(cst_false_value));
2993 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002994 }
Alexandre Rames880f1192016-06-13 16:04:50 +01002995
David Brazdil74eb1b22015-12-14 11:44:01 +00002996 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2997 locations->SetInAt(2, Location::RequiresRegister());
2998 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002999}
3000
3001void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003002 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003003 Condition csel_cond;
3004
3005 if (IsBooleanValueOrMaterializedCondition(cond)) {
3006 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003007 // Use the condition flags set by the previous instruction.
3008 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003009 } else {
3010 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003011 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003012 }
3013 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003014 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003015 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003016 } else {
3017 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003018 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003019 }
3020
Alexandre Rames880f1192016-06-13 16:04:50 +01003021 if (Primitive::IsFloatingPointType(select->GetType())) {
3022 __ Fcsel(OutputFPRegister(select),
3023 InputFPRegisterAt(select, 1),
3024 InputFPRegisterAt(select, 0),
3025 csel_cond);
3026 } else {
3027 __ Csel(OutputRegister(select),
3028 InputOperandAt(select, 1),
3029 InputOperandAt(select, 0),
3030 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003031 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003032}
3033
David Srbecky0cf44932015-12-09 14:09:59 +00003034void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3035 new (GetGraph()->GetArena()) LocationSummary(info);
3036}
3037
David Srbeckyd28f4a02016-03-14 17:14:24 +00003038void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3039 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003040}
3041
3042void CodeGeneratorARM64::GenerateNop() {
3043 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003044}
3045
Alexandre Rames5319def2014-10-23 10:03:10 +01003046void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003047 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003048}
3049
3050void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003051 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003052}
3053
3054void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003055 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003056}
3057
3058void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003059 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003060}
3061
Roland Levillain44015862016-01-22 11:47:17 +00003062static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3063 return kEmitCompilerReadBarrier &&
3064 (kUseBakerReadBarrier ||
3065 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3066 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3067 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3068}
3069
Alexandre Rames67555f72014-11-18 10:55:16 +00003070void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003071 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003072 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3073 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003074 case TypeCheckKind::kExactCheck:
3075 case TypeCheckKind::kAbstractClassCheck:
3076 case TypeCheckKind::kClassHierarchyCheck:
3077 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003078 call_kind =
3079 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003080 break;
3081 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003082 case TypeCheckKind::kUnresolvedCheck:
3083 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003084 call_kind = LocationSummary::kCallOnSlowPath;
3085 break;
3086 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003087
Alexandre Rames67555f72014-11-18 10:55:16 +00003088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003089 locations->SetInAt(0, Location::RequiresRegister());
3090 locations->SetInAt(1, Location::RequiresRegister());
3091 // The "out" register is used as a temporary, so it overlaps with the inputs.
3092 // Note that TypeCheckSlowPathARM64 uses this register too.
3093 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3094 // When read barriers are enabled, we need a temporary register for
3095 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003096 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003097 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003098 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003099}
3100
3101void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003102 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003103 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003104 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003105 Register obj = InputRegisterAt(instruction, 0);
3106 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003107 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003108 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003109 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3110 locations->GetTemp(0) :
3111 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003112 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3113 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3114 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3115 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003116
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003117 vixl::Label done, zero;
3118 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003119
3120 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003121 // Avoid null check if we know `obj` is not null.
3122 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 __ Cbz(obj, &zero);
3124 }
3125
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003127 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003128
Roland Levillain44015862016-01-22 11:47:17 +00003129 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003130 case TypeCheckKind::kExactCheck: {
3131 __ Cmp(out, cls);
3132 __ Cset(out, eq);
3133 if (zero.IsLinked()) {
3134 __ B(&done);
3135 }
3136 break;
3137 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003138
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 case TypeCheckKind::kAbstractClassCheck: {
3140 // If the class is abstract, we eagerly fetch the super class of the
3141 // object to avoid doing a comparison we know will fail.
3142 vixl::Label loop, success;
3143 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003144 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003145 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003146 // If `out` is null, we use it for the result, and jump to `done`.
3147 __ Cbz(out, &done);
3148 __ Cmp(out, cls);
3149 __ B(ne, &loop);
3150 __ Mov(out, 1);
3151 if (zero.IsLinked()) {
3152 __ B(&done);
3153 }
3154 break;
3155 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 case TypeCheckKind::kClassHierarchyCheck: {
3158 // Walk over the class hierarchy to find a match.
3159 vixl::Label loop, success;
3160 __ Bind(&loop);
3161 __ Cmp(out, cls);
3162 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003163 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003164 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003165 __ Cbnz(out, &loop);
3166 // If `out` is null, we use it for the result, and jump to `done`.
3167 __ B(&done);
3168 __ Bind(&success);
3169 __ Mov(out, 1);
3170 if (zero.IsLinked()) {
3171 __ B(&done);
3172 }
3173 break;
3174 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003176 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003177 // Do an exact check.
3178 vixl::Label exact_check;
3179 __ Cmp(out, cls);
3180 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003181 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003182 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003183 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003184 // If `out` is null, we use it for the result, and jump to `done`.
3185 __ Cbz(out, &done);
3186 __ Ldrh(out, HeapOperand(out, primitive_offset));
3187 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3188 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003189 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003190 __ Mov(out, 1);
3191 __ B(&done);
3192 break;
3193 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003195 case TypeCheckKind::kArrayCheck: {
3196 __ Cmp(out, cls);
3197 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003198 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3199 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003200 codegen_->AddSlowPath(slow_path);
3201 __ B(ne, slow_path->GetEntryLabel());
3202 __ Mov(out, 1);
3203 if (zero.IsLinked()) {
3204 __ B(&done);
3205 }
3206 break;
3207 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003208
Calin Juravle98893e12015-10-02 21:05:03 +01003209 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003210 case TypeCheckKind::kInterfaceCheck: {
3211 // Note that we indeed only call on slow path, but we always go
3212 // into the slow path for the unresolved and interface check
3213 // cases.
3214 //
3215 // We cannot directly call the InstanceofNonTrivial runtime
3216 // entry point without resorting to a type checking slow path
3217 // here (i.e. by calling InvokeRuntime directly), as it would
3218 // require to assign fixed registers for the inputs of this
3219 // HInstanceOf instruction (following the runtime calling
3220 // convention), which might be cluttered by the potential first
3221 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003222 //
3223 // TODO: Introduce a new runtime entry point taking the object
3224 // to test (instead of its class) as argument, and let it deal
3225 // with the read barrier issues. This will let us refactor this
3226 // case of the `switch` code as it was previously (with a direct
3227 // call to the runtime not using a type checking slow path).
3228 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003229 DCHECK(locations->OnlyCallsOnSlowPath());
3230 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3231 /* is_fatal */ false);
3232 codegen_->AddSlowPath(slow_path);
3233 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003234 if (zero.IsLinked()) {
3235 __ B(&done);
3236 }
3237 break;
3238 }
3239 }
3240
3241 if (zero.IsLinked()) {
3242 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003243 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003244 }
3245
3246 if (done.IsLinked()) {
3247 __ Bind(&done);
3248 }
3249
3250 if (slow_path != nullptr) {
3251 __ Bind(slow_path->GetExitLabel());
3252 }
3253}
3254
3255void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3256 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3257 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3258
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003259 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3260 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003261 case TypeCheckKind::kExactCheck:
3262 case TypeCheckKind::kAbstractClassCheck:
3263 case TypeCheckKind::kClassHierarchyCheck:
3264 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003265 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3266 LocationSummary::kCallOnSlowPath :
3267 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003268 break;
3269 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003270 case TypeCheckKind::kUnresolvedCheck:
3271 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003272 call_kind = LocationSummary::kCallOnSlowPath;
3273 break;
3274 }
3275
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003276 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, Location::RequiresRegister());
3279 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3280 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003281 // When read barriers are enabled, we need an additional temporary
3282 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003283 if (TypeCheckNeedsATemporary(type_check_kind)) {
3284 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003285 }
3286}
3287
3288void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003289 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003290 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003291 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003292 Register obj = InputRegisterAt(instruction, 0);
3293 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003294 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003295 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3296 locations->GetTemp(1) :
3297 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003298 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003299 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3300 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3301 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3302 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003303
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304 bool is_type_check_slow_path_fatal =
3305 (type_check_kind == TypeCheckKind::kExactCheck ||
3306 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3307 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3308 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3309 !instruction->CanThrowIntoCatchBlock();
3310 SlowPathCodeARM64* type_check_slow_path =
3311 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3312 is_type_check_slow_path_fatal);
3313 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003314
3315 vixl::Label done;
3316 // Avoid null check if we know obj is not null.
3317 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003318 __ Cbz(obj, &done);
3319 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003320
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003321 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003322 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003324 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003325 case TypeCheckKind::kExactCheck:
3326 case TypeCheckKind::kArrayCheck: {
3327 __ Cmp(temp, cls);
3328 // Jump to slow path for throwing the exception or doing a
3329 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003330 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 break;
3332 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003334 case TypeCheckKind::kAbstractClassCheck: {
3335 // If the class is abstract, we eagerly fetch the super class of the
3336 // object to avoid doing a comparison we know will fail.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 vixl::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003339 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003340 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003341
3342 // If the class reference currently in `temp` is not null, jump
3343 // to the `compare_classes` label to compare it with the checked
3344 // class.
3345 __ Cbnz(temp, &compare_classes);
3346 // Otherwise, jump to the slow path to throw the exception.
3347 //
3348 // But before, move back the object's class into `temp` before
3349 // going into the slow path, as it has been overwritten in the
3350 // meantime.
3351 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003352 GenerateReferenceLoadTwoRegisters(
3353 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003354 __ B(type_check_slow_path->GetEntryLabel());
3355
3356 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003357 __ Cmp(temp, cls);
3358 __ B(ne, &loop);
3359 break;
3360 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003362 case TypeCheckKind::kClassHierarchyCheck: {
3363 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003364 vixl::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 __ Bind(&loop);
3366 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003367 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003368
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003370 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003371
3372 // If the class reference currently in `temp` is not null, jump
3373 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003374 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375 // Otherwise, jump to the slow path to throw the exception.
3376 //
3377 // But before, move back the object's class into `temp` before
3378 // going into the slow path, as it has been overwritten in the
3379 // meantime.
3380 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003381 GenerateReferenceLoadTwoRegisters(
3382 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003383 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003384 break;
3385 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003387 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003388 // Do an exact check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003389 vixl::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003390 __ Cmp(temp, cls);
3391 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003392
3393 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003394 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003395 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003396
3397 // If the component type is not null (i.e. the object is indeed
3398 // an array), jump to label `check_non_primitive_component_type`
3399 // to further check that this component type is not a primitive
3400 // type.
3401 __ Cbnz(temp, &check_non_primitive_component_type);
3402 // Otherwise, jump to the slow path to throw the exception.
3403 //
3404 // But before, move back the object's class into `temp` before
3405 // going into the slow path, as it has been overwritten in the
3406 // meantime.
3407 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003408 GenerateReferenceLoadTwoRegisters(
3409 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410 __ B(type_check_slow_path->GetEntryLabel());
3411
3412 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003413 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3414 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003415 __ Cbz(temp, &done);
3416 // Same comment as above regarding `temp` and the slow path.
3417 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003418 GenerateReferenceLoadTwoRegisters(
3419 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 break;
3422 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003423
Calin Juravle98893e12015-10-02 21:05:03 +01003424 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003425 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426 // We always go into the type check slow path for the unresolved
3427 // and interface check cases.
3428 //
3429 // We cannot directly call the CheckCast runtime entry point
3430 // without resorting to a type checking slow path here (i.e. by
3431 // calling InvokeRuntime directly), as it would require to
3432 // assign fixed registers for the inputs of this HInstanceOf
3433 // instruction (following the runtime calling convention), which
3434 // might be cluttered by the potential first read barrier
3435 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003436 //
3437 // TODO: Introduce a new runtime entry point taking the object
3438 // to test (instead of its class) as argument, and let it deal
3439 // with the read barrier issues. This will let us refactor this
3440 // case of the `switch` code as it was previously (with a direct
3441 // call to the runtime not using a type checking slow path).
3442 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003443 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003444 break;
3445 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003446 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003448 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003449}
3450
Alexandre Rames5319def2014-10-23 10:03:10 +01003451void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3453 locations->SetOut(Location::ConstantLocation(constant));
3454}
3455
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003456void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003457 // Will be generated at use site.
3458}
3459
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003460void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3462 locations->SetOut(Location::ConstantLocation(constant));
3463}
3464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003465void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003466 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003467}
3468
Calin Juravle175dc732015-08-25 15:42:32 +01003469void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3470 // The trampoline uses the same calling convention as dex calling conventions,
3471 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3472 // the method_idx.
3473 HandleInvoke(invoke);
3474}
3475
3476void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3477 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3478}
3479
Alexandre Rames5319def2014-10-23 10:03:10 +01003480void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003481 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003482 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003483}
3484
Alexandre Rames67555f72014-11-18 10:55:16 +00003485void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3486 HandleInvoke(invoke);
3487}
3488
3489void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3490 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003491 LocationSummary* locations = invoke->GetLocations();
3492 Register temp = XRegisterFrom(locations->GetTemp(0));
Mathieu Chartiere401d142015-04-22 13:56:20 -07003493 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3494 invoke->GetImtIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003495 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003496 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003497 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003498
3499 // The register ip1 is required to be used for the hidden argument in
3500 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003501 MacroAssembler* masm = GetVIXLAssembler();
3502 UseScratchRegisterScope scratch_scope(masm);
3503 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003504 scratch_scope.Exclude(ip1);
3505 __ Mov(ip1, invoke->GetDexMethodIndex());
3506
Alexandre Rames67555f72014-11-18 10:55:16 +00003507 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003508 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003509 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003510 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003511 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003512 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003513 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003514 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003515 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003516 // Instead of simply (possibly) unpoisoning `temp` here, we should
3517 // emit a read barrier for the previous class reference load.
3518 // However this is not required in practice, as this is an
3519 // intermediate/temporary reference and because the current
3520 // concurrent copying collector keeps the from-space memory
3521 // intact/accessible until the end of the marking phase (the
3522 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003523 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Alexandre Rames67555f72014-11-18 10:55:16 +00003524 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003525 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003527 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 // lr();
3529 __ Blr(lr);
3530 DCHECK(!codegen_->IsLeafMethod());
3531 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3532}
3533
3534void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003535 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3536 if (intrinsic.TryDispatch(invoke)) {
3537 return;
3538 }
3539
Alexandre Rames67555f72014-11-18 10:55:16 +00003540 HandleInvoke(invoke);
3541}
3542
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003543void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003544 // Explicit clinit checks triggered by static invokes must have been pruned by
3545 // art::PrepareForRegisterAllocation.
3546 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003547
Andreas Gampe878d58c2015-01-15 23:24:00 -08003548 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3549 if (intrinsic.TryDispatch(invoke)) {
3550 return;
3551 }
3552
Alexandre Rames67555f72014-11-18 10:55:16 +00003553 HandleInvoke(invoke);
3554}
3555
Andreas Gampe878d58c2015-01-15 23:24:00 -08003556static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3557 if (invoke->GetLocations()->Intrinsified()) {
3558 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3559 intrinsic.Dispatch(invoke);
3560 return true;
3561 }
3562 return false;
3563}
3564
Vladimir Markodc151b22015-10-15 18:02:30 +01003565HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3566 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3567 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003568 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003569 return desired_dispatch_info;
3570}
3571
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003572void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003573 // For better instruction scheduling we load the direct code pointer before the method pointer.
3574 bool direct_code_loaded = false;
3575 switch (invoke->GetCodePtrLocation()) {
3576 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3577 // LR = code address from literal pool with link-time patch.
3578 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3579 direct_code_loaded = true;
3580 break;
3581 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3582 // LR = invoke->GetDirectCodePtr();
3583 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3584 direct_code_loaded = true;
3585 break;
3586 default:
3587 break;
3588 }
3589
Andreas Gampe878d58c2015-01-15 23:24:00 -08003590 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003591 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3592 switch (invoke->GetMethodLoadKind()) {
3593 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3594 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003595 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003596 break;
3597 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003598 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003599 break;
3600 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3601 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003602 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003603 break;
3604 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3605 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003606 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003607 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3608 break;
3609 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3610 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003611 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3612 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
3613 vixl::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003614 {
3615 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003616 __ Bind(adrp_label);
3617 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003618 }
Vladimir Marko58155012015-08-19 12:49:41 +00003619 // Add LDR with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003620 vixl::Label* ldr_label =
3621 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003622 {
3623 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003624 __ Bind(ldr_label);
3625 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003626 }
Vladimir Marko58155012015-08-19 12:49:41 +00003627 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003628 }
Vladimir Marko58155012015-08-19 12:49:41 +00003629 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003630 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003631 Register reg = XRegisterFrom(temp);
3632 Register method_reg;
3633 if (current_method.IsRegister()) {
3634 method_reg = XRegisterFrom(current_method);
3635 } else {
3636 DCHECK(invoke->GetLocations()->Intrinsified());
3637 DCHECK(!current_method.IsValid());
3638 method_reg = reg;
3639 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3640 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003641
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003642 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003643 __ Ldr(reg.X(),
3644 MemOperand(method_reg.X(),
3645 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003646 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003647 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3648 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003649 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3650 break;
3651 }
3652 }
3653
3654 switch (invoke->GetCodePtrLocation()) {
3655 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3656 __ Bl(&frame_entry_label_);
3657 break;
3658 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3659 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3660 vixl::Label* label = &relative_call_patches_.back().label;
Alexandre Rames6dc01742015-11-12 14:44:19 +00003661 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
3662 __ Bind(label);
3663 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003664 break;
3665 }
3666 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3667 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3668 // LR prepared above for better instruction scheduling.
3669 DCHECK(direct_code_loaded);
3670 // lr()
3671 __ Blr(lr);
3672 break;
3673 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3674 // LR = callee_method->entry_point_from_quick_compiled_code_;
3675 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003676 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003677 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3678 // lr()
3679 __ Blr(lr);
3680 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003681 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003682
Andreas Gampe878d58c2015-01-15 23:24:00 -08003683 DCHECK(!IsLeafMethod());
3684}
3685
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003686void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003687 // Use the calling convention instead of the location of the receiver, as
3688 // intrinsics may have put the receiver in a different register. In the intrinsics
3689 // slow path, the arguments have been moved to the right place, so here we are
3690 // guaranteed that the receiver is the first register of the calling convention.
3691 InvokeDexCallingConvention calling_convention;
3692 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003693 Register temp = XRegisterFrom(temp_in);
3694 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3695 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3696 Offset class_offset = mirror::Object::ClassOffset();
3697 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3698
3699 BlockPoolsScope block_pools(GetVIXLAssembler());
3700
3701 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003702 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003703 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003704 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003705 // Instead of simply (possibly) unpoisoning `temp` here, we should
3706 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003707 // intermediate/temporary reference and because the current
3708 // concurrent copying collector keeps the from-space memory
3709 // intact/accessible until the end of the marking phase (the
3710 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003711 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3712 // temp = temp->GetMethodAt(method_offset);
3713 __ Ldr(temp, MemOperand(temp, method_offset));
3714 // lr = temp->GetEntryPoint();
3715 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3716 // lr();
3717 __ Blr(lr);
3718}
3719
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003720vixl::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(const DexFile& dex_file,
3721 uint32_t string_index,
3722 vixl::Label* adrp_label) {
3723 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3724}
3725
3726vixl::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
3727 uint32_t element_offset,
3728 vixl::Label* adrp_label) {
3729 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3730}
3731
3732vixl::Label* CodeGeneratorARM64::NewPcRelativePatch(const DexFile& dex_file,
3733 uint32_t offset_or_index,
3734 vixl::Label* adrp_label,
3735 ArenaDeque<PcRelativePatchInfo>* patches) {
3736 // Add a patch entry and return the label.
3737 patches->emplace_back(dex_file, offset_or_index);
3738 PcRelativePatchInfo* info = &patches->back();
3739 vixl::Label* label = &info->label;
3740 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3741 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3742 return label;
3743}
3744
3745vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
3746 const DexFile& dex_file, uint32_t string_index) {
3747 return boot_image_string_patches_.GetOrCreate(
3748 StringReference(&dex_file, string_index),
3749 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3750}
3751
3752vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(uint64_t address) {
3753 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3754 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3755 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3756}
3757
3758vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(uint64_t address) {
3759 return DeduplicateUint64Literal(address);
3760}
3761
Vladimir Marko58155012015-08-19 12:49:41 +00003762void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3763 DCHECK(linker_patches->empty());
3764 size_t size =
3765 method_patches_.size() +
3766 call_patches_.size() +
3767 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003768 pc_relative_dex_cache_patches_.size() +
3769 boot_image_string_patches_.size() +
3770 pc_relative_string_patches_.size() +
3771 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003772 linker_patches->reserve(size);
3773 for (const auto& entry : method_patches_) {
3774 const MethodReference& target_method = entry.first;
3775 vixl::Literal<uint64_t>* literal = entry.second;
3776 linker_patches->push_back(LinkerPatch::MethodPatch(literal->offset(),
3777 target_method.dex_file,
3778 target_method.dex_method_index));
3779 }
3780 for (const auto& entry : call_patches_) {
3781 const MethodReference& target_method = entry.first;
3782 vixl::Literal<uint64_t>* literal = entry.second;
3783 linker_patches->push_back(LinkerPatch::CodePatch(literal->offset(),
3784 target_method.dex_file,
3785 target_method.dex_method_index));
3786 }
3787 for (const MethodPatchInfo<vixl::Label>& info : relative_call_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003788 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003789 info.target_method.dex_file,
3790 info.target_method.dex_method_index));
3791 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003792 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Alexandre Rames6dc01742015-11-12 14:44:19 +00003793 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.location(),
Vladimir Marko58155012015-08-19 12:49:41 +00003794 &info.target_dex_file,
Alexandre Rames6dc01742015-11-12 14:44:19 +00003795 info.pc_insn_label->location(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003796 info.offset_or_index));
3797 }
3798 for (const auto& entry : boot_image_string_patches_) {
3799 const StringReference& target_string = entry.first;
3800 vixl::Literal<uint32_t>* literal = entry.second;
3801 linker_patches->push_back(LinkerPatch::StringPatch(literal->offset(),
3802 target_string.dex_file,
3803 target_string.string_index));
3804 }
3805 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
3806 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.location(),
3807 &info.target_dex_file,
3808 info.pc_insn_label->location(),
3809 info.offset_or_index));
3810 }
3811 for (const auto& entry : boot_image_address_patches_) {
3812 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
3813 vixl::Literal<uint32_t>* literal = entry.second;
3814 linker_patches->push_back(LinkerPatch::RecordPosition(literal->offset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003815 }
3816}
3817
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003818vixl::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
3819 Uint32ToLiteralMap* map) {
3820 return map->GetOrCreate(
3821 value,
3822 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3823}
3824
Vladimir Marko58155012015-08-19 12:49:41 +00003825vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003826 return uint64_literals_.GetOrCreate(
3827 value,
3828 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003829}
3830
3831vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
3832 MethodReference target_method,
3833 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003834 return map->GetOrCreate(
3835 target_method,
3836 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003837}
3838
3839vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
3840 MethodReference target_method) {
3841 return DeduplicateMethodLiteral(target_method, &method_patches_);
3842}
3843
3844vixl::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
3845 MethodReference target_method) {
3846 return DeduplicateMethodLiteral(target_method, &call_patches_);
3847}
3848
3849
Andreas Gampe878d58c2015-01-15 23:24:00 -08003850void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003851 // Explicit clinit checks triggered by static invokes must have been pruned by
3852 // art::PrepareForRegisterAllocation.
3853 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003854
Andreas Gampe878d58c2015-01-15 23:24:00 -08003855 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3856 return;
3857 }
3858
Alexandre Ramesd921d642015-04-16 15:07:16 +01003859 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003860 LocationSummary* locations = invoke->GetLocations();
3861 codegen_->GenerateStaticOrDirectCall(
3862 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003863 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003864}
3865
3866void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003867 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3868 return;
3869 }
3870
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003871 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003872 DCHECK(!codegen_->IsLeafMethod());
3873 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3874}
3875
Alexandre Rames67555f72014-11-18 10:55:16 +00003876void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003877 InvokeRuntimeCallingConvention calling_convention;
3878 CodeGenerator::CreateLoadClassLocationSummary(
3879 cls,
3880 LocationFrom(calling_convention.GetRegisterAt(0)),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003881 LocationFrom(vixl::x0),
3882 /* code_generator_supports_read_barrier */ true);
Alexandre Rames67555f72014-11-18 10:55:16 +00003883}
3884
3885void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01003886 if (cls->NeedsAccessCheck()) {
3887 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
3888 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
3889 cls,
3890 cls->GetDexPc(),
3891 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003892 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01003893 return;
3894 }
3895
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003896 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01003897 Register out = OutputRegister(cls);
3898 Register current_method = InputRegisterAt(cls, 0);
3899 if (cls->IsReferrersClass()) {
Alexandre Rames67555f72014-11-18 10:55:16 +00003900 DCHECK(!cls->CanCallRuntime());
3901 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain44015862016-01-22 11:47:17 +00003902 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
3903 GenerateGcRootFieldLoad(
3904 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Alexandre Rames67555f72014-11-18 10:55:16 +00003905 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01003906 MemberOffset resolved_types_offset = ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003907 // /* GcRoot<mirror::Class>[] */ out =
3908 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
Vladimir Marko05792b92015-08-03 11:56:49 +01003909 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00003910 // /* GcRoot<mirror::Class> */ out = out[type_index]
3911 GenerateGcRootFieldLoad(
3912 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003913
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00003914 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
3915 DCHECK(cls->CanCallRuntime());
3916 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3917 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
3918 codegen_->AddSlowPath(slow_path);
3919 if (!cls->IsInDexCache()) {
3920 __ Cbz(out, slow_path->GetEntryLabel());
3921 }
3922 if (cls->MustGenerateClinitCheck()) {
3923 GenerateClassInitializationCheck(slow_path, out);
3924 } else {
3925 __ Bind(slow_path->GetExitLabel());
3926 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003927 }
3928 }
3929}
3930
David Brazdilcb1c0552015-08-04 16:22:25 +01003931static MemOperand GetExceptionTlsAddress() {
3932 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
3933}
3934
Alexandre Rames67555f72014-11-18 10:55:16 +00003935void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
3936 LocationSummary* locations =
3937 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3938 locations->SetOut(Location::RequiresRegister());
3939}
3940
3941void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01003942 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
3943}
3944
3945void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
3946 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3947}
3948
3949void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3950 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00003951}
3952
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003953HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
3954 HLoadString::LoadKind desired_string_load_kind) {
3955 if (kEmitCompilerReadBarrier) {
3956 switch (desired_string_load_kind) {
3957 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3958 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3959 case HLoadString::LoadKind::kBootImageAddress:
3960 // TODO: Implement for read barrier.
3961 return HLoadString::LoadKind::kDexCacheViaMethod;
3962 default:
3963 break;
3964 }
3965 }
3966 switch (desired_string_load_kind) {
3967 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
3968 DCHECK(!GetCompilerOptions().GetCompilePic());
3969 break;
3970 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
3971 DCHECK(GetCompilerOptions().GetCompilePic());
3972 break;
3973 case HLoadString::LoadKind::kBootImageAddress:
3974 break;
3975 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01003976 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003977 break;
3978 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01003979 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003980 break;
3981 case HLoadString::LoadKind::kDexCacheViaMethod:
3982 break;
3983 }
3984 return desired_string_load_kind;
3985}
3986
Alexandre Rames67555f72014-11-18 10:55:16 +00003987void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003988 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00003989 ? LocationSummary::kCallOnSlowPath
3990 : LocationSummary::kNoCall;
3991 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003992 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
3993 locations->SetInAt(0, Location::RequiresRegister());
3994 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003995 locations->SetOut(Location::RequiresRegister());
3996}
3997
3998void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004000 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004002 switch (load->GetLoadKind()) {
4003 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4004 DCHECK(!kEmitCompilerReadBarrier);
4005 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4006 load->GetStringIndex()));
4007 return; // No dex cache slow path.
4008 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4009 DCHECK(!kEmitCompilerReadBarrier);
4010 // Add ADRP with its PC-relative String patch.
4011 const DexFile& dex_file = load->GetDexFile();
4012 uint32_t string_index = load->GetStringIndex();
4013 vixl::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
4014 {
4015 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4016 __ Bind(adrp_label);
4017 __ adrp(out.X(), /* offset placeholder */ 0);
4018 }
4019 // Add ADD with its PC-relative String patch.
4020 vixl::Label* add_label =
4021 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4022 {
4023 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4024 __ Bind(add_label);
4025 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4026 }
4027 return; // No dex cache slow path.
4028 }
4029 case HLoadString::LoadKind::kBootImageAddress: {
4030 DCHECK(!kEmitCompilerReadBarrier);
4031 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4032 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4033 return; // No dex cache slow path.
4034 }
4035 case HLoadString::LoadKind::kDexCacheAddress: {
4036 DCHECK_NE(load->GetAddress(), 0u);
4037 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4038 // that gives a 16KiB range. To try and reduce the number of literals if we load
4039 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4040 // loaded from a literal and the remaining offset embedded in the load.
4041 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4042 DCHECK_ALIGNED(load->GetAddress(), 4u);
4043 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4044 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4045 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4046 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4047 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4048 break;
4049 }
4050 case HLoadString::LoadKind::kDexCachePcRelative: {
4051 // Add ADRP with its PC-relative DexCache access patch.
4052 const DexFile& dex_file = load->GetDexFile();
4053 uint32_t element_offset = load->GetDexCacheElementOffset();
4054 vixl::Label* adrp_label = codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
4055 {
4056 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4057 __ Bind(adrp_label);
4058 __ adrp(out.X(), /* offset placeholder */ 0);
4059 }
4060 // Add LDR with its PC-relative DexCache access patch.
4061 vixl::Label* ldr_label =
4062 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4063 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4064 break;
4065 }
4066 case HLoadString::LoadKind::kDexCacheViaMethod: {
4067 Register current_method = InputRegisterAt(load, 0);
4068 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4069 GenerateGcRootFieldLoad(
4070 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4071 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4072 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4073 // /* GcRoot<mirror::String> */ out = out[string_index]
4074 GenerateGcRootFieldLoad(
4075 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4076 break;
4077 }
4078 default:
4079 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4080 UNREACHABLE();
4081 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004082
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004083 if (!load->IsInDexCache()) {
4084 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4085 codegen_->AddSlowPath(slow_path);
4086 __ Cbz(out, slow_path->GetEntryLabel());
4087 __ Bind(slow_path->GetExitLabel());
4088 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004089}
4090
Alexandre Rames5319def2014-10-23 10:03:10 +01004091void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4092 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4093 locations->SetOut(Location::ConstantLocation(constant));
4094}
4095
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004096void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004097 // Will be generated at use site.
4098}
4099
Alexandre Rames67555f72014-11-18 10:55:16 +00004100void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4101 LocationSummary* locations =
4102 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4103 InvokeRuntimeCallingConvention calling_convention;
4104 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4105}
4106
4107void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4108 codegen_->InvokeRuntime(instruction->IsEnter()
4109 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4110 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004111 instruction->GetDexPc(),
4112 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004113 if (instruction->IsEnter()) {
4114 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4115 } else {
4116 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4117 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004118}
4119
Alexandre Rames42d641b2014-10-27 14:00:51 +00004120void LocationsBuilderARM64::VisitMul(HMul* mul) {
4121 LocationSummary* locations =
4122 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4123 switch (mul->GetResultType()) {
4124 case Primitive::kPrimInt:
4125 case Primitive::kPrimLong:
4126 locations->SetInAt(0, Location::RequiresRegister());
4127 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004128 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004129 break;
4130
4131 case Primitive::kPrimFloat:
4132 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004133 locations->SetInAt(0, Location::RequiresFpuRegister());
4134 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004135 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004136 break;
4137
4138 default:
4139 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4140 }
4141}
4142
4143void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4144 switch (mul->GetResultType()) {
4145 case Primitive::kPrimInt:
4146 case Primitive::kPrimLong:
4147 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4148 break;
4149
4150 case Primitive::kPrimFloat:
4151 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004152 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004153 break;
4154
4155 default:
4156 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4157 }
4158}
4159
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004160void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4161 LocationSummary* locations =
4162 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4163 switch (neg->GetResultType()) {
4164 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004165 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004166 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004168 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004169
4170 case Primitive::kPrimFloat:
4171 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004172 locations->SetInAt(0, Location::RequiresFpuRegister());
4173 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004174 break;
4175
4176 default:
4177 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4178 }
4179}
4180
4181void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4182 switch (neg->GetResultType()) {
4183 case Primitive::kPrimInt:
4184 case Primitive::kPrimLong:
4185 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4186 break;
4187
4188 case Primitive::kPrimFloat:
4189 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004190 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004191 break;
4192
4193 default:
4194 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4195 }
4196}
4197
4198void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4199 LocationSummary* locations =
4200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4201 InvokeRuntimeCallingConvention calling_convention;
4202 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004203 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004204 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004205 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004206}
4207
4208void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4209 LocationSummary* locations = instruction->GetLocations();
4210 InvokeRuntimeCallingConvention calling_convention;
4211 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4212 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004213 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004214 // Note: if heap poisoning is enabled, the entry point takes cares
4215 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004216 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4217 instruction,
4218 instruction->GetDexPc(),
4219 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004220 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004221}
4222
Alexandre Rames5319def2014-10-23 10:03:10 +01004223void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4224 LocationSummary* locations =
4225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4226 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004227 if (instruction->IsStringAlloc()) {
4228 locations->AddTemp(LocationFrom(kArtMethodRegister));
4229 } else {
4230 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4231 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4232 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004233 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4234}
4235
4236void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004237 // Note: if heap poisoning is enabled, the entry point takes cares
4238 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004239 if (instruction->IsStringAlloc()) {
4240 // String is allocated through StringFactory. Call NewEmptyString entry point.
4241 Location temp = instruction->GetLocations()->GetTemp(0);
4242 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4243 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4244 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4245 __ Blr(lr);
4246 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4247 } else {
4248 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4249 instruction,
4250 instruction->GetDexPc(),
4251 nullptr);
4252 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4253 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004254}
4255
4256void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004258 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004260}
4261
4262void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004263 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004264 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004265 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004266 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004267 break;
4268
4269 default:
4270 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4271 }
4272}
4273
David Brazdil66d126e2015-04-03 16:02:44 +01004274void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4275 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4276 locations->SetInAt(0, Location::RequiresRegister());
4277 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4278}
4279
4280void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
David Brazdil66d126e2015-04-03 16:02:44 +01004281 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::Operand(1));
4282}
4283
Alexandre Rames5319def2014-10-23 10:03:10 +01004284void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004285 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4286 ? LocationSummary::kCallOnSlowPath
4287 : LocationSummary::kNoCall;
4288 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004289 locations->SetInAt(0, Location::RequiresRegister());
4290 if (instruction->HasUses()) {
4291 locations->SetOut(Location::SameAsFirstInput());
4292 }
4293}
4294
Calin Juravle2ae48182016-03-16 14:05:09 +00004295void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4296 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004297 return;
4298 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004299
Alexandre Ramesd921d642015-04-16 15:07:16 +01004300 BlockPoolsScope block_pools(GetVIXLAssembler());
4301 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004302 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004303 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304}
4305
Calin Juravle2ae48182016-03-16 14:05:09 +00004306void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004307 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004308 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004309
4310 LocationSummary* locations = instruction->GetLocations();
4311 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004312
4313 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004314}
4315
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004316void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004317 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004318}
4319
Alexandre Rames67555f72014-11-18 10:55:16 +00004320void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4321 HandleBinaryOp(instruction);
4322}
4323
4324void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4325 HandleBinaryOp(instruction);
4326}
4327
Alexandre Rames3e69f162014-12-10 10:36:50 +00004328void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4329 LOG(FATAL) << "Unreachable";
4330}
4331
4332void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4333 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4334}
4335
Alexandre Rames5319def2014-10-23 10:03:10 +01004336void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4338 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4339 if (location.IsStackSlot()) {
4340 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4341 } else if (location.IsDoubleStackSlot()) {
4342 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4343 }
4344 locations->SetOut(location);
4345}
4346
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004347void InstructionCodeGeneratorARM64::VisitParameterValue(
4348 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004349 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004350}
4351
4352void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4353 LocationSummary* locations =
4354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004355 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004356}
4357
4358void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4359 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4360 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004361}
4362
4363void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4364 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004365 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004366 locations->SetInAt(i, Location::Any());
4367 }
4368 locations->SetOut(Location::Any());
4369}
4370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004371void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004372 LOG(FATAL) << "Unreachable";
4373}
4374
Serban Constantinescu02164b32014-11-13 14:05:07 +00004375void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004376 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004377 LocationSummary::CallKind call_kind =
4378 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4380
4381 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004382 case Primitive::kPrimInt:
4383 case Primitive::kPrimLong:
4384 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004385 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004386 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4387 break;
4388
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004389 case Primitive::kPrimFloat:
4390 case Primitive::kPrimDouble: {
4391 InvokeRuntimeCallingConvention calling_convention;
4392 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4393 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4394 locations->SetOut(calling_convention.GetReturnLocation(type));
4395
4396 break;
4397 }
4398
Serban Constantinescu02164b32014-11-13 14:05:07 +00004399 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004400 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004401 }
4402}
4403
4404void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4405 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004406
Serban Constantinescu02164b32014-11-13 14:05:07 +00004407 switch (type) {
4408 case Primitive::kPrimInt:
4409 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004410 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004411 break;
4412 }
4413
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004414 case Primitive::kPrimFloat:
4415 case Primitive::kPrimDouble: {
4416 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4417 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004418 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004419 if (type == Primitive::kPrimFloat) {
4420 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4421 } else {
4422 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4423 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004424 break;
4425 }
4426
Serban Constantinescu02164b32014-11-13 14:05:07 +00004427 default:
4428 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004429 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004430 }
4431}
4432
Calin Juravle27df7582015-04-17 19:12:31 +01004433void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4434 memory_barrier->SetLocations(nullptr);
4435}
4436
4437void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004438 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004439}
4440
Alexandre Rames5319def2014-10-23 10:03:10 +01004441void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4443 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004444 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004445}
4446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004447void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004448 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004449}
4450
4451void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4452 instruction->SetLocations(nullptr);
4453}
4454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004455void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004456 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004457}
4458
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004459void LocationsBuilderARM64::VisitRor(HRor* ror) {
4460 HandleBinaryOp(ror);
4461}
4462
4463void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4464 HandleBinaryOp(ror);
4465}
4466
Serban Constantinescu02164b32014-11-13 14:05:07 +00004467void LocationsBuilderARM64::VisitShl(HShl* shl) {
4468 HandleShift(shl);
4469}
4470
4471void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4472 HandleShift(shl);
4473}
4474
4475void LocationsBuilderARM64::VisitShr(HShr* shr) {
4476 HandleShift(shr);
4477}
4478
4479void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4480 HandleShift(shr);
4481}
4482
Alexandre Rames5319def2014-10-23 10:03:10 +01004483void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004484 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004485}
4486
4487void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004488 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004489}
4490
Alexandre Rames67555f72014-11-18 10:55:16 +00004491void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004492 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004493}
4494
4495void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004496 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004497}
4498
4499void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004500 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004501}
4502
Alexandre Rames67555f72014-11-18 10:55:16 +00004503void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004504 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004505}
4506
Calin Juravlee460d1d2015-09-29 04:52:17 +01004507void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4508 HUnresolvedInstanceFieldGet* instruction) {
4509 FieldAccessCallingConventionARM64 calling_convention;
4510 codegen_->CreateUnresolvedFieldLocationSummary(
4511 instruction, instruction->GetFieldType(), calling_convention);
4512}
4513
4514void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4515 HUnresolvedInstanceFieldGet* instruction) {
4516 FieldAccessCallingConventionARM64 calling_convention;
4517 codegen_->GenerateUnresolvedFieldAccess(instruction,
4518 instruction->GetFieldType(),
4519 instruction->GetFieldIndex(),
4520 instruction->GetDexPc(),
4521 calling_convention);
4522}
4523
4524void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4525 HUnresolvedInstanceFieldSet* instruction) {
4526 FieldAccessCallingConventionARM64 calling_convention;
4527 codegen_->CreateUnresolvedFieldLocationSummary(
4528 instruction, instruction->GetFieldType(), calling_convention);
4529}
4530
4531void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4532 HUnresolvedInstanceFieldSet* instruction) {
4533 FieldAccessCallingConventionARM64 calling_convention;
4534 codegen_->GenerateUnresolvedFieldAccess(instruction,
4535 instruction->GetFieldType(),
4536 instruction->GetFieldIndex(),
4537 instruction->GetDexPc(),
4538 calling_convention);
4539}
4540
4541void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4542 HUnresolvedStaticFieldGet* instruction) {
4543 FieldAccessCallingConventionARM64 calling_convention;
4544 codegen_->CreateUnresolvedFieldLocationSummary(
4545 instruction, instruction->GetFieldType(), calling_convention);
4546}
4547
4548void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4549 HUnresolvedStaticFieldGet* instruction) {
4550 FieldAccessCallingConventionARM64 calling_convention;
4551 codegen_->GenerateUnresolvedFieldAccess(instruction,
4552 instruction->GetFieldType(),
4553 instruction->GetFieldIndex(),
4554 instruction->GetDexPc(),
4555 calling_convention);
4556}
4557
4558void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4559 HUnresolvedStaticFieldSet* instruction) {
4560 FieldAccessCallingConventionARM64 calling_convention;
4561 codegen_->CreateUnresolvedFieldLocationSummary(
4562 instruction, instruction->GetFieldType(), calling_convention);
4563}
4564
4565void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4566 HUnresolvedStaticFieldSet* instruction) {
4567 FieldAccessCallingConventionARM64 calling_convention;
4568 codegen_->GenerateUnresolvedFieldAccess(instruction,
4569 instruction->GetFieldType(),
4570 instruction->GetFieldIndex(),
4571 instruction->GetDexPc(),
4572 calling_convention);
4573}
4574
Alexandre Rames5319def2014-10-23 10:03:10 +01004575void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4577}
4578
4579void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004580 HBasicBlock* block = instruction->GetBlock();
4581 if (block->GetLoopInformation() != nullptr) {
4582 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4583 // The back edge will generate the suspend check.
4584 return;
4585 }
4586 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4587 // The goto will generate the suspend check.
4588 return;
4589 }
4590 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004591}
4592
Alexandre Rames67555f72014-11-18 10:55:16 +00004593void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4594 LocationSummary* locations =
4595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4596 InvokeRuntimeCallingConvention calling_convention;
4597 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4598}
4599
4600void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4601 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004602 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004603 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004604}
4605
4606void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4607 LocationSummary* locations =
4608 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4609 Primitive::Type input_type = conversion->GetInputType();
4610 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004611 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004612 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4613 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4614 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4615 }
4616
Alexandre Rames542361f2015-01-29 16:57:31 +00004617 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004618 locations->SetInAt(0, Location::RequiresFpuRegister());
4619 } else {
4620 locations->SetInAt(0, Location::RequiresRegister());
4621 }
4622
Alexandre Rames542361f2015-01-29 16:57:31 +00004623 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004624 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4625 } else {
4626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4627 }
4628}
4629
4630void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4631 Primitive::Type result_type = conversion->GetResultType();
4632 Primitive::Type input_type = conversion->GetInputType();
4633
4634 DCHECK_NE(input_type, result_type);
4635
Alexandre Rames542361f2015-01-29 16:57:31 +00004636 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004637 int result_size = Primitive::ComponentSize(result_type);
4638 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004639 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004640 Register output = OutputRegister(conversion);
4641 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004642 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004643 // 'int' values are used directly as W registers, discarding the top
4644 // bits, so we don't need to sign-extend and can just perform a move.
4645 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4646 // top 32 bits of the target register. We theoretically could leave those
4647 // bits unchanged, but we would have to make sure that no code uses a
4648 // 32bit input value as a 64bit value assuming that the top 32 bits are
4649 // zero.
4650 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004651 } else if (result_type == Primitive::kPrimChar ||
4652 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4653 __ Ubfx(output,
4654 output.IsX() ? source.X() : source.W(),
4655 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004656 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004657 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004658 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004659 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004660 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004661 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004662 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4663 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004664 } else if (Primitive::IsFloatingPointType(result_type) &&
4665 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004666 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4667 } else {
4668 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4669 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004670 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004671}
Alexandre Rames67555f72014-11-18 10:55:16 +00004672
Serban Constantinescu02164b32014-11-13 14:05:07 +00004673void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4674 HandleShift(ushr);
4675}
4676
4677void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4678 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004679}
4680
4681void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4682 HandleBinaryOp(instruction);
4683}
4684
4685void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4686 HandleBinaryOp(instruction);
4687}
4688
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004689void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004690 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004691 LOG(FATAL) << "Unreachable";
4692}
4693
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004694void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004695 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004696 LOG(FATAL) << "Unreachable";
4697}
4698
Mark Mendellfe57faa2015-09-18 09:26:15 -04004699// Simple implementation of packed switch - generate cascaded compare/jumps.
4700void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4701 LocationSummary* locations =
4702 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4703 locations->SetInAt(0, Location::RequiresRegister());
4704}
4705
4706void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4707 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004708 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004709 Register value_reg = InputRegisterAt(switch_instr, 0);
4710 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4711
Zheng Xu3927c8b2015-11-18 17:46:25 +08004712 // Roughly set 16 as max average assemblies generated per HIR in a graph.
4713 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * vixl::kInstructionSize;
4714 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4715 // make sure we don't emit it if the target may run out of range.
4716 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4717 // ranges and emit the tables only as required.
4718 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004719
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004720 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004721 // Current instruction id is an upper bound of the number of HIRs in the graph.
4722 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4723 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004724 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4725 Register temp = temps.AcquireW();
4726 __ Subs(temp, value_reg, Operand(lower_bound));
4727
Zheng Xu3927c8b2015-11-18 17:46:25 +08004728 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004729 // Jump to successors[0] if value == lower_bound.
4730 __ B(eq, codegen_->GetLabelOf(successors[0]));
4731 int32_t last_index = 0;
4732 for (; num_entries - last_index > 2; last_index += 2) {
4733 __ Subs(temp, temp, Operand(2));
4734 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4735 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4736 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4737 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4738 }
4739 if (num_entries - last_index == 2) {
4740 // The last missing case_value.
4741 __ Cmp(temp, Operand(1));
4742 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004743 }
4744
4745 // And the default for any other value.
4746 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4747 __ B(codegen_->GetLabelOf(default_block));
4748 }
4749 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004750 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004751
4752 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4753
4754 // Below instructions should use at most one blocked register. Since there are two blocked
4755 // registers, we are free to block one.
4756 Register temp_w = temps.AcquireW();
4757 Register index;
4758 // Remove the bias.
4759 if (lower_bound != 0) {
4760 index = temp_w;
4761 __ Sub(index, value_reg, Operand(lower_bound));
4762 } else {
4763 index = value_reg;
4764 }
4765
4766 // Jump to default block if index is out of the range.
4767 __ Cmp(index, Operand(num_entries));
4768 __ B(hs, codegen_->GetLabelOf(default_block));
4769
4770 // In current VIXL implementation, it won't require any blocked registers to encode the
4771 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4772 // register pressure.
4773 Register table_base = temps.AcquireX();
4774 // Load jump offset from the table.
4775 __ Adr(table_base, jump_table->GetTableStartLabel());
4776 Register jump_offset = temp_w;
4777 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4778
4779 // Jump to target block by branching to table_base(pc related) + offset.
4780 Register target_address = table_base;
4781 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4782 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004783 }
4784}
4785
Roland Levillain44015862016-01-22 11:47:17 +00004786void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4787 Location out,
4788 uint32_t offset,
4789 Location maybe_temp) {
4790 Primitive::Type type = Primitive::kPrimNot;
4791 Register out_reg = RegisterFrom(out, type);
4792 if (kEmitCompilerReadBarrier) {
4793 Register temp_reg = RegisterFrom(maybe_temp, type);
4794 if (kUseBakerReadBarrier) {
4795 // Load with fast path based Baker's read barrier.
4796 // /* HeapReference<Object> */ out = *(out + offset)
4797 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4798 out,
4799 out_reg,
4800 offset,
4801 temp_reg,
4802 /* needs_null_check */ false,
4803 /* use_load_acquire */ false);
4804 } else {
4805 // Load with slow path based read barrier.
4806 // Save the value of `out` into `maybe_temp` before overwriting it
4807 // in the following move operation, as we will need it for the
4808 // read barrier below.
4809 __ Mov(temp_reg, out_reg);
4810 // /* HeapReference<Object> */ out = *(out + offset)
4811 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4812 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
4813 }
4814 } else {
4815 // Plain load with no read barrier.
4816 // /* HeapReference<Object> */ out = *(out + offset)
4817 __ Ldr(out_reg, HeapOperand(out_reg, offset));
4818 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4819 }
4820}
4821
4822void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
4823 Location out,
4824 Location obj,
4825 uint32_t offset,
4826 Location maybe_temp) {
4827 Primitive::Type type = Primitive::kPrimNot;
4828 Register out_reg = RegisterFrom(out, type);
4829 Register obj_reg = RegisterFrom(obj, type);
4830 if (kEmitCompilerReadBarrier) {
4831 if (kUseBakerReadBarrier) {
4832 // Load with fast path based Baker's read barrier.
4833 Register temp_reg = RegisterFrom(maybe_temp, type);
4834 // /* HeapReference<Object> */ out = *(obj + offset)
4835 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
4836 out,
4837 obj_reg,
4838 offset,
4839 temp_reg,
4840 /* needs_null_check */ false,
4841 /* use_load_acquire */ false);
4842 } else {
4843 // Load with slow path based read barrier.
4844 // /* HeapReference<Object> */ out = *(obj + offset)
4845 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4846 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
4847 }
4848 } else {
4849 // Plain load with no read barrier.
4850 // /* HeapReference<Object> */ out = *(obj + offset)
4851 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
4852 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
4853 }
4854}
4855
4856void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
4857 Location root,
4858 vixl::Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004859 uint32_t offset,
4860 vixl::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00004861 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
4862 if (kEmitCompilerReadBarrier) {
4863 if (kUseBakerReadBarrier) {
4864 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
4865 // Baker's read barrier are used:
4866 //
4867 // root = obj.field;
4868 // if (Thread::Current()->GetIsGcMarking()) {
4869 // root = ReadBarrier::Mark(root)
4870 // }
4871
4872 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004873 if (fixup_label == nullptr) {
4874 __ Ldr(root_reg, MemOperand(obj, offset));
4875 } else {
4876 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4877 __ Bind(fixup_label);
4878 __ ldr(root_reg, MemOperand(obj, offset));
4879 }
Roland Levillain44015862016-01-22 11:47:17 +00004880 static_assert(
4881 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
4882 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
4883 "have different sizes.");
4884 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
4885 "art::mirror::CompressedReference<mirror::Object> and int32_t "
4886 "have different sizes.");
4887
4888 // Slow path used to mark the GC root `root`.
4889 SlowPathCodeARM64* slow_path =
4890 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root, root);
4891 codegen_->AddSlowPath(slow_path);
4892
4893 MacroAssembler* masm = GetVIXLAssembler();
4894 UseScratchRegisterScope temps(masm);
4895 Register temp = temps.AcquireW();
4896 // temp = Thread::Current()->GetIsGcMarking()
4897 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
4898 __ Cbnz(temp, slow_path->GetEntryLabel());
4899 __ Bind(slow_path->GetExitLabel());
4900 } else {
4901 // GC root loaded through a slow path for read barriers other
4902 // than Baker's.
4903 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004904 if (fixup_label == nullptr) {
4905 __ Add(root_reg.X(), obj.X(), offset);
4906 } else {
4907 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4908 __ Bind(fixup_label);
4909 __ add(root_reg.X(), obj.X(), offset);
4910 }
Roland Levillain44015862016-01-22 11:47:17 +00004911 // /* mirror::Object* */ root = root->Read()
4912 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
4913 }
4914 } else {
4915 // Plain GC root load with no read barrier.
4916 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004917 if (fixup_label == nullptr) {
4918 __ Ldr(root_reg, MemOperand(obj, offset));
4919 } else {
4920 vixl::SingleEmissionCheckScope guard(GetVIXLAssembler());
4921 __ Bind(fixup_label);
4922 __ ldr(root_reg, MemOperand(obj, offset));
4923 }
Roland Levillain44015862016-01-22 11:47:17 +00004924 // Note that GC roots are not affected by heap poisoning, thus we
4925 // do not have to unpoison `root_reg` here.
4926 }
4927}
4928
4929void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
4930 Location ref,
4931 vixl::Register obj,
4932 uint32_t offset,
4933 Register temp,
4934 bool needs_null_check,
4935 bool use_load_acquire) {
4936 DCHECK(kEmitCompilerReadBarrier);
4937 DCHECK(kUseBakerReadBarrier);
4938
4939 // /* HeapReference<Object> */ ref = *(obj + offset)
4940 Location no_index = Location::NoLocation();
4941 GenerateReferenceLoadWithBakerReadBarrier(
4942 instruction, ref, obj, offset, no_index, temp, needs_null_check, use_load_acquire);
4943}
4944
4945void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
4946 Location ref,
4947 vixl::Register obj,
4948 uint32_t data_offset,
4949 Location index,
4950 Register temp,
4951 bool needs_null_check) {
4952 DCHECK(kEmitCompilerReadBarrier);
4953 DCHECK(kUseBakerReadBarrier);
4954
4955 // Array cells are never volatile variables, therefore array loads
4956 // never use Load-Acquire instructions on ARM64.
4957 const bool use_load_acquire = false;
4958
4959 // /* HeapReference<Object> */ ref =
4960 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4961 GenerateReferenceLoadWithBakerReadBarrier(
4962 instruction, ref, obj, data_offset, index, temp, needs_null_check, use_load_acquire);
4963}
4964
4965void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
4966 Location ref,
4967 vixl::Register obj,
4968 uint32_t offset,
4969 Location index,
4970 Register temp,
4971 bool needs_null_check,
4972 bool use_load_acquire) {
4973 DCHECK(kEmitCompilerReadBarrier);
4974 DCHECK(kUseBakerReadBarrier);
4975 // If `index` is a valid location, then we are emitting an array
4976 // load, so we shouldn't be using a Load Acquire instruction.
4977 // In other words: `index.IsValid()` => `!use_load_acquire`.
4978 DCHECK(!index.IsValid() || !use_load_acquire);
4979
4980 MacroAssembler* masm = GetVIXLAssembler();
4981 UseScratchRegisterScope temps(masm);
4982
4983 // In slow path based read barriers, the read barrier call is
4984 // inserted after the original load. However, in fast path based
4985 // Baker's read barriers, we need to perform the load of
4986 // mirror::Object::monitor_ *before* the original reference load.
4987 // This load-load ordering is required by the read barrier.
4988 // The fast path/slow path (for Baker's algorithm) should look like:
4989 //
4990 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
4991 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
4992 // HeapReference<Object> ref = *src; // Original reference load.
4993 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
4994 // if (is_gray) {
4995 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
4996 // }
4997 //
4998 // Note: the original implementation in ReadBarrier::Barrier is
4999 // slightly more complex as it performs additional checks that we do
5000 // not do here for performance reasons.
5001
5002 Primitive::Type type = Primitive::kPrimNot;
5003 Register ref_reg = RegisterFrom(ref, type);
5004 DCHECK(obj.IsW());
5005 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5006
5007 // /* int32_t */ monitor = obj->monitor_
5008 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5009 if (needs_null_check) {
5010 MaybeRecordImplicitNullCheck(instruction);
5011 }
5012 // /* LockWord */ lock_word = LockWord(monitor)
5013 static_assert(sizeof(LockWord) == sizeof(int32_t),
5014 "art::LockWord and int32_t have different sizes.");
5015 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
5016 __ Lsr(temp, temp, LockWord::kReadBarrierStateShift);
5017 __ And(temp, temp, Operand(LockWord::kReadBarrierStateMask));
5018 static_assert(
5019 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
5020 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
5021
5022 // Introduce a dependency on the high bits of rb_state, which shall
5023 // be all zeroes, to prevent load-load reordering, and without using
5024 // a memory barrier (which would be more expensive).
5025 // temp2 = rb_state & ~LockWord::kReadBarrierStateMask = 0
5026 Register temp2 = temps.AcquireW();
5027 __ Bic(temp2, temp, Operand(LockWord::kReadBarrierStateMask));
5028 // obj is unchanged by this operation, but its value now depends on
5029 // temp2, which depends on temp.
5030 __ Add(obj, obj, Operand(temp2));
5031 temps.Release(temp2);
5032
5033 // The actual reference load.
5034 if (index.IsValid()) {
5035 static_assert(
5036 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5037 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005038 // /* HeapReference<Object> */ ref =
5039 // *(obj + offset + index * sizeof(HeapReference<Object>))
Roland Levillainca0bf032016-02-09 12:49:18 +00005040 const size_t shift_amount = Primitive::ComponentSizeShift(type);
Roland Levillain44015862016-01-22 11:47:17 +00005041 if (index.IsConstant()) {
Roland Levillainca0bf032016-02-09 12:49:18 +00005042 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << shift_amount);
5043 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillain44015862016-01-22 11:47:17 +00005044 } else {
Roland Levillainca0bf032016-02-09 12:49:18 +00005045 temp2 = temps.AcquireW();
Roland Levillain44015862016-01-22 11:47:17 +00005046 __ Add(temp2, obj, offset);
Roland Levillainca0bf032016-02-09 12:49:18 +00005047 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, shift_amount));
5048 temps.Release(temp2);
Roland Levillain44015862016-01-22 11:47:17 +00005049 }
Roland Levillain44015862016-01-22 11:47:17 +00005050 } else {
5051 // /* HeapReference<Object> */ ref = *(obj + offset)
5052 MemOperand field = HeapOperand(obj, offset);
5053 if (use_load_acquire) {
5054 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5055 } else {
5056 Load(type, ref_reg, field);
5057 }
5058 }
5059
5060 // Object* ref = ref_addr->AsMirrorPtr()
5061 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5062
5063 // Slow path used to mark the object `ref` when it is gray.
5064 SlowPathCodeARM64* slow_path =
5065 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref, ref);
5066 AddSlowPath(slow_path);
5067
5068 // if (rb_state == ReadBarrier::gray_ptr_)
5069 // ref = ReadBarrier::Mark(ref);
5070 __ Cmp(temp, ReadBarrier::gray_ptr_);
5071 __ B(eq, slow_path->GetEntryLabel());
5072 __ Bind(slow_path->GetExitLabel());
5073}
5074
5075void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5076 Location out,
5077 Location ref,
5078 Location obj,
5079 uint32_t offset,
5080 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005081 DCHECK(kEmitCompilerReadBarrier);
5082
Roland Levillain44015862016-01-22 11:47:17 +00005083 // Insert a slow path based read barrier *after* the reference load.
5084 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005085 // If heap poisoning is enabled, the unpoisoning of the loaded
5086 // reference will be carried out by the runtime within the slow
5087 // path.
5088 //
5089 // Note that `ref` currently does not get unpoisoned (when heap
5090 // poisoning is enabled), which is alright as the `ref` argument is
5091 // not used by the artReadBarrierSlow entry point.
5092 //
5093 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5094 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5095 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5096 AddSlowPath(slow_path);
5097
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005098 __ B(slow_path->GetEntryLabel());
5099 __ Bind(slow_path->GetExitLabel());
5100}
5101
Roland Levillain44015862016-01-22 11:47:17 +00005102void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5103 Location out,
5104 Location ref,
5105 Location obj,
5106 uint32_t offset,
5107 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005108 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005109 // Baker's read barriers shall be handled by the fast path
5110 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5111 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005112 // If heap poisoning is enabled, unpoisoning will be taken care of
5113 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005114 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005115 } else if (kPoisonHeapReferences) {
5116 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5117 }
5118}
5119
Roland Levillain44015862016-01-22 11:47:17 +00005120void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5121 Location out,
5122 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005123 DCHECK(kEmitCompilerReadBarrier);
5124
Roland Levillain44015862016-01-22 11:47:17 +00005125 // Insert a slow path based read barrier *after* the GC root load.
5126 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005127 // Note that GC roots are not affected by heap poisoning, so we do
5128 // not need to do anything special for this here.
5129 SlowPathCodeARM64* slow_path =
5130 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5131 AddSlowPath(slow_path);
5132
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005133 __ B(slow_path->GetEntryLabel());
5134 __ Bind(slow_path->GetExitLabel());
5135}
5136
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005137void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5138 LocationSummary* locations =
5139 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5140 locations->SetInAt(0, Location::RequiresRegister());
5141 locations->SetOut(Location::RequiresRegister());
5142}
5143
5144void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5145 LocationSummary* locations = instruction->GetLocations();
5146 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005147 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005148 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5149 instruction->GetIndex(), kArm64PointerSize).SizeValue();
5150 } else {
5151 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
5152 instruction->GetIndex() % mirror::Class::kImtSize, kArm64PointerSize).Uint32Value();
5153 }
5154 __ Ldr(XRegisterFrom(locations->Out()),
5155 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
5156}
5157
5158
5159
Alexandre Rames67555f72014-11-18 10:55:16 +00005160#undef __
5161#undef QUICK_ENTRY_POINT
5162
Alexandre Rames5319def2014-10-23 10:03:10 +01005163} // namespace arm64
5164} // namespace art