blob: 2d95a2ed87123fdef0d04cabd311124f14dd2409 [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.
Roland Levillainff487002017-03-07 16:50:01 +0000854 // The 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 Levillainff487002017-03-07 16:50:01 +00001005 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1006 //
1007 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1008 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1009 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1010 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1011 // if (is_gray) {
1012 // old_ref = ref;
1013 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1014 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1015 // }
1016
Roland Levillain54f869e2017-03-06 13:54:11 +00001017 // /* int32_t */ monitor = obj->monitor_
1018 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1019 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1020 if (needs_null_check_) {
1021 codegen->MaybeRecordImplicitNullCheck(instruction_);
1022 }
1023 // /* LockWord */ lock_word = LockWord(monitor)
1024 static_assert(sizeof(LockWord) == sizeof(int32_t),
1025 "art::LockWord and int32_t have different sizes.");
1026
1027 // Introduce a dependency on the lock_word including rb_state,
1028 // to prevent load-load reordering, and without using
1029 // a memory barrier (which would be more expensive).
1030 // `obj` is unchanged by this operation, but its value now depends
1031 // on `temp`.
1032 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1033
1034 // The actual reference load.
1035 // A possible implicit null check has already been handled above.
1036 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1037 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1038 ref_,
1039 obj_,
1040 offset_,
1041 index_,
1042 scale_factor_,
1043 /* needs_null_check */ false,
1044 use_load_acquire_);
1045
1046 // Mark the object `ref` when `obj` is gray.
1047 //
1048 // if (rb_state == ReadBarrier::GrayState())
1049 // ref = ReadBarrier::Mark(ref);
1050 //
1051 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1052 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1053 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1054 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1055
1056 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001057 // Note that we cannot use IP to save the old reference, as IP is
1058 // used internally by the ReadBarrierMarkRegX entry point, and we
1059 // need the old reference after the call to that entry point.
1060 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1061 __ Mov(temp_.W(), ref_reg);
1062
Roland Levillain54f869e2017-03-06 13:54:11 +00001063 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064
1065 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001066 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001067 //
1068 // Note that this field could also hold a different object, if
1069 // another thread had concurrently changed it. In that case, the
1070 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1071 // (CAS) operation below would abort the CAS, leaving the field
1072 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001073 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001074 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001075
1076 // Update the the holder's field atomically. This may fail if
1077 // mutator updates before us, but it's OK. This is achieved
1078 // using a strong compare-and-set (CAS) operation with relaxed
1079 // memory synchronization ordering, where the expected value is
1080 // the old reference and the desired value is the new reference.
1081
1082 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1083 UseScratchRegisterScope temps(masm);
1084
1085 // Convenience aliases.
1086 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001087 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088 Register expected = temp_.W();
1089 Register value = ref_reg;
1090 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1091 Register tmp_value = temps.AcquireW(); // Value in memory.
1092
1093 __ Add(tmp_ptr, base.X(), Operand(offset));
1094
1095 if (kPoisonHeapReferences) {
1096 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1097 if (value.Is(expected)) {
1098 // Do not poison `value`, as it is the same register as
1099 // `expected`, which has just been poisoned.
1100 } else {
1101 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1102 }
1103 }
1104
1105 // do {
1106 // tmp_value = [tmp_ptr] - expected;
1107 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1108
Roland Levillain24a4d112016-10-26 13:10:46 +01001109 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001110 __ Bind(&loop_head);
1111 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1112 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001113 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001114 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1115 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001116 __ B(&exit_loop);
1117 __ Bind(&comparison_failed);
1118 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ Bind(&exit_loop);
1120
1121 if (kPoisonHeapReferences) {
1122 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1123 if (value.Is(expected)) {
1124 // Do not unpoison `value`, as it is the same register as
1125 // `expected`, which has just been unpoisoned.
1126 } else {
1127 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1128 }
1129 }
1130
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001131 __ B(GetExitLabel());
1132 }
1133
1134 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001135 // The register containing the object holding the marked object reference field.
1136 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001137 // The offset, index and scale factor to access the reference in `obj_`.
1138 uint32_t offset_;
1139 Location index_;
1140 size_t scale_factor_;
1141 // Is a null check required?
1142 bool needs_null_check_;
1143 // Should this reference load use Load-Acquire semantics?
1144 bool use_load_acquire_;
1145 // A temporary register used to hold the lock word of `obj_`; and
1146 // also to hold the original reference value, when the reference is
1147 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001148 const Register temp_;
1149
Roland Levillain54f869e2017-03-06 13:54:11 +00001150 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001151};
1152
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001153// Slow path generating a read barrier for a heap reference.
1154class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1155 public:
1156 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1157 Location out,
1158 Location ref,
1159 Location obj,
1160 uint32_t offset,
1161 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001162 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001163 out_(out),
1164 ref_(ref),
1165 obj_(obj),
1166 offset_(offset),
1167 index_(index) {
1168 DCHECK(kEmitCompilerReadBarrier);
1169 // If `obj` is equal to `out` or `ref`, it means the initial object
1170 // has been overwritten by (or after) the heap object reference load
1171 // to be instrumented, e.g.:
1172 //
1173 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001174 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001175 //
1176 // In that case, we have lost the information about the original
1177 // object, and the emitted read barrier cannot work properly.
1178 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1179 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1180 }
1181
1182 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1183 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1184 LocationSummary* locations = instruction_->GetLocations();
1185 Primitive::Type type = Primitive::kPrimNot;
1186 DCHECK(locations->CanCall());
1187 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001188 DCHECK(instruction_->IsInstanceFieldGet() ||
1189 instruction_->IsStaticFieldGet() ||
1190 instruction_->IsArrayGet() ||
1191 instruction_->IsInstanceOf() ||
1192 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001193 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001194 << "Unexpected instruction in read barrier for heap reference slow path: "
1195 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001196 // The read barrier instrumentation of object ArrayGet
1197 // instructions does not support the HIntermediateAddress
1198 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001199 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001200 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001201
1202 __ Bind(GetEntryLabel());
1203
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001204 SaveLiveRegisters(codegen, locations);
1205
1206 // We may have to change the index's value, but as `index_` is a
1207 // constant member (like other "inputs" of this slow path),
1208 // introduce a copy of it, `index`.
1209 Location index = index_;
1210 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001211 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001212 if (instruction_->IsArrayGet()) {
1213 // Compute the actual memory offset and store it in `index`.
1214 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1215 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1216 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1217 // We are about to change the value of `index_reg` (see the
1218 // calls to vixl::MacroAssembler::Lsl and
1219 // vixl::MacroAssembler::Mov below), but it has
1220 // not been saved by the previous call to
1221 // art::SlowPathCode::SaveLiveRegisters, as it is a
1222 // callee-save register --
1223 // art::SlowPathCode::SaveLiveRegisters does not consider
1224 // callee-save registers, as it has been designed with the
1225 // assumption that callee-save registers are supposed to be
1226 // handled by the called function. So, as a callee-save
1227 // register, `index_reg` _would_ eventually be saved onto
1228 // the stack, but it would be too late: we would have
1229 // changed its value earlier. Therefore, we manually save
1230 // it here into another freely available register,
1231 // `free_reg`, chosen of course among the caller-save
1232 // registers (as a callee-save `free_reg` register would
1233 // exhibit the same problem).
1234 //
1235 // Note we could have requested a temporary register from
1236 // the register allocator instead; but we prefer not to, as
1237 // this is a slow path, and we know we can find a
1238 // caller-save register that is available.
1239 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1240 __ Mov(free_reg.W(), index_reg);
1241 index_reg = free_reg;
1242 index = LocationFrom(index_reg);
1243 } else {
1244 // The initial register stored in `index_` has already been
1245 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1246 // (as it is not a callee-save register), so we can freely
1247 // use it.
1248 }
1249 // Shifting the index value contained in `index_reg` by the scale
1250 // factor (2) cannot overflow in practice, as the runtime is
1251 // unable to allocate object arrays with a size larger than
1252 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1253 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1254 static_assert(
1255 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1256 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1257 __ Add(index_reg, index_reg, Operand(offset_));
1258 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001259 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1260 // intrinsics, `index_` is not shifted by a scale factor of 2
1261 // (as in the case of ArrayGet), as it is actually an offset
1262 // to an object field within an object.
1263 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001264 DCHECK(instruction_->GetLocations()->Intrinsified());
1265 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1266 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1267 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001268 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001269 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001270 }
1271 }
1272
1273 // We're moving two or three locations to locations that could
1274 // overlap, so we need a parallel move resolver.
1275 InvokeRuntimeCallingConvention calling_convention;
1276 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1277 parallel_move.AddMove(ref_,
1278 LocationFrom(calling_convention.GetRegisterAt(0)),
1279 type,
1280 nullptr);
1281 parallel_move.AddMove(obj_,
1282 LocationFrom(calling_convention.GetRegisterAt(1)),
1283 type,
1284 nullptr);
1285 if (index.IsValid()) {
1286 parallel_move.AddMove(index,
1287 LocationFrom(calling_convention.GetRegisterAt(2)),
1288 Primitive::kPrimInt,
1289 nullptr);
1290 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1291 } else {
1292 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1293 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1294 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001295 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001296 instruction_,
1297 instruction_->GetDexPc(),
1298 this);
1299 CheckEntrypointTypes<
1300 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1301 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1302
1303 RestoreLiveRegisters(codegen, locations);
1304
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001305 __ B(GetExitLabel());
1306 }
1307
1308 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1309
1310 private:
1311 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001312 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1313 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001314 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1315 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1316 return Register(VIXLRegCodeFromART(i), kXRegSize);
1317 }
1318 }
1319 // We shall never fail to find a free caller-save register, as
1320 // there are more than two core caller-save registers on ARM64
1321 // (meaning it is possible to find one which is different from
1322 // `ref` and `obj`).
1323 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1324 LOG(FATAL) << "Could not find a free register";
1325 UNREACHABLE();
1326 }
1327
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001328 const Location out_;
1329 const Location ref_;
1330 const Location obj_;
1331 const uint32_t offset_;
1332 // An additional location containing an index to an array.
1333 // Only used for HArrayGet and the UnsafeGetObject &
1334 // UnsafeGetObjectVolatile intrinsics.
1335 const Location index_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1338};
1339
1340// Slow path generating a read barrier for a GC root.
1341class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1342 public:
1343 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001344 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001345 DCHECK(kEmitCompilerReadBarrier);
1346 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001347
1348 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1349 LocationSummary* locations = instruction_->GetLocations();
1350 Primitive::Type type = Primitive::kPrimNot;
1351 DCHECK(locations->CanCall());
1352 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001353 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1354 << "Unexpected instruction in read barrier for GC root slow path: "
1355 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001356
1357 __ Bind(GetEntryLabel());
1358 SaveLiveRegisters(codegen, locations);
1359
1360 InvokeRuntimeCallingConvention calling_convention;
1361 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1362 // The argument of the ReadBarrierForRootSlow is not a managed
1363 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1364 // thus we need a 64-bit move here, and we cannot use
1365 //
1366 // arm64_codegen->MoveLocation(
1367 // LocationFrom(calling_convention.GetRegisterAt(0)),
1368 // root_,
1369 // type);
1370 //
1371 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1372 // reference type (`Primitive::kPrimNot`).
1373 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001374 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001375 instruction_,
1376 instruction_->GetDexPc(),
1377 this);
1378 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1379 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1380
1381 RestoreLiveRegisters(codegen, locations);
1382 __ B(GetExitLabel());
1383 }
1384
1385 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1386
1387 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001388 const Location out_;
1389 const Location root_;
1390
1391 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1392};
1393
Alexandre Rames5319def2014-10-23 10:03:10 +01001394#undef __
1395
1396Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1397 Location next_location;
1398 if (type == Primitive::kPrimVoid) {
1399 LOG(FATAL) << "Unreachable type " << type;
1400 }
1401
1402 if (Primitive::IsFloatingPointType(type) &&
1403 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001404 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1405 } else if (!Primitive::IsFloatingPointType(type) &&
1406 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1407 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1408 } else {
1409 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001410 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1411 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001412 }
1413
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001414 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001415 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001416 return next_location;
1417}
1418
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001419Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001420 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001421}
1422
Serban Constantinescu579885a2015-02-22 20:51:33 +00001423CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1424 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001425 const CompilerOptions& compiler_options,
1426 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001427 : CodeGenerator(graph,
1428 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001429 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001430 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001431 callee_saved_core_registers.GetList(),
1432 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001433 compiler_options,
1434 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001435 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001436 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001437 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001438 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001439 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001440 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001441 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001442 uint32_literals_(std::less<uint32_t>(),
1443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001444 uint64_literals_(std::less<uint64_t>(),
1445 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001446 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1447 boot_image_string_patches_(StringReferenceValueComparator(),
1448 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1449 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001450 boot_image_type_patches_(TypeReferenceValueComparator(),
1451 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1452 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001453 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001454 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001455 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001456 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1457 jit_class_patches_(TypeReferenceValueComparator(),
1458 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001459 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001460 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001461}
Alexandre Rames5319def2014-10-23 10:03:10 +01001462
Alexandre Rames67555f72014-11-18 10:55:16 +00001463#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001464
Zheng Xu3927c8b2015-11-18 17:46:25 +08001465void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001466 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001467 jump_table->EmitTable(this);
1468 }
1469}
1470
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001471void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001472 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001473 // Ensure we emit the literal pool.
1474 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001475
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001476 CodeGenerator::Finalize(allocator);
1477}
1478
Zheng Xuad4450e2015-04-17 18:48:56 +08001479void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1480 // Note: There are 6 kinds of moves:
1481 // 1. constant -> GPR/FPR (non-cycle)
1482 // 2. constant -> stack (non-cycle)
1483 // 3. GPR/FPR -> GPR/FPR
1484 // 4. GPR/FPR -> stack
1485 // 5. stack -> GPR/FPR
1486 // 6. stack -> stack (non-cycle)
1487 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1488 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1489 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1490 // dependency.
1491 vixl_temps_.Open(GetVIXLAssembler());
1492}
1493
1494void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1495 vixl_temps_.Close();
1496}
1497
1498Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001499 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1500 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1501 || kind == Location::kSIMDStackSlot);
1502 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1503 ? Location::kFpuRegister
1504 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001505 Location scratch = GetScratchLocation(kind);
1506 if (!scratch.Equals(Location::NoLocation())) {
1507 return scratch;
1508 }
1509 // Allocate from VIXL temp registers.
1510 if (kind == Location::kRegister) {
1511 scratch = LocationFrom(vixl_temps_.AcquireX());
1512 } else {
1513 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001514 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1515 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1516 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001517 }
1518 AddScratchLocation(scratch);
1519 return scratch;
1520}
1521
1522void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1523 if (loc.IsRegister()) {
1524 vixl_temps_.Release(XRegisterFrom(loc));
1525 } else {
1526 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001527 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001528 }
1529 RemoveScratchLocation(loc);
1530}
1531
Alexandre Rames3e69f162014-12-10 10:36:50 +00001532void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001533 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001534 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001535}
1536
Alexandre Rames5319def2014-10-23 10:03:10 +01001537void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001538 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001539 __ Bind(&frame_entry_label_);
1540
Serban Constantinescu02164b32014-11-13 14:05:07 +00001541 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1542 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001543 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001544 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001545 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001546 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001547 {
1548 // Ensure that between load and RecordPcInfo there are no pools emitted.
1549 ExactAssemblyScope eas(GetVIXLAssembler(),
1550 kInstructionSize,
1551 CodeBufferCheckScope::kExactSize);
1552 __ ldr(wzr, MemOperand(temp, 0));
1553 RecordPcInfo(nullptr, 0);
1554 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001555 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001556
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001557 if (!HasEmptyFrame()) {
1558 int frame_size = GetFrameSize();
1559 // Stack layout:
1560 // sp[frame_size - 8] : lr.
1561 // ... : other preserved core registers.
1562 // ... : other preserved fp registers.
1563 // ... : reserved frame space.
1564 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001565
1566 // Save the current method if we need it. Note that we do not
1567 // do this in HCurrentMethod, as the instruction might have been removed
1568 // in the SSA graph.
1569 if (RequiresCurrentMethod()) {
1570 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001571 } else {
1572 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001573 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001574 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001575 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1576 frame_size - GetCoreSpillSize());
1577 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1578 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001579
1580 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1581 // Initialize should_deoptimize flag to 0.
1582 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1583 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1584 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001585 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001586}
1587
1588void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001589 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001590 if (!HasEmptyFrame()) {
1591 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001592 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1593 frame_size - FrameEntrySpillSize());
1594 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1595 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001596 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001597 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001598 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001599 __ Ret();
1600 GetAssembler()->cfi().RestoreState();
1601 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001602}
1603
Scott Wakeling97c72b72016-06-24 16:19:36 +01001604CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001605 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001606 return CPURegList(CPURegister::kRegister, kXRegSize,
1607 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001608}
1609
Scott Wakeling97c72b72016-06-24 16:19:36 +01001610CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001611 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1612 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001613 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1614 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001615}
1616
Alexandre Rames5319def2014-10-23 10:03:10 +01001617void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1618 __ Bind(GetLabelOf(block));
1619}
1620
Calin Juravle175dc732015-08-25 15:42:32 +01001621void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1622 DCHECK(location.IsRegister());
1623 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1624}
1625
Calin Juravlee460d1d2015-09-29 04:52:17 +01001626void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1627 if (location.IsRegister()) {
1628 locations->AddTemp(location);
1629 } else {
1630 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1631 }
1632}
1633
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001634void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001635 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001636 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001637 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001638 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001639 if (value_can_be_null) {
1640 __ Cbz(value, &done);
1641 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001642 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001643 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001644 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001645 if (value_can_be_null) {
1646 __ Bind(&done);
1647 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001648}
1649
David Brazdil58282f42016-01-14 12:45:10 +00001650void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001651 // Blocked core registers:
1652 // lr : Runtime reserved.
1653 // tr : Runtime reserved.
1654 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1655 // ip1 : VIXL core temp.
1656 // ip0 : VIXL core temp.
1657 //
1658 // Blocked fp registers:
1659 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001660 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1661 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001663 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001664 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001665
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001666 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001667 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001668 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001669 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001670
David Brazdil58282f42016-01-14 12:45:10 +00001671 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001672 // Stubs do not save callee-save floating point registers. If the graph
1673 // is debuggable, we need to deal with these registers differently. For
1674 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001675 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1676 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001677 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001678 }
1679 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001680}
1681
Alexandre Rames3e69f162014-12-10 10:36:50 +00001682size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1683 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1684 __ Str(reg, MemOperand(sp, stack_index));
1685 return kArm64WordSize;
1686}
1687
1688size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1689 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1690 __ Ldr(reg, MemOperand(sp, stack_index));
1691 return kArm64WordSize;
1692}
1693
1694size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1695 FPRegister reg = FPRegister(reg_id, kDRegSize);
1696 __ Str(reg, MemOperand(sp, stack_index));
1697 return kArm64WordSize;
1698}
1699
1700size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1701 FPRegister reg = FPRegister(reg_id, kDRegSize);
1702 __ Ldr(reg, MemOperand(sp, stack_index));
1703 return kArm64WordSize;
1704}
1705
Alexandre Rames5319def2014-10-23 10:03:10 +01001706void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001707 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001708}
1709
1710void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001711 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001712}
1713
Alexandre Rames67555f72014-11-18 10:55:16 +00001714void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001715 if (constant->IsIntConstant()) {
1716 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1717 } else if (constant->IsLongConstant()) {
1718 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1719 } else if (constant->IsNullConstant()) {
1720 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001721 } else if (constant->IsFloatConstant()) {
1722 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1723 } else {
1724 DCHECK(constant->IsDoubleConstant());
1725 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1726 }
1727}
1728
Alexandre Rames3e69f162014-12-10 10:36:50 +00001729
1730static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1731 DCHECK(constant.IsConstant());
1732 HConstant* cst = constant.GetConstant();
1733 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001734 // Null is mapped to a core W register, which we associate with kPrimInt.
1735 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001736 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1737 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1738 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1739}
1740
Roland Levillain558dea12017-01-27 19:40:44 +00001741// Allocate a scratch register from the VIXL pool, querying first into
1742// the floating-point register pool, and then the the core register
1743// pool. This is essentially a reimplementation of
1744// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1745// using a different allocation strategy.
1746static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1747 vixl::aarch64::UseScratchRegisterScope* temps,
1748 int size_in_bits) {
1749 return masm->GetScratchFPRegisterList()->IsEmpty()
1750 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1751 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1752}
1753
Calin Juravlee460d1d2015-09-29 04:52:17 +01001754void CodeGeneratorARM64::MoveLocation(Location destination,
1755 Location source,
1756 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001757 if (source.Equals(destination)) {
1758 return;
1759 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001760
1761 // A valid move can always be inferred from the destination and source
1762 // locations. When moving from and to a register, the argument type can be
1763 // used to generate 32bit instead of 64bit moves. In debug mode we also
1764 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001765 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001766
1767 if (destination.IsRegister() || destination.IsFpuRegister()) {
1768 if (unspecified_type) {
1769 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1770 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001771 (src_cst != nullptr && (src_cst->IsIntConstant()
1772 || src_cst->IsFloatConstant()
1773 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001774 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001775 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001776 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001777 // If the source is a double stack slot or a 64bit constant, a 64bit
1778 // type is appropriate. Else the source is a register, and since the
1779 // type has not been specified, we chose a 64bit type to force a 64bit
1780 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001781 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001782 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001783 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001784 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1785 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1786 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001787 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1788 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1789 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001790 } else if (source.IsSIMDStackSlot()) {
1791 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001792 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001793 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001795 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001797 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001799 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001800 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1801 ? Primitive::kPrimLong
1802 : Primitive::kPrimInt;
1803 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1804 }
1805 } else {
1806 DCHECK(source.IsFpuRegister());
1807 if (destination.IsRegister()) {
1808 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1809 ? Primitive::kPrimDouble
1810 : Primitive::kPrimFloat;
1811 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1812 } else {
1813 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001814 if (GetGraph()->HasSIMD()) {
1815 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1816 } else {
1817 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1818 }
1819 }
1820 }
1821 } else if (destination.IsSIMDStackSlot()) {
1822 if (source.IsFpuRegister()) {
1823 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1824 } else {
1825 DCHECK(source.IsSIMDStackSlot());
1826 UseScratchRegisterScope temps(GetVIXLAssembler());
1827 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1828 Register temp = temps.AcquireX();
1829 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1830 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1831 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1832 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1833 } else {
1834 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1835 __ Ldr(temp, StackOperandFrom(source));
1836 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001837 }
1838 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001839 } else { // The destination is not a register. It must be a stack slot.
1840 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1841 if (source.IsRegister() || source.IsFpuRegister()) {
1842 if (unspecified_type) {
1843 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001844 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001845 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001846 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001847 }
1848 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001849 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1850 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1851 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001852 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001853 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1854 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001855 UseScratchRegisterScope temps(GetVIXLAssembler());
1856 HConstant* src_cst = source.GetConstant();
1857 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001858 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001859 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1860 ? Register(xzr)
1861 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001862 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001863 if (src_cst->IsIntConstant()) {
1864 temp = temps.AcquireW();
1865 } else if (src_cst->IsLongConstant()) {
1866 temp = temps.AcquireX();
1867 } else if (src_cst->IsFloatConstant()) {
1868 temp = temps.AcquireS();
1869 } else {
1870 DCHECK(src_cst->IsDoubleConstant());
1871 temp = temps.AcquireD();
1872 }
1873 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001874 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001875 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001876 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001878 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001880 // Use any scratch register (a core or a floating-point one)
1881 // from VIXL scratch register pools as a temporary.
1882 //
1883 // We used to only use the FP scratch register pool, but in some
1884 // rare cases the only register from this pool (D31) would
1885 // already be used (e.g. within a ParallelMove instruction, when
1886 // a move is blocked by a another move requiring a scratch FP
1887 // register, which would reserve D31). To prevent this issue, we
1888 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001889 //
1890 // Also, we start by asking for a FP scratch register first, as the
1891 // demand of scratch core registers is higher. This is why we
1892 // use AcquireFPOrCoreCPURegisterOfSize instead of
1893 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1894 // allocates core scratch registers first.
1895 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1896 GetVIXLAssembler(),
1897 &temps,
1898 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001899 __ Ldr(temp, StackOperandFrom(source));
1900 __ Str(temp, StackOperandFrom(destination));
1901 }
1902 }
1903}
1904
1905void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001906 CPURegister dst,
1907 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001908 switch (type) {
1909 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001910 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001911 break;
1912 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001913 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001914 break;
1915 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001916 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001917 break;
1918 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001919 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001920 break;
1921 case Primitive::kPrimInt:
1922 case Primitive::kPrimNot:
1923 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001924 case Primitive::kPrimFloat:
1925 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001926 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001927 __ Ldr(dst, src);
1928 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001929 case Primitive::kPrimVoid:
1930 LOG(FATAL) << "Unreachable type " << type;
1931 }
1932}
1933
Calin Juravle77520bc2015-01-12 18:45:46 +00001934void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001935 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001936 const MemOperand& src,
1937 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001938 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001939 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001940 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001941 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001942
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001943 DCHECK(!src.IsPreIndex());
1944 DCHECK(!src.IsPostIndex());
1945
1946 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001947 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001948 {
1949 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1950 MemOperand base = MemOperand(temp_base);
1951 switch (type) {
1952 case Primitive::kPrimBoolean:
1953 {
1954 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1955 __ ldarb(Register(dst), base);
1956 if (needs_null_check) {
1957 MaybeRecordImplicitNullCheck(instruction);
1958 }
1959 }
1960 break;
1961 case Primitive::kPrimByte:
1962 {
1963 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1964 __ ldarb(Register(dst), base);
1965 if (needs_null_check) {
1966 MaybeRecordImplicitNullCheck(instruction);
1967 }
1968 }
1969 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1970 break;
1971 case Primitive::kPrimChar:
1972 {
1973 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1974 __ ldarh(Register(dst), base);
1975 if (needs_null_check) {
1976 MaybeRecordImplicitNullCheck(instruction);
1977 }
1978 }
1979 break;
1980 case Primitive::kPrimShort:
1981 {
1982 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1983 __ ldarh(Register(dst), base);
1984 if (needs_null_check) {
1985 MaybeRecordImplicitNullCheck(instruction);
1986 }
1987 }
1988 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1989 break;
1990 case Primitive::kPrimInt:
1991 case Primitive::kPrimNot:
1992 case Primitive::kPrimLong:
1993 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1994 {
1995 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1996 __ ldar(Register(dst), base);
1997 if (needs_null_check) {
1998 MaybeRecordImplicitNullCheck(instruction);
1999 }
2000 }
2001 break;
2002 case Primitive::kPrimFloat:
2003 case Primitive::kPrimDouble: {
2004 DCHECK(dst.IsFPRegister());
2005 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002006
Artem Serov914d7a82017-02-07 14:33:49 +00002007 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2008 {
2009 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2010 __ ldar(temp, base);
2011 if (needs_null_check) {
2012 MaybeRecordImplicitNullCheck(instruction);
2013 }
2014 }
2015 __ Fmov(FPRegister(dst), temp);
2016 break;
Roland Levillain44015862016-01-22 11:47:17 +00002017 }
Artem Serov914d7a82017-02-07 14:33:49 +00002018 case Primitive::kPrimVoid:
2019 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002020 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002021 }
2022}
2023
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002024void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002025 CPURegister src,
2026 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002027 switch (type) {
2028 case Primitive::kPrimBoolean:
2029 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002030 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002031 break;
2032 case Primitive::kPrimChar:
2033 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002034 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002035 break;
2036 case Primitive::kPrimInt:
2037 case Primitive::kPrimNot:
2038 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002039 case Primitive::kPrimFloat:
2040 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002041 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002042 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002043 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002044 case Primitive::kPrimVoid:
2045 LOG(FATAL) << "Unreachable type " << type;
2046 }
2047}
2048
Artem Serov914d7a82017-02-07 14:33:49 +00002049void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2050 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002051 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002052 const MemOperand& dst,
2053 bool needs_null_check) {
2054 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002055 UseScratchRegisterScope temps(GetVIXLAssembler());
2056 Register temp_base = temps.AcquireX();
2057
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002058 DCHECK(!dst.IsPreIndex());
2059 DCHECK(!dst.IsPostIndex());
2060
2061 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002062 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002063 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002064 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002065 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002066 switch (type) {
2067 case Primitive::kPrimBoolean:
2068 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002069 {
2070 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2071 __ stlrb(Register(src), base);
2072 if (needs_null_check) {
2073 MaybeRecordImplicitNullCheck(instruction);
2074 }
2075 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002076 break;
2077 case Primitive::kPrimChar:
2078 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002079 {
2080 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2081 __ stlrh(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::kPrimInt:
2088 case Primitive::kPrimNot:
2089 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002090 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002091 {
2092 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2093 __ stlr(Register(src), base);
2094 if (needs_null_check) {
2095 MaybeRecordImplicitNullCheck(instruction);
2096 }
2097 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002098 break;
2099 case Primitive::kPrimFloat:
2100 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002101 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002102 Register temp_src;
2103 if (src.IsZero()) {
2104 // The zero register is used to avoid synthesizing zero constants.
2105 temp_src = Register(src);
2106 } else {
2107 DCHECK(src.IsFPRegister());
2108 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2109 __ Fmov(temp_src, FPRegister(src));
2110 }
Artem Serov914d7a82017-02-07 14:33:49 +00002111 {
2112 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2113 __ stlr(temp_src, base);
2114 if (needs_null_check) {
2115 MaybeRecordImplicitNullCheck(instruction);
2116 }
2117 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002118 break;
2119 }
2120 case Primitive::kPrimVoid:
2121 LOG(FATAL) << "Unreachable type " << type;
2122 }
2123}
2124
Calin Juravle175dc732015-08-25 15:42:32 +01002125void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2126 HInstruction* instruction,
2127 uint32_t dex_pc,
2128 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002129 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002130
2131 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2132 {
2133 // Ensure the pc position is recorded immediately after the `blr` instruction.
2134 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2135 __ blr(lr);
2136 if (EntrypointRequiresStackMap(entrypoint)) {
2137 RecordPcInfo(instruction, dex_pc, slow_path);
2138 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002139 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002140}
2141
Roland Levillaindec8f632016-07-22 17:10:06 +01002142void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2143 HInstruction* instruction,
2144 SlowPathCode* slow_path) {
2145 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002146 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2147 __ Blr(lr);
2148}
2149
Alexandre Rames67555f72014-11-18 10:55:16 +00002150void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002151 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002152 UseScratchRegisterScope temps(GetVIXLAssembler());
2153 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002154 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2155
Serban Constantinescu02164b32014-11-13 14:05:07 +00002156 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002157 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2158 __ Add(temp, class_reg, status_offset);
2159 __ Ldar(temp, HeapOperand(temp));
2160 __ Cmp(temp, mirror::Class::kStatusInitialized);
2161 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002162 __ Bind(slow_path->GetExitLabel());
2163}
Alexandre Rames5319def2014-10-23 10:03:10 +01002164
Roland Levillain44015862016-01-22 11:47:17 +00002165void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002166 BarrierType type = BarrierAll;
2167
2168 switch (kind) {
2169 case MemBarrierKind::kAnyAny:
2170 case MemBarrierKind::kAnyStore: {
2171 type = BarrierAll;
2172 break;
2173 }
2174 case MemBarrierKind::kLoadAny: {
2175 type = BarrierReads;
2176 break;
2177 }
2178 case MemBarrierKind::kStoreStore: {
2179 type = BarrierWrites;
2180 break;
2181 }
2182 default:
2183 LOG(FATAL) << "Unexpected memory barrier " << kind;
2184 }
2185 __ Dmb(InnerShareable, type);
2186}
2187
Serban Constantinescu02164b32014-11-13 14:05:07 +00002188void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2189 HBasicBlock* successor) {
2190 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002191 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2192 if (slow_path == nullptr) {
2193 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2194 instruction->SetSlowPath(slow_path);
2195 codegen_->AddSlowPath(slow_path);
2196 if (successor != nullptr) {
2197 DCHECK(successor->IsLoopHeader());
2198 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2199 }
2200 } else {
2201 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2202 }
2203
Serban Constantinescu02164b32014-11-13 14:05:07 +00002204 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2205 Register temp = temps.AcquireW();
2206
Andreas Gampe542451c2016-07-26 09:02:02 -07002207 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002208 if (successor == nullptr) {
2209 __ Cbnz(temp, slow_path->GetEntryLabel());
2210 __ Bind(slow_path->GetReturnLabel());
2211 } else {
2212 __ Cbz(temp, codegen_->GetLabelOf(successor));
2213 __ B(slow_path->GetEntryLabel());
2214 // slow_path will return to GetLabelOf(successor).
2215 }
2216}
2217
Alexandre Rames5319def2014-10-23 10:03:10 +01002218InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2219 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002220 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002221 assembler_(codegen->GetAssembler()),
2222 codegen_(codegen) {}
2223
2224#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002225 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002226
2227#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2228
2229enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002230 // Using a base helps identify when we hit such breakpoints.
2231 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002232#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2233 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2234#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2235};
2236
2237#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002238 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002239 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2240 } \
2241 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2243 locations->SetOut(Location::Any()); \
2244 }
2245 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2246#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2247
2248#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002249#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002250
Alexandre Rames67555f72014-11-18 10:55:16 +00002251void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002252 DCHECK_EQ(instr->InputCount(), 2U);
2253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2254 Primitive::Type type = instr->GetResultType();
2255 switch (type) {
2256 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002257 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002258 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002259 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002260 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002261 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002262
2263 case Primitive::kPrimFloat:
2264 case Primitive::kPrimDouble:
2265 locations->SetInAt(0, Location::RequiresFpuRegister());
2266 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002267 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002268 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002269
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002271 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 }
2273}
2274
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002275void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2276 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002277 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2278
2279 bool object_field_get_with_read_barrier =
2280 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002281 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002282 new (GetGraph()->GetArena()) LocationSummary(instruction,
2283 object_field_get_with_read_barrier ?
2284 LocationSummary::kCallOnSlowPath :
2285 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002286 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002287 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002288 // We need a temporary register for the read barrier marking slow
2289 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002290 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2291 !Runtime::Current()->UseJitCompilation() &&
2292 !field_info.IsVolatile()) {
2293 // If link-time thunks for the Baker read barrier are enabled, for AOT
2294 // non-volatile loads we need a temporary only if the offset is too big.
2295 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2296 locations->AddTemp(FixedTempLocation());
2297 }
2298 } else {
2299 locations->AddTemp(Location::RequiresRegister());
2300 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002301 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002302 locations->SetInAt(0, Location::RequiresRegister());
2303 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2304 locations->SetOut(Location::RequiresFpuRegister());
2305 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002306 // The output overlaps for an object field get when read barriers
2307 // are enabled: we do not want the load to overwrite the object's
2308 // location, as we need it to emit the read barrier.
2309 locations->SetOut(
2310 Location::RequiresRegister(),
2311 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002312 }
2313}
2314
2315void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2316 const FieldInfo& field_info) {
2317 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002318 LocationSummary* locations = instruction->GetLocations();
2319 Location base_loc = locations->InAt(0);
2320 Location out = locations->Out();
2321 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002322 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002323 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002324
Roland Levillain44015862016-01-22 11:47:17 +00002325 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2326 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002327 // /* HeapReference<Object> */ out = *(base + offset)
2328 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002329 Location maybe_temp =
2330 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002331 // Note that potential implicit null checks are handled in this
2332 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2333 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2334 instruction,
2335 out,
2336 base,
2337 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002338 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002339 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002340 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002341 } else {
2342 // General case.
2343 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002344 // Note that a potential implicit null check is handled in this
2345 // CodeGeneratorARM64::LoadAcquire call.
2346 // NB: LoadAcquire will record the pc info if needed.
2347 codegen_->LoadAcquire(
2348 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002349 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002350 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2351 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002352 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002353 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002354 }
Roland Levillain44015862016-01-22 11:47:17 +00002355 if (field_type == Primitive::kPrimNot) {
2356 // If read barriers are enabled, emit read barriers other than
2357 // Baker's using a slow path (and also unpoison the loaded
2358 // reference, if heap poisoning is enabled).
2359 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2360 }
Roland Levillain4d027112015-07-01 15:41:14 +01002361 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002362}
2363
2364void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2365 LocationSummary* locations =
2366 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2367 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002368 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2369 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2370 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002371 locations->SetInAt(1, Location::RequiresFpuRegister());
2372 } else {
2373 locations->SetInAt(1, Location::RequiresRegister());
2374 }
2375}
2376
2377void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002378 const FieldInfo& field_info,
2379 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002380 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2381
2382 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002383 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002384 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002385 Offset offset = field_info.GetFieldOffset();
2386 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002387
Roland Levillain4d027112015-07-01 15:41:14 +01002388 {
2389 // We use a block to end the scratch scope before the write barrier, thus
2390 // freeing the temporary registers so they can be used in `MarkGCCard`.
2391 UseScratchRegisterScope temps(GetVIXLAssembler());
2392
2393 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2394 DCHECK(value.IsW());
2395 Register temp = temps.AcquireW();
2396 __ Mov(temp, value.W());
2397 GetAssembler()->PoisonHeapReference(temp.W());
2398 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002399 }
Roland Levillain4d027112015-07-01 15:41:14 +01002400
2401 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002402 codegen_->StoreRelease(
2403 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002404 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002405 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2406 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002407 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2408 codegen_->MaybeRecordImplicitNullCheck(instruction);
2409 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002410 }
2411
2412 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002413 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002414 }
2415}
2416
Alexandre Rames67555f72014-11-18 10:55:16 +00002417void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002418 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002419
2420 switch (type) {
2421 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002422 case Primitive::kPrimLong: {
2423 Register dst = OutputRegister(instr);
2424 Register lhs = InputRegisterAt(instr, 0);
2425 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002426 if (instr->IsAdd()) {
2427 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002428 } else if (instr->IsAnd()) {
2429 __ And(dst, lhs, rhs);
2430 } else if (instr->IsOr()) {
2431 __ Orr(dst, lhs, rhs);
2432 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002433 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002434 } else if (instr->IsRor()) {
2435 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002436 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002437 __ Ror(dst, lhs, shift);
2438 } else {
2439 // Ensure shift distance is in the same size register as the result. If
2440 // we are rotating a long and the shift comes in a w register originally,
2441 // we don't need to sxtw for use as an x since the shift distances are
2442 // all & reg_bits - 1.
2443 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2444 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002445 } else {
2446 DCHECK(instr->IsXor());
2447 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002448 }
2449 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002450 }
2451 case Primitive::kPrimFloat:
2452 case Primitive::kPrimDouble: {
2453 FPRegister dst = OutputFPRegister(instr);
2454 FPRegister lhs = InputFPRegisterAt(instr, 0);
2455 FPRegister rhs = InputFPRegisterAt(instr, 1);
2456 if (instr->IsAdd()) {
2457 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002458 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002459 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002460 } else {
2461 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002462 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002463 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002465 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002466 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002467 }
2468}
2469
Serban Constantinescu02164b32014-11-13 14:05:07 +00002470void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2471 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2472
2473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2474 Primitive::Type type = instr->GetResultType();
2475 switch (type) {
2476 case Primitive::kPrimInt:
2477 case Primitive::kPrimLong: {
2478 locations->SetInAt(0, Location::RequiresRegister());
2479 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002481 break;
2482 }
2483 default:
2484 LOG(FATAL) << "Unexpected shift type " << type;
2485 }
2486}
2487
2488void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2489 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2490
2491 Primitive::Type type = instr->GetType();
2492 switch (type) {
2493 case Primitive::kPrimInt:
2494 case Primitive::kPrimLong: {
2495 Register dst = OutputRegister(instr);
2496 Register lhs = InputRegisterAt(instr, 0);
2497 Operand rhs = InputOperandAt(instr, 1);
2498 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002499 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002500 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002501 if (instr->IsShl()) {
2502 __ Lsl(dst, lhs, shift_value);
2503 } else if (instr->IsShr()) {
2504 __ Asr(dst, lhs, shift_value);
2505 } else {
2506 __ Lsr(dst, lhs, shift_value);
2507 }
2508 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002509 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002510
2511 if (instr->IsShl()) {
2512 __ Lsl(dst, lhs, rhs_reg);
2513 } else if (instr->IsShr()) {
2514 __ Asr(dst, lhs, rhs_reg);
2515 } else {
2516 __ Lsr(dst, lhs, rhs_reg);
2517 }
2518 }
2519 break;
2520 }
2521 default:
2522 LOG(FATAL) << "Unexpected shift operation type " << type;
2523 }
2524}
2525
Alexandre Rames5319def2014-10-23 10:03:10 +01002526void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002527 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002528}
2529
2530void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002531 HandleBinaryOp(instruction);
2532}
2533
2534void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2535 HandleBinaryOp(instruction);
2536}
2537
2538void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2539 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002540}
2541
Artem Serov7fc63502016-02-09 17:15:29 +00002542void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002543 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2544 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2545 locations->SetInAt(0, Location::RequiresRegister());
2546 // There is no immediate variant of negated bitwise instructions in AArch64.
2547 locations->SetInAt(1, Location::RequiresRegister());
2548 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2549}
2550
Artem Serov7fc63502016-02-09 17:15:29 +00002551void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002552 Register dst = OutputRegister(instr);
2553 Register lhs = InputRegisterAt(instr, 0);
2554 Register rhs = InputRegisterAt(instr, 1);
2555
2556 switch (instr->GetOpKind()) {
2557 case HInstruction::kAnd:
2558 __ Bic(dst, lhs, rhs);
2559 break;
2560 case HInstruction::kOr:
2561 __ Orn(dst, lhs, rhs);
2562 break;
2563 case HInstruction::kXor:
2564 __ Eon(dst, lhs, rhs);
2565 break;
2566 default:
2567 LOG(FATAL) << "Unreachable";
2568 }
2569}
2570
Anton Kirilov74234da2017-01-13 14:42:47 +00002571void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2572 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002573 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2574 instruction->GetType() == Primitive::kPrimLong);
2575 LocationSummary* locations =
2576 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2577 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2578 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2579 } else {
2580 locations->SetInAt(0, Location::RequiresRegister());
2581 }
2582 locations->SetInAt(1, Location::RequiresRegister());
2583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2584}
2585
Anton Kirilov74234da2017-01-13 14:42:47 +00002586void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2587 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002588 Primitive::Type type = instruction->GetType();
2589 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2590 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2591 Register out = OutputRegister(instruction);
2592 Register left;
2593 if (kind != HInstruction::kNeg) {
2594 left = InputRegisterAt(instruction, 0);
2595 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002596 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002597 // shifter operand operation, the IR generating `right_reg` (input to the type
2598 // conversion) can have a different type from the current instruction's type,
2599 // so we manually indicate the type.
2600 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002601 Operand right_operand(0);
2602
Anton Kirilov74234da2017-01-13 14:42:47 +00002603 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2604 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002605 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2606 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002607 right_operand = Operand(right_reg,
2608 helpers::ShiftFromOpKind(op_kind),
2609 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002610 }
2611
2612 // Logical binary operations do not support extension operations in the
2613 // operand. Note that VIXL would still manage if it was passed by generating
2614 // the extension as a separate instruction.
2615 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2616 DCHECK(!right_operand.IsExtendedRegister() ||
2617 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2618 kind != HInstruction::kNeg));
2619 switch (kind) {
2620 case HInstruction::kAdd:
2621 __ Add(out, left, right_operand);
2622 break;
2623 case HInstruction::kAnd:
2624 __ And(out, left, right_operand);
2625 break;
2626 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002627 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002628 __ Neg(out, right_operand);
2629 break;
2630 case HInstruction::kOr:
2631 __ Orr(out, left, right_operand);
2632 break;
2633 case HInstruction::kSub:
2634 __ Sub(out, left, right_operand);
2635 break;
2636 case HInstruction::kXor:
2637 __ Eor(out, left, right_operand);
2638 break;
2639 default:
2640 LOG(FATAL) << "Unexpected operation kind: " << kind;
2641 UNREACHABLE();
2642 }
2643}
2644
Artem Serov328429f2016-07-06 16:23:04 +01002645void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002646 LocationSummary* locations =
2647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2648 locations->SetInAt(0, Location::RequiresRegister());
2649 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002651}
2652
Roland Levillain19c54192016-11-04 13:44:09 +00002653void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002654 __ Add(OutputRegister(instruction),
2655 InputRegisterAt(instruction, 0),
2656 Operand(InputOperandAt(instruction, 1)));
2657}
2658
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002659void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002660 LocationSummary* locations =
2661 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002662 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2663 if (instr->GetOpKind() == HInstruction::kSub &&
2664 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002665 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002666 // Don't allocate register for Mneg instruction.
2667 } else {
2668 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2669 Location::RequiresRegister());
2670 }
2671 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2672 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2674}
2675
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002676void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002677 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002678 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2679 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002680
2681 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2682 // This fixup should be carried out for all multiply-accumulate instructions:
2683 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2684 if (instr->GetType() == Primitive::kPrimLong &&
2685 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2686 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002687 vixl::aarch64::Instruction* prev =
2688 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002689 if (prev->IsLoadOrStore()) {
2690 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002691 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002692 __ nop();
2693 }
2694 }
2695
2696 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002697 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002698 __ Madd(res, mul_left, mul_right, accumulator);
2699 } else {
2700 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002701 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002702 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002703 __ Mneg(res, mul_left, mul_right);
2704 } else {
2705 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2706 __ Msub(res, mul_left, mul_right, accumulator);
2707 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002708 }
2709}
2710
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002711void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002712 bool object_array_get_with_read_barrier =
2713 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002714 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002715 new (GetGraph()->GetArena()) LocationSummary(instruction,
2716 object_array_get_with_read_barrier ?
2717 LocationSummary::kCallOnSlowPath :
2718 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002719 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002720 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002721 // We need a temporary register for the read barrier marking slow
2722 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002723 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2724 !Runtime::Current()->UseJitCompilation() &&
2725 instruction->GetIndex()->IsConstant()) {
2726 // Array loads with constant index are treated as field loads.
2727 // If link-time thunks for the Baker read barrier are enabled, for AOT
2728 // constant index loads we need a temporary only if the offset is too big.
2729 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2730 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2731 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2732 if (offset >= kReferenceLoadMinFarOffset) {
2733 locations->AddTemp(FixedTempLocation());
2734 }
2735 } else {
2736 locations->AddTemp(Location::RequiresRegister());
2737 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002738 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002739 locations->SetInAt(0, Location::RequiresRegister());
2740 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002741 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2742 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2743 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002744 // The output overlaps in the case of an object array get with
2745 // read barriers enabled: we do not want the move to overwrite the
2746 // array's location, as we need it to emit the read barrier.
2747 locations->SetOut(
2748 Location::RequiresRegister(),
2749 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002750 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002751}
2752
2753void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002754 Primitive::Type type = instruction->GetType();
2755 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002756 LocationSummary* locations = instruction->GetLocations();
2757 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002758 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002759 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002760 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2761 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002762 MacroAssembler* masm = GetVIXLAssembler();
2763 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002764
Roland Levillain19c54192016-11-04 13:44:09 +00002765 // The read barrier instrumentation of object ArrayGet instructions
2766 // does not support the HIntermediateAddress instruction.
2767 DCHECK(!((type == Primitive::kPrimNot) &&
2768 instruction->GetArray()->IsIntermediateAddress() &&
2769 kEmitCompilerReadBarrier));
2770
Roland Levillain44015862016-01-22 11:47:17 +00002771 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2772 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002773 // Note that a potential implicit null check is handled in the
2774 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002775 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002776 if (index.IsConstant()) {
2777 // Array load with a constant index can be treated as a field load.
2778 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2779 Location maybe_temp =
2780 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2781 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2782 out,
2783 obj.W(),
2784 offset,
2785 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002786 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002787 /* use_load_acquire */ false);
2788 } else {
2789 Register temp = WRegisterFrom(locations->GetTemp(0));
2790 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002791 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002792 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002793 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002794 // General case.
2795 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002796 Register length;
2797 if (maybe_compressed_char_at) {
2798 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2799 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002800 {
2801 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2802 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2803
2804 if (instruction->GetArray()->IsIntermediateAddress()) {
2805 DCHECK_LT(count_offset, offset);
2806 int64_t adjusted_offset =
2807 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2808 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2809 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2810 } else {
2811 __ Ldr(length, HeapOperand(obj, count_offset));
2812 }
2813 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002814 }
jessicahandojo05765752016-09-09 19:01:32 -07002815 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002816 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002817 if (maybe_compressed_char_at) {
2818 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002819 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2820 "Expecting 0=compressed, 1=uncompressed");
2821 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002822 __ Ldrb(Register(OutputCPURegister(instruction)),
2823 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2824 __ B(&done);
2825 __ Bind(&uncompressed_load);
2826 __ Ldrh(Register(OutputCPURegister(instruction)),
2827 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2828 __ Bind(&done);
2829 } else {
2830 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2831 source = HeapOperand(obj, offset);
2832 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002833 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002834 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002835 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002836 // We do not need to compute the intermediate address from the array: the
2837 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002838 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002839 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002840 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002841 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2842 }
2843 temp = obj;
2844 } else {
2845 __ Add(temp, obj, offset);
2846 }
jessicahandojo05765752016-09-09 19:01:32 -07002847 if (maybe_compressed_char_at) {
2848 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002849 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2850 "Expecting 0=compressed, 1=uncompressed");
2851 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002852 __ Ldrb(Register(OutputCPURegister(instruction)),
2853 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2854 __ B(&done);
2855 __ Bind(&uncompressed_load);
2856 __ Ldrh(Register(OutputCPURegister(instruction)),
2857 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2858 __ Bind(&done);
2859 } else {
2860 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2861 }
Roland Levillain44015862016-01-22 11:47:17 +00002862 }
jessicahandojo05765752016-09-09 19:01:32 -07002863 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002864 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2865 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002866 codegen_->Load(type, OutputCPURegister(instruction), source);
2867 codegen_->MaybeRecordImplicitNullCheck(instruction);
2868 }
Roland Levillain44015862016-01-22 11:47:17 +00002869
2870 if (type == Primitive::kPrimNot) {
2871 static_assert(
2872 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2873 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2874 Location obj_loc = locations->InAt(0);
2875 if (index.IsConstant()) {
2876 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2877 } else {
2878 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2879 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002880 }
Roland Levillain4d027112015-07-01 15:41:14 +01002881 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002882}
2883
Alexandre Rames5319def2014-10-23 10:03:10 +01002884void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2885 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2886 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002888}
2889
2890void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002891 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002892 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002893 {
2894 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2895 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2896 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2897 codegen_->MaybeRecordImplicitNullCheck(instruction);
2898 }
jessicahandojo05765752016-09-09 19:01:32 -07002899 // Mask out compression flag from String's array length.
2900 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002901 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002902 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002903}
2904
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002905void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002906 Primitive::Type value_type = instruction->GetComponentType();
2907
2908 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2910 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002911 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002912 LocationSummary::kCallOnSlowPath :
2913 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002914 locations->SetInAt(0, Location::RequiresRegister());
2915 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002916 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2917 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2918 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002919 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002920 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002921 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002922 }
2923}
2924
2925void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2926 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002927 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002928 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002929 bool needs_write_barrier =
2930 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002931
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002932 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002933 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002934 CPURegister source = value;
2935 Location index = locations->InAt(1);
2936 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2937 MemOperand destination = HeapOperand(array);
2938 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002939
2940 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002941 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002942 if (index.IsConstant()) {
2943 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2944 destination = HeapOperand(array, offset);
2945 } else {
2946 UseScratchRegisterScope temps(masm);
2947 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002948 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002949 // We do not need to compute the intermediate address from the array: the
2950 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002951 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002952 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002953 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002954 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2955 }
2956 temp = array;
2957 } else {
2958 __ Add(temp, array, offset);
2959 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002960 destination = HeapOperand(temp,
2961 XRegisterFrom(index),
2962 LSL,
2963 Primitive::ComponentSizeShift(value_type));
2964 }
Artem Serov914d7a82017-02-07 14:33:49 +00002965 {
2966 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2967 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2968 codegen_->Store(value_type, value, destination);
2969 codegen_->MaybeRecordImplicitNullCheck(instruction);
2970 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002971 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002972 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002973 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002974 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002975 {
2976 // We use a block to end the scratch scope before the write barrier, thus
2977 // freeing the temporary registers so they can be used in `MarkGCCard`.
2978 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002979 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002980 if (index.IsConstant()) {
2981 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002982 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002983 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002984 destination = HeapOperand(temp,
2985 XRegisterFrom(index),
2986 LSL,
2987 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002988 }
2989
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002990 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2991 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2992 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2993
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002994 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002995 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2996 codegen_->AddSlowPath(slow_path);
2997 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002998 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002999 __ Cbnz(Register(value), &non_zero);
3000 if (!index.IsConstant()) {
3001 __ Add(temp, array, offset);
3002 }
Artem Serov914d7a82017-02-07 14:33:49 +00003003 {
3004 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3005 // emitted.
3006 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3007 __ Str(wzr, destination);
3008 codegen_->MaybeRecordImplicitNullCheck(instruction);
3009 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003010 __ B(&done);
3011 __ Bind(&non_zero);
3012 }
3013
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003014 // Note that when Baker read barriers are enabled, the type
3015 // checks are performed without read barriers. This is fine,
3016 // even in the case where a class object is in the from-space
3017 // after the flip, as a comparison involving such a type would
3018 // not produce a false positive; it may of course produce a
3019 // false negative, in which case we would take the ArraySet
3020 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003021
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003022 Register temp2 = temps.AcquireSameSizeAs(array);
3023 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003024 {
3025 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3026 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3027 __ Ldr(temp, HeapOperand(array, class_offset));
3028 codegen_->MaybeRecordImplicitNullCheck(instruction);
3029 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003030 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003031
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003032 // /* HeapReference<Class> */ temp = temp->component_type_
3033 __ Ldr(temp, HeapOperand(temp, component_offset));
3034 // /* HeapReference<Class> */ temp2 = value->klass_
3035 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3036 // If heap poisoning is enabled, no need to unpoison `temp`
3037 // nor `temp2`, as we are comparing two poisoned references.
3038 __ Cmp(temp, temp2);
3039 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003040
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003041 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3042 vixl::aarch64::Label do_put;
3043 __ B(eq, &do_put);
3044 // If heap poisoning is enabled, the `temp` reference has
3045 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003046 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3047
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003048 // /* HeapReference<Class> */ temp = temp->super_class_
3049 __ Ldr(temp, HeapOperand(temp, super_offset));
3050 // If heap poisoning is enabled, no need to unpoison
3051 // `temp`, as we are comparing against null below.
3052 __ Cbnz(temp, slow_path->GetEntryLabel());
3053 __ Bind(&do_put);
3054 } else {
3055 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003056 }
3057 }
3058
3059 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003060 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003061 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003062 __ Mov(temp2, value.W());
3063 GetAssembler()->PoisonHeapReference(temp2);
3064 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003065 }
3066
3067 if (!index.IsConstant()) {
3068 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003069 } else {
3070 // We no longer need the `temp` here so release it as the store below may
3071 // need a scratch register (if the constant index makes the offset too large)
3072 // and the poisoned `source` could be using the other scratch register.
3073 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003074 }
Artem Serov914d7a82017-02-07 14:33:49 +00003075 {
3076 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3077 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3078 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003079
Artem Serov914d7a82017-02-07 14:33:49 +00003080 if (!may_need_runtime_call_for_type_check) {
3081 codegen_->MaybeRecordImplicitNullCheck(instruction);
3082 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003083 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003084 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003085
3086 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3087
3088 if (done.IsLinked()) {
3089 __ Bind(&done);
3090 }
3091
3092 if (slow_path != nullptr) {
3093 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003094 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003095 }
3096}
3097
Alexandre Rames67555f72014-11-18 10:55:16 +00003098void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003099 RegisterSet caller_saves = RegisterSet::Empty();
3100 InvokeRuntimeCallingConvention calling_convention;
3101 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3102 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3103 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003104 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003105 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003106}
3107
3108void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003109 BoundsCheckSlowPathARM64* slow_path =
3110 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003111 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003112 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3113 __ B(slow_path->GetEntryLabel(), hs);
3114}
3115
Alexandre Rames67555f72014-11-18 10:55:16 +00003116void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3117 LocationSummary* locations =
3118 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3119 locations->SetInAt(0, Location::RequiresRegister());
3120 if (check->HasUses()) {
3121 locations->SetOut(Location::SameAsFirstInput());
3122 }
3123}
3124
3125void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3126 // We assume the class is not null.
3127 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3128 check->GetLoadClass(), check, check->GetDexPc(), true);
3129 codegen_->AddSlowPath(slow_path);
3130 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3131}
3132
Roland Levillain1a653882016-03-18 18:05:57 +00003133static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3134 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3135 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3136}
3137
3138void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3139 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3140 Location rhs_loc = instruction->GetLocations()->InAt(1);
3141 if (rhs_loc.IsConstant()) {
3142 // 0.0 is the only immediate that can be encoded directly in
3143 // an FCMP instruction.
3144 //
3145 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3146 // specify that in a floating-point comparison, positive zero
3147 // and negative zero are considered equal, so we can use the
3148 // literal 0.0 for both cases here.
3149 //
3150 // Note however that some methods (Float.equal, Float.compare,
3151 // Float.compareTo, Double.equal, Double.compare,
3152 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3153 // StrictMath.min) consider 0.0 to be (strictly) greater than
3154 // -0.0. So if we ever translate calls to these methods into a
3155 // HCompare instruction, we must handle the -0.0 case with
3156 // care here.
3157 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3158 __ Fcmp(lhs_reg, 0.0);
3159 } else {
3160 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3161 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003162}
3163
Serban Constantinescu02164b32014-11-13 14:05:07 +00003164void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003165 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003166 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3167 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003168 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003169 case Primitive::kPrimBoolean:
3170 case Primitive::kPrimByte:
3171 case Primitive::kPrimShort:
3172 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003173 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003174 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003175 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003176 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178 break;
3179 }
3180 case Primitive::kPrimFloat:
3181 case Primitive::kPrimDouble: {
3182 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003183 locations->SetInAt(1,
3184 IsFloatingPointZeroConstant(compare->InputAt(1))
3185 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3186 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003187 locations->SetOut(Location::RequiresRegister());
3188 break;
3189 }
3190 default:
3191 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3192 }
3193}
3194
3195void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3196 Primitive::Type in_type = compare->InputAt(0)->GetType();
3197
3198 // 0 if: left == right
3199 // 1 if: left > right
3200 // -1 if: left < right
3201 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003202 case Primitive::kPrimBoolean:
3203 case Primitive::kPrimByte:
3204 case Primitive::kPrimShort:
3205 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003206 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003207 case Primitive::kPrimLong: {
3208 Register result = OutputRegister(compare);
3209 Register left = InputRegisterAt(compare, 0);
3210 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003211 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003212 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3213 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003214 break;
3215 }
3216 case Primitive::kPrimFloat:
3217 case Primitive::kPrimDouble: {
3218 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003219 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003220 __ Cset(result, ne);
3221 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003222 break;
3223 }
3224 default:
3225 LOG(FATAL) << "Unimplemented compare type " << in_type;
3226 }
3227}
3228
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003229void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003230 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003231
3232 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3233 locations->SetInAt(0, Location::RequiresFpuRegister());
3234 locations->SetInAt(1,
3235 IsFloatingPointZeroConstant(instruction->InputAt(1))
3236 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3237 : Location::RequiresFpuRegister());
3238 } else {
3239 // Integer cases.
3240 locations->SetInAt(0, Location::RequiresRegister());
3241 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3242 }
3243
David Brazdilb3e773e2016-01-26 11:28:37 +00003244 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003246 }
3247}
3248
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003249void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003250 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003251 return;
3252 }
3253
3254 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003255 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003256 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003257
Roland Levillain7f63c522015-07-13 15:54:55 +00003258 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003259 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003260 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003261 } else {
3262 // Integer cases.
3263 Register lhs = InputRegisterAt(instruction, 0);
3264 Operand rhs = InputOperandAt(instruction, 1);
3265 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003266 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003267 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003268}
3269
3270#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3271 M(Equal) \
3272 M(NotEqual) \
3273 M(LessThan) \
3274 M(LessThanOrEqual) \
3275 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003276 M(GreaterThanOrEqual) \
3277 M(Below) \
3278 M(BelowOrEqual) \
3279 M(Above) \
3280 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003281#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003282void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3283void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003284FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003285#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003286#undef FOR_EACH_CONDITION_INSTRUCTION
3287
Zheng Xuc6667102015-05-15 16:08:45 +08003288void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3289 DCHECK(instruction->IsDiv() || instruction->IsRem());
3290
3291 LocationSummary* locations = instruction->GetLocations();
3292 Location second = locations->InAt(1);
3293 DCHECK(second.IsConstant());
3294
3295 Register out = OutputRegister(instruction);
3296 Register dividend = InputRegisterAt(instruction, 0);
3297 int64_t imm = Int64FromConstant(second.GetConstant());
3298 DCHECK(imm == 1 || imm == -1);
3299
3300 if (instruction->IsRem()) {
3301 __ Mov(out, 0);
3302 } else {
3303 if (imm == 1) {
3304 __ Mov(out, dividend);
3305 } else {
3306 __ Neg(out, dividend);
3307 }
3308 }
3309}
3310
3311void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3312 DCHECK(instruction->IsDiv() || instruction->IsRem());
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location second = locations->InAt(1);
3316 DCHECK(second.IsConstant());
3317
3318 Register out = OutputRegister(instruction);
3319 Register dividend = InputRegisterAt(instruction, 0);
3320 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003321 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003322 int ctz_imm = CTZ(abs_imm);
3323
3324 UseScratchRegisterScope temps(GetVIXLAssembler());
3325 Register temp = temps.AcquireSameSizeAs(out);
3326
3327 if (instruction->IsDiv()) {
3328 __ Add(temp, dividend, abs_imm - 1);
3329 __ Cmp(dividend, 0);
3330 __ Csel(out, temp, dividend, lt);
3331 if (imm > 0) {
3332 __ Asr(out, out, ctz_imm);
3333 } else {
3334 __ Neg(out, Operand(out, ASR, ctz_imm));
3335 }
3336 } else {
3337 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3338 __ Asr(temp, dividend, bits - 1);
3339 __ Lsr(temp, temp, bits - ctz_imm);
3340 __ Add(out, dividend, temp);
3341 __ And(out, out, abs_imm - 1);
3342 __ Sub(out, out, temp);
3343 }
3344}
3345
3346void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3347 DCHECK(instruction->IsDiv() || instruction->IsRem());
3348
3349 LocationSummary* locations = instruction->GetLocations();
3350 Location second = locations->InAt(1);
3351 DCHECK(second.IsConstant());
3352
3353 Register out = OutputRegister(instruction);
3354 Register dividend = InputRegisterAt(instruction, 0);
3355 int64_t imm = Int64FromConstant(second.GetConstant());
3356
3357 Primitive::Type type = instruction->GetResultType();
3358 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3359
3360 int64_t magic;
3361 int shift;
3362 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3363
3364 UseScratchRegisterScope temps(GetVIXLAssembler());
3365 Register temp = temps.AcquireSameSizeAs(out);
3366
3367 // temp = get_high(dividend * magic)
3368 __ Mov(temp, magic);
3369 if (type == Primitive::kPrimLong) {
3370 __ Smulh(temp, dividend, temp);
3371 } else {
3372 __ Smull(temp.X(), dividend, temp);
3373 __ Lsr(temp.X(), temp.X(), 32);
3374 }
3375
3376 if (imm > 0 && magic < 0) {
3377 __ Add(temp, temp, dividend);
3378 } else if (imm < 0 && magic > 0) {
3379 __ Sub(temp, temp, dividend);
3380 }
3381
3382 if (shift != 0) {
3383 __ Asr(temp, temp, shift);
3384 }
3385
3386 if (instruction->IsDiv()) {
3387 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3388 } else {
3389 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3390 // TODO: Strength reduction for msub.
3391 Register temp_imm = temps.AcquireSameSizeAs(out);
3392 __ Mov(temp_imm, imm);
3393 __ Msub(out, temp, temp_imm, dividend);
3394 }
3395}
3396
3397void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3398 DCHECK(instruction->IsDiv() || instruction->IsRem());
3399 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003400 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003401
3402 LocationSummary* locations = instruction->GetLocations();
3403 Register out = OutputRegister(instruction);
3404 Location second = locations->InAt(1);
3405
3406 if (second.IsConstant()) {
3407 int64_t imm = Int64FromConstant(second.GetConstant());
3408
3409 if (imm == 0) {
3410 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3411 } else if (imm == 1 || imm == -1) {
3412 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003413 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003414 DivRemByPowerOfTwo(instruction);
3415 } else {
3416 DCHECK(imm <= -2 || imm >= 2);
3417 GenerateDivRemWithAnyConstant(instruction);
3418 }
3419 } else {
3420 Register dividend = InputRegisterAt(instruction, 0);
3421 Register divisor = InputRegisterAt(instruction, 1);
3422 if (instruction->IsDiv()) {
3423 __ Sdiv(out, dividend, divisor);
3424 } else {
3425 UseScratchRegisterScope temps(GetVIXLAssembler());
3426 Register temp = temps.AcquireSameSizeAs(out);
3427 __ Sdiv(temp, dividend, divisor);
3428 __ Msub(out, temp, divisor, dividend);
3429 }
3430 }
3431}
3432
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003433void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3434 LocationSummary* locations =
3435 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3436 switch (div->GetResultType()) {
3437 case Primitive::kPrimInt:
3438 case Primitive::kPrimLong:
3439 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003440 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3442 break;
3443
3444 case Primitive::kPrimFloat:
3445 case Primitive::kPrimDouble:
3446 locations->SetInAt(0, Location::RequiresFpuRegister());
3447 locations->SetInAt(1, Location::RequiresFpuRegister());
3448 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3449 break;
3450
3451 default:
3452 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3453 }
3454}
3455
3456void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3457 Primitive::Type type = div->GetResultType();
3458 switch (type) {
3459 case Primitive::kPrimInt:
3460 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003461 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003462 break;
3463
3464 case Primitive::kPrimFloat:
3465 case Primitive::kPrimDouble:
3466 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3467 break;
3468
3469 default:
3470 LOG(FATAL) << "Unexpected div type " << type;
3471 }
3472}
3473
Alexandre Rames67555f72014-11-18 10:55:16 +00003474void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003475 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003476 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003477}
3478
3479void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3480 SlowPathCodeARM64* slow_path =
3481 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3482 codegen_->AddSlowPath(slow_path);
3483 Location value = instruction->GetLocations()->InAt(0);
3484
Alexandre Rames3e69f162014-12-10 10:36:50 +00003485 Primitive::Type type = instruction->GetType();
3486
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003487 if (!Primitive::IsIntegralType(type)) {
3488 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003489 return;
3490 }
3491
Alexandre Rames67555f72014-11-18 10:55:16 +00003492 if (value.IsConstant()) {
3493 int64_t divisor = Int64ConstantFrom(value);
3494 if (divisor == 0) {
3495 __ B(slow_path->GetEntryLabel());
3496 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003497 // A division by a non-null constant is valid. We don't need to perform
3498 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003499 }
3500 } else {
3501 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3502 }
3503}
3504
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003505void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3506 LocationSummary* locations =
3507 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3508 locations->SetOut(Location::ConstantLocation(constant));
3509}
3510
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003511void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3512 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003513 // Will be generated at use site.
3514}
3515
Alexandre Rames5319def2014-10-23 10:03:10 +01003516void LocationsBuilderARM64::VisitExit(HExit* exit) {
3517 exit->SetLocations(nullptr);
3518}
3519
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003520void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003521}
3522
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003523void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3524 LocationSummary* locations =
3525 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3526 locations->SetOut(Location::ConstantLocation(constant));
3527}
3528
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003529void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003530 // Will be generated at use site.
3531}
3532
David Brazdilfc6a86a2015-06-26 10:33:45 +00003533void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003534 DCHECK(!successor->IsExitBlock());
3535 HBasicBlock* block = got->GetBlock();
3536 HInstruction* previous = got->GetPrevious();
3537 HLoopInformation* info = block->GetLoopInformation();
3538
David Brazdil46e2a392015-03-16 17:31:52 +00003539 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003540 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3541 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3542 return;
3543 }
3544 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3545 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3546 }
3547 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003548 __ B(codegen_->GetLabelOf(successor));
3549 }
3550}
3551
David Brazdilfc6a86a2015-06-26 10:33:45 +00003552void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3553 got->SetLocations(nullptr);
3554}
3555
3556void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3557 HandleGoto(got, got->GetSuccessor());
3558}
3559
3560void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3561 try_boundary->SetLocations(nullptr);
3562}
3563
3564void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3565 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3566 if (!successor->IsExitBlock()) {
3567 HandleGoto(try_boundary, successor);
3568 }
3569}
3570
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003571void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003572 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003573 vixl::aarch64::Label* true_target,
3574 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003575 // FP branching requires both targets to be explicit. If either of the targets
3576 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003577 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003578 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003579
David Brazdil0debae72015-11-12 18:37:00 +00003580 if (true_target == nullptr && false_target == nullptr) {
3581 // Nothing to do. The code always falls through.
3582 return;
3583 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003584 // Constant condition, statically compared against "true" (integer value 1).
3585 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003586 if (true_target != nullptr) {
3587 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003588 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003589 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003590 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (false_target != nullptr) {
3592 __ B(false_target);
3593 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003594 }
David Brazdil0debae72015-11-12 18:37:00 +00003595 return;
3596 }
3597
3598 // The following code generates these patterns:
3599 // (1) true_target == nullptr && false_target != nullptr
3600 // - opposite condition true => branch to false_target
3601 // (2) true_target != nullptr && false_target == nullptr
3602 // - condition true => branch to true_target
3603 // (3) true_target != nullptr && false_target != nullptr
3604 // - condition true => branch to true_target
3605 // - branch to false_target
3606 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003607 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003608 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003609 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003610 if (true_target == nullptr) {
3611 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3612 } else {
3613 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3614 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003615 } else {
3616 // The condition instruction has not been materialized, use its inputs as
3617 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003618 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003619
David Brazdil0debae72015-11-12 18:37:00 +00003620 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003621 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003622 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003623 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003624 IfCondition opposite_condition = condition->GetOppositeCondition();
3625 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003626 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003627 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003628 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003629 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003630 // Integer cases.
3631 Register lhs = InputRegisterAt(condition, 0);
3632 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003633
3634 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003635 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003636 if (true_target == nullptr) {
3637 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3638 non_fallthrough_target = false_target;
3639 } else {
3640 arm64_cond = ARM64Condition(condition->GetCondition());
3641 non_fallthrough_target = true_target;
3642 }
3643
Aart Bik086d27e2016-01-20 17:02:00 -08003644 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003645 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003646 switch (arm64_cond) {
3647 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003648 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003649 break;
3650 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003651 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003652 break;
3653 case lt:
3654 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003655 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003656 break;
3657 case ge:
3658 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003659 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003660 break;
3661 default:
3662 // Without the `static_cast` the compiler throws an error for
3663 // `-Werror=sign-promo`.
3664 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3665 }
3666 } else {
3667 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003668 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003669 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003670 }
3671 }
David Brazdil0debae72015-11-12 18:37:00 +00003672
3673 // If neither branch falls through (case 3), the conditional branch to `true_target`
3674 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3675 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003676 __ B(false_target);
3677 }
David Brazdil0debae72015-11-12 18:37:00 +00003678
3679 if (fallthrough_target.IsLinked()) {
3680 __ Bind(&fallthrough_target);
3681 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003682}
3683
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003684void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003686 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003687 locations->SetInAt(0, Location::RequiresRegister());
3688 }
3689}
3690
3691void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003692 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3693 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003694 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3695 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3696 true_target = nullptr;
3697 }
3698 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3699 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3700 false_target = nullptr;
3701 }
David Brazdil0debae72015-11-12 18:37:00 +00003702 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003703}
3704
3705void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3706 LocationSummary* locations = new (GetGraph()->GetArena())
3707 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003708 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003709 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003710 locations->SetInAt(0, Location::RequiresRegister());
3711 }
3712}
3713
3714void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003715 SlowPathCodeARM64* slow_path =
3716 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003717 GenerateTestAndBranch(deoptimize,
3718 /* condition_input_index */ 0,
3719 slow_path->GetEntryLabel(),
3720 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003721}
3722
Mingyao Yang063fc772016-08-02 11:02:54 -07003723void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3724 LocationSummary* locations = new (GetGraph()->GetArena())
3725 LocationSummary(flag, LocationSummary::kNoCall);
3726 locations->SetOut(Location::RequiresRegister());
3727}
3728
3729void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3730 __ Ldr(OutputRegister(flag),
3731 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3732}
3733
David Brazdilc0b601b2016-02-08 14:20:45 +00003734static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3735 return condition->IsCondition() &&
3736 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3737}
3738
Alexandre Rames880f1192016-06-13 16:04:50 +01003739static inline Condition GetConditionForSelect(HCondition* condition) {
3740 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003741 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3742 : ARM64Condition(cond);
3743}
3744
David Brazdil74eb1b22015-12-14 11:44:01 +00003745void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003747 if (Primitive::IsFloatingPointType(select->GetType())) {
3748 locations->SetInAt(0, Location::RequiresFpuRegister());
3749 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003750 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003751 } else {
3752 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3753 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3754 bool is_true_value_constant = cst_true_value != nullptr;
3755 bool is_false_value_constant = cst_false_value != nullptr;
3756 // Ask VIXL whether we should synthesize constants in registers.
3757 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3758 Operand true_op = is_true_value_constant ?
3759 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3760 Operand false_op = is_false_value_constant ?
3761 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3762 bool true_value_in_register = false;
3763 bool false_value_in_register = false;
3764 MacroAssembler::GetCselSynthesisInformation(
3765 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3766 true_value_in_register |= !is_true_value_constant;
3767 false_value_in_register |= !is_false_value_constant;
3768
3769 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3770 : Location::ConstantLocation(cst_true_value));
3771 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3772 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003773 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003774 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003775
David Brazdil74eb1b22015-12-14 11:44:01 +00003776 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3777 locations->SetInAt(2, Location::RequiresRegister());
3778 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003779}
3780
3781void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003782 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003783 Condition csel_cond;
3784
3785 if (IsBooleanValueOrMaterializedCondition(cond)) {
3786 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 // Use the condition flags set by the previous instruction.
3788 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003789 } else {
3790 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003791 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003792 }
3793 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003794 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003795 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003796 } else {
3797 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003798 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003799 }
3800
Alexandre Rames880f1192016-06-13 16:04:50 +01003801 if (Primitive::IsFloatingPointType(select->GetType())) {
3802 __ Fcsel(OutputFPRegister(select),
3803 InputFPRegisterAt(select, 1),
3804 InputFPRegisterAt(select, 0),
3805 csel_cond);
3806 } else {
3807 __ Csel(OutputRegister(select),
3808 InputOperandAt(select, 1),
3809 InputOperandAt(select, 0),
3810 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003811 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003812}
3813
David Srbecky0cf44932015-12-09 14:09:59 +00003814void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3815 new (GetGraph()->GetArena()) LocationSummary(info);
3816}
3817
David Srbeckyd28f4a02016-03-14 17:14:24 +00003818void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3819 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003820}
3821
3822void CodeGeneratorARM64::GenerateNop() {
3823 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003824}
3825
Alexandre Rames5319def2014-10-23 10:03:10 +01003826void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003827 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003828}
3829
3830void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003831 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003832}
3833
3834void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003835 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003836}
3837
3838void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003839 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003840}
3841
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003842// Temp is used for read barrier.
3843static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3844 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003845 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003846 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3847 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3848 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3849 return 1;
3850 }
3851 return 0;
3852}
3853
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003854// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003855// interface pointer, one for loading the current interface.
3856// The other checks have one temp for loading the object's class.
3857static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3858 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3859 return 3;
3860 }
3861 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003862}
3863
Alexandre Rames67555f72014-11-18 10:55:16 +00003864void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003865 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003866 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003867 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003868 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003869 case TypeCheckKind::kExactCheck:
3870 case TypeCheckKind::kAbstractClassCheck:
3871 case TypeCheckKind::kClassHierarchyCheck:
3872 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003873 call_kind =
3874 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003875 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003876 break;
3877 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003878 case TypeCheckKind::kUnresolvedCheck:
3879 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003880 call_kind = LocationSummary::kCallOnSlowPath;
3881 break;
3882 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003883
Alexandre Rames67555f72014-11-18 10:55:16 +00003884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003885 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003886 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003887 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003888 locations->SetInAt(0, Location::RequiresRegister());
3889 locations->SetInAt(1, Location::RequiresRegister());
3890 // The "out" register is used as a temporary, so it overlaps with the inputs.
3891 // Note that TypeCheckSlowPathARM64 uses this register too.
3892 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003893 // Add temps if necessary for read barriers.
3894 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003895}
3896
3897void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003898 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003899 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003900 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003901 Register obj = InputRegisterAt(instruction, 0);
3902 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003904 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003905 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3906 DCHECK_LE(num_temps, 1u);
3907 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3909 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3910 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3911 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003912
Scott Wakeling97c72b72016-06-24 16:19:36 +01003913 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003914 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003915
3916 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003917 // Avoid null check if we know `obj` is not null.
3918 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003919 __ Cbz(obj, &zero);
3920 }
3921
Roland Levillain44015862016-01-22 11:47:17 +00003922 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003923 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003924 // /* HeapReference<Class> */ out = obj->klass_
3925 GenerateReferenceLoadTwoRegisters(instruction,
3926 out_loc,
3927 obj_loc,
3928 class_offset,
3929 maybe_temp_loc,
3930 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 __ Cmp(out, cls);
3932 __ Cset(out, eq);
3933 if (zero.IsLinked()) {
3934 __ B(&done);
3935 }
3936 break;
3937 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003938
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003939 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003940 // /* HeapReference<Class> */ out = obj->klass_
3941 GenerateReferenceLoadTwoRegisters(instruction,
3942 out_loc,
3943 obj_loc,
3944 class_offset,
3945 maybe_temp_loc,
3946 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003947 // If the class is abstract, we eagerly fetch the super class of the
3948 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003949 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003950 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003951 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003952 GenerateReferenceLoadOneRegister(instruction,
3953 out_loc,
3954 super_offset,
3955 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003956 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003957 // If `out` is null, we use it for the result, and jump to `done`.
3958 __ Cbz(out, &done);
3959 __ Cmp(out, cls);
3960 __ B(ne, &loop);
3961 __ Mov(out, 1);
3962 if (zero.IsLinked()) {
3963 __ B(&done);
3964 }
3965 break;
3966 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003968 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003969 // /* HeapReference<Class> */ out = obj->klass_
3970 GenerateReferenceLoadTwoRegisters(instruction,
3971 out_loc,
3972 obj_loc,
3973 class_offset,
3974 maybe_temp_loc,
3975 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003977 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003978 __ Bind(&loop);
3979 __ Cmp(out, cls);
3980 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003981 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003982 GenerateReferenceLoadOneRegister(instruction,
3983 out_loc,
3984 super_offset,
3985 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003986 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 __ Cbnz(out, &loop);
3988 // If `out` is null, we use it for the result, and jump to `done`.
3989 __ B(&done);
3990 __ Bind(&success);
3991 __ Mov(out, 1);
3992 if (zero.IsLinked()) {
3993 __ B(&done);
3994 }
3995 break;
3996 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003999 // /* HeapReference<Class> */ out = obj->klass_
4000 GenerateReferenceLoadTwoRegisters(instruction,
4001 out_loc,
4002 obj_loc,
4003 class_offset,
4004 maybe_temp_loc,
4005 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004006 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004007 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004008 __ Cmp(out, cls);
4009 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004010 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004011 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004012 GenerateReferenceLoadOneRegister(instruction,
4013 out_loc,
4014 component_offset,
4015 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004016 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004017 // If `out` is null, we use it for the result, and jump to `done`.
4018 __ Cbz(out, &done);
4019 __ Ldrh(out, HeapOperand(out, primitive_offset));
4020 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4021 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004022 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004023 __ Mov(out, 1);
4024 __ B(&done);
4025 break;
4026 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004027
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004028 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004029 // No read barrier since the slow path will retry upon failure.
4030 // /* HeapReference<Class> */ out = obj->klass_
4031 GenerateReferenceLoadTwoRegisters(instruction,
4032 out_loc,
4033 obj_loc,
4034 class_offset,
4035 maybe_temp_loc,
4036 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004037 __ Cmp(out, cls);
4038 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004039 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4040 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004041 codegen_->AddSlowPath(slow_path);
4042 __ B(ne, slow_path->GetEntryLabel());
4043 __ Mov(out, 1);
4044 if (zero.IsLinked()) {
4045 __ B(&done);
4046 }
4047 break;
4048 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004049
Calin Juravle98893e12015-10-02 21:05:03 +01004050 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 case TypeCheckKind::kInterfaceCheck: {
4052 // Note that we indeed only call on slow path, but we always go
4053 // into the slow path for the unresolved and interface check
4054 // cases.
4055 //
4056 // We cannot directly call the InstanceofNonTrivial runtime
4057 // entry point without resorting to a type checking slow path
4058 // here (i.e. by calling InvokeRuntime directly), as it would
4059 // require to assign fixed registers for the inputs of this
4060 // HInstanceOf instruction (following the runtime calling
4061 // convention), which might be cluttered by the potential first
4062 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004063 //
4064 // TODO: Introduce a new runtime entry point taking the object
4065 // to test (instead of its class) as argument, and let it deal
4066 // with the read barrier issues. This will let us refactor this
4067 // case of the `switch` code as it was previously (with a direct
4068 // call to the runtime not using a type checking slow path).
4069 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004070 DCHECK(locations->OnlyCallsOnSlowPath());
4071 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4072 /* is_fatal */ false);
4073 codegen_->AddSlowPath(slow_path);
4074 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004075 if (zero.IsLinked()) {
4076 __ B(&done);
4077 }
4078 break;
4079 }
4080 }
4081
4082 if (zero.IsLinked()) {
4083 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004084 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004085 }
4086
4087 if (done.IsLinked()) {
4088 __ Bind(&done);
4089 }
4090
4091 if (slow_path != nullptr) {
4092 __ Bind(slow_path->GetExitLabel());
4093 }
4094}
4095
4096void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4097 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4098 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4099
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004100 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4101 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004102 case TypeCheckKind::kExactCheck:
4103 case TypeCheckKind::kAbstractClassCheck:
4104 case TypeCheckKind::kClassHierarchyCheck:
4105 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004106 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4107 LocationSummary::kCallOnSlowPath :
4108 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004109 break;
4110 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004111 case TypeCheckKind::kUnresolvedCheck:
4112 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004113 call_kind = LocationSummary::kCallOnSlowPath;
4114 break;
4115 }
4116
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4118 locations->SetInAt(0, Location::RequiresRegister());
4119 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004120 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4121 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122}
4123
4124void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004125 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004126 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004127 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004128 Register obj = InputRegisterAt(instruction, 0);
4129 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004130 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4131 DCHECK_GE(num_temps, 1u);
4132 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004133 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004134 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4135 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004136 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004137 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4138 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4139 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4140 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4141 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4142 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4143 const uint32_t object_array_data_offset =
4144 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004146 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004147 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4148 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4149 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004150 if (!kEmitCompilerReadBarrier) {
4151 is_type_check_slow_path_fatal =
4152 (type_check_kind == TypeCheckKind::kExactCheck ||
4153 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4154 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4155 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4156 !instruction->CanThrowIntoCatchBlock();
4157 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004158 SlowPathCodeARM64* type_check_slow_path =
4159 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4160 is_type_check_slow_path_fatal);
4161 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004162
Scott Wakeling97c72b72016-06-24 16:19:36 +01004163 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004164 // Avoid null check if we know obj is not null.
4165 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004166 __ Cbz(obj, &done);
4167 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004168
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004169 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004170 case TypeCheckKind::kExactCheck:
4171 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004172 // /* HeapReference<Class> */ temp = obj->klass_
4173 GenerateReferenceLoadTwoRegisters(instruction,
4174 temp_loc,
4175 obj_loc,
4176 class_offset,
4177 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004178 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004179
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 __ Cmp(temp, cls);
4181 // Jump to slow path for throwing the exception or doing a
4182 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004183 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004184 break;
4185 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004186
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004187 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004188 // /* HeapReference<Class> */ temp = obj->klass_
4189 GenerateReferenceLoadTwoRegisters(instruction,
4190 temp_loc,
4191 obj_loc,
4192 class_offset,
4193 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004194 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004195
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004196 // If the class is abstract, we eagerly fetch the super class of the
4197 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004198 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004200 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004201 GenerateReferenceLoadOneRegister(instruction,
4202 temp_loc,
4203 super_offset,
4204 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004205 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004207 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4208 // exception.
4209 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4210 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004211 __ Cmp(temp, cls);
4212 __ B(ne, &loop);
4213 break;
4214 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004215
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004216 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004217 // /* HeapReference<Class> */ temp = obj->klass_
4218 GenerateReferenceLoadTwoRegisters(instruction,
4219 temp_loc,
4220 obj_loc,
4221 class_offset,
4222 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004223 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004225 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004226 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 __ Bind(&loop);
4228 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004229 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004230
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004231 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004232 GenerateReferenceLoadOneRegister(instruction,
4233 temp_loc,
4234 super_offset,
4235 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004236 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004237
4238 // If the class reference currently in `temp` is not null, jump
4239 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004240 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004241 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004242 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004243 break;
4244 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004246 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004247 // /* HeapReference<Class> */ temp = obj->klass_
4248 GenerateReferenceLoadTwoRegisters(instruction,
4249 temp_loc,
4250 obj_loc,
4251 class_offset,
4252 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004253 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004254
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004255 // Do an exact check.
4256 __ Cmp(temp, cls);
4257 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004258
4259 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004261 GenerateReferenceLoadOneRegister(instruction,
4262 temp_loc,
4263 component_offset,
4264 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004265 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004266
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004267 // If the component type is null, jump to the slow path to throw the exception.
4268 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4269 // Otherwise, the object is indeed an array. Further check that this component type is not a
4270 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004271 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4272 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004273 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004274 break;
4275 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004276
Calin Juravle98893e12015-10-02 21:05:03 +01004277 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004278 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279 //
4280 // We cannot directly call the CheckCast runtime entry point
4281 // without resorting to a type checking slow path here (i.e. by
4282 // calling InvokeRuntime directly), as it would require to
4283 // assign fixed registers for the inputs of this HInstanceOf
4284 // instruction (following the runtime calling convention), which
4285 // might be cluttered by the potential first read barrier
4286 // emission at the beginning of this method.
4287 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004288 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004289 case TypeCheckKind::kInterfaceCheck: {
4290 // /* HeapReference<Class> */ temp = obj->klass_
4291 GenerateReferenceLoadTwoRegisters(instruction,
4292 temp_loc,
4293 obj_loc,
4294 class_offset,
4295 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004296 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004297
4298 // /* HeapReference<Class> */ temp = temp->iftable_
4299 GenerateReferenceLoadTwoRegisters(instruction,
4300 temp_loc,
4301 temp_loc,
4302 iftable_offset,
4303 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004304 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004305 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004306 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004307 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004308 vixl::aarch64::Label start_loop;
4309 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004310 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004311 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4312 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004313 // Go to next interface.
4314 __ Add(temp, temp, 2 * kHeapReferenceSize);
4315 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004316 // Compare the classes and continue the loop if they do not match.
4317 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4318 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004319 break;
4320 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004321 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004322 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004323
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004324 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004325}
4326
Alexandre Rames5319def2014-10-23 10:03:10 +01004327void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4328 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4329 locations->SetOut(Location::ConstantLocation(constant));
4330}
4331
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004332void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004333 // Will be generated at use site.
4334}
4335
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004336void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4337 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4338 locations->SetOut(Location::ConstantLocation(constant));
4339}
4340
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004341void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004342 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004343}
4344
Calin Juravle175dc732015-08-25 15:42:32 +01004345void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4346 // The trampoline uses the same calling convention as dex calling conventions,
4347 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4348 // the method_idx.
4349 HandleInvoke(invoke);
4350}
4351
4352void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4353 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4354}
4355
Alexandre Rames5319def2014-10-23 10:03:10 +01004356void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004357 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004358 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004359}
4360
Alexandre Rames67555f72014-11-18 10:55:16 +00004361void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4362 HandleInvoke(invoke);
4363}
4364
4365void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4366 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004367 LocationSummary* locations = invoke->GetLocations();
4368 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004369 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004370 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004371 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004372
4373 // The register ip1 is required to be used for the hidden argument in
4374 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004375 MacroAssembler* masm = GetVIXLAssembler();
4376 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004377 scratch_scope.Exclude(ip1);
4378 __ Mov(ip1, invoke->GetDexMethodIndex());
4379
Artem Serov914d7a82017-02-07 14:33:49 +00004380 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004381 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004382 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004383 {
4384 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4385 // /* HeapReference<Class> */ temp = temp->klass_
4386 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4387 codegen_->MaybeRecordImplicitNullCheck(invoke);
4388 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004389 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004390 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004391 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004392 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004393 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004394 }
Artem Serov914d7a82017-02-07 14:33:49 +00004395
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004396 // Instead of simply (possibly) unpoisoning `temp` here, we should
4397 // emit a read barrier for the previous class reference load.
4398 // However this is not required in practice, as this is an
4399 // intermediate/temporary reference and because the current
4400 // concurrent copying collector keeps the from-space memory
4401 // intact/accessible until the end of the marking phase (the
4402 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004403 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004404 __ Ldr(temp,
4405 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4406 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004407 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004408 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004409 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004410 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004411 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004412
4413 {
4414 // Ensure the pc position is recorded immediately after the `blr` instruction.
4415 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4416
4417 // lr();
4418 __ blr(lr);
4419 DCHECK(!codegen_->IsLeafMethod());
4420 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4421 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004422}
4423
4424void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004425 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004426 if (intrinsic.TryDispatch(invoke)) {
4427 return;
4428 }
4429
Alexandre Rames67555f72014-11-18 10:55:16 +00004430 HandleInvoke(invoke);
4431}
4432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004433void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004434 // Explicit clinit checks triggered by static invokes must have been pruned by
4435 // art::PrepareForRegisterAllocation.
4436 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004437
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004438 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004439 if (intrinsic.TryDispatch(invoke)) {
4440 return;
4441 }
4442
Alexandre Rames67555f72014-11-18 10:55:16 +00004443 HandleInvoke(invoke);
4444}
4445
Andreas Gampe878d58c2015-01-15 23:24:00 -08004446static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4447 if (invoke->GetLocations()->Intrinsified()) {
4448 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4449 intrinsic.Dispatch(invoke);
4450 return true;
4451 }
4452 return false;
4453}
4454
Vladimir Markodc151b22015-10-15 18:02:30 +01004455HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4456 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004457 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004458 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004459 return desired_dispatch_info;
4460}
4461
TatWai Chongd8c052a2016-11-02 16:12:48 +08004462Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4463 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004464 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004465 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4466 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004467 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4468 uint32_t offset =
4469 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004470 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004471 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004472 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004473 }
Vladimir Marko58155012015-08-19 12:49:41 +00004474 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004475 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004476 break;
4477 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4478 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004479 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004480 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004481 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4482 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004483 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004484 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004485 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004486 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004487 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004488 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004489 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004490 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004491 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004492 }
Vladimir Marko58155012015-08-19 12:49:41 +00004493 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004494 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004495 Register reg = XRegisterFrom(temp);
4496 Register method_reg;
4497 if (current_method.IsRegister()) {
4498 method_reg = XRegisterFrom(current_method);
4499 } else {
4500 DCHECK(invoke->GetLocations()->Intrinsified());
4501 DCHECK(!current_method.IsValid());
4502 method_reg = reg;
4503 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4504 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004505
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004506 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004507 __ Ldr(reg.X(),
4508 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004509 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004510 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004511 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4512 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004513 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4514 break;
4515 }
4516 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004517 return callee_method;
4518}
4519
4520void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4521 // All registers are assumed to be correctly set up.
4522 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004523
4524 switch (invoke->GetCodePtrLocation()) {
4525 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4526 __ Bl(&frame_entry_label_);
4527 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004528 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4529 // LR = callee_method->entry_point_from_quick_compiled_code_;
4530 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004531 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004532 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004533 {
4534 // To ensure that the pc position is recorded immediately after the `blr` instruction
4535 // BLR must be the last instruction emitted in this function.
4536 // Recording the pc will occur right after returning from this function.
4537 ExactAssemblyScope eas(GetVIXLAssembler(),
4538 kInstructionSize,
4539 CodeBufferCheckScope::kExactSize);
4540 // lr()
4541 __ blr(lr);
4542 }
Vladimir Marko58155012015-08-19 12:49:41 +00004543 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004544 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004545
Andreas Gampe878d58c2015-01-15 23:24:00 -08004546 DCHECK(!IsLeafMethod());
4547}
4548
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004549void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004550 // Use the calling convention instead of the location of the receiver, as
4551 // intrinsics may have put the receiver in a different register. In the intrinsics
4552 // slow path, the arguments have been moved to the right place, so here we are
4553 // guaranteed that the receiver is the first register of the calling convention.
4554 InvokeDexCallingConvention calling_convention;
4555 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004556 Register temp = XRegisterFrom(temp_in);
4557 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4558 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4559 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004560 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004561
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004562 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004563
4564 {
4565 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4566 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4567 // /* HeapReference<Class> */ temp = receiver->klass_
4568 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4569 MaybeRecordImplicitNullCheck(invoke);
4570 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004571 // Instead of simply (possibly) unpoisoning `temp` here, we should
4572 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004573 // intermediate/temporary reference and because the current
4574 // concurrent copying collector keeps the from-space memory
4575 // intact/accessible until the end of the marking phase (the
4576 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004577 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4578 // temp = temp->GetMethodAt(method_offset);
4579 __ Ldr(temp, MemOperand(temp, method_offset));
4580 // lr = temp->GetEntryPoint();
4581 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004582 {
4583 // To ensure that the pc position is recorded immediately after the `blr` instruction
4584 // BLR should be the last instruction emitted in this function.
4585 // Recording the pc will occur right after returning from this function.
4586 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4587 // lr();
4588 __ blr(lr);
4589 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004590}
4591
Orion Hodsonac141392017-01-13 11:53:47 +00004592void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4593 HandleInvoke(invoke);
4594}
4595
4596void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4597 codegen_->GenerateInvokePolymorphicCall(invoke);
4598}
4599
Scott Wakeling97c72b72016-06-24 16:19:36 +01004600vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4601 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004602 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004603 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004604 return
4605 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004606}
4607
Scott Wakeling97c72b72016-06-24 16:19:36 +01004608vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4609 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004610 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004611 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004612 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004613}
4614
Vladimir Marko1998cd02017-01-13 13:02:58 +00004615vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4616 const DexFile& dex_file,
4617 dex::TypeIndex type_index,
4618 vixl::aarch64::Label* adrp_label) {
4619 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4620}
4621
Scott Wakeling97c72b72016-06-24 16:19:36 +01004622vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4623 const DexFile& dex_file,
4624 uint32_t element_offset,
4625 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004626 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4627}
4628
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004629vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4630 baker_read_barrier_patches_.emplace_back(custom_data);
4631 return &baker_read_barrier_patches_.back().label;
4632}
4633
Scott Wakeling97c72b72016-06-24 16:19:36 +01004634vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4635 const DexFile& dex_file,
4636 uint32_t offset_or_index,
4637 vixl::aarch64::Label* adrp_label,
4638 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004639 // Add a patch entry and return the label.
4640 patches->emplace_back(dex_file, offset_or_index);
4641 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004642 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004643 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4644 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4645 return label;
4646}
4647
Scott Wakeling97c72b72016-06-24 16:19:36 +01004648vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004649 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004650 return boot_image_string_patches_.GetOrCreate(
4651 StringReference(&dex_file, string_index),
4652 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4653}
4654
Scott Wakeling97c72b72016-06-24 16:19:36 +01004655vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004656 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004657 return boot_image_type_patches_.GetOrCreate(
4658 TypeReference(&dex_file, type_index),
4659 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4660}
4661
Scott Wakeling97c72b72016-06-24 16:19:36 +01004662vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4663 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004664 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004665}
4666
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004667vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004668 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4669 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4670 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004671 return jit_string_patches_.GetOrCreate(
4672 StringReference(&dex_file, string_index),
4673 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4674}
4675
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004676vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004677 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4678 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4679 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004680 return jit_class_patches_.GetOrCreate(
4681 TypeReference(&dex_file, type_index),
4682 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4683}
4684
Vladimir Markoaad75c62016-10-03 08:46:48 +00004685void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4686 vixl::aarch64::Register reg) {
4687 DCHECK(reg.IsX());
4688 SingleEmissionCheckScope guard(GetVIXLAssembler());
4689 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004690 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004691}
4692
4693void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4694 vixl::aarch64::Register out,
4695 vixl::aarch64::Register base) {
4696 DCHECK(out.IsX());
4697 DCHECK(base.IsX());
4698 SingleEmissionCheckScope guard(GetVIXLAssembler());
4699 __ Bind(fixup_label);
4700 __ add(out, base, Operand(/* offset placeholder */ 0));
4701}
4702
4703void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4704 vixl::aarch64::Register out,
4705 vixl::aarch64::Register base) {
4706 DCHECK(base.IsX());
4707 SingleEmissionCheckScope guard(GetVIXLAssembler());
4708 __ Bind(fixup_label);
4709 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4710}
4711
4712template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4713inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4714 const ArenaDeque<PcRelativePatchInfo>& infos,
4715 ArenaVector<LinkerPatch>* linker_patches) {
4716 for (const PcRelativePatchInfo& info : infos) {
4717 linker_patches->push_back(Factory(info.label.GetLocation(),
4718 &info.target_dex_file,
4719 info.pc_insn_label->GetLocation(),
4720 info.offset_or_index));
4721 }
4722}
4723
Vladimir Marko58155012015-08-19 12:49:41 +00004724void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4725 DCHECK(linker_patches->empty());
4726 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004727 pc_relative_dex_cache_patches_.size() +
4728 boot_image_string_patches_.size() +
4729 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004730 boot_image_type_patches_.size() +
4731 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004732 type_bss_entry_patches_.size() +
4733 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004734 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004735 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004736 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004737 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004738 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004739 info.offset_or_index));
4740 }
4741 for (const auto& entry : boot_image_string_patches_) {
4742 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004743 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4744 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004745 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004746 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004747 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004748 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004749 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004750 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4751 linker_patches);
4752 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004753 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4754 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004755 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4756 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004757 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004758 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4759 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004760 for (const auto& entry : boot_image_type_patches_) {
4761 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004762 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4763 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004764 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004765 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004766 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004767 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4768 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4769 info.custom_data));
4770 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004771 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004772}
4773
Scott Wakeling97c72b72016-06-24 16:19:36 +01004774vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004775 Uint32ToLiteralMap* map) {
4776 return map->GetOrCreate(
4777 value,
4778 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4779}
4780
Scott Wakeling97c72b72016-06-24 16:19:36 +01004781vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004782 return uint64_literals_.GetOrCreate(
4783 value,
4784 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004785}
4786
Scott Wakeling97c72b72016-06-24 16:19:36 +01004787vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004788 MethodReference target_method,
4789 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004790 return map->GetOrCreate(
4791 target_method,
4792 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004793}
4794
Andreas Gampe878d58c2015-01-15 23:24:00 -08004795void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004796 // Explicit clinit checks triggered by static invokes must have been pruned by
4797 // art::PrepareForRegisterAllocation.
4798 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004799
Andreas Gampe878d58c2015-01-15 23:24:00 -08004800 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4801 return;
4802 }
4803
Artem Serov914d7a82017-02-07 14:33:49 +00004804 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4805 // are no pools emitted.
4806 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004807 LocationSummary* locations = invoke->GetLocations();
4808 codegen_->GenerateStaticOrDirectCall(
4809 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004810 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004811}
4812
4813void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004814 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4815 return;
4816 }
4817
Artem Serov914d7a82017-02-07 14:33:49 +00004818 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4819 // are no pools emitted.
4820 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004821 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004822 DCHECK(!codegen_->IsLeafMethod());
4823 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4824}
4825
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004826HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4827 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004829 case HLoadClass::LoadKind::kInvalid:
4830 LOG(FATAL) << "UNREACHABLE";
4831 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004832 case HLoadClass::LoadKind::kReferrersClass:
4833 break;
4834 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4835 DCHECK(!GetCompilerOptions().GetCompilePic());
4836 break;
4837 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4838 DCHECK(GetCompilerOptions().GetCompilePic());
4839 break;
4840 case HLoadClass::LoadKind::kBootImageAddress:
4841 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004842 case HLoadClass::LoadKind::kBssEntry:
4843 DCHECK(!Runtime::Current()->UseJitCompilation());
4844 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004845 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004846 DCHECK(Runtime::Current()->UseJitCompilation());
4847 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004848 case HLoadClass::LoadKind::kDexCacheViaMethod:
4849 break;
4850 }
4851 return desired_class_load_kind;
4852}
4853
Alexandre Rames67555f72014-11-18 10:55:16 +00004854void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004855 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4856 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004857 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004858 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 cls,
4860 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004861 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004862 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004863 return;
4864 }
Vladimir Marko41559982017-01-06 14:04:23 +00004865 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004866
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004867 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4868 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004869 ? LocationSummary::kCallOnSlowPath
4870 : LocationSummary::kNoCall;
4871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004872 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004873 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004874 }
4875
Vladimir Marko41559982017-01-06 14:04:23 +00004876 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004877 locations->SetInAt(0, Location::RequiresRegister());
4878 }
4879 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004880 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4881 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4882 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004883 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004884 RegisterSet caller_saves = RegisterSet::Empty();
4885 InvokeRuntimeCallingConvention calling_convention;
4886 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4887 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4888 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4889 Primitive::kPrimNot).GetCode());
4890 locations->SetCustomSlowPathCallerSaves(caller_saves);
4891 } else {
4892 // For non-Baker read barrier we have a temp-clobbering call.
4893 }
4894 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004895}
4896
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004897// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4898// move.
4899void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004900 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4901 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4902 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004903 return;
4904 }
Vladimir Marko41559982017-01-06 14:04:23 +00004905 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004906
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004907 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004908 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004909 Register bss_entry_temp;
4910 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004911
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004912 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4913 ? kWithoutReadBarrier
4914 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004915 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004916 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004917 case HLoadClass::LoadKind::kReferrersClass: {
4918 DCHECK(!cls->CanCallRuntime());
4919 DCHECK(!cls->MustGenerateClinitCheck());
4920 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4921 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004922 GenerateGcRootFieldLoad(cls,
4923 out_loc,
4924 current_method,
4925 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004926 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004927 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 break;
4929 }
4930 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004931 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004932 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4933 cls->GetTypeIndex()));
4934 break;
4935 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004936 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937 // Add ADRP with its PC-relative type patch.
4938 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004939 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004940 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004941 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004942 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004943 vixl::aarch64::Label* add_label =
4944 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004945 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004946 break;
4947 }
4948 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004949 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004950 uint32_t address = dchecked_integral_cast<uint32_t>(
4951 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4952 DCHECK_NE(address, 0u);
4953 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004954 break;
4955 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004956 case HLoadClass::LoadKind::kBssEntry: {
4957 // Add ADRP with its PC-relative Class .bss entry patch.
4958 const DexFile& dex_file = cls->GetDexFile();
4959 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004960 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004961 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4962 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004963 // Add LDR with its PC-relative Class patch.
4964 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004965 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004966 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4967 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004968 out_loc,
4969 bss_entry_temp,
4970 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004971 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004972 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004973 generate_null_check = true;
4974 break;
4975 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004976 case HLoadClass::LoadKind::kJitTableAddress: {
4977 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4978 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004979 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004980 GenerateGcRootFieldLoad(cls,
4981 out_loc,
4982 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004983 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004984 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004985 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004986 break;
4987 }
Vladimir Marko41559982017-01-06 14:04:23 +00004988 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004989 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004990 LOG(FATAL) << "UNREACHABLE";
4991 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004992 }
4993
Vladimir Markoea4c1262017-02-06 19:59:33 +00004994 bool do_clinit = cls->MustGenerateClinitCheck();
4995 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004996 DCHECK(cls->CanCallRuntime());
4997 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004998 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004999 codegen_->AddSlowPath(slow_path);
5000 if (generate_null_check) {
5001 __ Cbz(out, slow_path->GetEntryLabel());
5002 }
5003 if (cls->MustGenerateClinitCheck()) {
5004 GenerateClassInitializationCheck(slow_path, out);
5005 } else {
5006 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00005007 }
5008 }
5009}
5010
David Brazdilcb1c0552015-08-04 16:22:25 +01005011static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005012 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005013}
5014
Alexandre Rames67555f72014-11-18 10:55:16 +00005015void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5018 locations->SetOut(Location::RequiresRegister());
5019}
5020
5021void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005022 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5023}
5024
5025void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5026 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5027}
5028
5029void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5030 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005031}
5032
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5034 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005035 switch (desired_string_load_kind) {
5036 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5037 DCHECK(!GetCompilerOptions().GetCompilePic());
5038 break;
5039 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5040 DCHECK(GetCompilerOptions().GetCompilePic());
5041 break;
5042 case HLoadString::LoadKind::kBootImageAddress:
5043 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005044 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005045 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005046 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005047 case HLoadString::LoadKind::kJitTableAddress:
5048 DCHECK(Runtime::Current()->UseJitCompilation());
5049 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005050 case HLoadString::LoadKind::kDexCacheViaMethod:
5051 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005052 }
5053 return desired_string_load_kind;
5054}
5055
Alexandre Rames67555f72014-11-18 10:55:16 +00005056void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005057 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005058 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005059 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005060 InvokeRuntimeCallingConvention calling_convention;
5061 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5062 } else {
5063 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005064 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5065 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005066 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005067 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005068 RegisterSet caller_saves = RegisterSet::Empty();
5069 InvokeRuntimeCallingConvention calling_convention;
5070 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5071 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5072 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5073 Primitive::kPrimNot).GetCode());
5074 locations->SetCustomSlowPathCallerSaves(caller_saves);
5075 } else {
5076 // For non-Baker read barrier we have a temp-clobbering call.
5077 }
5078 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005079 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005080}
5081
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005082// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5083// move.
5084void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005085 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005086 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 switch (load->GetLoadKind()) {
5089 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5091 load->GetStringIndex()));
5092 return; // No dex cache slow path.
5093 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005094 // Add ADRP with its PC-relative String patch.
5095 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005096 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005097 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005098 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005100 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005101 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005102 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005103 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005104 return; // No dex cache slow path.
5105 }
5106 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005107 uint32_t address = dchecked_integral_cast<uint32_t>(
5108 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5109 DCHECK_NE(address, 0u);
5110 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005111 return; // No dex cache slow path.
5112 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005113 case HLoadString::LoadKind::kBssEntry: {
5114 // Add ADRP with its PC-relative String .bss entry 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());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005118 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005119 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005120 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005121 // Add LDR with its PC-relative String patch.
5122 vixl::aarch64::Label* ldr_label =
5123 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005124 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005125 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005126 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005127 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005128 /* offset placeholder */ 0u,
5129 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005130 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005131 SlowPathCodeARM64* slow_path =
5132 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005133 codegen_->AddSlowPath(slow_path);
5134 __ Cbz(out.X(), slow_path->GetEntryLabel());
5135 __ Bind(slow_path->GetExitLabel());
5136 return;
5137 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005138 case HLoadString::LoadKind::kJitTableAddress: {
5139 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005140 load->GetStringIndex(),
5141 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005142 GenerateGcRootFieldLoad(load,
5143 out_loc,
5144 out.X(),
5145 /* offset */ 0,
5146 /* fixup_label */ nullptr,
5147 kCompilerReadBarrierOption);
5148 return;
5149 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005150 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005151 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005152 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005153
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005154 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005155 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005156 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005157 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005158 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5159 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005160}
5161
Alexandre Rames5319def2014-10-23 10:03:10 +01005162void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5164 locations->SetOut(Location::ConstantLocation(constant));
5165}
5166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005167void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005168 // Will be generated at use site.
5169}
5170
Alexandre Rames67555f72014-11-18 10:55:16 +00005171void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5172 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005174 InvokeRuntimeCallingConvention calling_convention;
5175 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5176}
5177
5178void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005179 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005180 instruction,
5181 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005182 if (instruction->IsEnter()) {
5183 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5184 } else {
5185 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5186 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005187}
5188
Alexandre Rames42d641b2014-10-27 14:00:51 +00005189void LocationsBuilderARM64::VisitMul(HMul* mul) {
5190 LocationSummary* locations =
5191 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5192 switch (mul->GetResultType()) {
5193 case Primitive::kPrimInt:
5194 case Primitive::kPrimLong:
5195 locations->SetInAt(0, Location::RequiresRegister());
5196 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005197 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005198 break;
5199
5200 case Primitive::kPrimFloat:
5201 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005202 locations->SetInAt(0, Location::RequiresFpuRegister());
5203 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005204 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005205 break;
5206
5207 default:
5208 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5209 }
5210}
5211
5212void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5213 switch (mul->GetResultType()) {
5214 case Primitive::kPrimInt:
5215 case Primitive::kPrimLong:
5216 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5217 break;
5218
5219 case Primitive::kPrimFloat:
5220 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005221 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005222 break;
5223
5224 default:
5225 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5226 }
5227}
5228
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005229void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5230 LocationSummary* locations =
5231 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5232 switch (neg->GetResultType()) {
5233 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005234 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005235 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005237 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005238
5239 case Primitive::kPrimFloat:
5240 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005241 locations->SetInAt(0, Location::RequiresFpuRegister());
5242 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005243 break;
5244
5245 default:
5246 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5247 }
5248}
5249
5250void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5251 switch (neg->GetResultType()) {
5252 case Primitive::kPrimInt:
5253 case Primitive::kPrimLong:
5254 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5255 break;
5256
5257 case Primitive::kPrimFloat:
5258 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005259 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005260 break;
5261
5262 default:
5263 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5264 }
5265}
5266
5267void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5268 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005269 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005270 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005271 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005272 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5273 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005274}
5275
5276void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005277 // Note: if heap poisoning is enabled, the entry point takes cares
5278 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005279 QuickEntrypointEnum entrypoint =
5280 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5281 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005282 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005283}
5284
Alexandre Rames5319def2014-10-23 10:03:10 +01005285void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5286 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005287 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005288 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005289 if (instruction->IsStringAlloc()) {
5290 locations->AddTemp(LocationFrom(kArtMethodRegister));
5291 } else {
5292 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005293 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005294 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5295}
5296
5297void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005298 // Note: if heap poisoning is enabled, the entry point takes cares
5299 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005300 if (instruction->IsStringAlloc()) {
5301 // String is allocated through StringFactory. Call NewEmptyString entry point.
5302 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005303 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005304 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5305 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005306
5307 {
5308 // Ensure the pc position is recorded immediately after the `blr` instruction.
5309 ExactAssemblyScope eas(GetVIXLAssembler(),
5310 kInstructionSize,
5311 CodeBufferCheckScope::kExactSize);
5312 __ blr(lr);
5313 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5314 }
David Brazdil6de19382016-01-08 17:37:10 +00005315 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005316 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005317 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005318 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005319}
5320
5321void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5322 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005323 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005325}
5326
5327void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005328 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005329 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005330 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005331 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005332 break;
5333
5334 default:
5335 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5336 }
5337}
5338
David Brazdil66d126e2015-04-03 16:02:44 +01005339void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5340 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5341 locations->SetInAt(0, Location::RequiresRegister());
5342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5343}
5344
5345void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005346 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005347}
5348
Alexandre Rames5319def2014-10-23 10:03:10 +01005349void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005350 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5351 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005352}
5353
Calin Juravle2ae48182016-03-16 14:05:09 +00005354void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5355 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005356 return;
5357 }
Artem Serov914d7a82017-02-07 14:33:49 +00005358 {
5359 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5360 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5361 Location obj = instruction->GetLocations()->InAt(0);
5362 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5363 RecordPcInfo(instruction, instruction->GetDexPc());
5364 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005365}
5366
Calin Juravle2ae48182016-03-16 14:05:09 +00005367void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005368 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005369 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005370
5371 LocationSummary* locations = instruction->GetLocations();
5372 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005373
5374 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005375}
5376
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005377void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005378 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005379}
5380
Alexandre Rames67555f72014-11-18 10:55:16 +00005381void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5382 HandleBinaryOp(instruction);
5383}
5384
5385void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5386 HandleBinaryOp(instruction);
5387}
5388
Alexandre Rames3e69f162014-12-10 10:36:50 +00005389void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5390 LOG(FATAL) << "Unreachable";
5391}
5392
5393void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5394 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5395}
5396
Alexandre Rames5319def2014-10-23 10:03:10 +01005397void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5398 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5399 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5400 if (location.IsStackSlot()) {
5401 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5402 } else if (location.IsDoubleStackSlot()) {
5403 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5404 }
5405 locations->SetOut(location);
5406}
5407
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005408void InstructionCodeGeneratorARM64::VisitParameterValue(
5409 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005410 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005411}
5412
5413void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5414 LocationSummary* locations =
5415 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005416 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005417}
5418
5419void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5420 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5421 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005422}
5423
5424void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005426 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005427 locations->SetInAt(i, Location::Any());
5428 }
5429 locations->SetOut(Location::Any());
5430}
5431
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005432void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005433 LOG(FATAL) << "Unreachable";
5434}
5435
Serban Constantinescu02164b32014-11-13 14:05:07 +00005436void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005437 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005438 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005439 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5440 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005441 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5442
5443 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444 case Primitive::kPrimInt:
5445 case Primitive::kPrimLong:
5446 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005447 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005448 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5449 break;
5450
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005451 case Primitive::kPrimFloat:
5452 case Primitive::kPrimDouble: {
5453 InvokeRuntimeCallingConvention calling_convention;
5454 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5455 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5456 locations->SetOut(calling_convention.GetReturnLocation(type));
5457
5458 break;
5459 }
5460
Serban Constantinescu02164b32014-11-13 14:05:07 +00005461 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005462 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005463 }
5464}
5465
5466void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5467 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005468
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 switch (type) {
5470 case Primitive::kPrimInt:
5471 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005472 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005473 break;
5474 }
5475
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005476 case Primitive::kPrimFloat:
5477 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005478 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5479 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005480 if (type == Primitive::kPrimFloat) {
5481 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5482 } else {
5483 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5484 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005485 break;
5486 }
5487
Serban Constantinescu02164b32014-11-13 14:05:07 +00005488 default:
5489 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005490 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005491 }
5492}
5493
Calin Juravle27df7582015-04-17 19:12:31 +01005494void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5495 memory_barrier->SetLocations(nullptr);
5496}
5497
5498void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005499 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005500}
5501
Alexandre Rames5319def2014-10-23 10:03:10 +01005502void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5504 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005505 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005506}
5507
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005508void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005509 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005510}
5511
5512void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5513 instruction->SetLocations(nullptr);
5514}
5515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005516void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005517 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005518}
5519
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005520void LocationsBuilderARM64::VisitRor(HRor* ror) {
5521 HandleBinaryOp(ror);
5522}
5523
5524void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5525 HandleBinaryOp(ror);
5526}
5527
Serban Constantinescu02164b32014-11-13 14:05:07 +00005528void LocationsBuilderARM64::VisitShl(HShl* shl) {
5529 HandleShift(shl);
5530}
5531
5532void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5533 HandleShift(shl);
5534}
5535
5536void LocationsBuilderARM64::VisitShr(HShr* shr) {
5537 HandleShift(shr);
5538}
5539
5540void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5541 HandleShift(shr);
5542}
5543
Alexandre Rames5319def2014-10-23 10:03:10 +01005544void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005545 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005546}
5547
5548void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005549 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005550}
5551
Alexandre Rames67555f72014-11-18 10:55:16 +00005552void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005553 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005554}
5555
5556void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005557 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005558}
5559
5560void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005561 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005562}
5563
Alexandre Rames67555f72014-11-18 10:55:16 +00005564void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005565 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005566}
5567
Calin Juravlee460d1d2015-09-29 04:52:17 +01005568void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5569 HUnresolvedInstanceFieldGet* instruction) {
5570 FieldAccessCallingConventionARM64 calling_convention;
5571 codegen_->CreateUnresolvedFieldLocationSummary(
5572 instruction, instruction->GetFieldType(), calling_convention);
5573}
5574
5575void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5576 HUnresolvedInstanceFieldGet* instruction) {
5577 FieldAccessCallingConventionARM64 calling_convention;
5578 codegen_->GenerateUnresolvedFieldAccess(instruction,
5579 instruction->GetFieldType(),
5580 instruction->GetFieldIndex(),
5581 instruction->GetDexPc(),
5582 calling_convention);
5583}
5584
5585void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5586 HUnresolvedInstanceFieldSet* instruction) {
5587 FieldAccessCallingConventionARM64 calling_convention;
5588 codegen_->CreateUnresolvedFieldLocationSummary(
5589 instruction, instruction->GetFieldType(), calling_convention);
5590}
5591
5592void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5593 HUnresolvedInstanceFieldSet* instruction) {
5594 FieldAccessCallingConventionARM64 calling_convention;
5595 codegen_->GenerateUnresolvedFieldAccess(instruction,
5596 instruction->GetFieldType(),
5597 instruction->GetFieldIndex(),
5598 instruction->GetDexPc(),
5599 calling_convention);
5600}
5601
5602void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5603 HUnresolvedStaticFieldGet* instruction) {
5604 FieldAccessCallingConventionARM64 calling_convention;
5605 codegen_->CreateUnresolvedFieldLocationSummary(
5606 instruction, instruction->GetFieldType(), calling_convention);
5607}
5608
5609void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5610 HUnresolvedStaticFieldGet* instruction) {
5611 FieldAccessCallingConventionARM64 calling_convention;
5612 codegen_->GenerateUnresolvedFieldAccess(instruction,
5613 instruction->GetFieldType(),
5614 instruction->GetFieldIndex(),
5615 instruction->GetDexPc(),
5616 calling_convention);
5617}
5618
5619void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5620 HUnresolvedStaticFieldSet* instruction) {
5621 FieldAccessCallingConventionARM64 calling_convention;
5622 codegen_->CreateUnresolvedFieldLocationSummary(
5623 instruction, instruction->GetFieldType(), calling_convention);
5624}
5625
5626void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5627 HUnresolvedStaticFieldSet* instruction) {
5628 FieldAccessCallingConventionARM64 calling_convention;
5629 codegen_->GenerateUnresolvedFieldAccess(instruction,
5630 instruction->GetFieldType(),
5631 instruction->GetFieldIndex(),
5632 instruction->GetDexPc(),
5633 calling_convention);
5634}
5635
Alexandre Rames5319def2014-10-23 10:03:10 +01005636void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005637 LocationSummary* locations =
5638 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005639 // In suspend check slow path, usually there are no caller-save registers at all.
5640 // If SIMD instructions are present, however, we force spilling all live SIMD
5641 // registers in full width (since the runtime only saves/restores lower part).
5642 locations->SetCustomSlowPathCallerSaves(
5643 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005644}
5645
5646void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005647 HBasicBlock* block = instruction->GetBlock();
5648 if (block->GetLoopInformation() != nullptr) {
5649 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5650 // The back edge will generate the suspend check.
5651 return;
5652 }
5653 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5654 // The goto will generate the suspend check.
5655 return;
5656 }
5657 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005658}
5659
Alexandre Rames67555f72014-11-18 10:55:16 +00005660void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5661 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005663 InvokeRuntimeCallingConvention calling_convention;
5664 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5665}
5666
5667void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005668 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005669 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005670}
5671
5672void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5673 LocationSummary* locations =
5674 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5675 Primitive::Type input_type = conversion->GetInputType();
5676 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005677 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005678 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5679 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5680 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5681 }
5682
Alexandre Rames542361f2015-01-29 16:57:31 +00005683 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005684 locations->SetInAt(0, Location::RequiresFpuRegister());
5685 } else {
5686 locations->SetInAt(0, Location::RequiresRegister());
5687 }
5688
Alexandre Rames542361f2015-01-29 16:57:31 +00005689 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005690 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5691 } else {
5692 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5693 }
5694}
5695
5696void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5697 Primitive::Type result_type = conversion->GetResultType();
5698 Primitive::Type input_type = conversion->GetInputType();
5699
5700 DCHECK_NE(input_type, result_type);
5701
Alexandre Rames542361f2015-01-29 16:57:31 +00005702 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005703 int result_size = Primitive::ComponentSize(result_type);
5704 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005705 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005706 Register output = OutputRegister(conversion);
5707 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005708 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005709 // 'int' values are used directly as W registers, discarding the top
5710 // bits, so we don't need to sign-extend and can just perform a move.
5711 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5712 // top 32 bits of the target register. We theoretically could leave those
5713 // bits unchanged, but we would have to make sure that no code uses a
5714 // 32bit input value as a 64bit value assuming that the top 32 bits are
5715 // zero.
5716 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005717 } else if (result_type == Primitive::kPrimChar ||
5718 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5719 __ Ubfx(output,
5720 output.IsX() ? source.X() : source.W(),
5721 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005722 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005723 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005724 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005725 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005726 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005727 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005728 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5729 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005730 } else if (Primitive::IsFloatingPointType(result_type) &&
5731 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005732 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5733 } else {
5734 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5735 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005736 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005737}
Alexandre Rames67555f72014-11-18 10:55:16 +00005738
Serban Constantinescu02164b32014-11-13 14:05:07 +00005739void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5740 HandleShift(ushr);
5741}
5742
5743void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5744 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005745}
5746
5747void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5748 HandleBinaryOp(instruction);
5749}
5750
5751void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5752 HandleBinaryOp(instruction);
5753}
5754
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005755void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005756 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005757 LOG(FATAL) << "Unreachable";
5758}
5759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005760void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005761 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005762 LOG(FATAL) << "Unreachable";
5763}
5764
Mark Mendellfe57faa2015-09-18 09:26:15 -04005765// Simple implementation of packed switch - generate cascaded compare/jumps.
5766void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5767 LocationSummary* locations =
5768 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5769 locations->SetInAt(0, Location::RequiresRegister());
5770}
5771
5772void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5773 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005774 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005775 Register value_reg = InputRegisterAt(switch_instr, 0);
5776 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5777
Zheng Xu3927c8b2015-11-18 17:46:25 +08005778 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005779 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005780 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5781 // make sure we don't emit it if the target may run out of range.
5782 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5783 // ranges and emit the tables only as required.
5784 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005785
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005786 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005787 // Current instruction id is an upper bound of the number of HIRs in the graph.
5788 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5789 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005790 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5791 Register temp = temps.AcquireW();
5792 __ Subs(temp, value_reg, Operand(lower_bound));
5793
Zheng Xu3927c8b2015-11-18 17:46:25 +08005794 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005795 // Jump to successors[0] if value == lower_bound.
5796 __ B(eq, codegen_->GetLabelOf(successors[0]));
5797 int32_t last_index = 0;
5798 for (; num_entries - last_index > 2; last_index += 2) {
5799 __ Subs(temp, temp, Operand(2));
5800 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5801 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5802 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5803 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5804 }
5805 if (num_entries - last_index == 2) {
5806 // The last missing case_value.
5807 __ Cmp(temp, Operand(1));
5808 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005809 }
5810
5811 // And the default for any other value.
5812 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5813 __ B(codegen_->GetLabelOf(default_block));
5814 }
5815 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005816 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005817
5818 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5819
5820 // Below instructions should use at most one blocked register. Since there are two blocked
5821 // registers, we are free to block one.
5822 Register temp_w = temps.AcquireW();
5823 Register index;
5824 // Remove the bias.
5825 if (lower_bound != 0) {
5826 index = temp_w;
5827 __ Sub(index, value_reg, Operand(lower_bound));
5828 } else {
5829 index = value_reg;
5830 }
5831
5832 // Jump to default block if index is out of the range.
5833 __ Cmp(index, Operand(num_entries));
5834 __ B(hs, codegen_->GetLabelOf(default_block));
5835
5836 // In current VIXL implementation, it won't require any blocked registers to encode the
5837 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5838 // register pressure.
5839 Register table_base = temps.AcquireX();
5840 // Load jump offset from the table.
5841 __ Adr(table_base, jump_table->GetTableStartLabel());
5842 Register jump_offset = temp_w;
5843 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5844
5845 // Jump to target block by branching to table_base(pc related) + offset.
5846 Register target_address = table_base;
5847 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5848 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005849 }
5850}
5851
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005852void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5853 HInstruction* instruction,
5854 Location out,
5855 uint32_t offset,
5856 Location maybe_temp,
5857 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005858 Primitive::Type type = Primitive::kPrimNot;
5859 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005860 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005861 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005862 if (kUseBakerReadBarrier) {
5863 // Load with fast path based Baker's read barrier.
5864 // /* HeapReference<Object> */ out = *(out + offset)
5865 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5866 out,
5867 out_reg,
5868 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005869 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005870 /* needs_null_check */ false,
5871 /* use_load_acquire */ false);
5872 } else {
5873 // Load with slow path based read barrier.
5874 // Save the value of `out` into `maybe_temp` before overwriting it
5875 // in the following move operation, as we will need it for the
5876 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005877 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005878 __ Mov(temp_reg, out_reg);
5879 // /* HeapReference<Object> */ out = *(out + offset)
5880 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5881 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5882 }
5883 } else {
5884 // Plain load with no read barrier.
5885 // /* HeapReference<Object> */ out = *(out + offset)
5886 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5887 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5888 }
5889}
5890
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005891void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5892 HInstruction* instruction,
5893 Location out,
5894 Location obj,
5895 uint32_t offset,
5896 Location maybe_temp,
5897 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005898 Primitive::Type type = Primitive::kPrimNot;
5899 Register out_reg = RegisterFrom(out, type);
5900 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005901 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005902 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005903 if (kUseBakerReadBarrier) {
5904 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005905 // /* HeapReference<Object> */ out = *(obj + offset)
5906 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5907 out,
5908 obj_reg,
5909 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005910 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005911 /* needs_null_check */ false,
5912 /* use_load_acquire */ false);
5913 } else {
5914 // Load with slow path based read barrier.
5915 // /* HeapReference<Object> */ out = *(obj + offset)
5916 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5917 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5918 }
5919 } else {
5920 // Plain load with no read barrier.
5921 // /* HeapReference<Object> */ out = *(obj + offset)
5922 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5923 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5924 }
5925}
5926
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005927void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5928 HInstruction* instruction,
5929 Location root,
5930 Register obj,
5931 uint32_t offset,
5932 vixl::aarch64::Label* fixup_label,
5933 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005934 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005935 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005936 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005937 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005938 if (kUseBakerReadBarrier) {
5939 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005940 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005941 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5942 !Runtime::Current()->UseJitCompilation()) {
5943 // Note that we do not actually check the value of `GetIsGcMarking()`
5944 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005945 // load into `temp` (actually IP1) the read barrier mark introspection
5946 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5947 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005948 //
5949 // We use link-time generated thunks for the slow path. That thunk
5950 // checks the reference and jumps to the entrypoint if needed.
5951 //
5952 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5953 // lr = &return_address;
5954 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5955 // if (temp != nullptr) {
5956 // goto gc_root_thunk<root_reg>(lr)
5957 // }
5958 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005959
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005960 UseScratchRegisterScope temps(GetVIXLAssembler());
5961 DCHECK(temps.IsAvailable(ip0));
5962 DCHECK(temps.IsAvailable(ip1));
5963 temps.Exclude(ip0, ip1);
5964 uint32_t custom_data =
5965 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5966 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005967
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005968 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5969 DCHECK_EQ(ip0.GetCode(), 16u);
5970 const int32_t entry_point_offset =
5971 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5972 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5973 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5974 vixl::aarch64::Label return_address;
5975 __ adr(lr, &return_address);
5976 if (fixup_label != nullptr) {
5977 __ Bind(fixup_label);
5978 }
5979 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5980 "GC root LDR must be 2 instruction (8B) before the return address label.");
5981 __ ldr(root_reg, MemOperand(obj.X(), offset));
5982 __ Bind(cbnz_label);
5983 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5984 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005986 // Note that we do not actually check the value of
5987 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5988 // root or not. Instead, we load into `temp` the read barrier
5989 // mark entry point corresponding to register `root`. If `temp`
5990 // is null, it means that `GetIsGcMarking()` is false, and vice
5991 // versa.
5992 //
5993 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5994 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5995 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5996 // // Slow path.
5997 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5998 // }
Roland Levillain44015862016-01-22 11:47:17 +00005999
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006000 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
6001 Register temp = lr;
6002 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
6003 instruction, root, /* entrypoint */ LocationFrom(temp));
6004 codegen_->AddSlowPath(slow_path);
6005
6006 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6007 const int32_t entry_point_offset =
6008 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
6009 // Loading the entrypoint does not require a load acquire since it is only changed when
6010 // threads are suspended or running a checkpoint.
6011 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6012
6013 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6014 if (fixup_label == nullptr) {
6015 __ Ldr(root_reg, MemOperand(obj, offset));
6016 } else {
6017 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6018 }
6019 static_assert(
6020 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6021 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6022 "have different sizes.");
6023 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6024 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6025 "have different sizes.");
6026
6027 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6028 // checking GetIsGcMarking.
6029 __ Cbnz(temp, slow_path->GetEntryLabel());
6030 __ Bind(slow_path->GetExitLabel());
6031 }
Roland Levillain44015862016-01-22 11:47:17 +00006032 } else {
6033 // GC root loaded through a slow path for read barriers other
6034 // than Baker's.
6035 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006036 if (fixup_label == nullptr) {
6037 __ Add(root_reg.X(), obj.X(), offset);
6038 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006039 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006040 }
Roland Levillain44015862016-01-22 11:47:17 +00006041 // /* mirror::Object* */ root = root->Read()
6042 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6043 }
6044 } else {
6045 // Plain GC root load with no read barrier.
6046 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006047 if (fixup_label == nullptr) {
6048 __ Ldr(root_reg, MemOperand(obj, offset));
6049 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006050 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006051 }
Roland Levillain44015862016-01-22 11:47:17 +00006052 // Note that GC roots are not affected by heap poisoning, thus we
6053 // do not have to unpoison `root_reg` here.
6054 }
6055}
6056
6057void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6058 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006059 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006060 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006061 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006062 bool needs_null_check,
6063 bool use_load_acquire) {
6064 DCHECK(kEmitCompilerReadBarrier);
6065 DCHECK(kUseBakerReadBarrier);
6066
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006067 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6068 !use_load_acquire &&
6069 !Runtime::Current()->UseJitCompilation()) {
6070 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006071 // to decide whether to mark the loaded reference or not. Instead, we
6072 // load into `temp` (actually IP1) the read barrier mark introspection
6073 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6074 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006075 //
6076 // We use link-time generated thunks for the slow path. That thunk checks
6077 // the holder and jumps to the entrypoint if needed. If the holder is not
6078 // gray, it creates a fake dependency and returns to the LDR instruction.
6079 //
6080 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006081 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006082 // if (temp != nullptr) {
6083 // goto field_thunk<holder_reg, base_reg>(lr)
6084 // }
6085 // not_gray_return_address:
6086 // // Original reference load. If the offset is too large to fit
6087 // // into LDR, we use an adjusted base register here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006088 // GcRoot<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006089 // gray_return_address:
6090
6091 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6092 Register base = obj;
6093 if (offset >= kReferenceLoadMinFarOffset) {
6094 DCHECK(maybe_temp.IsRegister());
6095 base = WRegisterFrom(maybe_temp);
6096 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6097 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6098 offset &= (kReferenceLoadMinFarOffset - 1u);
6099 }
6100 UseScratchRegisterScope temps(GetVIXLAssembler());
6101 DCHECK(temps.IsAvailable(ip0));
6102 DCHECK(temps.IsAvailable(ip1));
6103 temps.Exclude(ip0, ip1);
6104 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6105 base.GetCode(),
6106 obj.GetCode());
6107 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6108
6109 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6110 DCHECK_EQ(ip0.GetCode(), 16u);
6111 const int32_t entry_point_offset =
6112 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6113 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006114 EmissionCheckScope guard(GetVIXLAssembler(),
6115 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006116 vixl::aarch64::Label return_address;
6117 __ adr(lr, &return_address);
6118 __ Bind(cbnz_label);
6119 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006120 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6121 "Field LDR must be 1 instruction (4B) before the return address label; "
6122 " 2 instructions (8B) for heap poisoning.");
6123 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6124 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006125 if (needs_null_check) {
6126 MaybeRecordImplicitNullCheck(instruction);
6127 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006128 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006129 __ Bind(&return_address);
6130 return;
6131 }
6132
Roland Levillain44015862016-01-22 11:47:17 +00006133 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006134 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006135 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006136 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006137 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6138 ref,
6139 obj,
6140 offset,
6141 no_index,
6142 no_scale_factor,
6143 temp,
6144 needs_null_check,
6145 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006146}
6147
6148void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6149 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006150 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006151 uint32_t data_offset,
6152 Location index,
6153 Register temp,
6154 bool needs_null_check) {
6155 DCHECK(kEmitCompilerReadBarrier);
6156 DCHECK(kUseBakerReadBarrier);
6157
Vladimir Marko66d691d2017-04-07 17:53:39 +01006158 static_assert(
6159 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6160 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6161 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6162
6163 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6164 !Runtime::Current()->UseJitCompilation()) {
6165 // Note that we do not actually check the value of `GetIsGcMarking()`
6166 // to decide whether to mark the loaded reference or not. Instead, we
6167 // load into `temp` (actually IP1) the read barrier mark introspection
6168 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6169 // false, and vice versa.
6170 //
6171 // We use link-time generated thunks for the slow path. That thunk checks
6172 // the holder and jumps to the entrypoint if needed. If the holder is not
6173 // gray, it creates a fake dependency and returns to the LDR instruction.
6174 //
6175 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6176 // lr = &gray_return_address;
6177 // if (temp != nullptr) {
6178 // goto field_thunk<holder_reg, base_reg>(lr)
6179 // }
6180 // not_gray_return_address:
6181 // // Original reference load. If the offset is too large to fit
6182 // // into LDR, we use an adjusted base register here.
6183 // GcRoot<mirror::Object> reference = data[index];
6184 // gray_return_address:
6185
6186 DCHECK(index.IsValid());
6187 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6188 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6189
6190 UseScratchRegisterScope temps(GetVIXLAssembler());
6191 DCHECK(temps.IsAvailable(ip0));
6192 DCHECK(temps.IsAvailable(ip1));
6193 temps.Exclude(ip0, ip1);
6194 uint32_t custom_data =
6195 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6196 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6197
6198 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6199 DCHECK_EQ(ip0.GetCode(), 16u);
6200 const int32_t entry_point_offset =
6201 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6202 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6203 __ Add(temp.X(), obj.X(), Operand(data_offset));
6204 EmissionCheckScope guard(GetVIXLAssembler(),
6205 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6206 vixl::aarch64::Label return_address;
6207 __ adr(lr, &return_address);
6208 __ Bind(cbnz_label);
6209 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6210 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6211 "Array LDR must be 1 instruction (4B) before the return address label; "
6212 " 2 instructions (8B) for heap poisoning.");
6213 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6214 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6215 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6216 __ Bind(&return_address);
6217 return;
6218 }
6219
Roland Levillain44015862016-01-22 11:47:17 +00006220 // Array cells are never volatile variables, therefore array loads
6221 // never use Load-Acquire instructions on ARM64.
6222 const bool use_load_acquire = false;
6223
6224 // /* HeapReference<Object> */ ref =
6225 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006226 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6227 ref,
6228 obj,
6229 data_offset,
6230 index,
6231 scale_factor,
6232 temp,
6233 needs_null_check,
6234 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006235}
6236
6237void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6238 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006239 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006240 uint32_t offset,
6241 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006242 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006243 Register temp,
6244 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006245 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006246 DCHECK(kEmitCompilerReadBarrier);
6247 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006248 // If we are emitting an array load, we should not be using a
6249 // Load Acquire instruction. In other words:
6250 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6251 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006252
Roland Levillain54f869e2017-03-06 13:54:11 +00006253 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6254 // whether we need to enter the slow path to mark the reference.
6255 // Then, in the slow path, check the gray bit in the lock word of
6256 // the reference's holder (`obj`) to decide whether to mark `ref` or
6257 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006258 //
Roland Levillainba650a42017-03-06 13:52:32 +00006259 // Note that we do not actually check the value of `GetIsGcMarking()`;
6260 // instead, we load into `temp2` the read barrier mark entry point
6261 // corresponding to register `ref`. If `temp2` is null, it means
6262 // that `GetIsGcMarking()` is false, and vice versa.
6263 //
6264 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006265 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6266 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006267 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6268 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6269 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6270 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6271 // if (is_gray) {
6272 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6273 // }
6274 // } else {
6275 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006276 // }
Roland Levillain44015862016-01-22 11:47:17 +00006277
Roland Levillainba650a42017-03-06 13:52:32 +00006278 // Slow path marking the object `ref` when the GC is marking. The
6279 // entrypoint will already be loaded in `temp2`.
6280 Register temp2 = lr;
6281 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006282 SlowPathCodeARM64* slow_path =
6283 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6284 instruction,
6285 ref,
6286 obj,
6287 offset,
6288 index,
6289 scale_factor,
6290 needs_null_check,
6291 use_load_acquire,
6292 temp,
6293 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006294 AddSlowPath(slow_path);
6295
6296 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6297 const int32_t entry_point_offset =
6298 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6299 // Loading the entrypoint does not require a load acquire since it is only changed when
6300 // threads are suspended or running a checkpoint.
6301 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006302 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6303 // checking GetIsGcMarking.
6304 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006305 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006306 GenerateRawReferenceLoad(
6307 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006308 __ Bind(slow_path->GetExitLabel());
6309}
6310
Roland Levillainff487002017-03-07 16:50:01 +00006311void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6312 Location ref,
6313 Register obj,
6314 Location field_offset,
6315 Register temp,
6316 bool needs_null_check,
6317 bool use_load_acquire) {
6318 DCHECK(kEmitCompilerReadBarrier);
6319 DCHECK(kUseBakerReadBarrier);
6320 // If we are emitting an array load, we should not be using a
6321 // Load Acquire instruction. In other words:
6322 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6323 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6324
6325 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6326 // whether we need to enter the slow path to update the reference
6327 // field within `obj`. Then, in the slow path, check the gray bit
6328 // in the lock word of the reference's holder (`obj`) to decide
6329 // whether to mark `ref` and update the field or not.
6330 //
6331 // Note that we do not actually check the value of `GetIsGcMarking()`;
6332 // instead, we load into `temp2` the read barrier mark entry point
6333 // corresponding to register `ref`. If `temp2` is null, it means
6334 // that `GetIsGcMarking()` is false, and vice versa.
6335 //
6336 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6337 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6338 // // Slow path.
6339 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6340 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6341 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6342 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6343 // if (is_gray) {
6344 // old_ref = ref;
6345 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6346 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6347 // }
6348 // }
6349
6350 // Slow path updating the object reference at address `obj + field_offset`
6351 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6352 Register temp2 = lr;
6353 Location temp2_loc = LocationFrom(temp2);
6354 SlowPathCodeARM64* slow_path =
6355 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6356 instruction,
6357 ref,
6358 obj,
6359 /* offset */ 0u,
6360 /* index */ field_offset,
6361 /* scale_factor */ 0u /* "times 1" */,
6362 needs_null_check,
6363 use_load_acquire,
6364 temp,
6365 /* entrypoint */ temp2_loc);
6366 AddSlowPath(slow_path);
6367
6368 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6369 const int32_t entry_point_offset =
6370 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6371 // Loading the entrypoint does not require a load acquire since it is only changed when
6372 // threads are suspended or running a checkpoint.
6373 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6374 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6375 // checking GetIsGcMarking.
6376 __ Cbnz(temp2, slow_path->GetEntryLabel());
6377 // Fast path: the GC is not marking: nothing to do (the field is
6378 // up-to-date, and we don't need to load the reference).
6379 __ Bind(slow_path->GetExitLabel());
6380}
6381
Roland Levillainba650a42017-03-06 13:52:32 +00006382void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6383 Location ref,
6384 Register obj,
6385 uint32_t offset,
6386 Location index,
6387 size_t scale_factor,
6388 bool needs_null_check,
6389 bool use_load_acquire) {
6390 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006391 Primitive::Type type = Primitive::kPrimNot;
6392 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006393
Roland Levillainba650a42017-03-06 13:52:32 +00006394 // If needed, vixl::EmissionCheckScope guards are used to ensure
6395 // that no pools are emitted between the load (macro) instruction
6396 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006397
Roland Levillain44015862016-01-22 11:47:17 +00006398 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006399 // Load types involving an "index": ArrayGet,
6400 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6401 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006402 if (use_load_acquire) {
6403 // UnsafeGetObjectVolatile intrinsic case.
6404 // Register `index` is not an index in an object array, but an
6405 // offset to an object reference field within object `obj`.
6406 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6407 DCHECK(instruction->GetLocations()->Intrinsified());
6408 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6409 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006410 DCHECK_EQ(offset, 0u);
6411 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006412 DCHECK_EQ(needs_null_check, false);
6413 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006414 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6415 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006416 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006417 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6418 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006419 if (index.IsConstant()) {
6420 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006421 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006422 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006423 if (needs_null_check) {
6424 MaybeRecordImplicitNullCheck(instruction);
6425 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006426 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006427 UseScratchRegisterScope temps(GetVIXLAssembler());
6428 Register temp = temps.AcquireW();
6429 __ Add(temp, obj, offset);
6430 {
6431 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6432 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6433 if (needs_null_check) {
6434 MaybeRecordImplicitNullCheck(instruction);
6435 }
6436 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006437 }
Roland Levillain44015862016-01-22 11:47:17 +00006438 }
Roland Levillain44015862016-01-22 11:47:17 +00006439 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006440 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006441 MemOperand field = HeapOperand(obj, offset);
6442 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006443 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6444 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006445 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006446 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006447 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006448 if (needs_null_check) {
6449 MaybeRecordImplicitNullCheck(instruction);
6450 }
Roland Levillain44015862016-01-22 11:47:17 +00006451 }
6452 }
6453
6454 // Object* ref = ref_addr->AsMirrorPtr()
6455 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006456}
6457
6458void CodeGeneratorARM64::GenerateReadBarrierSlow(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 DCHECK(kEmitCompilerReadBarrier);
6465
Roland Levillain44015862016-01-22 11:47:17 +00006466 // Insert a slow path based read barrier *after* the reference load.
6467 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006468 // If heap poisoning is enabled, the unpoisoning of the loaded
6469 // reference will be carried out by the runtime within the slow
6470 // path.
6471 //
6472 // Note that `ref` currently does not get unpoisoned (when heap
6473 // poisoning is enabled), which is alright as the `ref` argument is
6474 // not used by the artReadBarrierSlow entry point.
6475 //
6476 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6477 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6478 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6479 AddSlowPath(slow_path);
6480
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006481 __ B(slow_path->GetEntryLabel());
6482 __ Bind(slow_path->GetExitLabel());
6483}
6484
Roland Levillain44015862016-01-22 11:47:17 +00006485void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6486 Location out,
6487 Location ref,
6488 Location obj,
6489 uint32_t offset,
6490 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006491 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006492 // Baker's read barriers shall be handled by the fast path
6493 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6494 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006495 // If heap poisoning is enabled, unpoisoning will be taken care of
6496 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006497 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006498 } else if (kPoisonHeapReferences) {
6499 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6500 }
6501}
6502
Roland Levillain44015862016-01-22 11:47:17 +00006503void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6504 Location out,
6505 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006506 DCHECK(kEmitCompilerReadBarrier);
6507
Roland Levillain44015862016-01-22 11:47:17 +00006508 // Insert a slow path based read barrier *after* the GC root load.
6509 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006510 // Note that GC roots are not affected by heap poisoning, so we do
6511 // not need to do anything special for this here.
6512 SlowPathCodeARM64* slow_path =
6513 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6514 AddSlowPath(slow_path);
6515
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006516 __ B(slow_path->GetEntryLabel());
6517 __ Bind(slow_path->GetExitLabel());
6518}
6519
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006520void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6521 LocationSummary* locations =
6522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6523 locations->SetInAt(0, Location::RequiresRegister());
6524 locations->SetOut(Location::RequiresRegister());
6525}
6526
6527void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6528 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006529 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006530 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006531 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006532 __ Ldr(XRegisterFrom(locations->Out()),
6533 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006534 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006535 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006536 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006537 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6538 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006539 __ Ldr(XRegisterFrom(locations->Out()),
6540 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006541 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006542}
6543
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006544static void PatchJitRootUse(uint8_t* code,
6545 const uint8_t* roots_data,
6546 vixl::aarch64::Literal<uint32_t>* literal,
6547 uint64_t index_in_table) {
6548 uint32_t literal_offset = literal->GetOffset();
6549 uintptr_t address =
6550 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6551 uint8_t* data = code + literal_offset;
6552 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6553}
6554
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006555void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6556 for (const auto& entry : jit_string_patches_) {
6557 const auto& it = jit_string_roots_.find(entry.first);
6558 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006559 PatchJitRootUse(code, roots_data, entry.second, it->second);
6560 }
6561 for (const auto& entry : jit_class_patches_) {
6562 const auto& it = jit_class_roots_.find(entry.first);
6563 DCHECK(it != jit_class_roots_.end());
6564 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006565 }
6566}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006567
Alexandre Rames67555f72014-11-18 10:55:16 +00006568#undef __
6569#undef QUICK_ENTRY_POINT
6570
Alexandre Rames5319def2014-10-23 10:03:10 +01006571} // namespace arm64
6572} // namespace art