blob: c65245eb2bed847a9e2fd2620ffc1f6ba88618c2 [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"
Andreas Gamped4901292017-05-30 18:41:34 -070032#include "lock_word.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000033#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010034#include "thread.h"
35#include "utils/arm64/assembler_arm64.h"
36#include "utils/assembler.h"
37#include "utils/stack_checks.h"
38
Scott Wakeling97c72b72016-06-24 16:19:36 +010039using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000040using vixl::ExactAssemblyScope;
41using vixl::CodeBufferCheckScope;
42using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010043
44#ifdef __
45#error "ARM64 Codegen VIXL macro-assembler macro already defined."
46#endif
47
Alexandre Rames5319def2014-10-23 10:03:10 +010048namespace art {
49
Roland Levillain22ccc3a2015-11-24 13:10:05 +000050template<class MirrorType>
51class GcRoot;
52
Alexandre Rames5319def2014-10-23 10:03:10 +010053namespace arm64 {
54
Alexandre Ramesbe919d92016-08-23 18:33:36 +010055using helpers::ARM64EncodableConstantOrRegister;
56using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080057using helpers::CPURegisterFrom;
58using helpers::DRegisterFrom;
59using helpers::FPRegisterFrom;
60using helpers::HeapOperand;
61using helpers::HeapOperandFrom;
62using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010063using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080065using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010066using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080067using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010068using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080069using helpers::LocationFrom;
70using helpers::OperandFromMemOperand;
71using helpers::OutputCPURegister;
72using helpers::OutputFPRegister;
73using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010074using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080075using helpers::RegisterFrom;
76using helpers::StackOperandFrom;
77using helpers::VIXLRegCodeFromART;
78using helpers::WRegisterFrom;
79using helpers::XRegisterFrom;
80
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());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100590 LocationSummary* locations = instruction_->GetLocations();
591 SaveLiveRegisters(codegen, locations);
592 InvokeRuntimeCallingConvention calling_convention;
593 __ Mov(calling_convention.GetRegisterAt(0),
594 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000595 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100596 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700597 }
598
Alexandre Rames9931f312015-06-19 14:47:01 +0100599 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
600
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700601 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700602 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
603};
604
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100605class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
606 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000607 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100608
609 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
610 LocationSummary* locations = instruction_->GetLocations();
611 __ Bind(GetEntryLabel());
612 SaveLiveRegisters(codegen, locations);
613
614 InvokeRuntimeCallingConvention calling_convention;
615 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
616 parallel_move.AddMove(
617 locations->InAt(0),
618 LocationFrom(calling_convention.GetRegisterAt(0)),
619 Primitive::kPrimNot,
620 nullptr);
621 parallel_move.AddMove(
622 locations->InAt(1),
623 LocationFrom(calling_convention.GetRegisterAt(1)),
624 Primitive::kPrimInt,
625 nullptr);
626 parallel_move.AddMove(
627 locations->InAt(2),
628 LocationFrom(calling_convention.GetRegisterAt(2)),
629 Primitive::kPrimNot,
630 nullptr);
631 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
632
633 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000634 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100635 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
636 RestoreLiveRegisters(codegen, locations);
637 __ B(GetExitLabel());
638 }
639
640 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
641
642 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
644};
645
Zheng Xu3927c8b2015-11-18 17:46:25 +0800646void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
647 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000648 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800649
650 // We are about to use the assembler to place literals directly. Make sure we have enough
651 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000652 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
653 num_entries * sizeof(int32_t),
654 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800655
656 __ Bind(&table_start_);
657 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
658 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100659 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800660 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100661 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800662 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
663 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
664 Literal<int32_t> literal(jump_offset);
665 __ place(&literal);
666 }
667}
668
Roland Levillain54f869e2017-03-06 13:54:11 +0000669// Abstract base class for read barrier slow paths marking a reference
670// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000671//
Roland Levillain54f869e2017-03-06 13:54:11 +0000672// Argument `entrypoint` must be a register location holding the read
673// barrier marking runtime entry point to be invoked.
674class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
675 protected:
676 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
677 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678 DCHECK(kEmitCompilerReadBarrier);
679 }
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682
Roland Levillain54f869e2017-03-06 13:54:11 +0000683 // Generate assembly code calling the read barrier marking runtime
684 // entry point (ReadBarrierMarkRegX).
685 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000686 // No need to save live registers; it's taken care of by the
687 // entrypoint. Also, there is no need to update the stack mask,
688 // as this runtime call will not trigger a garbage collection.
689 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
690 DCHECK_NE(ref_.reg(), LR);
691 DCHECK_NE(ref_.reg(), WSP);
692 DCHECK_NE(ref_.reg(), WZR);
693 // IP0 is used internally by the ReadBarrierMarkRegX entry point
694 // as a temporary, it cannot be the entry point's input/output.
695 DCHECK_NE(ref_.reg(), IP0);
696 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
697 // "Compact" slow path, saving two moves.
698 //
699 // Instead of using the standard runtime calling convention (input
700 // and output in W0):
701 //
702 // W0 <- ref
703 // W0 <- ReadBarrierMark(W0)
704 // ref <- W0
705 //
706 // we just use rX (the register containing `ref`) as input and output
707 // of a dedicated entrypoint:
708 //
709 // rX <- ReadBarrierMarkRegX(rX)
710 //
711 if (entrypoint_.IsValid()) {
712 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
713 __ Blr(XRegisterFrom(entrypoint_));
714 } else {
715 // Entrypoint is not already loaded, load from the thread.
716 int32_t entry_point_offset =
717 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
718 // This runtime call does not require a stack map.
719 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
720 }
721 }
722
723 // The location (register) of the marked object reference.
724 const Location ref_;
725
726 // The location of the entrypoint if it is already loaded.
727 const Location entrypoint_;
728
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 private:
730 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
731};
732
Alexandre Rames5319def2014-10-23 10:03:10 +0100733// Slow path marking an object reference `ref` during a read
734// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000735// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100736//
737// This means that after the execution of this slow path, `ref` will
738// always be up-to-date, but `obj.field` may not; i.e., after the
739// flip, `ref` will be a to-space reference, but `obj.field` will
740// probably still be a from-space reference (unless it gets updated by
741// another thread, or if another thread installed another object
742// reference (different from `ref`) in `obj.field`).
743//
744// If `entrypoint` is a valid location it is assumed to already be
745// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000746// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000747class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 public:
749 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
750 Location ref,
751 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000752 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100753 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100754 }
755
756 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
757
758 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000759 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100760 DCHECK(locations->CanCall());
761 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000762 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000763 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
764 << "Unexpected instruction in read barrier marking slow path: "
765 << instruction_->DebugName();
766
767 __ Bind(GetEntryLabel());
768 GenerateReadBarrierMarkRuntimeCall(codegen);
769 __ B(GetExitLabel());
770 }
771
772 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000773 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
774};
775
Roland Levillain54f869e2017-03-06 13:54:11 +0000776// Slow path loading `obj`'s lock word, loading a reference from
777// object `*(obj + offset + (index << scale_factor))` into `ref`, and
778// marking `ref` if `obj` is gray according to the lock word (Baker
779// read barrier). The field `obj.field` in the object `obj` holding
780// this reference does not get updated by this slow path after marking
781// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
782// below for that).
783//
784// This means that after the execution of this slow path, `ref` will
785// always be up-to-date, but `obj.field` may not; i.e., after the
786// flip, `ref` will be a to-space reference, but `obj.field` will
787// probably still be a from-space reference (unless it gets updated by
788// another thread, or if another thread installed another object
789// reference (different from `ref`) in `obj.field`).
790//
791// Argument `entrypoint` must be a register location holding the read
792// barrier marking runtime entry point to be invoked.
793class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
794 public:
795 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
796 Location ref,
797 Register obj,
798 uint32_t offset,
799 Location index,
800 size_t scale_factor,
801 bool needs_null_check,
802 bool use_load_acquire,
803 Register temp,
804 Location entrypoint)
805 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
806 obj_(obj),
807 offset_(offset),
808 index_(index),
809 scale_factor_(scale_factor),
810 needs_null_check_(needs_null_check),
811 use_load_acquire_(use_load_acquire),
812 temp_(temp) {
813 DCHECK(kEmitCompilerReadBarrier);
814 DCHECK(kUseBakerReadBarrier);
815 }
816
817 const char* GetDescription() const OVERRIDE {
818 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
819 }
820
821 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
822 LocationSummary* locations = instruction_->GetLocations();
823 DCHECK(locations->CanCall());
824 DCHECK(ref_.IsRegister()) << ref_;
825 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
826 DCHECK(obj_.IsW());
827 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100828 DCHECK(instruction_->IsInstanceFieldGet() ||
829 instruction_->IsStaticFieldGet() ||
830 instruction_->IsArrayGet() ||
831 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100832 instruction_->IsInstanceOf() ||
833 instruction_->IsCheckCast() ||
834 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
835 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
836 << "Unexpected instruction in read barrier marking slow path: "
837 << instruction_->DebugName();
838 // The read barrier instrumentation of object ArrayGet
839 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000840 // instruction.
841 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000842 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
843
Roland Levillain54f869e2017-03-06 13:54:11 +0000844 // Temporary register `temp_`, used to store the lock word, must
845 // not be IP0 nor IP1, as we may use them to emit the reference
846 // load (in the call to GenerateRawReferenceLoad below), and we
847 // need the lock word to still be in `temp_` after the reference
848 // load.
849 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
850 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
851
Alexandre Rames5319def2014-10-23 10:03:10 +0100852 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000853
854 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
855 // inserted after the original load. However, in fast path based
856 // Baker's read barriers, we need to perform the load of
857 // mirror::Object::monitor_ *before* the original reference load.
858 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000859 // The slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100860 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
862 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
863 // HeapReference<mirror::Object> ref = *src; // Original reference load.
864 // bool is_gray = (rb_state == ReadBarrier::GrayState());
865 // if (is_gray) {
866 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
867 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000868 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 // Note: the original implementation in ReadBarrier::Barrier is
870 // slightly more complex as it performs additional checks that we do
871 // not do here for performance reasons.
872
873 // /* int32_t */ monitor = obj->monitor_
874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
875 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
876 if (needs_null_check_) {
877 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100878 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000879 // /* LockWord */ lock_word = LockWord(monitor)
880 static_assert(sizeof(LockWord) == sizeof(int32_t),
881 "art::LockWord and int32_t have different sizes.");
882
883 // Introduce a dependency on the lock_word including rb_state,
884 // to prevent load-load reordering, and without using
885 // a memory barrier (which would be more expensive).
886 // `obj` is unchanged by this operation, but its value now depends
887 // on `temp`.
888 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
889
890 // The actual reference load.
891 // A possible implicit null check has already been handled above.
892 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
893 arm64_codegen->GenerateRawReferenceLoad(instruction_,
894 ref_,
895 obj_,
896 offset_,
897 index_,
898 scale_factor_,
899 /* needs_null_check */ false,
900 use_load_acquire_);
901
902 // Mark the object `ref` when `obj` is gray.
903 //
904 // if (rb_state == ReadBarrier::GrayState())
905 // ref = ReadBarrier::Mark(ref);
906 //
907 // Given the numeric representation, it's enough to check the low bit of the rb_state.
908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
910 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
911 GenerateReadBarrierMarkRuntimeCall(codegen);
912
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000913 __ B(GetExitLabel());
914 }
915
916 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000917 // The register containing the object holding the marked object reference field.
918 Register obj_;
919 // The offset, index and scale factor to access the reference in `obj_`.
920 uint32_t offset_;
921 Location index_;
922 size_t scale_factor_;
923 // Is a null check required?
924 bool needs_null_check_;
925 // Should this reference load use Load-Acquire semantics?
926 bool use_load_acquire_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
940// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
950 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 size_t scale_factor,
958 bool needs_null_check,
959 bool use_load_acquire,
960 Register temp,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
968 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000969 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100972 }
973
974 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100976 }
977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = WRegisterFrom(ref_);
981 DCHECK(locations->CanCall());
982 DCHECK(ref_.IsRegister()) << ref_;
983 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000984 DCHECK(obj_.IsW());
985 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
986
987 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100988 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
989 << "Unexpected instruction in read barrier marking and field updating slow path: "
990 << instruction_->DebugName();
991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000993 DCHECK_EQ(offset_, 0u);
994 DCHECK_EQ(scale_factor_, 0u);
995 DCHECK_EQ(use_load_acquire_, false);
996 // The location of the offset of the marked reference field within `obj_`.
997 Location field_offset = index_;
998 DCHECK(field_offset.IsRegister()) << field_offset;
999
1000 // Temporary register `temp_`, used to store the lock word, must
1001 // not be IP0 nor IP1, as we may use them to emit the reference
1002 // load (in the call to GenerateRawReferenceLoad below), and we
1003 // need the lock word to still be in `temp_` after the reference
1004 // load.
1005 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1006 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001007
1008 __ Bind(GetEntryLabel());
1009
Roland Levillainff487002017-03-07 16:50:01 +00001010 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM64's:
1011 //
1012 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1013 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1014 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1015 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1016 // if (is_gray) {
1017 // old_ref = ref;
1018 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1019 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1020 // }
1021
Roland Levillain54f869e2017-03-06 13:54:11 +00001022 // /* int32_t */ monitor = obj->monitor_
1023 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1024 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1025 if (needs_null_check_) {
1026 codegen->MaybeRecordImplicitNullCheck(instruction_);
1027 }
1028 // /* LockWord */ lock_word = LockWord(monitor)
1029 static_assert(sizeof(LockWord) == sizeof(int32_t),
1030 "art::LockWord and int32_t have different sizes.");
1031
1032 // Introduce a dependency on the lock_word including rb_state,
1033 // to prevent load-load reordering, and without using
1034 // a memory barrier (which would be more expensive).
1035 // `obj` is unchanged by this operation, but its value now depends
1036 // on `temp`.
1037 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1038
1039 // The actual reference load.
1040 // A possible implicit null check has already been handled above.
1041 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1042 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1043 ref_,
1044 obj_,
1045 offset_,
1046 index_,
1047 scale_factor_,
1048 /* needs_null_check */ false,
1049 use_load_acquire_);
1050
1051 // Mark the object `ref` when `obj` is gray.
1052 //
1053 // if (rb_state == ReadBarrier::GrayState())
1054 // ref = ReadBarrier::Mark(ref);
1055 //
1056 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1057 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1058 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1059 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1060
1061 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001062 // Note that we cannot use IP to save the old reference, as IP is
1063 // used internally by the ReadBarrierMarkRegX entry point, and we
1064 // need the old reference after the call to that entry point.
1065 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1066 __ Mov(temp_.W(), ref_reg);
1067
Roland Levillain54f869e2017-03-06 13:54:11 +00001068 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001069
1070 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001071 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001072 //
1073 // Note that this field could also hold a different object, if
1074 // another thread had concurrently changed it. In that case, the
1075 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1076 // (CAS) operation below would abort the CAS, leaving the field
1077 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001079 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080
1081 // Update the the holder's field atomically. This may fail if
1082 // mutator updates before us, but it's OK. This is achieved
1083 // using a strong compare-and-set (CAS) operation with relaxed
1084 // memory synchronization ordering, where the expected value is
1085 // the old reference and the desired value is the new reference.
1086
1087 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1088 UseScratchRegisterScope temps(masm);
1089
1090 // Convenience aliases.
1091 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001092 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 Register expected = temp_.W();
1094 Register value = ref_reg;
1095 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1096 Register tmp_value = temps.AcquireW(); // Value in memory.
1097
1098 __ Add(tmp_ptr, base.X(), Operand(offset));
1099
1100 if (kPoisonHeapReferences) {
1101 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1102 if (value.Is(expected)) {
1103 // Do not poison `value`, as it is the same register as
1104 // `expected`, which has just been poisoned.
1105 } else {
1106 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1107 }
1108 }
1109
1110 // do {
1111 // tmp_value = [tmp_ptr] - expected;
1112 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1113
Roland Levillain24a4d112016-10-26 13:10:46 +01001114 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001115 __ Bind(&loop_head);
1116 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1117 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001118 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1120 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001121 __ B(&exit_loop);
1122 __ Bind(&comparison_failed);
1123 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001124 __ Bind(&exit_loop);
1125
1126 if (kPoisonHeapReferences) {
1127 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1128 if (value.Is(expected)) {
1129 // Do not unpoison `value`, as it is the same register as
1130 // `expected`, which has just been unpoisoned.
1131 } else {
1132 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1133 }
1134 }
1135
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001136 __ B(GetExitLabel());
1137 }
1138
1139 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140 // The register containing the object holding the marked object reference field.
1141 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001142 // The offset, index and scale factor to access the reference in `obj_`.
1143 uint32_t offset_;
1144 Location index_;
1145 size_t scale_factor_;
1146 // Is a null check required?
1147 bool needs_null_check_;
1148 // Should this reference load use Load-Acquire semantics?
1149 bool use_load_acquire_;
1150 // A temporary register used to hold the lock word of `obj_`; and
1151 // also to hold the original reference value, when the reference is
1152 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001153 const Register temp_;
1154
Roland Levillain54f869e2017-03-06 13:54:11 +00001155 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001156};
1157
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001158// Slow path generating a read barrier for a heap reference.
1159class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1160 public:
1161 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1162 Location out,
1163 Location ref,
1164 Location obj,
1165 uint32_t offset,
1166 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001167 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001168 out_(out),
1169 ref_(ref),
1170 obj_(obj),
1171 offset_(offset),
1172 index_(index) {
1173 DCHECK(kEmitCompilerReadBarrier);
1174 // If `obj` is equal to `out` or `ref`, it means the initial object
1175 // has been overwritten by (or after) the heap object reference load
1176 // to be instrumented, e.g.:
1177 //
1178 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001179 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001180 //
1181 // In that case, we have lost the information about the original
1182 // object, and the emitted read barrier cannot work properly.
1183 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1184 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1185 }
1186
1187 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1188 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1189 LocationSummary* locations = instruction_->GetLocations();
1190 Primitive::Type type = Primitive::kPrimNot;
1191 DCHECK(locations->CanCall());
1192 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001193 DCHECK(instruction_->IsInstanceFieldGet() ||
1194 instruction_->IsStaticFieldGet() ||
1195 instruction_->IsArrayGet() ||
1196 instruction_->IsInstanceOf() ||
1197 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001198 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001199 << "Unexpected instruction in read barrier for heap reference slow path: "
1200 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001201 // The read barrier instrumentation of object ArrayGet
1202 // instructions does not support the HIntermediateAddress
1203 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001204 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001205 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001206
1207 __ Bind(GetEntryLabel());
1208
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001209 SaveLiveRegisters(codegen, locations);
1210
1211 // We may have to change the index's value, but as `index_` is a
1212 // constant member (like other "inputs" of this slow path),
1213 // introduce a copy of it, `index`.
1214 Location index = index_;
1215 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001216 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001217 if (instruction_->IsArrayGet()) {
1218 // Compute the actual memory offset and store it in `index`.
1219 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1220 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1221 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1222 // We are about to change the value of `index_reg` (see the
1223 // calls to vixl::MacroAssembler::Lsl and
1224 // vixl::MacroAssembler::Mov below), but it has
1225 // not been saved by the previous call to
1226 // art::SlowPathCode::SaveLiveRegisters, as it is a
1227 // callee-save register --
1228 // art::SlowPathCode::SaveLiveRegisters does not consider
1229 // callee-save registers, as it has been designed with the
1230 // assumption that callee-save registers are supposed to be
1231 // handled by the called function. So, as a callee-save
1232 // register, `index_reg` _would_ eventually be saved onto
1233 // the stack, but it would be too late: we would have
1234 // changed its value earlier. Therefore, we manually save
1235 // it here into another freely available register,
1236 // `free_reg`, chosen of course among the caller-save
1237 // registers (as a callee-save `free_reg` register would
1238 // exhibit the same problem).
1239 //
1240 // Note we could have requested a temporary register from
1241 // the register allocator instead; but we prefer not to, as
1242 // this is a slow path, and we know we can find a
1243 // caller-save register that is available.
1244 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1245 __ Mov(free_reg.W(), index_reg);
1246 index_reg = free_reg;
1247 index = LocationFrom(index_reg);
1248 } else {
1249 // The initial register stored in `index_` has already been
1250 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1251 // (as it is not a callee-save register), so we can freely
1252 // use it.
1253 }
1254 // Shifting the index value contained in `index_reg` by the scale
1255 // factor (2) cannot overflow in practice, as the runtime is
1256 // unable to allocate object arrays with a size larger than
1257 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1258 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1259 static_assert(
1260 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1261 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1262 __ Add(index_reg, index_reg, Operand(offset_));
1263 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001264 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1265 // intrinsics, `index_` is not shifted by a scale factor of 2
1266 // (as in the case of ArrayGet), as it is actually an offset
1267 // to an object field within an object.
1268 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001269 DCHECK(instruction_->GetLocations()->Intrinsified());
1270 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1271 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1272 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001273 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001274 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001275 }
1276 }
1277
1278 // We're moving two or three locations to locations that could
1279 // overlap, so we need a parallel move resolver.
1280 InvokeRuntimeCallingConvention calling_convention;
1281 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1282 parallel_move.AddMove(ref_,
1283 LocationFrom(calling_convention.GetRegisterAt(0)),
1284 type,
1285 nullptr);
1286 parallel_move.AddMove(obj_,
1287 LocationFrom(calling_convention.GetRegisterAt(1)),
1288 type,
1289 nullptr);
1290 if (index.IsValid()) {
1291 parallel_move.AddMove(index,
1292 LocationFrom(calling_convention.GetRegisterAt(2)),
1293 Primitive::kPrimInt,
1294 nullptr);
1295 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1296 } else {
1297 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1298 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1299 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001300 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001301 instruction_,
1302 instruction_->GetDexPc(),
1303 this);
1304 CheckEntrypointTypes<
1305 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1306 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1307
1308 RestoreLiveRegisters(codegen, locations);
1309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001310 __ B(GetExitLabel());
1311 }
1312
1313 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1314
1315 private:
1316 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001317 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1318 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001319 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1320 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1321 return Register(VIXLRegCodeFromART(i), kXRegSize);
1322 }
1323 }
1324 // We shall never fail to find a free caller-save register, as
1325 // there are more than two core caller-save registers on ARM64
1326 // (meaning it is possible to find one which is different from
1327 // `ref` and `obj`).
1328 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1329 LOG(FATAL) << "Could not find a free register";
1330 UNREACHABLE();
1331 }
1332
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001333 const Location out_;
1334 const Location ref_;
1335 const Location obj_;
1336 const uint32_t offset_;
1337 // An additional location containing an index to an array.
1338 // Only used for HArrayGet and the UnsafeGetObject &
1339 // UnsafeGetObjectVolatile intrinsics.
1340 const Location index_;
1341
1342 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1343};
1344
1345// Slow path generating a read barrier for a GC root.
1346class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1347 public:
1348 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001349 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001350 DCHECK(kEmitCompilerReadBarrier);
1351 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001352
1353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1354 LocationSummary* locations = instruction_->GetLocations();
1355 Primitive::Type type = Primitive::kPrimNot;
1356 DCHECK(locations->CanCall());
1357 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001358 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1359 << "Unexpected instruction in read barrier for GC root slow path: "
1360 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001361
1362 __ Bind(GetEntryLabel());
1363 SaveLiveRegisters(codegen, locations);
1364
1365 InvokeRuntimeCallingConvention calling_convention;
1366 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1367 // The argument of the ReadBarrierForRootSlow is not a managed
1368 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1369 // thus we need a 64-bit move here, and we cannot use
1370 //
1371 // arm64_codegen->MoveLocation(
1372 // LocationFrom(calling_convention.GetRegisterAt(0)),
1373 // root_,
1374 // type);
1375 //
1376 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1377 // reference type (`Primitive::kPrimNot`).
1378 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001379 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001380 instruction_,
1381 instruction_->GetDexPc(),
1382 this);
1383 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1384 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1385
1386 RestoreLiveRegisters(codegen, locations);
1387 __ B(GetExitLabel());
1388 }
1389
1390 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1391
1392 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001393 const Location out_;
1394 const Location root_;
1395
1396 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1397};
1398
Alexandre Rames5319def2014-10-23 10:03:10 +01001399#undef __
1400
1401Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1402 Location next_location;
1403 if (type == Primitive::kPrimVoid) {
1404 LOG(FATAL) << "Unreachable type " << type;
1405 }
1406
1407 if (Primitive::IsFloatingPointType(type) &&
1408 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001409 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1410 } else if (!Primitive::IsFloatingPointType(type) &&
1411 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1412 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1413 } else {
1414 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001415 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1416 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001417 }
1418
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001419 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001420 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001421 return next_location;
1422}
1423
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001424Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001425 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001426}
1427
Serban Constantinescu579885a2015-02-22 20:51:33 +00001428CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1429 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001430 const CompilerOptions& compiler_options,
1431 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001432 : CodeGenerator(graph,
1433 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001434 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001435 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001436 callee_saved_core_registers.GetList(),
1437 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001438 compiler_options,
1439 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001440 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001441 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001442 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001443 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001444 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001445 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001446 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001447 uint32_literals_(std::less<uint32_t>(),
1448 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001449 uint64_literals_(std::less<uint64_t>(),
1450 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001451 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001452 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001453 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001454 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001455 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001456 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001457 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001458 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1459 jit_class_patches_(TypeReferenceValueComparator(),
1460 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001461 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001462 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001463}
Alexandre Rames5319def2014-10-23 10:03:10 +01001464
Alexandre Rames67555f72014-11-18 10:55:16 +00001465#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001466
Zheng Xu3927c8b2015-11-18 17:46:25 +08001467void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001468 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001469 jump_table->EmitTable(this);
1470 }
1471}
1472
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001473void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001474 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001475 // Ensure we emit the literal pool.
1476 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001477
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001478 CodeGenerator::Finalize(allocator);
1479}
1480
Zheng Xuad4450e2015-04-17 18:48:56 +08001481void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1482 // Note: There are 6 kinds of moves:
1483 // 1. constant -> GPR/FPR (non-cycle)
1484 // 2. constant -> stack (non-cycle)
1485 // 3. GPR/FPR -> GPR/FPR
1486 // 4. GPR/FPR -> stack
1487 // 5. stack -> GPR/FPR
1488 // 6. stack -> stack (non-cycle)
1489 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1490 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1491 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1492 // dependency.
1493 vixl_temps_.Open(GetVIXLAssembler());
1494}
1495
1496void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1497 vixl_temps_.Close();
1498}
1499
1500Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001501 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1502 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1503 || kind == Location::kSIMDStackSlot);
1504 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1505 ? Location::kFpuRegister
1506 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001507 Location scratch = GetScratchLocation(kind);
1508 if (!scratch.Equals(Location::NoLocation())) {
1509 return scratch;
1510 }
1511 // Allocate from VIXL temp registers.
1512 if (kind == Location::kRegister) {
1513 scratch = LocationFrom(vixl_temps_.AcquireX());
1514 } else {
Roland Levillain952b2352017-05-03 19:49:14 +01001515 DCHECK_EQ(kind, Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001516 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1517 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1518 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001519 }
1520 AddScratchLocation(scratch);
1521 return scratch;
1522}
1523
1524void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1525 if (loc.IsRegister()) {
1526 vixl_temps_.Release(XRegisterFrom(loc));
1527 } else {
1528 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001529 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001530 }
1531 RemoveScratchLocation(loc);
1532}
1533
Alexandre Rames3e69f162014-12-10 10:36:50 +00001534void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001535 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001537}
1538
Alexandre Rames5319def2014-10-23 10:03:10 +01001539void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001540 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001541 __ Bind(&frame_entry_label_);
1542
Serban Constantinescu02164b32014-11-13 14:05:07 +00001543 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1544 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001545 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001546 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001547 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001548 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001549 {
1550 // Ensure that between load and RecordPcInfo there are no pools emitted.
1551 ExactAssemblyScope eas(GetVIXLAssembler(),
1552 kInstructionSize,
1553 CodeBufferCheckScope::kExactSize);
1554 __ ldr(wzr, MemOperand(temp, 0));
1555 RecordPcInfo(nullptr, 0);
1556 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001557 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001558
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001559 if (!HasEmptyFrame()) {
1560 int frame_size = GetFrameSize();
1561 // Stack layout:
1562 // sp[frame_size - 8] : lr.
1563 // ... : other preserved core registers.
1564 // ... : other preserved fp registers.
1565 // ... : reserved frame space.
1566 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001567
1568 // Save the current method if we need it. Note that we do not
1569 // do this in HCurrentMethod, as the instruction might have been removed
1570 // in the SSA graph.
1571 if (RequiresCurrentMethod()) {
1572 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001573 } else {
1574 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001575 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001576 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001577 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1578 frame_size - GetCoreSpillSize());
1579 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1580 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001581
1582 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1583 // Initialize should_deoptimize flag to 0.
1584 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1585 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1586 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001587 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001588}
1589
1590void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001591 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001592 if (!HasEmptyFrame()) {
1593 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001594 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1595 frame_size - FrameEntrySpillSize());
1596 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1597 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001598 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001599 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001600 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001601 __ Ret();
1602 GetAssembler()->cfi().RestoreState();
1603 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001604}
1605
Scott Wakeling97c72b72016-06-24 16:19:36 +01001606CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001607 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001608 return CPURegList(CPURegister::kRegister, kXRegSize,
1609 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001610}
1611
Scott Wakeling97c72b72016-06-24 16:19:36 +01001612CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001613 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1614 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001615 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1616 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001617}
1618
Alexandre Rames5319def2014-10-23 10:03:10 +01001619void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1620 __ Bind(GetLabelOf(block));
1621}
1622
Calin Juravle175dc732015-08-25 15:42:32 +01001623void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1624 DCHECK(location.IsRegister());
1625 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1626}
1627
Calin Juravlee460d1d2015-09-29 04:52:17 +01001628void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1629 if (location.IsRegister()) {
1630 locations->AddTemp(location);
1631 } else {
1632 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1633 }
1634}
1635
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001636void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001637 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001638 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001639 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001640 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001641 if (value_can_be_null) {
1642 __ Cbz(value, &done);
1643 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001644 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001645 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001646 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001647 if (value_can_be_null) {
1648 __ Bind(&done);
1649 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001650}
1651
David Brazdil58282f42016-01-14 12:45:10 +00001652void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001653 // Blocked core registers:
1654 // lr : Runtime reserved.
1655 // tr : Runtime reserved.
1656 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1657 // ip1 : VIXL core temp.
1658 // ip0 : VIXL core temp.
1659 //
1660 // Blocked fp registers:
1661 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001662 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1663 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001664 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001665 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001666 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001667
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001668 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001669 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001670 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001671 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001672
David Brazdil58282f42016-01-14 12:45:10 +00001673 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001674 // Stubs do not save callee-save floating point registers. If the graph
1675 // is debuggable, we need to deal with these registers differently. For
1676 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001677 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1678 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001679 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001680 }
1681 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001682}
1683
Alexandre Rames3e69f162014-12-10 10:36:50 +00001684size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1685 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1686 __ Str(reg, MemOperand(sp, stack_index));
1687 return kArm64WordSize;
1688}
1689
1690size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1691 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1692 __ Ldr(reg, MemOperand(sp, stack_index));
1693 return kArm64WordSize;
1694}
1695
1696size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1697 FPRegister reg = FPRegister(reg_id, kDRegSize);
1698 __ Str(reg, MemOperand(sp, stack_index));
1699 return kArm64WordSize;
1700}
1701
1702size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1703 FPRegister reg = FPRegister(reg_id, kDRegSize);
1704 __ Ldr(reg, MemOperand(sp, stack_index));
1705 return kArm64WordSize;
1706}
1707
Alexandre Rames5319def2014-10-23 10:03:10 +01001708void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001709 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001710}
1711
1712void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001713 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001714}
1715
Alexandre Rames67555f72014-11-18 10:55:16 +00001716void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001717 if (constant->IsIntConstant()) {
1718 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1719 } else if (constant->IsLongConstant()) {
1720 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1721 } else if (constant->IsNullConstant()) {
1722 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001723 } else if (constant->IsFloatConstant()) {
1724 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1725 } else {
1726 DCHECK(constant->IsDoubleConstant());
1727 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1728 }
1729}
1730
Alexandre Rames3e69f162014-12-10 10:36:50 +00001731
1732static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1733 DCHECK(constant.IsConstant());
1734 HConstant* cst = constant.GetConstant();
1735 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001736 // Null is mapped to a core W register, which we associate with kPrimInt.
1737 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001738 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1739 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1740 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1741}
1742
Roland Levillain952b2352017-05-03 19:49:14 +01001743// Allocate a scratch register from the VIXL pool, querying first
1744// the floating-point register pool, and then the core register
1745// pool. This is essentially a reimplementation of
Roland Levillain558dea12017-01-27 19:40:44 +00001746// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1747// using a different allocation strategy.
1748static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1749 vixl::aarch64::UseScratchRegisterScope* temps,
1750 int size_in_bits) {
1751 return masm->GetScratchFPRegisterList()->IsEmpty()
1752 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1753 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1754}
1755
Calin Juravlee460d1d2015-09-29 04:52:17 +01001756void CodeGeneratorARM64::MoveLocation(Location destination,
1757 Location source,
1758 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001759 if (source.Equals(destination)) {
1760 return;
1761 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001762
1763 // A valid move can always be inferred from the destination and source
1764 // locations. When moving from and to a register, the argument type can be
1765 // used to generate 32bit instead of 64bit moves. In debug mode we also
1766 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001767 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001768
1769 if (destination.IsRegister() || destination.IsFpuRegister()) {
1770 if (unspecified_type) {
1771 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1772 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001773 (src_cst != nullptr && (src_cst->IsIntConstant()
1774 || src_cst->IsFloatConstant()
1775 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001776 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001777 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001778 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 // If the source is a double stack slot or a 64bit constant, a 64bit
1780 // type is appropriate. Else the source is a register, and since the
1781 // type has not been specified, we chose a 64bit type to force a 64bit
1782 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001783 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001784 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001786 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1787 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1788 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001789 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1790 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1791 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001792 } else if (source.IsSIMDStackSlot()) {
1793 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001794 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001795 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001796 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001797 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001798 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001799 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001800 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001801 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001802 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1803 ? Primitive::kPrimLong
1804 : Primitive::kPrimInt;
1805 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1806 }
1807 } else {
1808 DCHECK(source.IsFpuRegister());
1809 if (destination.IsRegister()) {
1810 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1811 ? Primitive::kPrimDouble
1812 : Primitive::kPrimFloat;
1813 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1814 } else {
1815 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001816 if (GetGraph()->HasSIMD()) {
1817 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1818 } else {
1819 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1820 }
1821 }
1822 }
1823 } else if (destination.IsSIMDStackSlot()) {
1824 if (source.IsFpuRegister()) {
1825 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1826 } else {
1827 DCHECK(source.IsSIMDStackSlot());
1828 UseScratchRegisterScope temps(GetVIXLAssembler());
1829 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1830 Register temp = temps.AcquireX();
1831 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1832 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1833 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1834 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1835 } else {
1836 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1837 __ Ldr(temp, StackOperandFrom(source));
1838 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001839 }
1840 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001841 } else { // The destination is not a register. It must be a stack slot.
1842 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1843 if (source.IsRegister() || source.IsFpuRegister()) {
1844 if (unspecified_type) {
1845 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001846 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001847 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001848 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001849 }
1850 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001851 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1852 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1853 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001854 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001855 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1856 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001857 UseScratchRegisterScope temps(GetVIXLAssembler());
1858 HConstant* src_cst = source.GetConstant();
1859 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001860 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001861 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1862 ? Register(xzr)
1863 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001864 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001865 if (src_cst->IsIntConstant()) {
1866 temp = temps.AcquireW();
1867 } else if (src_cst->IsLongConstant()) {
1868 temp = temps.AcquireX();
1869 } else if (src_cst->IsFloatConstant()) {
1870 temp = temps.AcquireS();
1871 } else {
1872 DCHECK(src_cst->IsDoubleConstant());
1873 temp = temps.AcquireD();
1874 }
1875 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001876 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001877 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001878 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001879 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001880 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001881 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001882 // Use any scratch register (a core or a floating-point one)
1883 // from VIXL scratch register pools as a temporary.
1884 //
1885 // We used to only use the FP scratch register pool, but in some
1886 // rare cases the only register from this pool (D31) would
1887 // already be used (e.g. within a ParallelMove instruction, when
1888 // a move is blocked by a another move requiring a scratch FP
1889 // register, which would reserve D31). To prevent this issue, we
1890 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001891 //
1892 // Also, we start by asking for a FP scratch register first, as the
Roland Levillain952b2352017-05-03 19:49:14 +01001893 // demand of scratch core registers is higher. This is why we
Roland Levillain558dea12017-01-27 19:40:44 +00001894 // use AcquireFPOrCoreCPURegisterOfSize instead of
1895 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1896 // allocates core scratch registers first.
1897 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1898 GetVIXLAssembler(),
1899 &temps,
1900 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001901 __ Ldr(temp, StackOperandFrom(source));
1902 __ Str(temp, StackOperandFrom(destination));
1903 }
1904 }
1905}
1906
1907void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001908 CPURegister dst,
1909 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001910 switch (type) {
1911 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001912 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001913 break;
1914 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001915 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001916 break;
1917 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001918 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001919 break;
1920 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001921 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001922 break;
1923 case Primitive::kPrimInt:
1924 case Primitive::kPrimNot:
1925 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001926 case Primitive::kPrimFloat:
1927 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001928 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001929 __ Ldr(dst, src);
1930 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001931 case Primitive::kPrimVoid:
1932 LOG(FATAL) << "Unreachable type " << type;
1933 }
1934}
1935
Calin Juravle77520bc2015-01-12 18:45:46 +00001936void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001937 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001938 const MemOperand& src,
1939 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001940 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001941 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001942 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001943 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001944
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001945 DCHECK(!src.IsPreIndex());
1946 DCHECK(!src.IsPostIndex());
1947
1948 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001949 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001950 {
1951 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1952 MemOperand base = MemOperand(temp_base);
1953 switch (type) {
1954 case Primitive::kPrimBoolean:
1955 {
1956 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1957 __ ldarb(Register(dst), base);
1958 if (needs_null_check) {
1959 MaybeRecordImplicitNullCheck(instruction);
1960 }
1961 }
1962 break;
1963 case Primitive::kPrimByte:
1964 {
1965 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1966 __ ldarb(Register(dst), base);
1967 if (needs_null_check) {
1968 MaybeRecordImplicitNullCheck(instruction);
1969 }
1970 }
1971 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1972 break;
1973 case Primitive::kPrimChar:
1974 {
1975 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1976 __ ldarh(Register(dst), base);
1977 if (needs_null_check) {
1978 MaybeRecordImplicitNullCheck(instruction);
1979 }
1980 }
1981 break;
1982 case Primitive::kPrimShort:
1983 {
1984 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1985 __ ldarh(Register(dst), base);
1986 if (needs_null_check) {
1987 MaybeRecordImplicitNullCheck(instruction);
1988 }
1989 }
1990 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1991 break;
1992 case Primitive::kPrimInt:
1993 case Primitive::kPrimNot:
1994 case Primitive::kPrimLong:
1995 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1996 {
1997 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1998 __ ldar(Register(dst), base);
1999 if (needs_null_check) {
2000 MaybeRecordImplicitNullCheck(instruction);
2001 }
2002 }
2003 break;
2004 case Primitive::kPrimFloat:
2005 case Primitive::kPrimDouble: {
2006 DCHECK(dst.IsFPRegister());
2007 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008
Artem Serov914d7a82017-02-07 14:33:49 +00002009 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2010 {
2011 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2012 __ ldar(temp, base);
2013 if (needs_null_check) {
2014 MaybeRecordImplicitNullCheck(instruction);
2015 }
2016 }
2017 __ Fmov(FPRegister(dst), temp);
2018 break;
Roland Levillain44015862016-01-22 11:47:17 +00002019 }
Artem Serov914d7a82017-02-07 14:33:49 +00002020 case Primitive::kPrimVoid:
2021 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002022 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002023 }
2024}
2025
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002026void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002027 CPURegister src,
2028 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002029 switch (type) {
2030 case Primitive::kPrimBoolean:
2031 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002032 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002033 break;
2034 case Primitive::kPrimChar:
2035 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002036 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002037 break;
2038 case Primitive::kPrimInt:
2039 case Primitive::kPrimNot:
2040 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002041 case Primitive::kPrimFloat:
2042 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002043 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002044 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002045 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002046 case Primitive::kPrimVoid:
2047 LOG(FATAL) << "Unreachable type " << type;
2048 }
2049}
2050
Artem Serov914d7a82017-02-07 14:33:49 +00002051void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2052 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002053 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002054 const MemOperand& dst,
2055 bool needs_null_check) {
2056 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002057 UseScratchRegisterScope temps(GetVIXLAssembler());
2058 Register temp_base = temps.AcquireX();
2059
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002060 DCHECK(!dst.IsPreIndex());
2061 DCHECK(!dst.IsPostIndex());
2062
2063 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002064 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002065 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002066 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002067 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002068 switch (type) {
2069 case Primitive::kPrimBoolean:
2070 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002071 {
2072 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2073 __ stlrb(Register(src), base);
2074 if (needs_null_check) {
2075 MaybeRecordImplicitNullCheck(instruction);
2076 }
2077 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002078 break;
2079 case Primitive::kPrimChar:
2080 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002081 {
2082 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2083 __ stlrh(Register(src), base);
2084 if (needs_null_check) {
2085 MaybeRecordImplicitNullCheck(instruction);
2086 }
2087 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002088 break;
2089 case Primitive::kPrimInt:
2090 case Primitive::kPrimNot:
2091 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002092 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002093 {
2094 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2095 __ stlr(Register(src), base);
2096 if (needs_null_check) {
2097 MaybeRecordImplicitNullCheck(instruction);
2098 }
2099 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002100 break;
2101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002103 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002104 Register temp_src;
2105 if (src.IsZero()) {
2106 // The zero register is used to avoid synthesizing zero constants.
2107 temp_src = Register(src);
2108 } else {
2109 DCHECK(src.IsFPRegister());
2110 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2111 __ Fmov(temp_src, FPRegister(src));
2112 }
Artem Serov914d7a82017-02-07 14:33:49 +00002113 {
2114 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2115 __ stlr(temp_src, base);
2116 if (needs_null_check) {
2117 MaybeRecordImplicitNullCheck(instruction);
2118 }
2119 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002120 break;
2121 }
2122 case Primitive::kPrimVoid:
2123 LOG(FATAL) << "Unreachable type " << type;
2124 }
2125}
2126
Calin Juravle175dc732015-08-25 15:42:32 +01002127void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2128 HInstruction* instruction,
2129 uint32_t dex_pc,
2130 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002131 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002132
2133 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2134 {
2135 // Ensure the pc position is recorded immediately after the `blr` instruction.
2136 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2137 __ blr(lr);
2138 if (EntrypointRequiresStackMap(entrypoint)) {
2139 RecordPcInfo(instruction, dex_pc, slow_path);
2140 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002141 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002142}
2143
Roland Levillaindec8f632016-07-22 17:10:06 +01002144void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2145 HInstruction* instruction,
2146 SlowPathCode* slow_path) {
2147 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002148 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2149 __ Blr(lr);
2150}
2151
Alexandre Rames67555f72014-11-18 10:55:16 +00002152void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002153 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002154 UseScratchRegisterScope temps(GetVIXLAssembler());
2155 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002156 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2157
Serban Constantinescu02164b32014-11-13 14:05:07 +00002158 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002159 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2160 __ Add(temp, class_reg, status_offset);
2161 __ Ldar(temp, HeapOperand(temp));
2162 __ Cmp(temp, mirror::Class::kStatusInitialized);
2163 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002164 __ Bind(slow_path->GetExitLabel());
2165}
Alexandre Rames5319def2014-10-23 10:03:10 +01002166
Roland Levillain44015862016-01-22 11:47:17 +00002167void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002168 BarrierType type = BarrierAll;
2169
2170 switch (kind) {
2171 case MemBarrierKind::kAnyAny:
2172 case MemBarrierKind::kAnyStore: {
2173 type = BarrierAll;
2174 break;
2175 }
2176 case MemBarrierKind::kLoadAny: {
2177 type = BarrierReads;
2178 break;
2179 }
2180 case MemBarrierKind::kStoreStore: {
2181 type = BarrierWrites;
2182 break;
2183 }
2184 default:
2185 LOG(FATAL) << "Unexpected memory barrier " << kind;
2186 }
2187 __ Dmb(InnerShareable, type);
2188}
2189
Serban Constantinescu02164b32014-11-13 14:05:07 +00002190void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2191 HBasicBlock* successor) {
2192 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002193 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2194 if (slow_path == nullptr) {
2195 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2196 instruction->SetSlowPath(slow_path);
2197 codegen_->AddSlowPath(slow_path);
2198 if (successor != nullptr) {
2199 DCHECK(successor->IsLoopHeader());
2200 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2201 }
2202 } else {
2203 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2204 }
2205
Serban Constantinescu02164b32014-11-13 14:05:07 +00002206 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2207 Register temp = temps.AcquireW();
2208
Andreas Gampe542451c2016-07-26 09:02:02 -07002209 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002210 if (successor == nullptr) {
2211 __ Cbnz(temp, slow_path->GetEntryLabel());
2212 __ Bind(slow_path->GetReturnLabel());
2213 } else {
2214 __ Cbz(temp, codegen_->GetLabelOf(successor));
2215 __ B(slow_path->GetEntryLabel());
2216 // slow_path will return to GetLabelOf(successor).
2217 }
2218}
2219
Alexandre Rames5319def2014-10-23 10:03:10 +01002220InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2221 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002222 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002223 assembler_(codegen->GetAssembler()),
2224 codegen_(codegen) {}
2225
2226#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002227 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002228
2229#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2230
2231enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002232 // Using a base helps identify when we hit such breakpoints.
2233 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002234#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2235 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2236#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2237};
2238
2239#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002240 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002241 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2242 } \
2243 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2244 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2245 locations->SetOut(Location::Any()); \
2246 }
2247 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2248#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2249
2250#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002251#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002252
Alexandre Rames67555f72014-11-18 10:55:16 +00002253void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002254 DCHECK_EQ(instr->InputCount(), 2U);
2255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2256 Primitive::Type type = instr->GetResultType();
2257 switch (type) {
2258 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002259 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002260 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002261 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002262 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002263 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002264
2265 case Primitive::kPrimFloat:
2266 case Primitive::kPrimDouble:
2267 locations->SetInAt(0, Location::RequiresFpuRegister());
2268 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002269 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002270 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002271
Alexandre Rames5319def2014-10-23 10:03:10 +01002272 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002273 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002274 }
2275}
2276
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002277void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2278 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002279 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2280
2281 bool object_field_get_with_read_barrier =
2282 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002283 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002284 new (GetGraph()->GetArena()) LocationSummary(instruction,
2285 object_field_get_with_read_barrier ?
2286 LocationSummary::kCallOnSlowPath :
2287 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002288 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002289 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002290 // We need a temporary register for the read barrier marking slow
2291 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002292 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2293 !Runtime::Current()->UseJitCompilation() &&
2294 !field_info.IsVolatile()) {
2295 // If link-time thunks for the Baker read barrier are enabled, for AOT
2296 // non-volatile loads we need a temporary only if the offset is too big.
2297 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2298 locations->AddTemp(FixedTempLocation());
2299 }
2300 } else {
2301 locations->AddTemp(Location::RequiresRegister());
2302 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002303 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002304 locations->SetInAt(0, Location::RequiresRegister());
2305 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2306 locations->SetOut(Location::RequiresFpuRegister());
2307 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002308 // The output overlaps for an object field get when read barriers
2309 // are enabled: we do not want the load to overwrite the object's
2310 // location, as we need it to emit the read barrier.
2311 locations->SetOut(
2312 Location::RequiresRegister(),
2313 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002314 }
2315}
2316
2317void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2318 const FieldInfo& field_info) {
2319 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002320 LocationSummary* locations = instruction->GetLocations();
2321 Location base_loc = locations->InAt(0);
2322 Location out = locations->Out();
2323 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002324 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002325 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002326
Roland Levillain44015862016-01-22 11:47:17 +00002327 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2328 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002329 // /* HeapReference<Object> */ out = *(base + offset)
2330 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002331 Location maybe_temp =
2332 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002333 // Note that potential implicit null checks are handled in this
2334 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2335 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2336 instruction,
2337 out,
2338 base,
2339 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002340 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002341 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002342 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002343 } else {
2344 // General case.
2345 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002346 // Note that a potential implicit null check is handled in this
2347 // CodeGeneratorARM64::LoadAcquire call.
2348 // NB: LoadAcquire will record the pc info if needed.
2349 codegen_->LoadAcquire(
2350 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002351 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002352 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2353 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002354 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002356 }
Roland Levillain44015862016-01-22 11:47:17 +00002357 if (field_type == Primitive::kPrimNot) {
2358 // If read barriers are enabled, emit read barriers other than
2359 // Baker's using a slow path (and also unpoison the loaded
2360 // reference, if heap poisoning is enabled).
2361 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2362 }
Roland Levillain4d027112015-07-01 15:41:14 +01002363 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002364}
2365
2366void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2367 LocationSummary* locations =
2368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2369 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002370 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2371 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2372 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002373 locations->SetInAt(1, Location::RequiresFpuRegister());
2374 } else {
2375 locations->SetInAt(1, Location::RequiresRegister());
2376 }
2377}
2378
2379void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002380 const FieldInfo& field_info,
2381 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002382 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2383
2384 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002385 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002386 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002387 Offset offset = field_info.GetFieldOffset();
2388 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002389
Roland Levillain4d027112015-07-01 15:41:14 +01002390 {
2391 // We use a block to end the scratch scope before the write barrier, thus
2392 // freeing the temporary registers so they can be used in `MarkGCCard`.
2393 UseScratchRegisterScope temps(GetVIXLAssembler());
2394
2395 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2396 DCHECK(value.IsW());
2397 Register temp = temps.AcquireW();
2398 __ Mov(temp, value.W());
2399 GetAssembler()->PoisonHeapReference(temp.W());
2400 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002401 }
Roland Levillain4d027112015-07-01 15:41:14 +01002402
2403 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002404 codegen_->StoreRelease(
2405 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002406 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002407 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2408 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002409 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2410 codegen_->MaybeRecordImplicitNullCheck(instruction);
2411 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002412 }
2413
2414 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002415 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002416 }
2417}
2418
Alexandre Rames67555f72014-11-18 10:55:16 +00002419void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002421
2422 switch (type) {
2423 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002424 case Primitive::kPrimLong: {
2425 Register dst = OutputRegister(instr);
2426 Register lhs = InputRegisterAt(instr, 0);
2427 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002428 if (instr->IsAdd()) {
2429 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002430 } else if (instr->IsAnd()) {
2431 __ And(dst, lhs, rhs);
2432 } else if (instr->IsOr()) {
2433 __ Orr(dst, lhs, rhs);
2434 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002435 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002436 } else if (instr->IsRor()) {
2437 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002438 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002439 __ Ror(dst, lhs, shift);
2440 } else {
2441 // Ensure shift distance is in the same size register as the result. If
2442 // we are rotating a long and the shift comes in a w register originally,
2443 // we don't need to sxtw for use as an x since the shift distances are
2444 // all & reg_bits - 1.
2445 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2446 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002447 } else {
2448 DCHECK(instr->IsXor());
2449 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002450 }
2451 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002452 }
2453 case Primitive::kPrimFloat:
2454 case Primitive::kPrimDouble: {
2455 FPRegister dst = OutputFPRegister(instr);
2456 FPRegister lhs = InputFPRegisterAt(instr, 0);
2457 FPRegister rhs = InputFPRegisterAt(instr, 1);
2458 if (instr->IsAdd()) {
2459 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002460 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002461 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002462 } else {
2463 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002464 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002465 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002466 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002467 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002468 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002469 }
2470}
2471
Serban Constantinescu02164b32014-11-13 14:05:07 +00002472void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2473 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2474
2475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2476 Primitive::Type type = instr->GetResultType();
2477 switch (type) {
2478 case Primitive::kPrimInt:
2479 case Primitive::kPrimLong: {
2480 locations->SetInAt(0, Location::RequiresRegister());
2481 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002483 break;
2484 }
2485 default:
2486 LOG(FATAL) << "Unexpected shift type " << type;
2487 }
2488}
2489
2490void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2491 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2492
2493 Primitive::Type type = instr->GetType();
2494 switch (type) {
2495 case Primitive::kPrimInt:
2496 case Primitive::kPrimLong: {
2497 Register dst = OutputRegister(instr);
2498 Register lhs = InputRegisterAt(instr, 0);
2499 Operand rhs = InputOperandAt(instr, 1);
2500 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002501 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002502 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002503 if (instr->IsShl()) {
2504 __ Lsl(dst, lhs, shift_value);
2505 } else if (instr->IsShr()) {
2506 __ Asr(dst, lhs, shift_value);
2507 } else {
2508 __ Lsr(dst, lhs, shift_value);
2509 }
2510 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002511 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002512
2513 if (instr->IsShl()) {
2514 __ Lsl(dst, lhs, rhs_reg);
2515 } else if (instr->IsShr()) {
2516 __ Asr(dst, lhs, rhs_reg);
2517 } else {
2518 __ Lsr(dst, lhs, rhs_reg);
2519 }
2520 }
2521 break;
2522 }
2523 default:
2524 LOG(FATAL) << "Unexpected shift operation type " << type;
2525 }
2526}
2527
Alexandre Rames5319def2014-10-23 10:03:10 +01002528void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002529 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002530}
2531
2532void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002533 HandleBinaryOp(instruction);
2534}
2535
2536void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2537 HandleBinaryOp(instruction);
2538}
2539
2540void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2541 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002542}
2543
Artem Serov7fc63502016-02-09 17:15:29 +00002544void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002545 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2546 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2547 locations->SetInAt(0, Location::RequiresRegister());
2548 // There is no immediate variant of negated bitwise instructions in AArch64.
2549 locations->SetInAt(1, Location::RequiresRegister());
2550 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2551}
2552
Artem Serov7fc63502016-02-09 17:15:29 +00002553void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002554 Register dst = OutputRegister(instr);
2555 Register lhs = InputRegisterAt(instr, 0);
2556 Register rhs = InputRegisterAt(instr, 1);
2557
2558 switch (instr->GetOpKind()) {
2559 case HInstruction::kAnd:
2560 __ Bic(dst, lhs, rhs);
2561 break;
2562 case HInstruction::kOr:
2563 __ Orn(dst, lhs, rhs);
2564 break;
2565 case HInstruction::kXor:
2566 __ Eon(dst, lhs, rhs);
2567 break;
2568 default:
2569 LOG(FATAL) << "Unreachable";
2570 }
2571}
2572
Anton Kirilov74234da2017-01-13 14:42:47 +00002573void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2574 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002575 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2576 instruction->GetType() == Primitive::kPrimLong);
2577 LocationSummary* locations =
2578 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2579 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2580 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2581 } else {
2582 locations->SetInAt(0, Location::RequiresRegister());
2583 }
2584 locations->SetInAt(1, Location::RequiresRegister());
2585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2586}
2587
Anton Kirilov74234da2017-01-13 14:42:47 +00002588void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2589 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002590 Primitive::Type type = instruction->GetType();
2591 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2592 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2593 Register out = OutputRegister(instruction);
2594 Register left;
2595 if (kind != HInstruction::kNeg) {
2596 left = InputRegisterAt(instruction, 0);
2597 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002598 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002599 // shifter operand operation, the IR generating `right_reg` (input to the type
2600 // conversion) can have a different type from the current instruction's type,
2601 // so we manually indicate the type.
2602 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002603 Operand right_operand(0);
2604
Anton Kirilov74234da2017-01-13 14:42:47 +00002605 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2606 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002607 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2608 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002609 right_operand = Operand(right_reg,
2610 helpers::ShiftFromOpKind(op_kind),
2611 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002612 }
2613
2614 // Logical binary operations do not support extension operations in the
2615 // operand. Note that VIXL would still manage if it was passed by generating
2616 // the extension as a separate instruction.
2617 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2618 DCHECK(!right_operand.IsExtendedRegister() ||
2619 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2620 kind != HInstruction::kNeg));
2621 switch (kind) {
2622 case HInstruction::kAdd:
2623 __ Add(out, left, right_operand);
2624 break;
2625 case HInstruction::kAnd:
2626 __ And(out, left, right_operand);
2627 break;
2628 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002629 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002630 __ Neg(out, right_operand);
2631 break;
2632 case HInstruction::kOr:
2633 __ Orr(out, left, right_operand);
2634 break;
2635 case HInstruction::kSub:
2636 __ Sub(out, left, right_operand);
2637 break;
2638 case HInstruction::kXor:
2639 __ Eor(out, left, right_operand);
2640 break;
2641 default:
2642 LOG(FATAL) << "Unexpected operation kind: " << kind;
2643 UNREACHABLE();
2644 }
2645}
2646
Artem Serov328429f2016-07-06 16:23:04 +01002647void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002648 LocationSummary* locations =
2649 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2650 locations->SetInAt(0, Location::RequiresRegister());
2651 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002653}
2654
Roland Levillain19c54192016-11-04 13:44:09 +00002655void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002656 __ Add(OutputRegister(instruction),
2657 InputRegisterAt(instruction, 0),
2658 Operand(InputOperandAt(instruction, 1)));
2659}
2660
Artem Serove1811ed2017-04-27 16:50:47 +01002661void LocationsBuilderARM64::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
2662 LocationSummary* locations =
2663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2664
2665 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
2666
2667 locations->SetInAt(0, Location::RequiresRegister());
2668 // For byte case we don't need to shift the index variable so we can encode the data offset into
2669 // ADD instruction. For other cases we prefer the data_offset to be in register; that will hoist
2670 // data offset constant generation out of the loop and reduce the critical path length in the
2671 // loop.
2672 locations->SetInAt(1, shift->GetValue() == 0
2673 ? Location::ConstantLocation(instruction->GetOffset()->AsIntConstant())
2674 : Location::RequiresRegister());
2675 locations->SetInAt(2, Location::ConstantLocation(shift));
2676 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2677}
2678
2679void InstructionCodeGeneratorARM64::VisitIntermediateAddressIndex(
2680 HIntermediateAddressIndex* instruction) {
2681 Register index_reg = InputRegisterAt(instruction, 0);
2682 uint32_t shift = Int64ConstantFrom(instruction->GetLocations()->InAt(2));
2683 uint32_t offset = instruction->GetOffset()->AsIntConstant()->GetValue();
2684
2685 if (shift == 0) {
2686 __ Add(OutputRegister(instruction), index_reg, offset);
2687 } else {
2688 Register offset_reg = InputRegisterAt(instruction, 1);
2689 __ Add(OutputRegister(instruction), offset_reg, Operand(index_reg, LSL, shift));
2690 }
2691}
2692
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002693void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002694 LocationSummary* locations =
2695 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002696 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2697 if (instr->GetOpKind() == HInstruction::kSub &&
2698 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002699 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002700 // Don't allocate register for Mneg instruction.
2701 } else {
2702 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2703 Location::RequiresRegister());
2704 }
2705 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2706 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2708}
2709
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002710void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002711 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002712 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2713 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002714
2715 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2716 // This fixup should be carried out for all multiply-accumulate instructions:
2717 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2718 if (instr->GetType() == Primitive::kPrimLong &&
2719 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2720 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002721 vixl::aarch64::Instruction* prev =
2722 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002723 if (prev->IsLoadOrStore()) {
2724 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002725 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002726 __ nop();
2727 }
2728 }
2729
2730 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002731 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002732 __ Madd(res, mul_left, mul_right, accumulator);
2733 } else {
2734 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002735 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002736 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002737 __ Mneg(res, mul_left, mul_right);
2738 } else {
2739 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2740 __ Msub(res, mul_left, mul_right, accumulator);
2741 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002742 }
2743}
2744
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002745void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002746 bool object_array_get_with_read_barrier =
2747 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002748 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002749 new (GetGraph()->GetArena()) LocationSummary(instruction,
2750 object_array_get_with_read_barrier ?
2751 LocationSummary::kCallOnSlowPath :
2752 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002753 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002754 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002755 // We need a temporary register for the read barrier marking slow
2756 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002757 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2758 !Runtime::Current()->UseJitCompilation() &&
2759 instruction->GetIndex()->IsConstant()) {
2760 // Array loads with constant index are treated as field loads.
2761 // If link-time thunks for the Baker read barrier are enabled, for AOT
2762 // constant index loads we need a temporary only if the offset is too big.
2763 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2764 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2765 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2766 if (offset >= kReferenceLoadMinFarOffset) {
2767 locations->AddTemp(FixedTempLocation());
2768 }
2769 } else {
2770 locations->AddTemp(Location::RequiresRegister());
2771 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002772 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002773 locations->SetInAt(0, Location::RequiresRegister());
2774 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002775 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2776 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2777 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002778 // The output overlaps in the case of an object array get with
2779 // read barriers enabled: we do not want the move to overwrite the
2780 // array's location, as we need it to emit the read barrier.
2781 locations->SetOut(
2782 Location::RequiresRegister(),
2783 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002784 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002785}
2786
2787void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002788 Primitive::Type type = instruction->GetType();
2789 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002790 LocationSummary* locations = instruction->GetLocations();
2791 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002792 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002793 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002794 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2795 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002796 MacroAssembler* masm = GetVIXLAssembler();
2797 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002798
Roland Levillain19c54192016-11-04 13:44:09 +00002799 // The read barrier instrumentation of object ArrayGet instructions
2800 // does not support the HIntermediateAddress instruction.
2801 DCHECK(!((type == Primitive::kPrimNot) &&
2802 instruction->GetArray()->IsIntermediateAddress() &&
2803 kEmitCompilerReadBarrier));
2804
Roland Levillain44015862016-01-22 11:47:17 +00002805 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2806 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002807 // Note that a potential implicit null check is handled in the
2808 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Marko66d691d2017-04-07 17:53:39 +01002809 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002810 if (index.IsConstant()) {
2811 // Array load with a constant index can be treated as a field load.
2812 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2813 Location maybe_temp =
2814 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2815 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2816 out,
2817 obj.W(),
2818 offset,
2819 maybe_temp,
Vladimir Marko66d691d2017-04-07 17:53:39 +01002820 /* needs_null_check */ false,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002821 /* use_load_acquire */ false);
2822 } else {
2823 Register temp = WRegisterFrom(locations->GetTemp(0));
2824 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko66d691d2017-04-07 17:53:39 +01002825 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ false);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002826 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002827 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002828 // General case.
2829 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002830 Register length;
2831 if (maybe_compressed_char_at) {
2832 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2833 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002834 {
2835 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2836 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2837
2838 if (instruction->GetArray()->IsIntermediateAddress()) {
2839 DCHECK_LT(count_offset, offset);
2840 int64_t adjusted_offset =
2841 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2842 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2843 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2844 } else {
2845 __ Ldr(length, HeapOperand(obj, count_offset));
2846 }
2847 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002848 }
jessicahandojo05765752016-09-09 19:01:32 -07002849 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002850 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002851 if (maybe_compressed_char_at) {
2852 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002853 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2854 "Expecting 0=compressed, 1=uncompressed");
2855 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002856 __ Ldrb(Register(OutputCPURegister(instruction)),
2857 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2858 __ B(&done);
2859 __ Bind(&uncompressed_load);
2860 __ Ldrh(Register(OutputCPURegister(instruction)),
2861 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2862 __ Bind(&done);
2863 } else {
2864 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2865 source = HeapOperand(obj, offset);
2866 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002867 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002868 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002869 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002870 // We do not need to compute the intermediate address from the array: the
2871 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002872 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002873 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002874 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002875 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2876 }
2877 temp = obj;
2878 } else {
2879 __ Add(temp, obj, offset);
2880 }
jessicahandojo05765752016-09-09 19:01:32 -07002881 if (maybe_compressed_char_at) {
2882 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002883 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2884 "Expecting 0=compressed, 1=uncompressed");
2885 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002886 __ Ldrb(Register(OutputCPURegister(instruction)),
2887 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2888 __ B(&done);
2889 __ Bind(&uncompressed_load);
2890 __ Ldrh(Register(OutputCPURegister(instruction)),
2891 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2892 __ Bind(&done);
2893 } else {
2894 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2895 }
Roland Levillain44015862016-01-22 11:47:17 +00002896 }
jessicahandojo05765752016-09-09 19:01:32 -07002897 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002898 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2899 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002900 codegen_->Load(type, OutputCPURegister(instruction), source);
2901 codegen_->MaybeRecordImplicitNullCheck(instruction);
2902 }
Roland Levillain44015862016-01-22 11:47:17 +00002903
2904 if (type == Primitive::kPrimNot) {
2905 static_assert(
2906 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2907 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2908 Location obj_loc = locations->InAt(0);
2909 if (index.IsConstant()) {
2910 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2911 } else {
2912 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2913 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002914 }
Roland Levillain4d027112015-07-01 15:41:14 +01002915 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002916}
2917
Alexandre Rames5319def2014-10-23 10:03:10 +01002918void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2920 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002921 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002922}
2923
2924void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002925 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002926 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002927 {
2928 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2929 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2930 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2931 codegen_->MaybeRecordImplicitNullCheck(instruction);
2932 }
jessicahandojo05765752016-09-09 19:01:32 -07002933 // Mask out compression flag from String's array length.
2934 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002935 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002936 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002937}
2938
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002939void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002940 Primitive::Type value_type = instruction->GetComponentType();
2941
2942 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002943 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2944 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002945 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002946 LocationSummary::kCallOnSlowPath :
2947 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002948 locations->SetInAt(0, Location::RequiresRegister());
2949 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002950 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2951 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2952 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002953 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002954 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002955 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002956 }
2957}
2958
2959void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2960 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002961 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002962 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002963 bool needs_write_barrier =
2964 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002965
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002966 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002967 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002968 CPURegister source = value;
2969 Location index = locations->InAt(1);
2970 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2971 MemOperand destination = HeapOperand(array);
2972 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002973
2974 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002975 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976 if (index.IsConstant()) {
2977 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2978 destination = HeapOperand(array, offset);
2979 } else {
2980 UseScratchRegisterScope temps(masm);
2981 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002982 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002983 // We do not need to compute the intermediate address from the array: the
2984 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002985 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002986 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002987 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002988 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2989 }
2990 temp = array;
2991 } else {
2992 __ Add(temp, array, offset);
2993 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002994 destination = HeapOperand(temp,
2995 XRegisterFrom(index),
2996 LSL,
2997 Primitive::ComponentSizeShift(value_type));
2998 }
Artem Serov914d7a82017-02-07 14:33:49 +00002999 {
3000 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3001 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3002 codegen_->Store(value_type, value, destination);
3003 codegen_->MaybeRecordImplicitNullCheck(instruction);
3004 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003005 } else {
Artem Serov328429f2016-07-06 16:23:04 +01003006 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01003007 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003008 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01003009 {
3010 // We use a block to end the scratch scope before the write barrier, thus
3011 // freeing the temporary registers so they can be used in `MarkGCCard`.
3012 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003013 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01003014 if (index.IsConstant()) {
3015 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003016 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01003017 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01003018 destination = HeapOperand(temp,
3019 XRegisterFrom(index),
3020 LSL,
3021 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01003022 }
3023
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003024 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3025 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3026 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3027
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003028 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003029 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
3030 codegen_->AddSlowPath(slow_path);
3031 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01003032 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003033 __ Cbnz(Register(value), &non_zero);
3034 if (!index.IsConstant()) {
3035 __ Add(temp, array, offset);
3036 }
Artem Serov914d7a82017-02-07 14:33:49 +00003037 {
3038 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
3039 // emitted.
3040 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3041 __ Str(wzr, destination);
3042 codegen_->MaybeRecordImplicitNullCheck(instruction);
3043 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003044 __ B(&done);
3045 __ Bind(&non_zero);
3046 }
3047
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003048 // Note that when Baker read barriers are enabled, the type
3049 // checks are performed without read barriers. This is fine,
3050 // even in the case where a class object is in the from-space
3051 // after the flip, as a comparison involving such a type would
3052 // not produce a false positive; it may of course produce a
3053 // false negative, in which case we would take the ArraySet
3054 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003055
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003056 Register temp2 = temps.AcquireSameSizeAs(array);
3057 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003058 {
3059 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3060 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3061 __ Ldr(temp, HeapOperand(array, class_offset));
3062 codegen_->MaybeRecordImplicitNullCheck(instruction);
3063 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003064 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003065
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003066 // /* HeapReference<Class> */ temp = temp->component_type_
3067 __ Ldr(temp, HeapOperand(temp, component_offset));
3068 // /* HeapReference<Class> */ temp2 = value->klass_
3069 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3070 // If heap poisoning is enabled, no need to unpoison `temp`
3071 // nor `temp2`, as we are comparing two poisoned references.
3072 __ Cmp(temp, temp2);
3073 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003074
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003075 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3076 vixl::aarch64::Label do_put;
3077 __ B(eq, &do_put);
3078 // If heap poisoning is enabled, the `temp` reference has
3079 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003080 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3081
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003082 // /* HeapReference<Class> */ temp = temp->super_class_
3083 __ Ldr(temp, HeapOperand(temp, super_offset));
3084 // If heap poisoning is enabled, no need to unpoison
3085 // `temp`, as we are comparing against null below.
3086 __ Cbnz(temp, slow_path->GetEntryLabel());
3087 __ Bind(&do_put);
3088 } else {
3089 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003090 }
3091 }
3092
3093 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003094 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003095 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003096 __ Mov(temp2, value.W());
3097 GetAssembler()->PoisonHeapReference(temp2);
3098 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003099 }
3100
3101 if (!index.IsConstant()) {
3102 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003103 } else {
3104 // We no longer need the `temp` here so release it as the store below may
3105 // need a scratch register (if the constant index makes the offset too large)
3106 // and the poisoned `source` could be using the other scratch register.
3107 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003108 }
Artem Serov914d7a82017-02-07 14:33:49 +00003109 {
3110 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3111 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3112 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003113
Artem Serov914d7a82017-02-07 14:33:49 +00003114 if (!may_need_runtime_call_for_type_check) {
3115 codegen_->MaybeRecordImplicitNullCheck(instruction);
3116 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003117 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003118 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003119
3120 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3121
3122 if (done.IsLinked()) {
3123 __ Bind(&done);
3124 }
3125
3126 if (slow_path != nullptr) {
3127 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003128 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003129 }
3130}
3131
Alexandre Rames67555f72014-11-18 10:55:16 +00003132void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003133 RegisterSet caller_saves = RegisterSet::Empty();
3134 InvokeRuntimeCallingConvention calling_convention;
3135 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3136 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3137 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003138 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003139 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003140}
3141
3142void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003143 BoundsCheckSlowPathARM64* slow_path =
3144 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003145 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003146 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3147 __ B(slow_path->GetEntryLabel(), hs);
3148}
3149
Alexandre Rames67555f72014-11-18 10:55:16 +00003150void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3151 LocationSummary* locations =
3152 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3153 locations->SetInAt(0, Location::RequiresRegister());
3154 if (check->HasUses()) {
3155 locations->SetOut(Location::SameAsFirstInput());
3156 }
3157}
3158
3159void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3160 // We assume the class is not null.
3161 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3162 check->GetLoadClass(), check, check->GetDexPc(), true);
3163 codegen_->AddSlowPath(slow_path);
3164 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3165}
3166
Roland Levillain1a653882016-03-18 18:05:57 +00003167static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3168 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3169 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3170}
3171
3172void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3173 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3174 Location rhs_loc = instruction->GetLocations()->InAt(1);
3175 if (rhs_loc.IsConstant()) {
3176 // 0.0 is the only immediate that can be encoded directly in
3177 // an FCMP instruction.
3178 //
3179 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3180 // specify that in a floating-point comparison, positive zero
3181 // and negative zero are considered equal, so we can use the
3182 // literal 0.0 for both cases here.
3183 //
3184 // Note however that some methods (Float.equal, Float.compare,
3185 // Float.compareTo, Double.equal, Double.compare,
3186 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3187 // StrictMath.min) consider 0.0 to be (strictly) greater than
3188 // -0.0. So if we ever translate calls to these methods into a
3189 // HCompare instruction, we must handle the -0.0 case with
3190 // care here.
3191 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3192 __ Fcmp(lhs_reg, 0.0);
3193 } else {
3194 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3195 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003196}
3197
Serban Constantinescu02164b32014-11-13 14:05:07 +00003198void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003199 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3201 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003202 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003203 case Primitive::kPrimBoolean:
3204 case Primitive::kPrimByte:
3205 case Primitive::kPrimShort:
3206 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003207 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003208 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003209 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003210 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3212 break;
3213 }
3214 case Primitive::kPrimFloat:
3215 case Primitive::kPrimDouble: {
3216 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003217 locations->SetInAt(1,
3218 IsFloatingPointZeroConstant(compare->InputAt(1))
3219 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3220 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003221 locations->SetOut(Location::RequiresRegister());
3222 break;
3223 }
3224 default:
3225 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3226 }
3227}
3228
3229void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3230 Primitive::Type in_type = compare->InputAt(0)->GetType();
3231
3232 // 0 if: left == right
3233 // 1 if: left > right
3234 // -1 if: left < right
3235 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003236 case Primitive::kPrimBoolean:
3237 case Primitive::kPrimByte:
3238 case Primitive::kPrimShort:
3239 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003240 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003241 case Primitive::kPrimLong: {
3242 Register result = OutputRegister(compare);
3243 Register left = InputRegisterAt(compare, 0);
3244 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003245 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003246 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3247 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003248 break;
3249 }
3250 case Primitive::kPrimFloat:
3251 case Primitive::kPrimDouble: {
3252 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003253 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003254 __ Cset(result, ne);
3255 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003256 break;
3257 }
3258 default:
3259 LOG(FATAL) << "Unimplemented compare type " << in_type;
3260 }
3261}
3262
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003263void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003265
3266 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3267 locations->SetInAt(0, Location::RequiresFpuRegister());
3268 locations->SetInAt(1,
3269 IsFloatingPointZeroConstant(instruction->InputAt(1))
3270 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3271 : Location::RequiresFpuRegister());
3272 } else {
3273 // Integer cases.
3274 locations->SetInAt(0, Location::RequiresRegister());
3275 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3276 }
3277
David Brazdilb3e773e2016-01-26 11:28:37 +00003278 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003280 }
3281}
3282
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003283void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003284 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003285 return;
3286 }
3287
3288 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003289 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003290 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003291
Roland Levillain7f63c522015-07-13 15:54:55 +00003292 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003293 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003294 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003295 } else {
3296 // Integer cases.
3297 Register lhs = InputRegisterAt(instruction, 0);
3298 Operand rhs = InputOperandAt(instruction, 1);
3299 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003300 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003301 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003302}
3303
3304#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3305 M(Equal) \
3306 M(NotEqual) \
3307 M(LessThan) \
3308 M(LessThanOrEqual) \
3309 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003310 M(GreaterThanOrEqual) \
3311 M(Below) \
3312 M(BelowOrEqual) \
3313 M(Above) \
3314 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003315#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003316void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3317void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003318FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003319#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003320#undef FOR_EACH_CONDITION_INSTRUCTION
3321
Zheng Xuc6667102015-05-15 16:08:45 +08003322void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3323 DCHECK(instruction->IsDiv() || instruction->IsRem());
3324
3325 LocationSummary* locations = instruction->GetLocations();
3326 Location second = locations->InAt(1);
3327 DCHECK(second.IsConstant());
3328
3329 Register out = OutputRegister(instruction);
3330 Register dividend = InputRegisterAt(instruction, 0);
3331 int64_t imm = Int64FromConstant(second.GetConstant());
3332 DCHECK(imm == 1 || imm == -1);
3333
3334 if (instruction->IsRem()) {
3335 __ Mov(out, 0);
3336 } else {
3337 if (imm == 1) {
3338 __ Mov(out, dividend);
3339 } else {
3340 __ Neg(out, dividend);
3341 }
3342 }
3343}
3344
3345void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3346 DCHECK(instruction->IsDiv() || instruction->IsRem());
3347
3348 LocationSummary* locations = instruction->GetLocations();
3349 Location second = locations->InAt(1);
3350 DCHECK(second.IsConstant());
3351
3352 Register out = OutputRegister(instruction);
3353 Register dividend = InputRegisterAt(instruction, 0);
3354 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003355 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003356 int ctz_imm = CTZ(abs_imm);
3357
3358 UseScratchRegisterScope temps(GetVIXLAssembler());
3359 Register temp = temps.AcquireSameSizeAs(out);
3360
3361 if (instruction->IsDiv()) {
3362 __ Add(temp, dividend, abs_imm - 1);
3363 __ Cmp(dividend, 0);
3364 __ Csel(out, temp, dividend, lt);
3365 if (imm > 0) {
3366 __ Asr(out, out, ctz_imm);
3367 } else {
3368 __ Neg(out, Operand(out, ASR, ctz_imm));
3369 }
3370 } else {
3371 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3372 __ Asr(temp, dividend, bits - 1);
3373 __ Lsr(temp, temp, bits - ctz_imm);
3374 __ Add(out, dividend, temp);
3375 __ And(out, out, abs_imm - 1);
3376 __ Sub(out, out, temp);
3377 }
3378}
3379
3380void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3381 DCHECK(instruction->IsDiv() || instruction->IsRem());
3382
3383 LocationSummary* locations = instruction->GetLocations();
3384 Location second = locations->InAt(1);
3385 DCHECK(second.IsConstant());
3386
3387 Register out = OutputRegister(instruction);
3388 Register dividend = InputRegisterAt(instruction, 0);
3389 int64_t imm = Int64FromConstant(second.GetConstant());
3390
3391 Primitive::Type type = instruction->GetResultType();
3392 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3393
3394 int64_t magic;
3395 int shift;
3396 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3397
3398 UseScratchRegisterScope temps(GetVIXLAssembler());
3399 Register temp = temps.AcquireSameSizeAs(out);
3400
3401 // temp = get_high(dividend * magic)
3402 __ Mov(temp, magic);
3403 if (type == Primitive::kPrimLong) {
3404 __ Smulh(temp, dividend, temp);
3405 } else {
3406 __ Smull(temp.X(), dividend, temp);
3407 __ Lsr(temp.X(), temp.X(), 32);
3408 }
3409
3410 if (imm > 0 && magic < 0) {
3411 __ Add(temp, temp, dividend);
3412 } else if (imm < 0 && magic > 0) {
3413 __ Sub(temp, temp, dividend);
3414 }
3415
3416 if (shift != 0) {
3417 __ Asr(temp, temp, shift);
3418 }
3419
3420 if (instruction->IsDiv()) {
3421 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3422 } else {
3423 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3424 // TODO: Strength reduction for msub.
3425 Register temp_imm = temps.AcquireSameSizeAs(out);
3426 __ Mov(temp_imm, imm);
3427 __ Msub(out, temp, temp_imm, dividend);
3428 }
3429}
3430
3431void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3432 DCHECK(instruction->IsDiv() || instruction->IsRem());
3433 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003434 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003435
3436 LocationSummary* locations = instruction->GetLocations();
3437 Register out = OutputRegister(instruction);
3438 Location second = locations->InAt(1);
3439
3440 if (second.IsConstant()) {
3441 int64_t imm = Int64FromConstant(second.GetConstant());
3442
3443 if (imm == 0) {
3444 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3445 } else if (imm == 1 || imm == -1) {
3446 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003447 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003448 DivRemByPowerOfTwo(instruction);
3449 } else {
3450 DCHECK(imm <= -2 || imm >= 2);
3451 GenerateDivRemWithAnyConstant(instruction);
3452 }
3453 } else {
3454 Register dividend = InputRegisterAt(instruction, 0);
3455 Register divisor = InputRegisterAt(instruction, 1);
3456 if (instruction->IsDiv()) {
3457 __ Sdiv(out, dividend, divisor);
3458 } else {
3459 UseScratchRegisterScope temps(GetVIXLAssembler());
3460 Register temp = temps.AcquireSameSizeAs(out);
3461 __ Sdiv(temp, dividend, divisor);
3462 __ Msub(out, temp, divisor, dividend);
3463 }
3464 }
3465}
3466
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003467void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3468 LocationSummary* locations =
3469 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3470 switch (div->GetResultType()) {
3471 case Primitive::kPrimInt:
3472 case Primitive::kPrimLong:
3473 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003474 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003475 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3476 break;
3477
3478 case Primitive::kPrimFloat:
3479 case Primitive::kPrimDouble:
3480 locations->SetInAt(0, Location::RequiresFpuRegister());
3481 locations->SetInAt(1, Location::RequiresFpuRegister());
3482 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3483 break;
3484
3485 default:
3486 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3487 }
3488}
3489
3490void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3491 Primitive::Type type = div->GetResultType();
3492 switch (type) {
3493 case Primitive::kPrimInt:
3494 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003495 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003496 break;
3497
3498 case Primitive::kPrimFloat:
3499 case Primitive::kPrimDouble:
3500 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3501 break;
3502
3503 default:
3504 LOG(FATAL) << "Unexpected div type " << type;
3505 }
3506}
3507
Alexandre Rames67555f72014-11-18 10:55:16 +00003508void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003509 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003510 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003511}
3512
3513void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3514 SlowPathCodeARM64* slow_path =
3515 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3516 codegen_->AddSlowPath(slow_path);
3517 Location value = instruction->GetLocations()->InAt(0);
3518
Alexandre Rames3e69f162014-12-10 10:36:50 +00003519 Primitive::Type type = instruction->GetType();
3520
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003521 if (!Primitive::IsIntegralType(type)) {
3522 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003523 return;
3524 }
3525
Alexandre Rames67555f72014-11-18 10:55:16 +00003526 if (value.IsConstant()) {
3527 int64_t divisor = Int64ConstantFrom(value);
3528 if (divisor == 0) {
3529 __ B(slow_path->GetEntryLabel());
3530 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003531 // A division by a non-null constant is valid. We don't need to perform
3532 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003533 }
3534 } else {
3535 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3536 }
3537}
3538
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003539void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3540 LocationSummary* locations =
3541 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3542 locations->SetOut(Location::ConstantLocation(constant));
3543}
3544
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003545void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3546 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003547 // Will be generated at use site.
3548}
3549
Alexandre Rames5319def2014-10-23 10:03:10 +01003550void LocationsBuilderARM64::VisitExit(HExit* exit) {
3551 exit->SetLocations(nullptr);
3552}
3553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003554void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003555}
3556
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003557void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3558 LocationSummary* locations =
3559 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3560 locations->SetOut(Location::ConstantLocation(constant));
3561}
3562
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003563void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003564 // Will be generated at use site.
3565}
3566
David Brazdilfc6a86a2015-06-26 10:33:45 +00003567void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003568 DCHECK(!successor->IsExitBlock());
3569 HBasicBlock* block = got->GetBlock();
3570 HInstruction* previous = got->GetPrevious();
3571 HLoopInformation* info = block->GetLoopInformation();
3572
David Brazdil46e2a392015-03-16 17:31:52 +00003573 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003574 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3575 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3576 return;
3577 }
3578 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3579 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3580 }
3581 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003582 __ B(codegen_->GetLabelOf(successor));
3583 }
3584}
3585
David Brazdilfc6a86a2015-06-26 10:33:45 +00003586void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3587 got->SetLocations(nullptr);
3588}
3589
3590void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3591 HandleGoto(got, got->GetSuccessor());
3592}
3593
3594void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3595 try_boundary->SetLocations(nullptr);
3596}
3597
3598void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3599 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3600 if (!successor->IsExitBlock()) {
3601 HandleGoto(try_boundary, successor);
3602 }
3603}
3604
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003605void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003606 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003607 vixl::aarch64::Label* true_target,
3608 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003609 // FP branching requires both targets to be explicit. If either of the targets
3610 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003611 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003612 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003613
David Brazdil0debae72015-11-12 18:37:00 +00003614 if (true_target == nullptr && false_target == nullptr) {
3615 // Nothing to do. The code always falls through.
3616 return;
3617 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003618 // Constant condition, statically compared against "true" (integer value 1).
3619 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003620 if (true_target != nullptr) {
3621 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003622 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003623 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003624 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003625 if (false_target != nullptr) {
3626 __ B(false_target);
3627 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003628 }
David Brazdil0debae72015-11-12 18:37:00 +00003629 return;
3630 }
3631
3632 // The following code generates these patterns:
3633 // (1) true_target == nullptr && false_target != nullptr
3634 // - opposite condition true => branch to false_target
3635 // (2) true_target != nullptr && false_target == nullptr
3636 // - condition true => branch to true_target
3637 // (3) true_target != nullptr && false_target != nullptr
3638 // - condition true => branch to true_target
3639 // - branch to false_target
3640 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003641 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003642 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003643 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003644 if (true_target == nullptr) {
3645 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3646 } else {
3647 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3648 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003649 } else {
3650 // The condition instruction has not been materialized, use its inputs as
3651 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003652 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003653
David Brazdil0debae72015-11-12 18:37:00 +00003654 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003655 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003656 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003657 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003658 IfCondition opposite_condition = condition->GetOppositeCondition();
3659 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003660 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003661 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003663 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003664 // Integer cases.
3665 Register lhs = InputRegisterAt(condition, 0);
3666 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003667
3668 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003669 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003670 if (true_target == nullptr) {
3671 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3672 non_fallthrough_target = false_target;
3673 } else {
3674 arm64_cond = ARM64Condition(condition->GetCondition());
3675 non_fallthrough_target = true_target;
3676 }
3677
Aart Bik086d27e2016-01-20 17:02:00 -08003678 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003679 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003680 switch (arm64_cond) {
3681 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003682 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003683 break;
3684 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003685 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003686 break;
3687 case lt:
3688 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003689 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003690 break;
3691 case ge:
3692 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003693 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003694 break;
3695 default:
3696 // Without the `static_cast` the compiler throws an error for
3697 // `-Werror=sign-promo`.
3698 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3699 }
3700 } else {
3701 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003702 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003703 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003704 }
3705 }
David Brazdil0debae72015-11-12 18:37:00 +00003706
3707 // If neither branch falls through (case 3), the conditional branch to `true_target`
3708 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3709 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003710 __ B(false_target);
3711 }
David Brazdil0debae72015-11-12 18:37:00 +00003712
3713 if (fallthrough_target.IsLinked()) {
3714 __ Bind(&fallthrough_target);
3715 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003716}
3717
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003718void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003720 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003721 locations->SetInAt(0, Location::RequiresRegister());
3722 }
3723}
3724
3725void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003726 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3727 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003728 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3729 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3730 true_target = nullptr;
3731 }
3732 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3733 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3734 false_target = nullptr;
3735 }
David Brazdil0debae72015-11-12 18:37:00 +00003736 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003737}
3738
3739void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3740 LocationSummary* locations = new (GetGraph()->GetArena())
3741 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003742 InvokeRuntimeCallingConvention calling_convention;
3743 RegisterSet caller_saves = RegisterSet::Empty();
3744 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3745 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003746 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003747 locations->SetInAt(0, Location::RequiresRegister());
3748 }
3749}
3750
3751void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003752 SlowPathCodeARM64* slow_path =
3753 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003754 GenerateTestAndBranch(deoptimize,
3755 /* condition_input_index */ 0,
3756 slow_path->GetEntryLabel(),
3757 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003758}
3759
Mingyao Yang063fc772016-08-02 11:02:54 -07003760void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3761 LocationSummary* locations = new (GetGraph()->GetArena())
3762 LocationSummary(flag, LocationSummary::kNoCall);
3763 locations->SetOut(Location::RequiresRegister());
3764}
3765
3766void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3767 __ Ldr(OutputRegister(flag),
3768 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3769}
3770
David Brazdilc0b601b2016-02-08 14:20:45 +00003771static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3772 return condition->IsCondition() &&
3773 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3774}
3775
Alexandre Rames880f1192016-06-13 16:04:50 +01003776static inline Condition GetConditionForSelect(HCondition* condition) {
3777 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003778 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3779 : ARM64Condition(cond);
3780}
3781
David Brazdil74eb1b22015-12-14 11:44:01 +00003782void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003784 if (Primitive::IsFloatingPointType(select->GetType())) {
3785 locations->SetInAt(0, Location::RequiresFpuRegister());
3786 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003787 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003788 } else {
3789 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3790 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3791 bool is_true_value_constant = cst_true_value != nullptr;
3792 bool is_false_value_constant = cst_false_value != nullptr;
3793 // Ask VIXL whether we should synthesize constants in registers.
3794 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3795 Operand true_op = is_true_value_constant ?
3796 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3797 Operand false_op = is_false_value_constant ?
3798 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3799 bool true_value_in_register = false;
3800 bool false_value_in_register = false;
3801 MacroAssembler::GetCselSynthesisInformation(
3802 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3803 true_value_in_register |= !is_true_value_constant;
3804 false_value_in_register |= !is_false_value_constant;
3805
3806 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3807 : Location::ConstantLocation(cst_true_value));
3808 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3809 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003810 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003811 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003812
David Brazdil74eb1b22015-12-14 11:44:01 +00003813 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3814 locations->SetInAt(2, Location::RequiresRegister());
3815 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003816}
3817
3818void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003819 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003820 Condition csel_cond;
3821
3822 if (IsBooleanValueOrMaterializedCondition(cond)) {
3823 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003824 // Use the condition flags set by the previous instruction.
3825 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003826 } else {
3827 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003828 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003829 }
3830 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003831 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003832 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003833 } else {
3834 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003835 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003836 }
3837
Alexandre Rames880f1192016-06-13 16:04:50 +01003838 if (Primitive::IsFloatingPointType(select->GetType())) {
3839 __ Fcsel(OutputFPRegister(select),
3840 InputFPRegisterAt(select, 1),
3841 InputFPRegisterAt(select, 0),
3842 csel_cond);
3843 } else {
3844 __ Csel(OutputRegister(select),
3845 InputOperandAt(select, 1),
3846 InputOperandAt(select, 0),
3847 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003848 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003849}
3850
David Srbecky0cf44932015-12-09 14:09:59 +00003851void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3852 new (GetGraph()->GetArena()) LocationSummary(info);
3853}
3854
David Srbeckyd28f4a02016-03-14 17:14:24 +00003855void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3856 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003857}
3858
3859void CodeGeneratorARM64::GenerateNop() {
3860 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003861}
3862
Alexandre Rames5319def2014-10-23 10:03:10 +01003863void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003864 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003865}
3866
3867void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003868 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003869}
3870
3871void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003872 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003873}
3874
3875void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003876 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003877}
3878
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003879// Temp is used for read barrier.
3880static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3881 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003882 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003883 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3884 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3885 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3886 return 1;
3887 }
3888 return 0;
3889}
3890
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003891// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003892// interface pointer, one for loading the current interface.
3893// The other checks have one temp for loading the object's class.
3894static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3895 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3896 return 3;
3897 }
3898 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003899}
3900
Alexandre Rames67555f72014-11-18 10:55:16 +00003901void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003902 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003903 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003904 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003905 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003906 case TypeCheckKind::kExactCheck:
3907 case TypeCheckKind::kAbstractClassCheck:
3908 case TypeCheckKind::kClassHierarchyCheck:
3909 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003910 call_kind =
3911 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003912 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003913 break;
3914 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003915 case TypeCheckKind::kUnresolvedCheck:
3916 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003917 call_kind = LocationSummary::kCallOnSlowPath;
3918 break;
3919 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003920
Alexandre Rames67555f72014-11-18 10:55:16 +00003921 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003922 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003923 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003924 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003925 locations->SetInAt(0, Location::RequiresRegister());
3926 locations->SetInAt(1, Location::RequiresRegister());
3927 // The "out" register is used as a temporary, so it overlaps with the inputs.
3928 // Note that TypeCheckSlowPathARM64 uses this register too.
3929 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003930 // Add temps if necessary for read barriers.
3931 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003932}
3933
3934void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003935 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003936 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 Register obj = InputRegisterAt(instruction, 0);
3939 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003940 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003941 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003942 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3943 DCHECK_LE(num_temps, 1u);
3944 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003945 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3946 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3947 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3948 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003949
Scott Wakeling97c72b72016-06-24 16:19:36 +01003950 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003951 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003952
3953 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003954 // Avoid null check if we know `obj` is not null.
3955 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003956 __ Cbz(obj, &zero);
3957 }
3958
Roland Levillain44015862016-01-22 11:47:17 +00003959 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003960 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003961 // /* HeapReference<Class> */ out = obj->klass_
3962 GenerateReferenceLoadTwoRegisters(instruction,
3963 out_loc,
3964 obj_loc,
3965 class_offset,
3966 maybe_temp_loc,
3967 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003968 __ Cmp(out, cls);
3969 __ Cset(out, eq);
3970 if (zero.IsLinked()) {
3971 __ B(&done);
3972 }
3973 break;
3974 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003975
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003976 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003977 // /* HeapReference<Class> */ out = obj->klass_
3978 GenerateReferenceLoadTwoRegisters(instruction,
3979 out_loc,
3980 obj_loc,
3981 class_offset,
3982 maybe_temp_loc,
3983 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003984 // If the class is abstract, we eagerly fetch the super class of the
3985 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003986 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003987 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003988 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003989 GenerateReferenceLoadOneRegister(instruction,
3990 out_loc,
3991 super_offset,
3992 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003993 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003994 // If `out` is null, we use it for the result, and jump to `done`.
3995 __ Cbz(out, &done);
3996 __ Cmp(out, cls);
3997 __ B(ne, &loop);
3998 __ Mov(out, 1);
3999 if (zero.IsLinked()) {
4000 __ B(&done);
4001 }
4002 break;
4003 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004004
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004005 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004006 // /* HeapReference<Class> */ out = obj->klass_
4007 GenerateReferenceLoadTwoRegisters(instruction,
4008 out_loc,
4009 obj_loc,
4010 class_offset,
4011 maybe_temp_loc,
4012 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004013 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004014 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004015 __ Bind(&loop);
4016 __ Cmp(out, cls);
4017 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004018 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004019 GenerateReferenceLoadOneRegister(instruction,
4020 out_loc,
4021 super_offset,
4022 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004023 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004024 __ Cbnz(out, &loop);
4025 // If `out` is null, we use it for the result, and jump to `done`.
4026 __ B(&done);
4027 __ Bind(&success);
4028 __ Mov(out, 1);
4029 if (zero.IsLinked()) {
4030 __ B(&done);
4031 }
4032 break;
4033 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004034
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004035 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004036 // /* HeapReference<Class> */ out = obj->klass_
4037 GenerateReferenceLoadTwoRegisters(instruction,
4038 out_loc,
4039 obj_loc,
4040 class_offset,
4041 maybe_temp_loc,
4042 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004043 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004044 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004045 __ Cmp(out, cls);
4046 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004047 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004048 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004049 GenerateReferenceLoadOneRegister(instruction,
4050 out_loc,
4051 component_offset,
4052 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004053 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004054 // If `out` is null, we use it for the result, and jump to `done`.
4055 __ Cbz(out, &done);
4056 __ Ldrh(out, HeapOperand(out, primitive_offset));
4057 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4058 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004059 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004060 __ Mov(out, 1);
4061 __ B(&done);
4062 break;
4063 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004064
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004065 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004066 // No read barrier since the slow path will retry upon failure.
4067 // /* HeapReference<Class> */ out = obj->klass_
4068 GenerateReferenceLoadTwoRegisters(instruction,
4069 out_loc,
4070 obj_loc,
4071 class_offset,
4072 maybe_temp_loc,
4073 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004074 __ Cmp(out, cls);
4075 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004076 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4077 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004078 codegen_->AddSlowPath(slow_path);
4079 __ B(ne, slow_path->GetEntryLabel());
4080 __ Mov(out, 1);
4081 if (zero.IsLinked()) {
4082 __ B(&done);
4083 }
4084 break;
4085 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086
Calin Juravle98893e12015-10-02 21:05:03 +01004087 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004088 case TypeCheckKind::kInterfaceCheck: {
4089 // Note that we indeed only call on slow path, but we always go
4090 // into the slow path for the unresolved and interface check
4091 // cases.
4092 //
4093 // We cannot directly call the InstanceofNonTrivial runtime
4094 // entry point without resorting to a type checking slow path
4095 // here (i.e. by calling InvokeRuntime directly), as it would
4096 // require to assign fixed registers for the inputs of this
4097 // HInstanceOf instruction (following the runtime calling
4098 // convention), which might be cluttered by the potential first
4099 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004100 //
4101 // TODO: Introduce a new runtime entry point taking the object
4102 // to test (instead of its class) as argument, and let it deal
4103 // with the read barrier issues. This will let us refactor this
4104 // case of the `switch` code as it was previously (with a direct
4105 // call to the runtime not using a type checking slow path).
4106 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004107 DCHECK(locations->OnlyCallsOnSlowPath());
4108 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4109 /* is_fatal */ false);
4110 codegen_->AddSlowPath(slow_path);
4111 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004112 if (zero.IsLinked()) {
4113 __ B(&done);
4114 }
4115 break;
4116 }
4117 }
4118
4119 if (zero.IsLinked()) {
4120 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004121 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004122 }
4123
4124 if (done.IsLinked()) {
4125 __ Bind(&done);
4126 }
4127
4128 if (slow_path != nullptr) {
4129 __ Bind(slow_path->GetExitLabel());
4130 }
4131}
4132
4133void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4134 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4135 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4136
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004137 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4138 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004139 case TypeCheckKind::kExactCheck:
4140 case TypeCheckKind::kAbstractClassCheck:
4141 case TypeCheckKind::kClassHierarchyCheck:
4142 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004143 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4144 LocationSummary::kCallOnSlowPath :
4145 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004146 break;
4147 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004148 case TypeCheckKind::kUnresolvedCheck:
4149 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 call_kind = LocationSummary::kCallOnSlowPath;
4151 break;
4152 }
4153
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004154 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4155 locations->SetInAt(0, Location::RequiresRegister());
4156 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004157 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4158 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004159}
4160
4161void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004162 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004163 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004164 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004165 Register obj = InputRegisterAt(instruction, 0);
4166 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004167 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4168 DCHECK_GE(num_temps, 1u);
4169 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004170 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004171 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4172 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004173 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4175 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4176 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4177 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4178 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4179 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4180 const uint32_t object_array_data_offset =
4181 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004183 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004184 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4185 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4186 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004187 if (!kEmitCompilerReadBarrier) {
4188 is_type_check_slow_path_fatal =
4189 (type_check_kind == TypeCheckKind::kExactCheck ||
4190 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4191 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4192 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4193 !instruction->CanThrowIntoCatchBlock();
4194 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004195 SlowPathCodeARM64* type_check_slow_path =
4196 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4197 is_type_check_slow_path_fatal);
4198 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004199
Scott Wakeling97c72b72016-06-24 16:19:36 +01004200 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004201 // Avoid null check if we know obj is not null.
4202 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004203 __ Cbz(obj, &done);
4204 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004205
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004206 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004207 case TypeCheckKind::kExactCheck:
4208 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004209 // /* HeapReference<Class> */ temp = obj->klass_
4210 GenerateReferenceLoadTwoRegisters(instruction,
4211 temp_loc,
4212 obj_loc,
4213 class_offset,
4214 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004215 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004217 __ Cmp(temp, cls);
4218 // Jump to slow path for throwing the exception or doing a
4219 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004220 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 break;
4222 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004225 // /* HeapReference<Class> */ temp = obj->klass_
4226 GenerateReferenceLoadTwoRegisters(instruction,
4227 temp_loc,
4228 obj_loc,
4229 class_offset,
4230 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004231 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004233 // If the class is abstract, we eagerly fetch the super class of the
4234 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004235 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004236 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004237 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004238 GenerateReferenceLoadOneRegister(instruction,
4239 temp_loc,
4240 super_offset,
4241 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004242 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004243
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004244 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4245 // exception.
4246 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4247 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004248 __ Cmp(temp, cls);
4249 __ B(ne, &loop);
4250 break;
4251 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004253 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004254 // /* HeapReference<Class> */ temp = obj->klass_
4255 GenerateReferenceLoadTwoRegisters(instruction,
4256 temp_loc,
4257 obj_loc,
4258 class_offset,
4259 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004260 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004261
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004262 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004263 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004264 __ Bind(&loop);
4265 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004266 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004267
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004268 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004269 GenerateReferenceLoadOneRegister(instruction,
4270 temp_loc,
4271 super_offset,
4272 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004273 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004274
4275 // If the class reference currently in `temp` is not null, jump
4276 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004277 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004278 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004279 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004280 break;
4281 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004282
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004283 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004284 // /* HeapReference<Class> */ temp = obj->klass_
4285 GenerateReferenceLoadTwoRegisters(instruction,
4286 temp_loc,
4287 obj_loc,
4288 class_offset,
4289 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004290 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004291
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004292 // Do an exact check.
4293 __ Cmp(temp, cls);
4294 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004295
4296 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004297 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004298 GenerateReferenceLoadOneRegister(instruction,
4299 temp_loc,
4300 component_offset,
4301 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004302 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004303
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004304 // If the component type is null, jump to the slow path to throw the exception.
4305 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4306 // Otherwise, the object is indeed an array. Further check that this component type is not a
4307 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004308 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4309 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004310 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004311 break;
4312 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004313
Calin Juravle98893e12015-10-02 21:05:03 +01004314 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004315 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004316 //
4317 // We cannot directly call the CheckCast runtime entry point
4318 // without resorting to a type checking slow path here (i.e. by
4319 // calling InvokeRuntime directly), as it would require to
4320 // assign fixed registers for the inputs of this HInstanceOf
4321 // instruction (following the runtime calling convention), which
4322 // might be cluttered by the potential first read barrier
4323 // emission at the beginning of this method.
4324 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004325 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004326 case TypeCheckKind::kInterfaceCheck: {
4327 // /* HeapReference<Class> */ temp = obj->klass_
4328 GenerateReferenceLoadTwoRegisters(instruction,
4329 temp_loc,
4330 obj_loc,
4331 class_offset,
4332 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004333 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004334
4335 // /* HeapReference<Class> */ temp = temp->iftable_
4336 GenerateReferenceLoadTwoRegisters(instruction,
4337 temp_loc,
4338 temp_loc,
4339 iftable_offset,
4340 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004341 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004342 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004343 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004344 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004345 vixl::aarch64::Label start_loop;
4346 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004347 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004348 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4349 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004350 // Go to next interface.
4351 __ Add(temp, temp, 2 * kHeapReferenceSize);
4352 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004353 // Compare the classes and continue the loop if they do not match.
4354 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4355 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004356 break;
4357 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004358 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004359 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004360
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004361 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004362}
4363
Alexandre Rames5319def2014-10-23 10:03:10 +01004364void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4366 locations->SetOut(Location::ConstantLocation(constant));
4367}
4368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004369void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004370 // Will be generated at use site.
4371}
4372
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004373void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4374 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4375 locations->SetOut(Location::ConstantLocation(constant));
4376}
4377
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004378void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004379 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004380}
4381
Calin Juravle175dc732015-08-25 15:42:32 +01004382void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4383 // The trampoline uses the same calling convention as dex calling conventions,
4384 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4385 // the method_idx.
4386 HandleInvoke(invoke);
4387}
4388
4389void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4390 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4391}
4392
Alexandre Rames5319def2014-10-23 10:03:10 +01004393void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004394 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004395 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004396}
4397
Alexandre Rames67555f72014-11-18 10:55:16 +00004398void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4399 HandleInvoke(invoke);
4400}
4401
4402void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4403 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004404 LocationSummary* locations = invoke->GetLocations();
4405 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004406 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004407 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004408 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004409
4410 // The register ip1 is required to be used for the hidden argument in
4411 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004412 MacroAssembler* masm = GetVIXLAssembler();
4413 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004414 scratch_scope.Exclude(ip1);
4415 __ Mov(ip1, invoke->GetDexMethodIndex());
4416
Artem Serov914d7a82017-02-07 14:33:49 +00004417 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004418 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004419 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004420 {
4421 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4422 // /* HeapReference<Class> */ temp = temp->klass_
4423 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4424 codegen_->MaybeRecordImplicitNullCheck(invoke);
4425 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004426 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004427 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004428 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004429 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004430 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004431 }
Artem Serov914d7a82017-02-07 14:33:49 +00004432
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004433 // Instead of simply (possibly) unpoisoning `temp` here, we should
4434 // emit a read barrier for the previous class reference load.
4435 // However this is not required in practice, as this is an
4436 // intermediate/temporary reference and because the current
4437 // concurrent copying collector keeps the from-space memory
4438 // intact/accessible until the end of the marking phase (the
4439 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004440 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004441 __ Ldr(temp,
4442 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4443 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004444 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004445 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004446 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004447 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004448 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004449
4450 {
4451 // Ensure the pc position is recorded immediately after the `blr` instruction.
4452 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4453
4454 // lr();
4455 __ blr(lr);
4456 DCHECK(!codegen_->IsLeafMethod());
4457 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4458 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004459}
4460
4461void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004462 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004463 if (intrinsic.TryDispatch(invoke)) {
4464 return;
4465 }
4466
Alexandre Rames67555f72014-11-18 10:55:16 +00004467 HandleInvoke(invoke);
4468}
4469
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004470void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004471 // Explicit clinit checks triggered by static invokes must have been pruned by
4472 // art::PrepareForRegisterAllocation.
4473 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004474
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004475 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004476 if (intrinsic.TryDispatch(invoke)) {
4477 return;
4478 }
4479
Alexandre Rames67555f72014-11-18 10:55:16 +00004480 HandleInvoke(invoke);
4481}
4482
Andreas Gampe878d58c2015-01-15 23:24:00 -08004483static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4484 if (invoke->GetLocations()->Intrinsified()) {
4485 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4486 intrinsic.Dispatch(invoke);
4487 return true;
4488 }
4489 return false;
4490}
4491
Vladimir Markodc151b22015-10-15 18:02:30 +01004492HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4493 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004494 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004495 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004496 return desired_dispatch_info;
4497}
4498
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004499void CodeGeneratorARM64::GenerateStaticOrDirectCall(
4500 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004501 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004502 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4503 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004504 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4505 uint32_t offset =
4506 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004507 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004508 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004509 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004510 }
Vladimir Marko58155012015-08-19 12:49:41 +00004511 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004512 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004513 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004514 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
4515 DCHECK(GetCompilerOptions().IsBootImage());
4516 // Add ADRP with its PC-relative method patch.
4517 vixl::aarch64::Label* adrp_label = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
4518 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
4519 // Add ADD with its PC-relative method patch.
4520 vixl::aarch64::Label* add_label =
4521 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), adrp_label);
4522 EmitAddPlaceholder(add_label, XRegisterFrom(temp), XRegisterFrom(temp));
4523 break;
4524 }
Vladimir Marko58155012015-08-19 12:49:41 +00004525 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4526 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004527 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004528 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004529 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4530 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004531 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004532 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004533 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004534 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004535 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004536 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004537 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004538 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004539 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004540 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004541 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
4542 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
4543 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko58155012015-08-19 12:49:41 +00004544 }
4545 }
4546
4547 switch (invoke->GetCodePtrLocation()) {
4548 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004549 {
4550 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
4551 ExactAssemblyScope eas(GetVIXLAssembler(),
4552 kInstructionSize,
4553 CodeBufferCheckScope::kExactSize);
4554 __ bl(&frame_entry_label_);
4555 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
4556 }
Vladimir Marko58155012015-08-19 12:49:41 +00004557 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004558 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4559 // LR = callee_method->entry_point_from_quick_compiled_code_;
4560 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004561 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004562 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004563 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004564 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004565 ExactAssemblyScope eas(GetVIXLAssembler(),
4566 kInstructionSize,
4567 CodeBufferCheckScope::kExactSize);
4568 // lr()
4569 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004570 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004571 }
Vladimir Marko58155012015-08-19 12:49:41 +00004572 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004573 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004574
Andreas Gampe878d58c2015-01-15 23:24:00 -08004575 DCHECK(!IsLeafMethod());
4576}
4577
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004578void CodeGeneratorARM64::GenerateVirtualCall(
4579 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004580 // Use the calling convention instead of the location of the receiver, as
4581 // intrinsics may have put the receiver in a different register. In the intrinsics
4582 // slow path, the arguments have been moved to the right place, so here we are
4583 // guaranteed that the receiver is the first register of the calling convention.
4584 InvokeDexCallingConvention calling_convention;
4585 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004586 Register temp = XRegisterFrom(temp_in);
4587 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4588 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4589 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004590 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004591
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004592 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004593
4594 {
4595 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4596 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4597 // /* HeapReference<Class> */ temp = receiver->klass_
4598 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4599 MaybeRecordImplicitNullCheck(invoke);
4600 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004601 // Instead of simply (possibly) unpoisoning `temp` here, we should
4602 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004603 // intermediate/temporary reference and because the current
4604 // concurrent copying collector keeps the from-space memory
4605 // intact/accessible until the end of the marking phase (the
4606 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004607 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4608 // temp = temp->GetMethodAt(method_offset);
4609 __ Ldr(temp, MemOperand(temp, method_offset));
4610 // lr = temp->GetEntryPoint();
4611 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004612 {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004613 // Use a scope to help guarantee that `RecordPcInfo()` records the correct pc.
Artem Serov914d7a82017-02-07 14:33:49 +00004614 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4615 // lr();
4616 __ blr(lr);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004617 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00004618 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004619}
4620
Orion Hodsonac141392017-01-13 11:53:47 +00004621void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4622 HandleInvoke(invoke);
4623}
4624
4625void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4626 codegen_->GenerateInvokePolymorphicCall(invoke);
4627}
4628
Vladimir Marko65979462017-05-19 17:25:12 +01004629vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeMethodPatch(
4630 MethodReference target_method,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004631 vixl::aarch64::Label* adrp_label) {
Vladimir Marko65979462017-05-19 17:25:12 +01004632 return NewPcRelativePatch(*target_method.dex_file,
4633 target_method.dex_method_index,
4634 adrp_label,
4635 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004636}
4637
Scott Wakeling97c72b72016-06-24 16:19:36 +01004638vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4639 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004640 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004641 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004642 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004643}
4644
Vladimir Marko1998cd02017-01-13 13:02:58 +00004645vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4646 const DexFile& dex_file,
4647 dex::TypeIndex type_index,
4648 vixl::aarch64::Label* adrp_label) {
4649 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4650}
4651
Vladimir Marko65979462017-05-19 17:25:12 +01004652vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4653 const DexFile& dex_file,
4654 dex::StringIndex string_index,
4655 vixl::aarch64::Label* adrp_label) {
4656 return
4657 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
4658}
4659
Scott Wakeling97c72b72016-06-24 16:19:36 +01004660vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4661 const DexFile& dex_file,
4662 uint32_t element_offset,
4663 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004664 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4665}
4666
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004667vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4668 baker_read_barrier_patches_.emplace_back(custom_data);
4669 return &baker_read_barrier_patches_.back().label;
4670}
4671
Scott Wakeling97c72b72016-06-24 16:19:36 +01004672vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4673 const DexFile& dex_file,
4674 uint32_t offset_or_index,
4675 vixl::aarch64::Label* adrp_label,
4676 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004677 // Add a patch entry and return the label.
4678 patches->emplace_back(dex_file, offset_or_index);
4679 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004680 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004681 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4682 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4683 return label;
4684}
4685
Scott Wakeling97c72b72016-06-24 16:19:36 +01004686vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4687 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004688 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004689}
4690
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004691vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004692 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4693 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4694 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004695 return jit_string_patches_.GetOrCreate(
4696 StringReference(&dex_file, string_index),
4697 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4698}
4699
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004700vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004701 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4702 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4703 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004704 return jit_class_patches_.GetOrCreate(
4705 TypeReference(&dex_file, type_index),
4706 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4707}
4708
Vladimir Markoaad75c62016-10-03 08:46:48 +00004709void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4710 vixl::aarch64::Register reg) {
4711 DCHECK(reg.IsX());
4712 SingleEmissionCheckScope guard(GetVIXLAssembler());
4713 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004714 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004715}
4716
4717void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4718 vixl::aarch64::Register out,
4719 vixl::aarch64::Register base) {
4720 DCHECK(out.IsX());
4721 DCHECK(base.IsX());
4722 SingleEmissionCheckScope guard(GetVIXLAssembler());
4723 __ Bind(fixup_label);
4724 __ add(out, base, Operand(/* offset placeholder */ 0));
4725}
4726
4727void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4728 vixl::aarch64::Register out,
4729 vixl::aarch64::Register base) {
4730 DCHECK(base.IsX());
4731 SingleEmissionCheckScope guard(GetVIXLAssembler());
4732 __ Bind(fixup_label);
4733 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4734}
4735
4736template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4737inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4738 const ArenaDeque<PcRelativePatchInfo>& infos,
4739 ArenaVector<LinkerPatch>* linker_patches) {
4740 for (const PcRelativePatchInfo& info : infos) {
4741 linker_patches->push_back(Factory(info.label.GetLocation(),
4742 &info.target_dex_file,
4743 info.pc_insn_label->GetLocation(),
4744 info.offset_or_index));
4745 }
4746}
4747
Vladimir Marko58155012015-08-19 12:49:41 +00004748void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4749 DCHECK(linker_patches->empty());
4750 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004751 pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004752 pc_relative_method_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004753 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004754 type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01004755 pc_relative_string_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004756 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004757 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004758 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004759 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004760 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004761 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004762 info.offset_or_index));
4763 }
Vladimir Marko65979462017-05-19 17:25:12 +01004764 if (GetCompilerOptions().IsBootImage()) {
4765 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00004766 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004767 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4768 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004769 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4770 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01004771 } else {
4772 DCHECK(pc_relative_method_patches_.empty());
4773 DCHECK(pc_relative_type_patches_.empty());
4774 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4775 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004776 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004777 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4778 linker_patches);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004779 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4780 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4781 info.custom_data));
4782 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004783 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004784}
4785
Scott Wakeling97c72b72016-06-24 16:19:36 +01004786vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004787 Uint32ToLiteralMap* map) {
4788 return map->GetOrCreate(
4789 value,
4790 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4791}
4792
Scott Wakeling97c72b72016-06-24 16:19:36 +01004793vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004794 return uint64_literals_.GetOrCreate(
4795 value,
4796 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004797}
4798
Andreas Gampe878d58c2015-01-15 23:24:00 -08004799void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004800 // Explicit clinit checks triggered by static invokes must have been pruned by
4801 // art::PrepareForRegisterAllocation.
4802 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004803
Andreas Gampe878d58c2015-01-15 23:24:00 -08004804 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4805 return;
4806 }
4807
Artem Serov914d7a82017-02-07 14:33:49 +00004808 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4809 // are no pools emitted.
4810 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004811 LocationSummary* locations = invoke->GetLocations();
4812 codegen_->GenerateStaticOrDirectCall(
4813 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Alexandre Rames5319def2014-10-23 10:03:10 +01004814}
4815
4816void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004817 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4818 return;
4819 }
4820
Artem Serov914d7a82017-02-07 14:33:49 +00004821 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4822 // are no pools emitted.
4823 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004824 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004825 DCHECK(!codegen_->IsLeafMethod());
Alexandre Rames5319def2014-10-23 10:03:10 +01004826}
4827
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004828HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4829 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004830 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004831 case HLoadClass::LoadKind::kInvalid:
4832 LOG(FATAL) << "UNREACHABLE";
4833 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004834 case HLoadClass::LoadKind::kReferrersClass:
4835 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004836 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004837 case HLoadClass::LoadKind::kBssEntry:
4838 DCHECK(!Runtime::Current()->UseJitCompilation());
4839 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004840 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004841 DCHECK(Runtime::Current()->UseJitCompilation());
4842 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01004843 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844 case HLoadClass::LoadKind::kDexCacheViaMethod:
4845 break;
4846 }
4847 return desired_class_load_kind;
4848}
4849
Alexandre Rames67555f72014-11-18 10:55:16 +00004850void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004851 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4852 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004853 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004854 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 cls,
4856 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004857 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004858 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004859 return;
4860 }
Vladimir Marko41559982017-01-06 14:04:23 +00004861 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004862
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004863 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4864 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004865 ? LocationSummary::kCallOnSlowPath
4866 : LocationSummary::kNoCall;
4867 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004868 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004869 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004870 }
4871
Vladimir Marko41559982017-01-06 14:04:23 +00004872 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004873 locations->SetInAt(0, Location::RequiresRegister());
4874 }
4875 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004876 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4877 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4878 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004879 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004880 RegisterSet caller_saves = RegisterSet::Empty();
4881 InvokeRuntimeCallingConvention calling_convention;
4882 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4883 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4884 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4885 Primitive::kPrimNot).GetCode());
4886 locations->SetCustomSlowPathCallerSaves(caller_saves);
4887 } else {
4888 // For non-Baker read barrier we have a temp-clobbering call.
4889 }
4890 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004891}
4892
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004893// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4894// move.
4895void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004896 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4897 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4898 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004899 return;
4900 }
Vladimir Marko41559982017-01-06 14:04:23 +00004901 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004902
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004903 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004904 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004905 Register bss_entry_temp;
4906 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004907
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004908 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4909 ? kWithoutReadBarrier
4910 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004911 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004912 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004913 case HLoadClass::LoadKind::kReferrersClass: {
4914 DCHECK(!cls->CanCallRuntime());
4915 DCHECK(!cls->MustGenerateClinitCheck());
4916 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4917 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004918 GenerateGcRootFieldLoad(cls,
4919 out_loc,
4920 current_method,
4921 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004922 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004923 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004924 break;
4925 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004926 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004927 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 // Add ADRP with its PC-relative type patch.
4929 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004930 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004931 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004932 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004933 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004934 vixl::aarch64::Label* add_label =
4935 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004936 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004937 break;
4938 }
4939 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004940 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004941 uint32_t address = dchecked_integral_cast<uint32_t>(
4942 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4943 DCHECK_NE(address, 0u);
4944 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004945 break;
4946 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004947 case HLoadClass::LoadKind::kBssEntry: {
4948 // Add ADRP with its PC-relative Class .bss entry patch.
4949 const DexFile& dex_file = cls->GetDexFile();
4950 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004951 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004952 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4953 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004954 // Add LDR with its PC-relative Class patch.
4955 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004956 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004957 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4958 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004959 out_loc,
4960 bss_entry_temp,
4961 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004962 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004963 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004964 generate_null_check = true;
4965 break;
4966 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004967 case HLoadClass::LoadKind::kJitTableAddress: {
4968 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4969 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004970 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004971 GenerateGcRootFieldLoad(cls,
4972 out_loc,
4973 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004974 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004975 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004976 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004977 break;
4978 }
Vladimir Marko41559982017-01-06 14:04:23 +00004979 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004980 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004981 LOG(FATAL) << "UNREACHABLE";
4982 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004983 }
4984
Vladimir Markoea4c1262017-02-06 19:59:33 +00004985 bool do_clinit = cls->MustGenerateClinitCheck();
4986 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004987 DCHECK(cls->CanCallRuntime());
4988 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004989 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004990 codegen_->AddSlowPath(slow_path);
4991 if (generate_null_check) {
4992 __ Cbz(out, slow_path->GetEntryLabel());
4993 }
4994 if (cls->MustGenerateClinitCheck()) {
4995 GenerateClassInitializationCheck(slow_path, out);
4996 } else {
4997 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004998 }
4999 }
5000}
5001
David Brazdilcb1c0552015-08-04 16:22:25 +01005002static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005003 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01005004}
5005
Alexandre Rames67555f72014-11-18 10:55:16 +00005006void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5007 LocationSummary* locations =
5008 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5009 locations->SetOut(Location::RequiresRegister());
5010}
5011
5012void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005013 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5014}
5015
5016void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5017 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5018}
5019
5020void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5021 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005022}
5023
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005024HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5025 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005026 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005027 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00005028 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005029 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005030 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005031 case HLoadString::LoadKind::kJitTableAddress:
5032 DCHECK(Runtime::Current()->UseJitCompilation());
5033 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01005034 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005035 case HLoadString::LoadKind::kDexCacheViaMethod:
5036 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005037 }
5038 return desired_string_load_kind;
5039}
5040
Alexandre Rames67555f72014-11-18 10:55:16 +00005041void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005042 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005044 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005045 InvokeRuntimeCallingConvention calling_convention;
5046 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5047 } else {
5048 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005049 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5050 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005051 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005052 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005053 RegisterSet caller_saves = RegisterSet::Empty();
5054 InvokeRuntimeCallingConvention calling_convention;
5055 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5056 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5057 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5058 Primitive::kPrimNot).GetCode());
5059 locations->SetCustomSlowPathCallerSaves(caller_saves);
5060 } else {
5061 // For non-Baker read barrier we have a temp-clobbering call.
5062 }
5063 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005064 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005065}
5066
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005067// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5068// move.
5069void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005070 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005071 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005072
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005073 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005074 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075 // Add ADRP with its PC-relative String patch.
5076 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005077 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005078 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005079 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005080 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005081 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005082 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005084 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005085 return; // No dex cache slow path.
5086 }
5087 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005088 uint32_t address = dchecked_integral_cast<uint32_t>(
5089 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5090 DCHECK_NE(address, 0u);
5091 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 return; // No dex cache slow path.
5093 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005094 case HLoadString::LoadKind::kBssEntry: {
5095 // Add ADRP with its PC-relative String .bss entry patch.
5096 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005097 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005098 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005099 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005100 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005101 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102 // Add LDR with its PC-relative String patch.
5103 vixl::aarch64::Label* ldr_label =
5104 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005105 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005106 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005107 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005108 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005109 /* offset placeholder */ 0u,
5110 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005111 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005112 SlowPathCodeARM64* slow_path =
5113 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005114 codegen_->AddSlowPath(slow_path);
5115 __ Cbz(out.X(), slow_path->GetEntryLabel());
5116 __ Bind(slow_path->GetExitLabel());
5117 return;
5118 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005119 case HLoadString::LoadKind::kJitTableAddress: {
5120 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005121 load->GetStringIndex(),
5122 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005123 GenerateGcRootFieldLoad(load,
5124 out_loc,
5125 out.X(),
5126 /* offset */ 0,
5127 /* fixup_label */ nullptr,
5128 kCompilerReadBarrierOption);
5129 return;
5130 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005131 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005132 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005133 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005134
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005135 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005136 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005137 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005138 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005139 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5140 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005141}
5142
Alexandre Rames5319def2014-10-23 10:03:10 +01005143void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5145 locations->SetOut(Location::ConstantLocation(constant));
5146}
5147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005148void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005149 // Will be generated at use site.
5150}
5151
Alexandre Rames67555f72014-11-18 10:55:16 +00005152void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5153 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005155 InvokeRuntimeCallingConvention calling_convention;
5156 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5157}
5158
5159void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005160 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005161 instruction,
5162 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005163 if (instruction->IsEnter()) {
5164 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5165 } else {
5166 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5167 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005168}
5169
Alexandre Rames42d641b2014-10-27 14:00:51 +00005170void LocationsBuilderARM64::VisitMul(HMul* mul) {
5171 LocationSummary* locations =
5172 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5173 switch (mul->GetResultType()) {
5174 case Primitive::kPrimInt:
5175 case Primitive::kPrimLong:
5176 locations->SetInAt(0, Location::RequiresRegister());
5177 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005179 break;
5180
5181 case Primitive::kPrimFloat:
5182 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005183 locations->SetInAt(0, Location::RequiresFpuRegister());
5184 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005185 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005186 break;
5187
5188 default:
5189 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5190 }
5191}
5192
5193void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5194 switch (mul->GetResultType()) {
5195 case Primitive::kPrimInt:
5196 case Primitive::kPrimLong:
5197 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5198 break;
5199
5200 case Primitive::kPrimFloat:
5201 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005202 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005203 break;
5204
5205 default:
5206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5207 }
5208}
5209
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005210void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5211 LocationSummary* locations =
5212 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5213 switch (neg->GetResultType()) {
5214 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005215 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005216 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005218 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005219
5220 case Primitive::kPrimFloat:
5221 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005222 locations->SetInAt(0, Location::RequiresFpuRegister());
5223 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005224 break;
5225
5226 default:
5227 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5228 }
5229}
5230
5231void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5232 switch (neg->GetResultType()) {
5233 case Primitive::kPrimInt:
5234 case Primitive::kPrimLong:
5235 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5236 break;
5237
5238 case Primitive::kPrimFloat:
5239 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005240 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005241 break;
5242
5243 default:
5244 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5245 }
5246}
5247
5248void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5249 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005251 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005252 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005253 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5254 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005255}
5256
5257void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005258 // Note: if heap poisoning is enabled, the entry point takes cares
5259 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005260 QuickEntrypointEnum entrypoint =
5261 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5262 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005263 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005264}
5265
Alexandre Rames5319def2014-10-23 10:03:10 +01005266void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5267 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005269 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005270 if (instruction->IsStringAlloc()) {
5271 locations->AddTemp(LocationFrom(kArtMethodRegister));
5272 } else {
5273 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005274 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005275 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5276}
5277
5278void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005279 // Note: if heap poisoning is enabled, the entry point takes cares
5280 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005281 if (instruction->IsStringAlloc()) {
5282 // String is allocated through StringFactory. Call NewEmptyString entry point.
5283 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005284 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005285 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5286 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005287
5288 {
5289 // Ensure the pc position is recorded immediately after the `blr` instruction.
5290 ExactAssemblyScope eas(GetVIXLAssembler(),
5291 kInstructionSize,
5292 CodeBufferCheckScope::kExactSize);
5293 __ blr(lr);
5294 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5295 }
David Brazdil6de19382016-01-08 17:37:10 +00005296 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005297 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005298 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005299 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005300}
5301
5302void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005304 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005306}
5307
5308void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005309 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005310 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005311 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005312 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005313 break;
5314
5315 default:
5316 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5317 }
5318}
5319
David Brazdil66d126e2015-04-03 16:02:44 +01005320void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5322 locations->SetInAt(0, Location::RequiresRegister());
5323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5324}
5325
5326void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005327 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005328}
5329
Alexandre Rames5319def2014-10-23 10:03:10 +01005330void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005331 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5332 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005333}
5334
Calin Juravle2ae48182016-03-16 14:05:09 +00005335void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5336 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 return;
5338 }
Artem Serov914d7a82017-02-07 14:33:49 +00005339 {
5340 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5341 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5342 Location obj = instruction->GetLocations()->InAt(0);
5343 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5344 RecordPcInfo(instruction, instruction->GetDexPc());
5345 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005346}
5347
Calin Juravle2ae48182016-03-16 14:05:09 +00005348void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005349 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005350 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005351
5352 LocationSummary* locations = instruction->GetLocations();
5353 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005354
5355 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005356}
5357
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005358void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005359 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005360}
5361
Alexandre Rames67555f72014-11-18 10:55:16 +00005362void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5363 HandleBinaryOp(instruction);
5364}
5365
5366void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5367 HandleBinaryOp(instruction);
5368}
5369
Alexandre Rames3e69f162014-12-10 10:36:50 +00005370void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5371 LOG(FATAL) << "Unreachable";
5372}
5373
5374void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5375 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5376}
5377
Alexandre Rames5319def2014-10-23 10:03:10 +01005378void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5380 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5381 if (location.IsStackSlot()) {
5382 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5383 } else if (location.IsDoubleStackSlot()) {
5384 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5385 }
5386 locations->SetOut(location);
5387}
5388
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005389void InstructionCodeGeneratorARM64::VisitParameterValue(
5390 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005391 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005392}
5393
5394void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5395 LocationSummary* locations =
5396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005397 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005398}
5399
5400void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5401 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5402 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005403}
5404
5405void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005407 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005408 locations->SetInAt(i, Location::Any());
5409 }
5410 locations->SetOut(Location::Any());
5411}
5412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005413void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005414 LOG(FATAL) << "Unreachable";
5415}
5416
Serban Constantinescu02164b32014-11-13 14:05:07 +00005417void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005418 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005419 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005420 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5421 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5423
5424 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005425 case Primitive::kPrimInt:
5426 case Primitive::kPrimLong:
5427 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005428 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5430 break;
5431
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005432 case Primitive::kPrimFloat:
5433 case Primitive::kPrimDouble: {
5434 InvokeRuntimeCallingConvention calling_convention;
5435 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5436 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5437 locations->SetOut(calling_convention.GetReturnLocation(type));
5438
5439 break;
5440 }
5441
Serban Constantinescu02164b32014-11-13 14:05:07 +00005442 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005443 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444 }
5445}
5446
5447void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5448 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005449
Serban Constantinescu02164b32014-11-13 14:05:07 +00005450 switch (type) {
5451 case Primitive::kPrimInt:
5452 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005453 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005454 break;
5455 }
5456
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005457 case Primitive::kPrimFloat:
5458 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005459 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5460 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005461 if (type == Primitive::kPrimFloat) {
5462 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5463 } else {
5464 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5465 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005466 break;
5467 }
5468
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 default:
5470 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005471 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005472 }
5473}
5474
Igor Murashkind01745e2017-04-05 16:40:31 -07005475void LocationsBuilderARM64::VisitConstructorFence(HConstructorFence* constructor_fence) {
5476 constructor_fence->SetLocations(nullptr);
5477}
5478
5479void InstructionCodeGeneratorARM64::VisitConstructorFence(
5480 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
5481 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
5482}
5483
Calin Juravle27df7582015-04-17 19:12:31 +01005484void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5485 memory_barrier->SetLocations(nullptr);
5486}
5487
5488void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005489 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005490}
5491
Alexandre Rames5319def2014-10-23 10:03:10 +01005492void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5493 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5494 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005495 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005496}
5497
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005498void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005499 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005500}
5501
5502void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5503 instruction->SetLocations(nullptr);
5504}
5505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005506void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005507 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005508}
5509
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005510void LocationsBuilderARM64::VisitRor(HRor* ror) {
5511 HandleBinaryOp(ror);
5512}
5513
5514void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5515 HandleBinaryOp(ror);
5516}
5517
Serban Constantinescu02164b32014-11-13 14:05:07 +00005518void LocationsBuilderARM64::VisitShl(HShl* shl) {
5519 HandleShift(shl);
5520}
5521
5522void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5523 HandleShift(shl);
5524}
5525
5526void LocationsBuilderARM64::VisitShr(HShr* shr) {
5527 HandleShift(shr);
5528}
5529
5530void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5531 HandleShift(shr);
5532}
5533
Alexandre Rames5319def2014-10-23 10:03:10 +01005534void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005535 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005536}
5537
5538void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005539 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005540}
5541
Alexandre Rames67555f72014-11-18 10:55:16 +00005542void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005543 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005544}
5545
5546void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005547 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005548}
5549
5550void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005551 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005552}
5553
Alexandre Rames67555f72014-11-18 10:55:16 +00005554void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005555 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005556}
5557
Calin Juravlee460d1d2015-09-29 04:52:17 +01005558void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5559 HUnresolvedInstanceFieldGet* instruction) {
5560 FieldAccessCallingConventionARM64 calling_convention;
5561 codegen_->CreateUnresolvedFieldLocationSummary(
5562 instruction, instruction->GetFieldType(), calling_convention);
5563}
5564
5565void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5566 HUnresolvedInstanceFieldGet* instruction) {
5567 FieldAccessCallingConventionARM64 calling_convention;
5568 codegen_->GenerateUnresolvedFieldAccess(instruction,
5569 instruction->GetFieldType(),
5570 instruction->GetFieldIndex(),
5571 instruction->GetDexPc(),
5572 calling_convention);
5573}
5574
5575void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5576 HUnresolvedInstanceFieldSet* instruction) {
5577 FieldAccessCallingConventionARM64 calling_convention;
5578 codegen_->CreateUnresolvedFieldLocationSummary(
5579 instruction, instruction->GetFieldType(), calling_convention);
5580}
5581
5582void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5583 HUnresolvedInstanceFieldSet* instruction) {
5584 FieldAccessCallingConventionARM64 calling_convention;
5585 codegen_->GenerateUnresolvedFieldAccess(instruction,
5586 instruction->GetFieldType(),
5587 instruction->GetFieldIndex(),
5588 instruction->GetDexPc(),
5589 calling_convention);
5590}
5591
5592void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5593 HUnresolvedStaticFieldGet* instruction) {
5594 FieldAccessCallingConventionARM64 calling_convention;
5595 codegen_->CreateUnresolvedFieldLocationSummary(
5596 instruction, instruction->GetFieldType(), calling_convention);
5597}
5598
5599void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5600 HUnresolvedStaticFieldGet* instruction) {
5601 FieldAccessCallingConventionARM64 calling_convention;
5602 codegen_->GenerateUnresolvedFieldAccess(instruction,
5603 instruction->GetFieldType(),
5604 instruction->GetFieldIndex(),
5605 instruction->GetDexPc(),
5606 calling_convention);
5607}
5608
5609void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5610 HUnresolvedStaticFieldSet* instruction) {
5611 FieldAccessCallingConventionARM64 calling_convention;
5612 codegen_->CreateUnresolvedFieldLocationSummary(
5613 instruction, instruction->GetFieldType(), calling_convention);
5614}
5615
5616void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5617 HUnresolvedStaticFieldSet* instruction) {
5618 FieldAccessCallingConventionARM64 calling_convention;
5619 codegen_->GenerateUnresolvedFieldAccess(instruction,
5620 instruction->GetFieldType(),
5621 instruction->GetFieldIndex(),
5622 instruction->GetDexPc(),
5623 calling_convention);
5624}
5625
Alexandre Rames5319def2014-10-23 10:03:10 +01005626void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005627 LocationSummary* locations =
5628 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005629 // In suspend check slow path, usually there are no caller-save registers at all.
5630 // If SIMD instructions are present, however, we force spilling all live SIMD
5631 // registers in full width (since the runtime only saves/restores lower part).
5632 locations->SetCustomSlowPathCallerSaves(
5633 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005634}
5635
5636void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005637 HBasicBlock* block = instruction->GetBlock();
5638 if (block->GetLoopInformation() != nullptr) {
5639 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5640 // The back edge will generate the suspend check.
5641 return;
5642 }
5643 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5644 // The goto will generate the suspend check.
5645 return;
5646 }
5647 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005648}
5649
Alexandre Rames67555f72014-11-18 10:55:16 +00005650void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5651 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005652 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005653 InvokeRuntimeCallingConvention calling_convention;
5654 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5655}
5656
5657void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005658 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005659 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005660}
5661
5662void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5663 LocationSummary* locations =
5664 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5665 Primitive::Type input_type = conversion->GetInputType();
5666 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005667 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005668 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5669 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5670 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5671 }
5672
Alexandre Rames542361f2015-01-29 16:57:31 +00005673 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005674 locations->SetInAt(0, Location::RequiresFpuRegister());
5675 } else {
5676 locations->SetInAt(0, Location::RequiresRegister());
5677 }
5678
Alexandre Rames542361f2015-01-29 16:57:31 +00005679 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005680 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5681 } else {
5682 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5683 }
5684}
5685
5686void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5687 Primitive::Type result_type = conversion->GetResultType();
5688 Primitive::Type input_type = conversion->GetInputType();
5689
5690 DCHECK_NE(input_type, result_type);
5691
Alexandre Rames542361f2015-01-29 16:57:31 +00005692 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005693 int result_size = Primitive::ComponentSize(result_type);
5694 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005695 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005696 Register output = OutputRegister(conversion);
5697 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005698 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005699 // 'int' values are used directly as W registers, discarding the top
5700 // bits, so we don't need to sign-extend and can just perform a move.
5701 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5702 // top 32 bits of the target register. We theoretically could leave those
5703 // bits unchanged, but we would have to make sure that no code uses a
5704 // 32bit input value as a 64bit value assuming that the top 32 bits are
5705 // zero.
5706 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005707 } else if (result_type == Primitive::kPrimChar ||
5708 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5709 __ Ubfx(output,
5710 output.IsX() ? source.X() : source.W(),
5711 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005712 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005713 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005714 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005715 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005716 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005717 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005718 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5719 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005720 } else if (Primitive::IsFloatingPointType(result_type) &&
5721 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005722 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5723 } else {
5724 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5725 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005726 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005727}
Alexandre Rames67555f72014-11-18 10:55:16 +00005728
Serban Constantinescu02164b32014-11-13 14:05:07 +00005729void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5730 HandleShift(ushr);
5731}
5732
5733void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5734 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005735}
5736
5737void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5738 HandleBinaryOp(instruction);
5739}
5740
5741void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5742 HandleBinaryOp(instruction);
5743}
5744
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005745void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005746 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005747 LOG(FATAL) << "Unreachable";
5748}
5749
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005750void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005751 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005752 LOG(FATAL) << "Unreachable";
5753}
5754
Mark Mendellfe57faa2015-09-18 09:26:15 -04005755// Simple implementation of packed switch - generate cascaded compare/jumps.
5756void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5757 LocationSummary* locations =
5758 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5759 locations->SetInAt(0, Location::RequiresRegister());
5760}
5761
5762void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5763 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005764 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005765 Register value_reg = InputRegisterAt(switch_instr, 0);
5766 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5767
Zheng Xu3927c8b2015-11-18 17:46:25 +08005768 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005769 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005770 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5771 // make sure we don't emit it if the target may run out of range.
5772 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5773 // ranges and emit the tables only as required.
5774 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005775
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005776 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005777 // Current instruction id is an upper bound of the number of HIRs in the graph.
5778 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5779 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005780 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5781 Register temp = temps.AcquireW();
5782 __ Subs(temp, value_reg, Operand(lower_bound));
5783
Zheng Xu3927c8b2015-11-18 17:46:25 +08005784 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005785 // Jump to successors[0] if value == lower_bound.
5786 __ B(eq, codegen_->GetLabelOf(successors[0]));
5787 int32_t last_index = 0;
5788 for (; num_entries - last_index > 2; last_index += 2) {
5789 __ Subs(temp, temp, Operand(2));
5790 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5791 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5792 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5793 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5794 }
5795 if (num_entries - last_index == 2) {
5796 // The last missing case_value.
5797 __ Cmp(temp, Operand(1));
5798 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005799 }
5800
5801 // And the default for any other value.
5802 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5803 __ B(codegen_->GetLabelOf(default_block));
5804 }
5805 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005806 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005807
5808 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5809
5810 // Below instructions should use at most one blocked register. Since there are two blocked
5811 // registers, we are free to block one.
5812 Register temp_w = temps.AcquireW();
5813 Register index;
5814 // Remove the bias.
5815 if (lower_bound != 0) {
5816 index = temp_w;
5817 __ Sub(index, value_reg, Operand(lower_bound));
5818 } else {
5819 index = value_reg;
5820 }
5821
5822 // Jump to default block if index is out of the range.
5823 __ Cmp(index, Operand(num_entries));
5824 __ B(hs, codegen_->GetLabelOf(default_block));
5825
5826 // In current VIXL implementation, it won't require any blocked registers to encode the
5827 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5828 // register pressure.
5829 Register table_base = temps.AcquireX();
5830 // Load jump offset from the table.
5831 __ Adr(table_base, jump_table->GetTableStartLabel());
5832 Register jump_offset = temp_w;
5833 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5834
5835 // Jump to target block by branching to table_base(pc related) + offset.
5836 Register target_address = table_base;
5837 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5838 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005839 }
5840}
5841
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005842void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5843 HInstruction* instruction,
5844 Location out,
5845 uint32_t offset,
5846 Location maybe_temp,
5847 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005848 Primitive::Type type = Primitive::kPrimNot;
5849 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005850 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005851 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005852 if (kUseBakerReadBarrier) {
5853 // Load with fast path based Baker's read barrier.
5854 // /* HeapReference<Object> */ out = *(out + offset)
5855 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5856 out,
5857 out_reg,
5858 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005859 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005860 /* needs_null_check */ false,
5861 /* use_load_acquire */ false);
5862 } else {
5863 // Load with slow path based read barrier.
5864 // Save the value of `out` into `maybe_temp` before overwriting it
5865 // in the following move operation, as we will need it for the
5866 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005867 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005868 __ Mov(temp_reg, out_reg);
5869 // /* HeapReference<Object> */ out = *(out + offset)
5870 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5871 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5872 }
5873 } else {
5874 // Plain load with no read barrier.
5875 // /* HeapReference<Object> */ out = *(out + offset)
5876 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5877 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5878 }
5879}
5880
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005881void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5882 HInstruction* instruction,
5883 Location out,
5884 Location obj,
5885 uint32_t offset,
5886 Location maybe_temp,
5887 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005888 Primitive::Type type = Primitive::kPrimNot;
5889 Register out_reg = RegisterFrom(out, type);
5890 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005891 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005892 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005893 if (kUseBakerReadBarrier) {
5894 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005895 // /* HeapReference<Object> */ out = *(obj + offset)
5896 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5897 out,
5898 obj_reg,
5899 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005900 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005901 /* needs_null_check */ false,
5902 /* use_load_acquire */ false);
5903 } else {
5904 // Load with slow path based read barrier.
5905 // /* HeapReference<Object> */ out = *(obj + offset)
5906 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5907 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5908 }
5909 } else {
5910 // Plain load with no read barrier.
5911 // /* HeapReference<Object> */ out = *(obj + offset)
5912 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5913 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5914 }
5915}
5916
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005917void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5918 HInstruction* instruction,
5919 Location root,
5920 Register obj,
5921 uint32_t offset,
5922 vixl::aarch64::Label* fixup_label,
5923 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005924 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005925 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005926 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005927 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005928 if (kUseBakerReadBarrier) {
5929 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005930 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005931 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5932 !Runtime::Current()->UseJitCompilation()) {
5933 // Note that we do not actually check the value of `GetIsGcMarking()`
5934 // to decide whether to mark the loaded GC root or not. Instead, we
Vladimir Marko66d691d2017-04-07 17:53:39 +01005935 // load into `temp` (actually IP1) the read barrier mark introspection
5936 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
5937 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005938 //
5939 // We use link-time generated thunks for the slow path. That thunk
5940 // checks the reference and jumps to the entrypoint if needed.
5941 //
5942 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5943 // lr = &return_address;
5944 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5945 // if (temp != nullptr) {
5946 // goto gc_root_thunk<root_reg>(lr)
5947 // }
5948 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005949
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005950 UseScratchRegisterScope temps(GetVIXLAssembler());
5951 DCHECK(temps.IsAvailable(ip0));
5952 DCHECK(temps.IsAvailable(ip1));
5953 temps.Exclude(ip0, ip1);
5954 uint32_t custom_data =
5955 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5956 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005957
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005958 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5959 DCHECK_EQ(ip0.GetCode(), 16u);
5960 const int32_t entry_point_offset =
5961 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5962 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5963 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5964 vixl::aarch64::Label return_address;
5965 __ adr(lr, &return_address);
5966 if (fixup_label != nullptr) {
5967 __ Bind(fixup_label);
5968 }
5969 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5970 "GC root LDR must be 2 instruction (8B) before the return address label.");
5971 __ ldr(root_reg, MemOperand(obj.X(), offset));
5972 __ Bind(cbnz_label);
5973 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5974 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005975 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005976 // Note that we do not actually check the value of
5977 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5978 // root or not. Instead, we load into `temp` the read barrier
5979 // mark entry point corresponding to register `root`. If `temp`
5980 // is null, it means that `GetIsGcMarking()` is false, and vice
5981 // versa.
5982 //
5983 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5984 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5985 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5986 // // Slow path.
5987 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5988 // }
Roland Levillain44015862016-01-22 11:47:17 +00005989
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005990 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5991 Register temp = lr;
5992 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5993 instruction, root, /* entrypoint */ LocationFrom(temp));
5994 codegen_->AddSlowPath(slow_path);
5995
5996 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5997 const int32_t entry_point_offset =
5998 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5999 // Loading the entrypoint does not require a load acquire since it is only changed when
6000 // threads are suspended or running a checkpoint.
6001 __ Ldr(temp, MemOperand(tr, entry_point_offset));
6002
6003 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6004 if (fixup_label == nullptr) {
6005 __ Ldr(root_reg, MemOperand(obj, offset));
6006 } else {
6007 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6008 }
6009 static_assert(
6010 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6011 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6012 "have different sizes.");
6013 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6014 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6015 "have different sizes.");
6016
6017 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6018 // checking GetIsGcMarking.
6019 __ Cbnz(temp, slow_path->GetEntryLabel());
6020 __ Bind(slow_path->GetExitLabel());
6021 }
Roland Levillain44015862016-01-22 11:47:17 +00006022 } else {
6023 // GC root loaded through a slow path for read barriers other
6024 // than Baker's.
6025 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 if (fixup_label == nullptr) {
6027 __ Add(root_reg.X(), obj.X(), offset);
6028 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006029 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006030 }
Roland Levillain44015862016-01-22 11:47:17 +00006031 // /* mirror::Object* */ root = root->Read()
6032 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6033 }
6034 } else {
6035 // Plain GC root load with no read barrier.
6036 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006037 if (fixup_label == nullptr) {
6038 __ Ldr(root_reg, MemOperand(obj, offset));
6039 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006040 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006041 }
Roland Levillain44015862016-01-22 11:47:17 +00006042 // Note that GC roots are not affected by heap poisoning, thus we
6043 // do not have to unpoison `root_reg` here.
6044 }
6045}
6046
6047void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6048 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006049 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006050 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006051 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006052 bool needs_null_check,
6053 bool use_load_acquire) {
6054 DCHECK(kEmitCompilerReadBarrier);
6055 DCHECK(kUseBakerReadBarrier);
6056
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006057 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6058 !use_load_acquire &&
6059 !Runtime::Current()->UseJitCompilation()) {
6060 // Note that we do not actually check the value of `GetIsGcMarking()`
Vladimir Marko66d691d2017-04-07 17:53:39 +01006061 // to decide whether to mark the loaded reference or not. Instead, we
6062 // load into `temp` (actually IP1) the read barrier mark introspection
6063 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6064 // false, and vice versa.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006065 //
6066 // We use link-time generated thunks for the slow path. That thunk checks
6067 // the holder and jumps to the entrypoint if needed. If the holder is not
6068 // gray, it creates a fake dependency and returns to the LDR instruction.
6069 //
6070 // temp = Thread::Current()->pReadBarrierMarkIntrospection
Vladimir Marko66d691d2017-04-07 17:53:39 +01006071 // lr = &gray_return_address;
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006072 // if (temp != nullptr) {
6073 // goto field_thunk<holder_reg, base_reg>(lr)
6074 // }
6075 // not_gray_return_address:
6076 // // Original reference load. If the offset is too large to fit
6077 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006078 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006079 // gray_return_address:
6080
6081 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6082 Register base = obj;
6083 if (offset >= kReferenceLoadMinFarOffset) {
6084 DCHECK(maybe_temp.IsRegister());
6085 base = WRegisterFrom(maybe_temp);
6086 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6087 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6088 offset &= (kReferenceLoadMinFarOffset - 1u);
6089 }
6090 UseScratchRegisterScope temps(GetVIXLAssembler());
6091 DCHECK(temps.IsAvailable(ip0));
6092 DCHECK(temps.IsAvailable(ip1));
6093 temps.Exclude(ip0, ip1);
6094 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6095 base.GetCode(),
6096 obj.GetCode());
6097 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6098
6099 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6100 DCHECK_EQ(ip0.GetCode(), 16u);
6101 const int32_t entry_point_offset =
6102 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6103 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006104 EmissionCheckScope guard(GetVIXLAssembler(),
6105 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006106 vixl::aarch64::Label return_address;
6107 __ adr(lr, &return_address);
6108 __ Bind(cbnz_label);
6109 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006110 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6111 "Field LDR must be 1 instruction (4B) before the return address label; "
6112 " 2 instructions (8B) for heap poisoning.");
6113 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6114 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006115 if (needs_null_check) {
6116 MaybeRecordImplicitNullCheck(instruction);
6117 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006118 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006119 __ Bind(&return_address);
6120 return;
6121 }
6122
Roland Levillain44015862016-01-22 11:47:17 +00006123 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006124 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006125 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006126 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006127 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6128 ref,
6129 obj,
6130 offset,
6131 no_index,
6132 no_scale_factor,
6133 temp,
6134 needs_null_check,
6135 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006136}
6137
6138void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6139 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006140 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006141 uint32_t data_offset,
6142 Location index,
6143 Register temp,
6144 bool needs_null_check) {
6145 DCHECK(kEmitCompilerReadBarrier);
6146 DCHECK(kUseBakerReadBarrier);
6147
Vladimir Marko66d691d2017-04-07 17:53:39 +01006148 static_assert(
6149 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6150 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6151 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6152
6153 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6154 !Runtime::Current()->UseJitCompilation()) {
6155 // Note that we do not actually check the value of `GetIsGcMarking()`
6156 // to decide whether to mark the loaded reference or not. Instead, we
6157 // load into `temp` (actually IP1) the read barrier mark introspection
6158 // entrypoint. If `temp` is null, it means that `GetIsGcMarking()` is
6159 // false, and vice versa.
6160 //
6161 // We use link-time generated thunks for the slow path. That thunk checks
6162 // the holder and jumps to the entrypoint if needed. If the holder is not
6163 // gray, it creates a fake dependency and returns to the LDR instruction.
6164 //
6165 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6166 // lr = &gray_return_address;
6167 // if (temp != nullptr) {
6168 // goto field_thunk<holder_reg, base_reg>(lr)
6169 // }
6170 // not_gray_return_address:
6171 // // Original reference load. If the offset is too large to fit
6172 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01006173 // HeapReference<mirror::Object> reference = data[index];
Vladimir Marko66d691d2017-04-07 17:53:39 +01006174 // gray_return_address:
6175
6176 DCHECK(index.IsValid());
6177 Register index_reg = RegisterFrom(index, Primitive::kPrimInt);
6178 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6179
6180 UseScratchRegisterScope temps(GetVIXLAssembler());
6181 DCHECK(temps.IsAvailable(ip0));
6182 DCHECK(temps.IsAvailable(ip1));
6183 temps.Exclude(ip0, ip1);
6184 uint32_t custom_data =
6185 linker::Arm64RelativePatcher::EncodeBakerReadBarrierArrayData(temp.GetCode());
6186 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6187
6188 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6189 DCHECK_EQ(ip0.GetCode(), 16u);
6190 const int32_t entry_point_offset =
6191 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6192 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6193 __ Add(temp.X(), obj.X(), Operand(data_offset));
6194 EmissionCheckScope guard(GetVIXLAssembler(),
6195 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
6196 vixl::aarch64::Label return_address;
6197 __ adr(lr, &return_address);
6198 __ Bind(cbnz_label);
6199 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6200 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6201 "Array LDR must be 1 instruction (4B) before the return address label; "
6202 " 2 instructions (8B) for heap poisoning.");
6203 __ ldr(ref_reg, MemOperand(temp.X(), index_reg.X(), LSL, scale_factor));
6204 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
6205 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
6206 __ Bind(&return_address);
6207 return;
6208 }
6209
Roland Levillain44015862016-01-22 11:47:17 +00006210 // Array cells are never volatile variables, therefore array loads
6211 // never use Load-Acquire instructions on ARM64.
6212 const bool use_load_acquire = false;
6213
6214 // /* HeapReference<Object> */ ref =
6215 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006216 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6217 ref,
6218 obj,
6219 data_offset,
6220 index,
6221 scale_factor,
6222 temp,
6223 needs_null_check,
6224 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006225}
6226
6227void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6228 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006229 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006230 uint32_t offset,
6231 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006232 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006233 Register temp,
6234 bool needs_null_check,
Roland Levillainff487002017-03-07 16:50:01 +00006235 bool use_load_acquire) {
Roland Levillain44015862016-01-22 11:47:17 +00006236 DCHECK(kEmitCompilerReadBarrier);
6237 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006238 // If we are emitting an array load, we should not be using a
6239 // Load Acquire instruction. In other words:
6240 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6241 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006242
Roland Levillain54f869e2017-03-06 13:54:11 +00006243 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6244 // whether we need to enter the slow path to mark the reference.
6245 // Then, in the slow path, check the gray bit in the lock word of
6246 // the reference's holder (`obj`) to decide whether to mark `ref` or
6247 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006248 //
Roland Levillainba650a42017-03-06 13:52:32 +00006249 // Note that we do not actually check the value of `GetIsGcMarking()`;
6250 // instead, we load into `temp2` the read barrier mark entry point
6251 // corresponding to register `ref`. If `temp2` is null, it means
6252 // that `GetIsGcMarking()` is false, and vice versa.
6253 //
6254 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006255 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6256 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006257 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6258 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6259 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6260 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6261 // if (is_gray) {
6262 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6263 // }
6264 // } else {
6265 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006266 // }
Roland Levillain44015862016-01-22 11:47:17 +00006267
Roland Levillainba650a42017-03-06 13:52:32 +00006268 // Slow path marking the object `ref` when the GC is marking. The
6269 // entrypoint will already be loaded in `temp2`.
6270 Register temp2 = lr;
6271 Location temp2_loc = LocationFrom(temp2);
Roland Levillainff487002017-03-07 16:50:01 +00006272 SlowPathCodeARM64* slow_path =
6273 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6274 instruction,
6275 ref,
6276 obj,
6277 offset,
6278 index,
6279 scale_factor,
6280 needs_null_check,
6281 use_load_acquire,
6282 temp,
6283 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006284 AddSlowPath(slow_path);
6285
6286 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6287 const int32_t entry_point_offset =
6288 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6289 // Loading the entrypoint does not require a load acquire since it is only changed when
6290 // threads are suspended or running a checkpoint.
6291 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006292 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6293 // checking GetIsGcMarking.
6294 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00006295 // Fast path: the GC is not marking: just load the reference.
Roland Levillain54f869e2017-03-06 13:54:11 +00006296 GenerateRawReferenceLoad(
6297 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006298 __ Bind(slow_path->GetExitLabel());
6299}
6300
Roland Levillainff487002017-03-07 16:50:01 +00006301void CodeGeneratorARM64::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
6302 Location ref,
6303 Register obj,
6304 Location field_offset,
6305 Register temp,
6306 bool needs_null_check,
6307 bool use_load_acquire) {
6308 DCHECK(kEmitCompilerReadBarrier);
6309 DCHECK(kUseBakerReadBarrier);
6310 // If we are emitting an array load, we should not be using a
6311 // Load Acquire instruction. In other words:
6312 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6313 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
6314
6315 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6316 // whether we need to enter the slow path to update the reference
6317 // field within `obj`. Then, in the slow path, check the gray bit
6318 // in the lock word of the reference's holder (`obj`) to decide
6319 // whether to mark `ref` and update the field or not.
6320 //
6321 // Note that we do not actually check the value of `GetIsGcMarking()`;
6322 // instead, we load into `temp2` the read barrier mark entry point
6323 // corresponding to register `ref`. If `temp2` is null, it means
6324 // that `GetIsGcMarking()` is false, and vice versa.
6325 //
6326 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6327 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6328 // // Slow path.
6329 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6330 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6331 // HeapReference<mirror::Object> ref = *(obj + field_offset); // Reference load.
6332 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6333 // if (is_gray) {
6334 // old_ref = ref;
6335 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6336 // compareAndSwapObject(obj, field_offset, old_ref, ref);
6337 // }
6338 // }
6339
6340 // Slow path updating the object reference at address `obj + field_offset`
6341 // when the GC is marking. The entrypoint will already be loaded in `temp2`.
6342 Register temp2 = lr;
6343 Location temp2_loc = LocationFrom(temp2);
6344 SlowPathCodeARM64* slow_path =
6345 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6346 instruction,
6347 ref,
6348 obj,
6349 /* offset */ 0u,
6350 /* index */ field_offset,
6351 /* scale_factor */ 0u /* "times 1" */,
6352 needs_null_check,
6353 use_load_acquire,
6354 temp,
6355 /* entrypoint */ temp2_loc);
6356 AddSlowPath(slow_path);
6357
6358 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6359 const int32_t entry_point_offset =
6360 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6361 // Loading the entrypoint does not require a load acquire since it is only changed when
6362 // threads are suspended or running a checkpoint.
6363 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
6364 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6365 // checking GetIsGcMarking.
6366 __ Cbnz(temp2, slow_path->GetEntryLabel());
6367 // Fast path: the GC is not marking: nothing to do (the field is
6368 // up-to-date, and we don't need to load the reference).
6369 __ Bind(slow_path->GetExitLabel());
6370}
6371
Roland Levillainba650a42017-03-06 13:52:32 +00006372void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6373 Location ref,
6374 Register obj,
6375 uint32_t offset,
6376 Location index,
6377 size_t scale_factor,
6378 bool needs_null_check,
6379 bool use_load_acquire) {
6380 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006381 Primitive::Type type = Primitive::kPrimNot;
6382 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006383
Roland Levillainba650a42017-03-06 13:52:32 +00006384 // If needed, vixl::EmissionCheckScope guards are used to ensure
6385 // that no pools are emitted between the load (macro) instruction
6386 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006387
Roland Levillain44015862016-01-22 11:47:17 +00006388 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006389 // Load types involving an "index": ArrayGet,
6390 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6391 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006392 if (use_load_acquire) {
6393 // UnsafeGetObjectVolatile intrinsic case.
6394 // Register `index` is not an index in an object array, but an
6395 // offset to an object reference field within object `obj`.
6396 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6397 DCHECK(instruction->GetLocations()->Intrinsified());
6398 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6399 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006400 DCHECK_EQ(offset, 0u);
6401 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006402 DCHECK_EQ(needs_null_check, false);
6403 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006404 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6405 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006406 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006407 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6408 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006409 if (index.IsConstant()) {
6410 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006411 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006412 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006413 if (needs_null_check) {
6414 MaybeRecordImplicitNullCheck(instruction);
6415 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006416 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006417 UseScratchRegisterScope temps(GetVIXLAssembler());
6418 Register temp = temps.AcquireW();
6419 __ Add(temp, obj, offset);
6420 {
6421 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6422 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6423 if (needs_null_check) {
6424 MaybeRecordImplicitNullCheck(instruction);
6425 }
6426 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006427 }
Roland Levillain44015862016-01-22 11:47:17 +00006428 }
Roland Levillain44015862016-01-22 11:47:17 +00006429 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006430 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006431 MemOperand field = HeapOperand(obj, offset);
6432 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006433 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6434 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006435 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006436 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006437 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006438 if (needs_null_check) {
6439 MaybeRecordImplicitNullCheck(instruction);
6440 }
Roland Levillain44015862016-01-22 11:47:17 +00006441 }
6442 }
6443
6444 // Object* ref = ref_addr->AsMirrorPtr()
6445 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006446}
6447
6448void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6449 Location out,
6450 Location ref,
6451 Location obj,
6452 uint32_t offset,
6453 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006454 DCHECK(kEmitCompilerReadBarrier);
6455
Roland Levillain44015862016-01-22 11:47:17 +00006456 // Insert a slow path based read barrier *after* the reference load.
6457 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006458 // If heap poisoning is enabled, the unpoisoning of the loaded
6459 // reference will be carried out by the runtime within the slow
6460 // path.
6461 //
6462 // Note that `ref` currently does not get unpoisoned (when heap
6463 // poisoning is enabled), which is alright as the `ref` argument is
6464 // not used by the artReadBarrierSlow entry point.
6465 //
6466 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6467 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6468 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6469 AddSlowPath(slow_path);
6470
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006471 __ B(slow_path->GetEntryLabel());
6472 __ Bind(slow_path->GetExitLabel());
6473}
6474
Roland Levillain44015862016-01-22 11:47:17 +00006475void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6476 Location out,
6477 Location ref,
6478 Location obj,
6479 uint32_t offset,
6480 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006481 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006482 // Baker's read barriers shall be handled by the fast path
6483 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6484 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006485 // If heap poisoning is enabled, unpoisoning will be taken care of
6486 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006487 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006488 } else if (kPoisonHeapReferences) {
6489 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6490 }
6491}
6492
Roland Levillain44015862016-01-22 11:47:17 +00006493void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6494 Location out,
6495 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006496 DCHECK(kEmitCompilerReadBarrier);
6497
Roland Levillain44015862016-01-22 11:47:17 +00006498 // Insert a slow path based read barrier *after* the GC root load.
6499 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006500 // Note that GC roots are not affected by heap poisoning, so we do
6501 // not need to do anything special for this here.
6502 SlowPathCodeARM64* slow_path =
6503 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6504 AddSlowPath(slow_path);
6505
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006506 __ B(slow_path->GetEntryLabel());
6507 __ Bind(slow_path->GetExitLabel());
6508}
6509
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006510void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6511 LocationSummary* locations =
6512 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6513 locations->SetInAt(0, Location::RequiresRegister());
6514 locations->SetOut(Location::RequiresRegister());
6515}
6516
6517void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6518 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006519 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006520 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006521 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006522 __ Ldr(XRegisterFrom(locations->Out()),
6523 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006524 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006525 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006526 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006527 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6528 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006529 __ Ldr(XRegisterFrom(locations->Out()),
6530 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006531 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006532}
6533
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006534static void PatchJitRootUse(uint8_t* code,
6535 const uint8_t* roots_data,
6536 vixl::aarch64::Literal<uint32_t>* literal,
6537 uint64_t index_in_table) {
6538 uint32_t literal_offset = literal->GetOffset();
6539 uintptr_t address =
6540 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6541 uint8_t* data = code + literal_offset;
6542 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6543}
6544
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006545void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6546 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006547 const StringReference& string_reference = entry.first;
6548 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6549 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006550 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006551 uint64_t index_in_table = it->second;
6552 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006553 }
6554 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006555 const TypeReference& type_reference = entry.first;
6556 vixl::aarch64::Literal<uint32_t>* table_entry_literal = entry.second;
6557 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006558 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01006559 uint64_t index_in_table = it->second;
6560 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006561 }
6562}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006563
Alexandre Rames67555f72014-11-18 10:55:16 +00006564#undef __
6565#undef QUICK_ENTRY_POINT
6566
Alexandre Rames5319def2014-10-23 10:03:10 +01006567} // namespace arm64
6568} // namespace art