blob: 115cee64923c6f25be275375ae65a24368a28f45 [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
Andreas Gampe542451c2016-07-26 09:02:02 -0700136#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, 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);
Andreas Gampe67409972016-07-19 22:34:53 -0700465 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t,
Roland Levillain888d0672015-11-23 18:53:50 +0000466 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() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100600 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000601 << "Unexpected instruction in read barrier marking slow path: "
602 << instruction_->DebugName();
603
604 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100605 // No need to save live registers; it's taken care of by the
606 // entrypoint. Also, there is no need to update the stack mask,
607 // as this runtime call will not trigger a garbage collection.
Roland Levillain44015862016-01-22 11:47:17 +0000608 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100609 DCHECK_NE(obj_.reg(), LR);
610 DCHECK_NE(obj_.reg(), WSP);
611 DCHECK_NE(obj_.reg(), WZR);
612 DCHECK(0 <= obj_.reg() && obj_.reg() < kNumberOfWRegisters) << obj_.reg();
613 // "Compact" slow path, saving two moves.
614 //
615 // Instead of using the standard runtime calling convention (input
616 // and output in W0):
617 //
618 // W0 <- obj
619 // W0 <- ReadBarrierMark(W0)
620 // obj <- W0
621 //
622 // we just use rX (the register holding `obj`) as input and output
623 // of a dedicated entrypoint:
624 //
625 // rX <- ReadBarrierMarkRegX(rX)
626 //
627 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700628 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(obj_.reg());
Roland Levillaindec8f632016-07-22 17:10:06 +0100629 // This runtime call does not require a stack map.
630 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain44015862016-01-22 11:47:17 +0000631 __ B(GetExitLabel());
632 }
633
634 private:
Roland Levillain44015862016-01-22 11:47:17 +0000635 const Location obj_;
636
637 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
638};
639
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000640// Slow path generating a read barrier for a heap reference.
641class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
642 public:
643 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
644 Location out,
645 Location ref,
646 Location obj,
647 uint32_t offset,
648 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000649 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000650 out_(out),
651 ref_(ref),
652 obj_(obj),
653 offset_(offset),
654 index_(index) {
655 DCHECK(kEmitCompilerReadBarrier);
656 // If `obj` is equal to `out` or `ref`, it means the initial object
657 // has been overwritten by (or after) the heap object reference load
658 // to be instrumented, e.g.:
659 //
660 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +0000661 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000662 //
663 // In that case, we have lost the information about the original
664 // object, and the emitted read barrier cannot work properly.
665 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
666 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
667 }
668
669 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
670 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
671 LocationSummary* locations = instruction_->GetLocations();
672 Primitive::Type type = Primitive::kPrimNot;
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +0100675 DCHECK(instruction_->IsInstanceFieldGet() ||
676 instruction_->IsStaticFieldGet() ||
677 instruction_->IsArrayGet() ||
678 instruction_->IsInstanceOf() ||
679 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100680 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain44015862016-01-22 11:47:17 +0000681 << "Unexpected instruction in read barrier for heap reference slow path: "
682 << instruction_->DebugName();
Artem Serov328429f2016-07-06 16:23:04 +0100683 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +0000684 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +0100685 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000686
687 __ Bind(GetEntryLabel());
688
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000689 SaveLiveRegisters(codegen, locations);
690
691 // We may have to change the index's value, but as `index_` is a
692 // constant member (like other "inputs" of this slow path),
693 // introduce a copy of it, `index`.
694 Location index = index_;
695 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100696 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000697 if (instruction_->IsArrayGet()) {
698 // Compute the actual memory offset and store it in `index`.
699 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
700 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
701 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
702 // We are about to change the value of `index_reg` (see the
703 // calls to vixl::MacroAssembler::Lsl and
704 // vixl::MacroAssembler::Mov below), but it has
705 // not been saved by the previous call to
706 // art::SlowPathCode::SaveLiveRegisters, as it is a
707 // callee-save register --
708 // art::SlowPathCode::SaveLiveRegisters does not consider
709 // callee-save registers, as it has been designed with the
710 // assumption that callee-save registers are supposed to be
711 // handled by the called function. So, as a callee-save
712 // register, `index_reg` _would_ eventually be saved onto
713 // the stack, but it would be too late: we would have
714 // changed its value earlier. Therefore, we manually save
715 // it here into another freely available register,
716 // `free_reg`, chosen of course among the caller-save
717 // registers (as a callee-save `free_reg` register would
718 // exhibit the same problem).
719 //
720 // Note we could have requested a temporary register from
721 // the register allocator instead; but we prefer not to, as
722 // this is a slow path, and we know we can find a
723 // caller-save register that is available.
724 Register free_reg = FindAvailableCallerSaveRegister(codegen);
725 __ Mov(free_reg.W(), index_reg);
726 index_reg = free_reg;
727 index = LocationFrom(index_reg);
728 } else {
729 // The initial register stored in `index_` has already been
730 // saved in the call to art::SlowPathCode::SaveLiveRegisters
731 // (as it is not a callee-save register), so we can freely
732 // use it.
733 }
734 // Shifting the index value contained in `index_reg` by the scale
735 // factor (2) cannot overflow in practice, as the runtime is
736 // unable to allocate object arrays with a size larger than
737 // 2^26 - 1 (that is, 2^28 - 4 bytes).
738 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
739 static_assert(
740 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
741 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
742 __ Add(index_reg, index_reg, Operand(offset_));
743 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100744 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
745 // intrinsics, `index_` is not shifted by a scale factor of 2
746 // (as in the case of ArrayGet), as it is actually an offset
747 // to an object field within an object.
748 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000749 DCHECK(instruction_->GetLocations()->Intrinsified());
750 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
751 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
752 << instruction_->AsInvoke()->GetIntrinsic();
753 DCHECK_EQ(offset_, 0U);
754 DCHECK(index_.IsRegisterPair());
755 // UnsafeGet's offset location is a register pair, the low
756 // part contains the correct offset.
757 index = index_.ToLow();
758 }
759 }
760
761 // We're moving two or three locations to locations that could
762 // overlap, so we need a parallel move resolver.
763 InvokeRuntimeCallingConvention calling_convention;
764 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
765 parallel_move.AddMove(ref_,
766 LocationFrom(calling_convention.GetRegisterAt(0)),
767 type,
768 nullptr);
769 parallel_move.AddMove(obj_,
770 LocationFrom(calling_convention.GetRegisterAt(1)),
771 type,
772 nullptr);
773 if (index.IsValid()) {
774 parallel_move.AddMove(index,
775 LocationFrom(calling_convention.GetRegisterAt(2)),
776 Primitive::kPrimInt,
777 nullptr);
778 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
779 } else {
780 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
781 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
782 }
783 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
784 instruction_,
785 instruction_->GetDexPc(),
786 this);
787 CheckEntrypointTypes<
788 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
789 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
790
791 RestoreLiveRegisters(codegen, locations);
792
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000793 __ B(GetExitLabel());
794 }
795
796 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
797
798 private:
799 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100800 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
801 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000802 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
803 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
804 return Register(VIXLRegCodeFromART(i), kXRegSize);
805 }
806 }
807 // We shall never fail to find a free caller-save register, as
808 // there are more than two core caller-save registers on ARM64
809 // (meaning it is possible to find one which is different from
810 // `ref` and `obj`).
811 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
812 LOG(FATAL) << "Could not find a free register";
813 UNREACHABLE();
814 }
815
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000816 const Location out_;
817 const Location ref_;
818 const Location obj_;
819 const uint32_t offset_;
820 // An additional location containing an index to an array.
821 // Only used for HArrayGet and the UnsafeGetObject &
822 // UnsafeGetObjectVolatile intrinsics.
823 const Location index_;
824
825 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
826};
827
828// Slow path generating a read barrier for a GC root.
829class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
830 public:
831 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000832 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +0000833 DCHECK(kEmitCompilerReadBarrier);
834 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000835
836 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
837 LocationSummary* locations = instruction_->GetLocations();
838 Primitive::Type type = Primitive::kPrimNot;
839 DCHECK(locations->CanCall());
840 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +0000841 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
842 << "Unexpected instruction in read barrier for GC root slow path: "
843 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000844
845 __ Bind(GetEntryLabel());
846 SaveLiveRegisters(codegen, locations);
847
848 InvokeRuntimeCallingConvention calling_convention;
849 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
850 // The argument of the ReadBarrierForRootSlow is not a managed
851 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
852 // thus we need a 64-bit move here, and we cannot use
853 //
854 // arm64_codegen->MoveLocation(
855 // LocationFrom(calling_convention.GetRegisterAt(0)),
856 // root_,
857 // type);
858 //
859 // which would emit a 32-bit move, as `type` is a (32-bit wide)
860 // reference type (`Primitive::kPrimNot`).
861 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
862 arm64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
863 instruction_,
864 instruction_->GetDexPc(),
865 this);
866 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
867 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
868
869 RestoreLiveRegisters(codegen, locations);
870 __ B(GetExitLabel());
871 }
872
873 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
874
875 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000876 const Location out_;
877 const Location root_;
878
879 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
880};
881
Alexandre Rames5319def2014-10-23 10:03:10 +0100882#undef __
883
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100884Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
Alexandre Rames5319def2014-10-23 10:03:10 +0100885 Location next_location;
886 if (type == Primitive::kPrimVoid) {
887 LOG(FATAL) << "Unreachable type " << type;
888 }
889
Alexandre Rames542361f2015-01-29 16:57:31 +0000890 if (Primitive::IsFloatingPointType(type) &&
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100891 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
892 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
Alexandre Rames542361f2015-01-29 16:57:31 +0000893 } else if (!Primitive::IsFloatingPointType(type) &&
894 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000895 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
896 } else {
897 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +0000898 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
899 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +0100900 }
901
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000902 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +0000903 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +0100904 return next_location;
905}
906
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100907Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100908 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100909}
910
Serban Constantinescu579885a2015-02-22 20:51:33 +0000911CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
912 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100913 const CompilerOptions& compiler_options,
914 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +0100915 : CodeGenerator(graph,
916 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000917 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000918 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100919 callee_saved_core_registers.GetList(),
920 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100921 compiler_options,
922 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100923 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +0800924 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +0100925 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +0000926 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +0000927 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100928 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000929 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000930 uint32_literals_(std::less<uint32_t>(),
931 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100932 uint64_literals_(std::less<uint64_t>(),
933 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
934 method_patches_(MethodReferenceComparator(),
935 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
936 call_patches_(MethodReferenceComparator(),
937 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
938 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000939 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
940 boot_image_string_patches_(StringReferenceValueComparator(),
941 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
942 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100943 boot_image_type_patches_(TypeReferenceValueComparator(),
944 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
945 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000946 boot_image_address_patches_(std::less<uint32_t>(),
947 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000948 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +0000949 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000950}
Alexandre Rames5319def2014-10-23 10:03:10 +0100951
Alexandre Rames67555f72014-11-18 10:55:16 +0000952#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +0100953
Zheng Xu3927c8b2015-11-18 17:46:25 +0800954void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +0100955 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800956 jump_table->EmitTable(this);
957 }
958}
959
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000960void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +0800961 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000962 // Ensure we emit the literal pool.
963 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +0000964
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000965 CodeGenerator::Finalize(allocator);
966}
967
Zheng Xuad4450e2015-04-17 18:48:56 +0800968void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
969 // Note: There are 6 kinds of moves:
970 // 1. constant -> GPR/FPR (non-cycle)
971 // 2. constant -> stack (non-cycle)
972 // 3. GPR/FPR -> GPR/FPR
973 // 4. GPR/FPR -> stack
974 // 5. stack -> GPR/FPR
975 // 6. stack -> stack (non-cycle)
976 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
977 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
978 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
979 // dependency.
980 vixl_temps_.Open(GetVIXLAssembler());
981}
982
983void ParallelMoveResolverARM64::FinishEmitNativeCode() {
984 vixl_temps_.Close();
985}
986
987Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
988 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister ||
989 kind == Location::kStackSlot || kind == Location::kDoubleStackSlot);
990 kind = (kind == Location::kFpuRegister) ? Location::kFpuRegister : Location::kRegister;
991 Location scratch = GetScratchLocation(kind);
992 if (!scratch.Equals(Location::NoLocation())) {
993 return scratch;
994 }
995 // Allocate from VIXL temp registers.
996 if (kind == Location::kRegister) {
997 scratch = LocationFrom(vixl_temps_.AcquireX());
998 } else {
999 DCHECK(kind == Location::kFpuRegister);
1000 scratch = LocationFrom(vixl_temps_.AcquireD());
1001 }
1002 AddScratchLocation(scratch);
1003 return scratch;
1004}
1005
1006void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1007 if (loc.IsRegister()) {
1008 vixl_temps_.Release(XRegisterFrom(loc));
1009 } else {
1010 DCHECK(loc.IsFpuRegister());
1011 vixl_temps_.Release(DRegisterFrom(loc));
1012 }
1013 RemoveScratchLocation(loc);
1014}
1015
Alexandre Rames3e69f162014-12-10 10:36:50 +00001016void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001017 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001018 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001019}
1020
Alexandre Rames5319def2014-10-23 10:03:10 +01001021void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001022 MacroAssembler* masm = GetVIXLAssembler();
1023 BlockPoolsScope block_pools(masm);
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001024 __ Bind(&frame_entry_label_);
1025
Serban Constantinescu02164b32014-11-13 14:05:07 +00001026 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1027 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001028 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001029 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001030 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001031 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001032 __ Ldr(wzr, MemOperand(temp, 0));
1033 RecordPcInfo(nullptr, 0);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001034 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001035
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001036 if (!HasEmptyFrame()) {
1037 int frame_size = GetFrameSize();
1038 // Stack layout:
1039 // sp[frame_size - 8] : lr.
1040 // ... : other preserved core registers.
1041 // ... : other preserved fp registers.
1042 // ... : reserved frame space.
1043 // sp[0] : current method.
1044 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001045 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001046 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1047 frame_size - GetCoreSpillSize());
1048 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1049 frame_size - FrameEntrySpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001050 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001051}
1052
1053void CodeGeneratorARM64::GenerateFrameExit() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001054 BlockPoolsScope block_pools(GetVIXLAssembler());
David Srbeckyc34dc932015-04-12 09:27:43 +01001055 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001056 if (!HasEmptyFrame()) {
1057 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001058 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1059 frame_size - FrameEntrySpillSize());
1060 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1061 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001062 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001063 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001064 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001065 __ Ret();
1066 GetAssembler()->cfi().RestoreState();
1067 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001068}
1069
Scott Wakeling97c72b72016-06-24 16:19:36 +01001070CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001071 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001072 return CPURegList(CPURegister::kRegister, kXRegSize,
1073 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001074}
1075
Scott Wakeling97c72b72016-06-24 16:19:36 +01001076CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001077 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1078 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001079 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1080 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001081}
1082
Alexandre Rames5319def2014-10-23 10:03:10 +01001083void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1084 __ Bind(GetLabelOf(block));
1085}
1086
Calin Juravle175dc732015-08-25 15:42:32 +01001087void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1088 DCHECK(location.IsRegister());
1089 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1090}
1091
Calin Juravlee460d1d2015-09-29 04:52:17 +01001092void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1093 if (location.IsRegister()) {
1094 locations->AddTemp(location);
1095 } else {
1096 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1097 }
1098}
1099
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001100void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001101 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001102 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001103 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001104 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001105 if (value_can_be_null) {
1106 __ Cbz(value, &done);
1107 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001108 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001109 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001110 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001111 if (value_can_be_null) {
1112 __ Bind(&done);
1113 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001114}
1115
David Brazdil58282f42016-01-14 12:45:10 +00001116void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001117 // Blocked core registers:
1118 // lr : Runtime reserved.
1119 // tr : Runtime reserved.
1120 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1121 // ip1 : VIXL core temp.
1122 // ip0 : VIXL core temp.
1123 //
1124 // Blocked fp registers:
1125 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001126 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1127 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001128 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001129 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001130 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001131
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001132 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001133 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001134 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001135 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001136
David Brazdil58282f42016-01-14 12:45:10 +00001137 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001138 // Stubs do not save callee-save floating point registers. If the graph
1139 // is debuggable, we need to deal with these registers differently. For
1140 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001141 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1142 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001143 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001144 }
1145 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001146}
1147
Alexandre Rames3e69f162014-12-10 10:36:50 +00001148size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1150 __ Str(reg, MemOperand(sp, stack_index));
1151 return kArm64WordSize;
1152}
1153
1154size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1156 __ Ldr(reg, MemOperand(sp, stack_index));
1157 return kArm64WordSize;
1158}
1159
1160size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1161 FPRegister reg = FPRegister(reg_id, kDRegSize);
1162 __ Str(reg, MemOperand(sp, stack_index));
1163 return kArm64WordSize;
1164}
1165
1166size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1167 FPRegister reg = FPRegister(reg_id, kDRegSize);
1168 __ Ldr(reg, MemOperand(sp, stack_index));
1169 return kArm64WordSize;
1170}
1171
Alexandre Rames5319def2014-10-23 10:03:10 +01001172void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001174}
1175
1176void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001178}
1179
Alexandre Rames67555f72014-11-18 10:55:16 +00001180void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001181 if (constant->IsIntConstant()) {
1182 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1183 } else if (constant->IsLongConstant()) {
1184 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1185 } else if (constant->IsNullConstant()) {
1186 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001187 } else if (constant->IsFloatConstant()) {
1188 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1189 } else {
1190 DCHECK(constant->IsDoubleConstant());
1191 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1192 }
1193}
1194
Alexandre Rames3e69f162014-12-10 10:36:50 +00001195
1196static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1197 DCHECK(constant.IsConstant());
1198 HConstant* cst = constant.GetConstant();
1199 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001200 // Null is mapped to a core W register, which we associate with kPrimInt.
1201 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001202 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1203 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1204 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1205}
1206
Calin Juravlee460d1d2015-09-29 04:52:17 +01001207void CodeGeneratorARM64::MoveLocation(Location destination,
1208 Location source,
1209 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001210 if (source.Equals(destination)) {
1211 return;
1212 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001213
1214 // A valid move can always be inferred from the destination and source
1215 // locations. When moving from and to a register, the argument type can be
1216 // used to generate 32bit instead of 64bit moves. In debug mode we also
1217 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001219
1220 if (destination.IsRegister() || destination.IsFpuRegister()) {
1221 if (unspecified_type) {
1222 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1223 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001224 (src_cst != nullptr && (src_cst->IsIntConstant()
1225 || src_cst->IsFloatConstant()
1226 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001227 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001228 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001229 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001230 // If the source is a double stack slot or a 64bit constant, a 64bit
1231 // type is appropriate. Else the source is a register, and since the
1232 // type has not been specified, we chose a 64bit type to force a 64bit
1233 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001234 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001235 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001236 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1238 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1239 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001240 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1241 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1242 __ Ldr(dst, StackOperandFrom(source));
1243 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001244 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001245 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001246 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001247 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001248 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001249 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001250 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001251 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1252 ? Primitive::kPrimLong
1253 : Primitive::kPrimInt;
1254 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1255 }
1256 } else {
1257 DCHECK(source.IsFpuRegister());
1258 if (destination.IsRegister()) {
1259 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1260 ? Primitive::kPrimDouble
1261 : Primitive::kPrimFloat;
1262 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1263 } else {
1264 DCHECK(destination.IsFpuRegister());
1265 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001266 }
1267 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001268 } else { // The destination is not a register. It must be a stack slot.
1269 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1270 if (source.IsRegister() || source.IsFpuRegister()) {
1271 if (unspecified_type) {
1272 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001274 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001275 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001276 }
1277 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001278 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1279 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1280 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001281 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1283 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001284 UseScratchRegisterScope temps(GetVIXLAssembler());
1285 HConstant* src_cst = source.GetConstant();
1286 CPURegister temp;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001287 if (src_cst->IsIntConstant() || src_cst->IsNullConstant()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001288 temp = temps.AcquireW();
1289 } else if (src_cst->IsLongConstant()) {
1290 temp = temps.AcquireX();
1291 } else if (src_cst->IsFloatConstant()) {
1292 temp = temps.AcquireS();
1293 } else {
1294 DCHECK(src_cst->IsDoubleConstant());
1295 temp = temps.AcquireD();
1296 }
1297 MoveConstant(temp, src_cst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001298 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001299 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001300 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001301 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001302 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001303 // There is generally less pressure on FP registers.
1304 FPRegister temp = destination.IsDoubleStackSlot() ? temps.AcquireD() : temps.AcquireS();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001305 __ Ldr(temp, StackOperandFrom(source));
1306 __ Str(temp, StackOperandFrom(destination));
1307 }
1308 }
1309}
1310
1311void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001312 CPURegister dst,
1313 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001314 switch (type) {
1315 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001316 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001317 break;
1318 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001319 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001320 break;
1321 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001322 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001323 break;
1324 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001325 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001326 break;
1327 case Primitive::kPrimInt:
1328 case Primitive::kPrimNot:
1329 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001330 case Primitive::kPrimFloat:
1331 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001332 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001333 __ Ldr(dst, src);
1334 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001335 case Primitive::kPrimVoid:
1336 LOG(FATAL) << "Unreachable type " << type;
1337 }
1338}
1339
Calin Juravle77520bc2015-01-12 18:45:46 +00001340void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001341 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001342 const MemOperand& src,
1343 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001344 MacroAssembler* masm = GetVIXLAssembler();
1345 BlockPoolsScope block_pools(masm);
1346 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001347 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001348 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001349
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001350 DCHECK(!src.IsPreIndex());
1351 DCHECK(!src.IsPostIndex());
1352
1353 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001354 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001355 MemOperand base = MemOperand(temp_base);
1356 switch (type) {
1357 case Primitive::kPrimBoolean:
1358 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001359 if (needs_null_check) {
1360 MaybeRecordImplicitNullCheck(instruction);
1361 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001362 break;
1363 case Primitive::kPrimByte:
1364 __ Ldarb(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001365 if (needs_null_check) {
1366 MaybeRecordImplicitNullCheck(instruction);
1367 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001368 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1369 break;
1370 case Primitive::kPrimChar:
1371 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001372 if (needs_null_check) {
1373 MaybeRecordImplicitNullCheck(instruction);
1374 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001375 break;
1376 case Primitive::kPrimShort:
1377 __ Ldarh(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001378 if (needs_null_check) {
1379 MaybeRecordImplicitNullCheck(instruction);
1380 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001381 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1382 break;
1383 case Primitive::kPrimInt:
1384 case Primitive::kPrimNot:
1385 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001386 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001387 __ Ldar(Register(dst), base);
Roland Levillain44015862016-01-22 11:47:17 +00001388 if (needs_null_check) {
1389 MaybeRecordImplicitNullCheck(instruction);
1390 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001391 break;
1392 case Primitive::kPrimFloat:
1393 case Primitive::kPrimDouble: {
1394 DCHECK(dst.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001395 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001396
1397 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1398 __ Ldar(temp, 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 __ Fmov(FPRegister(dst), temp);
1403 break;
1404 }
1405 case Primitive::kPrimVoid:
1406 LOG(FATAL) << "Unreachable type " << type;
1407 }
1408}
1409
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001410void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001411 CPURegister src,
1412 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001413 switch (type) {
1414 case Primitive::kPrimBoolean:
1415 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001416 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001417 break;
1418 case Primitive::kPrimChar:
1419 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001420 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001421 break;
1422 case Primitive::kPrimInt:
1423 case Primitive::kPrimNot:
1424 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001425 case Primitive::kPrimFloat:
1426 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001427 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001428 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00001429 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001430 case Primitive::kPrimVoid:
1431 LOG(FATAL) << "Unreachable type " << type;
1432 }
1433}
1434
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001435void CodeGeneratorARM64::StoreRelease(Primitive::Type type,
1436 CPURegister src,
1437 const MemOperand& dst) {
1438 UseScratchRegisterScope temps(GetVIXLAssembler());
1439 Register temp_base = temps.AcquireX();
1440
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001441 DCHECK(!dst.IsPreIndex());
1442 DCHECK(!dst.IsPostIndex());
1443
1444 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001445 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001446 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001447 MemOperand base = MemOperand(temp_base);
1448 switch (type) {
1449 case Primitive::kPrimBoolean:
1450 case Primitive::kPrimByte:
1451 __ Stlrb(Register(src), base);
1452 break;
1453 case Primitive::kPrimChar:
1454 case Primitive::kPrimShort:
1455 __ Stlrh(Register(src), base);
1456 break;
1457 case Primitive::kPrimInt:
1458 case Primitive::kPrimNot:
1459 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00001460 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001461 __ Stlr(Register(src), base);
1462 break;
1463 case Primitive::kPrimFloat:
1464 case Primitive::kPrimDouble: {
1465 DCHECK(src.IsFPRegister());
Alexandre Rames542361f2015-01-29 16:57:31 +00001466 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001467
1468 Register temp = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1469 __ Fmov(temp, FPRegister(src));
1470 __ Stlr(temp, base);
1471 break;
1472 }
1473 case Primitive::kPrimVoid:
1474 LOG(FATAL) << "Unreachable type " << type;
1475 }
1476}
1477
Calin Juravle175dc732015-08-25 15:42:32 +01001478void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1479 HInstruction* instruction,
1480 uint32_t dex_pc,
1481 SlowPathCode* slow_path) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001482 InvokeRuntime(GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value(),
Calin Juravle175dc732015-08-25 15:42:32 +01001483 instruction,
1484 dex_pc,
1485 slow_path);
1486}
1487
Alexandre Rames67555f72014-11-18 10:55:16 +00001488void CodeGeneratorARM64::InvokeRuntime(int32_t entry_point_offset,
1489 HInstruction* instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001490 uint32_t dex_pc,
1491 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001492 ValidateInvokeRuntime(instruction, slow_path);
Alexandre Ramesd921d642015-04-16 15:07:16 +01001493 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames67555f72014-11-18 10:55:16 +00001494 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1495 __ Blr(lr);
Roland Levillain896e32d2015-05-05 18:07:10 +01001496 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00001497}
1498
Roland Levillaindec8f632016-07-22 17:10:06 +01001499void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1500 HInstruction* instruction,
1501 SlowPathCode* slow_path) {
1502 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1503 BlockPoolsScope block_pools(GetVIXLAssembler());
1504 __ Ldr(lr, MemOperand(tr, entry_point_offset));
1505 __ Blr(lr);
1506}
1507
Alexandre Rames67555f72014-11-18 10:55:16 +00001508void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001509 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001510 UseScratchRegisterScope temps(GetVIXLAssembler());
1511 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001512 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
1513
Serban Constantinescu02164b32014-11-13 14:05:07 +00001514 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001515 // TODO(vixl): Let the MacroAssembler handle MemOperand.
1516 __ Add(temp, class_reg, status_offset);
1517 __ Ldar(temp, HeapOperand(temp));
1518 __ Cmp(temp, mirror::Class::kStatusInitialized);
1519 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00001520 __ Bind(slow_path->GetExitLabel());
1521}
Alexandre Rames5319def2014-10-23 10:03:10 +01001522
Roland Levillain44015862016-01-22 11:47:17 +00001523void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001524 BarrierType type = BarrierAll;
1525
1526 switch (kind) {
1527 case MemBarrierKind::kAnyAny:
1528 case MemBarrierKind::kAnyStore: {
1529 type = BarrierAll;
1530 break;
1531 }
1532 case MemBarrierKind::kLoadAny: {
1533 type = BarrierReads;
1534 break;
1535 }
1536 case MemBarrierKind::kStoreStore: {
1537 type = BarrierWrites;
1538 break;
1539 }
1540 default:
1541 LOG(FATAL) << "Unexpected memory barrier " << kind;
1542 }
1543 __ Dmb(InnerShareable, type);
1544}
1545
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
1547 HBasicBlock* successor) {
1548 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001549 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
1550 if (slow_path == nullptr) {
1551 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
1552 instruction->SetSlowPath(slow_path);
1553 codegen_->AddSlowPath(slow_path);
1554 if (successor != nullptr) {
1555 DCHECK(successor->IsLoopHeader());
1556 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
1557 }
1558 } else {
1559 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1560 }
1561
Serban Constantinescu02164b32014-11-13 14:05:07 +00001562 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
1563 Register temp = temps.AcquireW();
1564
Andreas Gampe542451c2016-07-26 09:02:02 -07001565 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00001566 if (successor == nullptr) {
1567 __ Cbnz(temp, slow_path->GetEntryLabel());
1568 __ Bind(slow_path->GetReturnLabel());
1569 } else {
1570 __ Cbz(temp, codegen_->GetLabelOf(successor));
1571 __ B(slow_path->GetEntryLabel());
1572 // slow_path will return to GetLabelOf(successor).
1573 }
1574}
1575
Alexandre Rames5319def2014-10-23 10:03:10 +01001576InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
1577 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001578 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01001579 assembler_(codegen->GetAssembler()),
1580 codegen_(codegen) {}
1581
1582#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00001583 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01001584
1585#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
1586
1587enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00001588 // Using a base helps identify when we hit such breakpoints.
1589 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01001590#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
1591 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
1592#undef ENUM_UNIMPLEMENTED_INSTRUCTION
1593};
1594
1595#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001596 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01001597 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
1598 } \
1599 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
1600 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
1601 locations->SetOut(Location::Any()); \
1602 }
1603 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
1604#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
1605
1606#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00001607#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01001608
Alexandre Rames67555f72014-11-18 10:55:16 +00001609void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001610 DCHECK_EQ(instr->InputCount(), 2U);
1611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1612 Primitive::Type type = instr->GetResultType();
1613 switch (type) {
1614 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001615 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01001616 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00001617 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00001618 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001619 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001620
1621 case Primitive::kPrimFloat:
1622 case Primitive::kPrimDouble:
1623 locations->SetInAt(0, Location::RequiresFpuRegister());
1624 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00001625 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01001626 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001627
Alexandre Rames5319def2014-10-23 10:03:10 +01001628 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001629 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 }
1631}
1632
Alexandre Rames09a99962015-04-15 11:47:56 +01001633void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001634 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
1635
1636 bool object_field_get_with_read_barrier =
1637 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01001638 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001639 new (GetGraph()->GetArena()) LocationSummary(instruction,
1640 object_field_get_with_read_barrier ?
1641 LocationSummary::kCallOnSlowPath :
1642 LocationSummary::kNoCall);
Alexandre Rames09a99962015-04-15 11:47:56 +01001643 locations->SetInAt(0, Location::RequiresRegister());
1644 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1645 locations->SetOut(Location::RequiresFpuRegister());
1646 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001647 // The output overlaps for an object field get when read barriers
1648 // are enabled: we do not want the load to overwrite the object's
1649 // location, as we need it to emit the read barrier.
1650 locations->SetOut(
1651 Location::RequiresRegister(),
1652 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01001653 }
1654}
1655
1656void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
1657 const FieldInfo& field_info) {
1658 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00001659 LocationSummary* locations = instruction->GetLocations();
1660 Location base_loc = locations->InAt(0);
1661 Location out = locations->Out();
1662 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01001663 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001664 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001665 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01001666
Roland Levillain44015862016-01-22 11:47:17 +00001667 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1668 // Object FieldGet with Baker's read barrier case.
1669 MacroAssembler* masm = GetVIXLAssembler();
1670 UseScratchRegisterScope temps(masm);
1671 // /* HeapReference<Object> */ out = *(base + offset)
1672 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
1673 Register temp = temps.AcquireW();
1674 // Note that potential implicit null checks are handled in this
1675 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
1676 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1677 instruction,
1678 out,
1679 base,
1680 offset,
1681 temp,
1682 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001683 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00001684 } else {
1685 // General case.
1686 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001687 // Note that a potential implicit null check is handled in this
1688 // CodeGeneratorARM64::LoadAcquire call.
1689 // NB: LoadAcquire will record the pc info if needed.
1690 codegen_->LoadAcquire(
1691 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01001692 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01001693 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01001694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01001695 }
Roland Levillain44015862016-01-22 11:47:17 +00001696 if (field_type == Primitive::kPrimNot) {
1697 // If read barriers are enabled, emit read barriers other than
1698 // Baker's using a slow path (and also unpoison the loaded
1699 // reference, if heap poisoning is enabled).
1700 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
1701 }
Roland Levillain4d027112015-07-01 15:41:14 +01001702 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001703}
1704
1705void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
1706 LocationSummary* locations =
1707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1708 locations->SetInAt(0, Location::RequiresRegister());
1709 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
1710 locations->SetInAt(1, Location::RequiresFpuRegister());
1711 } else {
1712 locations->SetInAt(1, Location::RequiresRegister());
1713 }
1714}
1715
1716void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001717 const FieldInfo& field_info,
1718 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01001719 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
Alexandre Ramesd921d642015-04-16 15:07:16 +01001720 BlockPoolsScope block_pools(GetVIXLAssembler());
Alexandre Rames09a99962015-04-15 11:47:56 +01001721
1722 Register obj = InputRegisterAt(instruction, 0);
1723 CPURegister value = InputCPURegisterAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01001724 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01001725 Offset offset = field_info.GetFieldOffset();
1726 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01001727
Roland Levillain4d027112015-07-01 15:41:14 +01001728 {
1729 // We use a block to end the scratch scope before the write barrier, thus
1730 // freeing the temporary registers so they can be used in `MarkGCCard`.
1731 UseScratchRegisterScope temps(GetVIXLAssembler());
1732
1733 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
1734 DCHECK(value.IsW());
1735 Register temp = temps.AcquireW();
1736 __ Mov(temp, value.W());
1737 GetAssembler()->PoisonHeapReference(temp.W());
1738 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01001739 }
Roland Levillain4d027112015-07-01 15:41:14 +01001740
1741 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001742 codegen_->StoreRelease(field_type, source, HeapOperand(obj, offset));
1743 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01001744 } else {
1745 codegen_->Store(field_type, source, HeapOperand(obj, offset));
1746 codegen_->MaybeRecordImplicitNullCheck(instruction);
1747 }
Alexandre Rames09a99962015-04-15 11:47:56 +01001748 }
1749
1750 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001751 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01001752 }
1753}
1754
Alexandre Rames67555f72014-11-18 10:55:16 +00001755void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001756 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01001757
1758 switch (type) {
1759 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001760 case Primitive::kPrimLong: {
1761 Register dst = OutputRegister(instr);
1762 Register lhs = InputRegisterAt(instr, 0);
1763 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01001764 if (instr->IsAdd()) {
1765 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001766 } else if (instr->IsAnd()) {
1767 __ And(dst, lhs, rhs);
1768 } else if (instr->IsOr()) {
1769 __ Orr(dst, lhs, rhs);
1770 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01001771 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001772 } else if (instr->IsRor()) {
1773 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001774 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001775 __ Ror(dst, lhs, shift);
1776 } else {
1777 // Ensure shift distance is in the same size register as the result. If
1778 // we are rotating a long and the shift comes in a w register originally,
1779 // we don't need to sxtw for use as an x since the shift distances are
1780 // all & reg_bits - 1.
1781 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
1782 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001783 } else {
1784 DCHECK(instr->IsXor());
1785 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01001786 }
1787 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001788 }
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble: {
1791 FPRegister dst = OutputFPRegister(instr);
1792 FPRegister lhs = InputFPRegisterAt(instr, 0);
1793 FPRegister rhs = InputFPRegisterAt(instr, 1);
1794 if (instr->IsAdd()) {
1795 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001796 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001797 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00001798 } else {
1799 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001800 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001801 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001802 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001803 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00001804 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01001805 }
1806}
1807
Serban Constantinescu02164b32014-11-13 14:05:07 +00001808void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
1809 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1810
1811 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1812 Primitive::Type type = instr->GetResultType();
1813 switch (type) {
1814 case Primitive::kPrimInt:
1815 case Primitive::kPrimLong: {
1816 locations->SetInAt(0, Location::RequiresRegister());
1817 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1818 locations->SetOut(Location::RequiresRegister());
1819 break;
1820 }
1821 default:
1822 LOG(FATAL) << "Unexpected shift type " << type;
1823 }
1824}
1825
1826void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
1827 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1828
1829 Primitive::Type type = instr->GetType();
1830 switch (type) {
1831 case Primitive::kPrimInt:
1832 case Primitive::kPrimLong: {
1833 Register dst = OutputRegister(instr);
1834 Register lhs = InputRegisterAt(instr, 0);
1835 Operand rhs = InputOperandAt(instr, 1);
1836 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001837 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00001838 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001839 if (instr->IsShl()) {
1840 __ Lsl(dst, lhs, shift_value);
1841 } else if (instr->IsShr()) {
1842 __ Asr(dst, lhs, shift_value);
1843 } else {
1844 __ Lsr(dst, lhs, shift_value);
1845 }
1846 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001847 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001848
1849 if (instr->IsShl()) {
1850 __ Lsl(dst, lhs, rhs_reg);
1851 } else if (instr->IsShr()) {
1852 __ Asr(dst, lhs, rhs_reg);
1853 } else {
1854 __ Lsr(dst, lhs, rhs_reg);
1855 }
1856 }
1857 break;
1858 }
1859 default:
1860 LOG(FATAL) << "Unexpected shift operation type " << type;
1861 }
1862}
1863
Alexandre Rames5319def2014-10-23 10:03:10 +01001864void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001865 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001866}
1867
1868void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001869 HandleBinaryOp(instruction);
1870}
1871
1872void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
1873 HandleBinaryOp(instruction);
1874}
1875
1876void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
1877 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01001878}
1879
Artem Serov7fc63502016-02-09 17:15:29 +00001880void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001881 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
1882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1883 locations->SetInAt(0, Location::RequiresRegister());
1884 // There is no immediate variant of negated bitwise instructions in AArch64.
1885 locations->SetInAt(1, Location::RequiresRegister());
1886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1887}
1888
Artem Serov7fc63502016-02-09 17:15:29 +00001889void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00001890 Register dst = OutputRegister(instr);
1891 Register lhs = InputRegisterAt(instr, 0);
1892 Register rhs = InputRegisterAt(instr, 1);
1893
1894 switch (instr->GetOpKind()) {
1895 case HInstruction::kAnd:
1896 __ Bic(dst, lhs, rhs);
1897 break;
1898 case HInstruction::kOr:
1899 __ Orn(dst, lhs, rhs);
1900 break;
1901 case HInstruction::kXor:
1902 __ Eon(dst, lhs, rhs);
1903 break;
1904 default:
1905 LOG(FATAL) << "Unreachable";
1906 }
1907}
1908
Alexandre Rames8626b742015-11-25 16:28:08 +00001909void LocationsBuilderARM64::VisitArm64DataProcWithShifterOp(
1910 HArm64DataProcWithShifterOp* instruction) {
1911 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
1912 instruction->GetType() == Primitive::kPrimLong);
1913 LocationSummary* locations =
1914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1915 if (instruction->GetInstrKind() == HInstruction::kNeg) {
1916 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
1917 } else {
1918 locations->SetInAt(0, Location::RequiresRegister());
1919 }
1920 locations->SetInAt(1, Location::RequiresRegister());
1921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1922}
1923
1924void InstructionCodeGeneratorARM64::VisitArm64DataProcWithShifterOp(
1925 HArm64DataProcWithShifterOp* instruction) {
1926 Primitive::Type type = instruction->GetType();
1927 HInstruction::InstructionKind kind = instruction->GetInstrKind();
1928 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
1929 Register out = OutputRegister(instruction);
1930 Register left;
1931 if (kind != HInstruction::kNeg) {
1932 left = InputRegisterAt(instruction, 0);
1933 }
1934 // If this `HArm64DataProcWithShifterOp` was created by merging a type conversion as the
1935 // shifter operand operation, the IR generating `right_reg` (input to the type
1936 // conversion) can have a different type from the current instruction's type,
1937 // so we manually indicate the type.
1938 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Roland Levillain5b5b9312016-03-22 14:57:31 +00001939 int64_t shift_amount = instruction->GetShiftAmount() &
1940 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Alexandre Rames8626b742015-11-25 16:28:08 +00001941
1942 Operand right_operand(0);
1943
1944 HArm64DataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
1945 if (HArm64DataProcWithShifterOp::IsExtensionOp(op_kind)) {
1946 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
1947 } else {
1948 right_operand = Operand(right_reg, helpers::ShiftFromOpKind(op_kind), shift_amount);
1949 }
1950
1951 // Logical binary operations do not support extension operations in the
1952 // operand. Note that VIXL would still manage if it was passed by generating
1953 // the extension as a separate instruction.
1954 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
1955 DCHECK(!right_operand.IsExtendedRegister() ||
1956 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
1957 kind != HInstruction::kNeg));
1958 switch (kind) {
1959 case HInstruction::kAdd:
1960 __ Add(out, left, right_operand);
1961 break;
1962 case HInstruction::kAnd:
1963 __ And(out, left, right_operand);
1964 break;
1965 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00001966 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00001967 __ Neg(out, right_operand);
1968 break;
1969 case HInstruction::kOr:
1970 __ Orr(out, left, right_operand);
1971 break;
1972 case HInstruction::kSub:
1973 __ Sub(out, left, right_operand);
1974 break;
1975 case HInstruction::kXor:
1976 __ Eor(out, left, right_operand);
1977 break;
1978 default:
1979 LOG(FATAL) << "Unexpected operation kind: " << kind;
1980 UNREACHABLE();
1981 }
1982}
1983
Artem Serov328429f2016-07-06 16:23:04 +01001984void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
1985 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001986 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001987 LocationSummary* locations =
1988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
1991 locations->SetOut(Location::RequiresRegister());
1992}
1993
Artem Serov328429f2016-07-06 16:23:04 +01001994void InstructionCodeGeneratorARM64::VisitIntermediateAddress(
1995 HIntermediateAddress* instruction) {
1996 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001997 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001998 __ Add(OutputRegister(instruction),
1999 InputRegisterAt(instruction, 0),
2000 Operand(InputOperandAt(instruction, 1)));
2001}
2002
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002003void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002004 LocationSummary* locations =
2005 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002006 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2007 if (instr->GetOpKind() == HInstruction::kSub &&
2008 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002009 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002010 // Don't allocate register for Mneg instruction.
2011 } else {
2012 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2013 Location::RequiresRegister());
2014 }
2015 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2016 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2018}
2019
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002020void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002021 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002022 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2023 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002024
2025 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2026 // This fixup should be carried out for all multiply-accumulate instructions:
2027 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2028 if (instr->GetType() == Primitive::kPrimLong &&
2029 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2030 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002031 vixl::aarch64::Instruction* prev =
2032 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002033 if (prev->IsLoadOrStore()) {
2034 // Make sure we emit only exactly one nop.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035 vixl::aarch64::CodeBufferCheckScope scope(masm,
2036 kInstructionSize,
2037 vixl::aarch64::CodeBufferCheckScope::kCheck,
2038 vixl::aarch64::CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002039 __ nop();
2040 }
2041 }
2042
2043 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002044 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002045 __ Madd(res, mul_left, mul_right, accumulator);
2046 } else {
2047 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002048 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002049 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002050 __ Mneg(res, mul_left, mul_right);
2051 } else {
2052 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2053 __ Msub(res, mul_left, mul_right, accumulator);
2054 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002055 }
2056}
2057
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002058void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002059 bool object_array_get_with_read_barrier =
2060 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002061 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002062 new (GetGraph()->GetArena()) LocationSummary(instruction,
2063 object_array_get_with_read_barrier ?
2064 LocationSummary::kCallOnSlowPath :
2065 LocationSummary::kNoCall);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002066 locations->SetInAt(0, Location::RequiresRegister());
2067 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002068 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2069 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2070 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002071 // The output overlaps in the case of an object array get with
2072 // read barriers enabled: we do not want the move to overwrite the
2073 // array's location, as we need it to emit the read barrier.
2074 locations->SetOut(
2075 Location::RequiresRegister(),
2076 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002077 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002078}
2079
2080void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002081 Primitive::Type type = instruction->GetType();
2082 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002083 LocationSummary* locations = instruction->GetLocations();
2084 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002085 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002086 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002087
Alexandre Ramesd921d642015-04-16 15:07:16 +01002088 MacroAssembler* masm = GetVIXLAssembler();
2089 UseScratchRegisterScope temps(masm);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002090 // Block pools between `Load` and `MaybeRecordImplicitNullCheck`.
Alexandre Ramesd921d642015-04-16 15:07:16 +01002091 BlockPoolsScope block_pools(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002092
Roland Levillain44015862016-01-22 11:47:17 +00002093 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2094 // Object ArrayGet with Baker's read barrier case.
2095 Register temp = temps.AcquireW();
Artem Serov328429f2016-07-06 16:23:04 +01002096 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
2097 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Roland Levillain44015862016-01-22 11:47:17 +00002098 // Note that a potential implicit null check is handled in the
2099 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
2100 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2101 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002102 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002103 // General case.
2104 MemOperand source = HeapOperand(obj);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002105 if (index.IsConstant()) {
Roland Levillain44015862016-01-22 11:47:17 +00002106 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2107 source = HeapOperand(obj, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002108 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002109 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002110 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002111 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002112 // HIntermediateAddress instruction yet.
Roland Levillain44015862016-01-22 11:47:17 +00002113 DCHECK(!kEmitCompilerReadBarrier);
2114 // We do not need to compute the intermediate address from the array: the
2115 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002116 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002117 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002118 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002119 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2120 }
2121 temp = obj;
2122 } else {
2123 __ Add(temp, obj, offset);
2124 }
2125 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2126 }
2127
2128 codegen_->Load(type, OutputCPURegister(instruction), source);
2129 codegen_->MaybeRecordImplicitNullCheck(instruction);
2130
2131 if (type == Primitive::kPrimNot) {
2132 static_assert(
2133 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2134 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2135 Location obj_loc = locations->InAt(0);
2136 if (index.IsConstant()) {
2137 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2138 } else {
2139 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2140 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002141 }
Roland Levillain4d027112015-07-01 15:41:14 +01002142 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002143}
2144
Alexandre Rames5319def2014-10-23 10:03:10 +01002145void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2147 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002148 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002149}
2150
2151void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002152 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Alexandre Ramesd921d642015-04-16 15:07:16 +01002153 BlockPoolsScope block_pools(GetVIXLAssembler());
Vladimir Markodce016e2016-04-28 13:10:02 +01002154 __ Ldr(OutputRegister(instruction), HeapOperand(InputRegisterAt(instruction, 0), offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00002155 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002156}
2157
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002158void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002159 Primitive::Type value_type = instruction->GetComponentType();
2160
2161 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2162 bool object_array_set_with_read_barrier =
2163 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2165 instruction,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002166 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
2167 LocationSummary::kCallOnSlowPath :
2168 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002169 locations->SetInAt(0, Location::RequiresRegister());
2170 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002171 if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002172 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002173 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002174 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002175 }
2176}
2177
2178void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2179 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002180 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002181 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002182 bool needs_write_barrier =
2183 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002184
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002185 Register array = InputRegisterAt(instruction, 0);
2186 CPURegister value = InputCPURegisterAt(instruction, 2);
2187 CPURegister source = value;
2188 Location index = locations->InAt(1);
2189 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2190 MemOperand destination = HeapOperand(array);
2191 MacroAssembler* masm = GetVIXLAssembler();
2192 BlockPoolsScope block_pools(masm);
2193
2194 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002195 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002196 if (index.IsConstant()) {
2197 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2198 destination = HeapOperand(array, offset);
2199 } else {
2200 UseScratchRegisterScope temps(masm);
2201 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002202 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002203 // The read barrier instrumentation does not support the
Artem Serov328429f2016-07-06 16:23:04 +01002204 // HIntermediateAddress instruction yet.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00002205 DCHECK(!kEmitCompilerReadBarrier);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002206 // We do not need to compute the intermediate address from the array: the
2207 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002208 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002209 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002210 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002211 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2212 }
2213 temp = array;
2214 } else {
2215 __ Add(temp, array, offset);
2216 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002217 destination = HeapOperand(temp,
2218 XRegisterFrom(index),
2219 LSL,
2220 Primitive::ComponentSizeShift(value_type));
2221 }
2222 codegen_->Store(value_type, value, destination);
2223 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002224 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002225 DCHECK(needs_write_barrier);
Artem Serov328429f2016-07-06 16:23:04 +01002226 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002227 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002228 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002229 {
2230 // We use a block to end the scratch scope before the write barrier, thus
2231 // freeing the temporary registers so they can be used in `MarkGCCard`.
2232 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002233 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002234 if (index.IsConstant()) {
2235 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002236 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002237 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002238 destination = HeapOperand(temp,
2239 XRegisterFrom(index),
2240 LSL,
2241 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002242 }
2243
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002244 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2245 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2246 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2247
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002248 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002249 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2250 codegen_->AddSlowPath(slow_path);
2251 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002252 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002253 __ Cbnz(Register(value), &non_zero);
2254 if (!index.IsConstant()) {
2255 __ Add(temp, array, offset);
2256 }
2257 __ Str(wzr, destination);
2258 codegen_->MaybeRecordImplicitNullCheck(instruction);
2259 __ B(&done);
2260 __ Bind(&non_zero);
2261 }
2262
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002263 if (kEmitCompilerReadBarrier) {
2264 // When read barriers are enabled, the type checking
2265 // instrumentation requires two read barriers:
2266 //
2267 // __ Mov(temp2, temp);
2268 // // /* HeapReference<Class> */ temp = temp->component_type_
2269 // __ Ldr(temp, HeapOperand(temp, component_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002270 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002271 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
2272 //
2273 // // /* HeapReference<Class> */ temp2 = value->klass_
2274 // __ Ldr(temp2, HeapOperand(Register(value), class_offset));
Roland Levillain44015862016-01-22 11:47:17 +00002275 // codegen_->GenerateReadBarrierSlow(
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002276 // instruction, temp2_loc, temp2_loc, value_loc, class_offset, temp_loc);
2277 //
2278 // __ Cmp(temp, temp2);
2279 //
2280 // However, the second read barrier may trash `temp`, as it
2281 // is a temporary register, and as such would not be saved
2282 // along with live registers before calling the runtime (nor
2283 // restored afterwards). So in this case, we bail out and
2284 // delegate the work to the array set slow path.
2285 //
2286 // TODO: Extend the register allocator to support a new
2287 // "(locally) live temp" location so as to avoid always
2288 // going into the slow path when read barriers are enabled.
2289 __ B(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002290 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002291 Register temp2 = temps.AcquireSameSizeAs(array);
2292 // /* HeapReference<Class> */ temp = array->klass_
2293 __ Ldr(temp, HeapOperand(array, class_offset));
2294 codegen_->MaybeRecordImplicitNullCheck(instruction);
2295 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2296
2297 // /* HeapReference<Class> */ temp = temp->component_type_
2298 __ Ldr(temp, HeapOperand(temp, component_offset));
2299 // /* HeapReference<Class> */ temp2 = value->klass_
2300 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
2301 // If heap poisoning is enabled, no need to unpoison `temp`
2302 // nor `temp2`, as we are comparing two poisoned references.
2303 __ Cmp(temp, temp2);
2304
2305 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002306 vixl::aarch64::Label do_put;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002307 __ B(eq, &do_put);
2308 // If heap poisoning is enabled, the `temp` reference has
2309 // not been unpoisoned yet; unpoison it now.
2310 GetAssembler()->MaybeUnpoisonHeapReference(temp);
2311
2312 // /* HeapReference<Class> */ temp = temp->super_class_
2313 __ Ldr(temp, HeapOperand(temp, super_offset));
2314 // If heap poisoning is enabled, no need to unpoison
2315 // `temp`, as we are comparing against null below.
2316 __ Cbnz(temp, slow_path->GetEntryLabel());
2317 __ Bind(&do_put);
2318 } else {
2319 __ B(ne, slow_path->GetEntryLabel());
2320 }
2321 temps.Release(temp2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002322 }
2323 }
2324
2325 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002326 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002327 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01002328 __ Mov(temp2, value.W());
2329 GetAssembler()->PoisonHeapReference(temp2);
2330 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002331 }
2332
2333 if (!index.IsConstant()) {
2334 __ Add(temp, array, offset);
2335 }
Nicolas Geoffray61b1dbe2015-10-01 10:27:52 +01002336 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002337
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002338 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002339 codegen_->MaybeRecordImplicitNullCheck(instruction);
2340 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002341 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002342
2343 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
2344
2345 if (done.IsLinked()) {
2346 __ Bind(&done);
2347 }
2348
2349 if (slow_path != nullptr) {
2350 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01002351 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002352 }
2353}
2354
Alexandre Rames67555f72014-11-18 10:55:16 +00002355void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002356 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2357 ? LocationSummary::kCallOnSlowPath
2358 : LocationSummary::kNoCall;
2359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002360 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00002361 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00002362 if (instruction->HasUses()) {
2363 locations->SetOut(Location::SameAsFirstInput());
2364 }
2365}
2366
2367void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002368 BoundsCheckSlowPathARM64* slow_path =
2369 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00002370 codegen_->AddSlowPath(slow_path);
2371
2372 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
2373 __ B(slow_path->GetEntryLabel(), hs);
2374}
2375
Alexandre Rames67555f72014-11-18 10:55:16 +00002376void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
2377 LocationSummary* locations =
2378 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
2379 locations->SetInAt(0, Location::RequiresRegister());
2380 if (check->HasUses()) {
2381 locations->SetOut(Location::SameAsFirstInput());
2382 }
2383}
2384
2385void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
2386 // We assume the class is not null.
2387 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
2388 check->GetLoadClass(), check, check->GetDexPc(), true);
2389 codegen_->AddSlowPath(slow_path);
2390 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
2391}
2392
Roland Levillain1a653882016-03-18 18:05:57 +00002393static bool IsFloatingPointZeroConstant(HInstruction* inst) {
2394 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
2395 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
2396}
2397
2398void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
2399 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
2400 Location rhs_loc = instruction->GetLocations()->InAt(1);
2401 if (rhs_loc.IsConstant()) {
2402 // 0.0 is the only immediate that can be encoded directly in
2403 // an FCMP instruction.
2404 //
2405 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
2406 // specify that in a floating-point comparison, positive zero
2407 // and negative zero are considered equal, so we can use the
2408 // literal 0.0 for both cases here.
2409 //
2410 // Note however that some methods (Float.equal, Float.compare,
2411 // Float.compareTo, Double.equal, Double.compare,
2412 // Double.compareTo, Math.max, Math.min, StrictMath.max,
2413 // StrictMath.min) consider 0.0 to be (strictly) greater than
2414 // -0.0. So if we ever translate calls to these methods into a
2415 // HCompare instruction, we must handle the -0.0 case with
2416 // care here.
2417 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
2418 __ Fcmp(lhs_reg, 0.0);
2419 } else {
2420 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
2421 }
Roland Levillain7f63c522015-07-13 15:54:55 +00002422}
2423
Serban Constantinescu02164b32014-11-13 14:05:07 +00002424void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002425 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00002426 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
2427 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002429 case Primitive::kPrimBoolean:
2430 case Primitive::kPrimByte:
2431 case Primitive::kPrimShort:
2432 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002433 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01002434 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002435 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002436 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2438 break;
2439 }
2440 case Primitive::kPrimFloat:
2441 case Primitive::kPrimDouble: {
2442 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00002443 locations->SetInAt(1,
2444 IsFloatingPointZeroConstant(compare->InputAt(1))
2445 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
2446 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00002447 locations->SetOut(Location::RequiresRegister());
2448 break;
2449 }
2450 default:
2451 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
2452 }
2453}
2454
2455void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
2456 Primitive::Type in_type = compare->InputAt(0)->GetType();
2457
2458 // 0 if: left == right
2459 // 1 if: left > right
2460 // -1 if: left < right
2461 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00002462 case Primitive::kPrimBoolean:
2463 case Primitive::kPrimByte:
2464 case Primitive::kPrimShort:
2465 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08002466 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00002467 case Primitive::kPrimLong: {
2468 Register result = OutputRegister(compare);
2469 Register left = InputRegisterAt(compare, 0);
2470 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002471 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08002472 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
2473 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00002474 break;
2475 }
2476 case Primitive::kPrimFloat:
2477 case Primitive::kPrimDouble: {
2478 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00002479 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002480 __ Cset(result, ne);
2481 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01002482 break;
2483 }
2484 default:
2485 LOG(FATAL) << "Unimplemented compare type " << in_type;
2486 }
2487}
2488
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002489void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002490 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00002491
2492 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
2493 locations->SetInAt(0, Location::RequiresFpuRegister());
2494 locations->SetInAt(1,
2495 IsFloatingPointZeroConstant(instruction->InputAt(1))
2496 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
2497 : Location::RequiresFpuRegister());
2498 } else {
2499 // Integer cases.
2500 locations->SetInAt(0, Location::RequiresRegister());
2501 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
2502 }
2503
David Brazdilb3e773e2016-01-26 11:28:37 +00002504 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002506 }
2507}
2508
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002509void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002510 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002511 return;
2512 }
2513
2514 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01002515 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00002516 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01002517
Roland Levillain7f63c522015-07-13 15:54:55 +00002518 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00002519 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002520 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00002521 } else {
2522 // Integer cases.
2523 Register lhs = InputRegisterAt(instruction, 0);
2524 Operand rhs = InputOperandAt(instruction, 1);
2525 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00002526 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00002527 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
2530#define FOR_EACH_CONDITION_INSTRUCTION(M) \
2531 M(Equal) \
2532 M(NotEqual) \
2533 M(LessThan) \
2534 M(LessThanOrEqual) \
2535 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07002536 M(GreaterThanOrEqual) \
2537 M(Below) \
2538 M(BelowOrEqual) \
2539 M(Above) \
2540 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01002541#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002542void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
2543void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01002544FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00002545#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01002546#undef FOR_EACH_CONDITION_INSTRUCTION
2547
Zheng Xuc6667102015-05-15 16:08:45 +08002548void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2549 DCHECK(instruction->IsDiv() || instruction->IsRem());
2550
2551 LocationSummary* locations = instruction->GetLocations();
2552 Location second = locations->InAt(1);
2553 DCHECK(second.IsConstant());
2554
2555 Register out = OutputRegister(instruction);
2556 Register dividend = InputRegisterAt(instruction, 0);
2557 int64_t imm = Int64FromConstant(second.GetConstant());
2558 DCHECK(imm == 1 || imm == -1);
2559
2560 if (instruction->IsRem()) {
2561 __ Mov(out, 0);
2562 } else {
2563 if (imm == 1) {
2564 __ Mov(out, dividend);
2565 } else {
2566 __ Neg(out, dividend);
2567 }
2568 }
2569}
2570
2571void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2572 DCHECK(instruction->IsDiv() || instruction->IsRem());
2573
2574 LocationSummary* locations = instruction->GetLocations();
2575 Location second = locations->InAt(1);
2576 DCHECK(second.IsConstant());
2577
2578 Register out = OutputRegister(instruction);
2579 Register dividend = InputRegisterAt(instruction, 0);
2580 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002581 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002582 int ctz_imm = CTZ(abs_imm);
2583
2584 UseScratchRegisterScope temps(GetVIXLAssembler());
2585 Register temp = temps.AcquireSameSizeAs(out);
2586
2587 if (instruction->IsDiv()) {
2588 __ Add(temp, dividend, abs_imm - 1);
2589 __ Cmp(dividend, 0);
2590 __ Csel(out, temp, dividend, lt);
2591 if (imm > 0) {
2592 __ Asr(out, out, ctz_imm);
2593 } else {
2594 __ Neg(out, Operand(out, ASR, ctz_imm));
2595 }
2596 } else {
2597 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
2598 __ Asr(temp, dividend, bits - 1);
2599 __ Lsr(temp, temp, bits - ctz_imm);
2600 __ Add(out, dividend, temp);
2601 __ And(out, out, abs_imm - 1);
2602 __ Sub(out, out, temp);
2603 }
2604}
2605
2606void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2607 DCHECK(instruction->IsDiv() || instruction->IsRem());
2608
2609 LocationSummary* locations = instruction->GetLocations();
2610 Location second = locations->InAt(1);
2611 DCHECK(second.IsConstant());
2612
2613 Register out = OutputRegister(instruction);
2614 Register dividend = InputRegisterAt(instruction, 0);
2615 int64_t imm = Int64FromConstant(second.GetConstant());
2616
2617 Primitive::Type type = instruction->GetResultType();
2618 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2619
2620 int64_t magic;
2621 int shift;
2622 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
2623
2624 UseScratchRegisterScope temps(GetVIXLAssembler());
2625 Register temp = temps.AcquireSameSizeAs(out);
2626
2627 // temp = get_high(dividend * magic)
2628 __ Mov(temp, magic);
2629 if (type == Primitive::kPrimLong) {
2630 __ Smulh(temp, dividend, temp);
2631 } else {
2632 __ Smull(temp.X(), dividend, temp);
2633 __ Lsr(temp.X(), temp.X(), 32);
2634 }
2635
2636 if (imm > 0 && magic < 0) {
2637 __ Add(temp, temp, dividend);
2638 } else if (imm < 0 && magic > 0) {
2639 __ Sub(temp, temp, dividend);
2640 }
2641
2642 if (shift != 0) {
2643 __ Asr(temp, temp, shift);
2644 }
2645
2646 if (instruction->IsDiv()) {
2647 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2648 } else {
2649 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
2650 // TODO: Strength reduction for msub.
2651 Register temp_imm = temps.AcquireSameSizeAs(out);
2652 __ Mov(temp_imm, imm);
2653 __ Msub(out, temp, temp_imm, dividend);
2654 }
2655}
2656
2657void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2658 DCHECK(instruction->IsDiv() || instruction->IsRem());
2659 Primitive::Type type = instruction->GetResultType();
2660 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
2661
2662 LocationSummary* locations = instruction->GetLocations();
2663 Register out = OutputRegister(instruction);
2664 Location second = locations->InAt(1);
2665
2666 if (second.IsConstant()) {
2667 int64_t imm = Int64FromConstant(second.GetConstant());
2668
2669 if (imm == 0) {
2670 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2671 } else if (imm == 1 || imm == -1) {
2672 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002673 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08002674 DivRemByPowerOfTwo(instruction);
2675 } else {
2676 DCHECK(imm <= -2 || imm >= 2);
2677 GenerateDivRemWithAnyConstant(instruction);
2678 }
2679 } else {
2680 Register dividend = InputRegisterAt(instruction, 0);
2681 Register divisor = InputRegisterAt(instruction, 1);
2682 if (instruction->IsDiv()) {
2683 __ Sdiv(out, dividend, divisor);
2684 } else {
2685 UseScratchRegisterScope temps(GetVIXLAssembler());
2686 Register temp = temps.AcquireSameSizeAs(out);
2687 __ Sdiv(temp, dividend, divisor);
2688 __ Msub(out, temp, divisor, dividend);
2689 }
2690 }
2691}
2692
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002693void LocationsBuilderARM64::VisitDiv(HDiv* div) {
2694 LocationSummary* locations =
2695 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
2696 switch (div->GetResultType()) {
2697 case Primitive::kPrimInt:
2698 case Primitive::kPrimLong:
2699 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08002700 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002701 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2702 break;
2703
2704 case Primitive::kPrimFloat:
2705 case Primitive::kPrimDouble:
2706 locations->SetInAt(0, Location::RequiresFpuRegister());
2707 locations->SetInAt(1, Location::RequiresFpuRegister());
2708 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
2713 }
2714}
2715
2716void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
2717 Primitive::Type type = div->GetResultType();
2718 switch (type) {
2719 case Primitive::kPrimInt:
2720 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08002721 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002722 break;
2723
2724 case Primitive::kPrimFloat:
2725 case Primitive::kPrimDouble:
2726 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
2727 break;
2728
2729 default:
2730 LOG(FATAL) << "Unexpected div type " << type;
2731 }
2732}
2733
Alexandre Rames67555f72014-11-18 10:55:16 +00002734void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002735 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2736 ? LocationSummary::kCallOnSlowPath
2737 : LocationSummary::kNoCall;
2738 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames67555f72014-11-18 10:55:16 +00002739 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2740 if (instruction->HasUses()) {
2741 locations->SetOut(Location::SameAsFirstInput());
2742 }
2743}
2744
2745void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2746 SlowPathCodeARM64* slow_path =
2747 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
2748 codegen_->AddSlowPath(slow_path);
2749 Location value = instruction->GetLocations()->InAt(0);
2750
Alexandre Rames3e69f162014-12-10 10:36:50 +00002751 Primitive::Type type = instruction->GetType();
2752
Nicolas Geoffraye5671612016-03-16 11:03:54 +00002753 if (!Primitive::IsIntegralType(type)) {
2754 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00002755 return;
2756 }
2757
Alexandre Rames67555f72014-11-18 10:55:16 +00002758 if (value.IsConstant()) {
2759 int64_t divisor = Int64ConstantFrom(value);
2760 if (divisor == 0) {
2761 __ B(slow_path->GetEntryLabel());
2762 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00002763 // A division by a non-null constant is valid. We don't need to perform
2764 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00002765 }
2766 } else {
2767 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
2768 }
2769}
2770
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002771void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
2772 LocationSummary* locations =
2773 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2774 locations->SetOut(Location::ConstantLocation(constant));
2775}
2776
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002777void InstructionCodeGeneratorARM64::VisitDoubleConstant(
2778 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002779 // Will be generated at use site.
2780}
2781
Alexandre Rames5319def2014-10-23 10:03:10 +01002782void LocationsBuilderARM64::VisitExit(HExit* exit) {
2783 exit->SetLocations(nullptr);
2784}
2785
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002786void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002787}
2788
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002789void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
2790 LocationSummary* locations =
2791 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2792 locations->SetOut(Location::ConstantLocation(constant));
2793}
2794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002795void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002796 // Will be generated at use site.
2797}
2798
David Brazdilfc6a86a2015-06-26 10:33:45 +00002799void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002800 DCHECK(!successor->IsExitBlock());
2801 HBasicBlock* block = got->GetBlock();
2802 HInstruction* previous = got->GetPrevious();
2803 HLoopInformation* info = block->GetLoopInformation();
2804
David Brazdil46e2a392015-03-16 17:31:52 +00002805 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00002806 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2807 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2808 return;
2809 }
2810 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2811 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2812 }
2813 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002814 __ B(codegen_->GetLabelOf(successor));
2815 }
2816}
2817
David Brazdilfc6a86a2015-06-26 10:33:45 +00002818void LocationsBuilderARM64::VisitGoto(HGoto* got) {
2819 got->SetLocations(nullptr);
2820}
2821
2822void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
2823 HandleGoto(got, got->GetSuccessor());
2824}
2825
2826void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2827 try_boundary->SetLocations(nullptr);
2828}
2829
2830void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
2831 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2832 if (!successor->IsExitBlock()) {
2833 HandleGoto(try_boundary, successor);
2834 }
2835}
2836
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002837void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002838 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002839 vixl::aarch64::Label* true_target,
2840 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002841 // FP branching requires both targets to be explicit. If either of the targets
2842 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002843 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002844 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002845
David Brazdil0debae72015-11-12 18:37:00 +00002846 if (true_target == nullptr && false_target == nullptr) {
2847 // Nothing to do. The code always falls through.
2848 return;
2849 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002850 // Constant condition, statically compared against "true" (integer value 1).
2851 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002852 if (true_target != nullptr) {
2853 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002854 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002855 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002856 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002857 if (false_target != nullptr) {
2858 __ B(false_target);
2859 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00002860 }
David Brazdil0debae72015-11-12 18:37:00 +00002861 return;
2862 }
2863
2864 // The following code generates these patterns:
2865 // (1) true_target == nullptr && false_target != nullptr
2866 // - opposite condition true => branch to false_target
2867 // (2) true_target != nullptr && false_target == nullptr
2868 // - condition true => branch to true_target
2869 // (3) true_target != nullptr && false_target != nullptr
2870 // - condition true => branch to true_target
2871 // - branch to false_target
2872 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002873 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00002874 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01002875 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00002876 if (true_target == nullptr) {
2877 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
2878 } else {
2879 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
2880 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002881 } else {
2882 // The condition instruction has not been materialized, use its inputs as
2883 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00002884 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00002885
David Brazdil0debae72015-11-12 18:37:00 +00002886 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00002887 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00002888 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00002889 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002890 IfCondition opposite_condition = condition->GetOppositeCondition();
2891 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00002892 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00002893 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00002894 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002895 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00002896 // Integer cases.
2897 Register lhs = InputRegisterAt(condition, 0);
2898 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00002899
2900 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002901 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00002902 if (true_target == nullptr) {
2903 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
2904 non_fallthrough_target = false_target;
2905 } else {
2906 arm64_cond = ARM64Condition(condition->GetCondition());
2907 non_fallthrough_target = true_target;
2908 }
2909
Aart Bik086d27e2016-01-20 17:02:00 -08002910 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01002911 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00002912 switch (arm64_cond) {
2913 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00002914 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002915 break;
2916 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00002917 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002918 break;
2919 case lt:
2920 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002921 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002922 break;
2923 case ge:
2924 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00002925 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002926 break;
2927 default:
2928 // Without the `static_cast` the compiler throws an error for
2929 // `-Werror=sign-promo`.
2930 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
2931 }
2932 } else {
2933 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002934 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00002935 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002936 }
2937 }
David Brazdil0debae72015-11-12 18:37:00 +00002938
2939 // If neither branch falls through (case 3), the conditional branch to `true_target`
2940 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2941 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002942 __ B(false_target);
2943 }
David Brazdil0debae72015-11-12 18:37:00 +00002944
2945 if (fallthrough_target.IsLinked()) {
2946 __ Bind(&fallthrough_target);
2947 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002948}
2949
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002950void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
2951 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002952 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953 locations->SetInAt(0, Location::RequiresRegister());
2954 }
2955}
2956
2957void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002958 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2959 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002960 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
2961 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
2962 true_target = nullptr;
2963 }
2964 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
2965 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
2966 false_target = nullptr;
2967 }
David Brazdil0debae72015-11-12 18:37:00 +00002968 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002969}
2970
2971void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
2972 LocationSummary* locations = new (GetGraph()->GetArena())
2973 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00002974 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002975 locations->SetInAt(0, Location::RequiresRegister());
2976 }
2977}
2978
2979void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002980 SlowPathCodeARM64* slow_path =
2981 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002982 GenerateTestAndBranch(deoptimize,
2983 /* condition_input_index */ 0,
2984 slow_path->GetEntryLabel(),
2985 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002986}
2987
David Brazdilc0b601b2016-02-08 14:20:45 +00002988static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
2989 return condition->IsCondition() &&
2990 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
2991}
2992
Alexandre Rames880f1192016-06-13 16:04:50 +01002993static inline Condition GetConditionForSelect(HCondition* condition) {
2994 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00002995 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
2996 : ARM64Condition(cond);
2997}
2998
David Brazdil74eb1b22015-12-14 11:44:01 +00002999void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003001 if (Primitive::IsFloatingPointType(select->GetType())) {
3002 locations->SetInAt(0, Location::RequiresFpuRegister());
3003 locations->SetInAt(1, Location::RequiresFpuRegister());
3004 locations->SetOut(Location::RequiresFpuRegister());
3005 } else {
3006 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3007 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3008 bool is_true_value_constant = cst_true_value != nullptr;
3009 bool is_false_value_constant = cst_false_value != nullptr;
3010 // Ask VIXL whether we should synthesize constants in registers.
3011 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3012 Operand true_op = is_true_value_constant ?
3013 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3014 Operand false_op = is_false_value_constant ?
3015 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3016 bool true_value_in_register = false;
3017 bool false_value_in_register = false;
3018 MacroAssembler::GetCselSynthesisInformation(
3019 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3020 true_value_in_register |= !is_true_value_constant;
3021 false_value_in_register |= !is_false_value_constant;
3022
3023 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3024 : Location::ConstantLocation(cst_true_value));
3025 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3026 : Location::ConstantLocation(cst_false_value));
3027 locations->SetOut(Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00003028 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003029
David Brazdil74eb1b22015-12-14 11:44:01 +00003030 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3031 locations->SetInAt(2, Location::RequiresRegister());
3032 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003033}
3034
3035void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003036 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003037 Condition csel_cond;
3038
3039 if (IsBooleanValueOrMaterializedCondition(cond)) {
3040 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003041 // Use the condition flags set by the previous instruction.
3042 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003043 } else {
3044 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003045 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003046 }
3047 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003048 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003049 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003050 } else {
3051 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003052 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003053 }
3054
Alexandre Rames880f1192016-06-13 16:04:50 +01003055 if (Primitive::IsFloatingPointType(select->GetType())) {
3056 __ Fcsel(OutputFPRegister(select),
3057 InputFPRegisterAt(select, 1),
3058 InputFPRegisterAt(select, 0),
3059 csel_cond);
3060 } else {
3061 __ Csel(OutputRegister(select),
3062 InputOperandAt(select, 1),
3063 InputOperandAt(select, 0),
3064 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003065 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003066}
3067
David Srbecky0cf44932015-12-09 14:09:59 +00003068void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3069 new (GetGraph()->GetArena()) LocationSummary(info);
3070}
3071
David Srbeckyd28f4a02016-03-14 17:14:24 +00003072void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3073 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003074}
3075
3076void CodeGeneratorARM64::GenerateNop() {
3077 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003078}
3079
Alexandre Rames5319def2014-10-23 10:03:10 +01003080void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003081 HandleFieldGet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003082}
3083
3084void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003085 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003086}
3087
3088void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003089 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003090}
3091
3092void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003093 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003094}
3095
Roland Levillain44015862016-01-22 11:47:17 +00003096static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
3097 return kEmitCompilerReadBarrier &&
3098 (kUseBakerReadBarrier ||
3099 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3100 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3101 type_check_kind == TypeCheckKind::kArrayObjectCheck);
3102}
3103
Alexandre Rames67555f72014-11-18 10:55:16 +00003104void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003105 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003106 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3107 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003108 case TypeCheckKind::kExactCheck:
3109 case TypeCheckKind::kAbstractClassCheck:
3110 case TypeCheckKind::kClassHierarchyCheck:
3111 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003112 call_kind =
3113 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003114 break;
3115 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003116 case TypeCheckKind::kUnresolvedCheck:
3117 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003118 call_kind = LocationSummary::kCallOnSlowPath;
3119 break;
3120 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003121
Alexandre Rames67555f72014-11-18 10:55:16 +00003122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003123 locations->SetInAt(0, Location::RequiresRegister());
3124 locations->SetInAt(1, Location::RequiresRegister());
3125 // The "out" register is used as a temporary, so it overlaps with the inputs.
3126 // Note that TypeCheckSlowPathARM64 uses this register too.
3127 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3128 // When read barriers are enabled, we need a temporary register for
3129 // some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003130 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003131 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003132 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003133}
3134
3135void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003136 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003137 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003138 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003139 Register obj = InputRegisterAt(instruction, 0);
3140 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003141 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003142 Register out = OutputRegister(instruction);
Roland Levillain44015862016-01-22 11:47:17 +00003143 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3144 locations->GetTemp(0) :
3145 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003146 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3147 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3148 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3149 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003150
Scott Wakeling97c72b72016-06-24 16:19:36 +01003151 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003152 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003153
3154 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003155 // Avoid null check if we know `obj` is not null.
3156 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003157 __ Cbz(obj, &zero);
3158 }
3159
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003160 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003161 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003162
Roland Levillain44015862016-01-22 11:47:17 +00003163 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003164 case TypeCheckKind::kExactCheck: {
3165 __ Cmp(out, cls);
3166 __ Cset(out, eq);
3167 if (zero.IsLinked()) {
3168 __ B(&done);
3169 }
3170 break;
3171 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003172
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003173 case TypeCheckKind::kAbstractClassCheck: {
3174 // If the class is abstract, we eagerly fetch the super class of the
3175 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003176 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003177 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003178 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003179 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003180 // If `out` is null, we use it for the result, and jump to `done`.
3181 __ Cbz(out, &done);
3182 __ Cmp(out, cls);
3183 __ B(ne, &loop);
3184 __ Mov(out, 1);
3185 if (zero.IsLinked()) {
3186 __ B(&done);
3187 }
3188 break;
3189 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003190
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003191 case TypeCheckKind::kClassHierarchyCheck: {
3192 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003193 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003194 __ Bind(&loop);
3195 __ Cmp(out, cls);
3196 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003197 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003198 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003199 __ Cbnz(out, &loop);
3200 // If `out` is null, we use it for the result, and jump to `done`.
3201 __ B(&done);
3202 __ Bind(&success);
3203 __ Mov(out, 1);
3204 if (zero.IsLinked()) {
3205 __ B(&done);
3206 }
3207 break;
3208 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003210 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003211 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003212 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003213 __ Cmp(out, cls);
3214 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003215 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003216 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003217 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003218 // If `out` is null, we use it for the result, and jump to `done`.
3219 __ Cbz(out, &done);
3220 __ Ldrh(out, HeapOperand(out, primitive_offset));
3221 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3222 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003223 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003224 __ Mov(out, 1);
3225 __ B(&done);
3226 break;
3227 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003229 case TypeCheckKind::kArrayCheck: {
3230 __ Cmp(out, cls);
3231 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003232 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3233 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003234 codegen_->AddSlowPath(slow_path);
3235 __ B(ne, slow_path->GetEntryLabel());
3236 __ Mov(out, 1);
3237 if (zero.IsLinked()) {
3238 __ B(&done);
3239 }
3240 break;
3241 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003242
Calin Juravle98893e12015-10-02 21:05:03 +01003243 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003244 case TypeCheckKind::kInterfaceCheck: {
3245 // Note that we indeed only call on slow path, but we always go
3246 // into the slow path for the unresolved and interface check
3247 // cases.
3248 //
3249 // We cannot directly call the InstanceofNonTrivial runtime
3250 // entry point without resorting to a type checking slow path
3251 // here (i.e. by calling InvokeRuntime directly), as it would
3252 // require to assign fixed registers for the inputs of this
3253 // HInstanceOf instruction (following the runtime calling
3254 // convention), which might be cluttered by the potential first
3255 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003256 //
3257 // TODO: Introduce a new runtime entry point taking the object
3258 // to test (instead of its class) as argument, and let it deal
3259 // with the read barrier issues. This will let us refactor this
3260 // case of the `switch` code as it was previously (with a direct
3261 // call to the runtime not using a type checking slow path).
3262 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003263 DCHECK(locations->OnlyCallsOnSlowPath());
3264 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3265 /* is_fatal */ false);
3266 codegen_->AddSlowPath(slow_path);
3267 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003268 if (zero.IsLinked()) {
3269 __ B(&done);
3270 }
3271 break;
3272 }
3273 }
3274
3275 if (zero.IsLinked()) {
3276 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003277 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003278 }
3279
3280 if (done.IsLinked()) {
3281 __ Bind(&done);
3282 }
3283
3284 if (slow_path != nullptr) {
3285 __ Bind(slow_path->GetExitLabel());
3286 }
3287}
3288
3289void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
3290 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3291 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3292
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003293 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3294 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003295 case TypeCheckKind::kExactCheck:
3296 case TypeCheckKind::kAbstractClassCheck:
3297 case TypeCheckKind::kClassHierarchyCheck:
3298 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003299 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
3300 LocationSummary::kCallOnSlowPath :
3301 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003302 break;
3303 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003304 case TypeCheckKind::kUnresolvedCheck:
3305 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003306 call_kind = LocationSummary::kCallOnSlowPath;
3307 break;
3308 }
3309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3311 locations->SetInAt(0, Location::RequiresRegister());
3312 locations->SetInAt(1, Location::RequiresRegister());
3313 // Note that TypeCheckSlowPathARM64 uses this "temp" register too.
3314 locations->AddTemp(Location::RequiresRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003315 // When read barriers are enabled, we need an additional temporary
3316 // register for some cases.
Roland Levillain44015862016-01-22 11:47:17 +00003317 if (TypeCheckNeedsATemporary(type_check_kind)) {
3318 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003319 }
3320}
3321
3322void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003323 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003324 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003325 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003326 Register obj = InputRegisterAt(instruction, 0);
3327 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003328 Location temp_loc = locations->GetTemp(0);
Roland Levillain44015862016-01-22 11:47:17 +00003329 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
3330 locations->GetTemp(1) :
3331 Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003332 Register temp = WRegisterFrom(temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003333 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3334 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3335 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3336 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003337
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003338 bool is_type_check_slow_path_fatal =
3339 (type_check_kind == TypeCheckKind::kExactCheck ||
3340 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3341 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3342 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3343 !instruction->CanThrowIntoCatchBlock();
3344 SlowPathCodeARM64* type_check_slow_path =
3345 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
3346 is_type_check_slow_path_fatal);
3347 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003348
Scott Wakeling97c72b72016-06-24 16:19:36 +01003349 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003350 // Avoid null check if we know obj is not null.
3351 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003352 __ Cbz(obj, &done);
3353 }
Alexandre Rames67555f72014-11-18 10:55:16 +00003354
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003355 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003356 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray75374372015-09-17 17:12:19 +00003357
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003358 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003359 case TypeCheckKind::kExactCheck:
3360 case TypeCheckKind::kArrayCheck: {
3361 __ Cmp(temp, cls);
3362 // Jump to slow path for throwing the exception or doing a
3363 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003364 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003365 break;
3366 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003367
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003368 case TypeCheckKind::kAbstractClassCheck: {
3369 // If the class is abstract, we eagerly fetch the super class of the
3370 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003371 vixl::aarch64::Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003372 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003373 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003374 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003375
3376 // If the class reference currently in `temp` is not null, jump
3377 // to the `compare_classes` label to compare it with the checked
3378 // class.
3379 __ Cbnz(temp, &compare_classes);
3380 // Otherwise, jump to the slow path to throw the exception.
3381 //
3382 // But before, move back the object's class into `temp` before
3383 // going into the slow path, as it has been overwritten in the
3384 // meantime.
3385 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003386 GenerateReferenceLoadTwoRegisters(
3387 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003388 __ B(type_check_slow_path->GetEntryLabel());
3389
3390 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003391 __ Cmp(temp, cls);
3392 __ B(ne, &loop);
3393 break;
3394 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003395
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003396 case TypeCheckKind::kClassHierarchyCheck: {
3397 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003398 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003399 __ Bind(&loop);
3400 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003401 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003402
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003403 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain44015862016-01-22 11:47:17 +00003404 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003405
3406 // If the class reference currently in `temp` is not null, jump
3407 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003408 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003409 // Otherwise, jump to the slow path to throw the exception.
3410 //
3411 // But before, move back the object's class into `temp` before
3412 // going into the slow path, as it has been overwritten in the
3413 // meantime.
3414 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003415 GenerateReferenceLoadTwoRegisters(
3416 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003417 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003418 break;
3419 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003421 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003422 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003423 vixl::aarch64::Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003424 __ Cmp(temp, cls);
3425 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003426
3427 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003428 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain44015862016-01-22 11:47:17 +00003429 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003430
3431 // If the component type is not null (i.e. the object is indeed
3432 // an array), jump to label `check_non_primitive_component_type`
3433 // to further check that this component type is not a primitive
3434 // type.
3435 __ Cbnz(temp, &check_non_primitive_component_type);
3436 // Otherwise, jump to the slow path to throw the exception.
3437 //
3438 // But before, move back the object's class into `temp` before
3439 // going into the slow path, as it has been overwritten in the
3440 // meantime.
3441 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003442 GenerateReferenceLoadTwoRegisters(
3443 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003444 __ B(type_check_slow_path->GetEntryLabel());
3445
3446 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003447 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
3448 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003449 __ Cbz(temp, &done);
3450 // Same comment as above regarding `temp` and the slow path.
3451 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain44015862016-01-22 11:47:17 +00003452 GenerateReferenceLoadTwoRegisters(
3453 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003454 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003455 break;
3456 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003457
Calin Juravle98893e12015-10-02 21:05:03 +01003458 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003459 case TypeCheckKind::kInterfaceCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003460 // We always go into the type check slow path for the unresolved
3461 // and interface check cases.
3462 //
3463 // We cannot directly call the CheckCast runtime entry point
3464 // without resorting to a type checking slow path here (i.e. by
3465 // calling InvokeRuntime directly), as it would require to
3466 // assign fixed registers for the inputs of this HInstanceOf
3467 // instruction (following the runtime calling convention), which
3468 // might be cluttered by the potential first read barrier
3469 // emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00003470 //
3471 // TODO: Introduce a new runtime entry point taking the object
3472 // to test (instead of its class) as argument, and let it deal
3473 // with the read barrier issues. This will let us refactor this
3474 // case of the `switch` code as it was previously (with a direct
3475 // call to the runtime not using a type checking slow path).
3476 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003477 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003478 break;
3479 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00003480 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003481
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003482 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00003483}
3484
Alexandre Rames5319def2014-10-23 10:03:10 +01003485void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
3486 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3487 locations->SetOut(Location::ConstantLocation(constant));
3488}
3489
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003490void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003491 // Will be generated at use site.
3492}
3493
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003494void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
3495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3496 locations->SetOut(Location::ConstantLocation(constant));
3497}
3498
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003499void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003500 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003501}
3502
Calin Juravle175dc732015-08-25 15:42:32 +01003503void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3504 // The trampoline uses the same calling convention as dex calling conventions,
3505 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3506 // the method_idx.
3507 HandleInvoke(invoke);
3508}
3509
3510void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3511 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3512}
3513
Alexandre Rames5319def2014-10-23 10:03:10 +01003514void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003515 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003516 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01003517}
3518
Alexandre Rames67555f72014-11-18 10:55:16 +00003519void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3520 HandleInvoke(invoke);
3521}
3522
3523void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
3524 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003525 LocationSummary* locations = invoke->GetLocations();
3526 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003527 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00003528 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003529 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00003530
3531 // The register ip1 is required to be used for the hidden argument in
3532 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01003533 MacroAssembler* masm = GetVIXLAssembler();
3534 UseScratchRegisterScope scratch_scope(masm);
3535 BlockPoolsScope block_pools(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00003536 scratch_scope.Exclude(ip1);
3537 __ Mov(ip1, invoke->GetDexMethodIndex());
3538
Alexandre Rames67555f72014-11-18 10:55:16 +00003539 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003540 __ Ldr(temp.W(), StackOperandFrom(receiver));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003541 // /* HeapReference<Class> */ temp = temp->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003542 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003543 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003544 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07003545 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003546 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003547 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003548 // Instead of simply (possibly) unpoisoning `temp` here, we should
3549 // emit a read barrier for the previous class reference load.
3550 // However this is not required in practice, as this is an
3551 // intermediate/temporary reference and because the current
3552 // concurrent copying collector keeps the from-space memory
3553 // intact/accessible until the end of the marking phase (the
3554 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003555 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003556 __ Ldr(temp,
3557 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
3558 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003559 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00003560 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003561 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00003562 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003563 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Alexandre Rames67555f72014-11-18 10:55:16 +00003564 // lr();
3565 __ Blr(lr);
3566 DCHECK(!codegen_->IsLeafMethod());
3567 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3568}
3569
3570void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003571 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3572 if (intrinsic.TryDispatch(invoke)) {
3573 return;
3574 }
3575
Alexandre Rames67555f72014-11-18 10:55:16 +00003576 HandleInvoke(invoke);
3577}
3578
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003579void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003580 // Explicit clinit checks triggered by static invokes must have been pruned by
3581 // art::PrepareForRegisterAllocation.
3582 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003583
Andreas Gampe878d58c2015-01-15 23:24:00 -08003584 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena());
3585 if (intrinsic.TryDispatch(invoke)) {
3586 return;
3587 }
3588
Alexandre Rames67555f72014-11-18 10:55:16 +00003589 HandleInvoke(invoke);
3590}
3591
Andreas Gampe878d58c2015-01-15 23:24:00 -08003592static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
3593 if (invoke->GetLocations()->Intrinsified()) {
3594 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
3595 intrinsic.Dispatch(invoke);
3596 return true;
3597 }
3598 return false;
3599}
3600
Vladimir Markodc151b22015-10-15 18:02:30 +01003601HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
3602 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3603 MethodReference target_method ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00003604 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01003605 return desired_dispatch_info;
3606}
3607
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003608void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00003609 // For better instruction scheduling we load the direct code pointer before the method pointer.
3610 bool direct_code_loaded = false;
3611 switch (invoke->GetCodePtrLocation()) {
3612 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3613 // LR = code address from literal pool with link-time patch.
3614 __ Ldr(lr, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
3615 direct_code_loaded = true;
3616 break;
3617 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3618 // LR = invoke->GetDirectCodePtr();
3619 __ Ldr(lr, DeduplicateUint64Literal(invoke->GetDirectCodePtr()));
3620 direct_code_loaded = true;
3621 break;
3622 default:
3623 break;
3624 }
3625
Andreas Gampe878d58c2015-01-15 23:24:00 -08003626 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00003627 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3628 switch (invoke->GetMethodLoadKind()) {
3629 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3630 // temp = thread->string_init_entrypoint
Alexandre Rames6dc01742015-11-12 14:44:19 +00003631 __ Ldr(XRegisterFrom(temp), MemOperand(tr, invoke->GetStringInitOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003632 break;
3633 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00003634 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003635 break;
3636 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3637 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003638 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00003639 break;
3640 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3641 // Load method address from literal pool with a link-time patch.
Alexandre Rames6dc01742015-11-12 14:44:19 +00003642 __ Ldr(XRegisterFrom(temp),
Vladimir Marko58155012015-08-19 12:49:41 +00003643 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
3644 break;
3645 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3646 // Add ADRP with its PC-relative DexCache access patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003647 const DexFile& dex_file = *invoke->GetTargetMethod().dex_file;
3648 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003649 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Marko58155012015-08-19 12:49:41 +00003650 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003651 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003652 __ Bind(adrp_label);
3653 __ adrp(XRegisterFrom(temp), /* offset placeholder */ 0);
Vladimir Marko58155012015-08-19 12:49:41 +00003654 }
Vladimir Marko58155012015-08-19 12:49:41 +00003655 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003656 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003657 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Alexandre Rames6dc01742015-11-12 14:44:19 +00003658 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003659 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003660 __ Bind(ldr_label);
3661 __ ldr(XRegisterFrom(temp), MemOperand(XRegisterFrom(temp), /* offset placeholder */ 0));
Alexandre Rames6dc01742015-11-12 14:44:19 +00003662 }
Vladimir Marko58155012015-08-19 12:49:41 +00003663 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003664 }
Vladimir Marko58155012015-08-19 12:49:41 +00003665 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00003666 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00003667 Register reg = XRegisterFrom(temp);
3668 Register method_reg;
3669 if (current_method.IsRegister()) {
3670 method_reg = XRegisterFrom(current_method);
3671 } else {
3672 DCHECK(invoke->GetLocations()->Intrinsified());
3673 DCHECK(!current_method.IsValid());
3674 method_reg = reg;
3675 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
3676 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00003677
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003678 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003679 __ Ldr(reg.X(),
3680 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07003681 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003682 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01003683 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
3684 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00003685 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
3686 break;
3687 }
3688 }
3689
3690 switch (invoke->GetCodePtrLocation()) {
3691 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3692 __ Bl(&frame_entry_label_);
3693 break;
3694 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3695 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003696 vixl::aarch64::Label* label = &relative_call_patches_.back().label;
3697 SingleEmissionCheckScope guard(GetVIXLAssembler());
Alexandre Rames6dc01742015-11-12 14:44:19 +00003698 __ Bind(label);
3699 __ bl(0); // Branch and link to itself. This will be overriden at link time.
Vladimir Marko58155012015-08-19 12:49:41 +00003700 break;
3701 }
3702 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3703 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3704 // LR prepared above for better instruction scheduling.
3705 DCHECK(direct_code_loaded);
3706 // lr()
3707 __ Blr(lr);
3708 break;
3709 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3710 // LR = callee_method->entry_point_from_quick_compiled_code_;
3711 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00003712 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07003713 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003714 // lr()
3715 __ Blr(lr);
3716 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003717 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003718
Andreas Gampe878d58c2015-01-15 23:24:00 -08003719 DCHECK(!IsLeafMethod());
3720}
3721
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003722void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003723 // Use the calling convention instead of the location of the receiver, as
3724 // intrinsics may have put the receiver in a different register. In the intrinsics
3725 // slow path, the arguments have been moved to the right place, so here we are
3726 // guaranteed that the receiver is the first register of the calling convention.
3727 InvokeDexCallingConvention calling_convention;
3728 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003729 Register temp = XRegisterFrom(temp_in);
3730 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3731 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
3732 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07003733 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003734
3735 BlockPoolsScope block_pools(GetVIXLAssembler());
3736
3737 DCHECK(receiver.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003738 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00003739 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003740 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003741 // Instead of simply (possibly) unpoisoning `temp` here, we should
3742 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003743 // intermediate/temporary reference and because the current
3744 // concurrent copying collector keeps the from-space memory
3745 // intact/accessible until the end of the marking phase (the
3746 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003747 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
3748 // temp = temp->GetMethodAt(method_offset);
3749 __ Ldr(temp, MemOperand(temp, method_offset));
3750 // lr = temp->GetEntryPoint();
3751 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
3752 // lr();
3753 __ Blr(lr);
3754}
3755
Scott Wakeling97c72b72016-06-24 16:19:36 +01003756vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
3757 const DexFile& dex_file,
3758 uint32_t string_index,
3759 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003760 return NewPcRelativePatch(dex_file, string_index, adrp_label, &pc_relative_string_patches_);
3761}
3762
Scott Wakeling97c72b72016-06-24 16:19:36 +01003763vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
3764 const DexFile& dex_file,
3765 uint32_t type_index,
3766 vixl::aarch64::Label* adrp_label) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003767 return NewPcRelativePatch(dex_file, type_index, adrp_label, &pc_relative_type_patches_);
3768}
3769
Scott Wakeling97c72b72016-06-24 16:19:36 +01003770vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
3771 const DexFile& dex_file,
3772 uint32_t element_offset,
3773 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003774 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
3775}
3776
Scott Wakeling97c72b72016-06-24 16:19:36 +01003777vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
3778 const DexFile& dex_file,
3779 uint32_t offset_or_index,
3780 vixl::aarch64::Label* adrp_label,
3781 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003782 // Add a patch entry and return the label.
3783 patches->emplace_back(dex_file, offset_or_index);
3784 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003785 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003786 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
3787 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
3788 return label;
3789}
3790
Scott Wakeling97c72b72016-06-24 16:19:36 +01003791vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003792 const DexFile& dex_file, uint32_t string_index) {
3793 return boot_image_string_patches_.GetOrCreate(
3794 StringReference(&dex_file, string_index),
3795 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3796}
3797
Scott Wakeling97c72b72016-06-24 16:19:36 +01003798vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003799 const DexFile& dex_file, uint32_t type_index) {
3800 return boot_image_type_patches_.GetOrCreate(
3801 TypeReference(&dex_file, type_index),
3802 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
3803}
3804
Scott Wakeling97c72b72016-06-24 16:19:36 +01003805vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
3806 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003807 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
3808 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
3809 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
3810}
3811
Scott Wakeling97c72b72016-06-24 16:19:36 +01003812vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateDexCacheAddressLiteral(
3813 uint64_t address) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003814 return DeduplicateUint64Literal(address);
3815}
3816
Vladimir Marko58155012015-08-19 12:49:41 +00003817void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3818 DCHECK(linker_patches->empty());
3819 size_t size =
3820 method_patches_.size() +
3821 call_patches_.size() +
3822 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003823 pc_relative_dex_cache_patches_.size() +
3824 boot_image_string_patches_.size() +
3825 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003826 boot_image_type_patches_.size() +
3827 pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003828 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00003829 linker_patches->reserve(size);
3830 for (const auto& entry : method_patches_) {
3831 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003832 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3833 linker_patches->push_back(LinkerPatch::MethodPatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003834 target_method.dex_file,
3835 target_method.dex_method_index));
3836 }
3837 for (const auto& entry : call_patches_) {
3838 const MethodReference& target_method = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003839 vixl::aarch64::Literal<uint64_t>* literal = entry.second;
3840 linker_patches->push_back(LinkerPatch::CodePatch(literal->GetOffset(),
Vladimir Marko58155012015-08-19 12:49:41 +00003841 target_method.dex_file,
3842 target_method.dex_method_index));
3843 }
Scott Wakeling97c72b72016-06-24 16:19:36 +01003844 for (const MethodPatchInfo<vixl::aarch64::Label>& info : relative_call_patches_) {
3845 linker_patches->push_back(LinkerPatch::RelativeCodePatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003846 info.target_method.dex_file,
3847 info.target_method.dex_method_index));
3848 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003849 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003850 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00003851 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003852 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003853 info.offset_or_index));
3854 }
3855 for (const auto& entry : boot_image_string_patches_) {
3856 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003857 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3858 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003859 target_string.dex_file,
3860 target_string.string_index));
3861 }
3862 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003863 linker_patches->push_back(LinkerPatch::RelativeStringPatch(info.label.GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003864 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003865 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003866 info.offset_or_index));
3867 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003868 for (const auto& entry : boot_image_type_patches_) {
3869 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003870 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3871 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003872 target_type.dex_file,
3873 target_type.type_index));
3874 }
3875 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003876 linker_patches->push_back(LinkerPatch::RelativeTypePatch(info.label.GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003877 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003878 info.pc_insn_label->GetLocation(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003879 info.offset_or_index));
3880 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003881 for (const auto& entry : boot_image_address_patches_) {
3882 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003883 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
3884 linker_patches->push_back(LinkerPatch::RecordPosition(literal->GetOffset()));
Vladimir Marko58155012015-08-19 12:49:41 +00003885 }
3886}
3887
Scott Wakeling97c72b72016-06-24 16:19:36 +01003888vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003889 Uint32ToLiteralMap* map) {
3890 return map->GetOrCreate(
3891 value,
3892 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
3893}
3894
Scott Wakeling97c72b72016-06-24 16:19:36 +01003895vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003896 return uint64_literals_.GetOrCreate(
3897 value,
3898 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00003899}
3900
Scott Wakeling97c72b72016-06-24 16:19:36 +01003901vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003902 MethodReference target_method,
3903 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00003904 return map->GetOrCreate(
3905 target_method,
3906 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00003907}
3908
Scott Wakeling97c72b72016-06-24 16:19:36 +01003909vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodAddressLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003910 MethodReference target_method) {
3911 return DeduplicateMethodLiteral(target_method, &method_patches_);
3912}
3913
Scott Wakeling97c72b72016-06-24 16:19:36 +01003914vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodCodeLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00003915 MethodReference target_method) {
3916 return DeduplicateMethodLiteral(target_method, &call_patches_);
3917}
3918
3919
Andreas Gampe878d58c2015-01-15 23:24:00 -08003920void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003921 // Explicit clinit checks triggered by static invokes must have been pruned by
3922 // art::PrepareForRegisterAllocation.
3923 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003924
Andreas Gampe878d58c2015-01-15 23:24:00 -08003925 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3926 return;
3927 }
3928
Alexandre Ramesd921d642015-04-16 15:07:16 +01003929 BlockPoolsScope block_pools(GetVIXLAssembler());
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003930 LocationSummary* locations = invoke->GetLocations();
3931 codegen_->GenerateStaticOrDirectCall(
3932 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003933 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01003934}
3935
3936void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08003937 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3938 return;
3939 }
3940
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003941 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01003942 DCHECK(!codegen_->IsLeafMethod());
3943 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3944}
3945
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003946HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
3947 HLoadClass::LoadKind desired_class_load_kind) {
3948 if (kEmitCompilerReadBarrier) {
3949 switch (desired_class_load_kind) {
3950 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3951 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3952 case HLoadClass::LoadKind::kBootImageAddress:
3953 // TODO: Implement for read barrier.
3954 return HLoadClass::LoadKind::kDexCacheViaMethod;
3955 default:
3956 break;
3957 }
3958 }
3959 switch (desired_class_load_kind) {
3960 case HLoadClass::LoadKind::kReferrersClass:
3961 break;
3962 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
3963 DCHECK(!GetCompilerOptions().GetCompilePic());
3964 break;
3965 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
3966 DCHECK(GetCompilerOptions().GetCompilePic());
3967 break;
3968 case HLoadClass::LoadKind::kBootImageAddress:
3969 break;
3970 case HLoadClass::LoadKind::kDexCacheAddress:
3971 DCHECK(Runtime::Current()->UseJitCompilation());
3972 break;
3973 case HLoadClass::LoadKind::kDexCachePcRelative:
3974 DCHECK(!Runtime::Current()->UseJitCompilation());
3975 break;
3976 case HLoadClass::LoadKind::kDexCacheViaMethod:
3977 break;
3978 }
3979 return desired_class_load_kind;
3980}
3981
Alexandre Rames67555f72014-11-18 10:55:16 +00003982void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003983 if (cls->NeedsAccessCheck()) {
3984 InvokeRuntimeCallingConvention calling_convention;
3985 CodeGenerator::CreateLoadClassLocationSummary(
3986 cls,
3987 LocationFrom(calling_convention.GetRegisterAt(0)),
Scott Wakeling97c72b72016-06-24 16:19:36 +01003988 LocationFrom(vixl::aarch64::x0),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01003989 /* code_generator_supports_read_barrier */ true);
3990 return;
3991 }
3992
3993 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
3994 ? LocationSummary::kCallOnSlowPath
3995 : LocationSummary::kNoCall;
3996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3997 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
3998 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
3999 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4000 locations->SetInAt(0, Location::RequiresRegister());
4001 }
4002 locations->SetOut(Location::RequiresRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004003}
4004
4005void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01004006 if (cls->NeedsAccessCheck()) {
4007 codegen_->MoveConstant(cls->GetLocations()->GetTemp(0), cls->GetTypeIndex());
4008 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
4009 cls,
4010 cls->GetDexPc(),
4011 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004012 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01004013 return;
4014 }
4015
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004016 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004017 Register out = OutputRegister(cls);
Alexandre Rames67555f72014-11-18 10:55:16 +00004018
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004019 bool generate_null_check = false;
4020 switch (cls->GetLoadKind()) {
4021 case HLoadClass::LoadKind::kReferrersClass: {
4022 DCHECK(!cls->CanCallRuntime());
4023 DCHECK(!cls->MustGenerateClinitCheck());
4024 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4025 Register current_method = InputRegisterAt(cls, 0);
4026 GenerateGcRootFieldLoad(
4027 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4028 break;
4029 }
4030 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4031 DCHECK(!kEmitCompilerReadBarrier);
4032 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4033 cls->GetTypeIndex()));
4034 break;
4035 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
4036 DCHECK(!kEmitCompilerReadBarrier);
4037 // Add ADRP with its PC-relative type patch.
4038 const DexFile& dex_file = cls->GetDexFile();
4039 uint32_t type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004040 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004041 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004042 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004043 __ Bind(adrp_label);
4044 __ adrp(out.X(), /* offset placeholder */ 0);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004045 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004046 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004047 vixl::aarch64::Label* add_label =
4048 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004049 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004050 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004051 __ Bind(add_label);
4052 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00004053 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004054 break;
4055 }
4056 case HLoadClass::LoadKind::kBootImageAddress: {
4057 DCHECK(!kEmitCompilerReadBarrier);
4058 DCHECK(cls->GetAddress() != 0u && IsUint<32>(cls->GetAddress()));
4059 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(cls->GetAddress()));
4060 break;
4061 }
4062 case HLoadClass::LoadKind::kDexCacheAddress: {
4063 DCHECK_NE(cls->GetAddress(), 0u);
4064 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4065 // that gives a 16KiB range. To try and reduce the number of literals if we load
4066 // multiple types, simply split the dex cache address to a 16KiB aligned base
4067 // loaded from a literal and the remaining offset embedded in the load.
4068 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
4069 DCHECK_ALIGNED(cls->GetAddress(), 4u);
4070 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4071 uint64_t base_address = cls->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4072 uint32_t offset = cls->GetAddress() & MaxInt<uint64_t>(offset_bits);
4073 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
4074 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
4075 GenerateGcRootFieldLoad(cls, out_loc, out.X(), offset);
4076 generate_null_check = !cls->IsInDexCache();
4077 break;
4078 }
4079 case HLoadClass::LoadKind::kDexCachePcRelative: {
4080 // Add ADRP with its PC-relative DexCache access patch.
4081 const DexFile& dex_file = cls->GetDexFile();
4082 uint32_t element_offset = cls->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004083 vixl::aarch64::Label* adrp_label =
4084 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004085 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004086 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004087 __ Bind(adrp_label);
4088 __ adrp(out.X(), /* offset placeholder */ 0);
4089 }
4090 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004091 vixl::aarch64::Label* ldr_label =
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004092 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
4093 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4094 GenerateGcRootFieldLoad(cls, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4095 generate_null_check = !cls->IsInDexCache();
4096 break;
4097 }
4098 case HLoadClass::LoadKind::kDexCacheViaMethod: {
4099 MemberOffset resolved_types_offset =
4100 ArtMethod::DexCacheResolvedTypesOffset(kArm64PointerSize);
4101 // /* GcRoot<mirror::Class>[] */ out =
4102 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
4103 Register current_method = InputRegisterAt(cls, 0);
4104 __ Ldr(out.X(), MemOperand(current_method, resolved_types_offset.Int32Value()));
4105 // /* GcRoot<mirror::Class> */ out = out[type_index]
4106 GenerateGcRootFieldLoad(
4107 cls, out_loc, out.X(), CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
4108 generate_null_check = !cls->IsInDexCache();
4109 break;
4110 }
4111 }
4112
4113 if (generate_null_check || cls->MustGenerateClinitCheck()) {
4114 DCHECK(cls->CanCallRuntime());
4115 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
4116 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4117 codegen_->AddSlowPath(slow_path);
4118 if (generate_null_check) {
4119 __ Cbz(out, slow_path->GetEntryLabel());
4120 }
4121 if (cls->MustGenerateClinitCheck()) {
4122 GenerateClassInitializationCheck(slow_path, out);
4123 } else {
4124 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004125 }
4126 }
4127}
4128
David Brazdilcb1c0552015-08-04 16:22:25 +01004129static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004130 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004131}
4132
Alexandre Rames67555f72014-11-18 10:55:16 +00004133void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4134 LocationSummary* locations =
4135 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4136 locations->SetOut(Location::RequiresRegister());
4137}
4138
4139void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01004140 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
4141}
4142
4143void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
4144 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
4145}
4146
4147void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
4148 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00004149}
4150
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004151HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
4152 HLoadString::LoadKind desired_string_load_kind) {
4153 if (kEmitCompilerReadBarrier) {
4154 switch (desired_string_load_kind) {
4155 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4156 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4157 case HLoadString::LoadKind::kBootImageAddress:
4158 // TODO: Implement for read barrier.
4159 return HLoadString::LoadKind::kDexCacheViaMethod;
4160 default:
4161 break;
4162 }
4163 }
4164 switch (desired_string_load_kind) {
4165 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4166 DCHECK(!GetCompilerOptions().GetCompilePic());
4167 break;
4168 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
4169 DCHECK(GetCompilerOptions().GetCompilePic());
4170 break;
4171 case HLoadString::LoadKind::kBootImageAddress:
4172 break;
4173 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01004174 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004175 break;
4176 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01004177 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004178 break;
4179 case HLoadString::LoadKind::kDexCacheViaMethod:
4180 break;
4181 }
4182 return desired_string_load_kind;
4183}
4184
Alexandre Rames67555f72014-11-18 10:55:16 +00004185void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004186 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004187 ? LocationSummary::kCallOnSlowPath
4188 : LocationSummary::kNoCall;
4189 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004190 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
4191 locations->SetInAt(0, Location::RequiresRegister());
4192 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004193 locations->SetOut(Location::RequiresRegister());
4194}
4195
4196void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004197 Location out_loc = load->GetLocations()->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00004198 Register out = OutputRegister(load);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004199
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004200 switch (load->GetLoadKind()) {
4201 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
4202 DCHECK(!kEmitCompilerReadBarrier);
4203 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
4204 load->GetStringIndex()));
4205 return; // No dex cache slow path.
4206 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
4207 DCHECK(!kEmitCompilerReadBarrier);
4208 // Add ADRP with its PC-relative String patch.
4209 const DexFile& dex_file = load->GetDexFile();
4210 uint32_t string_index = load->GetStringIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004211 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004212 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004213 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004214 __ Bind(adrp_label);
4215 __ adrp(out.X(), /* offset placeholder */ 0);
4216 }
4217 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004218 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004219 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
4220 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004221 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004222 __ Bind(add_label);
4223 __ add(out.X(), out.X(), Operand(/* offset placeholder */ 0));
4224 }
4225 return; // No dex cache slow path.
4226 }
4227 case HLoadString::LoadKind::kBootImageAddress: {
4228 DCHECK(!kEmitCompilerReadBarrier);
4229 DCHECK(load->GetAddress() != 0u && IsUint<32>(load->GetAddress()));
4230 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(load->GetAddress()));
4231 return; // No dex cache slow path.
4232 }
4233 case HLoadString::LoadKind::kDexCacheAddress: {
4234 DCHECK_NE(load->GetAddress(), 0u);
4235 // LDR immediate has a 12-bit offset multiplied by the size and for 32-bit loads
4236 // that gives a 16KiB range. To try and reduce the number of literals if we load
4237 // multiple strings, simply split the dex cache address to a 16KiB aligned base
4238 // loaded from a literal and the remaining offset embedded in the load.
4239 static_assert(sizeof(GcRoot<mirror::String>) == 4u, "Expected GC root to be 4 bytes.");
4240 DCHECK_ALIGNED(load->GetAddress(), 4u);
4241 constexpr size_t offset_bits = /* encoded bits */ 12 + /* scale */ 2;
4242 uint64_t base_address = load->GetAddress() & ~MaxInt<uint64_t>(offset_bits);
4243 uint32_t offset = load->GetAddress() & MaxInt<uint64_t>(offset_bits);
4244 __ Ldr(out.X(), codegen_->DeduplicateDexCacheAddressLiteral(base_address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004245 // /* GcRoot<mirror::String> */ out = *(base_address + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004246 GenerateGcRootFieldLoad(load, out_loc, out.X(), offset);
4247 break;
4248 }
4249 case HLoadString::LoadKind::kDexCachePcRelative: {
4250 // Add ADRP with its PC-relative DexCache access patch.
4251 const DexFile& dex_file = load->GetDexFile();
4252 uint32_t element_offset = load->GetDexCacheElementOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004253 vixl::aarch64::Label* adrp_label =
4254 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004255 {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004256 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004257 __ Bind(adrp_label);
4258 __ adrp(out.X(), /* offset placeholder */ 0);
4259 }
4260 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004261 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004262 codegen_->NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004263 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004264 GenerateGcRootFieldLoad(load, out_loc, out.X(), /* offset placeholder */ 0, ldr_label);
4265 break;
4266 }
4267 case HLoadString::LoadKind::kDexCacheViaMethod: {
4268 Register current_method = InputRegisterAt(load, 0);
4269 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4270 GenerateGcRootFieldLoad(
4271 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
4272 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
4273 __ Ldr(out.X(), HeapOperand(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
4274 // /* GcRoot<mirror::String> */ out = out[string_index]
4275 GenerateGcRootFieldLoad(
4276 load, out_loc, out.X(), CodeGenerator::GetCacheOffset(load->GetStringIndex()));
4277 break;
4278 }
4279 default:
4280 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
4281 UNREACHABLE();
4282 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004283
Nicolas Geoffray917d0162015-11-24 18:25:35 +00004284 if (!load->IsInDexCache()) {
4285 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load);
4286 codegen_->AddSlowPath(slow_path);
4287 __ Cbz(out, slow_path->GetEntryLabel());
4288 __ Bind(slow_path->GetExitLabel());
4289 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004290}
4291
Alexandre Rames5319def2014-10-23 10:03:10 +01004292void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
4293 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4294 locations->SetOut(Location::ConstantLocation(constant));
4295}
4296
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004297void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004298 // Will be generated at use site.
4299}
4300
Alexandre Rames67555f72014-11-18 10:55:16 +00004301void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4302 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004304 InvokeRuntimeCallingConvention calling_convention;
4305 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4306}
4307
4308void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
4309 codegen_->InvokeRuntime(instruction->IsEnter()
4310 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject),
4311 instruction,
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004312 instruction->GetDexPc(),
4313 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004314 if (instruction->IsEnter()) {
4315 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
4316 } else {
4317 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
4318 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004319}
4320
Alexandre Rames42d641b2014-10-27 14:00:51 +00004321void LocationsBuilderARM64::VisitMul(HMul* mul) {
4322 LocationSummary* locations =
4323 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4324 switch (mul->GetResultType()) {
4325 case Primitive::kPrimInt:
4326 case Primitive::kPrimLong:
4327 locations->SetInAt(0, Location::RequiresRegister());
4328 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004330 break;
4331
4332 case Primitive::kPrimFloat:
4333 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004334 locations->SetInAt(0, Location::RequiresFpuRegister());
4335 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00004336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00004337 break;
4338
4339 default:
4340 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4341 }
4342}
4343
4344void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
4345 switch (mul->GetResultType()) {
4346 case Primitive::kPrimInt:
4347 case Primitive::kPrimLong:
4348 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
4349 break;
4350
4351 case Primitive::kPrimFloat:
4352 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004353 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00004354 break;
4355
4356 default:
4357 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
4358 }
4359}
4360
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004361void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
4362 LocationSummary* locations =
4363 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
4364 switch (neg->GetResultType()) {
4365 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00004366 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00004367 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00004368 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004369 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004370
4371 case Primitive::kPrimFloat:
4372 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004373 locations->SetInAt(0, Location::RequiresFpuRegister());
4374 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004375 break;
4376
4377 default:
4378 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4379 }
4380}
4381
4382void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
4383 switch (neg->GetResultType()) {
4384 case Primitive::kPrimInt:
4385 case Primitive::kPrimLong:
4386 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
4387 break;
4388
4389 case Primitive::kPrimFloat:
4390 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00004391 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004392 break;
4393
4394 default:
4395 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
4396 }
4397}
4398
4399void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
4400 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004402 InvokeRuntimeCallingConvention calling_convention;
4403 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004404 locations->SetOut(LocationFrom(x0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004405 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004406 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(2)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004407}
4408
4409void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
4410 LocationSummary* locations = instruction->GetLocations();
4411 InvokeRuntimeCallingConvention calling_convention;
4412 Register type_index = RegisterFrom(locations->GetTemp(0), Primitive::kPrimInt);
4413 DCHECK(type_index.Is(w0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004414 __ Mov(type_index, instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01004415 // Note: if heap poisoning is enabled, the entry point takes cares
4416 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004417 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4418 instruction,
4419 instruction->GetDexPc(),
4420 nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004421 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00004422}
4423
Alexandre Rames5319def2014-10-23 10:03:10 +01004424void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
4425 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004426 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01004427 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00004428 if (instruction->IsStringAlloc()) {
4429 locations->AddTemp(LocationFrom(kArtMethodRegister));
4430 } else {
4431 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4432 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
4433 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004434 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
4435}
4436
4437void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004438 // Note: if heap poisoning is enabled, the entry point takes cares
4439 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004440 if (instruction->IsStringAlloc()) {
4441 // String is allocated through StringFactory. Call NewEmptyString entry point.
4442 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07004443 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004444 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
4445 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
4446 __ Blr(lr);
4447 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4448 } else {
4449 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4450 instruction,
4451 instruction->GetDexPc(),
4452 nullptr);
4453 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4454 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004455}
4456
4457void LocationsBuilderARM64::VisitNot(HNot* instruction) {
4458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00004459 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00004460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01004461}
4462
4463void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004464 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004465 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01004466 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01004467 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004468 break;
4469
4470 default:
4471 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
4472 }
4473}
4474
David Brazdil66d126e2015-04-03 16:02:44 +01004475void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
4476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4477 locations->SetInAt(0, Location::RequiresRegister());
4478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4479}
4480
4481void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004482 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01004483}
4484
Alexandre Rames5319def2014-10-23 10:03:10 +01004485void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004486 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4487 ? LocationSummary::kCallOnSlowPath
4488 : LocationSummary::kNoCall;
4489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexandre Rames5319def2014-10-23 10:03:10 +01004490 locations->SetInAt(0, Location::RequiresRegister());
4491 if (instruction->HasUses()) {
4492 locations->SetOut(Location::SameAsFirstInput());
4493 }
4494}
4495
Calin Juravle2ae48182016-03-16 14:05:09 +00004496void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
4497 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004498 return;
4499 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004500
Alexandre Ramesd921d642015-04-16 15:07:16 +01004501 BlockPoolsScope block_pools(GetVIXLAssembler());
4502 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004503 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
Calin Juravle2ae48182016-03-16 14:05:09 +00004504 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004505}
4506
Calin Juravle2ae48182016-03-16 14:05:09 +00004507void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004508 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004509 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01004510
4511 LocationSummary* locations = instruction->GetLocations();
4512 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004513
4514 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01004515}
4516
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004517void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004518 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004519}
4520
Alexandre Rames67555f72014-11-18 10:55:16 +00004521void LocationsBuilderARM64::VisitOr(HOr* instruction) {
4522 HandleBinaryOp(instruction);
4523}
4524
4525void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
4526 HandleBinaryOp(instruction);
4527}
4528
Alexandre Rames3e69f162014-12-10 10:36:50 +00004529void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
4530 LOG(FATAL) << "Unreachable";
4531}
4532
4533void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
4534 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4535}
4536
Alexandre Rames5319def2014-10-23 10:03:10 +01004537void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
4538 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4539 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4540 if (location.IsStackSlot()) {
4541 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4542 } else if (location.IsDoubleStackSlot()) {
4543 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4544 }
4545 locations->SetOut(location);
4546}
4547
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004548void InstructionCodeGeneratorARM64::VisitParameterValue(
4549 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004550 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004551}
4552
4553void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
4554 LocationSummary* locations =
4555 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004556 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004557}
4558
4559void InstructionCodeGeneratorARM64::VisitCurrentMethod(
4560 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4561 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01004562}
4563
4564void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
4565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004566 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004567 locations->SetInAt(i, Location::Any());
4568 }
4569 locations->SetOut(Location::Any());
4570}
4571
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004572void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004573 LOG(FATAL) << "Unreachable";
4574}
4575
Serban Constantinescu02164b32014-11-13 14:05:07 +00004576void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004577 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00004578 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004579 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
4580 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4582
4583 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004584 case Primitive::kPrimInt:
4585 case Primitive::kPrimLong:
4586 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08004587 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00004588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4589 break;
4590
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004591 case Primitive::kPrimFloat:
4592 case Primitive::kPrimDouble: {
4593 InvokeRuntimeCallingConvention calling_convention;
4594 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
4595 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
4596 locations->SetOut(calling_convention.GetReturnLocation(type));
4597
4598 break;
4599 }
4600
Serban Constantinescu02164b32014-11-13 14:05:07 +00004601 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004602 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00004603 }
4604}
4605
4606void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
4607 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004608
Serban Constantinescu02164b32014-11-13 14:05:07 +00004609 switch (type) {
4610 case Primitive::kPrimInt:
4611 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08004612 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004613 break;
4614 }
4615
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004616 case Primitive::kPrimFloat:
4617 case Primitive::kPrimDouble: {
4618 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
4619 : QUICK_ENTRY_POINT(pFmod);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004620 codegen_->InvokeRuntime(entry_offset, rem, rem->GetDexPc(), nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004621 if (type == Primitive::kPrimFloat) {
4622 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
4623 } else {
4624 CheckEntrypointTypes<kQuickFmod, double, double, double>();
4625 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00004626 break;
4627 }
4628
Serban Constantinescu02164b32014-11-13 14:05:07 +00004629 default:
4630 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004631 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00004632 }
4633}
4634
Calin Juravle27df7582015-04-17 19:12:31 +01004635void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
4636 memory_barrier->SetLocations(nullptr);
4637}
4638
4639void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00004640 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01004641}
4642
Alexandre Rames5319def2014-10-23 10:03:10 +01004643void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
4644 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
4645 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00004646 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01004647}
4648
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004649void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004650 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004651}
4652
4653void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
4654 instruction->SetLocations(nullptr);
4655}
4656
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004657void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004658 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01004659}
4660
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004661void LocationsBuilderARM64::VisitRor(HRor* ror) {
4662 HandleBinaryOp(ror);
4663}
4664
4665void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
4666 HandleBinaryOp(ror);
4667}
4668
Serban Constantinescu02164b32014-11-13 14:05:07 +00004669void LocationsBuilderARM64::VisitShl(HShl* shl) {
4670 HandleShift(shl);
4671}
4672
4673void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
4674 HandleShift(shl);
4675}
4676
4677void LocationsBuilderARM64::VisitShr(HShr* shr) {
4678 HandleShift(shr);
4679}
4680
4681void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
4682 HandleShift(shr);
4683}
4684
Alexandre Rames5319def2014-10-23 10:03:10 +01004685void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004686 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004687}
4688
4689void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004690 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004691}
4692
Alexandre Rames67555f72014-11-18 10:55:16 +00004693void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004694 HandleFieldGet(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00004695}
4696
4697void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004698 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00004699}
4700
4701void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01004702 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01004703}
4704
Alexandre Rames67555f72014-11-18 10:55:16 +00004705void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004706 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01004707}
4708
Calin Juravlee460d1d2015-09-29 04:52:17 +01004709void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
4710 HUnresolvedInstanceFieldGet* instruction) {
4711 FieldAccessCallingConventionARM64 calling_convention;
4712 codegen_->CreateUnresolvedFieldLocationSummary(
4713 instruction, instruction->GetFieldType(), calling_convention);
4714}
4715
4716void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
4717 HUnresolvedInstanceFieldGet* instruction) {
4718 FieldAccessCallingConventionARM64 calling_convention;
4719 codegen_->GenerateUnresolvedFieldAccess(instruction,
4720 instruction->GetFieldType(),
4721 instruction->GetFieldIndex(),
4722 instruction->GetDexPc(),
4723 calling_convention);
4724}
4725
4726void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
4727 HUnresolvedInstanceFieldSet* instruction) {
4728 FieldAccessCallingConventionARM64 calling_convention;
4729 codegen_->CreateUnresolvedFieldLocationSummary(
4730 instruction, instruction->GetFieldType(), calling_convention);
4731}
4732
4733void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
4734 HUnresolvedInstanceFieldSet* instruction) {
4735 FieldAccessCallingConventionARM64 calling_convention;
4736 codegen_->GenerateUnresolvedFieldAccess(instruction,
4737 instruction->GetFieldType(),
4738 instruction->GetFieldIndex(),
4739 instruction->GetDexPc(),
4740 calling_convention);
4741}
4742
4743void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
4744 HUnresolvedStaticFieldGet* instruction) {
4745 FieldAccessCallingConventionARM64 calling_convention;
4746 codegen_->CreateUnresolvedFieldLocationSummary(
4747 instruction, instruction->GetFieldType(), calling_convention);
4748}
4749
4750void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
4751 HUnresolvedStaticFieldGet* instruction) {
4752 FieldAccessCallingConventionARM64 calling_convention;
4753 codegen_->GenerateUnresolvedFieldAccess(instruction,
4754 instruction->GetFieldType(),
4755 instruction->GetFieldIndex(),
4756 instruction->GetDexPc(),
4757 calling_convention);
4758}
4759
4760void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
4761 HUnresolvedStaticFieldSet* instruction) {
4762 FieldAccessCallingConventionARM64 calling_convention;
4763 codegen_->CreateUnresolvedFieldLocationSummary(
4764 instruction, instruction->GetFieldType(), calling_convention);
4765}
4766
4767void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
4768 HUnresolvedStaticFieldSet* instruction) {
4769 FieldAccessCallingConventionARM64 calling_convention;
4770 codegen_->GenerateUnresolvedFieldAccess(instruction,
4771 instruction->GetFieldType(),
4772 instruction->GetFieldIndex(),
4773 instruction->GetDexPc(),
4774 calling_convention);
4775}
4776
Alexandre Rames5319def2014-10-23 10:03:10 +01004777void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
4778 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4779}
4780
4781void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004782 HBasicBlock* block = instruction->GetBlock();
4783 if (block->GetLoopInformation() != nullptr) {
4784 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4785 // The back edge will generate the suspend check.
4786 return;
4787 }
4788 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4789 // The goto will generate the suspend check.
4790 return;
4791 }
4792 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01004793}
4794
Alexandre Rames67555f72014-11-18 10:55:16 +00004795void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
4796 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004797 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00004798 InvokeRuntimeCallingConvention calling_convention;
4799 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
4800}
4801
4802void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
4803 codegen_->InvokeRuntime(
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00004804 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004805 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00004806}
4807
4808void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
4809 LocationSummary* locations =
4810 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
4811 Primitive::Type input_type = conversion->GetInputType();
4812 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00004813 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00004814 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
4815 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
4816 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
4817 }
4818
Alexandre Rames542361f2015-01-29 16:57:31 +00004819 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004820 locations->SetInAt(0, Location::RequiresFpuRegister());
4821 } else {
4822 locations->SetInAt(0, Location::RequiresRegister());
4823 }
4824
Alexandre Rames542361f2015-01-29 16:57:31 +00004825 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004826 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4827 } else {
4828 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4829 }
4830}
4831
4832void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
4833 Primitive::Type result_type = conversion->GetResultType();
4834 Primitive::Type input_type = conversion->GetInputType();
4835
4836 DCHECK_NE(input_type, result_type);
4837
Alexandre Rames542361f2015-01-29 16:57:31 +00004838 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00004839 int result_size = Primitive::ComponentSize(result_type);
4840 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00004841 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00004842 Register output = OutputRegister(conversion);
4843 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00004844 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01004845 // 'int' values are used directly as W registers, discarding the top
4846 // bits, so we don't need to sign-extend and can just perform a move.
4847 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
4848 // top 32 bits of the target register. We theoretically could leave those
4849 // bits unchanged, but we would have to make sure that no code uses a
4850 // 32bit input value as a 64bit value assuming that the top 32 bits are
4851 // zero.
4852 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00004853 } else if (result_type == Primitive::kPrimChar ||
4854 (input_type == Primitive::kPrimChar && input_size < result_size)) {
4855 __ Ubfx(output,
4856 output.IsX() ? source.X() : source.W(),
4857 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004858 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00004859 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00004860 }
Alexandre Rames542361f2015-01-29 16:57:31 +00004861 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004862 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004863 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004864 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
4865 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00004866 } else if (Primitive::IsFloatingPointType(result_type) &&
4867 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00004868 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
4869 } else {
4870 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4871 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00004872 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00004873}
Alexandre Rames67555f72014-11-18 10:55:16 +00004874
Serban Constantinescu02164b32014-11-13 14:05:07 +00004875void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
4876 HandleShift(ushr);
4877}
4878
4879void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
4880 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00004881}
4882
4883void LocationsBuilderARM64::VisitXor(HXor* instruction) {
4884 HandleBinaryOp(instruction);
4885}
4886
4887void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
4888 HandleBinaryOp(instruction);
4889}
4890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004891void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00004892 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00004893 LOG(FATAL) << "Unreachable";
4894}
4895
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004896void InstructionCodeGeneratorARM64::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
Mark Mendellfe57faa2015-09-18 09:26:15 -04004901// Simple implementation of packed switch - generate cascaded compare/jumps.
4902void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4903 LocationSummary* locations =
4904 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4905 locations->SetInAt(0, Location::RequiresRegister());
4906}
4907
4908void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4909 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08004910 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04004911 Register value_reg = InputRegisterAt(switch_instr, 0);
4912 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4913
Zheng Xu3927c8b2015-11-18 17:46:25 +08004914 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004915 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08004916 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
4917 // make sure we don't emit it if the target may run out of range.
4918 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
4919 // ranges and emit the tables only as required.
4920 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04004921
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004922 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08004923 // Current instruction id is an upper bound of the number of HIRs in the graph.
4924 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
4925 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004926 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4927 Register temp = temps.AcquireW();
4928 __ Subs(temp, value_reg, Operand(lower_bound));
4929
Zheng Xu3927c8b2015-11-18 17:46:25 +08004930 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00004931 // Jump to successors[0] if value == lower_bound.
4932 __ B(eq, codegen_->GetLabelOf(successors[0]));
4933 int32_t last_index = 0;
4934 for (; num_entries - last_index > 2; last_index += 2) {
4935 __ Subs(temp, temp, Operand(2));
4936 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
4937 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
4938 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
4939 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
4940 }
4941 if (num_entries - last_index == 2) {
4942 // The last missing case_value.
4943 __ Cmp(temp, Operand(1));
4944 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08004945 }
4946
4947 // And the default for any other value.
4948 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4949 __ B(codegen_->GetLabelOf(default_block));
4950 }
4951 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01004952 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08004953
4954 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
4955
4956 // Below instructions should use at most one blocked register. Since there are two blocked
4957 // registers, we are free to block one.
4958 Register temp_w = temps.AcquireW();
4959 Register index;
4960 // Remove the bias.
4961 if (lower_bound != 0) {
4962 index = temp_w;
4963 __ Sub(index, value_reg, Operand(lower_bound));
4964 } else {
4965 index = value_reg;
4966 }
4967
4968 // Jump to default block if index is out of the range.
4969 __ Cmp(index, Operand(num_entries));
4970 __ B(hs, codegen_->GetLabelOf(default_block));
4971
4972 // In current VIXL implementation, it won't require any blocked registers to encode the
4973 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
4974 // register pressure.
4975 Register table_base = temps.AcquireX();
4976 // Load jump offset from the table.
4977 __ Adr(table_base, jump_table->GetTableStartLabel());
4978 Register jump_offset = temp_w;
4979 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
4980
4981 // Jump to target block by branching to table_base(pc related) + offset.
4982 Register target_address = table_base;
4983 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
4984 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04004985 }
4986}
4987
Roland Levillain44015862016-01-22 11:47:17 +00004988void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
4989 Location out,
4990 uint32_t offset,
4991 Location maybe_temp) {
4992 Primitive::Type type = Primitive::kPrimNot;
4993 Register out_reg = RegisterFrom(out, type);
4994 if (kEmitCompilerReadBarrier) {
4995 Register temp_reg = RegisterFrom(maybe_temp, type);
4996 if (kUseBakerReadBarrier) {
4997 // Load with fast path based Baker's read barrier.
4998 // /* HeapReference<Object> */ out = *(out + offset)
4999 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5000 out,
5001 out_reg,
5002 offset,
5003 temp_reg,
5004 /* needs_null_check */ false,
5005 /* use_load_acquire */ false);
5006 } else {
5007 // Load with slow path based read barrier.
5008 // Save the value of `out` into `maybe_temp` before overwriting it
5009 // in the following move operation, as we will need it for the
5010 // read barrier below.
5011 __ Mov(temp_reg, out_reg);
5012 // /* HeapReference<Object> */ out = *(out + offset)
5013 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5014 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5015 }
5016 } else {
5017 // Plain load with no read barrier.
5018 // /* HeapReference<Object> */ out = *(out + offset)
5019 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5020 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5021 }
5022}
5023
5024void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
5025 Location out,
5026 Location obj,
5027 uint32_t offset,
5028 Location maybe_temp) {
5029 Primitive::Type type = Primitive::kPrimNot;
5030 Register out_reg = RegisterFrom(out, type);
5031 Register obj_reg = RegisterFrom(obj, type);
5032 if (kEmitCompilerReadBarrier) {
5033 if (kUseBakerReadBarrier) {
5034 // Load with fast path based Baker's read barrier.
5035 Register temp_reg = RegisterFrom(maybe_temp, type);
5036 // /* HeapReference<Object> */ out = *(obj + offset)
5037 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5038 out,
5039 obj_reg,
5040 offset,
5041 temp_reg,
5042 /* needs_null_check */ false,
5043 /* use_load_acquire */ false);
5044 } else {
5045 // Load with slow path based read barrier.
5046 // /* HeapReference<Object> */ out = *(obj + offset)
5047 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5048 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5049 }
5050 } else {
5051 // Plain load with no read barrier.
5052 // /* HeapReference<Object> */ out = *(obj + offset)
5053 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5054 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5055 }
5056}
5057
5058void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(HInstruction* instruction,
5059 Location root,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005060 Register obj,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005061 uint32_t offset,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005062 vixl::aarch64::Label* fixup_label) {
Roland Levillain44015862016-01-22 11:47:17 +00005063 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
5064 if (kEmitCompilerReadBarrier) {
5065 if (kUseBakerReadBarrier) {
5066 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
5067 // Baker's read barrier are used:
5068 //
5069 // root = obj.field;
5070 // if (Thread::Current()->GetIsGcMarking()) {
5071 // root = ReadBarrier::Mark(root)
5072 // }
5073
5074 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075 if (fixup_label == nullptr) {
5076 __ Ldr(root_reg, MemOperand(obj, offset));
5077 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005078 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 __ Bind(fixup_label);
5080 __ ldr(root_reg, MemOperand(obj, offset));
5081 }
Roland Levillain44015862016-01-22 11:47:17 +00005082 static_assert(
5083 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
5084 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
5085 "have different sizes.");
5086 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
5087 "art::mirror::CompressedReference<mirror::Object> and int32_t "
5088 "have different sizes.");
5089
5090 // Slow path used to mark the GC root `root`.
5091 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005092 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, root);
Roland Levillain44015862016-01-22 11:47:17 +00005093 codegen_->AddSlowPath(slow_path);
5094
5095 MacroAssembler* masm = GetVIXLAssembler();
5096 UseScratchRegisterScope temps(masm);
5097 Register temp = temps.AcquireW();
5098 // temp = Thread::Current()->GetIsGcMarking()
Andreas Gampe542451c2016-07-26 09:02:02 -07005099 __ Ldr(temp, MemOperand(tr, Thread::IsGcMarkingOffset<kArm64PointerSize>().Int32Value()));
Roland Levillain44015862016-01-22 11:47:17 +00005100 __ Cbnz(temp, slow_path->GetEntryLabel());
5101 __ Bind(slow_path->GetExitLabel());
5102 } else {
5103 // GC root loaded through a slow path for read barriers other
5104 // than Baker's.
5105 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005106 if (fixup_label == nullptr) {
5107 __ Add(root_reg.X(), obj.X(), offset);
5108 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005109 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 __ Bind(fixup_label);
5111 __ add(root_reg.X(), obj.X(), offset);
5112 }
Roland Levillain44015862016-01-22 11:47:17 +00005113 // /* mirror::Object* */ root = root->Read()
5114 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
5115 }
5116 } else {
5117 // Plain GC root load with no read barrier.
5118 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005119 if (fixup_label == nullptr) {
5120 __ Ldr(root_reg, MemOperand(obj, offset));
5121 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005122 SingleEmissionCheckScope guard(GetVIXLAssembler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005123 __ Bind(fixup_label);
5124 __ ldr(root_reg, MemOperand(obj, offset));
5125 }
Roland Levillain44015862016-01-22 11:47:17 +00005126 // Note that GC roots are not affected by heap poisoning, thus we
5127 // do not have to unpoison `root_reg` here.
5128 }
5129}
5130
5131void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
5132 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005133 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005134 uint32_t offset,
5135 Register temp,
5136 bool needs_null_check,
5137 bool use_load_acquire) {
5138 DCHECK(kEmitCompilerReadBarrier);
5139 DCHECK(kUseBakerReadBarrier);
5140
5141 // /* HeapReference<Object> */ ref = *(obj + offset)
5142 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01005143 size_t no_scale_factor = 0U;
5144 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5145 ref,
5146 obj,
5147 offset,
5148 no_index,
5149 no_scale_factor,
5150 temp,
5151 needs_null_check,
5152 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005153}
5154
5155void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
5156 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005157 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005158 uint32_t data_offset,
5159 Location index,
5160 Register temp,
5161 bool needs_null_check) {
5162 DCHECK(kEmitCompilerReadBarrier);
5163 DCHECK(kUseBakerReadBarrier);
5164
5165 // Array cells are never volatile variables, therefore array loads
5166 // never use Load-Acquire instructions on ARM64.
5167 const bool use_load_acquire = false;
5168
Roland Levillainbfea3352016-06-23 13:48:47 +01005169 static_assert(
5170 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5171 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005172 // /* HeapReference<Object> */ ref =
5173 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01005174 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
5175 GenerateReferenceLoadWithBakerReadBarrier(instruction,
5176 ref,
5177 obj,
5178 data_offset,
5179 index,
5180 scale_factor,
5181 temp,
5182 needs_null_check,
5183 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005184}
5185
5186void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
5187 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01005188 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00005189 uint32_t offset,
5190 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01005191 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00005192 Register temp,
5193 bool needs_null_check,
5194 bool use_load_acquire) {
5195 DCHECK(kEmitCompilerReadBarrier);
5196 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01005197 // If we are emitting an array load, we should not be using a
5198 // Load Acquire instruction. In other words:
5199 // `instruction->IsArrayGet()` => `!use_load_acquire`.
5200 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00005201
5202 MacroAssembler* masm = GetVIXLAssembler();
5203 UseScratchRegisterScope temps(masm);
5204
5205 // In slow path based read barriers, the read barrier call is
5206 // inserted after the original load. However, in fast path based
5207 // Baker's read barriers, we need to perform the load of
5208 // mirror::Object::monitor_ *before* the original reference load.
5209 // This load-load ordering is required by the read barrier.
5210 // The fast path/slow path (for Baker's algorithm) should look like:
5211 //
5212 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
5213 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
5214 // HeapReference<Object> ref = *src; // Original reference load.
5215 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
5216 // if (is_gray) {
5217 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
5218 // }
5219 //
5220 // Note: the original implementation in ReadBarrier::Barrier is
5221 // slightly more complex as it performs additional checks that we do
5222 // not do here for performance reasons.
5223
5224 Primitive::Type type = Primitive::kPrimNot;
5225 Register ref_reg = RegisterFrom(ref, type);
5226 DCHECK(obj.IsW());
5227 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
5228
5229 // /* int32_t */ monitor = obj->monitor_
5230 __ Ldr(temp, HeapOperand(obj, monitor_offset));
5231 if (needs_null_check) {
5232 MaybeRecordImplicitNullCheck(instruction);
5233 }
5234 // /* LockWord */ lock_word = LockWord(monitor)
5235 static_assert(sizeof(LockWord) == sizeof(int32_t),
5236 "art::LockWord and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00005237
Vladimir Marko877a0332016-07-11 19:30:56 +01005238 // Introduce a dependency on the lock_word including rb_state,
5239 // to prevent load-load reordering, and without using
Roland Levillain44015862016-01-22 11:47:17 +00005240 // a memory barrier (which would be more expensive).
Vladimir Marko877a0332016-07-11 19:30:56 +01005241 // obj is unchanged by this operation, but its value now depends on temp.
5242 __ Add(obj.X(), obj.X(), Operand(temp.X(), LSR, 32));
Roland Levillain44015862016-01-22 11:47:17 +00005243
5244 // The actual reference load.
5245 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01005246 // Load types involving an "index".
5247 if (use_load_acquire) {
5248 // UnsafeGetObjectVolatile intrinsic case.
5249 // Register `index` is not an index in an object array, but an
5250 // offset to an object reference field within object `obj`.
5251 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
5252 DCHECK(instruction->GetLocations()->Intrinsified());
5253 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
5254 << instruction->AsInvoke()->GetIntrinsic();
5255 DCHECK_EQ(offset, 0U);
5256 DCHECK_EQ(scale_factor, 0U);
5257 DCHECK_EQ(needs_null_check, 0U);
5258 // /* HeapReference<Object> */ ref = *(obj + index)
5259 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
5260 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00005261 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01005262 // ArrayGet and UnsafeGetObject intrinsics cases.
5263 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
5264 if (index.IsConstant()) {
5265 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
5266 Load(type, ref_reg, HeapOperand(obj, computed_offset));
5267 } else {
Vladimir Marko877a0332016-07-11 19:30:56 +01005268 Register temp2 = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +01005269 __ Add(temp2, obj, offset);
5270 Load(type, ref_reg, HeapOperand(temp2, XRegisterFrom(index), LSL, scale_factor));
5271 temps.Release(temp2);
5272 }
Roland Levillain44015862016-01-22 11:47:17 +00005273 }
Roland Levillain44015862016-01-22 11:47:17 +00005274 } else {
5275 // /* HeapReference<Object> */ ref = *(obj + offset)
5276 MemOperand field = HeapOperand(obj, offset);
5277 if (use_load_acquire) {
5278 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
5279 } else {
5280 Load(type, ref_reg, field);
5281 }
5282 }
5283
5284 // Object* ref = ref_addr->AsMirrorPtr()
5285 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
5286
5287 // Slow path used to mark the object `ref` when it is gray.
5288 SlowPathCodeARM64* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01005289 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(instruction, ref);
Roland Levillain44015862016-01-22 11:47:17 +00005290 AddSlowPath(slow_path);
5291
5292 // if (rb_state == ReadBarrier::gray_ptr_)
5293 // ref = ReadBarrier::Mark(ref);
Vladimir Marko877a0332016-07-11 19:30:56 +01005294 // Given the numeric representation, it's enough to check the low bit of the rb_state.
5295 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
5296 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
5297 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
5298 __ Tbnz(temp, LockWord::kReadBarrierStateShift, slow_path->GetEntryLabel());
Roland Levillain44015862016-01-22 11:47:17 +00005299 __ Bind(slow_path->GetExitLabel());
5300}
5301
5302void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
5303 Location out,
5304 Location ref,
5305 Location obj,
5306 uint32_t offset,
5307 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005308 DCHECK(kEmitCompilerReadBarrier);
5309
Roland Levillain44015862016-01-22 11:47:17 +00005310 // Insert a slow path based read barrier *after* the reference load.
5311 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005312 // If heap poisoning is enabled, the unpoisoning of the loaded
5313 // reference will be carried out by the runtime within the slow
5314 // path.
5315 //
5316 // Note that `ref` currently does not get unpoisoned (when heap
5317 // poisoning is enabled), which is alright as the `ref` argument is
5318 // not used by the artReadBarrierSlow entry point.
5319 //
5320 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5321 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
5322 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
5323 AddSlowPath(slow_path);
5324
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005325 __ B(slow_path->GetEntryLabel());
5326 __ Bind(slow_path->GetExitLabel());
5327}
5328
Roland Levillain44015862016-01-22 11:47:17 +00005329void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
5330 Location out,
5331 Location ref,
5332 Location obj,
5333 uint32_t offset,
5334 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005335 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005336 // Baker's read barriers shall be handled by the fast path
5337 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
5338 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005339 // If heap poisoning is enabled, unpoisoning will be taken care of
5340 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00005341 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005342 } else if (kPoisonHeapReferences) {
5343 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
5344 }
5345}
5346
Roland Levillain44015862016-01-22 11:47:17 +00005347void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
5348 Location out,
5349 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005350 DCHECK(kEmitCompilerReadBarrier);
5351
Roland Levillain44015862016-01-22 11:47:17 +00005352 // Insert a slow path based read barrier *after* the GC root load.
5353 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005354 // Note that GC roots are not affected by heap poisoning, so we do
5355 // not need to do anything special for this here.
5356 SlowPathCodeARM64* slow_path =
5357 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
5358 AddSlowPath(slow_path);
5359
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005360 __ B(slow_path->GetEntryLabel());
5361 __ Bind(slow_path->GetExitLabel());
5362}
5363
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005364void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
5365 LocationSummary* locations =
5366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5367 locations->SetInAt(0, Location::RequiresRegister());
5368 locations->SetOut(Location::RequiresRegister());
5369}
5370
5371void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
5372 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005373 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005374 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005375 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005376 __ Ldr(XRegisterFrom(locations->Out()),
5377 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005378 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005379 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005380 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00005381 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
5382 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005383 __ Ldr(XRegisterFrom(locations->Out()),
5384 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005385 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005386}
5387
5388
5389
Alexandre Rames67555f72014-11-18 10:55:16 +00005390#undef __
5391#undef QUICK_ENTRY_POINT
5392
Alexandre Rames5319def2014-10-23 10:03:10 +01005393} // namespace arm64
5394} // namespace art