blob: 78b627ad903184509021bf3c0c33993dbb2b3a09 [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
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010073using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::RegisterFrom;
75using helpers::StackOperandFrom;
76using helpers::VIXLRegCodeFromART;
77using helpers::WRegisterFrom;
78using helpers::XRegisterFrom;
79
Alexandre Rames5319def2014-10-23 10:03:10 +010080static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080082// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
83// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010085
Vladimir Markof4f2daa2017-03-20 18:26:59 +000086// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
87// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010093constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
Vladimir Marko66d691d2017-04-07 17:53:39 +010094constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
Vladimir Markod1ef8732017-04-18 13:55:13 +010095constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000096
97// Some instructions have special requirements for a temporary, for example
98// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
99// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
100// loads with large offsets need a fixed register to limit the number of link-time
101// thunks we generate. For these and similar cases, we want to reserve a specific
102// register that's neither callee-save nor an argument register. We choose x15.
103inline Location FixedTempLocation() {
104 return Location::RegisterLocation(x15.GetCode());
105}
106
Alexandre Rames5319def2014-10-23 10:03:10 +0100107inline Condition ARM64Condition(IfCondition cond) {
108 switch (cond) {
109 case kCondEQ: return eq;
110 case kCondNE: return ne;
111 case kCondLT: return lt;
112 case kCondLE: return le;
113 case kCondGT: return gt;
114 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700115 case kCondB: return lo;
116 case kCondBE: return ls;
117 case kCondA: return hi;
118 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100119 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000120 LOG(FATAL) << "Unreachable";
121 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100122}
123
Vladimir Markod6e069b2016-01-18 11:11:01 +0000124inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
125 // The ARM64 condition codes can express all the necessary branches, see the
126 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
127 // There is no dex instruction or HIR that would need the missing conditions
128 // "equal or unordered" or "not equal".
129 switch (cond) {
130 case kCondEQ: return eq;
131 case kCondNE: return ne /* unordered */;
132 case kCondLT: return gt_bias ? cc : lt /* unordered */;
133 case kCondLE: return gt_bias ? ls : le /* unordered */;
134 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
135 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
136 default:
137 LOG(FATAL) << "UNREACHABLE";
138 UNREACHABLE();
139 }
140}
141
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000143 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
144 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
145 // but we use the exact registers for clarity.
146 if (return_type == Primitive::kPrimFloat) {
147 return LocationFrom(s0);
148 } else if (return_type == Primitive::kPrimDouble) {
149 return LocationFrom(d0);
150 } else if (return_type == Primitive::kPrimLong) {
151 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100152 } else if (return_type == Primitive::kPrimVoid) {
153 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000154 } else {
155 return LocationFrom(w0);
156 }
157}
158
Alexandre Rames5319def2014-10-23 10:03:10 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000160 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100166
Zheng Xuda403092015-04-24 17:35:39 +0800167// Calculate memory accessing operand for save/restore live registers.
168static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100169 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800170 int64_t spill_offset,
171 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100172 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
173 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
174 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800175 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100176 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800177 codegen->GetNumberOfFloatingPointRegisters()));
178
Vladimir Marko804b03f2016-09-14 16:26:36 +0100179 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100180 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
181 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800182
183 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
184 UseScratchRegisterScope temps(masm);
185
186 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100187 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
188 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800189 int64_t reg_size = kXRegSizeInBytes;
190 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
191 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100192 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800193 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
194 // If the offset does not fit in the instruction's immediate field, use an alternate register
195 // to compute the base address(float point registers spill base address).
196 Register new_base = temps.AcquireSameSizeAs(base);
197 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
198 base = new_base;
199 spill_offset = -core_spill_size;
200 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
201 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
202 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
203 }
204
205 if (is_save) {
206 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
207 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
208 } else {
209 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
210 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
211 }
212}
213
214void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800215 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 // If the register holds an object, update the stack mask.
219 if (locations->RegisterContainsObject(i)) {
220 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800221 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100222 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
223 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
224 saved_core_stack_offsets_[i] = stack_offset;
225 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800226 }
227
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
229 for (uint32_t i : LowToHighBits(fp_spills)) {
230 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
231 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
232 saved_fpu_stack_offsets_[i] = stack_offset;
233 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800234 }
235
Vladimir Marko804b03f2016-09-14 16:26:36 +0100236 SaveRestoreLiveRegistersHelper(codegen,
237 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800238 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
239}
240
241void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100242 SaveRestoreLiveRegistersHelper(codegen,
243 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800244 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
245}
246
Alexandre Rames5319def2014-10-23 10:03:10 +0100247class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
248 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000249 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100250
Alexandre Rames67555f72014-11-18 10:55:16 +0000251 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100252 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000253 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100254
Alexandre Rames5319def2014-10-23 10:03:10 +0100255 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 if (instruction_->CanThrowIntoCatchBlock()) {
257 // Live registers will be restored in the catch block if caught.
258 SaveLiveRegisters(codegen, instruction_->GetLocations());
259 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000260 // We're moving two locations to locations that could overlap, so we need a parallel
261 // move resolver.
262 InvokeRuntimeCallingConvention calling_convention;
263 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100264 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
265 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000266 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
267 ? kQuickThrowStringBounds
268 : kQuickThrowArrayBounds;
269 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100270 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800271 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100272 }
273
Alexandre Rames8158f282015-08-07 10:26:17 +0100274 bool IsFatal() const OVERRIDE { return true; }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
277
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100279 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
280};
281
Alexandre Rames67555f72014-11-18 10:55:16 +0000282class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
283 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000284 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
288 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000289 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800290 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000291 }
292
Alexandre Rames8158f282015-08-07 10:26:17 +0100293 bool IsFatal() const OVERRIDE { return true; }
294
Alexandre Rames9931f312015-06-19 14:47:01 +0100295 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
296
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000298 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
299};
300
301class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
302 public:
303 LoadClassSlowPathARM64(HLoadClass* cls,
304 HInstruction* at,
305 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000306 bool do_clinit,
307 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
308 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
309 : SlowPathCodeARM64(at),
310 cls_(cls),
311 dex_pc_(dex_pc),
312 do_clinit_(do_clinit),
313 bss_entry_temp_(bss_entry_temp),
314 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000315 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
316 }
317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000319 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000320 Location out = locations->Out();
321 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000322 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
323
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000324 InvokeRuntimeCallingConvention calling_convention;
325 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
326 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000327 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
328 bool is_load_class_bss_entry =
329 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000331 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000332 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
333 DCHECK(
334 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000335 }
336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000338 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000339
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000340 dex::TypeIndex type_index = cls_->GetTypeIndex();
341 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000342 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
343 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000344 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800345 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100346 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800347 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100348 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800349 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000350
351 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000352 if (out.IsValid()) {
353 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000354 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000355 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000356 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000357 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000359 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000362 if (call_saves_everything_except_r0_ip0) {
363 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
364 } else {
365 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
366 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
367 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
368 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000369 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 {
372 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
373 __ Bind(strp_label);
374 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000375 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 }
377 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 __ B(GetExitLabel());
379 }
380
Alexandre Rames9931f312015-06-19 14:47:01 +0100381 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
382
Alexandre Rames67555f72014-11-18 10:55:16 +0000383 private:
384 // The class this slow path will load.
385 HLoadClass* const cls_;
386
Alexandre Rames67555f72014-11-18 10:55:16 +0000387 // The dex PC of `at_`.
388 const uint32_t dex_pc_;
389
390 // Whether to initialize the class.
391 const bool do_clinit_;
392
Vladimir Markoea4c1262017-02-06 19:59:33 +0000393 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
394 vixl::aarch64::Register bss_entry_temp_;
395 vixl::aarch64::Label* bss_entry_adrp_label_;
396
Alexandre Rames67555f72014-11-18 10:55:16 +0000397 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
398};
399
Vladimir Markoaad75c62016-10-03 08:46:48 +0000400class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
401 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100402 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
403 : SlowPathCodeARM64(instruction),
404 temp_(temp),
405 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 LocationSummary* locations = instruction_->GetLocations();
409 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
410 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
411
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000412 InvokeRuntimeCallingConvention calling_convention;
413 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
414 DCHECK(temp_.IsValid());
415 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
416 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100417
Vladimir Markoaad75c62016-10-03 08:46:48 +0000418 __ Bind(GetEntryLabel());
419 SaveLiveRegisters(codegen, locations);
420
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000421 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
422 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000423 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
424 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
425 Primitive::Type type = instruction_->GetType();
426 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
427
428 RestoreLiveRegisters(codegen, locations);
429
430 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000431 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 if (!kUseReadBarrier || kUseBakerReadBarrier) {
433 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
434 } else {
435 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
436 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
437 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
438 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100440 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000441 {
442 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
443 __ Bind(strp_label);
444 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100445 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000446 }
447
448 __ B(GetExitLabel());
449 }
450
451 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
452
453 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100454 const Register temp_;
455 vixl::aarch64::Label* adrp_label_;
456
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
458};
459
Alexandre Rames5319def2014-10-23 10:03:10 +0100460class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
461 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000462 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100463
Alexandre Rames67555f72014-11-18 10:55:16 +0000464 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
465 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100466 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000467 if (instruction_->CanThrowIntoCatchBlock()) {
468 // Live registers will be restored in the catch block if caught.
469 SaveLiveRegisters(codegen, instruction_->GetLocations());
470 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000471 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
472 instruction_,
473 instruction_->GetDexPc(),
474 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800475 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100476 }
477
Alexandre Rames8158f282015-08-07 10:26:17 +0100478 bool IsFatal() const OVERRIDE { return true; }
479
Alexandre Rames9931f312015-06-19 14:47:01 +0100480 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
481
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
484};
485
486class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
487 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100488 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000489 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100490
Alexandre Rames67555f72014-11-18 10:55:16 +0000491 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100492 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000493 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100494 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100495 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000496 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800497 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100498 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000499 if (successor_ == nullptr) {
500 __ B(GetReturnLabel());
501 } else {
502 __ B(arm64_codegen->GetLabelOf(successor_));
503 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100504 }
505
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100507 DCHECK(successor_ == nullptr);
508 return &return_label_;
509 }
510
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100511 HBasicBlock* GetSuccessor() const {
512 return successor_;
513 }
514
Alexandre Rames9931f312015-06-19 14:47:01 +0100515 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
516
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100518 // If not null, the block to branch to after the suspend check.
519 HBasicBlock* const successor_;
520
521 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100522 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100523
524 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
525};
526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
528 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000530 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000531
532 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000533 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534
Alexandre Rames3e69f162014-12-10 10:36:50 +0000535 DCHECK(instruction_->IsCheckCast()
536 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
537 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100538 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000539
Alexandre Rames67555f72014-11-18 10:55:16 +0000540 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000542 if (!is_fatal_) {
543 SaveLiveRegisters(codegen, locations);
544 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000545
546 // We're moving two locations to locations that could overlap, so we need a parallel
547 // move resolver.
548 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800549 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800550 LocationFrom(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800553 LocationFrom(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000555 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000556 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800557 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000558 Primitive::Type ret_type = instruction_->GetType();
559 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
560 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
561 } else {
562 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800563 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
564 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000565 }
566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567 if (!is_fatal_) {
568 RestoreLiveRegisters(codegen, locations);
569 __ B(GetExitLabel());
570 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000571 }
572
Alexandre Rames9931f312015-06-19 14:47:01 +0100573 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100574 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100575
Alexandre Rames67555f72014-11-18 10:55:16 +0000576 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000577 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000578
Alexandre Rames67555f72014-11-18 10:55:16 +0000579 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
580};
581
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700582class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
583 public:
Aart Bik42249c32016-01-07 15:33:50 -0800584 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000585 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586
587 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800588 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000590 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000591 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700592 }
593
Alexandre Rames9931f312015-06-19 14:47:01 +0100594 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
595
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700596 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
598};
599
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
601 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000602 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100603
604 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
605 LocationSummary* locations = instruction_->GetLocations();
606 __ Bind(GetEntryLabel());
607 SaveLiveRegisters(codegen, locations);
608
609 InvokeRuntimeCallingConvention calling_convention;
610 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
611 parallel_move.AddMove(
612 locations->InAt(0),
613 LocationFrom(calling_convention.GetRegisterAt(0)),
614 Primitive::kPrimNot,
615 nullptr);
616 parallel_move.AddMove(
617 locations->InAt(1),
618 LocationFrom(calling_convention.GetRegisterAt(1)),
619 Primitive::kPrimInt,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(2),
623 LocationFrom(calling_convention.GetRegisterAt(2)),
624 Primitive::kPrimNot,
625 nullptr);
626 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
627
628 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000629 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100630 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
631 RestoreLiveRegisters(codegen, locations);
632 __ B(GetExitLabel());
633 }
634
635 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
636
637 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100638 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
639};
640
Zheng Xu3927c8b2015-11-18 17:46:25 +0800641void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
642 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000643 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800644
645 // We are about to use the assembler to place literals directly. Make sure we have enough
646 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000647 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
648 num_entries * sizeof(int32_t),
649 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800650
651 __ Bind(&table_start_);
652 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
653 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100654 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100656 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800657 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
658 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
659 Literal<int32_t> literal(jump_offset);
660 __ place(&literal);
661 }
662}
663
Roland Levillain54f869e2017-03-06 13:54:11 +0000664// Abstract base class for read barrier slow paths marking a reference
665// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000666//
Roland Levillain54f869e2017-03-06 13:54:11 +0000667// Argument `entrypoint` must be a register location holding the read
668// barrier marking runtime entry point to be invoked.
669class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
670 protected:
671 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
672 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000673 DCHECK(kEmitCompilerReadBarrier);
674 }
675
Roland Levillain54f869e2017-03-06 13:54:11 +0000676 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000677
Roland Levillain54f869e2017-03-06 13:54:11 +0000678 // Generate assembly code calling the read barrier marking runtime
679 // entry point (ReadBarrierMarkRegX).
680 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000681 // No need to save live registers; it's taken care of by the
682 // entrypoint. Also, there is no need to update the stack mask,
683 // as this runtime call will not trigger a garbage collection.
684 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
685 DCHECK_NE(ref_.reg(), LR);
686 DCHECK_NE(ref_.reg(), WSP);
687 DCHECK_NE(ref_.reg(), WZR);
688 // IP0 is used internally by the ReadBarrierMarkRegX entry point
689 // as a temporary, it cannot be the entry point's input/output.
690 DCHECK_NE(ref_.reg(), IP0);
691 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
692 // "Compact" slow path, saving two moves.
693 //
694 // Instead of using the standard runtime calling convention (input
695 // and output in W0):
696 //
697 // W0 <- ref
698 // W0 <- ReadBarrierMark(W0)
699 // ref <- W0
700 //
701 // we just use rX (the register containing `ref`) as input and output
702 // of a dedicated entrypoint:
703 //
704 // rX <- ReadBarrierMarkRegX(rX)
705 //
706 if (entrypoint_.IsValid()) {
707 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
708 __ Blr(XRegisterFrom(entrypoint_));
709 } else {
710 // Entrypoint is not already loaded, load from the thread.
711 int32_t entry_point_offset =
712 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
713 // This runtime call does not require a stack map.
714 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
715 }
716 }
717
718 // The location (register) of the marked object reference.
719 const Location ref_;
720
721 // The location of the entrypoint if it is already loaded.
722 const Location entrypoint_;
723
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 private:
725 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
726};
727
Alexandre Rames5319def2014-10-23 10:03:10 +0100728// Slow path marking an object reference `ref` during a read
729// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000730// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100731//
732// This means that after the execution of this slow path, `ref` will
733// always be up-to-date, but `obj.field` may not; i.e., after the
734// flip, `ref` will be a to-space reference, but `obj.field` will
735// probably still be a from-space reference (unless it gets updated by
736// another thread, or if another thread installed another object
737// reference (different from `ref`) in `obj.field`).
738//
739// If `entrypoint` is a valid location it is assumed to already be
740// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000741// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000742class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100743 public:
744 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
745 Location ref,
746 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100748 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100749 }
750
751 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
752
753 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000754 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100755 DCHECK(locations->CanCall());
756 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000757 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000758 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
759 << "Unexpected instruction in read barrier marking slow path: "
760 << instruction_->DebugName();
761
762 __ Bind(GetEntryLabel());
763 GenerateReadBarrierMarkRuntimeCall(codegen);
764 __ B(GetExitLabel());
765 }
766
767 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000768 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
769};
770
Roland Levillain54f869e2017-03-06 13:54:11 +0000771// Slow path loading `obj`'s lock word, loading a reference from
772// object `*(obj + offset + (index << scale_factor))` into `ref`, and
773// marking `ref` if `obj` is gray according to the lock word (Baker
774// read barrier). The field `obj.field` in the object `obj` holding
775// this reference does not get updated by this slow path after marking
776// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
777// below for that).
778//
779// This means that after the execution of this slow path, `ref` will
780// always be up-to-date, but `obj.field` may not; i.e., after the
781// flip, `ref` will be a to-space reference, but `obj.field` will
782// probably still be a from-space reference (unless it gets updated by
783// another thread, or if another thread installed another object
784// reference (different from `ref`) in `obj.field`).
785//
786// Argument `entrypoint` must be a register location holding the read
787// barrier marking runtime entry point to be invoked.
788class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
789 public:
790 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
791 Location ref,
792 Register obj,
793 uint32_t offset,
794 Location index,
795 size_t scale_factor,
796 bool needs_null_check,
797 bool use_load_acquire,
798 Register temp,
799 Location entrypoint)
800 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
801 obj_(obj),
802 offset_(offset),
803 index_(index),
804 scale_factor_(scale_factor),
805 needs_null_check_(needs_null_check),
806 use_load_acquire_(use_load_acquire),
807 temp_(temp) {
808 DCHECK(kEmitCompilerReadBarrier);
809 DCHECK(kUseBakerReadBarrier);
810 }
811
812 const char* GetDescription() const OVERRIDE {
813 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
814 }
815
816 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
817 LocationSummary* locations = instruction_->GetLocations();
818 DCHECK(locations->CanCall());
819 DCHECK(ref_.IsRegister()) << ref_;
820 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
821 DCHECK(obj_.IsW());
822 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100823 DCHECK(instruction_->IsInstanceFieldGet() ||
824 instruction_->IsStaticFieldGet() ||
825 instruction_->IsArrayGet() ||
826 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100827 instruction_->IsInstanceOf() ||
828 instruction_->IsCheckCast() ||
829 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
830 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
831 << "Unexpected instruction in read barrier marking slow path: "
832 << instruction_->DebugName();
833 // The read barrier instrumentation of object ArrayGet
834 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000835 // instruction.
836 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000837 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
838
Roland Levillain54f869e2017-03-06 13:54:11 +0000839 // Temporary register `temp_`, used to store the lock word, must
840 // not be IP0 nor IP1, as we may use them to emit the reference
841 // load (in the call to GenerateRawReferenceLoad below), and we
842 // need the lock word to still be in `temp_` after the reference
843 // load.
844 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
845 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
846
Alexandre Rames5319def2014-10-23 10:03:10 +0100847 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000848
849 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
850 // inserted after the original load. However, in fast path based
851 // Baker's read barriers, we need to perform the load of
852 // mirror::Object::monitor_ *before* the original reference load.
853 // This load-load ordering is required by the read barrier.
854 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100855 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000856 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
857 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
858 // HeapReference<mirror::Object> ref = *src; // Original reference load.
859 // bool is_gray = (rb_state == ReadBarrier::GrayState());
860 // if (is_gray) {
861 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
862 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000863 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000864 // Note: the original implementation in ReadBarrier::Barrier is
865 // slightly more complex as it performs additional checks that we do
866 // not do here for performance reasons.
867
868 // /* int32_t */ monitor = obj->monitor_
869 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
870 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
871 if (needs_null_check_) {
872 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100873 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // /* LockWord */ lock_word = LockWord(monitor)
875 static_assert(sizeof(LockWord) == sizeof(int32_t),
876 "art::LockWord and int32_t have different sizes.");
877
878 // Introduce a dependency on the lock_word including rb_state,
879 // to prevent load-load reordering, and without using
880 // a memory barrier (which would be more expensive).
881 // `obj` is unchanged by this operation, but its value now depends
882 // on `temp`.
883 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
884
885 // The actual reference load.
886 // A possible implicit null check has already been handled above.
887 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
888 arm64_codegen->GenerateRawReferenceLoad(instruction_,
889 ref_,
890 obj_,
891 offset_,
892 index_,
893 scale_factor_,
894 /* needs_null_check */ false,
895 use_load_acquire_);
896
897 // Mark the object `ref` when `obj` is gray.
898 //
899 // if (rb_state == ReadBarrier::GrayState())
900 // ref = ReadBarrier::Mark(ref);
901 //
902 // Given the numeric representation, it's enough to check the low bit of the rb_state.
903 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
904 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
905 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
906 GenerateReadBarrierMarkRuntimeCall(codegen);
907
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000908 __ B(GetExitLabel());
909 }
910
911 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000912 // The register containing the object holding the marked object reference field.
913 Register obj_;
914 // The offset, index and scale factor to access the reference in `obj_`.
915 uint32_t offset_;
916 Location index_;
917 size_t scale_factor_;
918 // Is a null check required?
919 bool needs_null_check_;
920 // Should this reference load use Load-Acquire semantics?
921 bool use_load_acquire_;
922 // A temporary register used to hold the lock word of `obj_`.
923 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000924
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000926};
927
Roland Levillain54f869e2017-03-06 13:54:11 +0000928// Slow path loading `obj`'s lock word, loading a reference from
929// object `*(obj + offset + (index << scale_factor))` into `ref`, and
930// marking `ref` if `obj` is gray according to the lock word (Baker
931// read barrier). If needed, this slow path also atomically updates
932// the field `obj.field` in the object `obj` holding this reference
933// after marking (contrary to
934// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
935// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100936//
937// This means that after the execution of this slow path, both `ref`
938// and `obj.field` will be up-to-date; i.e., after the flip, both will
939// hold the same to-space reference (unless another thread installed
940// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000941//
Roland Levillain54f869e2017-03-06 13:54:11 +0000942// Argument `entrypoint` must be a register location holding the read
943// barrier marking runtime entry point to be invoked.
944class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
945 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100946 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000947 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
948 Location ref,
949 Register obj,
950 uint32_t offset,
951 Location index,
952 size_t scale_factor,
953 bool needs_null_check,
954 bool use_load_acquire,
955 Register temp,
956 Location entrypoint)
957 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100958 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000959 offset_(offset),
960 index_(index),
961 scale_factor_(scale_factor),
962 needs_null_check_(needs_null_check),
963 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000964 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100965 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000966 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100967 }
968
969 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000970 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100971 }
972
973 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
974 LocationSummary* locations = instruction_->GetLocations();
975 Register ref_reg = WRegisterFrom(ref_);
976 DCHECK(locations->CanCall());
977 DCHECK(ref_.IsRegister()) << ref_;
978 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000979 DCHECK(obj_.IsW());
980 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
981
982 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100983 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
984 << "Unexpected instruction in read barrier marking and field updating slow path: "
985 << instruction_->DebugName();
986 DCHECK(instruction_->GetLocations()->Intrinsified());
987 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000988 DCHECK_EQ(offset_, 0u);
989 DCHECK_EQ(scale_factor_, 0u);
990 DCHECK_EQ(use_load_acquire_, false);
991 // The location of the offset of the marked reference field within `obj_`.
992 Location field_offset = index_;
993 DCHECK(field_offset.IsRegister()) << field_offset;
994
995 // Temporary register `temp_`, used to store the lock word, must
996 // not be IP0 nor IP1, as we may use them to emit the reference
997 // load (in the call to GenerateRawReferenceLoad below), and we
998 // need the lock word to still be in `temp_` after the reference
999 // load.
1000 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1001 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001002
1003 __ Bind(GetEntryLabel());
1004
Roland Levillain54f869e2017-03-06 13:54:11 +00001005 // /* int32_t */ monitor = obj->monitor_
1006 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1007 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1008 if (needs_null_check_) {
1009 codegen->MaybeRecordImplicitNullCheck(instruction_);
1010 }
1011 // /* LockWord */ lock_word = LockWord(monitor)
1012 static_assert(sizeof(LockWord) == sizeof(int32_t),
1013 "art::LockWord and int32_t have different sizes.");
1014
1015 // Introduce a dependency on the lock_word including rb_state,
1016 // to prevent load-load reordering, and without using
1017 // a memory barrier (which would be more expensive).
1018 // `obj` is unchanged by this operation, but its value now depends
1019 // on `temp`.
1020 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1021
1022 // The actual reference load.
1023 // A possible implicit null check has already been handled above.
1024 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1025 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1026 ref_,
1027 obj_,
1028 offset_,
1029 index_,
1030 scale_factor_,
1031 /* needs_null_check */ false,
1032 use_load_acquire_);
1033
1034 // Mark the object `ref` when `obj` is gray.
1035 //
1036 // if (rb_state == ReadBarrier::GrayState())
1037 // ref = ReadBarrier::Mark(ref);
1038 //
1039 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1040 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1041 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1042 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1043
1044 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001045 // Note that we cannot use IP to save the old reference, as IP is
1046 // used internally by the ReadBarrierMarkRegX entry point, and we
1047 // need the old reference after the call to that entry point.
1048 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1049 __ Mov(temp_.W(), ref_reg);
1050
Roland Levillain54f869e2017-03-06 13:54:11 +00001051 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001052
1053 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001054 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055 //
1056 // Note that this field could also hold a different object, if
1057 // another thread had concurrently changed it. In that case, the
1058 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1059 // (CAS) operation below would abort the CAS, leaving the field
1060 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001061 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001062 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063
1064 // Update the the holder's field atomically. This may fail if
1065 // mutator updates before us, but it's OK. This is achieved
1066 // using a strong compare-and-set (CAS) operation with relaxed
1067 // memory synchronization ordering, where the expected value is
1068 // the old reference and the desired value is the new reference.
1069
1070 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1071 UseScratchRegisterScope temps(masm);
1072
1073 // Convenience aliases.
1074 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001075 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001076 Register expected = temp_.W();
1077 Register value = ref_reg;
1078 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1079 Register tmp_value = temps.AcquireW(); // Value in memory.
1080
1081 __ Add(tmp_ptr, base.X(), Operand(offset));
1082
1083 if (kPoisonHeapReferences) {
1084 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1085 if (value.Is(expected)) {
1086 // Do not poison `value`, as it is the same register as
1087 // `expected`, which has just been poisoned.
1088 } else {
1089 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1090 }
1091 }
1092
1093 // do {
1094 // tmp_value = [tmp_ptr] - expected;
1095 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1096
Roland Levillain24a4d112016-10-26 13:10:46 +01001097 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001098 __ Bind(&loop_head);
1099 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1100 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001101 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001102 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1103 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001104 __ B(&exit_loop);
1105 __ Bind(&comparison_failed);
1106 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001107 __ Bind(&exit_loop);
1108
1109 if (kPoisonHeapReferences) {
1110 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1111 if (value.Is(expected)) {
1112 // Do not unpoison `value`, as it is the same register as
1113 // `expected`, which has just been unpoisoned.
1114 } else {
1115 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1116 }
1117 }
1118
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ B(GetExitLabel());
1120 }
1121
1122 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001123 // The register containing the object holding the marked object reference field.
1124 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001125 // The offset, index and scale factor to access the reference in `obj_`.
1126 uint32_t offset_;
1127 Location index_;
1128 size_t scale_factor_;
1129 // Is a null check required?
1130 bool needs_null_check_;
1131 // Should this reference load use Load-Acquire semantics?
1132 bool use_load_acquire_;
1133 // A temporary register used to hold the lock word of `obj_`; and
1134 // also to hold the original reference value, when the reference is
1135 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 const Register temp_;
1137
Roland Levillain54f869e2017-03-06 13:54:11 +00001138 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001139};
1140
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001141// Slow path generating a read barrier for a heap reference.
1142class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1143 public:
1144 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1145 Location out,
1146 Location ref,
1147 Location obj,
1148 uint32_t offset,
1149 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001150 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001151 out_(out),
1152 ref_(ref),
1153 obj_(obj),
1154 offset_(offset),
1155 index_(index) {
1156 DCHECK(kEmitCompilerReadBarrier);
1157 // If `obj` is equal to `out` or `ref`, it means the initial object
1158 // has been overwritten by (or after) the heap object reference load
1159 // to be instrumented, e.g.:
1160 //
1161 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001162 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001163 //
1164 // In that case, we have lost the information about the original
1165 // object, and the emitted read barrier cannot work properly.
1166 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1167 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1168 }
1169
1170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1171 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1172 LocationSummary* locations = instruction_->GetLocations();
1173 Primitive::Type type = Primitive::kPrimNot;
1174 DCHECK(locations->CanCall());
1175 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001176 DCHECK(instruction_->IsInstanceFieldGet() ||
1177 instruction_->IsStaticFieldGet() ||
1178 instruction_->IsArrayGet() ||
1179 instruction_->IsInstanceOf() ||
1180 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001181 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001182 << "Unexpected instruction in read barrier for heap reference slow path: "
1183 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001184 // The read barrier instrumentation of object ArrayGet
1185 // instructions does not support the HIntermediateAddress
1186 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001187 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001188 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001189
1190 __ Bind(GetEntryLabel());
1191
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001192 SaveLiveRegisters(codegen, locations);
1193
1194 // We may have to change the index's value, but as `index_` is a
1195 // constant member (like other "inputs" of this slow path),
1196 // introduce a copy of it, `index`.
1197 Location index = index_;
1198 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001199 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001200 if (instruction_->IsArrayGet()) {
1201 // Compute the actual memory offset and store it in `index`.
1202 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1203 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1204 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1205 // We are about to change the value of `index_reg` (see the
1206 // calls to vixl::MacroAssembler::Lsl and
1207 // vixl::MacroAssembler::Mov below), but it has
1208 // not been saved by the previous call to
1209 // art::SlowPathCode::SaveLiveRegisters, as it is a
1210 // callee-save register --
1211 // art::SlowPathCode::SaveLiveRegisters does not consider
1212 // callee-save registers, as it has been designed with the
1213 // assumption that callee-save registers are supposed to be
1214 // handled by the called function. So, as a callee-save
1215 // register, `index_reg` _would_ eventually be saved onto
1216 // the stack, but it would be too late: we would have
1217 // changed its value earlier. Therefore, we manually save
1218 // it here into another freely available register,
1219 // `free_reg`, chosen of course among the caller-save
1220 // registers (as a callee-save `free_reg` register would
1221 // exhibit the same problem).
1222 //
1223 // Note we could have requested a temporary register from
1224 // the register allocator instead; but we prefer not to, as
1225 // this is a slow path, and we know we can find a
1226 // caller-save register that is available.
1227 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1228 __ Mov(free_reg.W(), index_reg);
1229 index_reg = free_reg;
1230 index = LocationFrom(index_reg);
1231 } else {
1232 // The initial register stored in `index_` has already been
1233 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1234 // (as it is not a callee-save register), so we can freely
1235 // use it.
1236 }
1237 // Shifting the index value contained in `index_reg` by the scale
1238 // factor (2) cannot overflow in practice, as the runtime is
1239 // unable to allocate object arrays with a size larger than
1240 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1241 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1242 static_assert(
1243 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1244 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1245 __ Add(index_reg, index_reg, Operand(offset_));
1246 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001247 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1248 // intrinsics, `index_` is not shifted by a scale factor of 2
1249 // (as in the case of ArrayGet), as it is actually an offset
1250 // to an object field within an object.
1251 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001252 DCHECK(instruction_->GetLocations()->Intrinsified());
1253 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1254 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1255 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001256 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001257 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001258 }
1259 }
1260
1261 // We're moving two or three locations to locations that could
1262 // overlap, so we need a parallel move resolver.
1263 InvokeRuntimeCallingConvention calling_convention;
1264 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1265 parallel_move.AddMove(ref_,
1266 LocationFrom(calling_convention.GetRegisterAt(0)),
1267 type,
1268 nullptr);
1269 parallel_move.AddMove(obj_,
1270 LocationFrom(calling_convention.GetRegisterAt(1)),
1271 type,
1272 nullptr);
1273 if (index.IsValid()) {
1274 parallel_move.AddMove(index,
1275 LocationFrom(calling_convention.GetRegisterAt(2)),
1276 Primitive::kPrimInt,
1277 nullptr);
1278 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1279 } else {
1280 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1281 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1282 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001283 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001284 instruction_,
1285 instruction_->GetDexPc(),
1286 this);
1287 CheckEntrypointTypes<
1288 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1289 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1290
1291 RestoreLiveRegisters(codegen, locations);
1292
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001293 __ B(GetExitLabel());
1294 }
1295
1296 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1297
1298 private:
1299 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001300 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1301 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001302 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1303 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1304 return Register(VIXLRegCodeFromART(i), kXRegSize);
1305 }
1306 }
1307 // We shall never fail to find a free caller-save register, as
1308 // there are more than two core caller-save registers on ARM64
1309 // (meaning it is possible to find one which is different from
1310 // `ref` and `obj`).
1311 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1312 LOG(FATAL) << "Could not find a free register";
1313 UNREACHABLE();
1314 }
1315
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001316 const Location out_;
1317 const Location ref_;
1318 const Location obj_;
1319 const uint32_t offset_;
1320 // An additional location containing an index to an array.
1321 // Only used for HArrayGet and the UnsafeGetObject &
1322 // UnsafeGetObjectVolatile intrinsics.
1323 const Location index_;
1324
1325 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1326};
1327
1328// Slow path generating a read barrier for a GC root.
1329class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1330 public:
1331 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001332 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001333 DCHECK(kEmitCompilerReadBarrier);
1334 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001335
1336 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1337 LocationSummary* locations = instruction_->GetLocations();
1338 Primitive::Type type = Primitive::kPrimNot;
1339 DCHECK(locations->CanCall());
1340 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001341 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1342 << "Unexpected instruction in read barrier for GC root slow path: "
1343 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001344
1345 __ Bind(GetEntryLabel());
1346 SaveLiveRegisters(codegen, locations);
1347
1348 InvokeRuntimeCallingConvention calling_convention;
1349 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1350 // The argument of the ReadBarrierForRootSlow is not a managed
1351 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1352 // thus we need a 64-bit move here, and we cannot use
1353 //
1354 // arm64_codegen->MoveLocation(
1355 // LocationFrom(calling_convention.GetRegisterAt(0)),
1356 // root_,
1357 // type);
1358 //
1359 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1360 // reference type (`Primitive::kPrimNot`).
1361 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001362 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001363 instruction_,
1364 instruction_->GetDexPc(),
1365 this);
1366 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1367 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1368
1369 RestoreLiveRegisters(codegen, locations);
1370 __ B(GetExitLabel());
1371 }
1372
1373 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1374
1375 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001376 const Location out_;
1377 const Location root_;
1378
1379 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1380};
1381
Alexandre Rames5319def2014-10-23 10:03:10 +01001382#undef __
1383
1384Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1385 Location next_location;
1386 if (type == Primitive::kPrimVoid) {
1387 LOG(FATAL) << "Unreachable type " << type;
1388 }
1389
1390 if (Primitive::IsFloatingPointType(type) &&
1391 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001392 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1393 } else if (!Primitive::IsFloatingPointType(type) &&
1394 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1395 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1396 } else {
1397 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001398 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1399 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001400 }
1401
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001402 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001403 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001404 return next_location;
1405}
1406
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001407Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001408 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001409}
1410
Serban Constantinescu579885a2015-02-22 20:51:33 +00001411CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1412 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001413 const CompilerOptions& compiler_options,
1414 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001415 : CodeGenerator(graph,
1416 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001417 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001418 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001419 callee_saved_core_registers.GetList(),
1420 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001421 compiler_options,
1422 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001423 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001424 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001425 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001426 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001427 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001428 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001429 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001430 uint32_literals_(std::less<uint32_t>(),
1431 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001432 uint64_literals_(std::less<uint64_t>(),
1433 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001434 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1435 boot_image_string_patches_(StringReferenceValueComparator(),
1436 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1437 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001438 boot_image_type_patches_(TypeReferenceValueComparator(),
1439 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1440 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001441 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001442 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001443 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001444 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1445 jit_class_patches_(TypeReferenceValueComparator(),
1446 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001447 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001448 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001449}
Alexandre Rames5319def2014-10-23 10:03:10 +01001450
Alexandre Rames67555f72014-11-18 10:55:16 +00001451#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001452
Zheng Xu3927c8b2015-11-18 17:46:25 +08001453void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001454 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001455 jump_table->EmitTable(this);
1456 }
1457}
1458
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001459void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001460 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001461 // Ensure we emit the literal pool.
1462 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001463
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001464 CodeGenerator::Finalize(allocator);
1465}
1466
Zheng Xuad4450e2015-04-17 18:48:56 +08001467void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1468 // Note: There are 6 kinds of moves:
1469 // 1. constant -> GPR/FPR (non-cycle)
1470 // 2. constant -> stack (non-cycle)
1471 // 3. GPR/FPR -> GPR/FPR
1472 // 4. GPR/FPR -> stack
1473 // 5. stack -> GPR/FPR
1474 // 6. stack -> stack (non-cycle)
1475 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1476 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1477 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1478 // dependency.
1479 vixl_temps_.Open(GetVIXLAssembler());
1480}
1481
1482void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1483 vixl_temps_.Close();
1484}
1485
1486Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001487 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1488 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1489 || kind == Location::kSIMDStackSlot);
1490 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1491 ? Location::kFpuRegister
1492 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001493 Location scratch = GetScratchLocation(kind);
1494 if (!scratch.Equals(Location::NoLocation())) {
1495 return scratch;
1496 }
1497 // Allocate from VIXL temp registers.
1498 if (kind == Location::kRegister) {
1499 scratch = LocationFrom(vixl_temps_.AcquireX());
1500 } else {
1501 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001502 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1503 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1504 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001505 }
1506 AddScratchLocation(scratch);
1507 return scratch;
1508}
1509
1510void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1511 if (loc.IsRegister()) {
1512 vixl_temps_.Release(XRegisterFrom(loc));
1513 } else {
1514 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001515 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001516 }
1517 RemoveScratchLocation(loc);
1518}
1519
Alexandre Rames3e69f162014-12-10 10:36:50 +00001520void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001521 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001522 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001523}
1524
Alexandre Rames5319def2014-10-23 10:03:10 +01001525void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001526 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001527 __ Bind(&frame_entry_label_);
1528
Serban Constantinescu02164b32014-11-13 14:05:07 +00001529 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1530 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001531 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001532 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001533 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001534 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001535 {
1536 // Ensure that between load and RecordPcInfo there are no pools emitted.
1537 ExactAssemblyScope eas(GetVIXLAssembler(),
1538 kInstructionSize,
1539 CodeBufferCheckScope::kExactSize);
1540 __ ldr(wzr, MemOperand(temp, 0));
1541 RecordPcInfo(nullptr, 0);
1542 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001544
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001545 if (!HasEmptyFrame()) {
1546 int frame_size = GetFrameSize();
1547 // Stack layout:
1548 // sp[frame_size - 8] : lr.
1549 // ... : other preserved core registers.
1550 // ... : other preserved fp registers.
1551 // ... : reserved frame space.
1552 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001553
1554 // Save the current method if we need it. Note that we do not
1555 // do this in HCurrentMethod, as the instruction might have been removed
1556 // in the SSA graph.
1557 if (RequiresCurrentMethod()) {
1558 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001559 } else {
1560 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001561 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001562 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001563 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1564 frame_size - GetCoreSpillSize());
1565 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1566 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001567
1568 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1569 // Initialize should_deoptimize flag to 0.
1570 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1571 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1572 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001573 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001574}
1575
1576void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001577 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001578 if (!HasEmptyFrame()) {
1579 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001580 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1581 frame_size - FrameEntrySpillSize());
1582 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1583 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001584 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001585 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001586 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001587 __ Ret();
1588 GetAssembler()->cfi().RestoreState();
1589 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001590}
1591
Scott Wakeling97c72b72016-06-24 16:19:36 +01001592CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001593 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001594 return CPURegList(CPURegister::kRegister, kXRegSize,
1595 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001596}
1597
Scott Wakeling97c72b72016-06-24 16:19:36 +01001598CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001599 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1600 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001601 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1602 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001603}
1604
Alexandre Rames5319def2014-10-23 10:03:10 +01001605void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1606 __ Bind(GetLabelOf(block));
1607}
1608
Calin Juravle175dc732015-08-25 15:42:32 +01001609void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1610 DCHECK(location.IsRegister());
1611 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1612}
1613
Calin Juravlee460d1d2015-09-29 04:52:17 +01001614void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1615 if (location.IsRegister()) {
1616 locations->AddTemp(location);
1617 } else {
1618 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1619 }
1620}
1621
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001622void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001623 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001624 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001625 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001626 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001627 if (value_can_be_null) {
1628 __ Cbz(value, &done);
1629 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001630 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001631 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001632 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001633 if (value_can_be_null) {
1634 __ Bind(&done);
1635 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001636}
1637
David Brazdil58282f42016-01-14 12:45:10 +00001638void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001639 // Blocked core registers:
1640 // lr : Runtime reserved.
1641 // tr : Runtime reserved.
1642 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1643 // ip1 : VIXL core temp.
1644 // ip0 : VIXL core temp.
1645 //
1646 // Blocked fp registers:
1647 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001648 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1649 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001650 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001651 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001652 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001653
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001654 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001655 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001656 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001657 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001658
David Brazdil58282f42016-01-14 12:45:10 +00001659 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001660 // Stubs do not save callee-save floating point registers. If the graph
1661 // is debuggable, we need to deal with these registers differently. For
1662 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001663 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1664 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001665 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001666 }
1667 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001668}
1669
Alexandre Rames3e69f162014-12-10 10:36:50 +00001670size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1671 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1672 __ Str(reg, MemOperand(sp, stack_index));
1673 return kArm64WordSize;
1674}
1675
1676size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1677 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1678 __ Ldr(reg, MemOperand(sp, stack_index));
1679 return kArm64WordSize;
1680}
1681
1682size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1683 FPRegister reg = FPRegister(reg_id, kDRegSize);
1684 __ Str(reg, MemOperand(sp, stack_index));
1685 return kArm64WordSize;
1686}
1687
1688size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1689 FPRegister reg = FPRegister(reg_id, kDRegSize);
1690 __ Ldr(reg, MemOperand(sp, stack_index));
1691 return kArm64WordSize;
1692}
1693
Alexandre Rames5319def2014-10-23 10:03:10 +01001694void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001695 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001696}
1697
1698void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001699 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001700}
1701
Alexandre Rames67555f72014-11-18 10:55:16 +00001702void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001703 if (constant->IsIntConstant()) {
1704 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1705 } else if (constant->IsLongConstant()) {
1706 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1707 } else if (constant->IsNullConstant()) {
1708 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001709 } else if (constant->IsFloatConstant()) {
1710 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1711 } else {
1712 DCHECK(constant->IsDoubleConstant());
1713 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1714 }
1715}
1716
Alexandre Rames3e69f162014-12-10 10:36:50 +00001717
1718static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1719 DCHECK(constant.IsConstant());
1720 HConstant* cst = constant.GetConstant();
1721 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001722 // Null is mapped to a core W register, which we associate with kPrimInt.
1723 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001724 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1725 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1726 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1727}
1728
Roland Levillain558dea12017-01-27 19:40:44 +00001729// Allocate a scratch register from the VIXL pool, querying first into
1730// the floating-point register pool, and then the the core register
1731// pool. This is essentially a reimplementation of
1732// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1733// using a different allocation strategy.
1734static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1735 vixl::aarch64::UseScratchRegisterScope* temps,
1736 int size_in_bits) {
1737 return masm->GetScratchFPRegisterList()->IsEmpty()
1738 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1739 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1740}
1741
Calin Juravlee460d1d2015-09-29 04:52:17 +01001742void CodeGeneratorARM64::MoveLocation(Location destination,
1743 Location source,
1744 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001745 if (source.Equals(destination)) {
1746 return;
1747 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001748
1749 // A valid move can always be inferred from the destination and source
1750 // locations. When moving from and to a register, the argument type can be
1751 // used to generate 32bit instead of 64bit moves. In debug mode we also
1752 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001753 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001754
1755 if (destination.IsRegister() || destination.IsFpuRegister()) {
1756 if (unspecified_type) {
1757 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1758 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001759 (src_cst != nullptr && (src_cst->IsIntConstant()
1760 || src_cst->IsFloatConstant()
1761 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001762 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001763 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001764 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001765 // If the source is a double stack slot or a 64bit constant, a 64bit
1766 // type is appropriate. Else the source is a register, and since the
1767 // type has not been specified, we chose a 64bit type to force a 64bit
1768 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001769 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001770 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001771 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001772 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1773 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1774 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001775 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1776 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1777 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001778 } else if (source.IsSIMDStackSlot()) {
1779 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001780 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001781 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001782 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001783 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001784 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001785 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001786 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001787 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001788 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1789 ? Primitive::kPrimLong
1790 : Primitive::kPrimInt;
1791 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1792 }
1793 } else {
1794 DCHECK(source.IsFpuRegister());
1795 if (destination.IsRegister()) {
1796 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1797 ? Primitive::kPrimDouble
1798 : Primitive::kPrimFloat;
1799 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1800 } else {
1801 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001802 if (GetGraph()->HasSIMD()) {
1803 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1804 } else {
1805 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1806 }
1807 }
1808 }
1809 } else if (destination.IsSIMDStackSlot()) {
1810 if (source.IsFpuRegister()) {
1811 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1812 } else {
1813 DCHECK(source.IsSIMDStackSlot());
1814 UseScratchRegisterScope temps(GetVIXLAssembler());
1815 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1816 Register temp = temps.AcquireX();
1817 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1818 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1819 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1820 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1821 } else {
1822 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1823 __ Ldr(temp, StackOperandFrom(source));
1824 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001825 }
1826 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001827 } else { // The destination is not a register. It must be a stack slot.
1828 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1829 if (source.IsRegister() || source.IsFpuRegister()) {
1830 if (unspecified_type) {
1831 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001832 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001833 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001834 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001835 }
1836 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001837 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1838 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1839 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001840 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001841 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1842 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001843 UseScratchRegisterScope temps(GetVIXLAssembler());
1844 HConstant* src_cst = source.GetConstant();
1845 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001846 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001847 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1848 ? Register(xzr)
1849 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001850 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001851 if (src_cst->IsIntConstant()) {
1852 temp = temps.AcquireW();
1853 } else if (src_cst->IsLongConstant()) {
1854 temp = temps.AcquireX();
1855 } else if (src_cst->IsFloatConstant()) {
1856 temp = temps.AcquireS();
1857 } else {
1858 DCHECK(src_cst->IsDoubleConstant());
1859 temp = temps.AcquireD();
1860 }
1861 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001862 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001863 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001864 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001865 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001866 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001867 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001868 // Use any scratch register (a core or a floating-point one)
1869 // from VIXL scratch register pools as a temporary.
1870 //
1871 // We used to only use the FP scratch register pool, but in some
1872 // rare cases the only register from this pool (D31) would
1873 // already be used (e.g. within a ParallelMove instruction, when
1874 // a move is blocked by a another move requiring a scratch FP
1875 // register, which would reserve D31). To prevent this issue, we
1876 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001877 //
1878 // Also, we start by asking for a FP scratch register first, as the
1879 // demand of scratch core registers is higher. This is why we
1880 // use AcquireFPOrCoreCPURegisterOfSize instead of
1881 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1882 // allocates core scratch registers first.
1883 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1884 GetVIXLAssembler(),
1885 &temps,
1886 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001887 __ Ldr(temp, StackOperandFrom(source));
1888 __ Str(temp, StackOperandFrom(destination));
1889 }
1890 }
1891}
1892
1893void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001894 CPURegister dst,
1895 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001896 switch (type) {
1897 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001898 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001899 break;
1900 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001901 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001902 break;
1903 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001904 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001905 break;
1906 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001907 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001908 break;
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot:
1911 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001912 case Primitive::kPrimFloat:
1913 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001914 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 __ Ldr(dst, src);
1916 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 case Primitive::kPrimVoid:
1918 LOG(FATAL) << "Unreachable type " << type;
1919 }
1920}
1921
Calin Juravle77520bc2015-01-12 18:45:46 +00001922void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001923 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001924 const MemOperand& src,
1925 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001926 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001927 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001928 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001929 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001930
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001931 DCHECK(!src.IsPreIndex());
1932 DCHECK(!src.IsPostIndex());
1933
1934 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001935 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001936 {
1937 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1938 MemOperand base = MemOperand(temp_base);
1939 switch (type) {
1940 case Primitive::kPrimBoolean:
1941 {
1942 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1943 __ ldarb(Register(dst), base);
1944 if (needs_null_check) {
1945 MaybeRecordImplicitNullCheck(instruction);
1946 }
1947 }
1948 break;
1949 case Primitive::kPrimByte:
1950 {
1951 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1952 __ ldarb(Register(dst), base);
1953 if (needs_null_check) {
1954 MaybeRecordImplicitNullCheck(instruction);
1955 }
1956 }
1957 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1958 break;
1959 case Primitive::kPrimChar:
1960 {
1961 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1962 __ ldarh(Register(dst), base);
1963 if (needs_null_check) {
1964 MaybeRecordImplicitNullCheck(instruction);
1965 }
1966 }
1967 break;
1968 case Primitive::kPrimShort:
1969 {
1970 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1971 __ ldarh(Register(dst), base);
1972 if (needs_null_check) {
1973 MaybeRecordImplicitNullCheck(instruction);
1974 }
1975 }
1976 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1977 break;
1978 case Primitive::kPrimInt:
1979 case Primitive::kPrimNot:
1980 case Primitive::kPrimLong:
1981 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1982 {
1983 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1984 __ ldar(Register(dst), base);
1985 if (needs_null_check) {
1986 MaybeRecordImplicitNullCheck(instruction);
1987 }
1988 }
1989 break;
1990 case Primitive::kPrimFloat:
1991 case Primitive::kPrimDouble: {
1992 DCHECK(dst.IsFPRegister());
1993 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001994
Artem Serov914d7a82017-02-07 14:33:49 +00001995 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1996 {
1997 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1998 __ ldar(temp, base);
1999 if (needs_null_check) {
2000 MaybeRecordImplicitNullCheck(instruction);
2001 }
2002 }
2003 __ Fmov(FPRegister(dst), temp);
2004 break;
Roland Levillain44015862016-01-22 11:47:17 +00002005 }
Artem Serov914d7a82017-02-07 14:33:49 +00002006 case Primitive::kPrimVoid:
2007 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002009 }
2010}
2011
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002012void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002013 CPURegister src,
2014 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002015 switch (type) {
2016 case Primitive::kPrimBoolean:
2017 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002018 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002019 break;
2020 case Primitive::kPrimChar:
2021 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002023 break;
2024 case Primitive::kPrimInt:
2025 case Primitive::kPrimNot:
2026 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027 case Primitive::kPrimFloat:
2028 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002029 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002031 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002032 case Primitive::kPrimVoid:
2033 LOG(FATAL) << "Unreachable type " << type;
2034 }
2035}
2036
Artem Serov914d7a82017-02-07 14:33:49 +00002037void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2038 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002039 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002040 const MemOperand& dst,
2041 bool needs_null_check) {
2042 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002043 UseScratchRegisterScope temps(GetVIXLAssembler());
2044 Register temp_base = temps.AcquireX();
2045
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002046 DCHECK(!dst.IsPreIndex());
2047 DCHECK(!dst.IsPostIndex());
2048
2049 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002050 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002051 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002052 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002053 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002054 switch (type) {
2055 case Primitive::kPrimBoolean:
2056 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002057 {
2058 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2059 __ stlrb(Register(src), base);
2060 if (needs_null_check) {
2061 MaybeRecordImplicitNullCheck(instruction);
2062 }
2063 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002064 break;
2065 case Primitive::kPrimChar:
2066 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002067 {
2068 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2069 __ stlrh(Register(src), base);
2070 if (needs_null_check) {
2071 MaybeRecordImplicitNullCheck(instruction);
2072 }
2073 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002074 break;
2075 case Primitive::kPrimInt:
2076 case Primitive::kPrimNot:
2077 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002078 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002079 {
2080 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2081 __ stlr(Register(src), base);
2082 if (needs_null_check) {
2083 MaybeRecordImplicitNullCheck(instruction);
2084 }
2085 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002086 break;
2087 case Primitive::kPrimFloat:
2088 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002089 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002090 Register temp_src;
2091 if (src.IsZero()) {
2092 // The zero register is used to avoid synthesizing zero constants.
2093 temp_src = Register(src);
2094 } else {
2095 DCHECK(src.IsFPRegister());
2096 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2097 __ Fmov(temp_src, FPRegister(src));
2098 }
Artem Serov914d7a82017-02-07 14:33:49 +00002099 {
2100 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2101 __ stlr(temp_src, base);
2102 if (needs_null_check) {
2103 MaybeRecordImplicitNullCheck(instruction);
2104 }
2105 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002106 break;
2107 }
2108 case Primitive::kPrimVoid:
2109 LOG(FATAL) << "Unreachable type " << type;
2110 }
2111}
2112
Calin Juravle175dc732015-08-25 15:42:32 +01002113void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2114 HInstruction* instruction,
2115 uint32_t dex_pc,
2116 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002117 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002118
2119 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2120 {
2121 // Ensure the pc position is recorded immediately after the `blr` instruction.
2122 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2123 __ blr(lr);
2124 if (EntrypointRequiresStackMap(entrypoint)) {
2125 RecordPcInfo(instruction, dex_pc, slow_path);
2126 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002127 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002128}
2129
Roland Levillaindec8f632016-07-22 17:10:06 +01002130void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2131 HInstruction* instruction,
2132 SlowPathCode* slow_path) {
2133 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002134 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2135 __ Blr(lr);
2136}
2137
Alexandre Rames67555f72014-11-18 10:55:16 +00002138void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002139 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002140 UseScratchRegisterScope temps(GetVIXLAssembler());
2141 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002142 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2143
Serban Constantinescu02164b32014-11-13 14:05:07 +00002144 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002145 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2146 __ Add(temp, class_reg, status_offset);
2147 __ Ldar(temp, HeapOperand(temp));
2148 __ Cmp(temp, mirror::Class::kStatusInitialized);
2149 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002150 __ Bind(slow_path->GetExitLabel());
2151}
Alexandre Rames5319def2014-10-23 10:03:10 +01002152
Roland Levillain44015862016-01-22 11:47:17 +00002153void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002154 BarrierType type = BarrierAll;
2155
2156 switch (kind) {
2157 case MemBarrierKind::kAnyAny:
2158 case MemBarrierKind::kAnyStore: {
2159 type = BarrierAll;
2160 break;
2161 }
2162 case MemBarrierKind::kLoadAny: {
2163 type = BarrierReads;
2164 break;
2165 }
2166 case MemBarrierKind::kStoreStore: {
2167 type = BarrierWrites;
2168 break;
2169 }
2170 default:
2171 LOG(FATAL) << "Unexpected memory barrier " << kind;
2172 }
2173 __ Dmb(InnerShareable, type);
2174}
2175
Serban Constantinescu02164b32014-11-13 14:05:07 +00002176void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2177 HBasicBlock* successor) {
2178 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002179 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2180 if (slow_path == nullptr) {
2181 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2182 instruction->SetSlowPath(slow_path);
2183 codegen_->AddSlowPath(slow_path);
2184 if (successor != nullptr) {
2185 DCHECK(successor->IsLoopHeader());
2186 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2187 }
2188 } else {
2189 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2190 }
2191
Serban Constantinescu02164b32014-11-13 14:05:07 +00002192 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2193 Register temp = temps.AcquireW();
2194
Andreas Gampe542451c2016-07-26 09:02:02 -07002195 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002196 if (successor == nullptr) {
2197 __ Cbnz(temp, slow_path->GetEntryLabel());
2198 __ Bind(slow_path->GetReturnLabel());
2199 } else {
2200 __ Cbz(temp, codegen_->GetLabelOf(successor));
2201 __ B(slow_path->GetEntryLabel());
2202 // slow_path will return to GetLabelOf(successor).
2203 }
2204}
2205
Alexandre Rames5319def2014-10-23 10:03:10 +01002206InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2207 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002208 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002209 assembler_(codegen->GetAssembler()),
2210 codegen_(codegen) {}
2211
2212#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002213 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002214
2215#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2216
2217enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002218 // Using a base helps identify when we hit such breakpoints.
2219 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002220#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2221 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2222#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2223};
2224
2225#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002226 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002227 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2228 } \
2229 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2231 locations->SetOut(Location::Any()); \
2232 }
2233 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2234#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2235
2236#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002237#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002238
Alexandre Rames67555f72014-11-18 10:55:16 +00002239void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002240 DCHECK_EQ(instr->InputCount(), 2U);
2241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2242 Primitive::Type type = instr->GetResultType();
2243 switch (type) {
2244 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002245 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002246 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002247 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002249 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002250
2251 case Primitive::kPrimFloat:
2252 case Primitive::kPrimDouble:
2253 locations->SetInAt(0, Location::RequiresFpuRegister());
2254 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002255 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002256 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002257
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002259 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 }
2261}
2262
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002263void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2264 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002265 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2266
2267 bool object_field_get_with_read_barrier =
2268 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002269 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002270 new (GetGraph()->GetArena()) LocationSummary(instruction,
2271 object_field_get_with_read_barrier ?
2272 LocationSummary::kCallOnSlowPath :
2273 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002274 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002275 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002276 // We need a temporary register for the read barrier marking slow
2277 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002278 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2279 !Runtime::Current()->UseJitCompilation() &&
2280 !field_info.IsVolatile()) {
2281 // If link-time thunks for the Baker read barrier are enabled, for AOT
2282 // non-volatile loads we need a temporary only if the offset is too big.
2283 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2284 locations->AddTemp(FixedTempLocation());
2285 }
2286 } else {
2287 locations->AddTemp(Location::RequiresRegister());
2288 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002289 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002290 locations->SetInAt(0, Location::RequiresRegister());
2291 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2292 locations->SetOut(Location::RequiresFpuRegister());
2293 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002294 // The output overlaps for an object field get when read barriers
2295 // are enabled: we do not want the load to overwrite the object's
2296 // location, as we need it to emit the read barrier.
2297 locations->SetOut(
2298 Location::RequiresRegister(),
2299 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002300 }
2301}
2302
2303void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2304 const FieldInfo& field_info) {
2305 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002306 LocationSummary* locations = instruction->GetLocations();
2307 Location base_loc = locations->InAt(0);
2308 Location out = locations->Out();
2309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002310 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002311 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002312
Roland Levillain44015862016-01-22 11:47:17 +00002313 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2314 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002315 // /* HeapReference<Object> */ out = *(base + offset)
2316 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002317 Location maybe_temp =
2318 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002319 // Note that potential implicit null checks are handled in this
2320 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2321 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2322 instruction,
2323 out,
2324 base,
2325 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002326 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002327 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002328 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002329 } else {
2330 // General case.
2331 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002332 // Note that a potential implicit null check is handled in this
2333 // CodeGeneratorARM64::LoadAcquire call.
2334 // NB: LoadAcquire will record the pc info if needed.
2335 codegen_->LoadAcquire(
2336 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002337 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002338 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2339 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002340 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002342 }
Roland Levillain44015862016-01-22 11:47:17 +00002343 if (field_type == Primitive::kPrimNot) {
2344 // If read barriers are enabled, emit read barriers other than
2345 // Baker's using a slow path (and also unpoison the loaded
2346 // reference, if heap poisoning is enabled).
2347 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2348 }
Roland Levillain4d027112015-07-01 15:41:14 +01002349 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002350}
2351
2352void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2353 LocationSummary* locations =
2354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2355 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002356 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2357 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2358 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002359 locations->SetInAt(1, Location::RequiresFpuRegister());
2360 } else {
2361 locations->SetInAt(1, Location::RequiresRegister());
2362 }
2363}
2364
2365void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002366 const FieldInfo& field_info,
2367 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002368 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2369
2370 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002371 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002372 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 Offset offset = field_info.GetFieldOffset();
2374 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002375
Roland Levillain4d027112015-07-01 15:41:14 +01002376 {
2377 // We use a block to end the scratch scope before the write barrier, thus
2378 // freeing the temporary registers so they can be used in `MarkGCCard`.
2379 UseScratchRegisterScope temps(GetVIXLAssembler());
2380
2381 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2382 DCHECK(value.IsW());
2383 Register temp = temps.AcquireW();
2384 __ Mov(temp, value.W());
2385 GetAssembler()->PoisonHeapReference(temp.W());
2386 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002387 }
Roland Levillain4d027112015-07-01 15:41:14 +01002388
2389 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002390 codegen_->StoreRelease(
2391 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002392 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002393 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2394 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002395 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2396 codegen_->MaybeRecordImplicitNullCheck(instruction);
2397 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002398 }
2399
2400 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002401 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002402 }
2403}
2404
Alexandre Rames67555f72014-11-18 10:55:16 +00002405void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002406 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002407
2408 switch (type) {
2409 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002410 case Primitive::kPrimLong: {
2411 Register dst = OutputRegister(instr);
2412 Register lhs = InputRegisterAt(instr, 0);
2413 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002414 if (instr->IsAdd()) {
2415 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002416 } else if (instr->IsAnd()) {
2417 __ And(dst, lhs, rhs);
2418 } else if (instr->IsOr()) {
2419 __ Orr(dst, lhs, rhs);
2420 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002421 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002422 } else if (instr->IsRor()) {
2423 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002424 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002425 __ Ror(dst, lhs, shift);
2426 } else {
2427 // Ensure shift distance is in the same size register as the result. If
2428 // we are rotating a long and the shift comes in a w register originally,
2429 // we don't need to sxtw for use as an x since the shift distances are
2430 // all & reg_bits - 1.
2431 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2432 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002433 } else {
2434 DCHECK(instr->IsXor());
2435 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002436 }
2437 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002438 }
2439 case Primitive::kPrimFloat:
2440 case Primitive::kPrimDouble: {
2441 FPRegister dst = OutputFPRegister(instr);
2442 FPRegister lhs = InputFPRegisterAt(instr, 0);
2443 FPRegister rhs = InputFPRegisterAt(instr, 1);
2444 if (instr->IsAdd()) {
2445 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002446 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002447 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002448 } else {
2449 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002450 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002451 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002453 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002454 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002455 }
2456}
2457
Serban Constantinescu02164b32014-11-13 14:05:07 +00002458void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2459 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2460
2461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2462 Primitive::Type type = instr->GetResultType();
2463 switch (type) {
2464 case Primitive::kPrimInt:
2465 case Primitive::kPrimLong: {
2466 locations->SetInAt(0, Location::RequiresRegister());
2467 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002469 break;
2470 }
2471 default:
2472 LOG(FATAL) << "Unexpected shift type " << type;
2473 }
2474}
2475
2476void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2477 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2478
2479 Primitive::Type type = instr->GetType();
2480 switch (type) {
2481 case Primitive::kPrimInt:
2482 case Primitive::kPrimLong: {
2483 Register dst = OutputRegister(instr);
2484 Register lhs = InputRegisterAt(instr, 0);
2485 Operand rhs = InputOperandAt(instr, 1);
2486 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002487 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002488 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002489 if (instr->IsShl()) {
2490 __ Lsl(dst, lhs, shift_value);
2491 } else if (instr->IsShr()) {
2492 __ Asr(dst, lhs, shift_value);
2493 } else {
2494 __ Lsr(dst, lhs, shift_value);
2495 }
2496 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002497 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002498
2499 if (instr->IsShl()) {
2500 __ Lsl(dst, lhs, rhs_reg);
2501 } else if (instr->IsShr()) {
2502 __ Asr(dst, lhs, rhs_reg);
2503 } else {
2504 __ Lsr(dst, lhs, rhs_reg);
2505 }
2506 }
2507 break;
2508 }
2509 default:
2510 LOG(FATAL) << "Unexpected shift operation type " << type;
2511 }
2512}
2513
Alexandre Rames5319def2014-10-23 10:03:10 +01002514void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002515 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002516}
2517
2518void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002519 HandleBinaryOp(instruction);
2520}
2521
2522void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2527 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
Artem Serov7fc63502016-02-09 17:15:29 +00002530void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002531 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2532 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2533 locations->SetInAt(0, Location::RequiresRegister());
2534 // There is no immediate variant of negated bitwise instructions in AArch64.
2535 locations->SetInAt(1, Location::RequiresRegister());
2536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2537}
2538
Artem Serov7fc63502016-02-09 17:15:29 +00002539void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002540 Register dst = OutputRegister(instr);
2541 Register lhs = InputRegisterAt(instr, 0);
2542 Register rhs = InputRegisterAt(instr, 1);
2543
2544 switch (instr->GetOpKind()) {
2545 case HInstruction::kAnd:
2546 __ Bic(dst, lhs, rhs);
2547 break;
2548 case HInstruction::kOr:
2549 __ Orn(dst, lhs, rhs);
2550 break;
2551 case HInstruction::kXor:
2552 __ Eon(dst, lhs, rhs);
2553 break;
2554 default:
2555 LOG(FATAL) << "Unreachable";
2556 }
2557}
2558
Anton Kirilov74234da2017-01-13 14:42:47 +00002559void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2560 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002561 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2562 instruction->GetType() == Primitive::kPrimLong);
2563 LocationSummary* locations =
2564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2565 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2566 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2567 } else {
2568 locations->SetInAt(0, Location::RequiresRegister());
2569 }
2570 locations->SetInAt(1, Location::RequiresRegister());
2571 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2572}
2573
Anton Kirilov74234da2017-01-13 14:42:47 +00002574void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2575 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002576 Primitive::Type type = instruction->GetType();
2577 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2578 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2579 Register out = OutputRegister(instruction);
2580 Register left;
2581 if (kind != HInstruction::kNeg) {
2582 left = InputRegisterAt(instruction, 0);
2583 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002584 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002585 // shifter operand operation, the IR generating `right_reg` (input to the type
2586 // conversion) can have a different type from the current instruction's type,
2587 // so we manually indicate the type.
2588 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002589 Operand right_operand(0);
2590
Anton Kirilov74234da2017-01-13 14:42:47 +00002591 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2592 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002593 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2594 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002595 right_operand = Operand(right_reg,
2596 helpers::ShiftFromOpKind(op_kind),
2597 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002598 }
2599
2600 // Logical binary operations do not support extension operations in the
2601 // operand. Note that VIXL would still manage if it was passed by generating
2602 // the extension as a separate instruction.
2603 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2604 DCHECK(!right_operand.IsExtendedRegister() ||
2605 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2606 kind != HInstruction::kNeg));
2607 switch (kind) {
2608 case HInstruction::kAdd:
2609 __ Add(out, left, right_operand);
2610 break;
2611 case HInstruction::kAnd:
2612 __ And(out, left, right_operand);
2613 break;
2614 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002615 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002616 __ Neg(out, right_operand);
2617 break;
2618 case HInstruction::kOr:
2619 __ Orr(out, left, right_operand);
2620 break;
2621 case HInstruction::kSub:
2622 __ Sub(out, left, right_operand);
2623 break;
2624 case HInstruction::kXor:
2625 __ Eor(out, left, right_operand);
2626 break;
2627 default:
2628 LOG(FATAL) << "Unexpected operation kind: " << kind;
2629 UNREACHABLE();
2630 }
2631}
2632
Artem Serov328429f2016-07-06 16:23:04 +01002633void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2636 locations->SetInAt(0, Location::RequiresRegister());
2637 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002639}
2640
Roland Levillain19c54192016-11-04 13:44:09 +00002641void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002642 __ Add(OutputRegister(instruction),
2643 InputRegisterAt(instruction, 0),
2644 Operand(InputOperandAt(instruction, 1)));
2645}
2646
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002647void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002650 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2651 if (instr->GetOpKind() == HInstruction::kSub &&
2652 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002653 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002654 // Don't allocate register for Mneg instruction.
2655 } else {
2656 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2657 Location::RequiresRegister());
2658 }
2659 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2660 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2662}
2663
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002664void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002665 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002666 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2667 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002668
2669 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2670 // This fixup should be carried out for all multiply-accumulate instructions:
2671 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2672 if (instr->GetType() == Primitive::kPrimLong &&
2673 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2674 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002675 vixl::aarch64::Instruction* prev =
2676 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002677 if (prev->IsLoadOrStore()) {
2678 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002679 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002680 __ nop();
2681 }
2682 }
2683
2684 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002685 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002686 __ Madd(res, mul_left, mul_right, accumulator);
2687 } else {
2688 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002689 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002690 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002691 __ Mneg(res, mul_left, mul_right);
2692 } else {
2693 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2694 __ Msub(res, mul_left, mul_right, accumulator);
2695 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002696 }
2697}
2698
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002699void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002700 bool object_array_get_with_read_barrier =
2701 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002702 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002703 new (GetGraph()->GetArena()) LocationSummary(instruction,
2704 object_array_get_with_read_barrier ?
2705 LocationSummary::kCallOnSlowPath :
2706 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002707 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002708 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002709 // We need a temporary register for the read barrier marking slow
2710 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002711 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2712 !Runtime::Current()->UseJitCompilation() &&
2713 instruction->GetIndex()->IsConstant()) {
2714 // Array loads with constant index are treated as field loads.
2715 // If link-time thunks for the Baker read barrier are enabled, for AOT
2716 // constant index loads we need a temporary only if the offset is too big.
2717 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2718 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2719 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2720 if (offset >= kReferenceLoadMinFarOffset) {
2721 locations->AddTemp(FixedTempLocation());
2722 }
2723 } else {
2724 locations->AddTemp(Location::RequiresRegister());
2725 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002726 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002727 locations->SetInAt(0, Location::RequiresRegister());
2728 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002729 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2730 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2731 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002732 // The output overlaps in the case of an object array get with
2733 // read barriers enabled: we do not want the move to overwrite the
2734 // array's location, as we need it to emit the read barrier.
2735 locations->SetOut(
2736 Location::RequiresRegister(),
2737 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002738 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002739}
2740
2741void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002742 Primitive::Type type = instruction->GetType();
2743 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002744 LocationSummary* locations = instruction->GetLocations();
2745 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002746 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002747 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002748 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2749 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002750 MacroAssembler* masm = GetVIXLAssembler();
2751 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002752
Roland Levillain19c54192016-11-04 13:44:09 +00002753 // The read barrier instrumentation of object ArrayGet instructions
2754 // does not support the HIntermediateAddress instruction.
2755 DCHECK(!((type == Primitive::kPrimNot) &&
2756 instruction->GetArray()->IsIntermediateAddress() &&
2757 kEmitCompilerReadBarrier));
2758
Roland Levillain44015862016-01-22 11:47:17 +00002759 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2760 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002761 // Note that a potential implicit null check is handled in the
2762 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002763 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002764 if (index.IsConstant()) {
2765 // Array load with a constant index can be treated as a field load.
2766 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2767 Location maybe_temp =
2768 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2769 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2770 out,
2771 obj.W(),
2772 offset,
2773 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002774 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002775 /* use_load_acquire */ false);
2776 } else {
2777 Register temp = WRegisterFrom(locations->GetTemp(0));
2778 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002779 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002780 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002781 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002782 // General case.
2783 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002784 Register length;
2785 if (maybe_compressed_char_at) {
2786 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2787 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002788 {
2789 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2790 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2791
2792 if (instruction->GetArray()->IsIntermediateAddress()) {
2793 DCHECK_LT(count_offset, offset);
2794 int64_t adjusted_offset =
2795 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2796 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2797 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2798 } else {
2799 __ Ldr(length, HeapOperand(obj, count_offset));
2800 }
2801 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002802 }
jessicahandojo05765752016-09-09 19:01:32 -07002803 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002804 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002805 if (maybe_compressed_char_at) {
2806 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002807 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2808 "Expecting 0=compressed, 1=uncompressed");
2809 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002810 __ Ldrb(Register(OutputCPURegister(instruction)),
2811 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2812 __ B(&done);
2813 __ Bind(&uncompressed_load);
2814 __ Ldrh(Register(OutputCPURegister(instruction)),
2815 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2816 __ Bind(&done);
2817 } else {
2818 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2819 source = HeapOperand(obj, offset);
2820 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002821 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002822 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002823 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002824 // We do not need to compute the intermediate address from the array: the
2825 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002826 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002827 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002828 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002829 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2830 }
2831 temp = obj;
2832 } else {
2833 __ Add(temp, obj, offset);
2834 }
jessicahandojo05765752016-09-09 19:01:32 -07002835 if (maybe_compressed_char_at) {
2836 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002837 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2838 "Expecting 0=compressed, 1=uncompressed");
2839 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002840 __ Ldrb(Register(OutputCPURegister(instruction)),
2841 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2842 __ B(&done);
2843 __ Bind(&uncompressed_load);
2844 __ Ldrh(Register(OutputCPURegister(instruction)),
2845 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2846 __ Bind(&done);
2847 } else {
2848 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2849 }
Roland Levillain44015862016-01-22 11:47:17 +00002850 }
jessicahandojo05765752016-09-09 19:01:32 -07002851 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002852 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2853 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002854 codegen_->Load(type, OutputCPURegister(instruction), source);
2855 codegen_->MaybeRecordImplicitNullCheck(instruction);
2856 }
Roland Levillain44015862016-01-22 11:47:17 +00002857
2858 if (type == Primitive::kPrimNot) {
2859 static_assert(
2860 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2861 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2862 Location obj_loc = locations->InAt(0);
2863 if (index.IsConstant()) {
2864 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2865 } else {
2866 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2867 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002868 }
Roland Levillain4d027112015-07-01 15:41:14 +01002869 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002870}
2871
Alexandre Rames5319def2014-10-23 10:03:10 +01002872void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2873 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2874 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002875 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002876}
2877
2878void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002879 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002880 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002881 {
2882 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2883 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2884 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2885 codegen_->MaybeRecordImplicitNullCheck(instruction);
2886 }
jessicahandojo05765752016-09-09 19:01:32 -07002887 // Mask out compression flag from String's array length.
2888 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002889 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002890 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002891}
2892
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002893void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002894 Primitive::Type value_type = instruction->GetComponentType();
2895
2896 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002897 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2898 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002899 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002900 LocationSummary::kCallOnSlowPath :
2901 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002904 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2905 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2906 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002907 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002908 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002909 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002910 }
2911}
2912
2913void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2914 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002915 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002916 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002917 bool needs_write_barrier =
2918 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002919
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002920 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002921 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002922 CPURegister source = value;
2923 Location index = locations->InAt(1);
2924 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2925 MemOperand destination = HeapOperand(array);
2926 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002927
2928 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002929 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002930 if (index.IsConstant()) {
2931 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2932 destination = HeapOperand(array, offset);
2933 } else {
2934 UseScratchRegisterScope temps(masm);
2935 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002936 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002937 // We do not need to compute the intermediate address from the array: the
2938 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002939 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002940 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002941 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002942 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2943 }
2944 temp = array;
2945 } else {
2946 __ Add(temp, array, offset);
2947 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002948 destination = HeapOperand(temp,
2949 XRegisterFrom(index),
2950 LSL,
2951 Primitive::ComponentSizeShift(value_type));
2952 }
Artem Serov914d7a82017-02-07 14:33:49 +00002953 {
2954 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2955 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2956 codegen_->Store(value_type, value, destination);
2957 codegen_->MaybeRecordImplicitNullCheck(instruction);
2958 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002959 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002960 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002961 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002962 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002963 {
2964 // We use a block to end the scratch scope before the write barrier, thus
2965 // freeing the temporary registers so they can be used in `MarkGCCard`.
2966 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002967 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002968 if (index.IsConstant()) {
2969 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002970 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002971 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002972 destination = HeapOperand(temp,
2973 XRegisterFrom(index),
2974 LSL,
2975 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002976 }
2977
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002978 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2979 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2980 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2981
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002982 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002983 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2984 codegen_->AddSlowPath(slow_path);
2985 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002986 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002987 __ Cbnz(Register(value), &non_zero);
2988 if (!index.IsConstant()) {
2989 __ Add(temp, array, offset);
2990 }
Artem Serov914d7a82017-02-07 14:33:49 +00002991 {
2992 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2993 // emitted.
2994 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2995 __ Str(wzr, destination);
2996 codegen_->MaybeRecordImplicitNullCheck(instruction);
2997 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002998 __ B(&done);
2999 __ Bind(&non_zero);
3000 }
3001
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003002 // Note that when Baker read barriers are enabled, the type
3003 // checks are performed without read barriers. This is fine,
3004 // even in the case where a class object is in the from-space
3005 // after the flip, as a comparison involving such a type would
3006 // not produce a false positive; it may of course produce a
3007 // false negative, in which case we would take the ArraySet
3008 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003009
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003010 Register temp2 = temps.AcquireSameSizeAs(array);
3011 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003012 {
3013 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3014 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3015 __ Ldr(temp, HeapOperand(array, class_offset));
3016 codegen_->MaybeRecordImplicitNullCheck(instruction);
3017 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003018 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003019
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003020 // /* HeapReference<Class> */ temp = temp->component_type_
3021 __ Ldr(temp, HeapOperand(temp, component_offset));
3022 // /* HeapReference<Class> */ temp2 = value->klass_
3023 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3024 // If heap poisoning is enabled, no need to unpoison `temp`
3025 // nor `temp2`, as we are comparing two poisoned references.
3026 __ Cmp(temp, temp2);
3027 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003028
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003029 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3030 vixl::aarch64::Label do_put;
3031 __ B(eq, &do_put);
3032 // If heap poisoning is enabled, the `temp` reference has
3033 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003034 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3035
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003036 // /* HeapReference<Class> */ temp = temp->super_class_
3037 __ Ldr(temp, HeapOperand(temp, super_offset));
3038 // If heap poisoning is enabled, no need to unpoison
3039 // `temp`, as we are comparing against null below.
3040 __ Cbnz(temp, slow_path->GetEntryLabel());
3041 __ Bind(&do_put);
3042 } else {
3043 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003044 }
3045 }
3046
3047 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003048 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003049 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003050 __ Mov(temp2, value.W());
3051 GetAssembler()->PoisonHeapReference(temp2);
3052 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003053 }
3054
3055 if (!index.IsConstant()) {
3056 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003057 } else {
3058 // We no longer need the `temp` here so release it as the store below may
3059 // need a scratch register (if the constant index makes the offset too large)
3060 // and the poisoned `source` could be using the other scratch register.
3061 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003062 }
Artem Serov914d7a82017-02-07 14:33:49 +00003063 {
3064 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3065 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3066 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003067
Artem Serov914d7a82017-02-07 14:33:49 +00003068 if (!may_need_runtime_call_for_type_check) {
3069 codegen_->MaybeRecordImplicitNullCheck(instruction);
3070 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003071 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003072 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003073
3074 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3075
3076 if (done.IsLinked()) {
3077 __ Bind(&done);
3078 }
3079
3080 if (slow_path != nullptr) {
3081 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003082 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003083 }
3084}
3085
Alexandre Rames67555f72014-11-18 10:55:16 +00003086void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003087 RegisterSet caller_saves = RegisterSet::Empty();
3088 InvokeRuntimeCallingConvention calling_convention;
3089 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3090 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3091 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003092 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003093 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003094}
3095
3096void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003097 BoundsCheckSlowPathARM64* slow_path =
3098 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003099 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003100 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3101 __ B(slow_path->GetEntryLabel(), hs);
3102}
3103
Alexandre Rames67555f72014-11-18 10:55:16 +00003104void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3105 LocationSummary* locations =
3106 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3107 locations->SetInAt(0, Location::RequiresRegister());
3108 if (check->HasUses()) {
3109 locations->SetOut(Location::SameAsFirstInput());
3110 }
3111}
3112
3113void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3114 // We assume the class is not null.
3115 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3116 check->GetLoadClass(), check, check->GetDexPc(), true);
3117 codegen_->AddSlowPath(slow_path);
3118 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3119}
3120
Roland Levillain1a653882016-03-18 18:05:57 +00003121static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3122 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3123 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3124}
3125
3126void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3127 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3128 Location rhs_loc = instruction->GetLocations()->InAt(1);
3129 if (rhs_loc.IsConstant()) {
3130 // 0.0 is the only immediate that can be encoded directly in
3131 // an FCMP instruction.
3132 //
3133 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3134 // specify that in a floating-point comparison, positive zero
3135 // and negative zero are considered equal, so we can use the
3136 // literal 0.0 for both cases here.
3137 //
3138 // Note however that some methods (Float.equal, Float.compare,
3139 // Float.compareTo, Double.equal, Double.compare,
3140 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3141 // StrictMath.min) consider 0.0 to be (strictly) greater than
3142 // -0.0. So if we ever translate calls to these methods into a
3143 // HCompare instruction, we must handle the -0.0 case with
3144 // care here.
3145 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3146 __ Fcmp(lhs_reg, 0.0);
3147 } else {
3148 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3149 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003150}
3151
Serban Constantinescu02164b32014-11-13 14:05:07 +00003152void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003153 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003154 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3155 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003156 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003157 case Primitive::kPrimBoolean:
3158 case Primitive::kPrimByte:
3159 case Primitive::kPrimShort:
3160 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003161 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003162 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003163 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003164 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3166 break;
3167 }
3168 case Primitive::kPrimFloat:
3169 case Primitive::kPrimDouble: {
3170 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003171 locations->SetInAt(1,
3172 IsFloatingPointZeroConstant(compare->InputAt(1))
3173 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3174 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003175 locations->SetOut(Location::RequiresRegister());
3176 break;
3177 }
3178 default:
3179 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3180 }
3181}
3182
3183void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3184 Primitive::Type in_type = compare->InputAt(0)->GetType();
3185
3186 // 0 if: left == right
3187 // 1 if: left > right
3188 // -1 if: left < right
3189 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003190 case Primitive::kPrimBoolean:
3191 case Primitive::kPrimByte:
3192 case Primitive::kPrimShort:
3193 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003194 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003195 case Primitive::kPrimLong: {
3196 Register result = OutputRegister(compare);
3197 Register left = InputRegisterAt(compare, 0);
3198 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003199 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003200 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3201 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003202 break;
3203 }
3204 case Primitive::kPrimFloat:
3205 case Primitive::kPrimDouble: {
3206 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003207 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003208 __ Cset(result, ne);
3209 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003210 break;
3211 }
3212 default:
3213 LOG(FATAL) << "Unimplemented compare type " << in_type;
3214 }
3215}
3216
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003217void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003218 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003219
3220 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3221 locations->SetInAt(0, Location::RequiresFpuRegister());
3222 locations->SetInAt(1,
3223 IsFloatingPointZeroConstant(instruction->InputAt(1))
3224 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3225 : Location::RequiresFpuRegister());
3226 } else {
3227 // Integer cases.
3228 locations->SetInAt(0, Location::RequiresRegister());
3229 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3230 }
3231
David Brazdilb3e773e2016-01-26 11:28:37 +00003232 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003234 }
3235}
3236
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003237void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003238 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003239 return;
3240 }
3241
3242 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003243 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003244 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003245
Roland Levillain7f63c522015-07-13 15:54:55 +00003246 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003247 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003248 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003249 } else {
3250 // Integer cases.
3251 Register lhs = InputRegisterAt(instruction, 0);
3252 Operand rhs = InputOperandAt(instruction, 1);
3253 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003254 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003255 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003256}
3257
3258#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3259 M(Equal) \
3260 M(NotEqual) \
3261 M(LessThan) \
3262 M(LessThanOrEqual) \
3263 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003264 M(GreaterThanOrEqual) \
3265 M(Below) \
3266 M(BelowOrEqual) \
3267 M(Above) \
3268 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003269#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003270void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3271void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003272FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003273#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003274#undef FOR_EACH_CONDITION_INSTRUCTION
3275
Zheng Xuc6667102015-05-15 16:08:45 +08003276void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3277 DCHECK(instruction->IsDiv() || instruction->IsRem());
3278
3279 LocationSummary* locations = instruction->GetLocations();
3280 Location second = locations->InAt(1);
3281 DCHECK(second.IsConstant());
3282
3283 Register out = OutputRegister(instruction);
3284 Register dividend = InputRegisterAt(instruction, 0);
3285 int64_t imm = Int64FromConstant(second.GetConstant());
3286 DCHECK(imm == 1 || imm == -1);
3287
3288 if (instruction->IsRem()) {
3289 __ Mov(out, 0);
3290 } else {
3291 if (imm == 1) {
3292 __ Mov(out, dividend);
3293 } else {
3294 __ Neg(out, dividend);
3295 }
3296 }
3297}
3298
3299void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3300 DCHECK(instruction->IsDiv() || instruction->IsRem());
3301
3302 LocationSummary* locations = instruction->GetLocations();
3303 Location second = locations->InAt(1);
3304 DCHECK(second.IsConstant());
3305
3306 Register out = OutputRegister(instruction);
3307 Register dividend = InputRegisterAt(instruction, 0);
3308 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003309 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003310 int ctz_imm = CTZ(abs_imm);
3311
3312 UseScratchRegisterScope temps(GetVIXLAssembler());
3313 Register temp = temps.AcquireSameSizeAs(out);
3314
3315 if (instruction->IsDiv()) {
3316 __ Add(temp, dividend, abs_imm - 1);
3317 __ Cmp(dividend, 0);
3318 __ Csel(out, temp, dividend, lt);
3319 if (imm > 0) {
3320 __ Asr(out, out, ctz_imm);
3321 } else {
3322 __ Neg(out, Operand(out, ASR, ctz_imm));
3323 }
3324 } else {
3325 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3326 __ Asr(temp, dividend, bits - 1);
3327 __ Lsr(temp, temp, bits - ctz_imm);
3328 __ Add(out, dividend, temp);
3329 __ And(out, out, abs_imm - 1);
3330 __ Sub(out, out, temp);
3331 }
3332}
3333
3334void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3335 DCHECK(instruction->IsDiv() || instruction->IsRem());
3336
3337 LocationSummary* locations = instruction->GetLocations();
3338 Location second = locations->InAt(1);
3339 DCHECK(second.IsConstant());
3340
3341 Register out = OutputRegister(instruction);
3342 Register dividend = InputRegisterAt(instruction, 0);
3343 int64_t imm = Int64FromConstant(second.GetConstant());
3344
3345 Primitive::Type type = instruction->GetResultType();
3346 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3347
3348 int64_t magic;
3349 int shift;
3350 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3351
3352 UseScratchRegisterScope temps(GetVIXLAssembler());
3353 Register temp = temps.AcquireSameSizeAs(out);
3354
3355 // temp = get_high(dividend * magic)
3356 __ Mov(temp, magic);
3357 if (type == Primitive::kPrimLong) {
3358 __ Smulh(temp, dividend, temp);
3359 } else {
3360 __ Smull(temp.X(), dividend, temp);
3361 __ Lsr(temp.X(), temp.X(), 32);
3362 }
3363
3364 if (imm > 0 && magic < 0) {
3365 __ Add(temp, temp, dividend);
3366 } else if (imm < 0 && magic > 0) {
3367 __ Sub(temp, temp, dividend);
3368 }
3369
3370 if (shift != 0) {
3371 __ Asr(temp, temp, shift);
3372 }
3373
3374 if (instruction->IsDiv()) {
3375 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3376 } else {
3377 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3378 // TODO: Strength reduction for msub.
3379 Register temp_imm = temps.AcquireSameSizeAs(out);
3380 __ Mov(temp_imm, imm);
3381 __ Msub(out, temp, temp_imm, dividend);
3382 }
3383}
3384
3385void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3386 DCHECK(instruction->IsDiv() || instruction->IsRem());
3387 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003388 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003389
3390 LocationSummary* locations = instruction->GetLocations();
3391 Register out = OutputRegister(instruction);
3392 Location second = locations->InAt(1);
3393
3394 if (second.IsConstant()) {
3395 int64_t imm = Int64FromConstant(second.GetConstant());
3396
3397 if (imm == 0) {
3398 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3399 } else if (imm == 1 || imm == -1) {
3400 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003401 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003402 DivRemByPowerOfTwo(instruction);
3403 } else {
3404 DCHECK(imm <= -2 || imm >= 2);
3405 GenerateDivRemWithAnyConstant(instruction);
3406 }
3407 } else {
3408 Register dividend = InputRegisterAt(instruction, 0);
3409 Register divisor = InputRegisterAt(instruction, 1);
3410 if (instruction->IsDiv()) {
3411 __ Sdiv(out, dividend, divisor);
3412 } else {
3413 UseScratchRegisterScope temps(GetVIXLAssembler());
3414 Register temp = temps.AcquireSameSizeAs(out);
3415 __ Sdiv(temp, dividend, divisor);
3416 __ Msub(out, temp, divisor, dividend);
3417 }
3418 }
3419}
3420
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003421void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3422 LocationSummary* locations =
3423 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3424 switch (div->GetResultType()) {
3425 case Primitive::kPrimInt:
3426 case Primitive::kPrimLong:
3427 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003428 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3430 break;
3431
3432 case Primitive::kPrimFloat:
3433 case Primitive::kPrimDouble:
3434 locations->SetInAt(0, Location::RequiresFpuRegister());
3435 locations->SetInAt(1, Location::RequiresFpuRegister());
3436 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3437 break;
3438
3439 default:
3440 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3441 }
3442}
3443
3444void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3445 Primitive::Type type = div->GetResultType();
3446 switch (type) {
3447 case Primitive::kPrimInt:
3448 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003449 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003450 break;
3451
3452 case Primitive::kPrimFloat:
3453 case Primitive::kPrimDouble:
3454 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3455 break;
3456
3457 default:
3458 LOG(FATAL) << "Unexpected div type " << type;
3459 }
3460}
3461
Alexandre Rames67555f72014-11-18 10:55:16 +00003462void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003463 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003464 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003465}
3466
3467void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3468 SlowPathCodeARM64* slow_path =
3469 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3470 codegen_->AddSlowPath(slow_path);
3471 Location value = instruction->GetLocations()->InAt(0);
3472
Alexandre Rames3e69f162014-12-10 10:36:50 +00003473 Primitive::Type type = instruction->GetType();
3474
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003475 if (!Primitive::IsIntegralType(type)) {
3476 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003477 return;
3478 }
3479
Alexandre Rames67555f72014-11-18 10:55:16 +00003480 if (value.IsConstant()) {
3481 int64_t divisor = Int64ConstantFrom(value);
3482 if (divisor == 0) {
3483 __ B(slow_path->GetEntryLabel());
3484 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003485 // A division by a non-null constant is valid. We don't need to perform
3486 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003487 }
3488 } else {
3489 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3490 }
3491}
3492
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003493void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3494 LocationSummary* locations =
3495 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3496 locations->SetOut(Location::ConstantLocation(constant));
3497}
3498
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003499void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3500 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003501 // Will be generated at use site.
3502}
3503
Alexandre Rames5319def2014-10-23 10:03:10 +01003504void LocationsBuilderARM64::VisitExit(HExit* exit) {
3505 exit->SetLocations(nullptr);
3506}
3507
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003508void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003509}
3510
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003511void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3512 LocationSummary* locations =
3513 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3514 locations->SetOut(Location::ConstantLocation(constant));
3515}
3516
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003517void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003518 // Will be generated at use site.
3519}
3520
David Brazdilfc6a86a2015-06-26 10:33:45 +00003521void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003522 DCHECK(!successor->IsExitBlock());
3523 HBasicBlock* block = got->GetBlock();
3524 HInstruction* previous = got->GetPrevious();
3525 HLoopInformation* info = block->GetLoopInformation();
3526
David Brazdil46e2a392015-03-16 17:31:52 +00003527 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003528 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3529 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3530 return;
3531 }
3532 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3533 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3534 }
3535 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003536 __ B(codegen_->GetLabelOf(successor));
3537 }
3538}
3539
David Brazdilfc6a86a2015-06-26 10:33:45 +00003540void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3541 got->SetLocations(nullptr);
3542}
3543
3544void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3545 HandleGoto(got, got->GetSuccessor());
3546}
3547
3548void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3549 try_boundary->SetLocations(nullptr);
3550}
3551
3552void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3553 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3554 if (!successor->IsExitBlock()) {
3555 HandleGoto(try_boundary, successor);
3556 }
3557}
3558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003559void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003560 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003561 vixl::aarch64::Label* true_target,
3562 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003563 // FP branching requires both targets to be explicit. If either of the targets
3564 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003565 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003566 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003567
David Brazdil0debae72015-11-12 18:37:00 +00003568 if (true_target == nullptr && false_target == nullptr) {
3569 // Nothing to do. The code always falls through.
3570 return;
3571 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003572 // Constant condition, statically compared against "true" (integer value 1).
3573 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003574 if (true_target != nullptr) {
3575 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003576 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003577 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003578 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003579 if (false_target != nullptr) {
3580 __ B(false_target);
3581 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003582 }
David Brazdil0debae72015-11-12 18:37:00 +00003583 return;
3584 }
3585
3586 // The following code generates these patterns:
3587 // (1) true_target == nullptr && false_target != nullptr
3588 // - opposite condition true => branch to false_target
3589 // (2) true_target != nullptr && false_target == nullptr
3590 // - condition true => branch to true_target
3591 // (3) true_target != nullptr && false_target != nullptr
3592 // - condition true => branch to true_target
3593 // - branch to false_target
3594 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003595 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003596 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003597 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003598 if (true_target == nullptr) {
3599 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3600 } else {
3601 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3602 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003603 } else {
3604 // The condition instruction has not been materialized, use its inputs as
3605 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003606 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003607
David Brazdil0debae72015-11-12 18:37:00 +00003608 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003609 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003610 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003611 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003612 IfCondition opposite_condition = condition->GetOppositeCondition();
3613 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003614 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003615 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003616 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003617 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003618 // Integer cases.
3619 Register lhs = InputRegisterAt(condition, 0);
3620 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003621
3622 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003623 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003624 if (true_target == nullptr) {
3625 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3626 non_fallthrough_target = false_target;
3627 } else {
3628 arm64_cond = ARM64Condition(condition->GetCondition());
3629 non_fallthrough_target = true_target;
3630 }
3631
Aart Bik086d27e2016-01-20 17:02:00 -08003632 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003633 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003634 switch (arm64_cond) {
3635 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003636 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003637 break;
3638 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003639 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003640 break;
3641 case lt:
3642 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003643 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003644 break;
3645 case ge:
3646 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003647 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003648 break;
3649 default:
3650 // Without the `static_cast` the compiler throws an error for
3651 // `-Werror=sign-promo`.
3652 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3653 }
3654 } else {
3655 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003656 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003657 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003658 }
3659 }
David Brazdil0debae72015-11-12 18:37:00 +00003660
3661 // If neither branch falls through (case 3), the conditional branch to `true_target`
3662 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3663 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003664 __ B(false_target);
3665 }
David Brazdil0debae72015-11-12 18:37:00 +00003666
3667 if (fallthrough_target.IsLinked()) {
3668 __ Bind(&fallthrough_target);
3669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003670}
3671
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003672void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3673 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003674 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003675 locations->SetInAt(0, Location::RequiresRegister());
3676 }
3677}
3678
3679void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003680 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3681 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003682 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3683 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3684 true_target = nullptr;
3685 }
3686 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3687 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3688 false_target = nullptr;
3689 }
David Brazdil0debae72015-11-12 18:37:00 +00003690 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003691}
3692
3693void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3694 LocationSummary* locations = new (GetGraph()->GetArena())
3695 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003696 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003697 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003698 locations->SetInAt(0, Location::RequiresRegister());
3699 }
3700}
3701
3702void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003703 SlowPathCodeARM64* slow_path =
3704 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003705 GenerateTestAndBranch(deoptimize,
3706 /* condition_input_index */ 0,
3707 slow_path->GetEntryLabel(),
3708 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003709}
3710
Mingyao Yang063fc772016-08-02 11:02:54 -07003711void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3712 LocationSummary* locations = new (GetGraph()->GetArena())
3713 LocationSummary(flag, LocationSummary::kNoCall);
3714 locations->SetOut(Location::RequiresRegister());
3715}
3716
3717void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3718 __ Ldr(OutputRegister(flag),
3719 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3720}
3721
David Brazdilc0b601b2016-02-08 14:20:45 +00003722static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3723 return condition->IsCondition() &&
3724 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3725}
3726
Alexandre Rames880f1192016-06-13 16:04:50 +01003727static inline Condition GetConditionForSelect(HCondition* condition) {
3728 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003729 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3730 : ARM64Condition(cond);
3731}
3732
David Brazdil74eb1b22015-12-14 11:44:01 +00003733void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3734 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003735 if (Primitive::IsFloatingPointType(select->GetType())) {
3736 locations->SetInAt(0, Location::RequiresFpuRegister());
3737 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003738 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003739 } else {
3740 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3741 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3742 bool is_true_value_constant = cst_true_value != nullptr;
3743 bool is_false_value_constant = cst_false_value != nullptr;
3744 // Ask VIXL whether we should synthesize constants in registers.
3745 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3746 Operand true_op = is_true_value_constant ?
3747 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3748 Operand false_op = is_false_value_constant ?
3749 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3750 bool true_value_in_register = false;
3751 bool false_value_in_register = false;
3752 MacroAssembler::GetCselSynthesisInformation(
3753 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3754 true_value_in_register |= !is_true_value_constant;
3755 false_value_in_register |= !is_false_value_constant;
3756
3757 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3758 : Location::ConstantLocation(cst_true_value));
3759 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3760 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003761 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003762 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003763
David Brazdil74eb1b22015-12-14 11:44:01 +00003764 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3765 locations->SetInAt(2, Location::RequiresRegister());
3766 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003767}
3768
3769void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003770 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003771 Condition csel_cond;
3772
3773 if (IsBooleanValueOrMaterializedCondition(cond)) {
3774 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003775 // Use the condition flags set by the previous instruction.
3776 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003777 } else {
3778 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003779 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003780 }
3781 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003782 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003783 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003784 } else {
3785 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003786 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003787 }
3788
Alexandre Rames880f1192016-06-13 16:04:50 +01003789 if (Primitive::IsFloatingPointType(select->GetType())) {
3790 __ Fcsel(OutputFPRegister(select),
3791 InputFPRegisterAt(select, 1),
3792 InputFPRegisterAt(select, 0),
3793 csel_cond);
3794 } else {
3795 __ Csel(OutputRegister(select),
3796 InputOperandAt(select, 1),
3797 InputOperandAt(select, 0),
3798 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003799 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003800}
3801
David Srbecky0cf44932015-12-09 14:09:59 +00003802void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3803 new (GetGraph()->GetArena()) LocationSummary(info);
3804}
3805
David Srbeckyd28f4a02016-03-14 17:14:24 +00003806void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3807 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003808}
3809
3810void CodeGeneratorARM64::GenerateNop() {
3811 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003812}
3813
Alexandre Rames5319def2014-10-23 10:03:10 +01003814void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003815 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003816}
3817
3818void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003819 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003820}
3821
3822void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003823 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003824}
3825
3826void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003827 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003828}
3829
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003830// Temp is used for read barrier.
3831static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3832 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003833 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003834 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3835 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3836 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3837 return 1;
3838 }
3839 return 0;
3840}
3841
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003842// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003843// interface pointer, one for loading the current interface.
3844// The other checks have one temp for loading the object's class.
3845static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3846 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3847 return 3;
3848 }
3849 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003850}
3851
Alexandre Rames67555f72014-11-18 10:55:16 +00003852void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003853 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003854 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003855 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003856 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 case TypeCheckKind::kExactCheck:
3858 case TypeCheckKind::kAbstractClassCheck:
3859 case TypeCheckKind::kClassHierarchyCheck:
3860 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003861 call_kind =
3862 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003863 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003864 break;
3865 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003866 case TypeCheckKind::kUnresolvedCheck:
3867 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003868 call_kind = LocationSummary::kCallOnSlowPath;
3869 break;
3870 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003871
Alexandre Rames67555f72014-11-18 10:55:16 +00003872 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003873 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003874 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003875 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003876 locations->SetInAt(0, Location::RequiresRegister());
3877 locations->SetInAt(1, Location::RequiresRegister());
3878 // The "out" register is used as a temporary, so it overlaps with the inputs.
3879 // Note that TypeCheckSlowPathARM64 uses this register too.
3880 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003881 // Add temps if necessary for read barriers.
3882 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003883}
3884
3885void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003886 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003887 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 Register obj = InputRegisterAt(instruction, 0);
3890 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003891 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003892 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003893 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3894 DCHECK_LE(num_temps, 1u);
3895 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003896 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3897 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3898 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3899 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003900
Scott Wakeling97c72b72016-06-24 16:19:36 +01003901 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003903
3904 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003905 // Avoid null check if we know `obj` is not null.
3906 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003907 __ Cbz(obj, &zero);
3908 }
3909
Roland Levillain44015862016-01-22 11:47:17 +00003910 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003911 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003912 // /* HeapReference<Class> */ out = obj->klass_
3913 GenerateReferenceLoadTwoRegisters(instruction,
3914 out_loc,
3915 obj_loc,
3916 class_offset,
3917 maybe_temp_loc,
3918 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 __ Cmp(out, cls);
3920 __ Cset(out, eq);
3921 if (zero.IsLinked()) {
3922 __ B(&done);
3923 }
3924 break;
3925 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003926
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003927 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003928 // /* HeapReference<Class> */ out = obj->klass_
3929 GenerateReferenceLoadTwoRegisters(instruction,
3930 out_loc,
3931 obj_loc,
3932 class_offset,
3933 maybe_temp_loc,
3934 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003935 // If the class is abstract, we eagerly fetch the super class of the
3936 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003937 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003939 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003940 GenerateReferenceLoadOneRegister(instruction,
3941 out_loc,
3942 super_offset,
3943 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003944 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003945 // If `out` is null, we use it for the result, and jump to `done`.
3946 __ Cbz(out, &done);
3947 __ Cmp(out, cls);
3948 __ B(ne, &loop);
3949 __ Mov(out, 1);
3950 if (zero.IsLinked()) {
3951 __ B(&done);
3952 }
3953 break;
3954 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003955
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003956 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003957 // /* HeapReference<Class> */ out = obj->klass_
3958 GenerateReferenceLoadTwoRegisters(instruction,
3959 out_loc,
3960 obj_loc,
3961 class_offset,
3962 maybe_temp_loc,
3963 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003964 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003965 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003966 __ Bind(&loop);
3967 __ Cmp(out, cls);
3968 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003969 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003970 GenerateReferenceLoadOneRegister(instruction,
3971 out_loc,
3972 super_offset,
3973 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003974 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003975 __ Cbnz(out, &loop);
3976 // If `out` is null, we use it for the result, and jump to `done`.
3977 __ B(&done);
3978 __ Bind(&success);
3979 __ Mov(out, 1);
3980 if (zero.IsLinked()) {
3981 __ B(&done);
3982 }
3983 break;
3984 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003985
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003986 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003987 // /* HeapReference<Class> */ out = obj->klass_
3988 GenerateReferenceLoadTwoRegisters(instruction,
3989 out_loc,
3990 obj_loc,
3991 class_offset,
3992 maybe_temp_loc,
3993 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003994 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003995 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003996 __ Cmp(out, cls);
3997 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003998 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003999 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004000 GenerateReferenceLoadOneRegister(instruction,
4001 out_loc,
4002 component_offset,
4003 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004004 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004005 // If `out` is null, we use it for the result, and jump to `done`.
4006 __ Cbz(out, &done);
4007 __ Ldrh(out, HeapOperand(out, primitive_offset));
4008 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4009 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004010 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004011 __ Mov(out, 1);
4012 __ B(&done);
4013 break;
4014 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004015
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004016 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004017 // No read barrier since the slow path will retry upon failure.
4018 // /* HeapReference<Class> */ out = obj->klass_
4019 GenerateReferenceLoadTwoRegisters(instruction,
4020 out_loc,
4021 obj_loc,
4022 class_offset,
4023 maybe_temp_loc,
4024 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004025 __ Cmp(out, cls);
4026 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004027 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4028 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004029 codegen_->AddSlowPath(slow_path);
4030 __ B(ne, slow_path->GetEntryLabel());
4031 __ Mov(out, 1);
4032 if (zero.IsLinked()) {
4033 __ B(&done);
4034 }
4035 break;
4036 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037
Calin Juravle98893e12015-10-02 21:05:03 +01004038 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004039 case TypeCheckKind::kInterfaceCheck: {
4040 // Note that we indeed only call on slow path, but we always go
4041 // into the slow path for the unresolved and interface check
4042 // cases.
4043 //
4044 // We cannot directly call the InstanceofNonTrivial runtime
4045 // entry point without resorting to a type checking slow path
4046 // here (i.e. by calling InvokeRuntime directly), as it would
4047 // require to assign fixed registers for the inputs of this
4048 // HInstanceOf instruction (following the runtime calling
4049 // convention), which might be cluttered by the potential first
4050 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004051 //
4052 // TODO: Introduce a new runtime entry point taking the object
4053 // to test (instead of its class) as argument, and let it deal
4054 // with the read barrier issues. This will let us refactor this
4055 // case of the `switch` code as it was previously (with a direct
4056 // call to the runtime not using a type checking slow path).
4057 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004058 DCHECK(locations->OnlyCallsOnSlowPath());
4059 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4060 /* is_fatal */ false);
4061 codegen_->AddSlowPath(slow_path);
4062 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004063 if (zero.IsLinked()) {
4064 __ B(&done);
4065 }
4066 break;
4067 }
4068 }
4069
4070 if (zero.IsLinked()) {
4071 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004072 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004073 }
4074
4075 if (done.IsLinked()) {
4076 __ Bind(&done);
4077 }
4078
4079 if (slow_path != nullptr) {
4080 __ Bind(slow_path->GetExitLabel());
4081 }
4082}
4083
4084void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4085 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4086 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4087
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4089 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004090 case TypeCheckKind::kExactCheck:
4091 case TypeCheckKind::kAbstractClassCheck:
4092 case TypeCheckKind::kClassHierarchyCheck:
4093 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004094 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4095 LocationSummary::kCallOnSlowPath :
4096 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004097 break;
4098 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004099 case TypeCheckKind::kUnresolvedCheck:
4100 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004101 call_kind = LocationSummary::kCallOnSlowPath;
4102 break;
4103 }
4104
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4106 locations->SetInAt(0, Location::RequiresRegister());
4107 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004108 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4109 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004110}
4111
4112void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004113 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004114 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004115 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004116 Register obj = InputRegisterAt(instruction, 0);
4117 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004118 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4119 DCHECK_GE(num_temps, 1u);
4120 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004121 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004122 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4123 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004124 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004125 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4126 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4127 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4128 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4129 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4130 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4131 const uint32_t object_array_data_offset =
4132 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004133
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004134 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004135 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4136 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4137 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004138 if (!kEmitCompilerReadBarrier) {
4139 is_type_check_slow_path_fatal =
4140 (type_check_kind == TypeCheckKind::kExactCheck ||
4141 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4142 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4143 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4144 !instruction->CanThrowIntoCatchBlock();
4145 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004146 SlowPathCodeARM64* type_check_slow_path =
4147 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4148 is_type_check_slow_path_fatal);
4149 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150
Scott Wakeling97c72b72016-06-24 16:19:36 +01004151 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004152 // Avoid null check if we know obj is not null.
4153 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004154 __ Cbz(obj, &done);
4155 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004156
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004157 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004158 case TypeCheckKind::kExactCheck:
4159 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160 // /* HeapReference<Class> */ temp = obj->klass_
4161 GenerateReferenceLoadTwoRegisters(instruction,
4162 temp_loc,
4163 obj_loc,
4164 class_offset,
4165 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004166 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 __ Cmp(temp, cls);
4169 // Jump to slow path for throwing the exception or doing a
4170 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004171 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004172 break;
4173 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004174
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004175 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176 // /* HeapReference<Class> */ temp = obj->klass_
4177 GenerateReferenceLoadTwoRegisters(instruction,
4178 temp_loc,
4179 obj_loc,
4180 class_offset,
4181 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004182 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 // If the class is abstract, we eagerly fetch the super class of the
4185 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004186 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004187 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004188 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004189 GenerateReferenceLoadOneRegister(instruction,
4190 temp_loc,
4191 super_offset,
4192 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004193 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004194
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004195 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4196 // exception.
4197 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4198 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199 __ Cmp(temp, cls);
4200 __ B(ne, &loop);
4201 break;
4202 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205 // /* HeapReference<Class> */ temp = obj->klass_
4206 GenerateReferenceLoadTwoRegisters(instruction,
4207 temp_loc,
4208 obj_loc,
4209 class_offset,
4210 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004211 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004214 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004215 __ Bind(&loop);
4216 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004217 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004219 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004220 GenerateReferenceLoadOneRegister(instruction,
4221 temp_loc,
4222 super_offset,
4223 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004224 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004225
4226 // If the class reference currently in `temp` is not null, jump
4227 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004228 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004229 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004230 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231 break;
4232 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004234 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235 // /* HeapReference<Class> */ temp = obj->klass_
4236 GenerateReferenceLoadTwoRegisters(instruction,
4237 temp_loc,
4238 obj_loc,
4239 class_offset,
4240 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004241 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004242
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004243 // Do an exact check.
4244 __ Cmp(temp, cls);
4245 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246
4247 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004248 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004249 GenerateReferenceLoadOneRegister(instruction,
4250 temp_loc,
4251 component_offset,
4252 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004253 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004254
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004255 // If the component type is null, jump to the slow path to throw the exception.
4256 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4257 // Otherwise, the object is indeed an array. Further check that this component type is not a
4258 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004259 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4260 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004261 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 break;
4263 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004264
Calin Juravle98893e12015-10-02 21:05:03 +01004265 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004266 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004267 //
4268 // We cannot directly call the CheckCast runtime entry point
4269 // without resorting to a type checking slow path here (i.e. by
4270 // calling InvokeRuntime directly), as it would require to
4271 // assign fixed registers for the inputs of this HInstanceOf
4272 // instruction (following the runtime calling convention), which
4273 // might be cluttered by the potential first read barrier
4274 // emission at the beginning of this method.
4275 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004276 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004277 case TypeCheckKind::kInterfaceCheck: {
4278 // /* HeapReference<Class> */ temp = obj->klass_
4279 GenerateReferenceLoadTwoRegisters(instruction,
4280 temp_loc,
4281 obj_loc,
4282 class_offset,
4283 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004284 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004285
4286 // /* HeapReference<Class> */ temp = temp->iftable_
4287 GenerateReferenceLoadTwoRegisters(instruction,
4288 temp_loc,
4289 temp_loc,
4290 iftable_offset,
4291 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004292 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004293 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004295 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004296 vixl::aarch64::Label start_loop;
4297 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004298 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4300 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004301 // Go to next interface.
4302 __ Add(temp, temp, 2 * kHeapReferenceSize);
4303 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004304 // Compare the classes and continue the loop if they do not match.
4305 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4306 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004307 break;
4308 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004309 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004310 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004312 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004313}
4314
Alexandre Rames5319def2014-10-23 10:03:10 +01004315void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4316 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4317 locations->SetOut(Location::ConstantLocation(constant));
4318}
4319
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004320void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004321 // Will be generated at use site.
4322}
4323
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004324void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4325 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4326 locations->SetOut(Location::ConstantLocation(constant));
4327}
4328
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004329void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004330 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004331}
4332
Calin Juravle175dc732015-08-25 15:42:32 +01004333void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4334 // The trampoline uses the same calling convention as dex calling conventions,
4335 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4336 // the method_idx.
4337 HandleInvoke(invoke);
4338}
4339
4340void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4341 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4342}
4343
Alexandre Rames5319def2014-10-23 10:03:10 +01004344void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004345 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004346 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004347}
4348
Alexandre Rames67555f72014-11-18 10:55:16 +00004349void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4350 HandleInvoke(invoke);
4351}
4352
4353void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4354 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004355 LocationSummary* locations = invoke->GetLocations();
4356 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004357 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004358 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004359 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004360
4361 // The register ip1 is required to be used for the hidden argument in
4362 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004363 MacroAssembler* masm = GetVIXLAssembler();
4364 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004365 scratch_scope.Exclude(ip1);
4366 __ Mov(ip1, invoke->GetDexMethodIndex());
4367
Artem Serov914d7a82017-02-07 14:33:49 +00004368 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004369 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004370 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004371 {
4372 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4373 // /* HeapReference<Class> */ temp = temp->klass_
4374 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4375 codegen_->MaybeRecordImplicitNullCheck(invoke);
4376 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004378 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004379 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004380 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004381 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004382 }
Artem Serov914d7a82017-02-07 14:33:49 +00004383
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004384 // Instead of simply (possibly) unpoisoning `temp` here, we should
4385 // emit a read barrier for the previous class reference load.
4386 // However this is not required in practice, as this is an
4387 // intermediate/temporary reference and because the current
4388 // concurrent copying collector keeps the from-space memory
4389 // intact/accessible until the end of the marking phase (the
4390 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004391 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004392 __ Ldr(temp,
4393 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4394 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004395 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004396 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004397 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004398 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004399 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004400
4401 {
4402 // Ensure the pc position is recorded immediately after the `blr` instruction.
4403 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4404
4405 // lr();
4406 __ blr(lr);
4407 DCHECK(!codegen_->IsLeafMethod());
4408 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4409 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004410}
4411
4412void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004413 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004414 if (intrinsic.TryDispatch(invoke)) {
4415 return;
4416 }
4417
Alexandre Rames67555f72014-11-18 10:55:16 +00004418 HandleInvoke(invoke);
4419}
4420
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004421void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004422 // Explicit clinit checks triggered by static invokes must have been pruned by
4423 // art::PrepareForRegisterAllocation.
4424 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004425
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004426 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004427 if (intrinsic.TryDispatch(invoke)) {
4428 return;
4429 }
4430
Alexandre Rames67555f72014-11-18 10:55:16 +00004431 HandleInvoke(invoke);
4432}
4433
Andreas Gampe878d58c2015-01-15 23:24:00 -08004434static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4435 if (invoke->GetLocations()->Intrinsified()) {
4436 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4437 intrinsic.Dispatch(invoke);
4438 return true;
4439 }
4440 return false;
4441}
4442
Vladimir Markodc151b22015-10-15 18:02:30 +01004443HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4444 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004446 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004447 return desired_dispatch_info;
4448}
4449
TatWai Chongd8c052a2016-11-02 16:12:48 +08004450Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4451 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004452 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004453 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4454 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004455 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4456 uint32_t offset =
4457 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004458 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004459 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004460 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004461 }
Vladimir Marko58155012015-08-19 12:49:41 +00004462 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004463 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004464 break;
4465 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4466 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004467 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004468 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004469 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4470 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004471 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004472 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004473 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004474 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004475 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004476 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004477 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004478 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004479 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004480 }
Vladimir Marko58155012015-08-19 12:49:41 +00004481 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004482 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004483 Register reg = XRegisterFrom(temp);
4484 Register method_reg;
4485 if (current_method.IsRegister()) {
4486 method_reg = XRegisterFrom(current_method);
4487 } else {
4488 DCHECK(invoke->GetLocations()->Intrinsified());
4489 DCHECK(!current_method.IsValid());
4490 method_reg = reg;
4491 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4492 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004493
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004494 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004495 __ Ldr(reg.X(),
4496 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004497 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004498 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004499 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4500 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004501 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4502 break;
4503 }
4504 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004505 return callee_method;
4506}
4507
4508void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4509 // All registers are assumed to be correctly set up.
4510 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004511
4512 switch (invoke->GetCodePtrLocation()) {
4513 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4514 __ Bl(&frame_entry_label_);
4515 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004516 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4517 // LR = callee_method->entry_point_from_quick_compiled_code_;
4518 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004519 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004520 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004521 {
4522 // To ensure that the pc position is recorded immediately after the `blr` instruction
4523 // BLR must be the last instruction emitted in this function.
4524 // Recording the pc will occur right after returning from this function.
4525 ExactAssemblyScope eas(GetVIXLAssembler(),
4526 kInstructionSize,
4527 CodeBufferCheckScope::kExactSize);
4528 // lr()
4529 __ blr(lr);
4530 }
Vladimir Marko58155012015-08-19 12:49:41 +00004531 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004532 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004533
Andreas Gampe878d58c2015-01-15 23:24:00 -08004534 DCHECK(!IsLeafMethod());
4535}
4536
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004537void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004538 // Use the calling convention instead of the location of the receiver, as
4539 // intrinsics may have put the receiver in a different register. In the intrinsics
4540 // slow path, the arguments have been moved to the right place, so here we are
4541 // guaranteed that the receiver is the first register of the calling convention.
4542 InvokeDexCallingConvention calling_convention;
4543 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004544 Register temp = XRegisterFrom(temp_in);
4545 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4546 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4547 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004548 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004549
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004550 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004551
4552 {
4553 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4554 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4555 // /* HeapReference<Class> */ temp = receiver->klass_
4556 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4557 MaybeRecordImplicitNullCheck(invoke);
4558 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004559 // Instead of simply (possibly) unpoisoning `temp` here, we should
4560 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004561 // intermediate/temporary reference and because the current
4562 // concurrent copying collector keeps the from-space memory
4563 // intact/accessible until the end of the marking phase (the
4564 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004565 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4566 // temp = temp->GetMethodAt(method_offset);
4567 __ Ldr(temp, MemOperand(temp, method_offset));
4568 // lr = temp->GetEntryPoint();
4569 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004570 {
4571 // To ensure that the pc position is recorded immediately after the `blr` instruction
4572 // BLR should be the last instruction emitted in this function.
4573 // Recording the pc will occur right after returning from this function.
4574 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4575 // lr();
4576 __ blr(lr);
4577 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004578}
4579
Orion Hodsonac141392017-01-13 11:53:47 +00004580void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4581 HandleInvoke(invoke);
4582}
4583
4584void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4585 codegen_->GenerateInvokePolymorphicCall(invoke);
4586}
4587
Scott Wakeling97c72b72016-06-24 16:19:36 +01004588vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4589 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004590 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004591 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004592 return
4593 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004594}
4595
Scott Wakeling97c72b72016-06-24 16:19:36 +01004596vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4597 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004598 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004599 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004600 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004601}
4602
Vladimir Marko1998cd02017-01-13 13:02:58 +00004603vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4604 const DexFile& dex_file,
4605 dex::TypeIndex type_index,
4606 vixl::aarch64::Label* adrp_label) {
4607 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4608}
4609
Scott Wakeling97c72b72016-06-24 16:19:36 +01004610vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4611 const DexFile& dex_file,
4612 uint32_t element_offset,
4613 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004614 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4615}
4616
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004617vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4618 baker_read_barrier_patches_.emplace_back(custom_data);
4619 return &baker_read_barrier_patches_.back().label;
4620}
4621
Scott Wakeling97c72b72016-06-24 16:19:36 +01004622vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4623 const DexFile& dex_file,
4624 uint32_t offset_or_index,
4625 vixl::aarch64::Label* adrp_label,
4626 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627 // Add a patch entry and return the label.
4628 patches->emplace_back(dex_file, offset_or_index);
4629 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004630 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004631 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4632 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4633 return label;
4634}
4635
Scott Wakeling97c72b72016-06-24 16:19:36 +01004636vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004637 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004638 return boot_image_string_patches_.GetOrCreate(
4639 StringReference(&dex_file, string_index),
4640 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4641}
4642
Scott Wakeling97c72b72016-06-24 16:19:36 +01004643vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004644 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004645 return boot_image_type_patches_.GetOrCreate(
4646 TypeReference(&dex_file, type_index),
4647 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4648}
4649
Scott Wakeling97c72b72016-06-24 16:19:36 +01004650vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4651 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004652 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004653}
4654
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004655vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004656 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4657 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4658 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004659 return jit_string_patches_.GetOrCreate(
4660 StringReference(&dex_file, string_index),
4661 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4662}
4663
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004664vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004665 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4666 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4667 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004668 return jit_class_patches_.GetOrCreate(
4669 TypeReference(&dex_file, type_index),
4670 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4671}
4672
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4674 vixl::aarch64::Register reg) {
4675 DCHECK(reg.IsX());
4676 SingleEmissionCheckScope guard(GetVIXLAssembler());
4677 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004678 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679}
4680
4681void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4682 vixl::aarch64::Register out,
4683 vixl::aarch64::Register base) {
4684 DCHECK(out.IsX());
4685 DCHECK(base.IsX());
4686 SingleEmissionCheckScope guard(GetVIXLAssembler());
4687 __ Bind(fixup_label);
4688 __ add(out, base, Operand(/* offset placeholder */ 0));
4689}
4690
4691void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4692 vixl::aarch64::Register out,
4693 vixl::aarch64::Register base) {
4694 DCHECK(base.IsX());
4695 SingleEmissionCheckScope guard(GetVIXLAssembler());
4696 __ Bind(fixup_label);
4697 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4698}
4699
4700template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4701inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4702 const ArenaDeque<PcRelativePatchInfo>& infos,
4703 ArenaVector<LinkerPatch>* linker_patches) {
4704 for (const PcRelativePatchInfo& info : infos) {
4705 linker_patches->push_back(Factory(info.label.GetLocation(),
4706 &info.target_dex_file,
4707 info.pc_insn_label->GetLocation(),
4708 info.offset_or_index));
4709 }
4710}
4711
Vladimir Marko58155012015-08-19 12:49:41 +00004712void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4713 DCHECK(linker_patches->empty());
4714 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004715 pc_relative_dex_cache_patches_.size() +
4716 boot_image_string_patches_.size() +
4717 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004718 boot_image_type_patches_.size() +
4719 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004720 type_bss_entry_patches_.size() +
4721 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004722 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004723 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004724 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004725 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004726 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004727 info.offset_or_index));
4728 }
4729 for (const auto& entry : boot_image_string_patches_) {
4730 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004731 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4732 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004733 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004734 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004735 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004737 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004738 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4739 linker_patches);
4740 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004741 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4742 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004743 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4744 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004745 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004746 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4747 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004748 for (const auto& entry : boot_image_type_patches_) {
4749 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004750 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4751 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004752 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004753 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004754 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004755 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4756 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4757 info.custom_data));
4758 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004759 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004760}
4761
Scott Wakeling97c72b72016-06-24 16:19:36 +01004762vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004763 Uint32ToLiteralMap* map) {
4764 return map->GetOrCreate(
4765 value,
4766 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4767}
4768
Scott Wakeling97c72b72016-06-24 16:19:36 +01004769vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004770 return uint64_literals_.GetOrCreate(
4771 value,
4772 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004773}
4774
Scott Wakeling97c72b72016-06-24 16:19:36 +01004775vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004776 MethodReference target_method,
4777 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004778 return map->GetOrCreate(
4779 target_method,
4780 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004781}
4782
Andreas Gampe878d58c2015-01-15 23:24:00 -08004783void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004784 // Explicit clinit checks triggered by static invokes must have been pruned by
4785 // art::PrepareForRegisterAllocation.
4786 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004787
Andreas Gampe878d58c2015-01-15 23:24:00 -08004788 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4789 return;
4790 }
4791
Artem Serov914d7a82017-02-07 14:33:49 +00004792 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4793 // are no pools emitted.
4794 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004795 LocationSummary* locations = invoke->GetLocations();
4796 codegen_->GenerateStaticOrDirectCall(
4797 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004798 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004799}
4800
4801void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004802 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4803 return;
4804 }
4805
Artem Serov914d7a82017-02-07 14:33:49 +00004806 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4807 // are no pools emitted.
4808 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004809 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004810 DCHECK(!codegen_->IsLeafMethod());
4811 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4812}
4813
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004814HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4815 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004816 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004817 case HLoadClass::LoadKind::kInvalid:
4818 LOG(FATAL) << "UNREACHABLE";
4819 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004820 case HLoadClass::LoadKind::kReferrersClass:
4821 break;
4822 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4823 DCHECK(!GetCompilerOptions().GetCompilePic());
4824 break;
4825 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4826 DCHECK(GetCompilerOptions().GetCompilePic());
4827 break;
4828 case HLoadClass::LoadKind::kBootImageAddress:
4829 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004830 case HLoadClass::LoadKind::kBssEntry:
4831 DCHECK(!Runtime::Current()->UseJitCompilation());
4832 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004833 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004834 DCHECK(Runtime::Current()->UseJitCompilation());
4835 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004836 case HLoadClass::LoadKind::kDexCacheViaMethod:
4837 break;
4838 }
4839 return desired_class_load_kind;
4840}
4841
Alexandre Rames67555f72014-11-18 10:55:16 +00004842void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004843 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4844 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004846 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004847 cls,
4848 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004849 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004850 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004851 return;
4852 }
Vladimir Marko41559982017-01-06 14:04:23 +00004853 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004854
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004855 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4856 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004857 ? LocationSummary::kCallOnSlowPath
4858 : LocationSummary::kNoCall;
4859 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004860 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004861 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004862 }
4863
Vladimir Marko41559982017-01-06 14:04:23 +00004864 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 locations->SetInAt(0, Location::RequiresRegister());
4866 }
4867 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004868 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4869 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4870 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004871 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004872 RegisterSet caller_saves = RegisterSet::Empty();
4873 InvokeRuntimeCallingConvention calling_convention;
4874 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4875 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4876 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4877 Primitive::kPrimNot).GetCode());
4878 locations->SetCustomSlowPathCallerSaves(caller_saves);
4879 } else {
4880 // For non-Baker read barrier we have a temp-clobbering call.
4881 }
4882 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004883}
4884
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004885// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4886// move.
4887void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004888 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4889 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4890 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004891 return;
4892 }
Vladimir Marko41559982017-01-06 14:04:23 +00004893 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004894
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004895 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004896 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004897 Register bss_entry_temp;
4898 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004899
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004900 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4901 ? kWithoutReadBarrier
4902 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004903 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004904 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004905 case HLoadClass::LoadKind::kReferrersClass: {
4906 DCHECK(!cls->CanCallRuntime());
4907 DCHECK(!cls->MustGenerateClinitCheck());
4908 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4909 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004910 GenerateGcRootFieldLoad(cls,
4911 out_loc,
4912 current_method,
4913 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004914 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004915 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004916 break;
4917 }
4918 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004919 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004920 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4921 cls->GetTypeIndex()));
4922 break;
4923 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004924 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004925 // Add ADRP with its PC-relative type patch.
4926 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004927 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004928 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004929 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004930 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004931 vixl::aarch64::Label* add_label =
4932 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004933 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004934 break;
4935 }
4936 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004937 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004938 uint32_t address = dchecked_integral_cast<uint32_t>(
4939 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4940 DCHECK_NE(address, 0u);
4941 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004942 break;
4943 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004944 case HLoadClass::LoadKind::kBssEntry: {
4945 // Add ADRP with its PC-relative Class .bss entry patch.
4946 const DexFile& dex_file = cls->GetDexFile();
4947 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004948 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004949 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4950 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004951 // Add LDR with its PC-relative Class patch.
4952 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004953 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004954 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4955 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004956 out_loc,
4957 bss_entry_temp,
4958 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004959 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004960 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004961 generate_null_check = true;
4962 break;
4963 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004964 case HLoadClass::LoadKind::kJitTableAddress: {
4965 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4966 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004967 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004968 GenerateGcRootFieldLoad(cls,
4969 out_loc,
4970 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004971 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004972 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004973 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004974 break;
4975 }
Vladimir Marko41559982017-01-06 14:04:23 +00004976 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004977 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004978 LOG(FATAL) << "UNREACHABLE";
4979 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004980 }
4981
Vladimir Markoea4c1262017-02-06 19:59:33 +00004982 bool do_clinit = cls->MustGenerateClinitCheck();
4983 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004984 DCHECK(cls->CanCallRuntime());
4985 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004986 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004987 codegen_->AddSlowPath(slow_path);
4988 if (generate_null_check) {
4989 __ Cbz(out, slow_path->GetEntryLabel());
4990 }
4991 if (cls->MustGenerateClinitCheck()) {
4992 GenerateClassInitializationCheck(slow_path, out);
4993 } else {
4994 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004995 }
4996 }
4997}
4998
David Brazdilcb1c0552015-08-04 16:22:25 +01004999static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005000 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005001}
5002
Alexandre Rames67555f72014-11-18 10:55:16 +00005003void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5004 LocationSummary* locations =
5005 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5006 locations->SetOut(Location::RequiresRegister());
5007}
5008
5009void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005010 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5011}
5012
5013void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5014 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5015}
5016
5017void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5018 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005019}
5020
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005021HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5022 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005023 switch (desired_string_load_kind) {
5024 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5025 DCHECK(!GetCompilerOptions().GetCompilePic());
5026 break;
5027 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5028 DCHECK(GetCompilerOptions().GetCompilePic());
5029 break;
5030 case HLoadString::LoadKind::kBootImageAddress:
5031 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005032 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005033 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005034 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005035 case HLoadString::LoadKind::kJitTableAddress:
5036 DCHECK(Runtime::Current()->UseJitCompilation());
5037 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005038 case HLoadString::LoadKind::kDexCacheViaMethod:
5039 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 }
5041 return desired_string_load_kind;
5042}
5043
Alexandre Rames67555f72014-11-18 10:55:16 +00005044void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005045 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005046 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005047 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005048 InvokeRuntimeCallingConvention calling_convention;
5049 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5050 } else {
5051 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005052 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5053 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005054 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005055 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005056 RegisterSet caller_saves = RegisterSet::Empty();
5057 InvokeRuntimeCallingConvention calling_convention;
5058 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5059 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5060 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5061 Primitive::kPrimNot).GetCode());
5062 locations->SetCustomSlowPathCallerSaves(caller_saves);
5063 } else {
5064 // For non-Baker read barrier we have a temp-clobbering call.
5065 }
5066 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005067 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005068}
5069
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005070// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5071// move.
5072void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005073 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005074 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005075
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 switch (load->GetLoadKind()) {
5077 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005078 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5079 load->GetStringIndex()));
5080 return; // No dex cache slow path.
5081 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005082 // Add ADRP with its PC-relative String patch.
5083 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005084 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005085 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005086 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005087 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005089 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005091 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 return; // No dex cache slow path.
5093 }
5094 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005095 uint32_t address = dchecked_integral_cast<uint32_t>(
5096 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5097 DCHECK_NE(address, 0u);
5098 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 return; // No dex cache slow path.
5100 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005101 case HLoadString::LoadKind::kBssEntry: {
5102 // Add ADRP with its PC-relative String .bss entry patch.
5103 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005104 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005105 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005106 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005108 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005109 // Add LDR with its PC-relative String patch.
5110 vixl::aarch64::Label* ldr_label =
5111 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005112 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005114 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005115 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005116 /* offset placeholder */ 0u,
5117 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005118 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005119 SlowPathCodeARM64* slow_path =
5120 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005121 codegen_->AddSlowPath(slow_path);
5122 __ Cbz(out.X(), slow_path->GetEntryLabel());
5123 __ Bind(slow_path->GetExitLabel());
5124 return;
5125 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005126 case HLoadString::LoadKind::kJitTableAddress: {
5127 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005128 load->GetStringIndex(),
5129 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005130 GenerateGcRootFieldLoad(load,
5131 out_loc,
5132 out.X(),
5133 /* offset */ 0,
5134 /* fixup_label */ nullptr,
5135 kCompilerReadBarrierOption);
5136 return;
5137 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005138 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005139 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005140 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005141
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005142 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005143 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005144 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005145 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005146 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5147 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005148}
5149
Alexandre Rames5319def2014-10-23 10:03:10 +01005150void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5151 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5152 locations->SetOut(Location::ConstantLocation(constant));
5153}
5154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005155void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005156 // Will be generated at use site.
5157}
5158
Alexandre Rames67555f72014-11-18 10:55:16 +00005159void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5160 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005162 InvokeRuntimeCallingConvention calling_convention;
5163 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5164}
5165
5166void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005167 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005168 instruction,
5169 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005170 if (instruction->IsEnter()) {
5171 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5172 } else {
5173 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5174 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005175}
5176
Alexandre Rames42d641b2014-10-27 14:00:51 +00005177void LocationsBuilderARM64::VisitMul(HMul* mul) {
5178 LocationSummary* locations =
5179 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5180 switch (mul->GetResultType()) {
5181 case Primitive::kPrimInt:
5182 case Primitive::kPrimLong:
5183 locations->SetInAt(0, Location::RequiresRegister());
5184 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005185 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005186 break;
5187
5188 case Primitive::kPrimFloat:
5189 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005190 locations->SetInAt(0, Location::RequiresFpuRegister());
5191 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005192 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005193 break;
5194
5195 default:
5196 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5197 }
5198}
5199
5200void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5201 switch (mul->GetResultType()) {
5202 case Primitive::kPrimInt:
5203 case Primitive::kPrimLong:
5204 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5205 break;
5206
5207 case Primitive::kPrimFloat:
5208 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005209 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005210 break;
5211
5212 default:
5213 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5214 }
5215}
5216
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005217void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5218 LocationSummary* locations =
5219 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5220 switch (neg->GetResultType()) {
5221 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005222 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005223 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005225 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005226
5227 case Primitive::kPrimFloat:
5228 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005229 locations->SetInAt(0, Location::RequiresFpuRegister());
5230 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005231 break;
5232
5233 default:
5234 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5235 }
5236}
5237
5238void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5239 switch (neg->GetResultType()) {
5240 case Primitive::kPrimInt:
5241 case Primitive::kPrimLong:
5242 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5243 break;
5244
5245 case Primitive::kPrimFloat:
5246 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005247 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005248 break;
5249
5250 default:
5251 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5252 }
5253}
5254
5255void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5256 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005257 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005258 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005259 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005260 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5261 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005262}
5263
5264void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005265 // Note: if heap poisoning is enabled, the entry point takes cares
5266 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005267 QuickEntrypointEnum entrypoint =
5268 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5269 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005270 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005271}
5272
Alexandre Rames5319def2014-10-23 10:03:10 +01005273void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5274 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005275 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005276 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005277 if (instruction->IsStringAlloc()) {
5278 locations->AddTemp(LocationFrom(kArtMethodRegister));
5279 } else {
5280 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005281 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005282 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5283}
5284
5285void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005286 // Note: if heap poisoning is enabled, the entry point takes cares
5287 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005288 if (instruction->IsStringAlloc()) {
5289 // String is allocated through StringFactory. Call NewEmptyString entry point.
5290 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005291 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005292 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5293 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005294
5295 {
5296 // Ensure the pc position is recorded immediately after the `blr` instruction.
5297 ExactAssemblyScope eas(GetVIXLAssembler(),
5298 kInstructionSize,
5299 CodeBufferCheckScope::kExactSize);
5300 __ blr(lr);
5301 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5302 }
David Brazdil6de19382016-01-08 17:37:10 +00005303 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005304 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005305 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005306 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005307}
5308
5309void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005311 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005313}
5314
5315void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005316 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005317 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005318 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005319 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005320 break;
5321
5322 default:
5323 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5324 }
5325}
5326
David Brazdil66d126e2015-04-03 16:02:44 +01005327void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5329 locations->SetInAt(0, Location::RequiresRegister());
5330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5331}
5332
5333void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005334 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005335}
5336
Alexandre Rames5319def2014-10-23 10:03:10 +01005337void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005338 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5339 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005340}
5341
Calin Juravle2ae48182016-03-16 14:05:09 +00005342void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5343 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005344 return;
5345 }
Artem Serov914d7a82017-02-07 14:33:49 +00005346 {
5347 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5348 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5349 Location obj = instruction->GetLocations()->InAt(0);
5350 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5351 RecordPcInfo(instruction, instruction->GetDexPc());
5352 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005353}
5354
Calin Juravle2ae48182016-03-16 14:05:09 +00005355void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005356 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005357 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005358
5359 LocationSummary* locations = instruction->GetLocations();
5360 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005361
5362 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005363}
5364
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005365void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005366 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005367}
5368
Alexandre Rames67555f72014-11-18 10:55:16 +00005369void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5370 HandleBinaryOp(instruction);
5371}
5372
5373void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5374 HandleBinaryOp(instruction);
5375}
5376
Alexandre Rames3e69f162014-12-10 10:36:50 +00005377void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5378 LOG(FATAL) << "Unreachable";
5379}
5380
5381void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5382 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5383}
5384
Alexandre Rames5319def2014-10-23 10:03:10 +01005385void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5386 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5387 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5388 if (location.IsStackSlot()) {
5389 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5390 } else if (location.IsDoubleStackSlot()) {
5391 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5392 }
5393 locations->SetOut(location);
5394}
5395
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005396void InstructionCodeGeneratorARM64::VisitParameterValue(
5397 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005398 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005399}
5400
5401void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5402 LocationSummary* locations =
5403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005404 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005405}
5406
5407void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5408 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5409 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005410}
5411
5412void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5413 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005414 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005415 locations->SetInAt(i, Location::Any());
5416 }
5417 locations->SetOut(Location::Any());
5418}
5419
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005420void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005421 LOG(FATAL) << "Unreachable";
5422}
5423
Serban Constantinescu02164b32014-11-13 14:05:07 +00005424void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005425 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005426 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005427 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5428 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005429 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5430
5431 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005432 case Primitive::kPrimInt:
5433 case Primitive::kPrimLong:
5434 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005435 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005436 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5437 break;
5438
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005439 case Primitive::kPrimFloat:
5440 case Primitive::kPrimDouble: {
5441 InvokeRuntimeCallingConvention calling_convention;
5442 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5443 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5444 locations->SetOut(calling_convention.GetReturnLocation(type));
5445
5446 break;
5447 }
5448
Serban Constantinescu02164b32014-11-13 14:05:07 +00005449 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005450 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005451 }
5452}
5453
5454void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5455 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005456
Serban Constantinescu02164b32014-11-13 14:05:07 +00005457 switch (type) {
5458 case Primitive::kPrimInt:
5459 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005460 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005461 break;
5462 }
5463
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005464 case Primitive::kPrimFloat:
5465 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005466 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5467 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005468 if (type == Primitive::kPrimFloat) {
5469 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5470 } else {
5471 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5472 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005473 break;
5474 }
5475
Serban Constantinescu02164b32014-11-13 14:05:07 +00005476 default:
5477 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005478 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005479 }
5480}
5481
Igor Murashkind01745e2017-04-05 16:40:31 -07005482void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5483 constructor_fence->SetLocations(nullptr);
5484}
5485
5486void InstructionCodeGeneratorARM64::VisitConstructorFence(
5487 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5488 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5489}
5490
Calin Juravle27df7582015-04-17 19:12:31 +01005491void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5492 memory_barrier->SetLocations(nullptr);
5493}
5494
5495void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005496 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005497}
5498
Alexandre Rames5319def2014-10-23 10:03:10 +01005499void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5500 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5501 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005502 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005503}
5504
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005505void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005506 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005507}
5508
5509void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5510 instruction->SetLocations(nullptr);
5511}
5512
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005513void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005514 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005515}
5516
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005517void LocationsBuilderARM64::VisitRor(HRor* ror) {
5518 HandleBinaryOp(ror);
5519}
5520
5521void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5522 HandleBinaryOp(ror);
5523}
5524
Serban Constantinescu02164b32014-11-13 14:05:07 +00005525void LocationsBuilderARM64::VisitShl(HShl* shl) {
5526 HandleShift(shl);
5527}
5528
5529void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5530 HandleShift(shl);
5531}
5532
5533void LocationsBuilderARM64::VisitShr(HShr* shr) {
5534 HandleShift(shr);
5535}
5536
5537void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5538 HandleShift(shr);
5539}
5540
Alexandre Rames5319def2014-10-23 10:03:10 +01005541void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005542 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005543}
5544
5545void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005546 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005547}
5548
Alexandre Rames67555f72014-11-18 10:55:16 +00005549void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005550 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005551}
5552
5553void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005554 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005555}
5556
5557void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005558 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005559}
5560
Alexandre Rames67555f72014-11-18 10:55:16 +00005561void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005562 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005563}
5564
Calin Juravlee460d1d2015-09-29 04:52:17 +01005565void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5566 HUnresolvedInstanceFieldGet* instruction) {
5567 FieldAccessCallingConventionARM64 calling_convention;
5568 codegen_->CreateUnresolvedFieldLocationSummary(
5569 instruction, instruction->GetFieldType(), calling_convention);
5570}
5571
5572void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5573 HUnresolvedInstanceFieldGet* instruction) {
5574 FieldAccessCallingConventionARM64 calling_convention;
5575 codegen_->GenerateUnresolvedFieldAccess(instruction,
5576 instruction->GetFieldType(),
5577 instruction->GetFieldIndex(),
5578 instruction->GetDexPc(),
5579 calling_convention);
5580}
5581
5582void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5583 HUnresolvedInstanceFieldSet* instruction) {
5584 FieldAccessCallingConventionARM64 calling_convention;
5585 codegen_->CreateUnresolvedFieldLocationSummary(
5586 instruction, instruction->GetFieldType(), calling_convention);
5587}
5588
5589void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5590 HUnresolvedInstanceFieldSet* instruction) {
5591 FieldAccessCallingConventionARM64 calling_convention;
5592 codegen_->GenerateUnresolvedFieldAccess(instruction,
5593 instruction->GetFieldType(),
5594 instruction->GetFieldIndex(),
5595 instruction->GetDexPc(),
5596 calling_convention);
5597}
5598
5599void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5600 HUnresolvedStaticFieldGet* instruction) {
5601 FieldAccessCallingConventionARM64 calling_convention;
5602 codegen_->CreateUnresolvedFieldLocationSummary(
5603 instruction, instruction->GetFieldType(), calling_convention);
5604}
5605
5606void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5607 HUnresolvedStaticFieldGet* instruction) {
5608 FieldAccessCallingConventionARM64 calling_convention;
5609 codegen_->GenerateUnresolvedFieldAccess(instruction,
5610 instruction->GetFieldType(),
5611 instruction->GetFieldIndex(),
5612 instruction->GetDexPc(),
5613 calling_convention);
5614}
5615
5616void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5617 HUnresolvedStaticFieldSet* instruction) {
5618 FieldAccessCallingConventionARM64 calling_convention;
5619 codegen_->CreateUnresolvedFieldLocationSummary(
5620 instruction, instruction->GetFieldType(), calling_convention);
5621}
5622
5623void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5624 HUnresolvedStaticFieldSet* instruction) {
5625 FieldAccessCallingConventionARM64 calling_convention;
5626 codegen_->GenerateUnresolvedFieldAccess(instruction,
5627 instruction->GetFieldType(),
5628 instruction->GetFieldIndex(),
5629 instruction->GetDexPc(),
5630 calling_convention);
5631}
5632
Alexandre Rames5319def2014-10-23 10:03:10 +01005633void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005634 LocationSummary* locations =
5635 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005636 // In suspend check slow path, usually there are no caller-save registers at all.
5637 // If SIMD instructions are present, however, we force spilling all live SIMD
5638 // registers in full width (since the runtime only saves/restores lower part).
5639 locations->SetCustomSlowPathCallerSaves(
5640 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005641}
5642
5643void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005644 HBasicBlock* block = instruction->GetBlock();
5645 if (block->GetLoopInformation() != nullptr) {
5646 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5647 // The back edge will generate the suspend check.
5648 return;
5649 }
5650 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5651 // The goto will generate the suspend check.
5652 return;
5653 }
5654 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005655}
5656
Alexandre Rames67555f72014-11-18 10:55:16 +00005657void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5658 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005660 InvokeRuntimeCallingConvention calling_convention;
5661 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5662}
5663
5664void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005665 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005666 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005667}
5668
5669void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5670 LocationSummary* locations =
5671 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5672 Primitive::Type input_type = conversion->GetInputType();
5673 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005674 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005675 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5676 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5677 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5678 }
5679
Alexandre Rames542361f2015-01-29 16:57:31 +00005680 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005681 locations->SetInAt(0, Location::RequiresFpuRegister());
5682 } else {
5683 locations->SetInAt(0, Location::RequiresRegister());
5684 }
5685
Alexandre Rames542361f2015-01-29 16:57:31 +00005686 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005687 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5688 } else {
5689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5690 }
5691}
5692
5693void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5694 Primitive::Type result_type = conversion->GetResultType();
5695 Primitive::Type input_type = conversion->GetInputType();
5696
5697 DCHECK_NE(input_type, result_type);
5698
Alexandre Rames542361f2015-01-29 16:57:31 +00005699 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005700 int result_size = Primitive::ComponentSize(result_type);
5701 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005702 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005703 Register output = OutputRegister(conversion);
5704 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005705 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005706 // 'int' values are used directly as W registers, discarding the top
5707 // bits, so we don't need to sign-extend and can just perform a move.
5708 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5709 // top 32 bits of the target register. We theoretically could leave those
5710 // bits unchanged, but we would have to make sure that no code uses a
5711 // 32bit input value as a 64bit value assuming that the top 32 bits are
5712 // zero.
5713 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005714 } else if (result_type == Primitive::kPrimChar ||
5715 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5716 __ Ubfx(output,
5717 output.IsX() ? source.X() : source.W(),
5718 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005719 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005720 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005721 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005722 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005723 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005724 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005725 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5726 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005727 } else if (Primitive::IsFloatingPointType(result_type) &&
5728 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005729 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5730 } else {
5731 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5732 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005733 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005734}
Alexandre Rames67555f72014-11-18 10:55:16 +00005735
Serban Constantinescu02164b32014-11-13 14:05:07 +00005736void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5737 HandleShift(ushr);
5738}
5739
5740void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5741 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005742}
5743
5744void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5745 HandleBinaryOp(instruction);
5746}
5747
5748void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5749 HandleBinaryOp(instruction);
5750}
5751
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005752void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005753 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005754 LOG(FATAL) << "Unreachable";
5755}
5756
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005757void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005758 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005759 LOG(FATAL) << "Unreachable";
5760}
5761
Mark Mendellfe57faa2015-09-18 09:26:15 -04005762// Simple implementation of packed switch - generate cascaded compare/jumps.
5763void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5764 LocationSummary* locations =
5765 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5766 locations->SetInAt(0, Location::RequiresRegister());
5767}
5768
5769void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5770 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005771 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005772 Register value_reg = InputRegisterAt(switch_instr, 0);
5773 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5774
Zheng Xu3927c8b2015-11-18 17:46:25 +08005775 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005776 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005777 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5778 // make sure we don't emit it if the target may run out of range.
5779 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5780 // ranges and emit the tables only as required.
5781 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005782
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005783 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005784 // Current instruction id is an upper bound of the number of HIRs in the graph.
5785 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5786 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005787 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5788 Register temp = temps.AcquireW();
5789 __ Subs(temp, value_reg, Operand(lower_bound));
5790
Zheng Xu3927c8b2015-11-18 17:46:25 +08005791 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005792 // Jump to successors[0] if value == lower_bound.
5793 __ B(eq, codegen_->GetLabelOf(successors[0]));
5794 int32_t last_index = 0;
5795 for (; num_entries - last_index > 2; last_index += 2) {
5796 __ Subs(temp, temp, Operand(2));
5797 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5798 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5799 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5800 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5801 }
5802 if (num_entries - last_index == 2) {
5803 // The last missing case_value.
5804 __ Cmp(temp, Operand(1));
5805 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005806 }
5807
5808 // And the default for any other value.
5809 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5810 __ B(codegen_->GetLabelOf(default_block));
5811 }
5812 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005813 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005814
5815 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5816
5817 // Below instructions should use at most one blocked register. Since there are two blocked
5818 // registers, we are free to block one.
5819 Register temp_w = temps.AcquireW();
5820 Register index;
5821 // Remove the bias.
5822 if (lower_bound != 0) {
5823 index = temp_w;
5824 __ Sub(index, value_reg, Operand(lower_bound));
5825 } else {
5826 index = value_reg;
5827 }
5828
5829 // Jump to default block if index is out of the range.
5830 __ Cmp(index, Operand(num_entries));
5831 __ B(hs, codegen_->GetLabelOf(default_block));
5832
5833 // In current VIXL implementation, it won't require any blocked registers to encode the
5834 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5835 // register pressure.
5836 Register table_base = temps.AcquireX();
5837 // Load jump offset from the table.
5838 __ Adr(table_base, jump_table->GetTableStartLabel());
5839 Register jump_offset = temp_w;
5840 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5841
5842 // Jump to target block by branching to table_base(pc related) + offset.
5843 Register target_address = table_base;
5844 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5845 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005846 }
5847}
5848
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005849void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5850 HInstruction* instruction,
5851 Location out,
5852 uint32_t offset,
5853 Location maybe_temp,
5854 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005855 Primitive::Type type = Primitive::kPrimNot;
5856 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005857 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005858 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005859 if (kUseBakerReadBarrier) {
5860 // Load with fast path based Baker's read barrier.
5861 // /* HeapReference<Object> */ out = *(out + offset)
5862 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5863 out,
5864 out_reg,
5865 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005866 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005867 /* needs_null_check */ false,
5868 /* use_load_acquire */ false);
5869 } else {
5870 // Load with slow path based read barrier.
5871 // Save the value of `out` into `maybe_temp` before overwriting it
5872 // in the following move operation, as we will need it for the
5873 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005874 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005875 __ Mov(temp_reg, out_reg);
5876 // /* HeapReference<Object> */ out = *(out + offset)
5877 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5878 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5879 }
5880 } else {
5881 // Plain load with no read barrier.
5882 // /* HeapReference<Object> */ out = *(out + offset)
5883 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5884 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5885 }
5886}
5887
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005888void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5889 HInstruction* instruction,
5890 Location out,
5891 Location obj,
5892 uint32_t offset,
5893 Location maybe_temp,
5894 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005895 Primitive::Type type = Primitive::kPrimNot;
5896 Register out_reg = RegisterFrom(out, type);
5897 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005898 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005899 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005900 if (kUseBakerReadBarrier) {
5901 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005902 // /* HeapReference<Object> */ out = *(obj + offset)
5903 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5904 out,
5905 obj_reg,
5906 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005907 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005908 /* needs_null_check */ false,
5909 /* use_load_acquire */ false);
5910 } else {
5911 // Load with slow path based read barrier.
5912 // /* HeapReference<Object> */ out = *(obj + offset)
5913 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5914 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5915 }
5916 } else {
5917 // Plain load with no read barrier.
5918 // /* HeapReference<Object> */ out = *(obj + offset)
5919 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5920 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5921 }
5922}
5923
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005924void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5925 HInstruction* instruction,
5926 Location root,
5927 Register obj,
5928 uint32_t offset,
5929 vixl::aarch64::Label* fixup_label,
5930 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005931 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005932 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005933 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005934 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005935 if (kUseBakerReadBarrier) {
5936 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005937 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005938 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5939 !Runtime::Current()->UseJitCompilation()) {
5940 // Note that we do not actually check the value of `GetIsGcMarking()`
5941 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005942 // load into `temp` (actually IP1) the read barrier mark introspection
5943 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5944 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005945 //
5946 // We use link-time generated thunks for the slow path. That thunk
5947 // checks the reference and jumps to the entrypoint if needed.
5948 //
5949 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5950 // lr = &return_address;
5951 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5952 // if (temp != nullptr) {
5953 // goto gc_root_thunk<root_reg>(lr)
5954 // }
5955 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005956
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005957 UseScratchRegisterScope temps(GetVIXLAssembler());
5958 DCHECK(temps.IsAvailable(ip0));
5959 DCHECK(temps.IsAvailable(ip1));
5960 temps.Exclude(ip0, ip1);
5961 uint32_t custom_data =
5962 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5963 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005964
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005965 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5966 DCHECK_EQ(ip0.GetCode(), 16u);
5967 const int32_t entry_point_offset =
5968 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5969 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5970 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5971 vixl::aarch64::Label return_address;
5972 __ adr(lr, &return_address);
5973 if (fixup_label != nullptr) {
5974 __ Bind(fixup_label);
5975 }
5976 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5977 "GC root LDR must be 2 instruction (8B) before the return address label.");
5978 __ ldr(root_reg, MemOperand(obj.X(), offset));
5979 __ Bind(cbnz_label);
5980 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5981 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005983 // Note that we do not actually check the value of
5984 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5985 // root or not. Instead, we load into `temp` the read barrier
5986 // mark entry point corresponding to register `root`. If `temp`
5987 // is null, it means that `GetIsGcMarking()` is false, and vice
5988 // versa.
5989 //
5990 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5991 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5992 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5993 // // Slow path.
5994 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5995 // }
Roland Levillain44015862016-01-22 11:47:17 +00005996
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005997 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5998 Register temp = lr;
5999 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6000 instruction, root, /* entrypoint */ LocationFrom(temp));
6001 codegen_->AddSlowPath(slow_path);
6002
6003 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6004 const int32_t entry_point_offset =
6005 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6006 // Loading the entrypoint does not require a load acquire since it is only changed when
6007 // threads are suspended or running a checkpoint.
6008 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6009
6010 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6011 if (fixup_label == nullptr) {
6012 __ Ldr(root_reg, MemOperand(obj, offset));
6013 } else {
6014 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6015 }
6016 static_assert(
6017 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6018 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6019 "have different sizes.");
6020 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6021 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6022 "have different sizes.");
6023
6024 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6025 // checking GetIsGcMarking.
6026 __ Cbnz(temp, slow_path->GetEntryLabel());
6027 __ Bind(slow_path->GetExitLabel());
6028 }
Roland Levillain44015862016-01-22 11:47:17 +00006029 } else {
6030 // GC root loaded through a slow path for read barriers other
6031 // than Baker's.
6032 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006033 if (fixup_label == nullptr) {
6034 __ Add(root_reg.X(), obj.X(), offset);
6035 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006036 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006037 }
Roland Levillain44015862016-01-22 11:47:17 +00006038 // /* mirror::Object* */ root = root->Read()
6039 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6040 }
6041 } else {
6042 // Plain GC root load with no read barrier.
6043 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006044 if (fixup_label == nullptr) {
6045 __ Ldr(root_reg, MemOperand(obj, offset));
6046 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006047 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006048 }
Roland Levillain44015862016-01-22 11:47:17 +00006049 // Note that GC roots are not affected by heap poisoning, thus we
6050 // do not have to unpoison `root_reg` here.
6051 }
6052}
6053
6054void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6055 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006056 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006057 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006058 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006059 bool needs_null_check,
6060 bool use_load_acquire) {
6061 DCHECK(kEmitCompilerReadBarrier);
6062 DCHECK(kUseBakerReadBarrier);
6063
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006064 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6065 !use_load_acquire &&
6066 !Runtime::Current()->UseJitCompilation()) {
6067 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006068 // to decide whether to mark the loaded reference or not. Instead, we
6069 // load into `temp` (actually IP1) the read barrier mark introspection
6070 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6071 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006072 //
6073 // We use link-time generated thunks for the slow path. That thunk checks
6074 // the holder and jumps to the entrypoint if needed. If the holder is not
6075 // gray, it creates a fake dependency and returns to the LDR instruction.
6076 //
6077 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006078 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006079 // if (temp != nullptr) {
6080 // goto field_thunk<holder_reg, base_reg>(lr)
6081 // }
6082 // not_gray_return_address:
6083 // // Original reference load. If the offset is too large to fit
6084 // // into LDR, we use an adjusted base register here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006085 // GcRoot<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006086 // gray_return_address:
6087
6088 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6089 Register base = obj;
6090 if (offset >= kReferenceLoadMinFarOffset) {
6091 DCHECK(maybe_temp.IsRegister());
6092 base = WRegisterFrom(maybe_temp);
6093 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6094 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6095 offset &= (kReferenceLoadMinFarOffset - 1u);
6096 }
6097 UseScratchRegisterScope temps(GetVIXLAssembler());
6098 DCHECK(temps.IsAvailable(ip0));
6099 DCHECK(temps.IsAvailable(ip1));
6100 temps.Exclude(ip0, ip1);
6101 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6102 base.GetCode(),
6103 obj.GetCode());
6104 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6105
6106 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6107 DCHECK_EQ(ip0.GetCode(), 16u);
6108 const int32_t entry_point_offset =
6109 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6110 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006111 EmissionCheckScope guard(GetVIXLAssembler(),
6112 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006113 vixl::aarch64::Label return_address;
6114 __ adr(lr, &return_address);
6115 __ Bind(cbnz_label);
6116 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006117 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6118 "Field LDR must be 1 instruction (4B) before the return address label; "
6119 " 2 instructions (8B) for heap poisoning.");
6120 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6121 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006122 if (needs_null_check) {
6123 MaybeRecordImplicitNullCheck(instruction);
6124 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006125 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006126 __ Bind(&return_address);
6127 return;
6128 }
6129
Roland Levillain44015862016-01-22 11:47:17 +00006130 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006131 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006132 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006133 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006134 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6135 ref,
6136 obj,
6137 offset,
6138 no_index,
6139 no_scale_factor,
6140 temp,
6141 needs_null_check,
6142 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006143}
6144
6145void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6146 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006147 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006148 uint32_t data_offset,
6149 Location index,
6150 Register temp,
6151 bool needs_null_check) {
6152 DCHECK(kEmitCompilerReadBarrier);
6153 DCHECK(kUseBakerReadBarrier);
6154
Vladimir Marko66d691d2017-04-07 17:53:39 +01006155 static_assert(
6156 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6157 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6158 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6159
6160 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6161 !Runtime::Current()->UseJitCompilation()) {
6162 // Note that we do not actually check the value of `GetIsGcMarking()`
6163 // to decide whether to mark the loaded reference or not. Instead, we
6164 // load into `temp` (actually IP1) the read barrier mark introspection
6165 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6166 // false, and vice versa.
6167 //
6168 // We use link-time generated thunks for the slow path. That thunk checks
6169 // the holder and jumps to the entrypoint if needed. If the holder is not
6170 // gray, it creates a fake dependency and returns to the LDR instruction.
6171 //
6172 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6173 // lr = &gray_return_address;
6174 // if (temp != nullptr) {
6175 // goto field_thunk<holder_reg, base_reg>(lr)
6176 // }
6177 // not_gray_return_address:
6178 // // Original reference load. If the offset is too large to fit
6179 // // into LDR, we use an adjusted base register here.
6180 // GcRoot<mirror::Object> reference = data[index];
6181 // gray_return_address:
6182
6183 DCHECK(index.IsValid());
6184 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6185 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6186
6187 UseScratchRegisterScope temps(GetVIXLAssembler());
6188 DCHECK(temps.IsAvailable(ip0));
6189 DCHECK(temps.IsAvailable(ip1));
6190 temps.Exclude(ip0, ip1);
6191 uint32_t custom_data =
6192 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6193 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6194
6195 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6196 DCHECK_EQ(ip0.GetCode(), 16u);
6197 const int32_t entry_point_offset =
6198 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6199 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6200 __ Add(temp.X(), obj.X(), Operand(data_offset));
6201 EmissionCheckScope guard(GetVIXLAssembler(),
6202 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6203 vixl::aarch64::Label return_address;
6204 __ adr(lr, &return_address);
6205 __ Bind(cbnz_label);
6206 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6207 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6208 "Array LDR must be 1 instruction (4B) before the return address label; "
6209 " 2 instructions (8B) for heap poisoning.");
6210 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6211 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6212 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6213 __ Bind(&return_address);
6214 return;
6215 }
6216
Roland Levillain44015862016-01-22 11:47:17 +00006217 // Array cells are never volatile variables, therefore array loads
6218 // never use Load-Acquire instructions on ARM64.
6219 const bool use_load_acquire = false;
6220
6221 // /* HeapReference<Object> */ ref =
6222 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006223 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6224 ref,
6225 obj,
6226 data_offset,
6227 index,
6228 scale_factor,
6229 temp,
6230 needs_null_check,
6231 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006232}
6233
6234void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6235 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006236 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006237 uint32_t offset,
6238 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006239 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006240 Register temp,
6241 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006242 bool use_load_acquire,
6243 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006244 DCHECK(kEmitCompilerReadBarrier);
6245 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006246 // If we are emitting an array load, we should not be using a
6247 // Load Acquire instruction. In other words:
6248 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6249 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006250
Roland Levillain54f869e2017-03-06 13:54:11 +00006251 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6252 // whether we need to enter the slow path to mark the reference.
6253 // Then, in the slow path, check the gray bit in the lock word of
6254 // the reference's holder (`obj`) to decide whether to mark `ref` or
6255 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006256 //
Roland Levillainba650a42017-03-06 13:52:32 +00006257 // Note that we do not actually check the value of `GetIsGcMarking()`;
6258 // instead, we load into `temp2` the read barrier mark entry point
6259 // corresponding to register `ref`. If `temp2` is null, it means
6260 // that `GetIsGcMarking()` is false, and vice versa.
6261 //
6262 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006263 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6264 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006265 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6266 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6267 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6268 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6269 // if (is_gray) {
6270 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6271 // }
6272 // } else {
6273 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006274 // }
Roland Levillain44015862016-01-22 11:47:17 +00006275
Roland Levillainba650a42017-03-06 13:52:32 +00006276 // Slow path marking the object `ref` when the GC is marking. The
6277 // entrypoint will already be loaded in `temp2`.
6278 Register temp2 = lr;
6279 Location temp2_loc = LocationFrom(temp2);
6280 SlowPathCodeARM64* slow_path;
6281 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006282 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6283 // only supports address of the form `obj + field_offset`, where
6284 // `obj` is a register and `field_offset` is a register. Thus
6285 // `offset` and `scale_factor` above are expected to be null in
6286 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006287 DCHECK_EQ(offset, 0u);
6288 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006289 Location field_offset = index;
6290 slow_path =
6291 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6292 instruction,
6293 ref,
6294 obj,
6295 offset,
6296 /* index */ field_offset,
6297 scale_factor,
6298 needs_null_check,
6299 use_load_acquire,
6300 temp,
6301 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006302 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006303 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6304 instruction,
6305 ref,
6306 obj,
6307 offset,
6308 index,
6309 scale_factor,
6310 needs_null_check,
6311 use_load_acquire,
6312 temp,
6313 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006314 }
6315 AddSlowPath(slow_path);
6316
6317 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6318 const int32_t entry_point_offset =
6319 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6320 // Loading the entrypoint does not require a load acquire since it is only changed when
6321 // threads are suspended or running a checkpoint.
6322 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006323 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6324 // checking GetIsGcMarking.
6325 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006326 // Fast path: just load the reference.
6327 GenerateRawReferenceLoad(
6328 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006329 __ Bind(slow_path->GetExitLabel());
6330}
6331
6332void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6333 Location ref,
6334 Register obj,
6335 uint32_t offset,
6336 Location index,
6337 size_t scale_factor,
6338 bool needs_null_check,
6339 bool use_load_acquire) {
6340 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006341 Primitive::Type type = Primitive::kPrimNot;
6342 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006343
Roland Levillainba650a42017-03-06 13:52:32 +00006344 // If needed, vixl::EmissionCheckScope guards are used to ensure
6345 // that no pools are emitted between the load (macro) instruction
6346 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006347
Roland Levillain44015862016-01-22 11:47:17 +00006348 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006349 // Load types involving an "index": ArrayGet,
6350 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6351 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006352 if (use_load_acquire) {
6353 // UnsafeGetObjectVolatile intrinsic case.
6354 // Register `index` is not an index in an object array, but an
6355 // offset to an object reference field within object `obj`.
6356 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6357 DCHECK(instruction->GetLocations()->Intrinsified());
6358 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6359 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006360 DCHECK_EQ(offset, 0u);
6361 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006362 DCHECK_EQ(needs_null_check, false);
6363 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006364 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6365 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006366 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006367 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6368 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006369 if (index.IsConstant()) {
6370 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006371 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006372 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006373 if (needs_null_check) {
6374 MaybeRecordImplicitNullCheck(instruction);
6375 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006376 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006377 UseScratchRegisterScope temps(GetVIXLAssembler());
6378 Register temp = temps.AcquireW();
6379 __ Add(temp, obj, offset);
6380 {
6381 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6382 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6383 if (needs_null_check) {
6384 MaybeRecordImplicitNullCheck(instruction);
6385 }
6386 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006387 }
Roland Levillain44015862016-01-22 11:47:17 +00006388 }
Roland Levillain44015862016-01-22 11:47:17 +00006389 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006390 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006391 MemOperand field = HeapOperand(obj, offset);
6392 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006393 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6394 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006395 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006396 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006397 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006398 if (needs_null_check) {
6399 MaybeRecordImplicitNullCheck(instruction);
6400 }
Roland Levillain44015862016-01-22 11:47:17 +00006401 }
6402 }
6403
6404 // Object* ref = ref_addr->AsMirrorPtr()
6405 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006406}
6407
6408void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6409 Location out,
6410 Location ref,
6411 Location obj,
6412 uint32_t offset,
6413 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006414 DCHECK(kEmitCompilerReadBarrier);
6415
Roland Levillain44015862016-01-22 11:47:17 +00006416 // Insert a slow path based read barrier *after* the reference load.
6417 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006418 // If heap poisoning is enabled, the unpoisoning of the loaded
6419 // reference will be carried out by the runtime within the slow
6420 // path.
6421 //
6422 // Note that `ref` currently does not get unpoisoned (when heap
6423 // poisoning is enabled), which is alright as the `ref` argument is
6424 // not used by the artReadBarrierSlow entry point.
6425 //
6426 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6427 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6428 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6429 AddSlowPath(slow_path);
6430
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006431 __ B(slow_path->GetEntryLabel());
6432 __ Bind(slow_path->GetExitLabel());
6433}
6434
Roland Levillain44015862016-01-22 11:47:17 +00006435void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6436 Location out,
6437 Location ref,
6438 Location obj,
6439 uint32_t offset,
6440 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006441 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006442 // Baker's read barriers shall be handled by the fast path
6443 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6444 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006445 // If heap poisoning is enabled, unpoisoning will be taken care of
6446 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006447 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006448 } else if (kPoisonHeapReferences) {
6449 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6450 }
6451}
6452
Roland Levillain44015862016-01-22 11:47:17 +00006453void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6454 Location out,
6455 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006456 DCHECK(kEmitCompilerReadBarrier);
6457
Roland Levillain44015862016-01-22 11:47:17 +00006458 // Insert a slow path based read barrier *after* the GC root load.
6459 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006460 // Note that GC roots are not affected by heap poisoning, so we do
6461 // not need to do anything special for this here.
6462 SlowPathCodeARM64* slow_path =
6463 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6464 AddSlowPath(slow_path);
6465
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006466 __ B(slow_path->GetEntryLabel());
6467 __ Bind(slow_path->GetExitLabel());
6468}
6469
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006470void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6471 LocationSummary* locations =
6472 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6473 locations->SetInAt(0, Location::RequiresRegister());
6474 locations->SetOut(Location::RequiresRegister());
6475}
6476
6477void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6478 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006480 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006481 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006482 __ Ldr(XRegisterFrom(locations->Out()),
6483 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006484 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006485 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006486 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006487 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6488 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006489 __ Ldr(XRegisterFrom(locations->Out()),
6490 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006491 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006492}
6493
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006494static void PatchJitRootUse(uint8_t* code,
6495 const uint8_t* roots_data,
6496 vixl::aarch64::Literal<uint32_t>* literal,
6497 uint64_t index_in_table) {
6498 uint32_t literal_offset = literal->GetOffset();
6499 uintptr_t address =
6500 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6501 uint8_t* data = code + literal_offset;
6502 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6503}
6504
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006505void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6506 for (const auto& entry : jit_string_patches_) {
6507 const auto& it = jit_string_roots_.find(entry.first);
6508 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006509 PatchJitRootUse(code, roots_data, entry.second, it->second);
6510 }
6511 for (const auto& entry : jit_class_patches_) {
6512 const auto& it = jit_class_roots_.find(entry.first);
6513 DCHECK(it != jit_class_roots_.end());
6514 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006515 }
6516}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006517
Alexandre Rames67555f72014-11-18 10:55:16 +00006518#undef __
6519#undef QUICK_ENTRY_POINT
6520
Alexandre Rames5319def2014-10-23 10:03:10 +01006521} // namespace arm64
6522} // namespace art