blob: 33eee3b515ada2ffcd2fba5c2fa9fbf3cc6f754a [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
Scott Wakeling97c72b72016-06-24 16:19:36 +010036using namespace vixl::aarch64; // NOLINT(build/namespaces)
Alexandre Rames5319def2014-10-23 10:03:10 +010037
38#ifdef __
39#error "ARM64 Codegen VIXL macro-assembler macro already defined."
40#endif
41
Alexandre Rames5319def2014-10-23 10:03:10 +010042namespace art {
43
Roland Levillain22ccc3a2015-11-24 13:10:05 +000044template<class MirrorType>
45class GcRoot;
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace arm64 {
48
Andreas Gampe878d58c2015-01-15 23:24:00 -080049using helpers::CPURegisterFrom;
50using helpers::DRegisterFrom;
51using helpers::FPRegisterFrom;
52using helpers::HeapOperand;
53using helpers::HeapOperandFrom;
54using helpers::InputCPURegisterAt;
55using helpers::InputFPRegisterAt;
56using helpers::InputRegisterAt;
57using helpers::InputOperandAt;
58using helpers::Int64ConstantFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059using helpers::LocationFrom;
60using helpers::OperandFromMemOperand;
61using helpers::OutputCPURegister;
62using helpers::OutputFPRegister;
63using helpers::OutputRegister;
64using helpers::RegisterFrom;
65using helpers::StackOperandFrom;
66using helpers::VIXLRegCodeFromART;
67using helpers::WRegisterFrom;
68using helpers::XRegisterFrom;
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +000069using helpers::ARM64EncodableConstantOrRegister;
Zheng Xuda403092015-04-24 17:35:39 +080070using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080071
Alexandre Rames5319def2014-10-23 10:03:10 +010072static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000073// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080074// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
75// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000076static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010077
Alexandre Rames5319def2014-10-23 10:03:10 +010078inline Condition ARM64Condition(IfCondition cond) {
79 switch (cond) {
80 case kCondEQ: return eq;
81 case kCondNE: return ne;
82 case kCondLT: return lt;
83 case kCondLE: return le;
84 case kCondGT: return gt;
85 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -070086 case kCondB: return lo;
87 case kCondBE: return ls;
88 case kCondA: return hi;
89 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +010090 }
Roland Levillain7f63c522015-07-13 15:54:55 +000091 LOG(FATAL) << "Unreachable";
92 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +010093}
94
Vladimir Markod6e069b2016-01-18 11:11:01 +000095inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
96 // The ARM64 condition codes can express all the necessary branches, see the
97 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
98 // There is no dex instruction or HIR that would need the missing conditions
99 // "equal or unordered" or "not equal".
100 switch (cond) {
101 case kCondEQ: return eq;
102 case kCondNE: return ne /* unordered */;
103 case kCondLT: return gt_bias ? cc : lt /* unordered */;
104 case kCondLE: return gt_bias ? ls : le /* unordered */;
105 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
106 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
107 default:
108 LOG(FATAL) << "UNREACHABLE";
109 UNREACHABLE();
110 }
111}
112
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000113Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000114 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
115 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
116 // but we use the exact registers for clarity.
117 if (return_type == Primitive::kPrimFloat) {
118 return LocationFrom(s0);
119 } else if (return_type == Primitive::kPrimDouble) {
120 return LocationFrom(d0);
121 } else if (return_type == Primitive::kPrimLong) {
122 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100123 } else if (return_type == Primitive::kPrimVoid) {
124 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000125 } else {
126 return LocationFrom(w0);
127 }
128}
129
Alexandre Rames5319def2014-10-23 10:03:10 +0100130Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000131 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100132}
133
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700134// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
135#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Alexandre Rames67555f72014-11-18 10:55:16 +0000136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100137
Zheng Xuda403092015-04-24 17:35:39 +0800138// Calculate memory accessing operand for save/restore live registers.
139static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
140 RegisterSet* register_set,
141 int64_t spill_offset,
142 bool is_save) {
143 DCHECK(ArtVixlRegCodeCoherentForRegSet(register_set->GetCoreRegisters(),
144 codegen->GetNumberOfCoreRegisters(),
145 register_set->GetFloatingPointRegisters(),
146 codegen->GetNumberOfFloatingPointRegisters()));
147
148 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100149 register_set->GetCoreRegisters() & (~callee_saved_core_registers.GetList()));
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000150 CPURegList fp_list = CPURegList(CPURegister::kFPRegister, kDRegSize,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100151 register_set->GetFloatingPointRegisters() & (~callee_saved_fp_registers.GetList()));
Zheng Xuda403092015-04-24 17:35:39 +0800152
153 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
154 UseScratchRegisterScope temps(masm);
155
156 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100157 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
158 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800159 int64_t reg_size = kXRegSizeInBytes;
160 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
161 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100162 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800163 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
164 // If the offset does not fit in the instruction's immediate field, use an alternate register
165 // to compute the base address(float point registers spill base address).
166 Register new_base = temps.AcquireSameSizeAs(base);
167 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
168 base = new_base;
169 spill_offset = -core_spill_size;
170 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
171 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
172 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
173 }
174
175 if (is_save) {
176 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
177 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
178 } else {
179 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
180 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
181 }
182}
183
184void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
185 RegisterSet* register_set = locations->GetLiveRegisters();
186 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
187 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
188 if (!codegen->IsCoreCalleeSaveRegister(i) && register_set->ContainsCoreRegister(i)) {
189 // If the register holds an object, update the stack mask.
190 if (locations->RegisterContainsObject(i)) {
191 locations->SetStackBit(stack_offset / kVRegSize);
192 }
193 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
194 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
195 saved_core_stack_offsets_[i] = stack_offset;
196 stack_offset += kXRegSizeInBytes;
197 }
198 }
199
200 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
201 if (!codegen->IsFloatingPointCalleeSaveRegister(i) &&
202 register_set->ContainsFloatingPointRegister(i)) {
203 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
204 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
205 saved_fpu_stack_offsets_[i] = stack_offset;
206 stack_offset += kDRegSizeInBytes;
207 }
208 }
209
210 SaveRestoreLiveRegistersHelper(codegen, register_set,
211 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
212}
213
214void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
215 RegisterSet* register_set = locations->GetLiveRegisters();
216 SaveRestoreLiveRegistersHelper(codegen, register_set,
217 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
218}
219
Alexandre Rames5319def2014-10-23 10:03:10 +0100220class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
221 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000222 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100223
Alexandre Rames67555f72014-11-18 10:55:16 +0000224 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100225 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000226 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100227
Alexandre Rames5319def2014-10-23 10:03:10 +0100228 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000229 if (instruction_->CanThrowIntoCatchBlock()) {
230 // Live registers will be restored in the catch block if caught.
231 SaveLiveRegisters(codegen, instruction_->GetLocations());
232 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000233 // We're moving two locations to locations that could overlap, so we need a parallel
234 // move resolver.
235 InvokeRuntimeCallingConvention calling_convention;
236 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100237 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
238 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100239 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
240 ? QUICK_ENTRY_POINT(pThrowStringBounds)
241 : QUICK_ENTRY_POINT(pThrowArrayBounds);
242 arm64_codegen->InvokeRuntime(entry_point_offset, instruction_, instruction_->GetDexPc(), this);
243 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800244 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100245 }
246
Alexandre Rames8158f282015-08-07 10:26:17 +0100247 bool IsFatal() const OVERRIDE { return true; }
248
Alexandre Rames9931f312015-06-19 14:47:01 +0100249 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
250
Alexandre Rames5319def2014-10-23 10:03:10 +0100251 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100252 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
253};
254
Alexandre Rames67555f72014-11-18 10:55:16 +0000255class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
256 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000258
259 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
260 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
261 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000262 if (instruction_->CanThrowIntoCatchBlock()) {
263 // Live registers will be restored in the catch block if caught.
264 SaveLiveRegisters(codegen, instruction_->GetLocations());
265 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000266 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000267 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800268 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000269 }
270
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 bool IsFatal() const OVERRIDE { return true; }
272
Alexandre Rames9931f312015-06-19 14:47:01 +0100273 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
277};
278
279class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
280 public:
281 LoadClassSlowPathARM64(HLoadClass* cls,
282 HInstruction* at,
283 uint32_t dex_pc,
284 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000285 : SlowPathCodeARM64(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
287 }
288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
290 LocationSummary* locations = at_->GetLocations();
291 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
292
293 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000295
296 InvokeRuntimeCallingConvention calling_convention;
297 __ Mov(calling_convention.GetRegisterAt(0).W(), cls_->GetTypeIndex());
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
299 : QUICK_ENTRY_POINT(pInitializeType);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000300 arm64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800301 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800303 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100304 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800305 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000306
307 // Move the class to the desired location.
308 Location out = locations->Out();
309 if (out.IsValid()) {
310 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
311 Primitive::Type type = at_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000312 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 }
314
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000315 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000316 __ B(GetExitLabel());
317 }
318
Alexandre Rames9931f312015-06-19 14:47:01 +0100319 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
320
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 private:
322 // The class this slow path will load.
323 HLoadClass* const cls_;
324
325 // The instruction where this slow path is happening.
326 // (Might be the load class or an initialization check).
327 HInstruction* const at_;
328
329 // The dex PC of `at_`.
330 const uint32_t dex_pc_;
331
332 // Whether to initialize the class.
333 const bool do_clinit_;
334
335 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
336};
337
338class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
339 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000340 explicit LoadStringSlowPathARM64(HLoadString* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000341
342 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
343 LocationSummary* locations = instruction_->GetLocations();
344 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
345 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
346
347 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000348 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000351 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
352 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index);
Alexandre Rames67555f72014-11-18 10:55:16 +0000353 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000354 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this);
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100355 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000357 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000359 RestoreLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000360 __ B(GetExitLabel());
361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
364
Alexandre Rames67555f72014-11-18 10:55:16 +0000365 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000366 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
367};
368
Alexandre Rames5319def2014-10-23 10:03:10 +0100369class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
370 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100372
Alexandre Rames67555f72014-11-18 10:55:16 +0000373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
374 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000380 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000381 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800382 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 }
384
Alexandre Rames8158f282015-08-07 10:26:17 +0100385 bool IsFatal() const OVERRIDE { return true; }
386
Alexandre Rames9931f312015-06-19 14:47:01 +0100387 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
388
Alexandre Rames5319def2014-10-23 10:03:10 +0100389 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100390 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
391};
392
393class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
394 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100395 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000396 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100397
Alexandre Rames67555f72014-11-18 10:55:16 +0000398 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
399 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100400 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000401 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000402 arm64_codegen->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000403 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800404 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000405 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames67555f72014-11-18 10:55:16 +0000406 if (successor_ == nullptr) {
407 __ B(GetReturnLabel());
408 } else {
409 __ B(arm64_codegen->GetLabelOf(successor_));
410 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100411 }
412
Scott Wakeling97c72b72016-06-24 16:19:36 +0100413 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100414 DCHECK(successor_ == nullptr);
415 return &return_label_;
416 }
417
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100418 HBasicBlock* GetSuccessor() const {
419 return successor_;
420 }
421
Alexandre Rames9931f312015-06-19 14:47:01 +0100422 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
423
Alexandre Rames5319def2014-10-23 10:03:10 +0100424 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100425 // If not null, the block to branch to after the suspend check.
426 HBasicBlock* const successor_;
427
428 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100429 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100430
431 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
432};
433
Alexandre Rames67555f72014-11-18 10:55:16 +0000434class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
435 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000436 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000440 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100441 Location class_to_check = locations->InAt(1);
442 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
443 : locations->Out();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000444 DCHECK(instruction_->IsCheckCast()
445 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
446 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100447 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000448
Alexandre Rames67555f72014-11-18 10:55:16 +0000449 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000451 if (!is_fatal_) {
452 SaveLiveRegisters(codegen, locations);
453 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000454
455 // We're moving two locations to locations that could overlap, so we need a parallel
456 // move resolver.
457 InvokeRuntimeCallingConvention calling_convention;
458 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100459 class_to_check, LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimNot,
460 object_class, LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000461
462 if (instruction_->IsInstanceOf()) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000463 arm64_codegen->InvokeRuntime(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100464 QUICK_ENTRY_POINT(pInstanceofNonTrivial), instruction_, dex_pc, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000465 CheckEntrypointTypes<kQuickInstanceofNonTrivial, uint32_t,
466 const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000467 Primitive::Type ret_type = instruction_->GetType();
468 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
469 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
470 } else {
471 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100472 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800473 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000474 }
475
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000476 if (!is_fatal_) {
477 RestoreLiveRegisters(codegen, locations);
478 __ B(GetExitLabel());
479 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000480 }
481
Alexandre Rames9931f312015-06-19 14:47:01 +0100482 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483 bool IsFatal() const { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100484
Alexandre Rames67555f72014-11-18 10:55:16 +0000485 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000486 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000487
Alexandre Rames67555f72014-11-18 10:55:16 +0000488 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
489};
490
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700491class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
492 public:
Aart Bik42249c32016-01-07 15:33:50 -0800493 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000494 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700495
496 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800497 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700498 __ Bind(GetEntryLabel());
499 SaveLiveRegisters(codegen, instruction_->GetLocations());
Aart Bik42249c32016-01-07 15:33:50 -0800500 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
501 instruction_,
502 instruction_->GetDexPc(),
503 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000504 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700505 }
506
Alexandre Rames9931f312015-06-19 14:47:01 +0100507 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700509 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700510 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
511};
512
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100513class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
514 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000515 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100516
517 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
518 LocationSummary* locations = instruction_->GetLocations();
519 __ Bind(GetEntryLabel());
520 SaveLiveRegisters(codegen, locations);
521
522 InvokeRuntimeCallingConvention calling_convention;
523 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
524 parallel_move.AddMove(
525 locations->InAt(0),
526 LocationFrom(calling_convention.GetRegisterAt(0)),
527 Primitive::kPrimNot,
528 nullptr);
529 parallel_move.AddMove(
530 locations->InAt(1),
531 LocationFrom(calling_convention.GetRegisterAt(1)),
532 Primitive::kPrimInt,
533 nullptr);
534 parallel_move.AddMove(
535 locations->InAt(2),
536 LocationFrom(calling_convention.GetRegisterAt(2)),
537 Primitive::kPrimNot,
538 nullptr);
539 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
540
541 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
542 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
543 instruction_,
544 instruction_->GetDexPc(),
545 this);
546 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
547 RestoreLiveRegisters(codegen, locations);
548 __ B(GetExitLabel());
549 }
550
551 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
552
553 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100554 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
555};
556
Zheng Xu3927c8b2015-11-18 17:46:25 +0800557void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
558 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000559 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800560
561 // We are about to use the assembler to place literals directly. Make sure we have enough
562 // underlying code buffer and we have generated the jump table with right size.
563 CodeBufferCheckScope scope(codegen->GetVIXLAssembler(), num_entries * sizeof(int32_t),
564 CodeBufferCheckScope::kCheck, CodeBufferCheckScope::kExactSize);
565
566 __ Bind(&table_start_);
567 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
568 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100569 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800570 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100571 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800572 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
573 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
574 Literal<int32_t> literal(jump_offset);
575 __ place(&literal);
576 }
577}
578
Roland Levillain44015862016-01-22 11:47:17 +0000579// Slow path marking an object during a read barrier.
580class ReadBarrierMarkSlowPathARM64 : public SlowPathCodeARM64 {
581 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100582 ReadBarrierMarkSlowPathARM64(HInstruction* instruction, Location obj)
583 : SlowPathCodeARM64(instruction), obj_(obj) {
Roland Levillain44015862016-01-22 11:47:17 +0000584 DCHECK(kEmitCompilerReadBarrier);
585 }
586
587 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
588
589 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
590 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain44015862016-01-22 11:47:17 +0000591 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100592 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(obj_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000593 DCHECK(instruction_->IsInstanceFieldGet() ||
594 instruction_->IsStaticFieldGet() ||
595 instruction_->IsArrayGet() ||
596 instruction_->IsLoadClass() ||
597 instruction_->IsLoadString() ||
598 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100599 instruction_->IsCheckCast() ||
600 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
601 instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +0000602 << "Unexpected instruction in read barrier marking slow path: "
603 << instruction_->DebugName();
604
605 __ Bind(GetEntryLabel());
Roland Levillain02b75802016-07-13 11:54:35 +0100606 // Save live registers before the runtime call, and in particular
607 // W0 (if it is live), as it is clobbered by functions
608 // art_quick_read_barrier_mark_regX.
Roland Levillain44015862016-01-22 11:47:17 +0000609 SaveLiveRegisters(codegen, locations);
610
611 InvokeRuntimeCallingConvention calling_convention;
612 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100613 DCHECK_NE(obj_.reg(), LR);
614 DCHECK_NE(obj_.reg(), WSP);
615 DCHECK_NE(obj_.reg(), WZR);
616 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
617 // "Compact" slow path, saving two moves.
618 //
619 // Instead of using the standard runtime calling convention (input
620 // and output in W0):
621 //
622 // W0 <- obj
623 // W0 <- ReadBarrierMark(W0)
624 // obj <- W0
625 //
626 // we just use rX (the register holding `obj`) as input and output
627 // of a dedicated entrypoint:
628 //
629 // rX <- ReadBarrierMarkRegX(rX)
630 //
631 int32_t entry_point_offset =
632 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64WordSize>(obj_.reg());
633 // TODO: Do not emit a stack map for this runtime call.
634 arm64_codegen->InvokeRuntime(entry_point_offset,
Roland Levillain44015862016-01-22 11:47:17 +0000635 instruction_,
636 instruction_->GetDexPc(),
637 this);
Roland Levillain44015862016-01-22 11:47:17 +0000638
639 RestoreLiveRegisters(codegen, locations);
640 __ B(GetExitLabel());
641 }
642
643 private:
Roland Levillain44015862016-01-22 11:47:17 +0000644 const Location obj_;
645
646 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
647};
648
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000649// Slow path generating a read barrier for a heap reference.
650class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
651 public:
652 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
653 Location out,
654 Location ref,
655 Location obj,
656 uint32_t offset,
657 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000658 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000659 out_(out),
660 ref_(ref),
661 obj_(obj),
662 offset_(offset),
663 index_(index) {
664 DCHECK(kEmitCompilerReadBarrier);
665 // If `obj` is equal to `out` or `ref`, it means the initial object
666 // has been overwritten by (or after) the heap object reference load
667 // to be instrumented, e.g.:
668 //
669 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000670 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000671 //
672 // In that case, we have lost the information about the original
673 // object, and the emitted read barrier cannot work properly.
674 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
675 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
676 }
677
678 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
679 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
680 LocationSummary* locations = instruction_->GetLocations();
681 Primitive::Type type = Primitive::kPrimNot;
682 DCHECK(locations->CanCall());
683 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100684 DCHECK(instruction_->IsInstanceFieldGet() ||
685 instruction_->IsStaticFieldGet() ||
686 instruction_->IsArrayGet() ||
687 instruction_->IsInstanceOf() ||
688 instruction_->IsCheckCast() ||
689 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain44015862016-01-22 11:47:17 +0000690 instruction_->GetLocations()->Intrinsified()))
691 << "Unexpected instruction in read barrier for heap reference slow path: "
692 << instruction_->DebugName();
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000693 // The read barrier instrumentation does not support the
694 // HArm64IntermediateAddress instruction yet.
695 DCHECK(!(instruction_->IsArrayGet() &&
696 instruction_->AsArrayGet()->GetArray()->IsArm64IntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697
698 __ Bind(GetEntryLabel());
699
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000700 SaveLiveRegisters(codegen, locations);
701
702 // We may have to change the index's value, but as `index_` is a
703 // constant member (like other "inputs" of this slow path),
704 // introduce a copy of it, `index`.
705 Location index = index_;
706 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100707 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000708 if (instruction_->IsArrayGet()) {
709 // Compute the actual memory offset and store it in `index`.
710 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
711 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
712 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
713 // We are about to change the value of `index_reg` (see the
714 // calls to vixl::MacroAssembler::Lsl and
715 // vixl::MacroAssembler::Mov below), but it has
716 // not been saved by the previous call to
717 // art::SlowPathCode::SaveLiveRegisters, as it is a
718 // callee-save register --
719 // art::SlowPathCode::SaveLiveRegisters does not consider
720 // callee-save registers, as it has been designed with the
721 // assumption that callee-save registers are supposed to be
722 // handled by the called function. So, as a callee-save
723 // register, `index_reg` _would_ eventually be saved onto
724 // the stack, but it would be too late: we would have
725 // changed its value earlier. Therefore, we manually save
726 // it here into another freely available register,
727 // `free_reg`, chosen of course among the caller-save
728 // registers (as a callee-save `free_reg` register would
729 // exhibit the same problem).
730 //
731 // Note we could have requested a temporary register from
732 // the register allocator instead; but we prefer not to, as
733 // this is a slow path, and we know we can find a
734 // caller-save register that is available.
735 Register free_reg = FindAvailableCallerSaveRegister(codegen);
736 __ Mov(free_reg.W(), index_reg);
737 index_reg = free_reg;
738 index = LocationFrom(index_reg);
739 } else {
740 // The initial register stored in `index_` has already been
741 // saved in the call to art::SlowPathCode::SaveLiveRegisters
742 // (as it is not a callee-save register), so we can freely
743 // use it.
744 }
745 // Shifting the index value contained in `index_reg` by the scale
746 // factor (2) cannot overflow in practice, as the runtime is
747 // unable to allocate object arrays with a size larger than
748 // 2^26 - 1 (that is, 2^28 - 4 bytes).
749 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
750 static_assert(
751 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
752 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
753 __ Add(index_reg, index_reg, Operand(offset_));
754 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100755 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
756 // intrinsics, `index_` is not shifted by a scale factor of 2
757 // (as in the case of ArrayGet), as it is actually an offset
758 // to an object field within an object.
759 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000760 DCHECK(instruction_->GetLocations()->Intrinsified());
761 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
762 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
763 << instruction_->AsInvoke()->GetIntrinsic();
764 DCHECK_EQ(offset_, 0U);
765 DCHECK(index_.IsRegisterPair());
766 // UnsafeGet's offset location is a register pair, the low
767 // part contains the correct offset.
768 index = index_.ToLow();
769 }
770 }
771
772 // We're moving two or three locations to locations that could
773 // overlap, so we need a parallel move resolver.
774 InvokeRuntimeCallingConvention calling_convention;
775 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
776 parallel_move.AddMove(ref_,
777 LocationFrom(calling_convention.GetRegisterAt(0)),
778 type,
779 nullptr);
780 parallel_move.AddMove(obj_,
781 LocationFrom(calling_convention.GetRegisterAt(1)),
782 type,
783 nullptr);
784 if (index.IsValid()) {
785 parallel_move.AddMove(index,
786 LocationFrom(calling_convention.GetRegisterAt(2)),
787 Primitive::kPrimInt,
788 nullptr);
789 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
790 } else {
791 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
792 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
793 }
794 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
795 instruction_,
796 instruction_->GetDexPc(),
797 this);
798 CheckEntrypointTypes<
799 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
800 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
801
802 RestoreLiveRegisters(codegen, locations);
803
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000804 __ B(GetExitLabel());
805 }
806
807 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
808
809 private:
810 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100811 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
812 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000813 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
814 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
815 return Register(VIXLRegCodeFromART(i), kXRegSize);
816 }
817 }
818 // We shall never fail to find a free caller-save register, as
819 // there are more than two core caller-save registers on ARM64
820 // (meaning it is possible to find one which is different from
821 // `ref` and `obj`).
822 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
823 LOG(FATAL) << "Could not find a free register";
824 UNREACHABLE();
825 }
826
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000827 const Location out_;
828 const Location ref_;
829 const Location obj_;
830 const uint32_t offset_;
831 // An additional location containing an index to an array.
832 // Only used for HArrayGet and the UnsafeGetObject &
833 // UnsafeGetObjectVolatile intrinsics.
834 const Location index_;
835
836 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
837};
838
839// Slow path generating a read barrier for a GC root.
840class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
841 public:
842 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000843 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000844 DCHECK(kEmitCompilerReadBarrier);
845 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000846
847 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
848 LocationSummary* locations = instruction_->GetLocations();
849 Primitive::Type type = Primitive::kPrimNot;
850 DCHECK(locations->CanCall());
851 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000852 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
853 << "Unexpected instruction in read barrier for GC root slow path: "
854 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000855
856 __ Bind(GetEntryLabel());
857 SaveLiveRegisters(codegen, locations);
858
859 InvokeRuntimeCallingConvention calling_convention;
860 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
861 // The argument of the ReadBarrierForRootSlow is not a managed
862 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
863 // thus we need a 64-bit move here, and we cannot use
864 //
865 // arm64_codegen->MoveLocation(
866 // LocationFrom(calling_convention.GetRegisterAt(0)),
867 // root_,
868 // type);
869 //
870 // which would emit a 32-bit move, as `type` is a (32-bit wide)
871 // reference type (`Primitive::kPrimNot`).
872 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
873 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
874 instruction_,
875 instruction_->GetDexPc(),
876 this);
877 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
878 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
879
880 RestoreLiveRegisters(codegen, locations);
881 __ B(GetExitLabel());
882 }
883
884 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
885
886 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000887 const Location out_;
888 const Location root_;
889
890 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
891};
892
Alexandre Rames5319def2014-10-23 10:03:10 +0100893#undef __
894
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100895Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100896 Location next_location;
897 if (type == Primitive::kPrimVoid) {
898 LOG(FATAL) << "Unreachable type " << type;
899 }
900
Alexandre Rames542361f2015-01-29 16:57:31 +0000901 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100902 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
903 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000904 } else if (!Primitive::IsFloatingPointType(type) &&
905 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000906 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
907 } else {
908 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000909 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
910 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100911 }
912
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000913 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000914 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100915 return next_location;
916}
917
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100918Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100919 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100920}
921
Serban Constantinescu579885a2015-02-22 20:51:33 +0000922CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
923 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100924 const CompilerOptions& compiler_options,
925 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100926 : CodeGenerator(graph,
927 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000928 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000929 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100930 callee_saved_core_registers.GetList(),
931 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100932 compiler_options,
933 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100934 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800935 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100936 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000937 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000938 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100939 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000940 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000941 uint32_literals_(std::less<uint32_t>(),
942 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100943 uint64_literals_(std::less<uint64_t>(),
944 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 method_patches_(MethodReferenceComparator(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
947 call_patches_(MethodReferenceComparator(),
948 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
949 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000950 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
951 boot_image_string_patches_(StringReferenceValueComparator(),
952 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
953 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100954 boot_image_type_patches_(TypeReferenceValueComparator(),
955 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
956 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000957 boot_image_address_patches_(std::less<uint32_t>(),
958 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000959 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000960 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000961}
Alexandre Rames5319def2014-10-23 10:03:10 +0100962
Alexandre Rames67555f72014-11-18 10:55:16 +0000963#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100964
Zheng Xu3927c8b2015-11-18 17:46:25 +0800965void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100966 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800967 jump_table->EmitTable(this);
968 }
969}
970
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000971void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800972 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000973 // Ensure we emit the literal pool.
974 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000975
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000976 CodeGenerator::Finalize(allocator);
977}
978
Zheng Xuad4450e2015-04-17 18:48:56 +0800979void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
980 // Note: There are 6 kinds of moves:
981 // 1. constant -> GPR/FPR (non-cycle)
982 // 2. constant -> stack (non-cycle)
983 // 3. GPR/FPR -> GPR/FPR
984 // 4. GPR/FPR -> stack
985 // 5. stack -> GPR/FPR
986 // 6. stack -> stack (non-cycle)
987 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
988 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
989 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
990 // dependency.
991 vixl_temps_.Open(GetVIXLAssembler());
992}
993
994void ParallelMoveResolverARM64::FinishEmitNativeCode() {
995 vixl_temps_.Close();
996}
997
998Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
999 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
1000 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
1001 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
1002 Location scratch = GetScratchLocation(kind);
1003 if (!scratch.Equals(Location::NoLocation())) {
1004 return scratch;
1005 }
1006 // Allocate from VIXL temp registers.
1007 if (kind == Location::kRegister) {
1008 scratch = LocationFrom(vixl_temps_.AcquireX());
1009 } else {
1010 DCHECK(kind == Location::kFpuRegister);
1011 scratch = LocationFrom(vixl_temps_.AcquireD());
1012 }
1013 AddScratchLocation(scratch);
1014 return scratch;
1015}
1016
1017void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1018 if (loc.IsRegister()) {
1019 vixl_temps_.Release(XRegisterFrom(loc));
1020 } else {
1021 DCHECK(loc.IsFpuRegister());
1022 vixl_temps_.Release(DRegisterFrom(loc));
1023 }
1024 RemoveScratchLocation(loc);
1025}
1026
Alexandre Rames3e69f162014-12-10 10:36:50 +00001027void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001028 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001029 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001030}
1031
Alexandre Rames5319def2014-10-23 10:03:10 +01001032void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001033 MacroAssembler* masm = GetVIXLAssembler();
1034 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001035 __ Bind(&frame_entry_label_);
1036
Serban Constantinescu02164b32014-11-13 14:05:07 +00001037 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1038 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001039 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001040 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001041 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001042 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 __ Ldr(wzr, MemOperand(temp, 0));
1044 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001045 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001046
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001047 if (!HasEmptyFrame()) {
1048 int frame_size = GetFrameSize();
1049 // Stack layout:
1050 // sp[frame_size - 8] : lr.
1051 // ... : other preserved core registers.
1052 // ... : other preserved fp registers.
1053 // ... : reserved frame space.
1054 // sp[0] : current method.
1055 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001057 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1058 frame_size - GetCoreSpillSize());
1059 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1060 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001061 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001062}
1063
1064void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001065 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001066 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001067 if (!HasEmptyFrame()) {
1068 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001069 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1070 frame_size - FrameEntrySpillSize());
1071 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1072 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001073 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001075 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001076 __ Ret();
1077 GetAssembler()->cfi().RestoreState();
1078 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001079}
1080
Scott Wakeling97c72b72016-06-24 16:19:36 +01001081CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001082 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001083 return CPURegList(CPURegister::kRegister, kXRegSize,
1084 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001085}
1086
Scott Wakeling97c72b72016-06-24 16:19:36 +01001087CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001088 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1089 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001090 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1091 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001092}
1093
Alexandre Rames5319def2014-10-23 10:03:10 +01001094void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1095 __ Bind(GetLabelOf(block));
1096}
1097
Calin Juravle175dc732015-08-25 15:42:32 +01001098void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1099 DCHECK(location.IsRegister());
1100 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1101}
1102
Calin Juravlee460d1d2015-09-29 04:52:17 +01001103void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1104 if (location.IsRegister()) {
1105 locations->AddTemp(location);
1106 } else {
1107 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1108 }
1109}
1110
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001112 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001113 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001114 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001115 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001116 if (value_can_be_null) {
1117 __ Cbz(value, &done);
1118 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001119 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64WordSize>().Int32Value()));
1120 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001121 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001122 if (value_can_be_null) {
1123 __ Bind(&done);
1124 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001125}
1126
David Brazdil58282f42016-01-14 12:45:10 +00001127void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001128 // Blocked core registers:
1129 // lr : Runtime reserved.
1130 // tr : Runtime reserved.
1131 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1132 // ip1 : VIXL core temp.
1133 // ip0 : VIXL core temp.
1134 //
1135 // Blocked fp registers:
1136 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001137 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1138 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001139 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001140 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001141 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001142
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001143 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001144 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001145 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001146 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001147
David Brazdil58282f42016-01-14 12:45:10 +00001148 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001149 // Stubs do not save callee-save floating point registers. If the graph
1150 // is debuggable, we need to deal with these registers differently. For
1151 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001152 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1153 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001154 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001155 }
1156 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001157}
1158
Alexandre Rames3e69f162014-12-10 10:36:50 +00001159size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1160 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1161 __ Str(reg, MemOperand(sp, stack_index));
1162 return kArm64WordSize;
1163}
1164
1165size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1166 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1167 __ Ldr(reg, MemOperand(sp, stack_index));
1168 return kArm64WordSize;
1169}
1170
1171size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1172 FPRegister reg = FPRegister(reg_id, kDRegSize);
1173 __ Str(reg, MemOperand(sp, stack_index));
1174 return kArm64WordSize;
1175}
1176
1177size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1178 FPRegister reg = FPRegister(reg_id, kDRegSize);
1179 __ Ldr(reg, MemOperand(sp, stack_index));
1180 return kArm64WordSize;
1181}
1182
Alexandre Rames5319def2014-10-23 10:03:10 +01001183void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001184 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001185}
1186
1187void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001188 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001189}
1190
Alexandre Rames67555f72014-11-18 10:55:16 +00001191void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001192 if (constant->IsIntConstant()) {
1193 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1194 } else if (constant->IsLongConstant()) {
1195 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1196 } else if (constant->IsNullConstant()) {
1197 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001198 } else if (constant->IsFloatConstant()) {
1199 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1200 } else {
1201 DCHECK(constant->IsDoubleConstant());
1202 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1203 }
1204}
1205
Alexandre Rames3e69f162014-12-10 10:36:50 +00001206
1207static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1208 DCHECK(constant.IsConstant());
1209 HConstant* cst = constant.GetConstant();
1210 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001211 // Null is mapped to a core W register, which we associate with kPrimInt.
1212 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1214 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1215 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1216}
1217
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218void CodeGeneratorARM64::MoveLocation(Location destination,
1219 Location source,
1220 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001221 if (source.Equals(destination)) {
1222 return;
1223 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001224
1225 // A valid move can always be inferred from the destination and source
1226 // locations. When moving from and to a register, the argument type can be
1227 // used to generate 32bit instead of 64bit moves. In debug mode we also
1228 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001229 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230
1231 if (destination.IsRegister() || destination.IsFpuRegister()) {
1232 if (unspecified_type) {
1233 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1234 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001235 (src_cst != nullptr && (src_cst->IsIntConstant()
1236 || src_cst->IsFloatConstant()
1237 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001238 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001239 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001240 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001241 // If the source is a double stack slot or a 64bit constant, a 64bit
1242 // type is appropriate. Else the source is a register, and since the
1243 // type has not been specified, we chose a 64bit type to force a 64bit
1244 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001245 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001246 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1249 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1250 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001251 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1252 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1253 __ Ldr(dst, StackOperandFrom(source));
1254 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001255 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001256 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001257 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001258 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001260 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001261 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001262 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1263 ? Primitive::kPrimLong
1264 : Primitive::kPrimInt;
1265 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1266 }
1267 } else {
1268 DCHECK(source.IsFpuRegister());
1269 if (destination.IsRegister()) {
1270 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1271 ? Primitive::kPrimDouble
1272 : Primitive::kPrimFloat;
1273 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1274 } else {
1275 DCHECK(destination.IsFpuRegister());
1276 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001277 }
1278 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001279 } else { // The destination is not a register. It must be a stack slot.
1280 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1281 if (source.IsRegister() || source.IsFpuRegister()) {
1282 if (unspecified_type) {
1283 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001284 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001285 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001286 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001287 }
1288 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001289 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1290 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1291 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001292 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001293 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1294 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001295 UseScratchRegisterScope temps(GetVIXLAssembler());
1296 HConstant* src_cst = source.GetConstant();
1297 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001298 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001299 temp = temps.AcquireW();
1300 } else if (src_cst->IsLongConstant()) {
1301 temp = temps.AcquireX();
1302 } else if (src_cst->IsFloatConstant()) {
1303 temp = temps.AcquireS();
1304 } else {
1305 DCHECK(src_cst->IsDoubleConstant());
1306 temp = temps.AcquireD();
1307 }
1308 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001309 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001310 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001311 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001312 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001313 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001314 // There is generally less pressure on FP registers.
1315 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001316 __ Ldr(temp, StackOperandFrom(source));
1317 __ Str(temp, StackOperandFrom(destination));
1318 }
1319 }
1320}
1321
1322void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001323 CPURegister dst,
1324 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001325 switch (type) {
1326 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001327 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001328 break;
1329 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001330 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001331 break;
1332 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001334 break;
1335 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001336 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001337 break;
1338 case Primitive::kPrimInt:
1339 case Primitive::kPrimNot:
1340 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001341 case Primitive::kPrimFloat:
1342 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001343 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001344 __ Ldr(dst, src);
1345 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001346 case Primitive::kPrimVoid:
1347 LOG(FATAL) << "Unreachable type " << type;
1348 }
1349}
1350
Calin Juravle77520bc2015-01-12 18:45:46 +00001351void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001352 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001353 const MemOperand& src,
1354 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001355 MacroAssembler* masm = GetVIXLAssembler();
1356 BlockPoolsScope block_pools(masm);
1357 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001358 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001359 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001360
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001361 DCHECK(!src.IsPreIndex());
1362 DCHECK(!src.IsPostIndex());
1363
1364 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001365 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001366 MemOperand base = MemOperand(temp_base);
1367 switch (type) {
1368 case Primitive::kPrimBoolean:
1369 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001370 if (needs_null_check) {
1371 MaybeRecordImplicitNullCheck(instruction);
1372 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001373 break;
1374 case Primitive::kPrimByte:
1375 __ Ldarb(Register(dst), 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 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1380 break;
1381 case Primitive::kPrimChar:
1382 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001383 if (needs_null_check) {
1384 MaybeRecordImplicitNullCheck(instruction);
1385 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001386 break;
1387 case Primitive::kPrimShort:
1388 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001389 if (needs_null_check) {
1390 MaybeRecordImplicitNullCheck(instruction);
1391 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001392 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1393 break;
1394 case Primitive::kPrimInt:
1395 case Primitive::kPrimNot:
1396 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001397 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001398 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001399 if (needs_null_check) {
1400 MaybeRecordImplicitNullCheck(instruction);
1401 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001402 break;
1403 case Primitive::kPrimFloat:
1404 case Primitive::kPrimDouble: {
1405 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001406 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001407
1408 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1409 __ Ldar(temp, base);
Roland Levillain44015862016-01-22 11:47:17 +00001410 if (needs_null_check) {
1411 MaybeRecordImplicitNullCheck(instruction);
1412 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001413 __ Fmov(FPRegister(dst), temp);
1414 break;
1415 }
1416 case Primitive::kPrimVoid:
1417 LOG(FATAL) << "Unreachable type " << type;
1418 }
1419}
1420
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001422 CPURegister src,
1423 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001424 switch (type) {
1425 case Primitive::kPrimBoolean:
1426 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001427 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001428 break;
1429 case Primitive::kPrimChar:
1430 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001431 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001432 break;
1433 case Primitive::kPrimInt:
1434 case Primitive::kPrimNot:
1435 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001436 case Primitive::kPrimFloat:
1437 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001438 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001439 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001440 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001441 case Primitive::kPrimVoid:
1442 LOG(FATAL) << "Unreachable type " << type;
1443 }
1444}
1445
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001446void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1447 CPURegister src,
1448 const MemOperand& dst) {
1449 UseScratchRegisterScope temps(GetVIXLAssembler());
1450 Register temp_base = temps.AcquireX();
1451
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001452 DCHECK(!dst.IsPreIndex());
1453 DCHECK(!dst.IsPostIndex());
1454
1455 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001456 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001457 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001458 MemOperand base = MemOperand(temp_base);
1459 switch (type) {
1460 case Primitive::kPrimBoolean:
1461 case Primitive::kPrimByte:
1462 __ Stlrb(Register(src), base);
1463 break;
1464 case Primitive::kPrimChar:
1465 case Primitive::kPrimShort:
1466 __ Stlrh(Register(src), base);
1467 break;
1468 case Primitive::kPrimInt:
1469 case Primitive::kPrimNot:
1470 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001471 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001472 __ Stlr(Register(src), base);
1473 break;
1474 case Primitive::kPrimFloat:
1475 case Primitive::kPrimDouble: {
1476 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001477 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001478
1479 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1480 __ Fmov(temp, FPRegister(src));
1481 __ Stlr(temp, base);
1482 break;
1483 }
1484 case Primitive::kPrimVoid:
1485 LOG(FATAL) << "Unreachable type " << type;
1486 }
1487}
1488
Calin Juravle175dc732015-08-25 15:42:32 +01001489void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1490 HInstruction* instruction,
1491 uint32_t dex_pc,
1492 SlowPathCode* slow_path) {
1493 InvokeRuntime(GetThreadOffset<kArm64WordSize>(entrypoint).Int32Value(),
1494 instruction,
1495 dex_pc,
1496 slow_path);
1497}
1498
Alexandre Rames67555f72014-11-18 10:55:16 +00001499void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1500 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001501 uint32_t dex_pc,
1502 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001503 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001504 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001505 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1506 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001507 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001508}
1509
1510void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001511 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001512 UseScratchRegisterScope temps(GetVIXLAssembler());
1513 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001514 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1515
Serban Constantinescu02164b32014-11-13 14:05:07 +00001516 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001517 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1518 __ Add(temp, class_reg, status_offset);
1519 __ Ldar(temp, HeapOperand(temp));
1520 __ Cmp(temp, mirror::Class::kStatusInitialized);
1521 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001522 __ Bind(slow_path->GetExitLabel());
1523}
Alexandre Rames5319def2014-10-23 10:03:10 +01001524
Roland Levillain44015862016-01-22 11:47:17 +00001525void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001526 BarrierType type = BarrierAll;
1527
1528 switch (kind) {
1529 case MemBarrierKind::kAnyAny:
1530 case MemBarrierKind::kAnyStore: {
1531 type = BarrierAll;
1532 break;
1533 }
1534 case MemBarrierKind::kLoadAny: {
1535 type = BarrierReads;
1536 break;
1537 }
1538 case MemBarrierKind::kStoreStore: {
1539 type = BarrierWrites;
1540 break;
1541 }
1542 default:
1543 LOG(FATAL) << "Unexpected memory barrier " << kind;
1544 }
1545 __ Dmb(InnerShareable, type);
1546}
1547
Serban Constantinescu02164b32014-11-13 14:05:07 +00001548void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1549 HBasicBlock* successor) {
1550 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001551 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1552 if (slow_path == nullptr) {
1553 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1554 instruction->SetSlowPath(slow_path);
1555 codegen_->AddSlowPath(slow_path);
1556 if (successor != nullptr) {
1557 DCHECK(successor->IsLoopHeader());
1558 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1559 }
1560 } else {
1561 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1562 }
1563
Serban Constantinescu02164b32014-11-13 14:05:07 +00001564 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1565 Register temp = temps.AcquireW();
1566
1567 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64WordSize>().SizeValue()));
1568 if (successor == nullptr) {
1569 __ Cbnz(temp, slow_path->GetEntryLabel());
1570 __ Bind(slow_path->GetReturnLabel());
1571 } else {
1572 __ Cbz(temp, codegen_->GetLabelOf(successor));
1573 __ B(slow_path->GetEntryLabel());
1574 // slow_path will return to GetLabelOf(successor).
1575 }
1576}
1577
Alexandre Rames5319def2014-10-23 10:03:10 +01001578InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1579 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001580 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001581 assembler_(codegen->GetAssembler()),
1582 codegen_(codegen) {}
1583
1584#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001585 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001586
1587#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1588
1589enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001590 // Using a base helps identify when we hit such breakpoints.
1591 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001592#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1593 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1594#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1595};
1596
1597#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001598 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001599 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1600 } \
1601 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1602 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1603 locations->SetOut(Location::Any()); \
1604 }
1605 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1606#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1607
1608#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001609#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001610
Alexandre Rames67555f72014-11-18 10:55:16 +00001611void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001612 DCHECK_EQ(instr->InputCount(), 2U);
1613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1614 Primitive::Type type = instr->GetResultType();
1615 switch (type) {
1616 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001617 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001618 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001619 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001621 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001622
1623 case Primitive::kPrimFloat:
1624 case Primitive::kPrimDouble:
1625 locations->SetInAt(0, Location::RequiresFpuRegister());
1626 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001627 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001629
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001631 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001632 }
1633}
1634
Alexandre Rames09a99962015-04-15 11:47:56 +01001635void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001636 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1637
1638 bool object_field_get_with_read_barrier =
1639 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001640 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001641 new (GetGraph()->GetArena()) LocationSummary(instruction,
1642 object_field_get_with_read_barrier ?
1643 LocationSummary::kCallOnSlowPath :
1644 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001645 locations->SetInAt(0, Location::RequiresRegister());
1646 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1647 locations->SetOut(Location::RequiresFpuRegister());
1648 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001649 // The output overlaps for an object field get when read barriers
1650 // are enabled: we do not want the load to overwrite the object's
1651 // location, as we need it to emit the read barrier.
1652 locations->SetOut(
1653 Location::RequiresRegister(),
1654 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001655 }
1656}
1657
1658void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1659 const FieldInfo& field_info) {
1660 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001661 LocationSummary* locations = instruction->GetLocations();
1662 Location base_loc = locations->InAt(0);
1663 Location out = locations->Out();
1664 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001665 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001666 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001667 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001668
Roland Levillain44015862016-01-22 11:47:17 +00001669 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1670 // Object FieldGet with Baker's read barrier case.
1671 MacroAssembler* masm = GetVIXLAssembler();
1672 UseScratchRegisterScope temps(masm);
1673 // /* HeapReference<Object> */ out = *(base + offset)
1674 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1675 Register temp = temps.AcquireW();
1676 // Note that potential implicit null checks are handled in this
1677 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1678 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1679 instruction,
1680 out,
1681 base,
1682 offset,
1683 temp,
1684 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001685 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001686 } else {
1687 // General case.
1688 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001689 // Note that a potential implicit null check is handled in this
1690 // CodeGeneratorARM64::LoadAcquire call.
1691 // NB: LoadAcquire will record the pc info if needed.
1692 codegen_->LoadAcquire(
1693 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001695 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001696 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001697 }
Roland Levillain44015862016-01-22 11:47:17 +00001698 if (field_type == Primitive::kPrimNot) {
1699 // If read barriers are enabled, emit read barriers other than
1700 // Baker's using a slow path (and also unpoison the loaded
1701 // reference, if heap poisoning is enabled).
1702 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1703 }
Roland Levillain4d027112015-07-01 15:41:14 +01001704 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001705}
1706
1707void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1708 LocationSummary* locations =
1709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1710 locations->SetInAt(0, Location::RequiresRegister());
1711 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1712 locations->SetInAt(1, Location::RequiresFpuRegister());
1713 } else {
1714 locations->SetInAt(1, Location::RequiresRegister());
1715 }
1716}
1717
1718void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001719 const FieldInfo& field_info,
1720 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001721 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001722 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001723
1724 Register obj = InputRegisterAt(instruction, 0);
1725 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001726 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001727 Offset offset = field_info.GetFieldOffset();
1728 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001729
Roland Levillain4d027112015-07-01 15:41:14 +01001730 {
1731 // We use a block to end the scratch scope before the write barrier, thus
1732 // freeing the temporary registers so they can be used in `MarkGCCard`.
1733 UseScratchRegisterScope temps(GetVIXLAssembler());
1734
1735 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1736 DCHECK(value.IsW());
1737 Register temp = temps.AcquireW();
1738 __ Mov(temp, value.W());
1739 GetAssembler()->PoisonHeapReference(temp.W());
1740 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001741 }
Roland Levillain4d027112015-07-01 15:41:14 +01001742
1743 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001744 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1745 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001746 } else {
1747 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1748 codegen_->MaybeRecordImplicitNullCheck(instruction);
1749 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001750 }
1751
1752 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001753 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001754 }
1755}
1756
Alexandre Rames67555f72014-11-18 10:55:16 +00001757void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001758 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001759
1760 switch (type) {
1761 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001762 case Primitive::kPrimLong: {
1763 Register dst = OutputRegister(instr);
1764 Register lhs = InputRegisterAt(instr, 0);
1765 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001766 if (instr->IsAdd()) {
1767 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001768 } else if (instr->IsAnd()) {
1769 __ And(dst, lhs, rhs);
1770 } else if (instr->IsOr()) {
1771 __ Orr(dst, lhs, rhs);
1772 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001773 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001774 } else if (instr->IsRor()) {
1775 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001776 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001777 __ Ror(dst, lhs, shift);
1778 } else {
1779 // Ensure shift distance is in the same size register as the result. If
1780 // we are rotating a long and the shift comes in a w register originally,
1781 // we don't need to sxtw for use as an x since the shift distances are
1782 // all & reg_bits - 1.
1783 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1784 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001785 } else {
1786 DCHECK(instr->IsXor());
1787 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001788 }
1789 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001790 }
1791 case Primitive::kPrimFloat:
1792 case Primitive::kPrimDouble: {
1793 FPRegister dst = OutputFPRegister(instr);
1794 FPRegister lhs = InputFPRegisterAt(instr, 0);
1795 FPRegister rhs = InputFPRegisterAt(instr, 1);
1796 if (instr->IsAdd()) {
1797 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001799 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001800 } else {
1801 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001804 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001806 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001807 }
1808}
1809
Serban Constantinescu02164b32014-11-13 14:05:07 +00001810void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1811 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1812
1813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1814 Primitive::Type type = instr->GetResultType();
1815 switch (type) {
1816 case Primitive::kPrimInt:
1817 case Primitive::kPrimLong: {
1818 locations->SetInAt(0, Location::RequiresRegister());
1819 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1820 locations->SetOut(Location::RequiresRegister());
1821 break;
1822 }
1823 default:
1824 LOG(FATAL) << "Unexpected shift type " << type;
1825 }
1826}
1827
1828void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1829 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1830
1831 Primitive::Type type = instr->GetType();
1832 switch (type) {
1833 case Primitive::kPrimInt:
1834 case Primitive::kPrimLong: {
1835 Register dst = OutputRegister(instr);
1836 Register lhs = InputRegisterAt(instr, 0);
1837 Operand rhs = InputOperandAt(instr, 1);
1838 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001839 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001840 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001841 if (instr->IsShl()) {
1842 __ Lsl(dst, lhs, shift_value);
1843 } else if (instr->IsShr()) {
1844 __ Asr(dst, lhs, shift_value);
1845 } else {
1846 __ Lsr(dst, lhs, shift_value);
1847 }
1848 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001849 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001850
1851 if (instr->IsShl()) {
1852 __ Lsl(dst, lhs, rhs_reg);
1853 } else if (instr->IsShr()) {
1854 __ Asr(dst, lhs, rhs_reg);
1855 } else {
1856 __ Lsr(dst, lhs, rhs_reg);
1857 }
1858 }
1859 break;
1860 }
1861 default:
1862 LOG(FATAL) << "Unexpected shift operation type " << type;
1863 }
1864}
1865
Alexandre Rames5319def2014-10-23 10:03:10 +01001866void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001868}
1869
1870void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001871 HandleBinaryOp(instruction);
1872}
1873
1874void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1875 HandleBinaryOp(instruction);
1876}
1877
1878void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1879 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001880}
1881
Artem Serov7fc63502016-02-09 17:15:29 +00001882void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001883 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1885 locations->SetInAt(0, Location::RequiresRegister());
1886 // There is no immediate variant of negated bitwise instructions in AArch64.
1887 locations->SetInAt(1, Location::RequiresRegister());
1888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1889}
1890
Artem Serov7fc63502016-02-09 17:15:29 +00001891void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001892 Register dst = OutputRegister(instr);
1893 Register lhs = InputRegisterAt(instr, 0);
1894 Register rhs = InputRegisterAt(instr, 1);
1895
1896 switch (instr->GetOpKind()) {
1897 case HInstruction::kAnd:
1898 __ Bic(dst, lhs, rhs);
1899 break;
1900 case HInstruction::kOr:
1901 __ Orn(dst, lhs, rhs);
1902 break;
1903 case HInstruction::kXor:
1904 __ Eon(dst, lhs, rhs);
1905 break;
1906 default:
1907 LOG(FATAL) << "Unreachable";
1908 }
1909}
1910
Alexandre Rames8626b742015-11-25 16:28:08 +00001911void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1912 HArm64DataProcWithShifterOp* instruction) {
1913 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1914 instruction->GetType() == Primitive::kPrimLong);
1915 LocationSummary* locations =
1916 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1917 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1918 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1919 } else {
1920 locations->SetInAt(0, Location::RequiresRegister());
1921 }
1922 locations->SetInAt(1, Location::RequiresRegister());
1923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1924}
1925
1926void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1927 HArm64DataProcWithShifterOp* instruction) {
1928 Primitive::Type type = instruction->GetType();
1929 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1930 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1931 Register out = OutputRegister(instruction);
1932 Register left;
1933 if (kind != HInstruction::kNeg) {
1934 left = InputRegisterAt(instruction, 0);
1935 }
1936 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1937 // shifter operand operation, the IR generating `right_reg` (input to the type
1938 // conversion) can have a different type from the current instruction's type,
1939 // so we manually indicate the type.
1940 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001941 int64_t shift_amount = instruction->GetShiftAmount() &
1942 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001943
1944 Operand right_operand(0);
1945
1946 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1947 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1948 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1949 } else {
1950 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1951 }
1952
1953 // Logical binary operations do not support extension operations in the
1954 // operand. Note that VIXL would still manage if it was passed by generating
1955 // the extension as a separate instruction.
1956 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1957 DCHECK(!right_operand.IsExtendedRegister() ||
1958 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1959 kind != HInstruction::kNeg));
1960 switch (kind) {
1961 case HInstruction::kAdd:
1962 __ Add(out, left, right_operand);
1963 break;
1964 case HInstruction::kAnd:
1965 __ And(out, left, right_operand);
1966 break;
1967 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001968 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001969 __ Neg(out, right_operand);
1970 break;
1971 case HInstruction::kOr:
1972 __ Orr(out, left, right_operand);
1973 break;
1974 case HInstruction::kSub:
1975 __ Sub(out, left, right_operand);
1976 break;
1977 case HInstruction::kXor:
1978 __ Eor(out, left, right_operand);
1979 break;
1980 default:
1981 LOG(FATAL) << "Unexpected operation kind: " << kind;
1982 UNREACHABLE();
1983 }
1984}
1985
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001986void LocationsBuilderARM64::VisitArm64IntermediateAddress(HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001987 // The read barrier instrumentation does not support the
1988 // HArm64IntermediateAddress instruction yet.
1989 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1992 locations->SetInAt(0, Location::RequiresRegister());
1993 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1994 locations->SetOut(Location::RequiresRegister());
1995}
1996
1997void InstructionCodeGeneratorARM64::VisitArm64IntermediateAddress(
1998 HArm64IntermediateAddress* instruction) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001999 // The read barrier instrumentation does not support the
2000 // HArm64IntermediateAddress instruction yet.
2001 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002002 __ Add(OutputRegister(instruction),
2003 InputRegisterAt(instruction, 0),
2004 Operand(InputOperandAt(instruction, 1)));
2005}
2006
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002007void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002008 LocationSummary* locations =
2009 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002010 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2011 if (instr->GetOpKind() == HInstruction::kSub &&
2012 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002013 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002014 // Don't allocate register for Mneg instruction.
2015 } else {
2016 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2017 Location::RequiresRegister());
2018 }
2019 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2020 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2022}
2023
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002024void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002025 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002026 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2027 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002028
2029 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2030 // This fixup should be carried out for all multiply-accumulate instructions:
2031 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2032 if (instr->GetType() == Primitive::kPrimLong &&
2033 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2034 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035 vixl::aarch64::Instruction* prev =
2036 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002037 if (prev->IsLoadOrStore()) {
2038 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002039 vixl::aarch64::CodeBufferCheckScope scope(masm,
2040 kInstructionSize,
2041 vixl::aarch64::CodeBufferCheckScope::kCheck,
2042 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002043 __ nop();
2044 }
2045 }
2046
2047 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002048 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002049 __ Madd(res, mul_left, mul_right, accumulator);
2050 } else {
2051 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002052 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002053 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002054 __ Mneg(res, mul_left, mul_right);
2055 } else {
2056 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2057 __ Msub(res, mul_left, mul_right, accumulator);
2058 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002059 }
2060}
2061
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002062void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002063 bool object_array_get_with_read_barrier =
2064 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002065 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002066 new (GetGraph()->GetArena()) LocationSummary(instruction,
2067 object_array_get_with_read_barrier ?
2068 LocationSummary::kCallOnSlowPath :
2069 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002072 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2073 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2074 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002075 // The output overlaps in the case of an object array get with
2076 // read barriers enabled: we do not want the move to overwrite the
2077 // array's location, as we need it to emit the read barrier.
2078 locations->SetOut(
2079 Location::RequiresRegister(),
2080 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002081 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002082}
2083
2084void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002085 Primitive::Type type = instruction->GetType();
2086 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002087 LocationSummary* locations = instruction->GetLocations();
2088 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002089 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002090 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002091
Alexandre Ramesd921d642015-04-16 15:07:16 +01002092 MacroAssembler* masm = GetVIXLAssembler();
2093 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002094 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002095 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002096
Roland Levillain44015862016-01-22 11:47:17 +00002097 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2098 // Object ArrayGet with Baker's read barrier case.
2099 Register temp = temps.AcquireW();
2100 // The read barrier instrumentation does not support the
2101 // HArm64IntermediateAddress instruction yet.
2102 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
2103 // Note that a potential implicit null check is handled in the
2104 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2105 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2106 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002107 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002108 // General case.
2109 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002110 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002111 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2112 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002113 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002114 Register temp = temps.AcquireSameSizeAs(obj);
2115 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
2116 // The read barrier instrumentation does not support the
2117 // HArm64IntermediateAddress instruction yet.
2118 DCHECK(!kEmitCompilerReadBarrier);
2119 // We do not need to compute the intermediate address from the array: the
2120 // input instruction has done it already. See the comment in
2121 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2122 if (kIsDebugBuild) {
2123 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2124 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2125 }
2126 temp = obj;
2127 } else {
2128 __ Add(temp, obj, offset);
2129 }
2130 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2131 }
2132
2133 codegen_->Load(type, OutputCPURegister(instruction), source);
2134 codegen_->MaybeRecordImplicitNullCheck(instruction);
2135
2136 if (type == Primitive::kPrimNot) {
2137 static_assert(
2138 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2139 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2140 Location obj_loc = locations->InAt(0);
2141 if (index.IsConstant()) {
2142 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2143 } else {
2144 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2145 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002146 }
Roland Levillain4d027112015-07-01 15:41:14 +01002147 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002148}
2149
Alexandre Rames5319def2014-10-23 10:03:10 +01002150void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2152 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002154}
2155
2156void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002157 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002158 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002159 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002160 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002161}
2162
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002163void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002164 Primitive::Type value_type = instruction->GetComponentType();
2165
2166 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2167 bool object_array_set_with_read_barrier =
2168 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002169 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2170 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002171 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2172 LocationSummary::kCallOnSlowPath :
2173 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002174 locations->SetInAt(0, Location::RequiresRegister());
2175 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002176 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002177 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002178 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002179 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002180 }
2181}
2182
2183void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2184 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002185 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002186 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002187 bool needs_write_barrier =
2188 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002189
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002190 Register array = InputRegisterAt(instruction, 0);
2191 CPURegister value = InputCPURegisterAt(instruction, 2);
2192 CPURegister source = value;
2193 Location index = locations->InAt(1);
2194 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2195 MemOperand destination = HeapOperand(array);
2196 MacroAssembler* masm = GetVIXLAssembler();
2197 BlockPoolsScope block_pools(masm);
2198
2199 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002200 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002201 if (index.IsConstant()) {
2202 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2203 destination = HeapOperand(array, offset);
2204 } else {
2205 UseScratchRegisterScope temps(masm);
2206 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002207 if (instruction->GetArray()->IsArm64IntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002208 // The read barrier instrumentation does not support the
2209 // HArm64IntermediateAddress instruction yet.
2210 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002211 // We do not need to compute the intermediate address from the array: the
2212 // input instruction has done it already. See the comment in
2213 // `InstructionSimplifierArm64::TryExtractArrayAccessAddress()`.
2214 if (kIsDebugBuild) {
2215 HArm64IntermediateAddress* tmp = instruction->GetArray()->AsArm64IntermediateAddress();
2216 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2217 }
2218 temp = array;
2219 } else {
2220 __ Add(temp, array, offset);
2221 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002222 destination = HeapOperand(temp,
2223 XRegisterFrom(index),
2224 LSL,
2225 Primitive::ComponentSizeShift(value_type));
2226 }
2227 codegen_->Store(value_type, value, destination);
2228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002229 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002230 DCHECK(needs_write_barrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002231 DCHECK(!instruction->GetArray()->IsArm64IntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002232 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002233 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002234 {
2235 // We use a block to end the scratch scope before the write barrier, thus
2236 // freeing the temporary registers so they can be used in `MarkGCCard`.
2237 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002238 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002239 if (index.IsConstant()) {
2240 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002241 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002242 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002243 destination = HeapOperand(temp,
2244 XRegisterFrom(index),
2245 LSL,
2246 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002247 }
2248
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002249 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2250 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2251 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2252
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002253 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002254 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2255 codegen_->AddSlowPath(slow_path);
2256 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002257 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002258 __ Cbnz(Register(value), &non_zero);
2259 if (!index.IsConstant()) {
2260 __ Add(temp, array, offset);
2261 }
2262 __ Str(wzr, destination);
2263 codegen_->MaybeRecordImplicitNullCheck(instruction);
2264 __ B(&done);
2265 __ Bind(&non_zero);
2266 }
2267
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002268 if (kEmitCompilerReadBarrier) {
2269 // When read barriers are enabled, the type checking
2270 // instrumentation requires two read barriers:
2271 //
2272 // __ Mov(temp2, temp);
2273 // // /* HeapReference<Class> */ temp = temp->component_type_
2274 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002275 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002276 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2277 //
2278 // // /* HeapReference<Class> */ temp2 = value->klass_
2279 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002280 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002281 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2282 //
2283 // __ Cmp(temp, temp2);
2284 //
2285 // However, the second read barrier may trash `temp`, as it
2286 // is a temporary register, and as such would not be saved
2287 // along with live registers before calling the runtime (nor
2288 // restored afterwards). So in this case, we bail out and
2289 // delegate the work to the array set slow path.
2290 //
2291 // TODO: Extend the register allocator to support a new
2292 // "(locally) live temp" location so as to avoid always
2293 // going into the slow path when read barriers are enabled.
2294 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002295 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002296 Register temp2 = temps.AcquireSameSizeAs(array);
2297 // /* HeapReference<Class> */ temp = array->klass_
2298 __ Ldr(temp, HeapOperand(array, class_offset));
2299 codegen_->MaybeRecordImplicitNullCheck(instruction);
2300 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2301
2302 // /* HeapReference<Class> */ temp = temp->component_type_
2303 __ Ldr(temp, HeapOperand(temp, component_offset));
2304 // /* HeapReference<Class> */ temp2 = value->klass_
2305 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2306 // If heap poisoning is enabled, no need to unpoison `temp`
2307 // nor `temp2`, as we are comparing two poisoned references.
2308 __ Cmp(temp, temp2);
2309
2310 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002311 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002312 __ B(eq, &do_put);
2313 // If heap poisoning is enabled, the `temp` reference has
2314 // not been unpoisoned yet; unpoison it now.
2315 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2316
2317 // /* HeapReference<Class> */ temp = temp->super_class_
2318 __ Ldr(temp, HeapOperand(temp, super_offset));
2319 // If heap poisoning is enabled, no need to unpoison
2320 // `temp`, as we are comparing against null below.
2321 __ Cbnz(temp, slow_path->GetEntryLabel());
2322 __ Bind(&do_put);
2323 } else {
2324 __ B(ne, slow_path->GetEntryLabel());
2325 }
2326 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002327 }
2328 }
2329
2330 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002331 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002332 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002333 __ Mov(temp2, value.W());
2334 GetAssembler()->PoisonHeapReference(temp2);
2335 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002336 }
2337
2338 if (!index.IsConstant()) {
2339 __ Add(temp, array, offset);
2340 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002341 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002342
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002343 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002344 codegen_->MaybeRecordImplicitNullCheck(instruction);
2345 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002346 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002347
2348 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2349
2350 if (done.IsLinked()) {
2351 __ Bind(&done);
2352 }
2353
2354 if (slow_path != nullptr) {
2355 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002356 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002357 }
2358}
2359
Alexandre Rames67555f72014-11-18 10:55:16 +00002360void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002361 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2362 ? LocationSummary::kCallOnSlowPath
2363 : LocationSummary::kNoCall;
2364 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002365 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002366 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002367 if (instruction->HasUses()) {
2368 locations->SetOut(Location::SameAsFirstInput());
2369 }
2370}
2371
2372void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002373 BoundsCheckSlowPathARM64* slow_path =
2374 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002375 codegen_->AddSlowPath(slow_path);
2376
2377 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2378 __ B(slow_path->GetEntryLabel(), hs);
2379}
2380
Alexandre Rames67555f72014-11-18 10:55:16 +00002381void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2382 LocationSummary* locations =
2383 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2384 locations->SetInAt(0, Location::RequiresRegister());
2385 if (check->HasUses()) {
2386 locations->SetOut(Location::SameAsFirstInput());
2387 }
2388}
2389
2390void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2391 // We assume the class is not null.
2392 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2393 check->GetLoadClass(), check, check->GetDexPc(), true);
2394 codegen_->AddSlowPath(slow_path);
2395 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2396}
2397
Roland Levillain1a653882016-03-18 18:05:57 +00002398static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2399 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2400 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2401}
2402
2403void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2404 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2405 Location rhs_loc = instruction->GetLocations()->InAt(1);
2406 if (rhs_loc.IsConstant()) {
2407 // 0.0 is the only immediate that can be encoded directly in
2408 // an FCMP instruction.
2409 //
2410 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2411 // specify that in a floating-point comparison, positive zero
2412 // and negative zero are considered equal, so we can use the
2413 // literal 0.0 for both cases here.
2414 //
2415 // Note however that some methods (Float.equal, Float.compare,
2416 // Float.compareTo, Double.equal, Double.compare,
2417 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2418 // StrictMath.min) consider 0.0 to be (strictly) greater than
2419 // -0.0. So if we ever translate calls to these methods into a
2420 // HCompare instruction, we must handle the -0.0 case with
2421 // care here.
2422 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2423 __ Fcmp(lhs_reg, 0.0);
2424 } else {
2425 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2426 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002427}
2428
Serban Constantinescu02164b32014-11-13 14:05:07 +00002429void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002430 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002431 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2432 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002433 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002434 case Primitive::kPrimBoolean:
2435 case Primitive::kPrimByte:
2436 case Primitive::kPrimShort:
2437 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002438 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002439 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002440 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002441 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002442 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2443 break;
2444 }
2445 case Primitive::kPrimFloat:
2446 case Primitive::kPrimDouble: {
2447 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002448 locations->SetInAt(1,
2449 IsFloatingPointZeroConstant(compare->InputAt(1))
2450 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2451 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002452 locations->SetOut(Location::RequiresRegister());
2453 break;
2454 }
2455 default:
2456 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2457 }
2458}
2459
2460void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2461 Primitive::Type in_type = compare->InputAt(0)->GetType();
2462
2463 // 0 if: left == right
2464 // 1 if: left > right
2465 // -1 if: left < right
2466 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002467 case Primitive::kPrimBoolean:
2468 case Primitive::kPrimByte:
2469 case Primitive::kPrimShort:
2470 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002471 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002472 case Primitive::kPrimLong: {
2473 Register result = OutputRegister(compare);
2474 Register left = InputRegisterAt(compare, 0);
2475 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002476 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002477 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2478 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002479 break;
2480 }
2481 case Primitive::kPrimFloat:
2482 case Primitive::kPrimDouble: {
2483 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002484 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002485 __ Cset(result, ne);
2486 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002487 break;
2488 }
2489 default:
2490 LOG(FATAL) << "Unimplemented compare type " << in_type;
2491 }
2492}
2493
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002494void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002496
2497 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2498 locations->SetInAt(0, Location::RequiresFpuRegister());
2499 locations->SetInAt(1,
2500 IsFloatingPointZeroConstant(instruction->InputAt(1))
2501 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2502 : Location::RequiresFpuRegister());
2503 } else {
2504 // Integer cases.
2505 locations->SetInAt(0, Location::RequiresRegister());
2506 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2507 }
2508
David Brazdilb3e773e2016-01-26 11:28:37 +00002509 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002511 }
2512}
2513
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002514void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002515 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002516 return;
2517 }
2518
2519 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002520 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002521 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002522
Roland Levillain7f63c522015-07-13 15:54:55 +00002523 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002524 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002525 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002526 } else {
2527 // Integer cases.
2528 Register lhs = InputRegisterAt(instruction, 0);
2529 Operand rhs = InputOperandAt(instruction, 1);
2530 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002531 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002532 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002533}
2534
2535#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2536 M(Equal) \
2537 M(NotEqual) \
2538 M(LessThan) \
2539 M(LessThanOrEqual) \
2540 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002541 M(GreaterThanOrEqual) \
2542 M(Below) \
2543 M(BelowOrEqual) \
2544 M(Above) \
2545 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002546#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002547void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2548void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002549FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002550#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002551#undef FOR_EACH_CONDITION_INSTRUCTION
2552
Zheng Xuc6667102015-05-15 16:08:45 +08002553void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2554 DCHECK(instruction->IsDiv() || instruction->IsRem());
2555
2556 LocationSummary* locations = instruction->GetLocations();
2557 Location second = locations->InAt(1);
2558 DCHECK(second.IsConstant());
2559
2560 Register out = OutputRegister(instruction);
2561 Register dividend = InputRegisterAt(instruction, 0);
2562 int64_t imm = Int64FromConstant(second.GetConstant());
2563 DCHECK(imm == 1 || imm == -1);
2564
2565 if (instruction->IsRem()) {
2566 __ Mov(out, 0);
2567 } else {
2568 if (imm == 1) {
2569 __ Mov(out, dividend);
2570 } else {
2571 __ Neg(out, dividend);
2572 }
2573 }
2574}
2575
2576void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(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());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002586 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002587 int ctz_imm = CTZ(abs_imm);
2588
2589 UseScratchRegisterScope temps(GetVIXLAssembler());
2590 Register temp = temps.AcquireSameSizeAs(out);
2591
2592 if (instruction->IsDiv()) {
2593 __ Add(temp, dividend, abs_imm - 1);
2594 __ Cmp(dividend, 0);
2595 __ Csel(out, temp, dividend, lt);
2596 if (imm > 0) {
2597 __ Asr(out, out, ctz_imm);
2598 } else {
2599 __ Neg(out, Operand(out, ASR, ctz_imm));
2600 }
2601 } else {
2602 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2603 __ Asr(temp, dividend, bits - 1);
2604 __ Lsr(temp, temp, bits - ctz_imm);
2605 __ Add(out, dividend, temp);
2606 __ And(out, out, abs_imm - 1);
2607 __ Sub(out, out, temp);
2608 }
2609}
2610
2611void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2612 DCHECK(instruction->IsDiv() || instruction->IsRem());
2613
2614 LocationSummary* locations = instruction->GetLocations();
2615 Location second = locations->InAt(1);
2616 DCHECK(second.IsConstant());
2617
2618 Register out = OutputRegister(instruction);
2619 Register dividend = InputRegisterAt(instruction, 0);
2620 int64_t imm = Int64FromConstant(second.GetConstant());
2621
2622 Primitive::Type type = instruction->GetResultType();
2623 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2624
2625 int64_t magic;
2626 int shift;
2627 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2628
2629 UseScratchRegisterScope temps(GetVIXLAssembler());
2630 Register temp = temps.AcquireSameSizeAs(out);
2631
2632 // temp = get_high(dividend * magic)
2633 __ Mov(temp, magic);
2634 if (type == Primitive::kPrimLong) {
2635 __ Smulh(temp, dividend, temp);
2636 } else {
2637 __ Smull(temp.X(), dividend, temp);
2638 __ Lsr(temp.X(), temp.X(), 32);
2639 }
2640
2641 if (imm > 0 && magic < 0) {
2642 __ Add(temp, temp, dividend);
2643 } else if (imm < 0 && magic > 0) {
2644 __ Sub(temp, temp, dividend);
2645 }
2646
2647 if (shift != 0) {
2648 __ Asr(temp, temp, shift);
2649 }
2650
2651 if (instruction->IsDiv()) {
2652 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2653 } else {
2654 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2655 // TODO: Strength reduction for msub.
2656 Register temp_imm = temps.AcquireSameSizeAs(out);
2657 __ Mov(temp_imm, imm);
2658 __ Msub(out, temp, temp_imm, dividend);
2659 }
2660}
2661
2662void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2663 DCHECK(instruction->IsDiv() || instruction->IsRem());
2664 Primitive::Type type = instruction->GetResultType();
2665 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2666
2667 LocationSummary* locations = instruction->GetLocations();
2668 Register out = OutputRegister(instruction);
2669 Location second = locations->InAt(1);
2670
2671 if (second.IsConstant()) {
2672 int64_t imm = Int64FromConstant(second.GetConstant());
2673
2674 if (imm == 0) {
2675 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2676 } else if (imm == 1 || imm == -1) {
2677 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002678 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002679 DivRemByPowerOfTwo(instruction);
2680 } else {
2681 DCHECK(imm <= -2 || imm >= 2);
2682 GenerateDivRemWithAnyConstant(instruction);
2683 }
2684 } else {
2685 Register dividend = InputRegisterAt(instruction, 0);
2686 Register divisor = InputRegisterAt(instruction, 1);
2687 if (instruction->IsDiv()) {
2688 __ Sdiv(out, dividend, divisor);
2689 } else {
2690 UseScratchRegisterScope temps(GetVIXLAssembler());
2691 Register temp = temps.AcquireSameSizeAs(out);
2692 __ Sdiv(temp, dividend, divisor);
2693 __ Msub(out, temp, divisor, dividend);
2694 }
2695 }
2696}
2697
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002698void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2699 LocationSummary* locations =
2700 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2701 switch (div->GetResultType()) {
2702 case Primitive::kPrimInt:
2703 case Primitive::kPrimLong:
2704 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002705 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002706 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2707 break;
2708
2709 case Primitive::kPrimFloat:
2710 case Primitive::kPrimDouble:
2711 locations->SetInAt(0, Location::RequiresFpuRegister());
2712 locations->SetInAt(1, Location::RequiresFpuRegister());
2713 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2718 }
2719}
2720
2721void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2722 Primitive::Type type = div->GetResultType();
2723 switch (type) {
2724 case Primitive::kPrimInt:
2725 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002726 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002727 break;
2728
2729 case Primitive::kPrimFloat:
2730 case Primitive::kPrimDouble:
2731 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2732 break;
2733
2734 default:
2735 LOG(FATAL) << "Unexpected div type " << type;
2736 }
2737}
2738
Alexandre Rames67555f72014-11-18 10:55:16 +00002739void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002740 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2741 ? LocationSummary::kCallOnSlowPath
2742 : LocationSummary::kNoCall;
2743 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002744 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2745 if (instruction->HasUses()) {
2746 locations->SetOut(Location::SameAsFirstInput());
2747 }
2748}
2749
2750void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2751 SlowPathCodeARM64* slow_path =
2752 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2753 codegen_->AddSlowPath(slow_path);
2754 Location value = instruction->GetLocations()->InAt(0);
2755
Alexandre Rames3e69f162014-12-10 10:36:50 +00002756 Primitive::Type type = instruction->GetType();
2757
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002758 if (!Primitive::IsIntegralType(type)) {
2759 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002760 return;
2761 }
2762
Alexandre Rames67555f72014-11-18 10:55:16 +00002763 if (value.IsConstant()) {
2764 int64_t divisor = Int64ConstantFrom(value);
2765 if (divisor == 0) {
2766 __ B(slow_path->GetEntryLabel());
2767 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002768 // A division by a non-null constant is valid. We don't need to perform
2769 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002770 }
2771 } else {
2772 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2773 }
2774}
2775
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002776void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2777 LocationSummary* locations =
2778 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2779 locations->SetOut(Location::ConstantLocation(constant));
2780}
2781
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002782void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2783 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002784 // Will be generated at use site.
2785}
2786
Alexandre Rames5319def2014-10-23 10:03:10 +01002787void LocationsBuilderARM64::VisitExit(HExit* exit) {
2788 exit->SetLocations(nullptr);
2789}
2790
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002791void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002792}
2793
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002794void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2795 LocationSummary* locations =
2796 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2797 locations->SetOut(Location::ConstantLocation(constant));
2798}
2799
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002800void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002801 // Will be generated at use site.
2802}
2803
David Brazdilfc6a86a2015-06-26 10:33:45 +00002804void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002805 DCHECK(!successor->IsExitBlock());
2806 HBasicBlock* block = got->GetBlock();
2807 HInstruction* previous = got->GetPrevious();
2808 HLoopInformation* info = block->GetLoopInformation();
2809
David Brazdil46e2a392015-03-16 17:31:52 +00002810 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002811 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2812 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2813 return;
2814 }
2815 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2816 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2817 }
2818 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002819 __ B(codegen_->GetLabelOf(successor));
2820 }
2821}
2822
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2824 got->SetLocations(nullptr);
2825}
2826
2827void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2828 HandleGoto(got, got->GetSuccessor());
2829}
2830
2831void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2832 try_boundary->SetLocations(nullptr);
2833}
2834
2835void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2836 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2837 if (!successor->IsExitBlock()) {
2838 HandleGoto(try_boundary, successor);
2839 }
2840}
2841
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002842void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002843 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002844 vixl::aarch64::Label* true_target,
2845 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002846 // FP branching requires both targets to be explicit. If either of the targets
2847 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002848 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002849 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002850
David Brazdil0debae72015-11-12 18:37:00 +00002851 if (true_target == nullptr && false_target == nullptr) {
2852 // Nothing to do. The code always falls through.
2853 return;
2854 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002855 // Constant condition, statically compared against "true" (integer value 1).
2856 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002857 if (true_target != nullptr) {
2858 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002859 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002860 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002861 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002862 if (false_target != nullptr) {
2863 __ B(false_target);
2864 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002865 }
David Brazdil0debae72015-11-12 18:37:00 +00002866 return;
2867 }
2868
2869 // The following code generates these patterns:
2870 // (1) true_target == nullptr && false_target != nullptr
2871 // - opposite condition true => branch to false_target
2872 // (2) true_target != nullptr && false_target == nullptr
2873 // - condition true => branch to true_target
2874 // (3) true_target != nullptr && false_target != nullptr
2875 // - condition true => branch to true_target
2876 // - branch to false_target
2877 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002878 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002879 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002880 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002881 if (true_target == nullptr) {
2882 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2883 } else {
2884 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2885 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002886 } else {
2887 // The condition instruction has not been materialized, use its inputs as
2888 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002889 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002890
David Brazdil0debae72015-11-12 18:37:00 +00002891 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002892 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002893 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002894 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002895 IfCondition opposite_condition = condition->GetOppositeCondition();
2896 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002897 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002898 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002899 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002900 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002901 // Integer cases.
2902 Register lhs = InputRegisterAt(condition, 0);
2903 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002904
2905 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002906 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002907 if (true_target == nullptr) {
2908 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2909 non_fallthrough_target = false_target;
2910 } else {
2911 arm64_cond = ARM64Condition(condition->GetCondition());
2912 non_fallthrough_target = true_target;
2913 }
2914
Aart Bik086d27e2016-01-20 17:02:00 -08002915 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002916 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002917 switch (arm64_cond) {
2918 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002919 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002920 break;
2921 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002922 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002923 break;
2924 case lt:
2925 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002926 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002927 break;
2928 case ge:
2929 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002930 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002931 break;
2932 default:
2933 // Without the `static_cast` the compiler throws an error for
2934 // `-Werror=sign-promo`.
2935 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2936 }
2937 } else {
2938 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002939 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002940 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002941 }
2942 }
David Brazdil0debae72015-11-12 18:37:00 +00002943
2944 // If neither branch falls through (case 3), the conditional branch to `true_target`
2945 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2946 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002947 __ B(false_target);
2948 }
David Brazdil0debae72015-11-12 18:37:00 +00002949
2950 if (fallthrough_target.IsLinked()) {
2951 __ Bind(&fallthrough_target);
2952 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002953}
2954
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002955void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2956 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002957 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002958 locations->SetInAt(0, Location::RequiresRegister());
2959 }
2960}
2961
2962void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002963 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2964 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002965 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2966 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2967 true_target = nullptr;
2968 }
2969 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2970 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2971 false_target = nullptr;
2972 }
David Brazdil0debae72015-11-12 18:37:00 +00002973 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002974}
2975
2976void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2977 LocationSummary* locations = new (GetGraph()->GetArena())
2978 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002979 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002980 locations->SetInAt(0, Location::RequiresRegister());
2981 }
2982}
2983
2984void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002985 SlowPathCodeARM64* slow_path =
2986 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002987 GenerateTestAndBranch(deoptimize,
2988 /* condition_input_index */ 0,
2989 slow_path->GetEntryLabel(),
2990 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002991}
2992
David Brazdilc0b601b2016-02-08 14:20:45 +00002993static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2994 return condition->IsCondition() &&
2995 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2996}
2997
Alexandre Rames880f1192016-06-13 16:04:50 +01002998static inline Condition GetConditionForSelect(HCondition* condition) {
2999 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003000 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3001 : ARM64Condition(cond);
3002}
3003
David Brazdil74eb1b22015-12-14 11:44:01 +00003004void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3005 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003006 if (Primitive::IsFloatingPointType(select->GetType())) {
3007 locations->SetInAt(0, Location::RequiresFpuRegister());
3008 locations->SetInAt(1, Location::RequiresFpuRegister());
3009 locations->SetOut(Location::RequiresFpuRegister());
3010 } else {
3011 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3012 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3013 bool is_true_value_constant = cst_true_value != nullptr;
3014 bool is_false_value_constant = cst_false_value != nullptr;
3015 // Ask VIXL whether we should synthesize constants in registers.
3016 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3017 Operand true_op = is_true_value_constant ?
3018 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3019 Operand false_op = is_false_value_constant ?
3020 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3021 bool true_value_in_register = false;
3022 bool false_value_in_register = false;
3023 MacroAssembler::GetCselSynthesisInformation(
3024 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3025 true_value_in_register |= !is_true_value_constant;
3026 false_value_in_register |= !is_false_value_constant;
3027
3028 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3029 : Location::ConstantLocation(cst_true_value));
3030 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3031 : Location::ConstantLocation(cst_false_value));
3032 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003033 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003034
David Brazdil74eb1b22015-12-14 11:44:01 +00003035 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3036 locations->SetInAt(2, Location::RequiresRegister());
3037 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003038}
3039
3040void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003041 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003042 Condition csel_cond;
3043
3044 if (IsBooleanValueOrMaterializedCondition(cond)) {
3045 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003046 // Use the condition flags set by the previous instruction.
3047 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003048 } else {
3049 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003050 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003051 }
3052 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003053 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003054 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003055 } else {
3056 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003057 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003058 }
3059
Alexandre Rames880f1192016-06-13 16:04:50 +01003060 if (Primitive::IsFloatingPointType(select->GetType())) {
3061 __ Fcsel(OutputFPRegister(select),
3062 InputFPRegisterAt(select, 1),
3063 InputFPRegisterAt(select, 0),
3064 csel_cond);
3065 } else {
3066 __ Csel(OutputRegister(select),
3067 InputOperandAt(select, 1),
3068 InputOperandAt(select, 0),
3069 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003070 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003071}
3072
David Srbecky0cf44932015-12-09 14:09:59 +00003073void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3074 new (GetGraph()->GetArena()) LocationSummary(info);
3075}
3076
David Srbeckyd28f4a02016-03-14 17:14:24 +00003077void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3078 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003079}
3080
3081void CodeGeneratorARM64::GenerateNop() {
3082 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003083}
3084
Alexandre Rames5319def2014-10-23 10:03:10 +01003085void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003086 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003087}
3088
3089void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003090 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003091}
3092
3093void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003094 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003095}
3096
3097void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003098 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003099}
3100
Roland Levillain44015862016-01-22 11:47:17 +00003101static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3102 return kEmitCompilerReadBarrier &&
3103 (kUseBakerReadBarrier ||
3104 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3105 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3106 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3107}
3108
Alexandre Rames67555f72014-11-18 10:55:16 +00003109void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003110 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003111 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3112 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003113 case TypeCheckKind::kExactCheck:
3114 case TypeCheckKind::kAbstractClassCheck:
3115 case TypeCheckKind::kClassHierarchyCheck:
3116 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003117 call_kind =
3118 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003119 break;
3120 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003121 case TypeCheckKind::kUnresolvedCheck:
3122 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003123 call_kind = LocationSummary::kCallOnSlowPath;
3124 break;
3125 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003126
Alexandre Rames67555f72014-11-18 10:55:16 +00003127 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003128 locations->SetInAt(0, Location::RequiresRegister());
3129 locations->SetInAt(1, Location::RequiresRegister());
3130 // The "out" register is used as a temporary, so it overlaps with the inputs.
3131 // Note that TypeCheckSlowPathARM64 uses this register too.
3132 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3133 // When read barriers are enabled, we need a temporary register for
3134 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003135 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003136 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003137 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003138}
3139
3140void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003141 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003142 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003143 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003144 Register obj = InputRegisterAt(instruction, 0);
3145 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003146 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003147 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003148 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3149 locations->GetTemp(0) :
3150 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003151 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3152 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3153 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3154 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003155
Scott Wakeling97c72b72016-06-24 16:19:36 +01003156 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003158
3159 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003160 // Avoid null check if we know `obj` is not null.
3161 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003162 __ Cbz(obj, &zero);
3163 }
3164
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003165 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003166 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003167
Roland Levillain44015862016-01-22 11:47:17 +00003168 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003169 case TypeCheckKind::kExactCheck: {
3170 __ Cmp(out, cls);
3171 __ Cset(out, eq);
3172 if (zero.IsLinked()) {
3173 __ B(&done);
3174 }
3175 break;
3176 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003178 case TypeCheckKind::kAbstractClassCheck: {
3179 // If the class is abstract, we eagerly fetch the super class of the
3180 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003181 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003182 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003183 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003184 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003185 // If `out` is null, we use it for the result, and jump to `done`.
3186 __ Cbz(out, &done);
3187 __ Cmp(out, cls);
3188 __ B(ne, &loop);
3189 __ Mov(out, 1);
3190 if (zero.IsLinked()) {
3191 __ B(&done);
3192 }
3193 break;
3194 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003195
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003196 case TypeCheckKind::kClassHierarchyCheck: {
3197 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003198 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003199 __ Bind(&loop);
3200 __ Cmp(out, cls);
3201 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003202 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003203 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003204 __ Cbnz(out, &loop);
3205 // If `out` is null, we use it for the result, and jump to `done`.
3206 __ B(&done);
3207 __ Bind(&success);
3208 __ Mov(out, 1);
3209 if (zero.IsLinked()) {
3210 __ B(&done);
3211 }
3212 break;
3213 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003214
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003215 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003216 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003217 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003218 __ Cmp(out, cls);
3219 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003220 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003221 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003222 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003223 // If `out` is null, we use it for the result, and jump to `done`.
3224 __ Cbz(out, &done);
3225 __ Ldrh(out, HeapOperand(out, primitive_offset));
3226 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3227 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003228 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003229 __ Mov(out, 1);
3230 __ B(&done);
3231 break;
3232 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003234 case TypeCheckKind::kArrayCheck: {
3235 __ Cmp(out, cls);
3236 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003237 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3238 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003239 codegen_->AddSlowPath(slow_path);
3240 __ B(ne, slow_path->GetEntryLabel());
3241 __ Mov(out, 1);
3242 if (zero.IsLinked()) {
3243 __ B(&done);
3244 }
3245 break;
3246 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003247
Calin Juravle98893e12015-10-02 21:05:03 +01003248 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003249 case TypeCheckKind::kInterfaceCheck: {
3250 // Note that we indeed only call on slow path, but we always go
3251 // into the slow path for the unresolved and interface check
3252 // cases.
3253 //
3254 // We cannot directly call the InstanceofNonTrivial runtime
3255 // entry point without resorting to a type checking slow path
3256 // here (i.e. by calling InvokeRuntime directly), as it would
3257 // require to assign fixed registers for the inputs of this
3258 // HInstanceOf instruction (following the runtime calling
3259 // convention), which might be cluttered by the potential first
3260 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003261 //
3262 // TODO: Introduce a new runtime entry point taking the object
3263 // to test (instead of its class) as argument, and let it deal
3264 // with the read barrier issues. This will let us refactor this
3265 // case of the `switch` code as it was previously (with a direct
3266 // call to the runtime not using a type checking slow path).
3267 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003268 DCHECK(locations->OnlyCallsOnSlowPath());
3269 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3270 /* is_fatal */ false);
3271 codegen_->AddSlowPath(slow_path);
3272 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003273 if (zero.IsLinked()) {
3274 __ B(&done);
3275 }
3276 break;
3277 }
3278 }
3279
3280 if (zero.IsLinked()) {
3281 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003282 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003283 }
3284
3285 if (done.IsLinked()) {
3286 __ Bind(&done);
3287 }
3288
3289 if (slow_path != nullptr) {
3290 __ Bind(slow_path->GetExitLabel());
3291 }
3292}
3293
3294void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3295 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3296 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3297
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003298 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3299 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003300 case TypeCheckKind::kExactCheck:
3301 case TypeCheckKind::kAbstractClassCheck:
3302 case TypeCheckKind::kClassHierarchyCheck:
3303 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3305 LocationSummary::kCallOnSlowPath :
3306 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003307 break;
3308 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003309 case TypeCheckKind::kUnresolvedCheck:
3310 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003311 call_kind = LocationSummary::kCallOnSlowPath;
3312 break;
3313 }
3314
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003315 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3316 locations->SetInAt(0, Location::RequiresRegister());
3317 locations->SetInAt(1, Location::RequiresRegister());
3318 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3319 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003320 // When read barriers are enabled, we need an additional temporary
3321 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003322 if (TypeCheckNeedsATemporary(type_check_kind)) {
3323 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003324 }
3325}
3326
3327void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003328 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003329 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003330 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003331 Register obj = InputRegisterAt(instruction, 0);
3332 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003333 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003334 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3335 locations->GetTemp(1) :
3336 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003337 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003338 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3339 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3340 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3341 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003342
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003343 bool is_type_check_slow_path_fatal =
3344 (type_check_kind == TypeCheckKind::kExactCheck ||
3345 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3346 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3347 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3348 !instruction->CanThrowIntoCatchBlock();
3349 SlowPathCodeARM64* type_check_slow_path =
3350 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3351 is_type_check_slow_path_fatal);
3352 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003353
Scott Wakeling97c72b72016-06-24 16:19:36 +01003354 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003355 // Avoid null check if we know obj is not null.
3356 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003357 __ Cbz(obj, &done);
3358 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003359
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003360 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003361 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003362
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003363 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003364 case TypeCheckKind::kExactCheck:
3365 case TypeCheckKind::kArrayCheck: {
3366 __ Cmp(temp, cls);
3367 // Jump to slow path for throwing the exception or doing a
3368 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003369 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003370 break;
3371 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003373 case TypeCheckKind::kAbstractClassCheck: {
3374 // If the class is abstract, we eagerly fetch the super class of the
3375 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003376 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003377 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003378 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003379 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003380
3381 // If the class reference currently in `temp` is not null, jump
3382 // to the `compare_classes` label to compare it with the checked
3383 // class.
3384 __ Cbnz(temp, &compare_classes);
3385 // Otherwise, jump to the slow path to throw the exception.
3386 //
3387 // But before, move back the object's class into `temp` before
3388 // going into the slow path, as it has been overwritten in the
3389 // meantime.
3390 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003391 GenerateReferenceLoadTwoRegisters(
3392 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003393 __ B(type_check_slow_path->GetEntryLabel());
3394
3395 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003396 __ Cmp(temp, cls);
3397 __ B(ne, &loop);
3398 break;
3399 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003400
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003401 case TypeCheckKind::kClassHierarchyCheck: {
3402 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003403 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003404 __ Bind(&loop);
3405 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003406 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003407
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003408 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003409 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003410
3411 // If the class reference currently in `temp` is not null, jump
3412 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003413 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003414 // Otherwise, jump to the slow path to throw the exception.
3415 //
3416 // But before, move back the object's class into `temp` before
3417 // going into the slow path, as it has been overwritten in the
3418 // meantime.
3419 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003420 GenerateReferenceLoadTwoRegisters(
3421 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003422 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003423 break;
3424 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003426 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003427 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003428 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003429 __ Cmp(temp, cls);
3430 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003431
3432 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003433 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003434 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003435
3436 // If the component type is not null (i.e. the object is indeed
3437 // an array), jump to label `check_non_primitive_component_type`
3438 // to further check that this component type is not a primitive
3439 // type.
3440 __ Cbnz(temp, &check_non_primitive_component_type);
3441 // Otherwise, jump to the slow path to throw the exception.
3442 //
3443 // But before, move back the object's class into `temp` before
3444 // going into the slow path, as it has been overwritten in the
3445 // meantime.
3446 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003447 GenerateReferenceLoadTwoRegisters(
3448 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449 __ B(type_check_slow_path->GetEntryLabel());
3450
3451 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003452 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3453 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003454 __ Cbz(temp, &done);
3455 // Same comment as above regarding `temp` and the slow path.
3456 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003457 GenerateReferenceLoadTwoRegisters(
3458 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003459 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003460 break;
3461 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003462
Calin Juravle98893e12015-10-02 21:05:03 +01003463 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003464 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003465 // We always go into the type check slow path for the unresolved
3466 // and interface check cases.
3467 //
3468 // We cannot directly call the CheckCast runtime entry point
3469 // without resorting to a type checking slow path here (i.e. by
3470 // calling InvokeRuntime directly), as it would require to
3471 // assign fixed registers for the inputs of this HInstanceOf
3472 // instruction (following the runtime calling convention), which
3473 // might be cluttered by the potential first read barrier
3474 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003475 //
3476 // TODO: Introduce a new runtime entry point taking the object
3477 // to test (instead of its class) as argument, and let it deal
3478 // with the read barrier issues. This will let us refactor this
3479 // case of the `switch` code as it was previously (with a direct
3480 // call to the runtime not using a type checking slow path).
3481 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003482 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003483 break;
3484 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003485 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003486
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003487 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003488}
3489
Alexandre Rames5319def2014-10-23 10:03:10 +01003490void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3491 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3492 locations->SetOut(Location::ConstantLocation(constant));
3493}
3494
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003495void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003496 // Will be generated at use site.
3497}
3498
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003499void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3501 locations->SetOut(Location::ConstantLocation(constant));
3502}
3503
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003504void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003505 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003506}
3507
Calin Juravle175dc732015-08-25 15:42:32 +01003508void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3509 // The trampoline uses the same calling convention as dex calling conventions,
3510 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3511 // the method_idx.
3512 HandleInvoke(invoke);
3513}
3514
3515void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3516 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3517}
3518
Alexandre Rames5319def2014-10-23 10:03:10 +01003519void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003520 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003521 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003522}
3523
Alexandre Rames67555f72014-11-18 10:55:16 +00003524void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3525 HandleInvoke(invoke);
3526}
3527
3528void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3529 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003530 LocationSummary* locations = invoke->GetLocations();
3531 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003532 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003533 Offset class_offset = mirror::Object::ClassOffset();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003534 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003535
3536 // The register ip1 is required to be used for the hidden argument in
3537 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003538 MacroAssembler* masm = GetVIXLAssembler();
3539 UseScratchRegisterScope scratch_scope(masm);
3540 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003541 scratch_scope.Exclude(ip1);
3542 __ Mov(ip1, invoke->GetDexMethodIndex());
3543
Alexandre Rames67555f72014-11-18 10:55:16 +00003544 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003546 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003547 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003548 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003549 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003550 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003551 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003552 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003553 // Instead of simply (possibly) unpoisoning `temp` here, we should
3554 // emit a read barrier for the previous class reference load.
3555 // However this is not required in practice, as this is an
3556 // intermediate/temporary reference and because the current
3557 // concurrent copying collector keeps the from-space memory
3558 // intact/accessible until the end of the marking phase (the
3559 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003560 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003561 __ Ldr(temp,
3562 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3563 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003564 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003565 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003566 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003567 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003568 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003569 // lr();
3570 __ Blr(lr);
3571 DCHECK(!codegen_->IsLeafMethod());
3572 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3573}
3574
3575void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003576 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3577 if (intrinsic.TryDispatch(invoke)) {
3578 return;
3579 }
3580
Alexandre Rames67555f72014-11-18 10:55:16 +00003581 HandleInvoke(invoke);
3582}
3583
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003584void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003585 // Explicit clinit checks triggered by static invokes must have been pruned by
3586 // art::PrepareForRegisterAllocation.
3587 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003588
Andreas Gampe878d58c2015-01-15 23:24:00 -08003589 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3590 if (intrinsic.TryDispatch(invoke)) {
3591 return;
3592 }
3593
Alexandre Rames67555f72014-11-18 10:55:16 +00003594 HandleInvoke(invoke);
3595}
3596
Andreas Gampe878d58c2015-01-15 23:24:00 -08003597static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3598 if (invoke->GetLocations()->Intrinsified()) {
3599 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3600 intrinsic.Dispatch(invoke);
3601 return true;
3602 }
3603 return false;
3604}
3605
Vladimir Markodc151b22015-10-15 18:02:30 +01003606HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3607 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3608 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003609 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003610 return desired_dispatch_info;
3611}
3612
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003613void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003614 // For better instruction scheduling we load the direct code pointer before the method pointer.
3615 bool direct_code_loaded = false;
3616 switch (invoke->GetCodePtrLocation()) {
3617 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3618 // LR = code address from literal pool with link-time patch.
3619 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3620 direct_code_loaded = true;
3621 break;
3622 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3623 // LR = invoke->GetDirectCodePtr();
3624 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3625 direct_code_loaded = true;
3626 break;
3627 default:
3628 break;
3629 }
3630
Andreas Gampe878d58c2015-01-15 23:24:00 -08003631 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003632 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3633 switch (invoke->GetMethodLoadKind()) {
3634 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3635 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003636 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003637 break;
3638 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003639 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003640 break;
3641 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3642 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003643 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003644 break;
3645 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3646 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003647 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003648 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3649 break;
3650 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3651 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003652 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3653 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003654 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003655 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003656 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003657 __ Bind(adrp_label);
3658 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003659 }
Vladimir Marko58155012015-08-19 12:49:41 +00003660 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003661 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003662 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003663 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003664 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003665 __ Bind(ldr_label);
3666 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003667 }
Vladimir Marko58155012015-08-19 12:49:41 +00003668 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003669 }
Vladimir Marko58155012015-08-19 12:49:41 +00003670 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003671 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003672 Register reg = XRegisterFrom(temp);
3673 Register method_reg;
3674 if (current_method.IsRegister()) {
3675 method_reg = XRegisterFrom(current_method);
3676 } else {
3677 DCHECK(invoke->GetLocations()->Intrinsified());
3678 DCHECK(!current_method.IsValid());
3679 method_reg = reg;
3680 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3681 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003682
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003683 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003684 __ Ldr(reg.X(),
3685 MemOperand(method_reg.X(),
3686 ArtMethod::DexCacheResolvedMethodsOffset(kArm64WordSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003687 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003688 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3689 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003690 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3691 break;
3692 }
3693 }
3694
3695 switch (invoke->GetCodePtrLocation()) {
3696 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3697 __ Bl(&frame_entry_label_);
3698 break;
3699 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3700 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003701 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3702 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003703 __ Bind(label);
3704 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003705 break;
3706 }
3707 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3708 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3709 // LR prepared above for better instruction scheduling.
3710 DCHECK(direct_code_loaded);
3711 // lr()
3712 __ Blr(lr);
3713 break;
3714 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3715 // LR = callee_method->entry_point_from_quick_compiled_code_;
3716 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003717 XRegisterFrom(callee_method),
Vladimir Marko58155012015-08-19 12:49:41 +00003718 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize).Int32Value()));
3719 // lr()
3720 __ Blr(lr);
3721 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003722 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003723
Andreas Gampe878d58c2015-01-15 23:24:00 -08003724 DCHECK(!IsLeafMethod());
3725}
3726
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003727void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003728 // Use the calling convention instead of the location of the receiver, as
3729 // intrinsics may have put the receiver in a different register. In the intrinsics
3730 // slow path, the arguments have been moved to the right place, so here we are
3731 // guaranteed that the receiver is the first register of the calling convention.
3732 InvokeDexCallingConvention calling_convention;
3733 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003734 Register temp = XRegisterFrom(temp_in);
3735 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3736 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3737 Offset class_offset = mirror::Object::ClassOffset();
3738 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
3739
3740 BlockPoolsScope block_pools(GetVIXLAssembler());
3741
3742 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003743 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003744 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003745 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003746 // Instead of simply (possibly) unpoisoning `temp` here, we should
3747 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003748 // intermediate/temporary reference and because the current
3749 // concurrent copying collector keeps the from-space memory
3750 // intact/accessible until the end of the marking phase (the
3751 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003752 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3753 // temp = temp->GetMethodAt(method_offset);
3754 __ Ldr(temp, MemOperand(temp, method_offset));
3755 // lr = temp->GetEntryPoint();
3756 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3757 // lr();
3758 __ Blr(lr);
3759}
3760
Scott Wakeling97c72b72016-06-24 16:19:36 +01003761vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3762 const DexFile& dex_file,
3763 uint32_t string_index,
3764 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003765 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3766}
3767
Scott Wakeling97c72b72016-06-24 16:19:36 +01003768vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3769 const DexFile& dex_file,
3770 uint32_t type_index,
3771 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003772 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3773}
3774
Scott Wakeling97c72b72016-06-24 16:19:36 +01003775vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3776 const DexFile& dex_file,
3777 uint32_t element_offset,
3778 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003779 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3780}
3781
Scott Wakeling97c72b72016-06-24 16:19:36 +01003782vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3783 const DexFile& dex_file,
3784 uint32_t offset_or_index,
3785 vixl::aarch64::Label* adrp_label,
3786 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003787 // Add a patch entry and return the label.
3788 patches->emplace_back(dex_file, offset_or_index);
3789 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003790 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003791 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3792 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3793 return label;
3794}
3795
Scott Wakeling97c72b72016-06-24 16:19:36 +01003796vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003797 const DexFile& dex_file, uint32_t string_index) {
3798 return boot_image_string_patches_.GetOrCreate(
3799 StringReference(&dex_file, string_index),
3800 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3801}
3802
Scott Wakeling97c72b72016-06-24 16:19:36 +01003803vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003804 const DexFile& dex_file, uint32_t type_index) {
3805 return boot_image_type_patches_.GetOrCreate(
3806 TypeReference(&dex_file, type_index),
3807 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3808}
3809
Scott Wakeling97c72b72016-06-24 16:19:36 +01003810vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3811 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003812 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3813 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3814 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3815}
3816
Scott Wakeling97c72b72016-06-24 16:19:36 +01003817vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3818 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003819 return DeduplicateUint64Literal(address);
3820}
3821
Vladimir Marko58155012015-08-19 12:49:41 +00003822void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3823 DCHECK(linker_patches->empty());
3824 size_t size =
3825 method_patches_.size() +
3826 call_patches_.size() +
3827 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003828 pc_relative_dex_cache_patches_.size() +
3829 boot_image_string_patches_.size() +
3830 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003831 boot_image_type_patches_.size() +
3832 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003833 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003834 linker_patches->reserve(size);
3835 for (const auto& entry : method_patches_) {
3836 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003837 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3838 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003839 target_method.dex_file,
3840 target_method.dex_method_index));
3841 }
3842 for (const auto& entry : call_patches_) {
3843 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003844 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3845 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003846 target_method.dex_file,
3847 target_method.dex_method_index));
3848 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003849 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3850 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003851 info.target_method.dex_file,
3852 info.target_method.dex_method_index));
3853 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003854 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003855 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003856 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003857 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003858 info.offset_or_index));
3859 }
3860 for (const auto& entry : boot_image_string_patches_) {
3861 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003862 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3863 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003864 target_string.dex_file,
3865 target_string.string_index));
3866 }
3867 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003868 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003869 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003870 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003871 info.offset_or_index));
3872 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003873 for (const auto& entry : boot_image_type_patches_) {
3874 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003875 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3876 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003877 target_type.dex_file,
3878 target_type.type_index));
3879 }
3880 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003881 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003882 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003883 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003884 info.offset_or_index));
3885 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003886 for (const auto& entry : boot_image_address_patches_) {
3887 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003888 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3889 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003890 }
3891}
3892
Scott Wakeling97c72b72016-06-24 16:19:36 +01003893vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003894 Uint32ToLiteralMap* map) {
3895 return map->GetOrCreate(
3896 value,
3897 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3898}
3899
Scott Wakeling97c72b72016-06-24 16:19:36 +01003900vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003901 return uint64_literals_.GetOrCreate(
3902 value,
3903 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003904}
3905
Scott Wakeling97c72b72016-06-24 16:19:36 +01003906vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003907 MethodReference target_method,
3908 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003909 return map->GetOrCreate(
3910 target_method,
3911 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003912}
3913
Scott Wakeling97c72b72016-06-24 16:19:36 +01003914vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003915 MethodReference target_method) {
3916 return DeduplicateMethodLiteral(target_method, &method_patches_);
3917}
3918
Scott Wakeling97c72b72016-06-24 16:19:36 +01003919vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003920 MethodReference target_method) {
3921 return DeduplicateMethodLiteral(target_method, &call_patches_);
3922}
3923
3924
Andreas Gampe878d58c2015-01-15 23:24:00 -08003925void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003926 // Explicit clinit checks triggered by static invokes must have been pruned by
3927 // art::PrepareForRegisterAllocation.
3928 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003929
Andreas Gampe878d58c2015-01-15 23:24:00 -08003930 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3931 return;
3932 }
3933
Alexandre Ramesd921d642015-04-16 15:07:16 +01003934 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003935 LocationSummary* locations = invoke->GetLocations();
3936 codegen_->GenerateStaticOrDirectCall(
3937 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003938 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003939}
3940
3941void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003942 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3943 return;
3944 }
3945
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003946 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003947 DCHECK(!codegen_->IsLeafMethod());
3948 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3949}
3950
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003951HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3952 HLoadClass::LoadKind desired_class_load_kind) {
3953 if (kEmitCompilerReadBarrier) {
3954 switch (desired_class_load_kind) {
3955 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3956 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3957 case HLoadClass::LoadKind::kBootImageAddress:
3958 // TODO: Implement for read barrier.
3959 return HLoadClass::LoadKind::kDexCacheViaMethod;
3960 default:
3961 break;
3962 }
3963 }
3964 switch (desired_class_load_kind) {
3965 case HLoadClass::LoadKind::kReferrersClass:
3966 break;
3967 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3968 DCHECK(!GetCompilerOptions().GetCompilePic());
3969 break;
3970 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3971 DCHECK(GetCompilerOptions().GetCompilePic());
3972 break;
3973 case HLoadClass::LoadKind::kBootImageAddress:
3974 break;
3975 case HLoadClass::LoadKind::kDexCacheAddress:
3976 DCHECK(Runtime::Current()->UseJitCompilation());
3977 break;
3978 case HLoadClass::LoadKind::kDexCachePcRelative:
3979 DCHECK(!Runtime::Current()->UseJitCompilation());
3980 break;
3981 case HLoadClass::LoadKind::kDexCacheViaMethod:
3982 break;
3983 }
3984 return desired_class_load_kind;
3985}
3986
Alexandre Rames67555f72014-11-18 10:55:16 +00003987void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003988 if (cls->NeedsAccessCheck()) {
3989 InvokeRuntimeCallingConvention calling_convention;
3990 CodeGenerator::CreateLoadClassLocationSummary(
3991 cls,
3992 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003993 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003994 /* code_generator_supports_read_barrier */ true);
3995 return;
3996 }
3997
3998 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3999 ? LocationSummary::kCallOnSlowPath
4000 : LocationSummary::kNoCall;
4001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
4002 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4003 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
4004 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4005 locations->SetInAt(0, Location::RequiresRegister());
4006 }
4007 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004008}
4009
4010void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004011 if (cls->NeedsAccessCheck()) {
4012 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4013 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4014 cls,
4015 cls->GetDexPc(),
4016 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004017 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004018 return;
4019 }
4020
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004021 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004022 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004023
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004024 bool generate_null_check = false;
4025 switch (cls->GetLoadKind()) {
4026 case HLoadClass::LoadKind::kReferrersClass: {
4027 DCHECK(!cls->CanCallRuntime());
4028 DCHECK(!cls->MustGenerateClinitCheck());
4029 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4030 Register current_method = InputRegisterAt(cls, 0);
4031 GenerateGcRootFieldLoad(
4032 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4033 break;
4034 }
4035 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4036 DCHECK(!kEmitCompilerReadBarrier);
4037 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4038 cls->GetTypeIndex()));
4039 break;
4040 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4041 DCHECK(!kEmitCompilerReadBarrier);
4042 // Add ADRP with its PC-relative type patch.
4043 const DexFile& dex_file = cls->GetDexFile();
4044 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004045 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004046 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004047 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004048 __ Bind(adrp_label);
4049 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004050 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004051 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004052 vixl::aarch64::Label* add_label =
4053 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004054 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004055 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004056 __ Bind(add_label);
4057 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004058 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004059 break;
4060 }
4061 case HLoadClass::LoadKind::kBootImageAddress: {
4062 DCHECK(!kEmitCompilerReadBarrier);
4063 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4064 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4065 break;
4066 }
4067 case HLoadClass::LoadKind::kDexCacheAddress: {
4068 DCHECK_NE(cls->GetAddress(), 0u);
4069 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4070 // that gives a 16KiB range. To try and reduce the number of literals if we load
4071 // multiple types, simply split the dex cache address to a 16KiB aligned base
4072 // loaded from a literal and the remaining offset embedded in the load.
4073 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4074 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4075 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4076 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4077 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4078 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4079 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4080 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4081 generate_null_check = !cls->IsInDexCache();
4082 break;
4083 }
4084 case HLoadClass::LoadKind::kDexCachePcRelative: {
4085 // Add ADRP with its PC-relative DexCache access patch.
4086 const DexFile& dex_file = cls->GetDexFile();
4087 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004088 vixl::aarch64::Label* adrp_label =
4089 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004090 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004091 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004092 __ Bind(adrp_label);
4093 __ adrp(out.X(), /* offset placeholder */ 0);
4094 }
4095 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004096 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004097 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4098 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4099 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4100 generate_null_check = !cls->IsInDexCache();
4101 break;
4102 }
4103 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4104 MemberOffset resolved_types_offset =
4105 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4106 // /* GcRoot<mirror::Class>[] */ out =
4107 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4108 Register current_method = InputRegisterAt(cls, 0);
4109 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4110 // /* GcRoot<mirror::Class> */ out = out[type_index]
4111 GenerateGcRootFieldLoad(
4112 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4113 generate_null_check = !cls->IsInDexCache();
4114 break;
4115 }
4116 }
4117
4118 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4119 DCHECK(cls->CanCallRuntime());
4120 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4121 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4122 codegen_->AddSlowPath(slow_path);
4123 if (generate_null_check) {
4124 __ Cbz(out, slow_path->GetEntryLabel());
4125 }
4126 if (cls->MustGenerateClinitCheck()) {
4127 GenerateClassInitializationCheck(slow_path, out);
4128 } else {
4129 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004130 }
4131 }
4132}
4133
David Brazdilcb1c0552015-08-04 16:22:25 +01004134static MemOperand GetExceptionTlsAddress() {
4135 return MemOperand(tr, Thread::ExceptionOffset<kArm64WordSize>().Int32Value());
4136}
4137
Alexandre Rames67555f72014-11-18 10:55:16 +00004138void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4139 LocationSummary* locations =
4140 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4141 locations->SetOut(Location::RequiresRegister());
4142}
4143
4144void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004145 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4146}
4147
4148void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4149 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4150}
4151
4152void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4153 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004154}
4155
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004156HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4157 HLoadString::LoadKind desired_string_load_kind) {
4158 if (kEmitCompilerReadBarrier) {
4159 switch (desired_string_load_kind) {
4160 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4161 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4162 case HLoadString::LoadKind::kBootImageAddress:
4163 // TODO: Implement for read barrier.
4164 return HLoadString::LoadKind::kDexCacheViaMethod;
4165 default:
4166 break;
4167 }
4168 }
4169 switch (desired_string_load_kind) {
4170 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4171 DCHECK(!GetCompilerOptions().GetCompilePic());
4172 break;
4173 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4174 DCHECK(GetCompilerOptions().GetCompilePic());
4175 break;
4176 case HLoadString::LoadKind::kBootImageAddress:
4177 break;
4178 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004179 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004180 break;
4181 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004182 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004183 break;
4184 case HLoadString::LoadKind::kDexCacheViaMethod:
4185 break;
4186 }
4187 return desired_string_load_kind;
4188}
4189
Alexandre Rames67555f72014-11-18 10:55:16 +00004190void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004191 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004192 ? LocationSummary::kCallOnSlowPath
4193 : LocationSummary::kNoCall;
4194 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004195 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4196 locations->SetInAt(0, Location::RequiresRegister());
4197 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004198 locations->SetOut(Location::RequiresRegister());
4199}
4200
4201void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004202 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004203 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004204
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004205 switch (load->GetLoadKind()) {
4206 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4207 DCHECK(!kEmitCompilerReadBarrier);
4208 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4209 load->GetStringIndex()));
4210 return; // No dex cache slow path.
4211 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4212 DCHECK(!kEmitCompilerReadBarrier);
4213 // Add ADRP with its PC-relative String patch.
4214 const DexFile& dex_file = load->GetDexFile();
4215 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004216 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004217 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004218 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004219 __ Bind(adrp_label);
4220 __ adrp(out.X(), /* offset placeholder */ 0);
4221 }
4222 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004223 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004224 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4225 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004226 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004227 __ Bind(add_label);
4228 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4229 }
4230 return; // No dex cache slow path.
4231 }
4232 case HLoadString::LoadKind::kBootImageAddress: {
4233 DCHECK(!kEmitCompilerReadBarrier);
4234 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4235 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4236 return; // No dex cache slow path.
4237 }
4238 case HLoadString::LoadKind::kDexCacheAddress: {
4239 DCHECK_NE(load->GetAddress(), 0u);
4240 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4241 // that gives a 16KiB range. To try and reduce the number of literals if we load
4242 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4243 // loaded from a literal and the remaining offset embedded in the load.
4244 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4245 DCHECK_ALIGNED(load->GetAddress(), 4u);
4246 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4247 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4248 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4249 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004250 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004251 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4252 break;
4253 }
4254 case HLoadString::LoadKind::kDexCachePcRelative: {
4255 // Add ADRP with its PC-relative DexCache access patch.
4256 const DexFile& dex_file = load->GetDexFile();
4257 uint32_t element_offset = load->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004258 vixl::aarch64::Label* adrp_label =
4259 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004260 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004261 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 __ Bind(adrp_label);
4263 __ adrp(out.X(), /* offset placeholder */ 0);
4264 }
4265 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004266 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004267 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004268 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004269 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4270 break;
4271 }
4272 case HLoadString::LoadKind::kDexCacheViaMethod: {
4273 Register current_method = InputRegisterAt(load, 0);
4274 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4275 GenerateGcRootFieldLoad(
4276 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4277 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4278 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4279 // /* GcRoot<mirror::String> */ out = out[string_index]
4280 GenerateGcRootFieldLoad(
4281 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4282 break;
4283 }
4284 default:
4285 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4286 UNREACHABLE();
4287 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004288
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004289 if (!load->IsInDexCache()) {
4290 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4291 codegen_->AddSlowPath(slow_path);
4292 __ Cbz(out, slow_path->GetEntryLabel());
4293 __ Bind(slow_path->GetExitLabel());
4294 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004295}
4296
Alexandre Rames5319def2014-10-23 10:03:10 +01004297void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4298 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4299 locations->SetOut(Location::ConstantLocation(constant));
4300}
4301
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004302void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004303 // Will be generated at use site.
4304}
4305
Alexandre Rames67555f72014-11-18 10:55:16 +00004306void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4307 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004309 InvokeRuntimeCallingConvention calling_convention;
4310 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4311}
4312
4313void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4314 codegen_->InvokeRuntime(instruction->IsEnter()
4315 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4316 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004317 instruction->GetDexPc(),
4318 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004319 if (instruction->IsEnter()) {
4320 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4321 } else {
4322 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4323 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004324}
4325
Alexandre Rames42d641b2014-10-27 14:00:51 +00004326void LocationsBuilderARM64::VisitMul(HMul* mul) {
4327 LocationSummary* locations =
4328 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4329 switch (mul->GetResultType()) {
4330 case Primitive::kPrimInt:
4331 case Primitive::kPrimLong:
4332 locations->SetInAt(0, Location::RequiresRegister());
4333 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004335 break;
4336
4337 case Primitive::kPrimFloat:
4338 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004339 locations->SetInAt(0, Location::RequiresFpuRegister());
4340 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004341 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004342 break;
4343
4344 default:
4345 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4346 }
4347}
4348
4349void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4350 switch (mul->GetResultType()) {
4351 case Primitive::kPrimInt:
4352 case Primitive::kPrimLong:
4353 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4354 break;
4355
4356 case Primitive::kPrimFloat:
4357 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004358 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004359 break;
4360
4361 default:
4362 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4363 }
4364}
4365
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004366void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4367 LocationSummary* locations =
4368 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4369 switch (neg->GetResultType()) {
4370 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004371 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004372 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004373 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004374 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004375
4376 case Primitive::kPrimFloat:
4377 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004378 locations->SetInAt(0, Location::RequiresFpuRegister());
4379 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004380 break;
4381
4382 default:
4383 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4384 }
4385}
4386
4387void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4388 switch (neg->GetResultType()) {
4389 case Primitive::kPrimInt:
4390 case Primitive::kPrimLong:
4391 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4392 break;
4393
4394 case Primitive::kPrimFloat:
4395 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004396 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004397 break;
4398
4399 default:
4400 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4401 }
4402}
4403
4404void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4405 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004406 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004407 InvokeRuntimeCallingConvention calling_convention;
4408 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004409 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004410 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004411 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004412}
4413
4414void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4415 LocationSummary* locations = instruction->GetLocations();
4416 InvokeRuntimeCallingConvention calling_convention;
4417 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4418 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004419 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004420 // Note: if heap poisoning is enabled, the entry point takes cares
4421 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004422 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4423 instruction,
4424 instruction->GetDexPc(),
4425 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004426 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004427}
4428
Alexandre Rames5319def2014-10-23 10:03:10 +01004429void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4430 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004431 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004432 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004433 if (instruction->IsStringAlloc()) {
4434 locations->AddTemp(LocationFrom(kArtMethodRegister));
4435 } else {
4436 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4437 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4438 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004439 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4440}
4441
4442void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004443 // Note: if heap poisoning is enabled, the entry point takes cares
4444 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004445 if (instruction->IsStringAlloc()) {
4446 // String is allocated through StringFactory. Call NewEmptyString entry point.
4447 Location temp = instruction->GetLocations()->GetTemp(0);
4448 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64WordSize);
4449 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4450 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4451 __ Blr(lr);
4452 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4453 } else {
4454 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4455 instruction,
4456 instruction->GetDexPc(),
4457 nullptr);
4458 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4459 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004460}
4461
4462void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004464 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004466}
4467
4468void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004469 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004470 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004471 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004472 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004473 break;
4474
4475 default:
4476 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4477 }
4478}
4479
David Brazdil66d126e2015-04-03 16:02:44 +01004480void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4481 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4482 locations->SetInAt(0, Location::RequiresRegister());
4483 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4484}
4485
4486void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004487 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004488}
4489
Alexandre Rames5319def2014-10-23 10:03:10 +01004490void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004491 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4492 ? LocationSummary::kCallOnSlowPath
4493 : LocationSummary::kNoCall;
4494 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004495 locations->SetInAt(0, Location::RequiresRegister());
4496 if (instruction->HasUses()) {
4497 locations->SetOut(Location::SameAsFirstInput());
4498 }
4499}
4500
Calin Juravle2ae48182016-03-16 14:05:09 +00004501void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4502 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004503 return;
4504 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004505
Alexandre Ramesd921d642015-04-16 15:07:16 +01004506 BlockPoolsScope block_pools(GetVIXLAssembler());
4507 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004508 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004509 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004510}
4511
Calin Juravle2ae48182016-03-16 14:05:09 +00004512void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004513 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004514 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004515
4516 LocationSummary* locations = instruction->GetLocations();
4517 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004518
4519 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004520}
4521
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004522void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004523 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004524}
4525
Alexandre Rames67555f72014-11-18 10:55:16 +00004526void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4527 HandleBinaryOp(instruction);
4528}
4529
4530void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4531 HandleBinaryOp(instruction);
4532}
4533
Alexandre Rames3e69f162014-12-10 10:36:50 +00004534void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4535 LOG(FATAL) << "Unreachable";
4536}
4537
4538void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4539 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4540}
4541
Alexandre Rames5319def2014-10-23 10:03:10 +01004542void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4544 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4545 if (location.IsStackSlot()) {
4546 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4547 } else if (location.IsDoubleStackSlot()) {
4548 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4549 }
4550 locations->SetOut(location);
4551}
4552
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004553void InstructionCodeGeneratorARM64::VisitParameterValue(
4554 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004555 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004556}
4557
4558void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4559 LocationSummary* locations =
4560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004561 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004562}
4563
4564void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4565 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4566 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004567}
4568
4569void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4570 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004571 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004572 locations->SetInAt(i, Location::Any());
4573 }
4574 locations->SetOut(Location::Any());
4575}
4576
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004577void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004578 LOG(FATAL) << "Unreachable";
4579}
4580
Serban Constantinescu02164b32014-11-13 14:05:07 +00004581void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004582 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004583 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004584 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4585 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4587
4588 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004589 case Primitive::kPrimInt:
4590 case Primitive::kPrimLong:
4591 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004592 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4594 break;
4595
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004596 case Primitive::kPrimFloat:
4597 case Primitive::kPrimDouble: {
4598 InvokeRuntimeCallingConvention calling_convention;
4599 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4600 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4601 locations->SetOut(calling_convention.GetReturnLocation(type));
4602
4603 break;
4604 }
4605
Serban Constantinescu02164b32014-11-13 14:05:07 +00004606 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004607 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004608 }
4609}
4610
4611void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4612 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004613
Serban Constantinescu02164b32014-11-13 14:05:07 +00004614 switch (type) {
4615 case Primitive::kPrimInt:
4616 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004617 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004618 break;
4619 }
4620
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004621 case Primitive::kPrimFloat:
4622 case Primitive::kPrimDouble: {
4623 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4624 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004625 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004626 if (type == Primitive::kPrimFloat) {
4627 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4628 } else {
4629 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4630 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004631 break;
4632 }
4633
Serban Constantinescu02164b32014-11-13 14:05:07 +00004634 default:
4635 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004636 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004637 }
4638}
4639
Calin Juravle27df7582015-04-17 19:12:31 +01004640void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4641 memory_barrier->SetLocations(nullptr);
4642}
4643
4644void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004645 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004646}
4647
Alexandre Rames5319def2014-10-23 10:03:10 +01004648void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4649 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4650 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004651 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004652}
4653
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004654void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004655 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004656}
4657
4658void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4659 instruction->SetLocations(nullptr);
4660}
4661
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004662void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004663 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004664}
4665
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004666void LocationsBuilderARM64::VisitRor(HRor* ror) {
4667 HandleBinaryOp(ror);
4668}
4669
4670void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4671 HandleBinaryOp(ror);
4672}
4673
Serban Constantinescu02164b32014-11-13 14:05:07 +00004674void LocationsBuilderARM64::VisitShl(HShl* shl) {
4675 HandleShift(shl);
4676}
4677
4678void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4679 HandleShift(shl);
4680}
4681
4682void LocationsBuilderARM64::VisitShr(HShr* shr) {
4683 HandleShift(shr);
4684}
4685
4686void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4687 HandleShift(shr);
4688}
4689
Alexandre Rames5319def2014-10-23 10:03:10 +01004690void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004691 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004692}
4693
4694void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004695 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004696}
4697
Alexandre Rames67555f72014-11-18 10:55:16 +00004698void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004699 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004700}
4701
4702void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004703 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004704}
4705
4706void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004707 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004708}
4709
Alexandre Rames67555f72014-11-18 10:55:16 +00004710void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004711 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004712}
4713
Calin Juravlee460d1d2015-09-29 04:52:17 +01004714void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4715 HUnresolvedInstanceFieldGet* instruction) {
4716 FieldAccessCallingConventionARM64 calling_convention;
4717 codegen_->CreateUnresolvedFieldLocationSummary(
4718 instruction, instruction->GetFieldType(), calling_convention);
4719}
4720
4721void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4722 HUnresolvedInstanceFieldGet* instruction) {
4723 FieldAccessCallingConventionARM64 calling_convention;
4724 codegen_->GenerateUnresolvedFieldAccess(instruction,
4725 instruction->GetFieldType(),
4726 instruction->GetFieldIndex(),
4727 instruction->GetDexPc(),
4728 calling_convention);
4729}
4730
4731void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4732 HUnresolvedInstanceFieldSet* instruction) {
4733 FieldAccessCallingConventionARM64 calling_convention;
4734 codegen_->CreateUnresolvedFieldLocationSummary(
4735 instruction, instruction->GetFieldType(), calling_convention);
4736}
4737
4738void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4739 HUnresolvedInstanceFieldSet* instruction) {
4740 FieldAccessCallingConventionARM64 calling_convention;
4741 codegen_->GenerateUnresolvedFieldAccess(instruction,
4742 instruction->GetFieldType(),
4743 instruction->GetFieldIndex(),
4744 instruction->GetDexPc(),
4745 calling_convention);
4746}
4747
4748void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4749 HUnresolvedStaticFieldGet* instruction) {
4750 FieldAccessCallingConventionARM64 calling_convention;
4751 codegen_->CreateUnresolvedFieldLocationSummary(
4752 instruction, instruction->GetFieldType(), calling_convention);
4753}
4754
4755void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4756 HUnresolvedStaticFieldGet* instruction) {
4757 FieldAccessCallingConventionARM64 calling_convention;
4758 codegen_->GenerateUnresolvedFieldAccess(instruction,
4759 instruction->GetFieldType(),
4760 instruction->GetFieldIndex(),
4761 instruction->GetDexPc(),
4762 calling_convention);
4763}
4764
4765void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4766 HUnresolvedStaticFieldSet* instruction) {
4767 FieldAccessCallingConventionARM64 calling_convention;
4768 codegen_->CreateUnresolvedFieldLocationSummary(
4769 instruction, instruction->GetFieldType(), calling_convention);
4770}
4771
4772void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4773 HUnresolvedStaticFieldSet* instruction) {
4774 FieldAccessCallingConventionARM64 calling_convention;
4775 codegen_->GenerateUnresolvedFieldAccess(instruction,
4776 instruction->GetFieldType(),
4777 instruction->GetFieldIndex(),
4778 instruction->GetDexPc(),
4779 calling_convention);
4780}
4781
Alexandre Rames5319def2014-10-23 10:03:10 +01004782void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4783 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4784}
4785
4786void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004787 HBasicBlock* block = instruction->GetBlock();
4788 if (block->GetLoopInformation() != nullptr) {
4789 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4790 // The back edge will generate the suspend check.
4791 return;
4792 }
4793 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4794 // The goto will generate the suspend check.
4795 return;
4796 }
4797 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004798}
4799
Alexandre Rames67555f72014-11-18 10:55:16 +00004800void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4801 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004802 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004803 InvokeRuntimeCallingConvention calling_convention;
4804 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4805}
4806
4807void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4808 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004809 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004810 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004811}
4812
4813void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4814 LocationSummary* locations =
4815 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4816 Primitive::Type input_type = conversion->GetInputType();
4817 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004818 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004819 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4820 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4821 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4822 }
4823
Alexandre Rames542361f2015-01-29 16:57:31 +00004824 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004825 locations->SetInAt(0, Location::RequiresFpuRegister());
4826 } else {
4827 locations->SetInAt(0, Location::RequiresRegister());
4828 }
4829
Alexandre Rames542361f2015-01-29 16:57:31 +00004830 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004831 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4832 } else {
4833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4834 }
4835}
4836
4837void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4838 Primitive::Type result_type = conversion->GetResultType();
4839 Primitive::Type input_type = conversion->GetInputType();
4840
4841 DCHECK_NE(input_type, result_type);
4842
Alexandre Rames542361f2015-01-29 16:57:31 +00004843 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004844 int result_size = Primitive::ComponentSize(result_type);
4845 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004846 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004847 Register output = OutputRegister(conversion);
4848 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004849 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004850 // 'int' values are used directly as W registers, discarding the top
4851 // bits, so we don't need to sign-extend and can just perform a move.
4852 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4853 // top 32 bits of the target register. We theoretically could leave those
4854 // bits unchanged, but we would have to make sure that no code uses a
4855 // 32bit input value as a 64bit value assuming that the top 32 bits are
4856 // zero.
4857 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004858 } else if (result_type == Primitive::kPrimChar ||
4859 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4860 __ Ubfx(output,
4861 output.IsX() ? source.X() : source.W(),
4862 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004863 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004864 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004865 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004866 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004867 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004868 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004869 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4870 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004871 } else if (Primitive::IsFloatingPointType(result_type) &&
4872 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004873 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4874 } else {
4875 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4876 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004877 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004878}
Alexandre Rames67555f72014-11-18 10:55:16 +00004879
Serban Constantinescu02164b32014-11-13 14:05:07 +00004880void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4881 HandleShift(ushr);
4882}
4883
4884void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4885 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004886}
4887
4888void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4889 HandleBinaryOp(instruction);
4890}
4891
4892void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4893 HandleBinaryOp(instruction);
4894}
4895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004896void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004897 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004898 LOG(FATAL) << "Unreachable";
4899}
4900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004901void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004902 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004903 LOG(FATAL) << "Unreachable";
4904}
4905
Mark Mendellfe57faa2015-09-18 09:26:15 -04004906// Simple implementation of packed switch - generate cascaded compare/jumps.
4907void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4908 LocationSummary* locations =
4909 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4910 locations->SetInAt(0, Location::RequiresRegister());
4911}
4912
4913void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4914 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004915 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004916 Register value_reg = InputRegisterAt(switch_instr, 0);
4917 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4918
Zheng Xu3927c8b2015-11-18 17:46:25 +08004919 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004920 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004921 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4922 // make sure we don't emit it if the target may run out of range.
4923 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4924 // ranges and emit the tables only as required.
4925 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004926
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004927 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004928 // Current instruction id is an upper bound of the number of HIRs in the graph.
4929 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4930 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004931 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4932 Register temp = temps.AcquireW();
4933 __ Subs(temp, value_reg, Operand(lower_bound));
4934
Zheng Xu3927c8b2015-11-18 17:46:25 +08004935 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004936 // Jump to successors[0] if value == lower_bound.
4937 __ B(eq, codegen_->GetLabelOf(successors[0]));
4938 int32_t last_index = 0;
4939 for (; num_entries - last_index > 2; last_index += 2) {
4940 __ Subs(temp, temp, Operand(2));
4941 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4942 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4943 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4944 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4945 }
4946 if (num_entries - last_index == 2) {
4947 // The last missing case_value.
4948 __ Cmp(temp, Operand(1));
4949 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004950 }
4951
4952 // And the default for any other value.
4953 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4954 __ B(codegen_->GetLabelOf(default_block));
4955 }
4956 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004957 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004958
4959 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4960
4961 // Below instructions should use at most one blocked register. Since there are two blocked
4962 // registers, we are free to block one.
4963 Register temp_w = temps.AcquireW();
4964 Register index;
4965 // Remove the bias.
4966 if (lower_bound != 0) {
4967 index = temp_w;
4968 __ Sub(index, value_reg, Operand(lower_bound));
4969 } else {
4970 index = value_reg;
4971 }
4972
4973 // Jump to default block if index is out of the range.
4974 __ Cmp(index, Operand(num_entries));
4975 __ B(hs, codegen_->GetLabelOf(default_block));
4976
4977 // In current VIXL implementation, it won't require any blocked registers to encode the
4978 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4979 // register pressure.
4980 Register table_base = temps.AcquireX();
4981 // Load jump offset from the table.
4982 __ Adr(table_base, jump_table->GetTableStartLabel());
4983 Register jump_offset = temp_w;
4984 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4985
4986 // Jump to target block by branching to table_base(pc related) + offset.
4987 Register target_address = table_base;
4988 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4989 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004990 }
4991}
4992
Roland Levillain44015862016-01-22 11:47:17 +00004993void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4994 Location out,
4995 uint32_t offset,
4996 Location maybe_temp) {
4997 Primitive::Type type = Primitive::kPrimNot;
4998 Register out_reg = RegisterFrom(out, type);
4999 if (kEmitCompilerReadBarrier) {
5000 Register temp_reg = RegisterFrom(maybe_temp, type);
5001 if (kUseBakerReadBarrier) {
5002 // Load with fast path based Baker's read barrier.
5003 // /* HeapReference<Object> */ out = *(out + offset)
5004 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5005 out,
5006 out_reg,
5007 offset,
5008 temp_reg,
5009 /* needs_null_check */ false,
5010 /* use_load_acquire */ false);
5011 } else {
5012 // Load with slow path based read barrier.
5013 // Save the value of `out` into `maybe_temp` before overwriting it
5014 // in the following move operation, as we will need it for the
5015 // read barrier below.
5016 __ Mov(temp_reg, out_reg);
5017 // /* HeapReference<Object> */ out = *(out + offset)
5018 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5019 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5020 }
5021 } else {
5022 // Plain load with no read barrier.
5023 // /* HeapReference<Object> */ out = *(out + offset)
5024 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5025 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5026 }
5027}
5028
5029void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5030 Location out,
5031 Location obj,
5032 uint32_t offset,
5033 Location maybe_temp) {
5034 Primitive::Type type = Primitive::kPrimNot;
5035 Register out_reg = RegisterFrom(out, type);
5036 Register obj_reg = RegisterFrom(obj, type);
5037 if (kEmitCompilerReadBarrier) {
5038 if (kUseBakerReadBarrier) {
5039 // Load with fast path based Baker's read barrier.
5040 Register temp_reg = RegisterFrom(maybe_temp, type);
5041 // /* HeapReference<Object> */ out = *(obj + offset)
5042 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5043 out,
5044 obj_reg,
5045 offset,
5046 temp_reg,
5047 /* needs_null_check */ false,
5048 /* use_load_acquire */ false);
5049 } else {
5050 // Load with slow path based read barrier.
5051 // /* HeapReference<Object> */ out = *(obj + offset)
5052 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5053 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5054 }
5055 } else {
5056 // Plain load with no read barrier.
5057 // /* HeapReference<Object> */ out = *(obj + offset)
5058 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5059 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5060 }
5061}
5062
5063void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5064 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005065 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005067 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005068 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5069 if (kEmitCompilerReadBarrier) {
5070 if (kUseBakerReadBarrier) {
5071 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5072 // Baker's read barrier are used:
5073 //
5074 // root = obj.field;
5075 // if (Thread::Current()->GetIsGcMarking()) {
5076 // root = ReadBarrier::Mark(root)
5077 // }
5078
5079 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005080 if (fixup_label == nullptr) {
5081 __ Ldr(root_reg, MemOperand(obj, offset));
5082 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005083 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005084 __ Bind(fixup_label);
5085 __ ldr(root_reg, MemOperand(obj, offset));
5086 }
Roland Levillain44015862016-01-22 11:47:17 +00005087 static_assert(
5088 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5089 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5090 "have different sizes.");
5091 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5092 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5093 "have different sizes.");
5094
5095 // Slow path used to mark the GC root `root`.
5096 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005097 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005098 codegen_->AddSlowPath(slow_path);
5099
5100 MacroAssembler* masm = GetVIXLAssembler();
5101 UseScratchRegisterScope temps(masm);
5102 Register temp = temps.AcquireW();
5103 // temp = Thread::Current()->GetIsGcMarking()
5104 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64WordSize>().Int32Value()));
5105 __ Cbnz(temp, slow_path->GetEntryLabel());
5106 __ Bind(slow_path->GetExitLabel());
5107 } else {
5108 // GC root loaded through a slow path for read barriers other
5109 // than Baker's.
5110 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 if (fixup_label == nullptr) {
5112 __ Add(root_reg.X(), obj.X(), offset);
5113 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005114 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005115 __ Bind(fixup_label);
5116 __ add(root_reg.X(), obj.X(), offset);
5117 }
Roland Levillain44015862016-01-22 11:47:17 +00005118 // /* mirror::Object* */ root = root->Read()
5119 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5120 }
5121 } else {
5122 // Plain GC root load with no read barrier.
5123 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005124 if (fixup_label == nullptr) {
5125 __ Ldr(root_reg, MemOperand(obj, offset));
5126 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005127 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005128 __ Bind(fixup_label);
5129 __ ldr(root_reg, MemOperand(obj, offset));
5130 }
Roland Levillain44015862016-01-22 11:47:17 +00005131 // Note that GC roots are not affected by heap poisoning, thus we
5132 // do not have to unpoison `root_reg` here.
5133 }
5134}
5135
5136void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5137 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005138 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005139 uint32_t offset,
5140 Register temp,
5141 bool needs_null_check,
5142 bool use_load_acquire) {
5143 DCHECK(kEmitCompilerReadBarrier);
5144 DCHECK(kUseBakerReadBarrier);
5145
5146 // /* HeapReference<Object> */ ref = *(obj + offset)
5147 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005148 size_t no_scale_factor = 0U;
5149 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5150 ref,
5151 obj,
5152 offset,
5153 no_index,
5154 no_scale_factor,
5155 temp,
5156 needs_null_check,
5157 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005158}
5159
5160void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5161 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005162 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005163 uint32_t data_offset,
5164 Location index,
5165 Register temp,
5166 bool needs_null_check) {
5167 DCHECK(kEmitCompilerReadBarrier);
5168 DCHECK(kUseBakerReadBarrier);
5169
5170 // Array cells are never volatile variables, therefore array loads
5171 // never use Load-Acquire instructions on ARM64.
5172 const bool use_load_acquire = false;
5173
Roland Levillainbfea3352016-06-23 13:48:47 +01005174 static_assert(
5175 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5176 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005177 // /* HeapReference<Object> */ ref =
5178 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005179 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5180 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5181 ref,
5182 obj,
5183 data_offset,
5184 index,
5185 scale_factor,
5186 temp,
5187 needs_null_check,
5188 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005189}
5190
5191void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5192 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005193 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005194 uint32_t offset,
5195 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005196 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005197 Register temp,
5198 bool needs_null_check,
5199 bool use_load_acquire) {
5200 DCHECK(kEmitCompilerReadBarrier);
5201 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005202 // If we are emitting an array load, we should not be using a
5203 // Load Acquire instruction. In other words:
5204 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5205 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005206
5207 MacroAssembler* masm = GetVIXLAssembler();
5208 UseScratchRegisterScope temps(masm);
5209
5210 // In slow path based read barriers, the read barrier call is
5211 // inserted after the original load. However, in fast path based
5212 // Baker's read barriers, we need to perform the load of
5213 // mirror::Object::monitor_ *before* the original reference load.
5214 // This load-load ordering is required by the read barrier.
5215 // The fast path/slow path (for Baker's algorithm) should look like:
5216 //
5217 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5218 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5219 // HeapReference<Object> ref = *src; // Original reference load.
5220 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5221 // if (is_gray) {
5222 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5223 // }
5224 //
5225 // Note: the original implementation in ReadBarrier::Barrier is
5226 // slightly more complex as it performs additional checks that we do
5227 // not do here for performance reasons.
5228
5229 Primitive::Type type = Primitive::kPrimNot;
5230 Register ref_reg = RegisterFrom(ref, type);
5231 DCHECK(obj.IsW());
5232 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5233
5234 // /* int32_t */ monitor = obj->monitor_
5235 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5236 if (needs_null_check) {
5237 MaybeRecordImplicitNullCheck(instruction);
5238 }
5239 // /* LockWord */ lock_word = LockWord(monitor)
5240 static_assert(sizeof(LockWord) == sizeof(int32_t),
5241 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005242
Vladimir Marko877a0332016-07-11 19:30:56 +01005243 // Introduce a dependency on the lock_word including rb_state,
5244 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005245 // a memory barrier (which would be more expensive).
Vladimir Marko877a0332016-07-11 19:30:56 +01005246 // obj is unchanged by this operation, but its value now depends on temp.
5247 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005248
5249 // The actual reference load.
5250 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005251 // Load types involving an "index".
5252 if (use_load_acquire) {
5253 // UnsafeGetObjectVolatile intrinsic case.
5254 // Register `index` is not an index in an object array, but an
5255 // offset to an object reference field within object `obj`.
5256 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5257 DCHECK(instruction->GetLocations()->Intrinsified());
5258 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5259 << instruction->AsInvoke()->GetIntrinsic();
5260 DCHECK_EQ(offset, 0U);
5261 DCHECK_EQ(scale_factor, 0U);
5262 DCHECK_EQ(needs_null_check, 0U);
5263 // /* HeapReference<Object> */ ref = *(obj + index)
5264 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5265 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005266 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005267 // ArrayGet and UnsafeGetObject intrinsics cases.
5268 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5269 if (index.IsConstant()) {
5270 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5271 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5272 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005273 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005274 __ Add(temp2, obj, offset);
5275 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5276 temps.Release(temp2);
5277 }
Roland Levillain44015862016-01-22 11:47:17 +00005278 }
Roland Levillain44015862016-01-22 11:47:17 +00005279 } else {
5280 // /* HeapReference<Object> */ ref = *(obj + offset)
5281 MemOperand field = HeapOperand(obj, offset);
5282 if (use_load_acquire) {
5283 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5284 } else {
5285 Load(type, ref_reg, field);
5286 }
5287 }
5288
5289 // Object* ref = ref_addr->AsMirrorPtr()
5290 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5291
5292 // Slow path used to mark the object `ref` when it is gray.
5293 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005294 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005295 AddSlowPath(slow_path);
5296
5297 // if (rb_state == ReadBarrier::gray_ptr_)
5298 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005299 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5300 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5301 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5302 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5303 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005304 __ Bind(slow_path->GetExitLabel());
5305}
5306
5307void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5308 Location out,
5309 Location ref,
5310 Location obj,
5311 uint32_t offset,
5312 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005313 DCHECK(kEmitCompilerReadBarrier);
5314
Roland Levillain44015862016-01-22 11:47:17 +00005315 // Insert a slow path based read barrier *after* the reference load.
5316 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005317 // If heap poisoning is enabled, the unpoisoning of the loaded
5318 // reference will be carried out by the runtime within the slow
5319 // path.
5320 //
5321 // Note that `ref` currently does not get unpoisoned (when heap
5322 // poisoning is enabled), which is alright as the `ref` argument is
5323 // not used by the artReadBarrierSlow entry point.
5324 //
5325 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5326 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5327 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5328 AddSlowPath(slow_path);
5329
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005330 __ B(slow_path->GetEntryLabel());
5331 __ Bind(slow_path->GetExitLabel());
5332}
5333
Roland Levillain44015862016-01-22 11:47:17 +00005334void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5335 Location out,
5336 Location ref,
5337 Location obj,
5338 uint32_t offset,
5339 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005340 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005341 // Baker's read barriers shall be handled by the fast path
5342 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5343 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005344 // If heap poisoning is enabled, unpoisoning will be taken care of
5345 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005346 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005347 } else if (kPoisonHeapReferences) {
5348 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5349 }
5350}
5351
Roland Levillain44015862016-01-22 11:47:17 +00005352void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5353 Location out,
5354 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005355 DCHECK(kEmitCompilerReadBarrier);
5356
Roland Levillain44015862016-01-22 11:47:17 +00005357 // Insert a slow path based read barrier *after* the GC root load.
5358 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005359 // Note that GC roots are not affected by heap poisoning, so we do
5360 // not need to do anything special for this here.
5361 SlowPathCodeARM64* slow_path =
5362 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5363 AddSlowPath(slow_path);
5364
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005365 __ B(slow_path->GetEntryLabel());
5366 __ Bind(slow_path->GetExitLabel());
5367}
5368
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005369void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5372 locations->SetInAt(0, Location::RequiresRegister());
5373 locations->SetOut(Location::RequiresRegister());
5374}
5375
5376void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5377 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005378 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005379 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005380 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005381 __ Ldr(XRegisterFrom(locations->Out()),
5382 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005383 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005384 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005385 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005386 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5387 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005388 __ Ldr(XRegisterFrom(locations->Out()),
5389 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005390 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005391}
5392
5393
5394
Alexandre Rames67555f72014-11-18 10:55:16 +00005395#undef __
5396#undef QUICK_ENTRY_POINT
5397
Alexandre Rames5319def2014-10-23 10:03:10 +01005398} // namespace arm64
5399} // namespace art