blob: 9f2272baae3f777327424dfd5a65e73efb18fd05 [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 Serove1811ed2017-04-27 16:50:47 +01002647void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2650
2651 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2652
2653 locations->SetInAt(0, Location::RequiresRegister());
2654 // For byte case we don't need to shift the index variable so we can encode the data offset into
2655 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2656 // data offset constant generation out of the loop and reduce the critical path length in the
2657 // loop.
2658 locations->SetInAt(1, shift->GetValue() == 0
2659 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2660 : Location::RequiresRegister());
2661 locations->SetInAt(2, Location::ConstantLocation(shift));
2662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2663}
2664
2665void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2666 HIntermediateAddressIndex* instruction) {
2667 Register index_reg = InputRegisterAt(instruction, 0);
2668 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2669 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2670
2671 if (shift == 0) {
2672 __ Add(OutputRegister(instruction), index_reg, offset);
2673 } else {
2674 Register offset_reg = InputRegisterAt(instruction, 1);
2675 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2676 }
2677}
2678
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002679void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002680 LocationSummary* locations =
2681 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002682 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2683 if (instr->GetOpKind() == HInstruction::kSub &&
2684 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002685 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002686 // Don't allocate register for Mneg instruction.
2687 } else {
2688 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2689 Location::RequiresRegister());
2690 }
2691 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2692 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2694}
2695
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002696void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002697 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002698 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2699 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002700
2701 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2702 // This fixup should be carried out for all multiply-accumulate instructions:
2703 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2704 if (instr->GetType() == Primitive::kPrimLong &&
2705 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2706 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002707 vixl::aarch64::Instruction* prev =
2708 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002709 if (prev->IsLoadOrStore()) {
2710 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002711 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002712 __ nop();
2713 }
2714 }
2715
2716 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002717 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002718 __ Madd(res, mul_left, mul_right, accumulator);
2719 } else {
2720 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002721 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002722 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002723 __ Mneg(res, mul_left, mul_right);
2724 } else {
2725 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2726 __ Msub(res, mul_left, mul_right, accumulator);
2727 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002728 }
2729}
2730
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002731void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002732 bool object_array_get_with_read_barrier =
2733 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002734 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002735 new (GetGraph()->GetArena()) LocationSummary(instruction,
2736 object_array_get_with_read_barrier ?
2737 LocationSummary::kCallOnSlowPath :
2738 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002739 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002741 // We need a temporary register for the read barrier marking slow
2742 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002743 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2744 !Runtime::Current()->UseJitCompilation() &&
2745 instruction->GetIndex()->IsConstant()) {
2746 // Array loads with constant index are treated as field loads.
2747 // If link-time thunks for the Baker read barrier are enabled, for AOT
2748 // constant index loads we need a temporary only if the offset is too big.
2749 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2750 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2751 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2752 if (offset >= kReferenceLoadMinFarOffset) {
2753 locations->AddTemp(FixedTempLocation());
2754 }
2755 } else {
2756 locations->AddTemp(Location::RequiresRegister());
2757 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002758 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002761 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2762 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2763 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002764 // The output overlaps in the case of an object array get with
2765 // read barriers enabled: we do not want the move to overwrite the
2766 // array's location, as we need it to emit the read barrier.
2767 locations->SetOut(
2768 Location::RequiresRegister(),
2769 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002770 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002771}
2772
2773void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002774 Primitive::Type type = instruction->GetType();
2775 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002776 LocationSummary* locations = instruction->GetLocations();
2777 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002778 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002779 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002780 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2781 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002782 MacroAssembler* masm = GetVIXLAssembler();
2783 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002784
Roland Levillain19c54192016-11-04 13:44:09 +00002785 // The read barrier instrumentation of object ArrayGet instructions
2786 // does not support the HIntermediateAddress instruction.
2787 DCHECK(!((type == Primitive::kPrimNot) &&
2788 instruction->GetArray()->IsIntermediateAddress() &&
2789 kEmitCompilerReadBarrier));
2790
Roland Levillain44015862016-01-22 11:47:17 +00002791 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2792 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002793 // Note that a potential implicit null check is handled in the
2794 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002795 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002796 if (index.IsConstant()) {
2797 // Array load with a constant index can be treated as a field load.
2798 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2799 Location maybe_temp =
2800 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2801 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2802 out,
2803 obj.W(),
2804 offset,
2805 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002806 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002807 /* use_load_acquire */ false);
2808 } else {
2809 Register temp = WRegisterFrom(locations->GetTemp(0));
2810 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002811 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002812 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002813 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002814 // General case.
2815 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002816 Register length;
2817 if (maybe_compressed_char_at) {
2818 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2819 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002820 {
2821 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2822 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2823
2824 if (instruction->GetArray()->IsIntermediateAddress()) {
2825 DCHECK_LT(count_offset, offset);
2826 int64_t adjusted_offset =
2827 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2828 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2829 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2830 } else {
2831 __ Ldr(length, HeapOperand(obj, count_offset));
2832 }
2833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002834 }
jessicahandojo05765752016-09-09 19:01:32 -07002835 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002836 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002837 if (maybe_compressed_char_at) {
2838 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002839 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2840 "Expecting 0=compressed, 1=uncompressed");
2841 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002842 __ Ldrb(Register(OutputCPURegister(instruction)),
2843 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2844 __ B(&done);
2845 __ Bind(&uncompressed_load);
2846 __ Ldrh(Register(OutputCPURegister(instruction)),
2847 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2848 __ Bind(&done);
2849 } else {
2850 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2851 source = HeapOperand(obj, offset);
2852 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002853 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002854 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002855 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002856 // We do not need to compute the intermediate address from the array: the
2857 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002858 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002859 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002860 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002861 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2862 }
2863 temp = obj;
2864 } else {
2865 __ Add(temp, obj, offset);
2866 }
jessicahandojo05765752016-09-09 19:01:32 -07002867 if (maybe_compressed_char_at) {
2868 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002869 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2870 "Expecting 0=compressed, 1=uncompressed");
2871 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002872 __ Ldrb(Register(OutputCPURegister(instruction)),
2873 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2874 __ B(&done);
2875 __ Bind(&uncompressed_load);
2876 __ Ldrh(Register(OutputCPURegister(instruction)),
2877 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2878 __ Bind(&done);
2879 } else {
2880 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2881 }
Roland Levillain44015862016-01-22 11:47:17 +00002882 }
jessicahandojo05765752016-09-09 19:01:32 -07002883 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002884 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2885 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002886 codegen_->Load(type, OutputCPURegister(instruction), source);
2887 codegen_->MaybeRecordImplicitNullCheck(instruction);
2888 }
Roland Levillain44015862016-01-22 11:47:17 +00002889
2890 if (type == Primitive::kPrimNot) {
2891 static_assert(
2892 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2893 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2894 Location obj_loc = locations->InAt(0);
2895 if (index.IsConstant()) {
2896 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2897 } else {
2898 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2899 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002900 }
Roland Levillain4d027112015-07-01 15:41:14 +01002901 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002902}
2903
Alexandre Rames5319def2014-10-23 10:03:10 +01002904void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2905 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2906 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002907 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002908}
2909
2910void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002911 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002912 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002913 {
2914 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2915 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2916 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2917 codegen_->MaybeRecordImplicitNullCheck(instruction);
2918 }
jessicahandojo05765752016-09-09 19:01:32 -07002919 // Mask out compression flag from String's array length.
2920 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002921 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002922 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002923}
2924
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002925void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002926 Primitive::Type value_type = instruction->GetComponentType();
2927
2928 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2930 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002931 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002932 LocationSummary::kCallOnSlowPath :
2933 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002934 locations->SetInAt(0, Location::RequiresRegister());
2935 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002936 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2937 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2938 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002940 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002941 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002942 }
2943}
2944
2945void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2946 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002947 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002948 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002949 bool needs_write_barrier =
2950 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002951
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002952 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002953 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002954 CPURegister source = value;
2955 Location index = locations->InAt(1);
2956 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2957 MemOperand destination = HeapOperand(array);
2958 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002959
2960 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002961 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002962 if (index.IsConstant()) {
2963 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2964 destination = HeapOperand(array, offset);
2965 } else {
2966 UseScratchRegisterScope temps(masm);
2967 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002968 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002969 // We do not need to compute the intermediate address from the array: the
2970 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002971 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002972 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002973 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002974 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2975 }
2976 temp = array;
2977 } else {
2978 __ Add(temp, array, offset);
2979 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002980 destination = HeapOperand(temp,
2981 XRegisterFrom(index),
2982 LSL,
2983 Primitive::ComponentSizeShift(value_type));
2984 }
Artem Serov914d7a82017-02-07 14:33:49 +00002985 {
2986 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2987 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2988 codegen_->Store(value_type, value, destination);
2989 codegen_->MaybeRecordImplicitNullCheck(instruction);
2990 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002991 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002992 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002993 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002994 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002995 {
2996 // We use a block to end the scratch scope before the write barrier, thus
2997 // freeing the temporary registers so they can be used in `MarkGCCard`.
2998 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002999 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003000 if (index.IsConstant()) {
3001 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003002 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003003 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003004 destination = HeapOperand(temp,
3005 XRegisterFrom(index),
3006 LSL,
3007 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003008 }
3009
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003010 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3011 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3012 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3013
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003014 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003015 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3016 codegen_->AddSlowPath(slow_path);
3017 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003018 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003019 __ Cbnz(Register(value), &non_zero);
3020 if (!index.IsConstant()) {
3021 __ Add(temp, array, offset);
3022 }
Artem Serov914d7a82017-02-07 14:33:49 +00003023 {
3024 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3025 // emitted.
3026 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3027 __ Str(wzr, destination);
3028 codegen_->MaybeRecordImplicitNullCheck(instruction);
3029 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003030 __ B(&done);
3031 __ Bind(&non_zero);
3032 }
3033
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003034 // Note that when Baker read barriers are enabled, the type
3035 // checks are performed without read barriers. This is fine,
3036 // even in the case where a class object is in the from-space
3037 // after the flip, as a comparison involving such a type would
3038 // not produce a false positive; it may of course produce a
3039 // false negative, in which case we would take the ArraySet
3040 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003041
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003042 Register temp2 = temps.AcquireSameSizeAs(array);
3043 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003044 {
3045 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3046 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3047 __ Ldr(temp, HeapOperand(array, class_offset));
3048 codegen_->MaybeRecordImplicitNullCheck(instruction);
3049 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003050 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003051
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003052 // /* HeapReference<Class> */ temp = temp->component_type_
3053 __ Ldr(temp, HeapOperand(temp, component_offset));
3054 // /* HeapReference<Class> */ temp2 = value->klass_
3055 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3056 // If heap poisoning is enabled, no need to unpoison `temp`
3057 // nor `temp2`, as we are comparing two poisoned references.
3058 __ Cmp(temp, temp2);
3059 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003060
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003061 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3062 vixl::aarch64::Label do_put;
3063 __ B(eq, &do_put);
3064 // If heap poisoning is enabled, the `temp` reference has
3065 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003066 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3067
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003068 // /* HeapReference<Class> */ temp = temp->super_class_
3069 __ Ldr(temp, HeapOperand(temp, super_offset));
3070 // If heap poisoning is enabled, no need to unpoison
3071 // `temp`, as we are comparing against null below.
3072 __ Cbnz(temp, slow_path->GetEntryLabel());
3073 __ Bind(&do_put);
3074 } else {
3075 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003076 }
3077 }
3078
3079 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003080 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003081 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003082 __ Mov(temp2, value.W());
3083 GetAssembler()->PoisonHeapReference(temp2);
3084 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003085 }
3086
3087 if (!index.IsConstant()) {
3088 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003089 } else {
3090 // We no longer need the `temp` here so release it as the store below may
3091 // need a scratch register (if the constant index makes the offset too large)
3092 // and the poisoned `source` could be using the other scratch register.
3093 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003094 }
Artem Serov914d7a82017-02-07 14:33:49 +00003095 {
3096 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3097 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3098 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003099
Artem Serov914d7a82017-02-07 14:33:49 +00003100 if (!may_need_runtime_call_for_type_check) {
3101 codegen_->MaybeRecordImplicitNullCheck(instruction);
3102 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003103 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003104 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003105
3106 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3107
3108 if (done.IsLinked()) {
3109 __ Bind(&done);
3110 }
3111
3112 if (slow_path != nullptr) {
3113 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003114 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003115 }
3116}
3117
Alexandre Rames67555f72014-11-18 10:55:16 +00003118void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003119 RegisterSet caller_saves = RegisterSet::Empty();
3120 InvokeRuntimeCallingConvention calling_convention;
3121 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3122 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3123 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003124 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003125 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003126}
3127
3128void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003129 BoundsCheckSlowPathARM64* slow_path =
3130 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003131 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003132 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3133 __ B(slow_path->GetEntryLabel(), hs);
3134}
3135
Alexandre Rames67555f72014-11-18 10:55:16 +00003136void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3137 LocationSummary* locations =
3138 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3139 locations->SetInAt(0, Location::RequiresRegister());
3140 if (check->HasUses()) {
3141 locations->SetOut(Location::SameAsFirstInput());
3142 }
3143}
3144
3145void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3146 // We assume the class is not null.
3147 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3148 check->GetLoadClass(), check, check->GetDexPc(), true);
3149 codegen_->AddSlowPath(slow_path);
3150 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3151}
3152
Roland Levillain1a653882016-03-18 18:05:57 +00003153static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3154 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3155 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3156}
3157
3158void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3159 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3160 Location rhs_loc = instruction->GetLocations()->InAt(1);
3161 if (rhs_loc.IsConstant()) {
3162 // 0.0 is the only immediate that can be encoded directly in
3163 // an FCMP instruction.
3164 //
3165 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3166 // specify that in a floating-point comparison, positive zero
3167 // and negative zero are considered equal, so we can use the
3168 // literal 0.0 for both cases here.
3169 //
3170 // Note however that some methods (Float.equal, Float.compare,
3171 // Float.compareTo, Double.equal, Double.compare,
3172 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3173 // StrictMath.min) consider 0.0 to be (strictly) greater than
3174 // -0.0. So if we ever translate calls to these methods into a
3175 // HCompare instruction, we must handle the -0.0 case with
3176 // care here.
3177 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3178 __ Fcmp(lhs_reg, 0.0);
3179 } else {
3180 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3181 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003182}
3183
Serban Constantinescu02164b32014-11-13 14:05:07 +00003184void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003185 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003186 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3187 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003188 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003189 case Primitive::kPrimBoolean:
3190 case Primitive::kPrimByte:
3191 case Primitive::kPrimShort:
3192 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003193 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003194 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003195 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003196 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3198 break;
3199 }
3200 case Primitive::kPrimFloat:
3201 case Primitive::kPrimDouble: {
3202 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003203 locations->SetInAt(1,
3204 IsFloatingPointZeroConstant(compare->InputAt(1))
3205 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3206 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003207 locations->SetOut(Location::RequiresRegister());
3208 break;
3209 }
3210 default:
3211 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3212 }
3213}
3214
3215void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3216 Primitive::Type in_type = compare->InputAt(0)->GetType();
3217
3218 // 0 if: left == right
3219 // 1 if: left > right
3220 // -1 if: left < right
3221 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003222 case Primitive::kPrimBoolean:
3223 case Primitive::kPrimByte:
3224 case Primitive::kPrimShort:
3225 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003226 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003227 case Primitive::kPrimLong: {
3228 Register result = OutputRegister(compare);
3229 Register left = InputRegisterAt(compare, 0);
3230 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003231 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003232 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3233 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003234 break;
3235 }
3236 case Primitive::kPrimFloat:
3237 case Primitive::kPrimDouble: {
3238 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003239 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003240 __ Cset(result, ne);
3241 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003242 break;
3243 }
3244 default:
3245 LOG(FATAL) << "Unimplemented compare type " << in_type;
3246 }
3247}
3248
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003249void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003250 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003251
3252 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3253 locations->SetInAt(0, Location::RequiresFpuRegister());
3254 locations->SetInAt(1,
3255 IsFloatingPointZeroConstant(instruction->InputAt(1))
3256 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3257 : Location::RequiresFpuRegister());
3258 } else {
3259 // Integer cases.
3260 locations->SetInAt(0, Location::RequiresRegister());
3261 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3262 }
3263
David Brazdilb3e773e2016-01-26 11:28:37 +00003264 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003265 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003266 }
3267}
3268
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003269void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003270 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003271 return;
3272 }
3273
3274 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003275 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003276 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003277
Roland Levillain7f63c522015-07-13 15:54:55 +00003278 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003279 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003280 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003281 } else {
3282 // Integer cases.
3283 Register lhs = InputRegisterAt(instruction, 0);
3284 Operand rhs = InputOperandAt(instruction, 1);
3285 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003286 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003287 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003288}
3289
3290#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3291 M(Equal) \
3292 M(NotEqual) \
3293 M(LessThan) \
3294 M(LessThanOrEqual) \
3295 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003296 M(GreaterThanOrEqual) \
3297 M(Below) \
3298 M(BelowOrEqual) \
3299 M(Above) \
3300 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003301#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003302void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3303void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003304FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003305#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003306#undef FOR_EACH_CONDITION_INSTRUCTION
3307
Zheng Xuc6667102015-05-15 16:08:45 +08003308void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3309 DCHECK(instruction->IsDiv() || instruction->IsRem());
3310
3311 LocationSummary* locations = instruction->GetLocations();
3312 Location second = locations->InAt(1);
3313 DCHECK(second.IsConstant());
3314
3315 Register out = OutputRegister(instruction);
3316 Register dividend = InputRegisterAt(instruction, 0);
3317 int64_t imm = Int64FromConstant(second.GetConstant());
3318 DCHECK(imm == 1 || imm == -1);
3319
3320 if (instruction->IsRem()) {
3321 __ Mov(out, 0);
3322 } else {
3323 if (imm == 1) {
3324 __ Mov(out, dividend);
3325 } else {
3326 __ Neg(out, dividend);
3327 }
3328 }
3329}
3330
3331void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3332 DCHECK(instruction->IsDiv() || instruction->IsRem());
3333
3334 LocationSummary* locations = instruction->GetLocations();
3335 Location second = locations->InAt(1);
3336 DCHECK(second.IsConstant());
3337
3338 Register out = OutputRegister(instruction);
3339 Register dividend = InputRegisterAt(instruction, 0);
3340 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003341 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003342 int ctz_imm = CTZ(abs_imm);
3343
3344 UseScratchRegisterScope temps(GetVIXLAssembler());
3345 Register temp = temps.AcquireSameSizeAs(out);
3346
3347 if (instruction->IsDiv()) {
3348 __ Add(temp, dividend, abs_imm - 1);
3349 __ Cmp(dividend, 0);
3350 __ Csel(out, temp, dividend, lt);
3351 if (imm > 0) {
3352 __ Asr(out, out, ctz_imm);
3353 } else {
3354 __ Neg(out, Operand(out, ASR, ctz_imm));
3355 }
3356 } else {
3357 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3358 __ Asr(temp, dividend, bits - 1);
3359 __ Lsr(temp, temp, bits - ctz_imm);
3360 __ Add(out, dividend, temp);
3361 __ And(out, out, abs_imm - 1);
3362 __ Sub(out, out, temp);
3363 }
3364}
3365
3366void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3367 DCHECK(instruction->IsDiv() || instruction->IsRem());
3368
3369 LocationSummary* locations = instruction->GetLocations();
3370 Location second = locations->InAt(1);
3371 DCHECK(second.IsConstant());
3372
3373 Register out = OutputRegister(instruction);
3374 Register dividend = InputRegisterAt(instruction, 0);
3375 int64_t imm = Int64FromConstant(second.GetConstant());
3376
3377 Primitive::Type type = instruction->GetResultType();
3378 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3379
3380 int64_t magic;
3381 int shift;
3382 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3383
3384 UseScratchRegisterScope temps(GetVIXLAssembler());
3385 Register temp = temps.AcquireSameSizeAs(out);
3386
3387 // temp = get_high(dividend * magic)
3388 __ Mov(temp, magic);
3389 if (type == Primitive::kPrimLong) {
3390 __ Smulh(temp, dividend, temp);
3391 } else {
3392 __ Smull(temp.X(), dividend, temp);
3393 __ Lsr(temp.X(), temp.X(), 32);
3394 }
3395
3396 if (imm > 0 && magic < 0) {
3397 __ Add(temp, temp, dividend);
3398 } else if (imm < 0 && magic > 0) {
3399 __ Sub(temp, temp, dividend);
3400 }
3401
3402 if (shift != 0) {
3403 __ Asr(temp, temp, shift);
3404 }
3405
3406 if (instruction->IsDiv()) {
3407 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3408 } else {
3409 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3410 // TODO: Strength reduction for msub.
3411 Register temp_imm = temps.AcquireSameSizeAs(out);
3412 __ Mov(temp_imm, imm);
3413 __ Msub(out, temp, temp_imm, dividend);
3414 }
3415}
3416
3417void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3418 DCHECK(instruction->IsDiv() || instruction->IsRem());
3419 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003420 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003421
3422 LocationSummary* locations = instruction->GetLocations();
3423 Register out = OutputRegister(instruction);
3424 Location second = locations->InAt(1);
3425
3426 if (second.IsConstant()) {
3427 int64_t imm = Int64FromConstant(second.GetConstant());
3428
3429 if (imm == 0) {
3430 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3431 } else if (imm == 1 || imm == -1) {
3432 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003433 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003434 DivRemByPowerOfTwo(instruction);
3435 } else {
3436 DCHECK(imm <= -2 || imm >= 2);
3437 GenerateDivRemWithAnyConstant(instruction);
3438 }
3439 } else {
3440 Register dividend = InputRegisterAt(instruction, 0);
3441 Register divisor = InputRegisterAt(instruction, 1);
3442 if (instruction->IsDiv()) {
3443 __ Sdiv(out, dividend, divisor);
3444 } else {
3445 UseScratchRegisterScope temps(GetVIXLAssembler());
3446 Register temp = temps.AcquireSameSizeAs(out);
3447 __ Sdiv(temp, dividend, divisor);
3448 __ Msub(out, temp, divisor, dividend);
3449 }
3450 }
3451}
3452
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003453void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3456 switch (div->GetResultType()) {
3457 case Primitive::kPrimInt:
3458 case Primitive::kPrimLong:
3459 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003460 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003461 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3462 break;
3463
3464 case Primitive::kPrimFloat:
3465 case Primitive::kPrimDouble:
3466 locations->SetInAt(0, Location::RequiresFpuRegister());
3467 locations->SetInAt(1, Location::RequiresFpuRegister());
3468 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3469 break;
3470
3471 default:
3472 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3473 }
3474}
3475
3476void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3477 Primitive::Type type = div->GetResultType();
3478 switch (type) {
3479 case Primitive::kPrimInt:
3480 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003481 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003482 break;
3483
3484 case Primitive::kPrimFloat:
3485 case Primitive::kPrimDouble:
3486 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3487 break;
3488
3489 default:
3490 LOG(FATAL) << "Unexpected div type " << type;
3491 }
3492}
3493
Alexandre Rames67555f72014-11-18 10:55:16 +00003494void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003495 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003496 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003497}
3498
3499void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3500 SlowPathCodeARM64* slow_path =
3501 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3502 codegen_->AddSlowPath(slow_path);
3503 Location value = instruction->GetLocations()->InAt(0);
3504
Alexandre Rames3e69f162014-12-10 10:36:50 +00003505 Primitive::Type type = instruction->GetType();
3506
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003507 if (!Primitive::IsIntegralType(type)) {
3508 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003509 return;
3510 }
3511
Alexandre Rames67555f72014-11-18 10:55:16 +00003512 if (value.IsConstant()) {
3513 int64_t divisor = Int64ConstantFrom(value);
3514 if (divisor == 0) {
3515 __ B(slow_path->GetEntryLabel());
3516 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003517 // A division by a non-null constant is valid. We don't need to perform
3518 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003519 }
3520 } else {
3521 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3522 }
3523}
3524
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003525void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3526 LocationSummary* locations =
3527 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3528 locations->SetOut(Location::ConstantLocation(constant));
3529}
3530
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003531void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3532 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003533 // Will be generated at use site.
3534}
3535
Alexandre Rames5319def2014-10-23 10:03:10 +01003536void LocationsBuilderARM64::VisitExit(HExit* exit) {
3537 exit->SetLocations(nullptr);
3538}
3539
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003540void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003541}
3542
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003543void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3544 LocationSummary* locations =
3545 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3546 locations->SetOut(Location::ConstantLocation(constant));
3547}
3548
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003549void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003550 // Will be generated at use site.
3551}
3552
David Brazdilfc6a86a2015-06-26 10:33:45 +00003553void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003554 DCHECK(!successor->IsExitBlock());
3555 HBasicBlock* block = got->GetBlock();
3556 HInstruction* previous = got->GetPrevious();
3557 HLoopInformation* info = block->GetLoopInformation();
3558
David Brazdil46e2a392015-03-16 17:31:52 +00003559 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003560 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3561 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3562 return;
3563 }
3564 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3565 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3566 }
3567 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003568 __ B(codegen_->GetLabelOf(successor));
3569 }
3570}
3571
David Brazdilfc6a86a2015-06-26 10:33:45 +00003572void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3573 got->SetLocations(nullptr);
3574}
3575
3576void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3577 HandleGoto(got, got->GetSuccessor());
3578}
3579
3580void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3581 try_boundary->SetLocations(nullptr);
3582}
3583
3584void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3585 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3586 if (!successor->IsExitBlock()) {
3587 HandleGoto(try_boundary, successor);
3588 }
3589}
3590
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003591void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003592 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003593 vixl::aarch64::Label* true_target,
3594 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003595 // FP branching requires both targets to be explicit. If either of the targets
3596 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003597 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003598 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003599
David Brazdil0debae72015-11-12 18:37:00 +00003600 if (true_target == nullptr && false_target == nullptr) {
3601 // Nothing to do. The code always falls through.
3602 return;
3603 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003604 // Constant condition, statically compared against "true" (integer value 1).
3605 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003606 if (true_target != nullptr) {
3607 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003608 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003609 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003610 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003611 if (false_target != nullptr) {
3612 __ B(false_target);
3613 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003614 }
David Brazdil0debae72015-11-12 18:37:00 +00003615 return;
3616 }
3617
3618 // The following code generates these patterns:
3619 // (1) true_target == nullptr && false_target != nullptr
3620 // - opposite condition true => branch to false_target
3621 // (2) true_target != nullptr && false_target == nullptr
3622 // - condition true => branch to true_target
3623 // (3) true_target != nullptr && false_target != nullptr
3624 // - condition true => branch to true_target
3625 // - branch to false_target
3626 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003627 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003628 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003629 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003630 if (true_target == nullptr) {
3631 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3632 } else {
3633 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3634 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003635 } else {
3636 // The condition instruction has not been materialized, use its inputs as
3637 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003638 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003639
David Brazdil0debae72015-11-12 18:37:00 +00003640 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003641 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003642 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003643 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003644 IfCondition opposite_condition = condition->GetOppositeCondition();
3645 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003646 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003647 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003648 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003649 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003650 // Integer cases.
3651 Register lhs = InputRegisterAt(condition, 0);
3652 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003653
3654 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003655 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003656 if (true_target == nullptr) {
3657 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3658 non_fallthrough_target = false_target;
3659 } else {
3660 arm64_cond = ARM64Condition(condition->GetCondition());
3661 non_fallthrough_target = true_target;
3662 }
3663
Aart Bik086d27e2016-01-20 17:02:00 -08003664 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003665 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003666 switch (arm64_cond) {
3667 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003668 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003669 break;
3670 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003671 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003672 break;
3673 case lt:
3674 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003675 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003676 break;
3677 case ge:
3678 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003679 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003680 break;
3681 default:
3682 // Without the `static_cast` the compiler throws an error for
3683 // `-Werror=sign-promo`.
3684 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3685 }
3686 } else {
3687 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003688 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003689 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003690 }
3691 }
David Brazdil0debae72015-11-12 18:37:00 +00003692
3693 // If neither branch falls through (case 3), the conditional branch to `true_target`
3694 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3695 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003696 __ B(false_target);
3697 }
David Brazdil0debae72015-11-12 18:37:00 +00003698
3699 if (fallthrough_target.IsLinked()) {
3700 __ Bind(&fallthrough_target);
3701 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003702}
3703
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003704void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003706 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003707 locations->SetInAt(0, Location::RequiresRegister());
3708 }
3709}
3710
3711void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003712 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3713 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003714 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3715 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3716 true_target = nullptr;
3717 }
3718 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3719 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3720 false_target = nullptr;
3721 }
David Brazdil0debae72015-11-12 18:37:00 +00003722 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003723}
3724
3725void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3726 LocationSummary* locations = new (GetGraph()->GetArena())
3727 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003728 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003729 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003730 locations->SetInAt(0, Location::RequiresRegister());
3731 }
3732}
3733
3734void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003735 SlowPathCodeARM64* slow_path =
3736 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003737 GenerateTestAndBranch(deoptimize,
3738 /* condition_input_index */ 0,
3739 slow_path->GetEntryLabel(),
3740 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003741}
3742
Mingyao Yang063fc772016-08-02 11:02:54 -07003743void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3744 LocationSummary* locations = new (GetGraph()->GetArena())
3745 LocationSummary(flag, LocationSummary::kNoCall);
3746 locations->SetOut(Location::RequiresRegister());
3747}
3748
3749void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3750 __ Ldr(OutputRegister(flag),
3751 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3752}
3753
David Brazdilc0b601b2016-02-08 14:20:45 +00003754static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3755 return condition->IsCondition() &&
3756 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3757}
3758
Alexandre Rames880f1192016-06-13 16:04:50 +01003759static inline Condition GetConditionForSelect(HCondition* condition) {
3760 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003761 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3762 : ARM64Condition(cond);
3763}
3764
David Brazdil74eb1b22015-12-14 11:44:01 +00003765void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3766 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003767 if (Primitive::IsFloatingPointType(select->GetType())) {
3768 locations->SetInAt(0, Location::RequiresFpuRegister());
3769 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003770 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003771 } else {
3772 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3773 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3774 bool is_true_value_constant = cst_true_value != nullptr;
3775 bool is_false_value_constant = cst_false_value != nullptr;
3776 // Ask VIXL whether we should synthesize constants in registers.
3777 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3778 Operand true_op = is_true_value_constant ?
3779 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3780 Operand false_op = is_false_value_constant ?
3781 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3782 bool true_value_in_register = false;
3783 bool false_value_in_register = false;
3784 MacroAssembler::GetCselSynthesisInformation(
3785 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3786 true_value_in_register |= !is_true_value_constant;
3787 false_value_in_register |= !is_false_value_constant;
3788
3789 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3790 : Location::ConstantLocation(cst_true_value));
3791 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3792 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003793 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003794 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003795
David Brazdil74eb1b22015-12-14 11:44:01 +00003796 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3797 locations->SetInAt(2, Location::RequiresRegister());
3798 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003799}
3800
3801void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003802 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003803 Condition csel_cond;
3804
3805 if (IsBooleanValueOrMaterializedCondition(cond)) {
3806 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003807 // Use the condition flags set by the previous instruction.
3808 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003809 } else {
3810 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003811 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003812 }
3813 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003814 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003815 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003816 } else {
3817 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003818 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003819 }
3820
Alexandre Rames880f1192016-06-13 16:04:50 +01003821 if (Primitive::IsFloatingPointType(select->GetType())) {
3822 __ Fcsel(OutputFPRegister(select),
3823 InputFPRegisterAt(select, 1),
3824 InputFPRegisterAt(select, 0),
3825 csel_cond);
3826 } else {
3827 __ Csel(OutputRegister(select),
3828 InputOperandAt(select, 1),
3829 InputOperandAt(select, 0),
3830 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003831 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003832}
3833
David Srbecky0cf44932015-12-09 14:09:59 +00003834void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3835 new (GetGraph()->GetArena()) LocationSummary(info);
3836}
3837
David Srbeckyd28f4a02016-03-14 17:14:24 +00003838void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3839 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003840}
3841
3842void CodeGeneratorARM64::GenerateNop() {
3843 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003844}
3845
Alexandre Rames5319def2014-10-23 10:03:10 +01003846void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003847 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003848}
3849
3850void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003851 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003852}
3853
3854void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003855 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003856}
3857
3858void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003859 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003860}
3861
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003862// Temp is used for read barrier.
3863static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3864 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003865 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003866 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3867 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3868 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3869 return 1;
3870 }
3871 return 0;
3872}
3873
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003874// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003875// interface pointer, one for loading the current interface.
3876// The other checks have one temp for loading the object's class.
3877static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3878 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3879 return 3;
3880 }
3881 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003882}
3883
Alexandre Rames67555f72014-11-18 10:55:16 +00003884void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003885 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003887 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 case TypeCheckKind::kExactCheck:
3890 case TypeCheckKind::kAbstractClassCheck:
3891 case TypeCheckKind::kClassHierarchyCheck:
3892 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003893 call_kind =
3894 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003895 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003896 break;
3897 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003898 case TypeCheckKind::kUnresolvedCheck:
3899 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 call_kind = LocationSummary::kCallOnSlowPath;
3901 break;
3902 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903
Alexandre Rames67555f72014-11-18 10:55:16 +00003904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003905 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003906 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003907 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003908 locations->SetInAt(0, Location::RequiresRegister());
3909 locations->SetInAt(1, Location::RequiresRegister());
3910 // The "out" register is used as a temporary, so it overlaps with the inputs.
3911 // Note that TypeCheckSlowPathARM64 uses this register too.
3912 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003913 // Add temps if necessary for read barriers.
3914 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003915}
3916
3917void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003918 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003919 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003921 Register obj = InputRegisterAt(instruction, 0);
3922 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003923 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003924 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003925 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3926 DCHECK_LE(num_temps, 1u);
3927 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003928 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3929 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3930 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3931 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003932
Scott Wakeling97c72b72016-06-24 16:19:36 +01003933 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003934 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003935
3936 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003937 // Avoid null check if we know `obj` is not null.
3938 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 __ Cbz(obj, &zero);
3940 }
3941
Roland Levillain44015862016-01-22 11:47:17 +00003942 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003944 // /* HeapReference<Class> */ out = obj->klass_
3945 GenerateReferenceLoadTwoRegisters(instruction,
3946 out_loc,
3947 obj_loc,
3948 class_offset,
3949 maybe_temp_loc,
3950 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 __ Cmp(out, cls);
3952 __ Cset(out, eq);
3953 if (zero.IsLinked()) {
3954 __ B(&done);
3955 }
3956 break;
3957 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003958
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003960 // /* HeapReference<Class> */ out = obj->klass_
3961 GenerateReferenceLoadTwoRegisters(instruction,
3962 out_loc,
3963 obj_loc,
3964 class_offset,
3965 maybe_temp_loc,
3966 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003967 // If the class is abstract, we eagerly fetch the super class of the
3968 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003969 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003970 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003971 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003972 GenerateReferenceLoadOneRegister(instruction,
3973 out_loc,
3974 super_offset,
3975 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003976 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003977 // If `out` is null, we use it for the result, and jump to `done`.
3978 __ Cbz(out, &done);
3979 __ Cmp(out, cls);
3980 __ B(ne, &loop);
3981 __ Mov(out, 1);
3982 if (zero.IsLinked()) {
3983 __ B(&done);
3984 }
3985 break;
3986 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003987
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003988 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003989 // /* HeapReference<Class> */ out = obj->klass_
3990 GenerateReferenceLoadTwoRegisters(instruction,
3991 out_loc,
3992 obj_loc,
3993 class_offset,
3994 maybe_temp_loc,
3995 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003996 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003997 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 __ Bind(&loop);
3999 __ Cmp(out, cls);
4000 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004001 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004002 GenerateReferenceLoadOneRegister(instruction,
4003 out_loc,
4004 super_offset,
4005 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004006 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004007 __ Cbnz(out, &loop);
4008 // If `out` is null, we use it for the result, and jump to `done`.
4009 __ B(&done);
4010 __ Bind(&success);
4011 __ Mov(out, 1);
4012 if (zero.IsLinked()) {
4013 __ B(&done);
4014 }
4015 break;
4016 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004017
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004019 // /* HeapReference<Class> */ out = obj->klass_
4020 GenerateReferenceLoadTwoRegisters(instruction,
4021 out_loc,
4022 obj_loc,
4023 class_offset,
4024 maybe_temp_loc,
4025 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004026 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004027 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004028 __ Cmp(out, cls);
4029 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004031 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004032 GenerateReferenceLoadOneRegister(instruction,
4033 out_loc,
4034 component_offset,
4035 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004036 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 // If `out` is null, we use it for the result, and jump to `done`.
4038 __ Cbz(out, &done);
4039 __ Ldrh(out, HeapOperand(out, primitive_offset));
4040 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4041 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004042 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004043 __ Mov(out, 1);
4044 __ B(&done);
4045 break;
4046 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004048 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004049 // No read barrier since the slow path will retry upon failure.
4050 // /* HeapReference<Class> */ out = obj->klass_
4051 GenerateReferenceLoadTwoRegisters(instruction,
4052 out_loc,
4053 obj_loc,
4054 class_offset,
4055 maybe_temp_loc,
4056 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004057 __ Cmp(out, cls);
4058 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004059 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4060 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061 codegen_->AddSlowPath(slow_path);
4062 __ B(ne, slow_path->GetEntryLabel());
4063 __ Mov(out, 1);
4064 if (zero.IsLinked()) {
4065 __ B(&done);
4066 }
4067 break;
4068 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004069
Calin Juravle98893e12015-10-02 21:05:03 +01004070 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004071 case TypeCheckKind::kInterfaceCheck: {
4072 // Note that we indeed only call on slow path, but we always go
4073 // into the slow path for the unresolved and interface check
4074 // cases.
4075 //
4076 // We cannot directly call the InstanceofNonTrivial runtime
4077 // entry point without resorting to a type checking slow path
4078 // here (i.e. by calling InvokeRuntime directly), as it would
4079 // require to assign fixed registers for the inputs of this
4080 // HInstanceOf instruction (following the runtime calling
4081 // convention), which might be cluttered by the potential first
4082 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004083 //
4084 // TODO: Introduce a new runtime entry point taking the object
4085 // to test (instead of its class) as argument, and let it deal
4086 // with the read barrier issues. This will let us refactor this
4087 // case of the `switch` code as it was previously (with a direct
4088 // call to the runtime not using a type checking slow path).
4089 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004090 DCHECK(locations->OnlyCallsOnSlowPath());
4091 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4092 /* is_fatal */ false);
4093 codegen_->AddSlowPath(slow_path);
4094 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 if (zero.IsLinked()) {
4096 __ B(&done);
4097 }
4098 break;
4099 }
4100 }
4101
4102 if (zero.IsLinked()) {
4103 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004104 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004105 }
4106
4107 if (done.IsLinked()) {
4108 __ Bind(&done);
4109 }
4110
4111 if (slow_path != nullptr) {
4112 __ Bind(slow_path->GetExitLabel());
4113 }
4114}
4115
4116void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4117 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4118 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4119
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004120 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4121 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122 case TypeCheckKind::kExactCheck:
4123 case TypeCheckKind::kAbstractClassCheck:
4124 case TypeCheckKind::kClassHierarchyCheck:
4125 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004126 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4127 LocationSummary::kCallOnSlowPath :
4128 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004129 break;
4130 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004131 case TypeCheckKind::kUnresolvedCheck:
4132 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004133 call_kind = LocationSummary::kCallOnSlowPath;
4134 break;
4135 }
4136
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4138 locations->SetInAt(0, Location::RequiresRegister());
4139 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004140 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4141 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004142}
4143
4144void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004145 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004147 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148 Register obj = InputRegisterAt(instruction, 0);
4149 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004150 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4151 DCHECK_GE(num_temps, 1u);
4152 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004153 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004154 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4155 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004156 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004157 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4158 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4159 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4160 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4161 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4162 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4163 const uint32_t object_array_data_offset =
4164 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004165
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004166 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004167 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4168 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4169 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004170 if (!kEmitCompilerReadBarrier) {
4171 is_type_check_slow_path_fatal =
4172 (type_check_kind == TypeCheckKind::kExactCheck ||
4173 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4174 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4175 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4176 !instruction->CanThrowIntoCatchBlock();
4177 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004178 SlowPathCodeARM64* type_check_slow_path =
4179 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4180 is_type_check_slow_path_fatal);
4181 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182
Scott Wakeling97c72b72016-06-24 16:19:36 +01004183 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 // Avoid null check if we know obj is not null.
4185 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004186 __ Cbz(obj, &done);
4187 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004188
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004189 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004190 case TypeCheckKind::kExactCheck:
4191 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004192 // /* HeapReference<Class> */ temp = obj->klass_
4193 GenerateReferenceLoadTwoRegisters(instruction,
4194 temp_loc,
4195 obj_loc,
4196 class_offset,
4197 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004198 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004200 __ Cmp(temp, cls);
4201 // Jump to slow path for throwing the exception or doing a
4202 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004203 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004204 break;
4205 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004208 // /* HeapReference<Class> */ temp = obj->klass_
4209 GenerateReferenceLoadTwoRegisters(instruction,
4210 temp_loc,
4211 obj_loc,
4212 class_offset,
4213 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004214 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004215
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 // If the class is abstract, we eagerly fetch the super class of the
4217 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004218 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004219 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004220 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004221 GenerateReferenceLoadOneRegister(instruction,
4222 temp_loc,
4223 super_offset,
4224 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004225 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004227 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4228 // exception.
4229 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4230 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004231 __ Cmp(temp, cls);
4232 __ B(ne, &loop);
4233 break;
4234 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004236 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004237 // /* HeapReference<Class> */ temp = obj->klass_
4238 GenerateReferenceLoadTwoRegisters(instruction,
4239 temp_loc,
4240 obj_loc,
4241 class_offset,
4242 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004243 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004244
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004245 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004246 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004247 __ Bind(&loop);
4248 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004249 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004250
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004251 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004252 GenerateReferenceLoadOneRegister(instruction,
4253 temp_loc,
4254 super_offset,
4255 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004256 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257
4258 // If the class reference currently in `temp` is not null, jump
4259 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004260 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004261 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004262 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004263 break;
4264 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004265
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004266 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004267 // /* HeapReference<Class> */ temp = obj->klass_
4268 GenerateReferenceLoadTwoRegisters(instruction,
4269 temp_loc,
4270 obj_loc,
4271 class_offset,
4272 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004273 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004274
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004275 // Do an exact check.
4276 __ Cmp(temp, cls);
4277 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004278
4279 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004280 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004281 GenerateReferenceLoadOneRegister(instruction,
4282 temp_loc,
4283 component_offset,
4284 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004285 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004286
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004287 // If the component type is null, jump to the slow path to throw the exception.
4288 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4289 // Otherwise, the object is indeed an array. Further check that this component type is not a
4290 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004291 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4292 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004293 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004294 break;
4295 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004296
Calin Juravle98893e12015-10-02 21:05:03 +01004297 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004298 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004299 //
4300 // We cannot directly call the CheckCast runtime entry point
4301 // without resorting to a type checking slow path here (i.e. by
4302 // calling InvokeRuntime directly), as it would require to
4303 // assign fixed registers for the inputs of this HInstanceOf
4304 // instruction (following the runtime calling convention), which
4305 // might be cluttered by the potential first read barrier
4306 // emission at the beginning of this method.
4307 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004308 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004309 case TypeCheckKind::kInterfaceCheck: {
4310 // /* HeapReference<Class> */ temp = obj->klass_
4311 GenerateReferenceLoadTwoRegisters(instruction,
4312 temp_loc,
4313 obj_loc,
4314 class_offset,
4315 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004316 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004317
4318 // /* HeapReference<Class> */ temp = temp->iftable_
4319 GenerateReferenceLoadTwoRegisters(instruction,
4320 temp_loc,
4321 temp_loc,
4322 iftable_offset,
4323 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004324 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004325 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004326 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004327 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004328 vixl::aarch64::Label start_loop;
4329 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004330 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004331 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4332 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004333 // Go to next interface.
4334 __ Add(temp, temp, 2 * kHeapReferenceSize);
4335 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004336 // Compare the classes and continue the loop if they do not match.
4337 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4338 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004339 break;
4340 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004341 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004342 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004343
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004344 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004345}
4346
Alexandre Rames5319def2014-10-23 10:03:10 +01004347void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4349 locations->SetOut(Location::ConstantLocation(constant));
4350}
4351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004352void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004353 // Will be generated at use site.
4354}
4355
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004356void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4358 locations->SetOut(Location::ConstantLocation(constant));
4359}
4360
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004361void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004362 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004363}
4364
Calin Juravle175dc732015-08-25 15:42:32 +01004365void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4366 // The trampoline uses the same calling convention as dex calling conventions,
4367 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4368 // the method_idx.
4369 HandleInvoke(invoke);
4370}
4371
4372void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4373 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4374}
4375
Alexandre Rames5319def2014-10-23 10:03:10 +01004376void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004377 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004378 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004379}
4380
Alexandre Rames67555f72014-11-18 10:55:16 +00004381void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4382 HandleInvoke(invoke);
4383}
4384
4385void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4386 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004387 LocationSummary* locations = invoke->GetLocations();
4388 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004389 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004390 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004391 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004392
4393 // The register ip1 is required to be used for the hidden argument in
4394 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004395 MacroAssembler* masm = GetVIXLAssembler();
4396 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004397 scratch_scope.Exclude(ip1);
4398 __ Mov(ip1, invoke->GetDexMethodIndex());
4399
Artem Serov914d7a82017-02-07 14:33:49 +00004400 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004401 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004402 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004403 {
4404 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4405 // /* HeapReference<Class> */ temp = temp->klass_
4406 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4407 codegen_->MaybeRecordImplicitNullCheck(invoke);
4408 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004409 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004410 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004411 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004412 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004413 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 }
Artem Serov914d7a82017-02-07 14:33:49 +00004415
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004416 // Instead of simply (possibly) unpoisoning `temp` here, we should
4417 // emit a read barrier for the previous class reference load.
4418 // However this is not required in practice, as this is an
4419 // intermediate/temporary reference and because the current
4420 // concurrent copying collector keeps the from-space memory
4421 // intact/accessible until the end of the marking phase (the
4422 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004423 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004424 __ Ldr(temp,
4425 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4426 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004427 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004428 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004429 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004431 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004432
4433 {
4434 // Ensure the pc position is recorded immediately after the `blr` instruction.
4435 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4436
4437 // lr();
4438 __ blr(lr);
4439 DCHECK(!codegen_->IsLeafMethod());
4440 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4441 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004442}
4443
4444void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004445 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004446 if (intrinsic.TryDispatch(invoke)) {
4447 return;
4448 }
4449
Alexandre Rames67555f72014-11-18 10:55:16 +00004450 HandleInvoke(invoke);
4451}
4452
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004453void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004454 // Explicit clinit checks triggered by static invokes must have been pruned by
4455 // art::PrepareForRegisterAllocation.
4456 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004457
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004458 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004459 if (intrinsic.TryDispatch(invoke)) {
4460 return;
4461 }
4462
Alexandre Rames67555f72014-11-18 10:55:16 +00004463 HandleInvoke(invoke);
4464}
4465
Andreas Gampe878d58c2015-01-15 23:24:00 -08004466static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4467 if (invoke->GetLocations()->Intrinsified()) {
4468 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4469 intrinsic.Dispatch(invoke);
4470 return true;
4471 }
4472 return false;
4473}
4474
Vladimir Markodc151b22015-10-15 18:02:30 +01004475HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4476 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004477 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004478 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004479 return desired_dispatch_info;
4480}
4481
TatWai Chongd8c052a2016-11-02 16:12:48 +08004482Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4483 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004484 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004485 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4486 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004487 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4488 uint32_t offset =
4489 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004490 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004491 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004492 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004493 }
Vladimir Marko58155012015-08-19 12:49:41 +00004494 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004495 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004496 break;
4497 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4498 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004499 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004500 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004501 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4502 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004503 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004504 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004505 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004506 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004508 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004509 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004510 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004511 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004512 }
Vladimir Marko58155012015-08-19 12:49:41 +00004513 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004514 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004515 Register reg = XRegisterFrom(temp);
4516 Register method_reg;
4517 if (current_method.IsRegister()) {
4518 method_reg = XRegisterFrom(current_method);
4519 } else {
4520 DCHECK(invoke->GetLocations()->Intrinsified());
4521 DCHECK(!current_method.IsValid());
4522 method_reg = reg;
4523 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4524 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004525
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004526 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004527 __ Ldr(reg.X(),
4528 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004529 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004530 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004531 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4532 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004533 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4534 break;
4535 }
4536 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004537 return callee_method;
4538}
4539
4540void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4541 // All registers are assumed to be correctly set up.
4542 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004543
4544 switch (invoke->GetCodePtrLocation()) {
4545 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4546 __ Bl(&frame_entry_label_);
4547 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004548 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4549 // LR = callee_method->entry_point_from_quick_compiled_code_;
4550 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004551 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004552 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004553 {
4554 // To ensure that the pc position is recorded immediately after the `blr` instruction
4555 // BLR must be the last instruction emitted in this function.
4556 // Recording the pc will occur right after returning from this function.
4557 ExactAssemblyScope eas(GetVIXLAssembler(),
4558 kInstructionSize,
4559 CodeBufferCheckScope::kExactSize);
4560 // lr()
4561 __ blr(lr);
4562 }
Vladimir Marko58155012015-08-19 12:49:41 +00004563 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004564 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004565
Andreas Gampe878d58c2015-01-15 23:24:00 -08004566 DCHECK(!IsLeafMethod());
4567}
4568
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004569void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004570 // Use the calling convention instead of the location of the receiver, as
4571 // intrinsics may have put the receiver in a different register. In the intrinsics
4572 // slow path, the arguments have been moved to the right place, so here we are
4573 // guaranteed that the receiver is the first register of the calling convention.
4574 InvokeDexCallingConvention calling_convention;
4575 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576 Register temp = XRegisterFrom(temp_in);
4577 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4578 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4579 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004580 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004581
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004582 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004583
4584 {
4585 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4586 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4587 // /* HeapReference<Class> */ temp = receiver->klass_
4588 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4589 MaybeRecordImplicitNullCheck(invoke);
4590 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004591 // Instead of simply (possibly) unpoisoning `temp` here, we should
4592 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004593 // intermediate/temporary reference and because the current
4594 // concurrent copying collector keeps the from-space memory
4595 // intact/accessible until the end of the marking phase (the
4596 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004597 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4598 // temp = temp->GetMethodAt(method_offset);
4599 __ Ldr(temp, MemOperand(temp, method_offset));
4600 // lr = temp->GetEntryPoint();
4601 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004602 {
4603 // To ensure that the pc position is recorded immediately after the `blr` instruction
4604 // BLR should be the last instruction emitted in this function.
4605 // Recording the pc will occur right after returning from this function.
4606 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4607 // lr();
4608 __ blr(lr);
4609 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004610}
4611
Orion Hodsonac141392017-01-13 11:53:47 +00004612void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4613 HandleInvoke(invoke);
4614}
4615
4616void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4617 codegen_->GenerateInvokePolymorphicCall(invoke);
4618}
4619
Scott Wakeling97c72b72016-06-24 16:19:36 +01004620vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4621 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004622 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004623 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004624 return
4625 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004626}
4627
Scott Wakeling97c72b72016-06-24 16:19:36 +01004628vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4629 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004630 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004631 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004632 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004633}
4634
Vladimir Marko1998cd02017-01-13 13:02:58 +00004635vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4636 const DexFile& dex_file,
4637 dex::TypeIndex type_index,
4638 vixl::aarch64::Label* adrp_label) {
4639 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4640}
4641
Scott Wakeling97c72b72016-06-24 16:19:36 +01004642vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4643 const DexFile& dex_file,
4644 uint32_t element_offset,
4645 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004646 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4647}
4648
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004649vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4650 baker_read_barrier_patches_.emplace_back(custom_data);
4651 return &baker_read_barrier_patches_.back().label;
4652}
4653
Scott Wakeling97c72b72016-06-24 16:19:36 +01004654vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4655 const DexFile& dex_file,
4656 uint32_t offset_or_index,
4657 vixl::aarch64::Label* adrp_label,
4658 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004659 // Add a patch entry and return the label.
4660 patches->emplace_back(dex_file, offset_or_index);
4661 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004662 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004663 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4664 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4665 return label;
4666}
4667
Scott Wakeling97c72b72016-06-24 16:19:36 +01004668vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004669 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004670 return boot_image_string_patches_.GetOrCreate(
4671 StringReference(&dex_file, string_index),
4672 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4673}
4674
Scott Wakeling97c72b72016-06-24 16:19:36 +01004675vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004676 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004677 return boot_image_type_patches_.GetOrCreate(
4678 TypeReference(&dex_file, type_index),
4679 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4680}
4681
Scott Wakeling97c72b72016-06-24 16:19:36 +01004682vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4683 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004684 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685}
4686
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004687vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004688 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4689 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4690 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004691 return jit_string_patches_.GetOrCreate(
4692 StringReference(&dex_file, string_index),
4693 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4694}
4695
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004696vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004697 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4698 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4699 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004700 return jit_class_patches_.GetOrCreate(
4701 TypeReference(&dex_file, type_index),
4702 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4703}
4704
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4706 vixl::aarch64::Register reg) {
4707 DCHECK(reg.IsX());
4708 SingleEmissionCheckScope guard(GetVIXLAssembler());
4709 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004710 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004711}
4712
4713void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4714 vixl::aarch64::Register out,
4715 vixl::aarch64::Register base) {
4716 DCHECK(out.IsX());
4717 DCHECK(base.IsX());
4718 SingleEmissionCheckScope guard(GetVIXLAssembler());
4719 __ Bind(fixup_label);
4720 __ add(out, base, Operand(/* offset placeholder */ 0));
4721}
4722
4723void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4724 vixl::aarch64::Register out,
4725 vixl::aarch64::Register base) {
4726 DCHECK(base.IsX());
4727 SingleEmissionCheckScope guard(GetVIXLAssembler());
4728 __ Bind(fixup_label);
4729 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4730}
4731
4732template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4733inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4734 const ArenaDeque<PcRelativePatchInfo>& infos,
4735 ArenaVector<LinkerPatch>* linker_patches) {
4736 for (const PcRelativePatchInfo& info : infos) {
4737 linker_patches->push_back(Factory(info.label.GetLocation(),
4738 &info.target_dex_file,
4739 info.pc_insn_label->GetLocation(),
4740 info.offset_or_index));
4741 }
4742}
4743
Vladimir Marko58155012015-08-19 12:49:41 +00004744void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4745 DCHECK(linker_patches->empty());
4746 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004747 pc_relative_dex_cache_patches_.size() +
4748 boot_image_string_patches_.size() +
4749 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004750 boot_image_type_patches_.size() +
4751 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004752 type_bss_entry_patches_.size() +
4753 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004754 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004755 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004756 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004757 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004758 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004759 info.offset_or_index));
4760 }
4761 for (const auto& entry : boot_image_string_patches_) {
4762 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004763 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4764 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004765 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004766 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004767 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004768 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004769 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004770 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4771 linker_patches);
4772 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004773 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4774 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004775 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4776 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004777 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004778 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4779 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004780 for (const auto& entry : boot_image_type_patches_) {
4781 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004782 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4783 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004784 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004785 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004786 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004787 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4788 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4789 info.custom_data));
4790 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004791 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004792}
4793
Scott Wakeling97c72b72016-06-24 16:19:36 +01004794vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004795 Uint32ToLiteralMap* map) {
4796 return map->GetOrCreate(
4797 value,
4798 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4799}
4800
Scott Wakeling97c72b72016-06-24 16:19:36 +01004801vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004802 return uint64_literals_.GetOrCreate(
4803 value,
4804 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004805}
4806
Scott Wakeling97c72b72016-06-24 16:19:36 +01004807vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004808 MethodReference target_method,
4809 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004810 return map->GetOrCreate(
4811 target_method,
4812 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004813}
4814
Andreas Gampe878d58c2015-01-15 23:24:00 -08004815void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004816 // Explicit clinit checks triggered by static invokes must have been pruned by
4817 // art::PrepareForRegisterAllocation.
4818 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004819
Andreas Gampe878d58c2015-01-15 23:24:00 -08004820 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4821 return;
4822 }
4823
Artem Serov914d7a82017-02-07 14:33:49 +00004824 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4825 // are no pools emitted.
4826 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004827 LocationSummary* locations = invoke->GetLocations();
4828 codegen_->GenerateStaticOrDirectCall(
4829 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004830 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004831}
4832
4833void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004834 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4835 return;
4836 }
4837
Artem Serov914d7a82017-02-07 14:33:49 +00004838 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4839 // are no pools emitted.
4840 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004841 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004842 DCHECK(!codegen_->IsLeafMethod());
4843 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4844}
4845
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004846HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4847 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004848 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004849 case HLoadClass::LoadKind::kInvalid:
4850 LOG(FATAL) << "UNREACHABLE";
4851 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852 case HLoadClass::LoadKind::kReferrersClass:
4853 break;
4854 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4855 DCHECK(!GetCompilerOptions().GetCompilePic());
4856 break;
4857 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4858 DCHECK(GetCompilerOptions().GetCompilePic());
4859 break;
4860 case HLoadClass::LoadKind::kBootImageAddress:
4861 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004862 case HLoadClass::LoadKind::kBssEntry:
4863 DCHECK(!Runtime::Current()->UseJitCompilation());
4864 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004865 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866 DCHECK(Runtime::Current()->UseJitCompilation());
4867 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004868 case HLoadClass::LoadKind::kDexCacheViaMethod:
4869 break;
4870 }
4871 return desired_class_load_kind;
4872}
4873
Alexandre Rames67555f72014-11-18 10:55:16 +00004874void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004875 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4876 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004878 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004879 cls,
4880 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004881 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004882 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004883 return;
4884 }
Vladimir Marko41559982017-01-06 14:04:23 +00004885 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004886
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004887 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4888 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004889 ? LocationSummary::kCallOnSlowPath
4890 : LocationSummary::kNoCall;
4891 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004892 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004893 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004894 }
4895
Vladimir Marko41559982017-01-06 14:04:23 +00004896 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004897 locations->SetInAt(0, Location::RequiresRegister());
4898 }
4899 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004900 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4901 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4902 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004903 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004904 RegisterSet caller_saves = RegisterSet::Empty();
4905 InvokeRuntimeCallingConvention calling_convention;
4906 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4907 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4908 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4909 Primitive::kPrimNot).GetCode());
4910 locations->SetCustomSlowPathCallerSaves(caller_saves);
4911 } else {
4912 // For non-Baker read barrier we have a temp-clobbering call.
4913 }
4914 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004915}
4916
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004917// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4918// move.
4919void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004920 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4921 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4922 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004923 return;
4924 }
Vladimir Marko41559982017-01-06 14:04:23 +00004925 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004926
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004927 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004928 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004929 Register bss_entry_temp;
4930 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004931
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004932 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4933 ? kWithoutReadBarrier
4934 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004935 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004936 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937 case HLoadClass::LoadKind::kReferrersClass: {
4938 DCHECK(!cls->CanCallRuntime());
4939 DCHECK(!cls->MustGenerateClinitCheck());
4940 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4941 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004942 GenerateGcRootFieldLoad(cls,
4943 out_loc,
4944 current_method,
4945 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004946 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004947 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004948 break;
4949 }
4950 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004951 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004952 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4953 cls->GetTypeIndex()));
4954 break;
4955 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004956 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004957 // Add ADRP with its PC-relative type patch.
4958 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004959 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004960 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004961 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004962 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004963 vixl::aarch64::Label* add_label =
4964 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004965 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004966 break;
4967 }
4968 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004969 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004970 uint32_t address = dchecked_integral_cast<uint32_t>(
4971 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4972 DCHECK_NE(address, 0u);
4973 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004974 break;
4975 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004976 case HLoadClass::LoadKind::kBssEntry: {
4977 // Add ADRP with its PC-relative Class .bss entry patch.
4978 const DexFile& dex_file = cls->GetDexFile();
4979 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004980 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004981 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4982 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004983 // Add LDR with its PC-relative Class patch.
4984 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004985 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004986 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4987 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004988 out_loc,
4989 bss_entry_temp,
4990 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004991 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004992 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004993 generate_null_check = true;
4994 break;
4995 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004996 case HLoadClass::LoadKind::kJitTableAddress: {
4997 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4998 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004999 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005000 GenerateGcRootFieldLoad(cls,
5001 out_loc,
5002 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005003 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01005004 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00005005 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005006 break;
5007 }
Vladimir Marko41559982017-01-06 14:04:23 +00005008 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005009 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005010 LOG(FATAL) << "UNREACHABLE";
5011 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005012 }
5013
Vladimir Markoea4c1262017-02-06 19:59:33 +00005014 bool do_clinit = cls->MustGenerateClinitCheck();
5015 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005016 DCHECK(cls->CanCallRuntime());
5017 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00005018 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005019 codegen_->AddSlowPath(slow_path);
5020 if (generate_null_check) {
5021 __ Cbz(out, slow_path->GetEntryLabel());
5022 }
5023 if (cls->MustGenerateClinitCheck()) {
5024 GenerateClassInitializationCheck(slow_path, out);
5025 } else {
5026 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005027 }
5028 }
5029}
5030
David Brazdilcb1c0552015-08-04 16:22:25 +01005031static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005032 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005033}
5034
Alexandre Rames67555f72014-11-18 10:55:16 +00005035void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5036 LocationSummary* locations =
5037 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5038 locations->SetOut(Location::RequiresRegister());
5039}
5040
5041void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005042 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5043}
5044
5045void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5046 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5047}
5048
5049void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5050 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005051}
5052
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005053HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5054 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005055 switch (desired_string_load_kind) {
5056 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5057 DCHECK(!GetCompilerOptions().GetCompilePic());
5058 break;
5059 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5060 DCHECK(GetCompilerOptions().GetCompilePic());
5061 break;
5062 case HLoadString::LoadKind::kBootImageAddress:
5063 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005064 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005065 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005066 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005067 case HLoadString::LoadKind::kJitTableAddress:
5068 DCHECK(Runtime::Current()->UseJitCompilation());
5069 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005070 case HLoadString::LoadKind::kDexCacheViaMethod:
5071 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005072 }
5073 return desired_string_load_kind;
5074}
5075
Alexandre Rames67555f72014-11-18 10:55:16 +00005076void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005077 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005080 InvokeRuntimeCallingConvention calling_convention;
5081 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5082 } else {
5083 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005084 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5085 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005086 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005087 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005088 RegisterSet caller_saves = RegisterSet::Empty();
5089 InvokeRuntimeCallingConvention calling_convention;
5090 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5091 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5092 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5093 Primitive::kPrimNot).GetCode());
5094 locations->SetCustomSlowPathCallerSaves(caller_saves);
5095 } else {
5096 // For non-Baker read barrier we have a temp-clobbering call.
5097 }
5098 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005099 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005100}
5101
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005102// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5103// move.
5104void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005105 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005106 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005107
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005108 switch (load->GetLoadKind()) {
5109 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005110 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5111 load->GetStringIndex()));
5112 return; // No dex cache slow path.
5113 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005114 // Add ADRP with its PC-relative String patch.
5115 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005116 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005118 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005119 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005120 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005121 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005122 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005123 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005124 return; // No dex cache slow path.
5125 }
5126 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005127 uint32_t address = dchecked_integral_cast<uint32_t>(
5128 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5129 DCHECK_NE(address, 0u);
5130 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005131 return; // No dex cache slow path.
5132 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 case HLoadString::LoadKind::kBssEntry: {
5134 // Add ADRP with its PC-relative String .bss entry patch.
5135 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005136 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005137 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005138 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005139 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005140 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005141 // Add LDR with its PC-relative String patch.
5142 vixl::aarch64::Label* ldr_label =
5143 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005144 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005145 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005146 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005147 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005148 /* offset placeholder */ 0u,
5149 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005150 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005151 SlowPathCodeARM64* slow_path =
5152 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005153 codegen_->AddSlowPath(slow_path);
5154 __ Cbz(out.X(), slow_path->GetEntryLabel());
5155 __ Bind(slow_path->GetExitLabel());
5156 return;
5157 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005158 case HLoadString::LoadKind::kJitTableAddress: {
5159 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005160 load->GetStringIndex(),
5161 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005162 GenerateGcRootFieldLoad(load,
5163 out_loc,
5164 out.X(),
5165 /* offset */ 0,
5166 /* fixup_label */ nullptr,
5167 kCompilerReadBarrierOption);
5168 return;
5169 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005170 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005171 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005172 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005173
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005174 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005175 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005176 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005177 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005178 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5179 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005180}
5181
Alexandre Rames5319def2014-10-23 10:03:10 +01005182void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5183 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5184 locations->SetOut(Location::ConstantLocation(constant));
5185}
5186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005187void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005188 // Will be generated at use site.
5189}
5190
Alexandre Rames67555f72014-11-18 10:55:16 +00005191void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5192 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005193 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005194 InvokeRuntimeCallingConvention calling_convention;
5195 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5196}
5197
5198void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005199 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005200 instruction,
5201 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005202 if (instruction->IsEnter()) {
5203 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5204 } else {
5205 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5206 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005207}
5208
Alexandre Rames42d641b2014-10-27 14:00:51 +00005209void LocationsBuilderARM64::VisitMul(HMul* mul) {
5210 LocationSummary* locations =
5211 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5212 switch (mul->GetResultType()) {
5213 case Primitive::kPrimInt:
5214 case Primitive::kPrimLong:
5215 locations->SetInAt(0, Location::RequiresRegister());
5216 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005218 break;
5219
5220 case Primitive::kPrimFloat:
5221 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005222 locations->SetInAt(0, Location::RequiresFpuRegister());
5223 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005224 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005225 break;
5226
5227 default:
5228 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5229 }
5230}
5231
5232void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5233 switch (mul->GetResultType()) {
5234 case Primitive::kPrimInt:
5235 case Primitive::kPrimLong:
5236 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5237 break;
5238
5239 case Primitive::kPrimFloat:
5240 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005241 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005242 break;
5243
5244 default:
5245 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5246 }
5247}
5248
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005249void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5250 LocationSummary* locations =
5251 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5252 switch (neg->GetResultType()) {
5253 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005254 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005255 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005257 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005258
5259 case Primitive::kPrimFloat:
5260 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005261 locations->SetInAt(0, Location::RequiresFpuRegister());
5262 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005263 break;
5264
5265 default:
5266 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5267 }
5268}
5269
5270void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5271 switch (neg->GetResultType()) {
5272 case Primitive::kPrimInt:
5273 case Primitive::kPrimLong:
5274 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5275 break;
5276
5277 case Primitive::kPrimFloat:
5278 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005279 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005280 break;
5281
5282 default:
5283 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5284 }
5285}
5286
5287void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5288 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005289 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005290 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005291 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005292 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5293 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005294}
5295
5296void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005297 // Note: if heap poisoning is enabled, the entry point takes cares
5298 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005299 QuickEntrypointEnum entrypoint =
5300 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5301 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005302 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005303}
5304
Alexandre Rames5319def2014-10-23 10:03:10 +01005305void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5306 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005307 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005308 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005309 if (instruction->IsStringAlloc()) {
5310 locations->AddTemp(LocationFrom(kArtMethodRegister));
5311 } else {
5312 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005313 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005314 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5315}
5316
5317void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005318 // Note: if heap poisoning is enabled, the entry point takes cares
5319 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005320 if (instruction->IsStringAlloc()) {
5321 // String is allocated through StringFactory. Call NewEmptyString entry point.
5322 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005323 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005324 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5325 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005326
5327 {
5328 // Ensure the pc position is recorded immediately after the `blr` instruction.
5329 ExactAssemblyScope eas(GetVIXLAssembler(),
5330 kInstructionSize,
5331 CodeBufferCheckScope::kExactSize);
5332 __ blr(lr);
5333 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5334 }
David Brazdil6de19382016-01-08 17:37:10 +00005335 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005336 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005337 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005338 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005339}
5340
5341void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005343 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005345}
5346
5347void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005348 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005349 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005350 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005351 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005352 break;
5353
5354 default:
5355 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5356 }
5357}
5358
David Brazdil66d126e2015-04-03 16:02:44 +01005359void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5361 locations->SetInAt(0, Location::RequiresRegister());
5362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5363}
5364
5365void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005366 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005367}
5368
Alexandre Rames5319def2014-10-23 10:03:10 +01005369void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005370 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5371 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005372}
5373
Calin Juravle2ae48182016-03-16 14:05:09 +00005374void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5375 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005376 return;
5377 }
Artem Serov914d7a82017-02-07 14:33:49 +00005378 {
5379 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5380 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5381 Location obj = instruction->GetLocations()->InAt(0);
5382 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5383 RecordPcInfo(instruction, instruction->GetDexPc());
5384 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005385}
5386
Calin Juravle2ae48182016-03-16 14:05:09 +00005387void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005388 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005389 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005390
5391 LocationSummary* locations = instruction->GetLocations();
5392 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005393
5394 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005395}
5396
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005397void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005398 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005399}
5400
Alexandre Rames67555f72014-11-18 10:55:16 +00005401void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5402 HandleBinaryOp(instruction);
5403}
5404
5405void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5406 HandleBinaryOp(instruction);
5407}
5408
Alexandre Rames3e69f162014-12-10 10:36:50 +00005409void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5410 LOG(FATAL) << "Unreachable";
5411}
5412
5413void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5414 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5415}
5416
Alexandre Rames5319def2014-10-23 10:03:10 +01005417void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5419 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5420 if (location.IsStackSlot()) {
5421 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5422 } else if (location.IsDoubleStackSlot()) {
5423 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5424 }
5425 locations->SetOut(location);
5426}
5427
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005428void InstructionCodeGeneratorARM64::VisitParameterValue(
5429 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005430 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005431}
5432
5433void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5434 LocationSummary* locations =
5435 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005436 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005437}
5438
5439void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5440 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5441 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005442}
5443
5444void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5445 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005446 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005447 locations->SetInAt(i, Location::Any());
5448 }
5449 locations->SetOut(Location::Any());
5450}
5451
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005452void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005453 LOG(FATAL) << "Unreachable";
5454}
5455
Serban Constantinescu02164b32014-11-13 14:05:07 +00005456void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005457 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005458 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005459 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5460 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005461 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5462
5463 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005464 case Primitive::kPrimInt:
5465 case Primitive::kPrimLong:
5466 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005467 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5469 break;
5470
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005471 case Primitive::kPrimFloat:
5472 case Primitive::kPrimDouble: {
5473 InvokeRuntimeCallingConvention calling_convention;
5474 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5475 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5476 locations->SetOut(calling_convention.GetReturnLocation(type));
5477
5478 break;
5479 }
5480
Serban Constantinescu02164b32014-11-13 14:05:07 +00005481 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005482 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005483 }
5484}
5485
5486void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5487 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005488
Serban Constantinescu02164b32014-11-13 14:05:07 +00005489 switch (type) {
5490 case Primitive::kPrimInt:
5491 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005492 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005493 break;
5494 }
5495
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005496 case Primitive::kPrimFloat:
5497 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005498 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5499 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005500 if (type == Primitive::kPrimFloat) {
5501 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5502 } else {
5503 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5504 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005505 break;
5506 }
5507
Serban Constantinescu02164b32014-11-13 14:05:07 +00005508 default:
5509 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005510 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005511 }
5512}
5513
Calin Juravle27df7582015-04-17 19:12:31 +01005514void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5515 memory_barrier->SetLocations(nullptr);
5516}
5517
5518void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005519 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005520}
5521
Alexandre Rames5319def2014-10-23 10:03:10 +01005522void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5523 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5524 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005525 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005526}
5527
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005528void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005529 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005530}
5531
5532void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5533 instruction->SetLocations(nullptr);
5534}
5535
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005536void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005537 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005538}
5539
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005540void LocationsBuilderARM64::VisitRor(HRor* ror) {
5541 HandleBinaryOp(ror);
5542}
5543
5544void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5545 HandleBinaryOp(ror);
5546}
5547
Serban Constantinescu02164b32014-11-13 14:05:07 +00005548void LocationsBuilderARM64::VisitShl(HShl* shl) {
5549 HandleShift(shl);
5550}
5551
5552void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5553 HandleShift(shl);
5554}
5555
5556void LocationsBuilderARM64::VisitShr(HShr* shr) {
5557 HandleShift(shr);
5558}
5559
5560void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5561 HandleShift(shr);
5562}
5563
Alexandre Rames5319def2014-10-23 10:03:10 +01005564void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005565 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005566}
5567
5568void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005569 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005570}
5571
Alexandre Rames67555f72014-11-18 10:55:16 +00005572void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005573 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005574}
5575
5576void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005577 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005578}
5579
5580void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005581 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005582}
5583
Alexandre Rames67555f72014-11-18 10:55:16 +00005584void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005585 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005586}
5587
Calin Juravlee460d1d2015-09-29 04:52:17 +01005588void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5589 HUnresolvedInstanceFieldGet* instruction) {
5590 FieldAccessCallingConventionARM64 calling_convention;
5591 codegen_->CreateUnresolvedFieldLocationSummary(
5592 instruction, instruction->GetFieldType(), calling_convention);
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5596 HUnresolvedInstanceFieldGet* instruction) {
5597 FieldAccessCallingConventionARM64 calling_convention;
5598 codegen_->GenerateUnresolvedFieldAccess(instruction,
5599 instruction->GetFieldType(),
5600 instruction->GetFieldIndex(),
5601 instruction->GetDexPc(),
5602 calling_convention);
5603}
5604
5605void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5606 HUnresolvedInstanceFieldSet* instruction) {
5607 FieldAccessCallingConventionARM64 calling_convention;
5608 codegen_->CreateUnresolvedFieldLocationSummary(
5609 instruction, instruction->GetFieldType(), calling_convention);
5610}
5611
5612void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5613 HUnresolvedInstanceFieldSet* instruction) {
5614 FieldAccessCallingConventionARM64 calling_convention;
5615 codegen_->GenerateUnresolvedFieldAccess(instruction,
5616 instruction->GetFieldType(),
5617 instruction->GetFieldIndex(),
5618 instruction->GetDexPc(),
5619 calling_convention);
5620}
5621
5622void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5623 HUnresolvedStaticFieldGet* instruction) {
5624 FieldAccessCallingConventionARM64 calling_convention;
5625 codegen_->CreateUnresolvedFieldLocationSummary(
5626 instruction, instruction->GetFieldType(), calling_convention);
5627}
5628
5629void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5630 HUnresolvedStaticFieldGet* instruction) {
5631 FieldAccessCallingConventionARM64 calling_convention;
5632 codegen_->GenerateUnresolvedFieldAccess(instruction,
5633 instruction->GetFieldType(),
5634 instruction->GetFieldIndex(),
5635 instruction->GetDexPc(),
5636 calling_convention);
5637}
5638
5639void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5640 HUnresolvedStaticFieldSet* instruction) {
5641 FieldAccessCallingConventionARM64 calling_convention;
5642 codegen_->CreateUnresolvedFieldLocationSummary(
5643 instruction, instruction->GetFieldType(), calling_convention);
5644}
5645
5646void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5647 HUnresolvedStaticFieldSet* instruction) {
5648 FieldAccessCallingConventionARM64 calling_convention;
5649 codegen_->GenerateUnresolvedFieldAccess(instruction,
5650 instruction->GetFieldType(),
5651 instruction->GetFieldIndex(),
5652 instruction->GetDexPc(),
5653 calling_convention);
5654}
5655
Alexandre Rames5319def2014-10-23 10:03:10 +01005656void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005657 LocationSummary* locations =
5658 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005659 // In suspend check slow path, usually there are no caller-save registers at all.
5660 // If SIMD instructions are present, however, we force spilling all live SIMD
5661 // registers in full width (since the runtime only saves/restores lower part).
5662 locations->SetCustomSlowPathCallerSaves(
5663 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005664}
5665
5666void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005667 HBasicBlock* block = instruction->GetBlock();
5668 if (block->GetLoopInformation() != nullptr) {
5669 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5670 // The back edge will generate the suspend check.
5671 return;
5672 }
5673 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5674 // The goto will generate the suspend check.
5675 return;
5676 }
5677 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005678}
5679
Alexandre Rames67555f72014-11-18 10:55:16 +00005680void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5681 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005683 InvokeRuntimeCallingConvention calling_convention;
5684 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5685}
5686
5687void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005688 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005689 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005690}
5691
5692void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5693 LocationSummary* locations =
5694 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5695 Primitive::Type input_type = conversion->GetInputType();
5696 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005697 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005698 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5699 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5700 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5701 }
5702
Alexandre Rames542361f2015-01-29 16:57:31 +00005703 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005704 locations->SetInAt(0, Location::RequiresFpuRegister());
5705 } else {
5706 locations->SetInAt(0, Location::RequiresRegister());
5707 }
5708
Alexandre Rames542361f2015-01-29 16:57:31 +00005709 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005710 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5711 } else {
5712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5713 }
5714}
5715
5716void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5717 Primitive::Type result_type = conversion->GetResultType();
5718 Primitive::Type input_type = conversion->GetInputType();
5719
5720 DCHECK_NE(input_type, result_type);
5721
Alexandre Rames542361f2015-01-29 16:57:31 +00005722 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005723 int result_size = Primitive::ComponentSize(result_type);
5724 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005725 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005726 Register output = OutputRegister(conversion);
5727 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005728 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005729 // 'int' values are used directly as W registers, discarding the top
5730 // bits, so we don't need to sign-extend and can just perform a move.
5731 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5732 // top 32 bits of the target register. We theoretically could leave those
5733 // bits unchanged, but we would have to make sure that no code uses a
5734 // 32bit input value as a 64bit value assuming that the top 32 bits are
5735 // zero.
5736 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005737 } else if (result_type == Primitive::kPrimChar ||
5738 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5739 __ Ubfx(output,
5740 output.IsX() ? source.X() : source.W(),
5741 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005742 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005743 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005744 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005745 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005746 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005747 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005748 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5749 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005750 } else if (Primitive::IsFloatingPointType(result_type) &&
5751 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005752 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5753 } else {
5754 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5755 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005756 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005757}
Alexandre Rames67555f72014-11-18 10:55:16 +00005758
Serban Constantinescu02164b32014-11-13 14:05:07 +00005759void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5760 HandleShift(ushr);
5761}
5762
5763void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5764 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005765}
5766
5767void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5768 HandleBinaryOp(instruction);
5769}
5770
5771void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5772 HandleBinaryOp(instruction);
5773}
5774
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005775void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005776 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005777 LOG(FATAL) << "Unreachable";
5778}
5779
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005780void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005781 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005782 LOG(FATAL) << "Unreachable";
5783}
5784
Mark Mendellfe57faa2015-09-18 09:26:15 -04005785// Simple implementation of packed switch - generate cascaded compare/jumps.
5786void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5787 LocationSummary* locations =
5788 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5789 locations->SetInAt(0, Location::RequiresRegister());
5790}
5791
5792void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5793 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005794 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005795 Register value_reg = InputRegisterAt(switch_instr, 0);
5796 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5797
Zheng Xu3927c8b2015-11-18 17:46:25 +08005798 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005799 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005800 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5801 // make sure we don't emit it if the target may run out of range.
5802 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5803 // ranges and emit the tables only as required.
5804 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005805
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005806 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005807 // Current instruction id is an upper bound of the number of HIRs in the graph.
5808 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5809 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005810 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5811 Register temp = temps.AcquireW();
5812 __ Subs(temp, value_reg, Operand(lower_bound));
5813
Zheng Xu3927c8b2015-11-18 17:46:25 +08005814 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005815 // Jump to successors[0] if value == lower_bound.
5816 __ B(eq, codegen_->GetLabelOf(successors[0]));
5817 int32_t last_index = 0;
5818 for (; num_entries - last_index > 2; last_index += 2) {
5819 __ Subs(temp, temp, Operand(2));
5820 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5821 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5822 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5823 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5824 }
5825 if (num_entries - last_index == 2) {
5826 // The last missing case_value.
5827 __ Cmp(temp, Operand(1));
5828 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005829 }
5830
5831 // And the default for any other value.
5832 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5833 __ B(codegen_->GetLabelOf(default_block));
5834 }
5835 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005836 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005837
5838 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5839
5840 // Below instructions should use at most one blocked register. Since there are two blocked
5841 // registers, we are free to block one.
5842 Register temp_w = temps.AcquireW();
5843 Register index;
5844 // Remove the bias.
5845 if (lower_bound != 0) {
5846 index = temp_w;
5847 __ Sub(index, value_reg, Operand(lower_bound));
5848 } else {
5849 index = value_reg;
5850 }
5851
5852 // Jump to default block if index is out of the range.
5853 __ Cmp(index, Operand(num_entries));
5854 __ B(hs, codegen_->GetLabelOf(default_block));
5855
5856 // In current VIXL implementation, it won't require any blocked registers to encode the
5857 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5858 // register pressure.
5859 Register table_base = temps.AcquireX();
5860 // Load jump offset from the table.
5861 __ Adr(table_base, jump_table->GetTableStartLabel());
5862 Register jump_offset = temp_w;
5863 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5864
5865 // Jump to target block by branching to table_base(pc related) + offset.
5866 Register target_address = table_base;
5867 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5868 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005869 }
5870}
5871
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005872void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5873 HInstruction* instruction,
5874 Location out,
5875 uint32_t offset,
5876 Location maybe_temp,
5877 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005878 Primitive::Type type = Primitive::kPrimNot;
5879 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005880 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005881 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005882 if (kUseBakerReadBarrier) {
5883 // Load with fast path based Baker's read barrier.
5884 // /* HeapReference<Object> */ out = *(out + offset)
5885 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5886 out,
5887 out_reg,
5888 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005889 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005890 /* needs_null_check */ false,
5891 /* use_load_acquire */ false);
5892 } else {
5893 // Load with slow path based read barrier.
5894 // Save the value of `out` into `maybe_temp` before overwriting it
5895 // in the following move operation, as we will need it for the
5896 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005897 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005898 __ Mov(temp_reg, out_reg);
5899 // /* HeapReference<Object> */ out = *(out + offset)
5900 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5901 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5902 }
5903 } else {
5904 // Plain load with no read barrier.
5905 // /* HeapReference<Object> */ out = *(out + offset)
5906 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5907 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5908 }
5909}
5910
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005911void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5912 HInstruction* instruction,
5913 Location out,
5914 Location obj,
5915 uint32_t offset,
5916 Location maybe_temp,
5917 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005918 Primitive::Type type = Primitive::kPrimNot;
5919 Register out_reg = RegisterFrom(out, type);
5920 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005921 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005922 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005923 if (kUseBakerReadBarrier) {
5924 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005925 // /* HeapReference<Object> */ out = *(obj + offset)
5926 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5927 out,
5928 obj_reg,
5929 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005930 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005931 /* needs_null_check */ false,
5932 /* use_load_acquire */ false);
5933 } else {
5934 // Load with slow path based read barrier.
5935 // /* HeapReference<Object> */ out = *(obj + offset)
5936 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5937 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5938 }
5939 } else {
5940 // Plain load with no read barrier.
5941 // /* HeapReference<Object> */ out = *(obj + offset)
5942 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5943 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5944 }
5945}
5946
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005947void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5948 HInstruction* instruction,
5949 Location root,
5950 Register obj,
5951 uint32_t offset,
5952 vixl::aarch64::Label* fixup_label,
5953 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005954 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005955 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005956 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005957 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005958 if (kUseBakerReadBarrier) {
5959 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005960 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005961 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5962 !Runtime::Current()->UseJitCompilation()) {
5963 // Note that we do not actually check the value of `GetIsGcMarking()`
5964 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005965 // load into `temp` (actually IP1) the read barrier mark introspection
5966 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5967 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005968 //
5969 // We use link-time generated thunks for the slow path. That thunk
5970 // checks the reference and jumps to the entrypoint if needed.
5971 //
5972 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5973 // lr = &return_address;
5974 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5975 // if (temp != nullptr) {
5976 // goto gc_root_thunk<root_reg>(lr)
5977 // }
5978 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005979
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005980 UseScratchRegisterScope temps(GetVIXLAssembler());
5981 DCHECK(temps.IsAvailable(ip0));
5982 DCHECK(temps.IsAvailable(ip1));
5983 temps.Exclude(ip0, ip1);
5984 uint32_t custom_data =
5985 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5986 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005987
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005988 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5989 DCHECK_EQ(ip0.GetCode(), 16u);
5990 const int32_t entry_point_offset =
5991 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5992 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5993 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5994 vixl::aarch64::Label return_address;
5995 __ adr(lr, &return_address);
5996 if (fixup_label != nullptr) {
5997 __ Bind(fixup_label);
5998 }
5999 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
6000 "GC root LDR must be 2 instruction (8B) before the return address label.");
6001 __ ldr(root_reg, MemOperand(obj.X(), offset));
6002 __ Bind(cbnz_label);
6003 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6004 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006005 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006006 // Note that we do not actually check the value of
6007 // `GetIsGcMarking()` to decide whether to mark the loaded GC
6008 // root or not. Instead, we load into `temp` the read barrier
6009 // mark entry point corresponding to register `root`. If `temp`
6010 // is null, it means that `GetIsGcMarking()` is false, and vice
6011 // versa.
6012 //
6013 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6014 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6015 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6016 // // Slow path.
6017 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
6018 // }
Roland Levillain44015862016-01-22 11:47:17 +00006019
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006020 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6021 Register temp = lr;
6022 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6023 instruction, root, /* entrypoint */ LocationFrom(temp));
6024 codegen_->AddSlowPath(slow_path);
6025
6026 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6027 const int32_t entry_point_offset =
6028 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6029 // Loading the entrypoint does not require a load acquire since it is only changed when
6030 // threads are suspended or running a checkpoint.
6031 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6032
6033 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6034 if (fixup_label == nullptr) {
6035 __ Ldr(root_reg, MemOperand(obj, offset));
6036 } else {
6037 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6038 }
6039 static_assert(
6040 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6041 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6042 "have different sizes.");
6043 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6044 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6045 "have different sizes.");
6046
6047 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6048 // checking GetIsGcMarking.
6049 __ Cbnz(temp, slow_path->GetEntryLabel());
6050 __ Bind(slow_path->GetExitLabel());
6051 }
Roland Levillain44015862016-01-22 11:47:17 +00006052 } else {
6053 // GC root loaded through a slow path for read barriers other
6054 // than Baker's.
6055 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006056 if (fixup_label == nullptr) {
6057 __ Add(root_reg.X(), obj.X(), offset);
6058 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006059 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006060 }
Roland Levillain44015862016-01-22 11:47:17 +00006061 // /* mirror::Object* */ root = root->Read()
6062 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6063 }
6064 } else {
6065 // Plain GC root load with no read barrier.
6066 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006067 if (fixup_label == nullptr) {
6068 __ Ldr(root_reg, MemOperand(obj, offset));
6069 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006070 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006071 }
Roland Levillain44015862016-01-22 11:47:17 +00006072 // Note that GC roots are not affected by heap poisoning, thus we
6073 // do not have to unpoison `root_reg` here.
6074 }
6075}
6076
6077void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6078 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006079 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006080 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006081 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006082 bool needs_null_check,
6083 bool use_load_acquire) {
6084 DCHECK(kEmitCompilerReadBarrier);
6085 DCHECK(kUseBakerReadBarrier);
6086
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006087 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6088 !use_load_acquire &&
6089 !Runtime::Current()->UseJitCompilation()) {
6090 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006091 // to decide whether to mark the loaded reference or not. Instead, we
6092 // load into `temp` (actually IP1) the read barrier mark introspection
6093 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6094 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006095 //
6096 // We use link-time generated thunks for the slow path. That thunk checks
6097 // the holder and jumps to the entrypoint if needed. If the holder is not
6098 // gray, it creates a fake dependency and returns to the LDR instruction.
6099 //
6100 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006101 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006102 // if (temp != nullptr) {
6103 // goto field_thunk<holder_reg, base_reg>(lr)
6104 // }
6105 // not_gray_return_address:
6106 // // Original reference load. If the offset is too large to fit
6107 // // into LDR, we use an adjusted base register here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006108 // GcRoot<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006109 // gray_return_address:
6110
6111 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6112 Register base = obj;
6113 if (offset >= kReferenceLoadMinFarOffset) {
6114 DCHECK(maybe_temp.IsRegister());
6115 base = WRegisterFrom(maybe_temp);
6116 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6117 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6118 offset &= (kReferenceLoadMinFarOffset - 1u);
6119 }
6120 UseScratchRegisterScope temps(GetVIXLAssembler());
6121 DCHECK(temps.IsAvailable(ip0));
6122 DCHECK(temps.IsAvailable(ip1));
6123 temps.Exclude(ip0, ip1);
6124 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6125 base.GetCode(),
6126 obj.GetCode());
6127 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6128
6129 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6130 DCHECK_EQ(ip0.GetCode(), 16u);
6131 const int32_t entry_point_offset =
6132 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6133 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006134 EmissionCheckScope guard(GetVIXLAssembler(),
6135 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006136 vixl::aarch64::Label return_address;
6137 __ adr(lr, &return_address);
6138 __ Bind(cbnz_label);
6139 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006140 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6141 "Field LDR must be 1 instruction (4B) before the return address label; "
6142 " 2 instructions (8B) for heap poisoning.");
6143 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6144 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006145 if (needs_null_check) {
6146 MaybeRecordImplicitNullCheck(instruction);
6147 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006148 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006149 __ Bind(&return_address);
6150 return;
6151 }
6152
Roland Levillain44015862016-01-22 11:47:17 +00006153 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006154 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006155 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006156 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006157 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6158 ref,
6159 obj,
6160 offset,
6161 no_index,
6162 no_scale_factor,
6163 temp,
6164 needs_null_check,
6165 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006166}
6167
6168void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6169 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006170 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006171 uint32_t data_offset,
6172 Location index,
6173 Register temp,
6174 bool needs_null_check) {
6175 DCHECK(kEmitCompilerReadBarrier);
6176 DCHECK(kUseBakerReadBarrier);
6177
Vladimir Marko66d691d2017-04-07 17:53:39 +01006178 static_assert(
6179 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6180 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6181 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6182
6183 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6184 !Runtime::Current()->UseJitCompilation()) {
6185 // Note that we do not actually check the value of `GetIsGcMarking()`
6186 // to decide whether to mark the loaded reference or not. Instead, we
6187 // load into `temp` (actually IP1) the read barrier mark introspection
6188 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6189 // false, and vice versa.
6190 //
6191 // We use link-time generated thunks for the slow path. That thunk checks
6192 // the holder and jumps to the entrypoint if needed. If the holder is not
6193 // gray, it creates a fake dependency and returns to the LDR instruction.
6194 //
6195 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6196 // lr = &gray_return_address;
6197 // if (temp != nullptr) {
6198 // goto field_thunk<holder_reg, base_reg>(lr)
6199 // }
6200 // not_gray_return_address:
6201 // // Original reference load. If the offset is too large to fit
6202 // // into LDR, we use an adjusted base register here.
6203 // GcRoot<mirror::Object> reference = data[index];
6204 // gray_return_address:
6205
6206 DCHECK(index.IsValid());
6207 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6208 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6209
6210 UseScratchRegisterScope temps(GetVIXLAssembler());
6211 DCHECK(temps.IsAvailable(ip0));
6212 DCHECK(temps.IsAvailable(ip1));
6213 temps.Exclude(ip0, ip1);
6214 uint32_t custom_data =
6215 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6216 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6217
6218 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6219 DCHECK_EQ(ip0.GetCode(), 16u);
6220 const int32_t entry_point_offset =
6221 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6222 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6223 __ Add(temp.X(), obj.X(), Operand(data_offset));
6224 EmissionCheckScope guard(GetVIXLAssembler(),
6225 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6226 vixl::aarch64::Label return_address;
6227 __ adr(lr, &return_address);
6228 __ Bind(cbnz_label);
6229 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6230 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6231 "Array LDR must be 1 instruction (4B) before the return address label; "
6232 " 2 instructions (8B) for heap poisoning.");
6233 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6234 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6235 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6236 __ Bind(&return_address);
6237 return;
6238 }
6239
Roland Levillain44015862016-01-22 11:47:17 +00006240 // Array cells are never volatile variables, therefore array loads
6241 // never use Load-Acquire instructions on ARM64.
6242 const bool use_load_acquire = false;
6243
6244 // /* HeapReference<Object> */ ref =
6245 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006246 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6247 ref,
6248 obj,
6249 data_offset,
6250 index,
6251 scale_factor,
6252 temp,
6253 needs_null_check,
6254 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006255}
6256
6257void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6258 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006259 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006260 uint32_t offset,
6261 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006262 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006263 Register temp,
6264 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006265 bool use_load_acquire,
6266 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006267 DCHECK(kEmitCompilerReadBarrier);
6268 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006269 // If we are emitting an array load, we should not be using a
6270 // Load Acquire instruction. In other words:
6271 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6272 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006273
Roland Levillain54f869e2017-03-06 13:54:11 +00006274 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6275 // whether we need to enter the slow path to mark the reference.
6276 // Then, in the slow path, check the gray bit in the lock word of
6277 // the reference's holder (`obj`) to decide whether to mark `ref` or
6278 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006279 //
Roland Levillainba650a42017-03-06 13:52:32 +00006280 // Note that we do not actually check the value of `GetIsGcMarking()`;
6281 // instead, we load into `temp2` the read barrier mark entry point
6282 // corresponding to register `ref`. If `temp2` is null, it means
6283 // that `GetIsGcMarking()` is false, and vice versa.
6284 //
6285 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006286 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6287 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006288 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6289 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6290 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6291 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6292 // if (is_gray) {
6293 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6294 // }
6295 // } else {
6296 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006297 // }
Roland Levillain44015862016-01-22 11:47:17 +00006298
Roland Levillainba650a42017-03-06 13:52:32 +00006299 // Slow path marking the object `ref` when the GC is marking. The
6300 // entrypoint will already be loaded in `temp2`.
6301 Register temp2 = lr;
6302 Location temp2_loc = LocationFrom(temp2);
6303 SlowPathCodeARM64* slow_path;
6304 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006305 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6306 // only supports address of the form `obj + field_offset`, where
6307 // `obj` is a register and `field_offset` is a register. Thus
6308 // `offset` and `scale_factor` above are expected to be null in
6309 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006310 DCHECK_EQ(offset, 0u);
6311 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006312 Location field_offset = index;
6313 slow_path =
6314 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6315 instruction,
6316 ref,
6317 obj,
6318 offset,
6319 /* index */ field_offset,
6320 scale_factor,
6321 needs_null_check,
6322 use_load_acquire,
6323 temp,
6324 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006325 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006326 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6327 instruction,
6328 ref,
6329 obj,
6330 offset,
6331 index,
6332 scale_factor,
6333 needs_null_check,
6334 use_load_acquire,
6335 temp,
6336 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006337 }
6338 AddSlowPath(slow_path);
6339
6340 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6341 const int32_t entry_point_offset =
6342 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6343 // Loading the entrypoint does not require a load acquire since it is only changed when
6344 // threads are suspended or running a checkpoint.
6345 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006346 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6347 // checking GetIsGcMarking.
6348 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006349 // Fast path: just load the reference.
6350 GenerateRawReferenceLoad(
6351 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006352 __ Bind(slow_path->GetExitLabel());
6353}
6354
6355void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6356 Location ref,
6357 Register obj,
6358 uint32_t offset,
6359 Location index,
6360 size_t scale_factor,
6361 bool needs_null_check,
6362 bool use_load_acquire) {
6363 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006364 Primitive::Type type = Primitive::kPrimNot;
6365 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006366
Roland Levillainba650a42017-03-06 13:52:32 +00006367 // If needed, vixl::EmissionCheckScope guards are used to ensure
6368 // that no pools are emitted between the load (macro) instruction
6369 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006370
Roland Levillain44015862016-01-22 11:47:17 +00006371 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006372 // Load types involving an "index": ArrayGet,
6373 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6374 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006375 if (use_load_acquire) {
6376 // UnsafeGetObjectVolatile intrinsic case.
6377 // Register `index` is not an index in an object array, but an
6378 // offset to an object reference field within object `obj`.
6379 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6380 DCHECK(instruction->GetLocations()->Intrinsified());
6381 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6382 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006383 DCHECK_EQ(offset, 0u);
6384 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006385 DCHECK_EQ(needs_null_check, false);
6386 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006387 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6388 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006389 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006390 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6391 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006392 if (index.IsConstant()) {
6393 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006394 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006395 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006396 if (needs_null_check) {
6397 MaybeRecordImplicitNullCheck(instruction);
6398 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006399 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006400 UseScratchRegisterScope temps(GetVIXLAssembler());
6401 Register temp = temps.AcquireW();
6402 __ Add(temp, obj, offset);
6403 {
6404 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6405 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6406 if (needs_null_check) {
6407 MaybeRecordImplicitNullCheck(instruction);
6408 }
6409 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006410 }
Roland Levillain44015862016-01-22 11:47:17 +00006411 }
Roland Levillain44015862016-01-22 11:47:17 +00006412 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006413 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006414 MemOperand field = HeapOperand(obj, offset);
6415 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006416 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6417 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006418 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006419 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006420 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006421 if (needs_null_check) {
6422 MaybeRecordImplicitNullCheck(instruction);
6423 }
Roland Levillain44015862016-01-22 11:47:17 +00006424 }
6425 }
6426
6427 // Object* ref = ref_addr->AsMirrorPtr()
6428 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006429}
6430
6431void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6432 Location out,
6433 Location ref,
6434 Location obj,
6435 uint32_t offset,
6436 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006437 DCHECK(kEmitCompilerReadBarrier);
6438
Roland Levillain44015862016-01-22 11:47:17 +00006439 // Insert a slow path based read barrier *after* the reference load.
6440 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006441 // If heap poisoning is enabled, the unpoisoning of the loaded
6442 // reference will be carried out by the runtime within the slow
6443 // path.
6444 //
6445 // Note that `ref` currently does not get unpoisoned (when heap
6446 // poisoning is enabled), which is alright as the `ref` argument is
6447 // not used by the artReadBarrierSlow entry point.
6448 //
6449 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6450 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6451 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6452 AddSlowPath(slow_path);
6453
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006454 __ B(slow_path->GetEntryLabel());
6455 __ Bind(slow_path->GetExitLabel());
6456}
6457
Roland Levillain44015862016-01-22 11:47:17 +00006458void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6459 Location out,
6460 Location ref,
6461 Location obj,
6462 uint32_t offset,
6463 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006464 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006465 // Baker's read barriers shall be handled by the fast path
6466 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6467 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006468 // If heap poisoning is enabled, unpoisoning will be taken care of
6469 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006470 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006471 } else if (kPoisonHeapReferences) {
6472 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6473 }
6474}
6475
Roland Levillain44015862016-01-22 11:47:17 +00006476void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6477 Location out,
6478 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006479 DCHECK(kEmitCompilerReadBarrier);
6480
Roland Levillain44015862016-01-22 11:47:17 +00006481 // Insert a slow path based read barrier *after* the GC root load.
6482 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006483 // Note that GC roots are not affected by heap poisoning, so we do
6484 // not need to do anything special for this here.
6485 SlowPathCodeARM64* slow_path =
6486 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6487 AddSlowPath(slow_path);
6488
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006489 __ B(slow_path->GetEntryLabel());
6490 __ Bind(slow_path->GetExitLabel());
6491}
6492
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006493void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6494 LocationSummary* locations =
6495 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6496 locations->SetInAt(0, Location::RequiresRegister());
6497 locations->SetOut(Location::RequiresRegister());
6498}
6499
6500void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6501 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006502 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006503 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006504 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006505 __ Ldr(XRegisterFrom(locations->Out()),
6506 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006507 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006508 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006509 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006510 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6511 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006512 __ Ldr(XRegisterFrom(locations->Out()),
6513 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006514 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006515}
6516
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006517static void PatchJitRootUse(uint8_t* code,
6518 const uint8_t* roots_data,
6519 vixl::aarch64::Literal<uint32_t>* literal,
6520 uint64_t index_in_table) {
6521 uint32_t literal_offset = literal->GetOffset();
6522 uintptr_t address =
6523 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6524 uint8_t* data = code + literal_offset;
6525 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6526}
6527
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006528void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6529 for (const auto& entry : jit_string_patches_) {
6530 const auto& it = jit_string_roots_.find(entry.first);
6531 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006532 PatchJitRootUse(code, roots_data, entry.second, it->second);
6533 }
6534 for (const auto& entry : jit_class_patches_) {
6535 const auto& it = jit_class_roots_.find(entry.first);
6536 DCHECK(it != jit_class_roots_.end());
6537 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006538 }
6539}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006540
Alexandre Rames67555f72014-11-18 10:55:16 +00006541#undef __
6542#undef QUICK_ENTRY_POINT
6543
Alexandre Rames5319def2014-10-23 10:03:10 +01006544} // namespace arm64
6545} // namespace art