blob: 4629c54a173af65707fe1ee8b909ae149922eee9 [file] [log] [blame]
Alexandre Rames5319def2014-10-23 10:03:10 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm64.h"
18
Vladimir Markof4f2daa2017-03-20 18:26:59 +000019#include "arch/arm64/asm_support_arm64.h"
Serban Constantinescu579885a2015-02-22 20:51:33 +000020#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe1cc7dba2014-12-17 18:43:01 -080025#include "entrypoints/quick/quick_entrypoints_enum.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010026#include "gc/accounting/card_table.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "intrinsics.h"
28#include "intrinsics_arm64.h"
Vladimir Markof4f2daa2017-03-20 18:26:59 +000029#include "linker/arm64/relative_patcher_arm64.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Calin Juravlecd6dffe2015-01-08 17:35:35 +000032#include "offsets.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010033#include "thread.h"
34#include "utils/arm64/assembler_arm64.h"
35#include "utils/assembler.h"
36#include "utils/stack_checks.h"
37
Scott Wakeling97c72b72016-06-24 16:19:36 +010038using namespace vixl::aarch64; // NOLINT(build/namespaces)
Artem Serov914d7a82017-02-07 14:33:49 +000039using vixl::ExactAssemblyScope;
40using vixl::CodeBufferCheckScope;
41using vixl::EmissionCheckScope;
Alexandre Rames5319def2014-10-23 10:03:10 +010042
43#ifdef __
44#error "ARM64 Codegen VIXL macro-assembler macro already defined."
45#endif
46
Alexandre Rames5319def2014-10-23 10:03:10 +010047namespace art {
48
Roland Levillain22ccc3a2015-11-24 13:10:05 +000049template<class MirrorType>
50class GcRoot;
51
Alexandre Rames5319def2014-10-23 10:03:10 +010052namespace arm64 {
53
Alexandre Ramesbe919d92016-08-23 18:33:36 +010054using helpers::ARM64EncodableConstantOrRegister;
55using helpers::ArtVixlRegCodeCoherentForRegSet;
Andreas Gampe878d58c2015-01-15 23:24:00 -080056using helpers::CPURegisterFrom;
57using helpers::DRegisterFrom;
58using helpers::FPRegisterFrom;
59using helpers::HeapOperand;
60using helpers::HeapOperandFrom;
61using helpers::InputCPURegisterAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010062using helpers::InputCPURegisterOrZeroRegAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080063using helpers::InputFPRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080064using helpers::InputOperandAt;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010065using helpers::InputRegisterAt;
Andreas Gampe878d58c2015-01-15 23:24:00 -080066using helpers::Int64ConstantFrom;
Alexandre Ramesbe919d92016-08-23 18:33:36 +010067using helpers::IsConstantZeroBitPattern;
Andreas Gampe878d58c2015-01-15 23:24:00 -080068using helpers::LocationFrom;
69using helpers::OperandFromMemOperand;
70using helpers::OutputCPURegister;
71using helpers::OutputFPRegister;
72using helpers::OutputRegister;
Artem Serovd4bccf12017-04-03 18:47:32 +010073using helpers::QRegisterFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080074using helpers::RegisterFrom;
75using helpers::StackOperandFrom;
76using helpers::VIXLRegCodeFromART;
77using helpers::WRegisterFrom;
78using helpers::XRegisterFrom;
79
Alexandre Rames5319def2014-10-23 10:03:10 +010080static constexpr int kCurrentMethodStackOffset = 0;
Vladimir Markof3e0ee22015-12-17 15:23:13 +000081// The compare/jump sequence will generate about (1.5 * num_entries + 3) instructions. While jump
Zheng Xu3927c8b2015-11-18 17:46:25 +080082// table version generates 7 instructions and num_entries literals. Compare/jump sequence will
83// generates less code/data with a small num_entries.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000084static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Alexandre Rames5319def2014-10-23 10:03:10 +010085
Vladimir Markof4f2daa2017-03-20 18:26:59 +000086// Reference load (except object array loads) is using LDR Wt, [Xn, #offset] which can handle
87// offset < 16KiB. For offsets >= 16KiB, the load shall be emitted as two or more instructions.
88// For the Baker read barrier implementation using link-generated thunks we need to split
89// the offset explicitly.
90constexpr uint32_t kReferenceLoadMinFarOffset = 16 * KB;
91
92// Flags controlling the use of link-time generated thunks for Baker read barriers.
Vladimir Markod1ef8732017-04-18 13:55:13 +010093constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
Vladimir Markof4f2daa2017-03-20 18:26:59 +000095
96// Some instructions have special requirements for a temporary, for example
97// LoadClass/kBssEntry and LoadString/kBssEntry for Baker read barrier require
98// temp that's not an R0 (to avoid an extra move) and Baker read barrier field
99// loads with large offsets need a fixed register to limit the number of link-time
100// thunks we generate. For these and similar cases, we want to reserve a specific
101// register that's neither callee-save nor an argument register. We choose x15.
102inline Location FixedTempLocation() {
103 return Location::RegisterLocation(x15.GetCode());
104}
105
Alexandre Rames5319def2014-10-23 10:03:10 +0100106inline Condition ARM64Condition(IfCondition cond) {
107 switch (cond) {
108 case kCondEQ: return eq;
109 case kCondNE: return ne;
110 case kCondLT: return lt;
111 case kCondLE: return le;
112 case kCondGT: return gt;
113 case kCondGE: return ge;
Aart Bike9f37602015-10-09 11:15:55 -0700114 case kCondB: return lo;
115 case kCondBE: return ls;
116 case kCondA: return hi;
117 case kCondAE: return hs;
Alexandre Rames5319def2014-10-23 10:03:10 +0100118 }
Roland Levillain7f63c522015-07-13 15:54:55 +0000119 LOG(FATAL) << "Unreachable";
120 UNREACHABLE();
Alexandre Rames5319def2014-10-23 10:03:10 +0100121}
122
Vladimir Markod6e069b2016-01-18 11:11:01 +0000123inline Condition ARM64FPCondition(IfCondition cond, bool gt_bias) {
124 // The ARM64 condition codes can express all the necessary branches, see the
125 // "Meaning (floating-point)" column in the table C1-1 in the ARMv8 reference manual.
126 // There is no dex instruction or HIR that would need the missing conditions
127 // "equal or unordered" or "not equal".
128 switch (cond) {
129 case kCondEQ: return eq;
130 case kCondNE: return ne /* unordered */;
131 case kCondLT: return gt_bias ? cc : lt /* unordered */;
132 case kCondLE: return gt_bias ? ls : le /* unordered */;
133 case kCondGT: return gt_bias ? hi /* unordered */ : gt;
134 case kCondGE: return gt_bias ? cs /* unordered */ : ge;
135 default:
136 LOG(FATAL) << "UNREACHABLE";
137 UNREACHABLE();
138 }
139}
140
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000141Location ARM64ReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000142 // Note that in practice, `LocationFrom(x0)` and `LocationFrom(w0)` create the
143 // same Location object, and so do `LocationFrom(d0)` and `LocationFrom(s0)`,
144 // but we use the exact registers for clarity.
145 if (return_type == Primitive::kPrimFloat) {
146 return LocationFrom(s0);
147 } else if (return_type == Primitive::kPrimDouble) {
148 return LocationFrom(d0);
149 } else if (return_type == Primitive::kPrimLong) {
150 return LocationFrom(x0);
Nicolas Geoffray925e5622015-06-03 12:23:32 +0100151 } else if (return_type == Primitive::kPrimVoid) {
152 return Location::NoLocation();
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000153 } else {
154 return LocationFrom(w0);
155 }
156}
157
Alexandre Rames5319def2014-10-23 10:03:10 +0100158Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type return_type) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000159 return ARM64ReturnLocation(return_type);
Alexandre Rames5319def2014-10-23 10:03:10 +0100160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, x).Int32Value()
Alexandre Rames5319def2014-10-23 10:03:10 +0100165
Zheng Xuda403092015-04-24 17:35:39 +0800166// Calculate memory accessing operand for save/restore live registers.
167static void SaveRestoreLiveRegistersHelper(CodeGenerator* codegen,
Vladimir Marko804b03f2016-09-14 16:26:36 +0100168 LocationSummary* locations,
Zheng Xuda403092015-04-24 17:35:39 +0800169 int64_t spill_offset,
170 bool is_save) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100171 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
172 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
173 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800174 codegen->GetNumberOfCoreRegisters(),
Vladimir Marko804b03f2016-09-14 16:26:36 +0100175 fp_spills,
Zheng Xuda403092015-04-24 17:35:39 +0800176 codegen->GetNumberOfFloatingPointRegisters()));
177
Vladimir Marko804b03f2016-09-14 16:26:36 +0100178 CPURegList core_list = CPURegList(CPURegister::kRegister, kXRegSize, core_spills);
Artem Serov7957d952017-04-04 15:44:09 +0100179 unsigned v_reg_size = codegen->GetGraph()->HasSIMD() ? kQRegSize : kDRegSize;
180 CPURegList fp_list = CPURegList(CPURegister::kVRegister, v_reg_size, fp_spills);
Zheng Xuda403092015-04-24 17:35:39 +0800181
182 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen)->GetVIXLAssembler();
183 UseScratchRegisterScope temps(masm);
184
185 Register base = masm->StackPointer();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100186 int64_t core_spill_size = core_list.GetTotalSizeInBytes();
187 int64_t fp_spill_size = fp_list.GetTotalSizeInBytes();
Zheng Xuda403092015-04-24 17:35:39 +0800188 int64_t reg_size = kXRegSizeInBytes;
189 int64_t max_ls_pair_offset = spill_offset + core_spill_size + fp_spill_size - 2 * reg_size;
190 uint32_t ls_access_size = WhichPowerOf2(reg_size);
Scott Wakeling97c72b72016-06-24 16:19:36 +0100191 if (((core_list.GetCount() > 1) || (fp_list.GetCount() > 1)) &&
Zheng Xuda403092015-04-24 17:35:39 +0800192 !masm->IsImmLSPair(max_ls_pair_offset, ls_access_size)) {
193 // If the offset does not fit in the instruction's immediate field, use an alternate register
194 // to compute the base address(float point registers spill base address).
195 Register new_base = temps.AcquireSameSizeAs(base);
196 __ Add(new_base, base, Operand(spill_offset + core_spill_size));
197 base = new_base;
198 spill_offset = -core_spill_size;
199 int64_t new_max_ls_pair_offset = fp_spill_size - 2 * reg_size;
200 DCHECK(masm->IsImmLSPair(spill_offset, ls_access_size));
201 DCHECK(masm->IsImmLSPair(new_max_ls_pair_offset, ls_access_size));
202 }
203
204 if (is_save) {
205 __ StoreCPURegList(core_list, MemOperand(base, spill_offset));
206 __ StoreCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
207 } else {
208 __ LoadCPURegList(core_list, MemOperand(base, spill_offset));
209 __ LoadCPURegList(fp_list, MemOperand(base, spill_offset + core_spill_size));
210 }
211}
212
213void SlowPathCodeARM64::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Zheng Xuda403092015-04-24 17:35:39 +0800214 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Vladimir Marko804b03f2016-09-14 16:26:36 +0100215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 // If the register holds an object, update the stack mask.
218 if (locations->RegisterContainsObject(i)) {
219 locations->SetStackBit(stack_offset / kVRegSize);
Zheng Xuda403092015-04-24 17:35:39 +0800220 }
Vladimir Marko804b03f2016-09-14 16:26:36 +0100221 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
222 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
223 saved_core_stack_offsets_[i] = stack_offset;
224 stack_offset += kXRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800225 }
226
Vladimir Marko804b03f2016-09-14 16:26:36 +0100227 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
228 for (uint32_t i : LowToHighBits(fp_spills)) {
229 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
230 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
231 saved_fpu_stack_offsets_[i] = stack_offset;
232 stack_offset += kDRegSizeInBytes;
Zheng Xuda403092015-04-24 17:35:39 +0800233 }
234
Vladimir Marko804b03f2016-09-14 16:26:36 +0100235 SaveRestoreLiveRegistersHelper(codegen,
236 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800237 codegen->GetFirstRegisterSlotInSlowPath(), true /* is_save */);
238}
239
240void SlowPathCodeARM64::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100241 SaveRestoreLiveRegistersHelper(codegen,
242 locations,
Zheng Xuda403092015-04-24 17:35:39 +0800243 codegen->GetFirstRegisterSlotInSlowPath(), false /* is_save */);
244}
245
Alexandre Rames5319def2014-10-23 10:03:10 +0100246class BoundsCheckSlowPathARM64 : public SlowPathCodeARM64 {
247 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000248 explicit BoundsCheckSlowPathARM64(HBoundsCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100251 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000252 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100253
Alexandre Rames5319def2014-10-23 10:03:10 +0100254 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000255 if (instruction_->CanThrowIntoCatchBlock()) {
256 // Live registers will be restored in the catch block if caught.
257 SaveLiveRegisters(codegen, instruction_->GetLocations());
258 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000259 // We're moving two locations to locations that could overlap, so we need a parallel
260 // move resolver.
261 InvokeRuntimeCallingConvention calling_convention;
262 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100263 locations->InAt(0), LocationFrom(calling_convention.GetRegisterAt(0)), Primitive::kPrimInt,
264 locations->InAt(1), LocationFrom(calling_convention.GetRegisterAt(1)), Primitive::kPrimInt);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000265 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
266 ? kQuickThrowStringBounds
267 : kQuickThrowArrayBounds;
268 arm64_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100269 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800270 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100271 }
272
Alexandre Rames8158f282015-08-07 10:26:17 +0100273 bool IsFatal() const OVERRIDE { return true; }
274
Alexandre Rames9931f312015-06-19 14:47:01 +0100275 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM64"; }
276
Alexandre Rames5319def2014-10-23 10:03:10 +0100277 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100278 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM64);
279};
280
Alexandre Rames67555f72014-11-18 10:55:16 +0000281class DivZeroCheckSlowPathARM64 : public SlowPathCodeARM64 {
282 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000283 explicit DivZeroCheckSlowPathARM64(HDivZeroCheck* instruction) : SlowPathCodeARM64(instruction) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000284
285 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
286 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
287 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000288 arm64_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800289 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Alexandre Rames67555f72014-11-18 10:55:16 +0000290 }
291
Alexandre Rames8158f282015-08-07 10:26:17 +0100292 bool IsFatal() const OVERRIDE { return true; }
293
Alexandre Rames9931f312015-06-19 14:47:01 +0100294 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM64"; }
295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 private:
Alexandre Rames67555f72014-11-18 10:55:16 +0000297 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM64);
298};
299
300class LoadClassSlowPathARM64 : public SlowPathCodeARM64 {
301 public:
302 LoadClassSlowPathARM64(HLoadClass* cls,
303 HInstruction* at,
304 uint32_t dex_pc,
Vladimir Markoea4c1262017-02-06 19:59:33 +0000305 bool do_clinit,
306 vixl::aarch64::Register bss_entry_temp = vixl::aarch64::Register(),
307 vixl::aarch64::Label* bss_entry_adrp_label = nullptr)
308 : SlowPathCodeARM64(at),
309 cls_(cls),
310 dex_pc_(dex_pc),
311 do_clinit_(do_clinit),
312 bss_entry_temp_(bss_entry_temp),
313 bss_entry_adrp_label_(bss_entry_adrp_label) {
Alexandre Rames67555f72014-11-18 10:55:16 +0000314 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
315 }
316
317 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000318 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000319 Location out = locations->Out();
320 constexpr bool call_saves_everything_except_r0_ip0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexandre Rames67555f72014-11-18 10:55:16 +0000321 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
322
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000323 InvokeRuntimeCallingConvention calling_convention;
324 // For HLoadClass/kBssEntry/kSaveEverything, the page address of the entry is in a temp
325 // register, make sure it's not clobbered by the call or by saving/restoring registers.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000326 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
327 bool is_load_class_bss_entry =
328 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000329 if (is_load_class_bss_entry) {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000330 DCHECK(bss_entry_temp_.IsValid());
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000331 DCHECK(!bss_entry_temp_.Is(calling_convention.GetRegisterAt(0)));
332 DCHECK(
333 !UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(bss_entry_temp_));
Vladimir Markoea4c1262017-02-06 19:59:33 +0000334 }
335
Alexandre Rames67555f72014-11-18 10:55:16 +0000336 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000337 SaveLiveRegisters(codegen, locations);
Alexandre Rames67555f72014-11-18 10:55:16 +0000338
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000339 dex::TypeIndex type_index = cls_->GetTypeIndex();
340 __ Mov(calling_convention.GetRegisterAt(0).W(), type_index.index_);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000341 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
342 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000343 arm64_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800344 if (do_clinit_) {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100345 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800346 } else {
Vladimir Marko5ea536a2015-04-20 20:11:30 +0100347 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800348 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000349
350 // Move the class to the desired location.
Alexandre Rames67555f72014-11-18 10:55:16 +0000351 if (out.IsValid()) {
352 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000353 Primitive::Type type = instruction_->GetType();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000354 arm64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
Alexandre Rames67555f72014-11-18 10:55:16 +0000355 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000356 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000357 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000358 if (is_load_class_bss_entry) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000359 DCHECK(out.IsValid());
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000360 const DexFile& dex_file = cls_->GetDexFile();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000361 if (call_saves_everything_except_r0_ip0) {
362 // The class entry page address was preserved in bss_entry_temp_ thanks to kSaveEverything.
363 } else {
364 // For non-Baker read barrier, we need to re-calculate the address of the class entry page.
365 bss_entry_adrp_label_ = arm64_codegen->NewBssEntryTypePatch(dex_file, type_index);
366 arm64_codegen->EmitAdrpPlaceholder(bss_entry_adrp_label_, bss_entry_temp_);
367 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000368 vixl::aarch64::Label* strp_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +0000369 arm64_codegen->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label_);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000370 {
371 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
372 __ Bind(strp_label);
373 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Markoea4c1262017-02-06 19:59:33 +0000374 MemOperand(bss_entry_temp_, /* offset placeholder */ 0));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000375 }
376 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000377 __ B(GetExitLabel());
378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM64"; }
381
Alexandre Rames67555f72014-11-18 10:55:16 +0000382 private:
383 // The class this slow path will load.
384 HLoadClass* const cls_;
385
Alexandre Rames67555f72014-11-18 10:55:16 +0000386 // The dex PC of `at_`.
387 const uint32_t dex_pc_;
388
389 // Whether to initialize the class.
390 const bool do_clinit_;
391
Vladimir Markoea4c1262017-02-06 19:59:33 +0000392 // For HLoadClass/kBssEntry, the temp register and the label of the ADRP where it was loaded.
393 vixl::aarch64::Register bss_entry_temp_;
394 vixl::aarch64::Label* bss_entry_adrp_label_;
395
Alexandre Rames67555f72014-11-18 10:55:16 +0000396 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM64);
397};
398
Vladimir Markoaad75c62016-10-03 08:46:48 +0000399class LoadStringSlowPathARM64 : public SlowPathCodeARM64 {
400 public:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100401 LoadStringSlowPathARM64(HLoadString* instruction, Register temp, vixl::aarch64::Label* adrp_label)
402 : SlowPathCodeARM64(instruction),
403 temp_(temp),
404 adrp_label_(adrp_label) {}
Vladimir Markoaad75c62016-10-03 08:46:48 +0000405
406 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
407 LocationSummary* locations = instruction_->GetLocations();
408 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
409 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
410
Vladimir Markof4f2daa2017-03-20 18:26:59 +0000411 InvokeRuntimeCallingConvention calling_convention;
412 // Make sure `temp_` is not clobbered by the call or by saving/restoring registers.
413 DCHECK(temp_.IsValid());
414 DCHECK(!temp_.Is(calling_convention.GetRegisterAt(0)));
415 DCHECK(!UseScratchRegisterScope(arm64_codegen->GetVIXLAssembler()).IsAvailable(temp_));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100416
Vladimir Markoaad75c62016-10-03 08:46:48 +0000417 __ Bind(GetEntryLabel());
418 SaveLiveRegisters(codegen, locations);
419
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000420 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
421 __ Mov(calling_convention.GetRegisterAt(0).W(), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000422 arm64_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
423 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
424 Primitive::Type type = instruction_->GetType();
425 arm64_codegen->MoveLocation(locations->Out(), calling_convention.GetReturnLocation(type), type);
426
427 RestoreLiveRegisters(codegen, locations);
428
429 // Store the resolved String to the BSS entry.
Vladimir Markoaad75c62016-10-03 08:46:48 +0000430 const DexFile& dex_file = instruction_->AsLoadString()->GetDexFile();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100431 if (!kUseReadBarrier || kUseBakerReadBarrier) {
432 // The string entry page address was preserved in temp_ thanks to kSaveEverything.
433 } else {
434 // For non-Baker read barrier, we need to re-calculate the address of the string entry page.
435 adrp_label_ = arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index);
436 arm64_codegen->EmitAdrpPlaceholder(adrp_label_, temp_);
437 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000438 vixl::aarch64::Label* strp_label =
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100439 arm64_codegen->NewPcRelativeStringPatch(dex_file, string_index, adrp_label_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000440 {
441 SingleEmissionCheckScope guard(arm64_codegen->GetVIXLAssembler());
442 __ Bind(strp_label);
443 __ str(RegisterFrom(locations->Out(), Primitive::kPrimNot),
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100444 MemOperand(temp_, /* offset placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000445 }
446
447 __ B(GetExitLabel());
448 }
449
450 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM64"; }
451
452 private:
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100453 const Register temp_;
454 vixl::aarch64::Label* adrp_label_;
455
Vladimir Markoaad75c62016-10-03 08:46:48 +0000456 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM64);
457};
458
Alexandre Rames5319def2014-10-23 10:03:10 +0100459class NullCheckSlowPathARM64 : public SlowPathCodeARM64 {
460 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000461 explicit NullCheckSlowPathARM64(HNullCheck* instr) : SlowPathCodeARM64(instr) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100462
Alexandre Rames67555f72014-11-18 10:55:16 +0000463 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
464 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100465 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000466 if (instruction_->CanThrowIntoCatchBlock()) {
467 // Live registers will be restored in the catch block if caught.
468 SaveLiveRegisters(codegen, instruction_->GetLocations());
469 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000470 arm64_codegen->InvokeRuntime(kQuickThrowNullPointer,
471 instruction_,
472 instruction_->GetDexPc(),
473 this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800474 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Alexandre Rames5319def2014-10-23 10:03:10 +0100475 }
476
Alexandre Rames8158f282015-08-07 10:26:17 +0100477 bool IsFatal() const OVERRIDE { return true; }
478
Alexandre Rames9931f312015-06-19 14:47:01 +0100479 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM64"; }
480
Alexandre Rames5319def2014-10-23 10:03:10 +0100481 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100482 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM64);
483};
484
485class SuspendCheckSlowPathARM64 : public SlowPathCodeARM64 {
486 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100487 SuspendCheckSlowPathARM64(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000488 : SlowPathCodeARM64(instruction), successor_(successor) {}
Alexandre Rames5319def2014-10-23 10:03:10 +0100489
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Artem Serov7957d952017-04-04 15:44:09 +0100491 LocationSummary* locations = instruction_->GetLocations();
Alexandre Rames67555f72014-11-18 10:55:16 +0000492 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Alexandre Rames5319def2014-10-23 10:03:10 +0100493 __ Bind(GetEntryLabel());
Artem Serov7957d952017-04-04 15:44:09 +0100494 SaveLiveRegisters(codegen, locations); // Only saves live 128-bit regs for SIMD.
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000495 arm64_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800496 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Artem Serov7957d952017-04-04 15:44:09 +0100497 RestoreLiveRegisters(codegen, locations); // Only restores live 128-bit regs for SIMD.
Alexandre Rames67555f72014-11-18 10:55:16 +0000498 if (successor_ == nullptr) {
499 __ B(GetReturnLabel());
500 } else {
501 __ B(arm64_codegen->GetLabelOf(successor_));
502 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100503 }
504
Scott Wakeling97c72b72016-06-24 16:19:36 +0100505 vixl::aarch64::Label* GetReturnLabel() {
Alexandre Rames5319def2014-10-23 10:03:10 +0100506 DCHECK(successor_ == nullptr);
507 return &return_label_;
508 }
509
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100510 HBasicBlock* GetSuccessor() const {
511 return successor_;
512 }
513
Alexandre Rames9931f312015-06-19 14:47:01 +0100514 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM64"; }
515
Alexandre Rames5319def2014-10-23 10:03:10 +0100516 private:
Alexandre Rames5319def2014-10-23 10:03:10 +0100517 // If not null, the block to branch to after the suspend check.
518 HBasicBlock* const successor_;
519
520 // If `successor_` is null, the label to branch to after the suspend check.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100521 vixl::aarch64::Label return_label_;
Alexandre Rames5319def2014-10-23 10:03:10 +0100522
523 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM64);
524};
525
Alexandre Rames67555f72014-11-18 10:55:16 +0000526class TypeCheckSlowPathARM64 : public SlowPathCodeARM64 {
527 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000528 TypeCheckSlowPathARM64(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000529 : SlowPathCodeARM64(instruction), is_fatal_(is_fatal) {}
Alexandre Rames67555f72014-11-18 10:55:16 +0000530
531 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames3e69f162014-12-10 10:36:50 +0000532 LocationSummary* locations = instruction_->GetLocations();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800533
Alexandre Rames3e69f162014-12-10 10:36:50 +0000534 DCHECK(instruction_->IsCheckCast()
535 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
536 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100537 uint32_t dex_pc = instruction_->GetDexPc();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000538
Alexandre Rames67555f72014-11-18 10:55:16 +0000539 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000540
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541 if (!is_fatal_) {
542 SaveLiveRegisters(codegen, locations);
543 }
Alexandre Rames3e69f162014-12-10 10:36:50 +0000544
545 // We're moving two locations to locations that could overlap, so we need a parallel
546 // move resolver.
547 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800548 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800549 LocationFrom(calling_convention.GetRegisterAt(0)),
550 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800551 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800552 LocationFrom(calling_convention.GetRegisterAt(1)),
553 Primitive::kPrimNot);
Alexandre Rames3e69f162014-12-10 10:36:50 +0000554 if (instruction_->IsInstanceOf()) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000555 arm64_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800556 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000557 Primitive::Type ret_type = instruction_->GetType();
558 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
559 arm64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
560 } else {
561 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800562 arm64_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
563 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Alexandre Rames3e69f162014-12-10 10:36:50 +0000564 }
565
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000566 if (!is_fatal_) {
567 RestoreLiveRegisters(codegen, locations);
568 __ B(GetExitLabel());
569 }
Alexandre Rames67555f72014-11-18 10:55:16 +0000570 }
571
Alexandre Rames9931f312015-06-19 14:47:01 +0100572 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM64"; }
Roland Levillainf41f9562016-09-14 19:26:48 +0100573 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100574
Alexandre Rames67555f72014-11-18 10:55:16 +0000575 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000576 const bool is_fatal_;
Alexandre Rames3e69f162014-12-10 10:36:50 +0000577
Alexandre Rames67555f72014-11-18 10:55:16 +0000578 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM64);
579};
580
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700581class DeoptimizationSlowPathARM64 : public SlowPathCodeARM64 {
582 public:
Aart Bik42249c32016-01-07 15:33:50 -0800583 explicit DeoptimizationSlowPathARM64(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000584 : SlowPathCodeARM64(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700585
586 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800587 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700588 __ Bind(GetEntryLabel());
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000589 arm64_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000590 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700591 }
592
Alexandre Rames9931f312015-06-19 14:47:01 +0100593 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM64"; }
594
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700595 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700596 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM64);
597};
598
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100599class ArraySetSlowPathARM64 : public SlowPathCodeARM64 {
600 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000601 explicit ArraySetSlowPathARM64(HInstruction* instruction) : SlowPathCodeARM64(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100602
603 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
604 LocationSummary* locations = instruction_->GetLocations();
605 __ Bind(GetEntryLabel());
606 SaveLiveRegisters(codegen, locations);
607
608 InvokeRuntimeCallingConvention calling_convention;
609 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
610 parallel_move.AddMove(
611 locations->InAt(0),
612 LocationFrom(calling_convention.GetRegisterAt(0)),
613 Primitive::kPrimNot,
614 nullptr);
615 parallel_move.AddMove(
616 locations->InAt(1),
617 LocationFrom(calling_convention.GetRegisterAt(1)),
618 Primitive::kPrimInt,
619 nullptr);
620 parallel_move.AddMove(
621 locations->InAt(2),
622 LocationFrom(calling_convention.GetRegisterAt(2)),
623 Primitive::kPrimNot,
624 nullptr);
625 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
626
627 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
Serban Constantinescu22f81d32016-02-18 16:06:31 +0000628 arm64_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100629 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
630 RestoreLiveRegisters(codegen, locations);
631 __ B(GetExitLabel());
632 }
633
634 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM64"; }
635
636 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100637 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM64);
638};
639
Zheng Xu3927c8b2015-11-18 17:46:25 +0800640void JumpTableARM64::EmitTable(CodeGeneratorARM64* codegen) {
641 uint32_t num_entries = switch_instr_->GetNumEntries();
Vladimir Markof3e0ee22015-12-17 15:23:13 +0000642 DCHECK_GE(num_entries, kPackedSwitchCompareJumpThreshold);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800643
644 // We are about to use the assembler to place literals directly. Make sure we have enough
645 // underlying code buffer and we have generated the jump table with right size.
Artem Serov914d7a82017-02-07 14:33:49 +0000646 EmissionCheckScope scope(codegen->GetVIXLAssembler(),
647 num_entries * sizeof(int32_t),
648 CodeBufferCheckScope::kExactSize);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800649
650 __ Bind(&table_start_);
651 const ArenaVector<HBasicBlock*>& successors = switch_instr_->GetBlock()->GetSuccessors();
652 for (uint32_t i = 0; i < num_entries; i++) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100653 vixl::aarch64::Label* target_label = codegen->GetLabelOf(successors[i]);
Zheng Xu3927c8b2015-11-18 17:46:25 +0800654 DCHECK(target_label->IsBound());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100655 ptrdiff_t jump_offset = target_label->GetLocation() - table_start_.GetLocation();
Zheng Xu3927c8b2015-11-18 17:46:25 +0800656 DCHECK_GT(jump_offset, std::numeric_limits<int32_t>::min());
657 DCHECK_LE(jump_offset, std::numeric_limits<int32_t>::max());
658 Literal<int32_t> literal(jump_offset);
659 __ place(&literal);
660 }
661}
662
Roland Levillain54f869e2017-03-06 13:54:11 +0000663// Abstract base class for read barrier slow paths marking a reference
664// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000665//
Roland Levillain54f869e2017-03-06 13:54:11 +0000666// Argument `entrypoint` must be a register location holding the read
667// barrier marking runtime entry point to be invoked.
668class ReadBarrierMarkSlowPathBaseARM64 : public SlowPathCodeARM64 {
669 protected:
670 ReadBarrierMarkSlowPathBaseARM64(HInstruction* instruction, Location ref, Location entrypoint)
671 : SlowPathCodeARM64(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000672 DCHECK(kEmitCompilerReadBarrier);
673 }
674
Roland Levillain54f869e2017-03-06 13:54:11 +0000675 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM64"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000676
Roland Levillain54f869e2017-03-06 13:54:11 +0000677 // Generate assembly code calling the read barrier marking runtime
678 // entry point (ReadBarrierMarkRegX).
679 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000680 // No need to save live registers; it's taken care of by the
681 // entrypoint. Also, there is no need to update the stack mask,
682 // as this runtime call will not trigger a garbage collection.
683 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
684 DCHECK_NE(ref_.reg(), LR);
685 DCHECK_NE(ref_.reg(), WSP);
686 DCHECK_NE(ref_.reg(), WZR);
687 // IP0 is used internally by the ReadBarrierMarkRegX entry point
688 // as a temporary, it cannot be the entry point's input/output.
689 DCHECK_NE(ref_.reg(), IP0);
690 DCHECK(0 <= ref_.reg() && ref_.reg() < kNumberOfWRegisters) << ref_.reg();
691 // "Compact" slow path, saving two moves.
692 //
693 // Instead of using the standard runtime calling convention (input
694 // and output in W0):
695 //
696 // W0 <- ref
697 // W0 <- ReadBarrierMark(W0)
698 // ref <- W0
699 //
700 // we just use rX (the register containing `ref`) as input and output
701 // of a dedicated entrypoint:
702 //
703 // rX <- ReadBarrierMarkRegX(rX)
704 //
705 if (entrypoint_.IsValid()) {
706 arm64_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
707 __ Blr(XRegisterFrom(entrypoint_));
708 } else {
709 // Entrypoint is not already loaded, load from the thread.
710 int32_t entry_point_offset =
711 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref_.reg());
712 // This runtime call does not require a stack map.
713 arm64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
714 }
715 }
716
717 // The location (register) of the marked object reference.
718 const Location ref_;
719
720 // The location of the entrypoint if it is already loaded.
721 const Location entrypoint_;
722
Roland Levillain54f869e2017-03-06 13:54:11 +0000723 private:
724 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM64);
725};
726
Alexandre Rames5319def2014-10-23 10:03:10 +0100727// Slow path marking an object reference `ref` during a read
728// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000729// reference does not get updated by this slow path after marking.
Alexandre Rames5319def2014-10-23 10:03:10 +0100730//
731// This means that after the execution of this slow path, `ref` will
732// always be up-to-date, but `obj.field` may not; i.e., after the
733// flip, `ref` will be a to-space reference, but `obj.field` will
734// probably still be a from-space reference (unless it gets updated by
735// another thread, or if another thread installed another object
736// reference (different from `ref`) in `obj.field`).
737//
738// If `entrypoint` is a valid location it is assumed to already be
739// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000740// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000741class ReadBarrierMarkSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
Alexandre Rames5319def2014-10-23 10:03:10 +0100742 public:
743 ReadBarrierMarkSlowPathARM64(HInstruction* instruction,
744 Location ref,
745 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000746 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100747 DCHECK(kEmitCompilerReadBarrier);
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 }
749
750 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM64"; }
751
752 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames542361f2015-01-29 16:57:31 +0000753 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100754 DCHECK(locations->CanCall());
755 DCHECK(ref_.IsRegister()) << ref_;
Alexandre Rames542361f2015-01-29 16:57:31 +0000756 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000757 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
758 << "Unexpected instruction in read barrier marking slow path: "
759 << instruction_->DebugName();
760
761 __ Bind(GetEntryLabel());
762 GenerateReadBarrierMarkRuntimeCall(codegen);
763 __ B(GetExitLabel());
764 }
765
766 private:
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000767 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM64);
768};
769
Roland Levillain54f869e2017-03-06 13:54:11 +0000770// Slow path loading `obj`'s lock word, loading a reference from
771// object `*(obj + offset + (index << scale_factor))` into `ref`, and
772// marking `ref` if `obj` is gray according to the lock word (Baker
773// read barrier). The field `obj.field` in the object `obj` holding
774// this reference does not get updated by this slow path after marking
775// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
776// below for that).
777//
778// This means that after the execution of this slow path, `ref` will
779// always be up-to-date, but `obj.field` may not; i.e., after the
780// flip, `ref` will be a to-space reference, but `obj.field` will
781// probably still be a from-space reference (unless it gets updated by
782// another thread, or if another thread installed another object
783// reference (different from `ref`) in `obj.field`).
784//
785// Argument `entrypoint` must be a register location holding the read
786// barrier marking runtime entry point to be invoked.
787class LoadReferenceWithBakerReadBarrierSlowPathARM64 : public ReadBarrierMarkSlowPathBaseARM64 {
788 public:
789 LoadReferenceWithBakerReadBarrierSlowPathARM64(HInstruction* instruction,
790 Location ref,
791 Register obj,
792 uint32_t offset,
793 Location index,
794 size_t scale_factor,
795 bool needs_null_check,
796 bool use_load_acquire,
797 Register temp,
798 Location entrypoint)
799 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
800 obj_(obj),
801 offset_(offset),
802 index_(index),
803 scale_factor_(scale_factor),
804 needs_null_check_(needs_null_check),
805 use_load_acquire_(use_load_acquire),
806 temp_(temp) {
807 DCHECK(kEmitCompilerReadBarrier);
808 DCHECK(kUseBakerReadBarrier);
809 }
810
811 const char* GetDescription() const OVERRIDE {
812 return "LoadReferenceWithBakerReadBarrierSlowPathARM64";
813 }
814
815 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
816 LocationSummary* locations = instruction_->GetLocations();
817 DCHECK(locations->CanCall());
818 DCHECK(ref_.IsRegister()) << ref_;
819 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
820 DCHECK(obj_.IsW());
821 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
Alexandre Rames5319def2014-10-23 10:03:10 +0100822 DCHECK(instruction_->IsInstanceFieldGet() ||
823 instruction_->IsStaticFieldGet() ||
824 instruction_->IsArrayGet() ||
825 instruction_->IsArraySet() ||
Alexandre Rames5319def2014-10-23 10:03:10 +0100826 instruction_->IsInstanceOf() ||
827 instruction_->IsCheckCast() ||
828 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
829 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
830 << "Unexpected instruction in read barrier marking slow path: "
831 << instruction_->DebugName();
832 // The read barrier instrumentation of object ArrayGet
833 // instructions does not support the HIntermediateAddress
Alexandre Ramesa89086e2014-11-07 17:13:25 +0000834 // instruction.
835 DCHECK(!(instruction_->IsArrayGet() &&
Alexandre Rames542361f2015-01-29 16:57:31 +0000836 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
837
Roland Levillain54f869e2017-03-06 13:54:11 +0000838 // Temporary register `temp_`, used to store the lock word, must
839 // not be IP0 nor IP1, as we may use them to emit the reference
840 // load (in the call to GenerateRawReferenceLoad below), and we
841 // need the lock word to still be in `temp_` after the reference
842 // load.
843 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
844 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
845
Alexandre Rames5319def2014-10-23 10:03:10 +0100846 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000847
848 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
849 // inserted after the original load. However, in fast path based
850 // Baker's read barriers, we need to perform the load of
851 // mirror::Object::monitor_ *before* the original reference load.
852 // This load-load ordering is required by the read barrier.
853 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100854 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000855 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
856 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
857 // HeapReference<mirror::Object> ref = *src; // Original reference load.
858 // bool is_gray = (rb_state == ReadBarrier::GrayState());
859 // if (is_gray) {
860 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
861 // }
Roland Levillaind966ce72017-02-09 16:20:14 +0000862 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000863 // Note: the original implementation in ReadBarrier::Barrier is
864 // slightly more complex as it performs additional checks that we do
865 // not do here for performance reasons.
866
867 // /* int32_t */ monitor = obj->monitor_
868 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
869 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
870 if (needs_null_check_) {
871 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100872 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000873 // /* LockWord */ lock_word = LockWord(monitor)
874 static_assert(sizeof(LockWord) == sizeof(int32_t),
875 "art::LockWord and int32_t have different sizes.");
876
877 // Introduce a dependency on the lock_word including rb_state,
878 // to prevent load-load reordering, and without using
879 // a memory barrier (which would be more expensive).
880 // `obj` is unchanged by this operation, but its value now depends
881 // on `temp`.
882 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
883
884 // The actual reference load.
885 // A possible implicit null check has already been handled above.
886 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
887 arm64_codegen->GenerateRawReferenceLoad(instruction_,
888 ref_,
889 obj_,
890 offset_,
891 index_,
892 scale_factor_,
893 /* needs_null_check */ false,
894 use_load_acquire_);
895
896 // Mark the object `ref` when `obj` is gray.
897 //
898 // if (rb_state == ReadBarrier::GrayState())
899 // ref = ReadBarrier::Mark(ref);
900 //
901 // Given the numeric representation, it's enough to check the low bit of the rb_state.
902 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
903 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
904 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
905 GenerateReadBarrierMarkRuntimeCall(codegen);
906
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000907 __ B(GetExitLabel());
908 }
909
910 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000911 // The register containing the object holding the marked object reference field.
912 Register obj_;
913 // The offset, index and scale factor to access the reference in `obj_`.
914 uint32_t offset_;
915 Location index_;
916 size_t scale_factor_;
917 // Is a null check required?
918 bool needs_null_check_;
919 // Should this reference load use Load-Acquire semantics?
920 bool use_load_acquire_;
921 // A temporary register used to hold the lock word of `obj_`.
922 Register temp_;
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000923
Roland Levillain54f869e2017-03-06 13:54:11 +0000924 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM64);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000925};
926
Roland Levillain54f869e2017-03-06 13:54:11 +0000927// Slow path loading `obj`'s lock word, loading a reference from
928// object `*(obj + offset + (index << scale_factor))` into `ref`, and
929// marking `ref` if `obj` is gray according to the lock word (Baker
930// read barrier). If needed, this slow path also atomically updates
931// the field `obj.field` in the object `obj` holding this reference
932// after marking (contrary to
933// LoadReferenceWithBakerReadBarrierSlowPathARM64 above, which never
934// tries to update `obj.field`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100935//
936// This means that after the execution of this slow path, both `ref`
937// and `obj.field` will be up-to-date; i.e., after the flip, both will
938// hold the same to-space reference (unless another thread installed
939// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000940//
Roland Levillain54f869e2017-03-06 13:54:11 +0000941// Argument `entrypoint` must be a register location holding the read
942// barrier marking runtime entry point to be invoked.
943class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
944 : public ReadBarrierMarkSlowPathBaseARM64 {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100945 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000946 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(HInstruction* instruction,
947 Location ref,
948 Register obj,
949 uint32_t offset,
950 Location index,
951 size_t scale_factor,
952 bool needs_null_check,
953 bool use_load_acquire,
954 Register temp,
955 Location entrypoint)
956 : ReadBarrierMarkSlowPathBaseARM64(instruction, ref, entrypoint),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100957 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000958 offset_(offset),
959 index_(index),
960 scale_factor_(scale_factor),
961 needs_null_check_(needs_null_check),
962 use_load_acquire_(use_load_acquire),
Roland Levillain35345a52017-02-27 14:32:08 +0000963 temp_(temp) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100964 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000965 DCHECK(kUseBakerReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100966 }
967
968 const char* GetDescription() const OVERRIDE {
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64";
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100970 }
971
972 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
973 LocationSummary* locations = instruction_->GetLocations();
974 Register ref_reg = WRegisterFrom(ref_);
975 DCHECK(locations->CanCall());
976 DCHECK(ref_.IsRegister()) << ref_;
977 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_.reg())) << ref_.reg();
Roland Levillain54f869e2017-03-06 13:54:11 +0000978 DCHECK(obj_.IsW());
979 DCHECK_NE(ref_.reg(), LocationFrom(temp_).reg());
980
981 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100982 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
983 << "Unexpected instruction in read barrier marking and field updating slow path: "
984 << instruction_->DebugName();
985 DCHECK(instruction_->GetLocations()->Intrinsified());
986 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000987 DCHECK_EQ(offset_, 0u);
988 DCHECK_EQ(scale_factor_, 0u);
989 DCHECK_EQ(use_load_acquire_, false);
990 // The location of the offset of the marked reference field within `obj_`.
991 Location field_offset = index_;
992 DCHECK(field_offset.IsRegister()) << field_offset;
993
994 // Temporary register `temp_`, used to store the lock word, must
995 // not be IP0 nor IP1, as we may use them to emit the reference
996 // load (in the call to GenerateRawReferenceLoad below), and we
997 // need the lock word to still be in `temp_` after the reference
998 // load.
999 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1000 DCHECK_NE(LocationFrom(temp_).reg(), IP1);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001001
1002 __ Bind(GetEntryLabel());
1003
Roland Levillain54f869e2017-03-06 13:54:11 +00001004 // /* int32_t */ monitor = obj->monitor_
1005 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1006 __ Ldr(temp_, HeapOperand(obj_, monitor_offset));
1007 if (needs_null_check_) {
1008 codegen->MaybeRecordImplicitNullCheck(instruction_);
1009 }
1010 // /* LockWord */ lock_word = LockWord(monitor)
1011 static_assert(sizeof(LockWord) == sizeof(int32_t),
1012 "art::LockWord and int32_t have different sizes.");
1013
1014 // Introduce a dependency on the lock_word including rb_state,
1015 // to prevent load-load reordering, and without using
1016 // a memory barrier (which would be more expensive).
1017 // `obj` is unchanged by this operation, but its value now depends
1018 // on `temp`.
1019 __ Add(obj_.X(), obj_.X(), Operand(temp_.X(), LSR, 32));
1020
1021 // The actual reference load.
1022 // A possible implicit null check has already been handled above.
1023 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1024 arm64_codegen->GenerateRawReferenceLoad(instruction_,
1025 ref_,
1026 obj_,
1027 offset_,
1028 index_,
1029 scale_factor_,
1030 /* needs_null_check */ false,
1031 use_load_acquire_);
1032
1033 // Mark the object `ref` when `obj` is gray.
1034 //
1035 // if (rb_state == ReadBarrier::GrayState())
1036 // ref = ReadBarrier::Mark(ref);
1037 //
1038 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1039 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1040 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1041 __ Tbz(temp_, LockWord::kReadBarrierStateShift, GetExitLabel());
1042
1043 // Save the old value of the reference before marking it.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001044 // Note that we cannot use IP to save the old reference, as IP is
1045 // used internally by the ReadBarrierMarkRegX entry point, and we
1046 // need the old reference after the call to that entry point.
1047 DCHECK_NE(LocationFrom(temp_).reg(), IP0);
1048 __ Mov(temp_.W(), ref_reg);
1049
Roland Levillain54f869e2017-03-06 13:54:11 +00001050 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001051
1052 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001053 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001054 //
1055 // Note that this field could also hold a different object, if
1056 // another thread had concurrently changed it. In that case, the
1057 // LDXR/CMP/BNE sequence of instructions in the compare-and-set
1058 // (CAS) operation below would abort the CAS, leaving the field
1059 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001060 __ Cmp(temp_.W(), ref_reg);
Roland Levillain54f869e2017-03-06 13:54:11 +00001061 __ B(eq, GetExitLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001062
1063 // Update the the holder's field atomically. This may fail if
1064 // mutator updates before us, but it's OK. This is achieved
1065 // using a strong compare-and-set (CAS) operation with relaxed
1066 // memory synchronization ordering, where the expected value is
1067 // the old reference and the desired value is the new reference.
1068
1069 MacroAssembler* masm = arm64_codegen->GetVIXLAssembler();
1070 UseScratchRegisterScope temps(masm);
1071
1072 // Convenience aliases.
1073 Register base = obj_.W();
Roland Levillain54f869e2017-03-06 13:54:11 +00001074 Register offset = XRegisterFrom(field_offset);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001075 Register expected = temp_.W();
1076 Register value = ref_reg;
1077 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1078 Register tmp_value = temps.AcquireW(); // Value in memory.
1079
1080 __ Add(tmp_ptr, base.X(), Operand(offset));
1081
1082 if (kPoisonHeapReferences) {
1083 arm64_codegen->GetAssembler()->PoisonHeapReference(expected);
1084 if (value.Is(expected)) {
1085 // Do not poison `value`, as it is the same register as
1086 // `expected`, which has just been poisoned.
1087 } else {
1088 arm64_codegen->GetAssembler()->PoisonHeapReference(value);
1089 }
1090 }
1091
1092 // do {
1093 // tmp_value = [tmp_ptr] - expected;
1094 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1095
Roland Levillain24a4d112016-10-26 13:10:46 +01001096 vixl::aarch64::Label loop_head, comparison_failed, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001097 __ Bind(&loop_head);
1098 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
1099 __ Cmp(tmp_value, expected);
Roland Levillain24a4d112016-10-26 13:10:46 +01001100 __ B(&comparison_failed, ne);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001101 __ Stxr(tmp_value, value, MemOperand(tmp_ptr));
1102 __ Cbnz(tmp_value, &loop_head);
Roland Levillain24a4d112016-10-26 13:10:46 +01001103 __ B(&exit_loop);
1104 __ Bind(&comparison_failed);
1105 __ Clrex();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001106 __ Bind(&exit_loop);
1107
1108 if (kPoisonHeapReferences) {
1109 arm64_codegen->GetAssembler()->UnpoisonHeapReference(expected);
1110 if (value.Is(expected)) {
1111 // Do not unpoison `value`, as it is the same register as
1112 // `expected`, which has just been unpoisoned.
1113 } else {
1114 arm64_codegen->GetAssembler()->UnpoisonHeapReference(value);
1115 }
1116 }
1117
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001118 __ B(GetExitLabel());
1119 }
1120
1121 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001122 // The register containing the object holding the marked object reference field.
1123 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001124 // The offset, index and scale factor to access the reference in `obj_`.
1125 uint32_t offset_;
1126 Location index_;
1127 size_t scale_factor_;
1128 // Is a null check required?
1129 bool needs_null_check_;
1130 // Should this reference load use Load-Acquire semantics?
1131 bool use_load_acquire_;
1132 // A temporary register used to hold the lock word of `obj_`; and
1133 // also to hold the original reference value, when the reference is
1134 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001135 const Register temp_;
1136
Roland Levillain54f869e2017-03-06 13:54:11 +00001137 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001138};
1139
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001140// Slow path generating a read barrier for a heap reference.
1141class ReadBarrierForHeapReferenceSlowPathARM64 : public SlowPathCodeARM64 {
1142 public:
1143 ReadBarrierForHeapReferenceSlowPathARM64(HInstruction* instruction,
1144 Location out,
1145 Location ref,
1146 Location obj,
1147 uint32_t offset,
1148 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +00001149 : SlowPathCodeARM64(instruction),
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001150 out_(out),
1151 ref_(ref),
1152 obj_(obj),
1153 offset_(offset),
1154 index_(index) {
1155 DCHECK(kEmitCompilerReadBarrier);
1156 // If `obj` is equal to `out` or `ref`, it means the initial object
1157 // has been overwritten by (or after) the heap object reference load
1158 // to be instrumented, e.g.:
1159 //
1160 // __ Ldr(out, HeapOperand(out, class_offset);
Roland Levillain44015862016-01-22 11:47:17 +00001161 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001162 //
1163 // In that case, we have lost the information about the original
1164 // object, and the emitted read barrier cannot work properly.
1165 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1166 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1167 }
1168
1169 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1170 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1171 LocationSummary* locations = instruction_->GetLocations();
1172 Primitive::Type type = Primitive::kPrimNot;
1173 DCHECK(locations->CanCall());
1174 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain3d312422016-06-23 13:53:42 +01001175 DCHECK(instruction_->IsInstanceFieldGet() ||
1176 instruction_->IsStaticFieldGet() ||
1177 instruction_->IsArrayGet() ||
1178 instruction_->IsInstanceOf() ||
1179 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001180 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain44015862016-01-22 11:47:17 +00001181 << "Unexpected instruction in read barrier for heap reference slow path: "
1182 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001183 // The read barrier instrumentation of object ArrayGet
1184 // instructions does not support the HIntermediateAddress
1185 // instruction.
Roland Levillaincd3d0fb2016-01-15 19:26:48 +00001186 DCHECK(!(instruction_->IsArrayGet() &&
Artem Serov328429f2016-07-06 16:23:04 +01001187 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001188
1189 __ Bind(GetEntryLabel());
1190
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001191 SaveLiveRegisters(codegen, locations);
1192
1193 // We may have to change the index's value, but as `index_` is a
1194 // constant member (like other "inputs" of this slow path),
1195 // introduce a copy of it, `index`.
1196 Location index = index_;
1197 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001198 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001199 if (instruction_->IsArrayGet()) {
1200 // Compute the actual memory offset and store it in `index`.
1201 Register index_reg = RegisterFrom(index_, Primitive::kPrimInt);
1202 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_.reg()));
1203 if (codegen->IsCoreCalleeSaveRegister(index_.reg())) {
1204 // We are about to change the value of `index_reg` (see the
1205 // calls to vixl::MacroAssembler::Lsl and
1206 // vixl::MacroAssembler::Mov below), but it has
1207 // not been saved by the previous call to
1208 // art::SlowPathCode::SaveLiveRegisters, as it is a
1209 // callee-save register --
1210 // art::SlowPathCode::SaveLiveRegisters does not consider
1211 // callee-save registers, as it has been designed with the
1212 // assumption that callee-save registers are supposed to be
1213 // handled by the called function. So, as a callee-save
1214 // register, `index_reg` _would_ eventually be saved onto
1215 // the stack, but it would be too late: we would have
1216 // changed its value earlier. Therefore, we manually save
1217 // it here into another freely available register,
1218 // `free_reg`, chosen of course among the caller-save
1219 // registers (as a callee-save `free_reg` register would
1220 // exhibit the same problem).
1221 //
1222 // Note we could have requested a temporary register from
1223 // the register allocator instead; but we prefer not to, as
1224 // this is a slow path, and we know we can find a
1225 // caller-save register that is available.
1226 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1227 __ Mov(free_reg.W(), index_reg);
1228 index_reg = free_reg;
1229 index = LocationFrom(index_reg);
1230 } else {
1231 // The initial register stored in `index_` has already been
1232 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1233 // (as it is not a callee-save register), so we can freely
1234 // use it.
1235 }
1236 // Shifting the index value contained in `index_reg` by the scale
1237 // factor (2) cannot overflow in practice, as the runtime is
1238 // unable to allocate object arrays with a size larger than
1239 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1240 __ Lsl(index_reg, index_reg, Primitive::ComponentSizeShift(type));
1241 static_assert(
1242 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1243 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1244 __ Add(index_reg, index_reg, Operand(offset_));
1245 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001246 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1247 // intrinsics, `index_` is not shifted by a scale factor of 2
1248 // (as in the case of ArrayGet), as it is actually an offset
1249 // to an object field within an object.
1250 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001251 DCHECK(instruction_->GetLocations()->Intrinsified());
1252 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1253 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1254 << instruction_->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001255 DCHECK_EQ(offset_, 0u);
Roland Levillaina7426c62016-08-03 15:02:10 +01001256 DCHECK(index_.IsRegister());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001257 }
1258 }
1259
1260 // We're moving two or three locations to locations that could
1261 // overlap, so we need a parallel move resolver.
1262 InvokeRuntimeCallingConvention calling_convention;
1263 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1264 parallel_move.AddMove(ref_,
1265 LocationFrom(calling_convention.GetRegisterAt(0)),
1266 type,
1267 nullptr);
1268 parallel_move.AddMove(obj_,
1269 LocationFrom(calling_convention.GetRegisterAt(1)),
1270 type,
1271 nullptr);
1272 if (index.IsValid()) {
1273 parallel_move.AddMove(index,
1274 LocationFrom(calling_convention.GetRegisterAt(2)),
1275 Primitive::kPrimInt,
1276 nullptr);
1277 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1278 } else {
1279 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1280 arm64_codegen->MoveConstant(LocationFrom(calling_convention.GetRegisterAt(2)), offset_);
1281 }
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001282 arm64_codegen->InvokeRuntime(kQuickReadBarrierSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001283 instruction_,
1284 instruction_->GetDexPc(),
1285 this);
1286 CheckEntrypointTypes<
1287 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1288 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1289
1290 RestoreLiveRegisters(codegen, locations);
1291
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001292 __ B(GetExitLabel());
1293 }
1294
1295 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM64"; }
1296
1297 private:
1298 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001299 size_t ref = static_cast<int>(XRegisterFrom(ref_).GetCode());
1300 size_t obj = static_cast<int>(XRegisterFrom(obj_).GetCode());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001301 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1302 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1303 return Register(VIXLRegCodeFromART(i), kXRegSize);
1304 }
1305 }
1306 // We shall never fail to find a free caller-save register, as
1307 // there are more than two core caller-save registers on ARM64
1308 // (meaning it is possible to find one which is different from
1309 // `ref` and `obj`).
1310 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1311 LOG(FATAL) << "Could not find a free register";
1312 UNREACHABLE();
1313 }
1314
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001315 const Location out_;
1316 const Location ref_;
1317 const Location obj_;
1318 const uint32_t offset_;
1319 // An additional location containing an index to an array.
1320 // Only used for HArrayGet and the UnsafeGetObject &
1321 // UnsafeGetObjectVolatile intrinsics.
1322 const Location index_;
1323
1324 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM64);
1325};
1326
1327// Slow path generating a read barrier for a GC root.
1328class ReadBarrierForRootSlowPathARM64 : public SlowPathCodeARM64 {
1329 public:
1330 ReadBarrierForRootSlowPathARM64(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +00001331 : SlowPathCodeARM64(instruction), out_(out), root_(root) {
Roland Levillain44015862016-01-22 11:47:17 +00001332 DCHECK(kEmitCompilerReadBarrier);
1333 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001334
1335 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1336 LocationSummary* locations = instruction_->GetLocations();
1337 Primitive::Type type = Primitive::kPrimNot;
1338 DCHECK(locations->CanCall());
1339 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
Roland Levillain44015862016-01-22 11:47:17 +00001340 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1341 << "Unexpected instruction in read barrier for GC root slow path: "
1342 << instruction_->DebugName();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001343
1344 __ Bind(GetEntryLabel());
1345 SaveLiveRegisters(codegen, locations);
1346
1347 InvokeRuntimeCallingConvention calling_convention;
1348 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1349 // The argument of the ReadBarrierForRootSlow is not a managed
1350 // reference (`mirror::Object*`), but a `GcRoot<mirror::Object>*`;
1351 // thus we need a 64-bit move here, and we cannot use
1352 //
1353 // arm64_codegen->MoveLocation(
1354 // LocationFrom(calling_convention.GetRegisterAt(0)),
1355 // root_,
1356 // type);
1357 //
1358 // which would emit a 32-bit move, as `type` is a (32-bit wide)
1359 // reference type (`Primitive::kPrimNot`).
1360 __ Mov(calling_convention.GetRegisterAt(0), XRegisterFrom(out_));
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001361 arm64_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001362 instruction_,
1363 instruction_->GetDexPc(),
1364 this);
1365 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1366 arm64_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(type), type);
1367
1368 RestoreLiveRegisters(codegen, locations);
1369 __ B(GetExitLabel());
1370 }
1371
1372 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM64"; }
1373
1374 private:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00001375 const Location out_;
1376 const Location root_;
1377
1378 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM64);
1379};
1380
Alexandre Rames5319def2014-10-23 10:03:10 +01001381#undef __
1382
1383Location InvokeDexCallingConventionVisitorARM64::GetNextLocation(Primitive::Type type) {
1384 Location next_location;
1385 if (type == Primitive::kPrimVoid) {
1386 LOG(FATAL) << "Unreachable type " << type;
1387 }
1388
1389 if (Primitive::IsFloatingPointType(type) &&
1390 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001391 next_location = LocationFrom(calling_convention.GetFpuRegisterAt(float_index_++));
1392 } else if (!Primitive::IsFloatingPointType(type) &&
1393 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
1394 next_location = LocationFrom(calling_convention.GetRegisterAt(gp_index_++));
1395 } else {
1396 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Alexandre Rames542361f2015-01-29 16:57:31 +00001397 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
1398 : Location::StackSlot(stack_offset);
Alexandre Rames5319def2014-10-23 10:03:10 +01001399 }
1400
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001401 // Space on the stack is reserved for all arguments.
Alexandre Rames542361f2015-01-29 16:57:31 +00001402 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
Alexandre Rames5319def2014-10-23 10:03:10 +01001403 return next_location;
1404}
1405
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001406Location InvokeDexCallingConventionVisitorARM64::GetMethodLocation() const {
Nicolas Geoffray38207af2015-06-01 15:46:22 +01001407 return LocationFrom(kArtMethodRegister);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001408}
1409
Serban Constantinescu579885a2015-02-22 20:51:33 +00001410CodeGeneratorARM64::CodeGeneratorARM64(HGraph* graph,
1411 const Arm64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001412 const CompilerOptions& compiler_options,
1413 OptimizingCompilerStats* stats)
Alexandre Rames5319def2014-10-23 10:03:10 +01001414 : CodeGenerator(graph,
1415 kNumberOfAllocatableRegisters,
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001416 kNumberOfAllocatableFPRegisters,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001417 kNumberOfAllocatableRegisterPairs,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001418 callee_saved_core_registers.GetList(),
1419 callee_saved_fp_registers.GetList(),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001420 compiler_options,
1421 stats),
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001422 block_labels_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Zheng Xu3927c8b2015-11-18 17:46:25 +08001423 jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexandre Rames5319def2014-10-23 10:03:10 +01001424 location_builder_(graph, this),
Alexandre Rames3e69f162014-12-10 10:36:50 +00001425 instruction_visitor_(graph, this),
Serban Constantinescu579885a2015-02-22 20:51:33 +00001426 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001427 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001428 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001429 uint32_literals_(std::less<uint32_t>(),
1430 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001431 uint64_literals_(std::less<uint64_t>(),
1432 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001433 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1434 boot_image_string_patches_(StringReferenceValueComparator(),
1435 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1436 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001437 boot_image_type_patches_(TypeReferenceValueComparator(),
1438 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1439 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001440 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markof4f2daa2017-03-20 18:26:59 +00001441 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001442 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1444 jit_class_patches_(TypeReferenceValueComparator(),
1445 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001446 // Save the link register (containing the return address) to mimic Quick.
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001447 AddAllocatedRegister(LocationFrom(lr));
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001448}
Alexandre Rames5319def2014-10-23 10:03:10 +01001449
Alexandre Rames67555f72014-11-18 10:55:16 +00001450#define __ GetVIXLAssembler()->
Alexandre Rames5319def2014-10-23 10:03:10 +01001451
Zheng Xu3927c8b2015-11-18 17:46:25 +08001452void CodeGeneratorARM64::EmitJumpTables() {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01001453 for (auto&& jump_table : jump_tables_) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001454 jump_table->EmitTable(this);
1455 }
1456}
1457
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001458void CodeGeneratorARM64::Finalize(CodeAllocator* allocator) {
Zheng Xu3927c8b2015-11-18 17:46:25 +08001459 EmitJumpTables();
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001460 // Ensure we emit the literal pool.
1461 __ FinalizeCode();
Vladimir Marko58155012015-08-19 12:49:41 +00001462
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +00001463 CodeGenerator::Finalize(allocator);
1464}
1465
Zheng Xuad4450e2015-04-17 18:48:56 +08001466void ParallelMoveResolverARM64::PrepareForEmitNativeCode() {
1467 // Note: There are 6 kinds of moves:
1468 // 1. constant -> GPR/FPR (non-cycle)
1469 // 2. constant -> stack (non-cycle)
1470 // 3. GPR/FPR -> GPR/FPR
1471 // 4. GPR/FPR -> stack
1472 // 5. stack -> GPR/FPR
1473 // 6. stack -> stack (non-cycle)
1474 // Case 1, 2 and 6 should never be included in a dependency cycle on ARM64. For case 3, 4, and 5
1475 // VIXL uses at most 1 GPR. VIXL has 2 GPR and 1 FPR temps, and there should be no intersecting
1476 // cycles on ARM64, so we always have 1 GPR and 1 FPR available VIXL temps to resolve the
1477 // dependency.
1478 vixl_temps_.Open(GetVIXLAssembler());
1479}
1480
1481void ParallelMoveResolverARM64::FinishEmitNativeCode() {
1482 vixl_temps_.Close();
1483}
1484
1485Location ParallelMoveResolverARM64::AllocateScratchLocationFor(Location::Kind kind) {
Artem Serovd4bccf12017-04-03 18:47:32 +01001486 DCHECK(kind == Location::kRegister || kind == Location::kFpuRegister
1487 || kind == Location::kStackSlot || kind == Location::kDoubleStackSlot
1488 || kind == Location::kSIMDStackSlot);
1489 kind = (kind == Location::kFpuRegister || kind == Location::kSIMDStackSlot)
1490 ? Location::kFpuRegister
1491 : Location::kRegister;
Zheng Xuad4450e2015-04-17 18:48:56 +08001492 Location scratch = GetScratchLocation(kind);
1493 if (!scratch.Equals(Location::NoLocation())) {
1494 return scratch;
1495 }
1496 // Allocate from VIXL temp registers.
1497 if (kind == Location::kRegister) {
1498 scratch = LocationFrom(vixl_temps_.AcquireX());
1499 } else {
1500 DCHECK(kind == Location::kFpuRegister);
Artem Serovd4bccf12017-04-03 18:47:32 +01001501 scratch = LocationFrom(codegen_->GetGraph()->HasSIMD()
1502 ? vixl_temps_.AcquireVRegisterOfSize(kQRegSize)
1503 : vixl_temps_.AcquireD());
Zheng Xuad4450e2015-04-17 18:48:56 +08001504 }
1505 AddScratchLocation(scratch);
1506 return scratch;
1507}
1508
1509void ParallelMoveResolverARM64::FreeScratchLocation(Location loc) {
1510 if (loc.IsRegister()) {
1511 vixl_temps_.Release(XRegisterFrom(loc));
1512 } else {
1513 DCHECK(loc.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001514 vixl_temps_.Release(codegen_->GetGraph()->HasSIMD() ? QRegisterFrom(loc) : DRegisterFrom(loc));
Zheng Xuad4450e2015-04-17 18:48:56 +08001515 }
1516 RemoveScratchLocation(loc);
1517}
1518
Alexandre Rames3e69f162014-12-10 10:36:50 +00001519void ParallelMoveResolverARM64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01001520 MoveOperands* move = moves_[index];
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001522}
1523
Alexandre Rames5319def2014-10-23 10:03:10 +01001524void CodeGeneratorARM64::GenerateFrameEntry() {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001525 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001526 __ Bind(&frame_entry_label_);
1527
Serban Constantinescu02164b32014-11-13 14:05:07 +00001528 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kArm64) || !IsLeafMethod();
1529 if (do_overflow_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001530 UseScratchRegisterScope temps(masm);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001531 Register temp = temps.AcquireX();
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001532 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001533 __ Sub(temp, sp, static_cast<int32_t>(GetStackOverflowReservedBytes(kArm64)));
Artem Serov914d7a82017-02-07 14:33:49 +00001534 {
1535 // Ensure that between load and RecordPcInfo there are no pools emitted.
1536 ExactAssemblyScope eas(GetVIXLAssembler(),
1537 kInstructionSize,
1538 CodeBufferCheckScope::kExactSize);
1539 __ ldr(wzr, MemOperand(temp, 0));
1540 RecordPcInfo(nullptr, 0);
1541 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00001542 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001543
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001544 if (!HasEmptyFrame()) {
1545 int frame_size = GetFrameSize();
1546 // Stack layout:
1547 // sp[frame_size - 8] : lr.
1548 // ... : other preserved core registers.
1549 // ... : other preserved fp registers.
1550 // ... : reserved frame space.
1551 // sp[0] : current method.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001552
1553 // Save the current method if we need it. Note that we do not
1554 // do this in HCurrentMethod, as the instruction might have been removed
1555 // in the SSA graph.
1556 if (RequiresCurrentMethod()) {
1557 __ Str(kArtMethodRegister, MemOperand(sp, -frame_size, PreIndex));
Nicolas Geoffray9989b162016-10-13 13:42:30 +01001558 } else {
1559 __ Claim(frame_size);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001560 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001561 GetAssembler()->cfi().AdjustCFAOffset(frame_size);
Zheng Xu69a50302015-04-14 20:04:41 +08001562 GetAssembler()->SpillRegisters(GetFramePreservedCoreRegisters(),
1563 frame_size - GetCoreSpillSize());
1564 GetAssembler()->SpillRegisters(GetFramePreservedFPRegisters(),
1565 frame_size - FrameEntrySpillSize());
Mingyao Yang063fc772016-08-02 11:02:54 -07001566
1567 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1568 // Initialize should_deoptimize flag to 0.
1569 Register wzr = Register(VIXLRegCodeFromART(WZR), kWRegSize);
1570 __ Str(wzr, MemOperand(sp, GetStackOffsetOfShouldDeoptimizeFlag()));
1571 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001572 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001573}
1574
1575void CodeGeneratorARM64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001576 GetAssembler()->cfi().RememberState();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001577 if (!HasEmptyFrame()) {
1578 int frame_size = GetFrameSize();
Zheng Xu69a50302015-04-14 20:04:41 +08001579 GetAssembler()->UnspillRegisters(GetFramePreservedFPRegisters(),
1580 frame_size - FrameEntrySpillSize());
1581 GetAssembler()->UnspillRegisters(GetFramePreservedCoreRegisters(),
1582 frame_size - GetCoreSpillSize());
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001583 __ Drop(frame_size);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001584 GetAssembler()->cfi().AdjustCFAOffset(-frame_size);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001585 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001586 __ Ret();
1587 GetAssembler()->cfi().RestoreState();
1588 GetAssembler()->cfi().DefCFAOffset(GetFrameSize());
Alexandre Rames5319def2014-10-23 10:03:10 +01001589}
1590
Scott Wakeling97c72b72016-06-24 16:19:36 +01001591CPURegList CodeGeneratorARM64::GetFramePreservedCoreRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001592 DCHECK(ArtVixlRegCodeCoherentForRegSet(core_spill_mask_, GetNumberOfCoreRegisters(), 0, 0));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001593 return CPURegList(CPURegister::kRegister, kXRegSize,
1594 core_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001595}
1596
Scott Wakeling97c72b72016-06-24 16:19:36 +01001597CPURegList CodeGeneratorARM64::GetFramePreservedFPRegisters() const {
Zheng Xuda403092015-04-24 17:35:39 +08001598 DCHECK(ArtVixlRegCodeCoherentForRegSet(0, 0, fpu_spill_mask_,
1599 GetNumberOfFloatingPointRegisters()));
Scott Wakeling97c72b72016-06-24 16:19:36 +01001600 return CPURegList(CPURegister::kFPRegister, kDRegSize,
1601 fpu_spill_mask_);
Zheng Xuda403092015-04-24 17:35:39 +08001602}
1603
Alexandre Rames5319def2014-10-23 10:03:10 +01001604void CodeGeneratorARM64::Bind(HBasicBlock* block) {
1605 __ Bind(GetLabelOf(block));
1606}
1607
Calin Juravle175dc732015-08-25 15:42:32 +01001608void CodeGeneratorARM64::MoveConstant(Location location, int32_t value) {
1609 DCHECK(location.IsRegister());
1610 __ Mov(RegisterFrom(location, Primitive::kPrimInt), value);
1611}
1612
Calin Juravlee460d1d2015-09-29 04:52:17 +01001613void CodeGeneratorARM64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1614 if (location.IsRegister()) {
1615 locations->AddTemp(location);
1616 } else {
1617 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1618 }
1619}
1620
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001621void CodeGeneratorARM64::MarkGCCard(Register object, Register value, bool value_can_be_null) {
Alexandre Rames67555f72014-11-18 10:55:16 +00001622 UseScratchRegisterScope temps(GetVIXLAssembler());
Alexandre Rames5319def2014-10-23 10:03:10 +01001623 Register card = temps.AcquireX();
Serban Constantinescu02164b32014-11-13 14:05:07 +00001624 Register temp = temps.AcquireW(); // Index within the CardTable - 32bit.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001625 vixl::aarch64::Label done;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001626 if (value_can_be_null) {
1627 __ Cbz(value, &done);
1628 }
Andreas Gampe542451c2016-07-26 09:02:02 -07001629 __ Ldr(card, MemOperand(tr, Thread::CardTableOffset<kArm64PointerSize>().Int32Value()));
Alexandre Rames5319def2014-10-23 10:03:10 +01001630 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
Serban Constantinescu02164b32014-11-13 14:05:07 +00001631 __ Strb(card, MemOperand(card, temp.X()));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001632 if (value_can_be_null) {
1633 __ Bind(&done);
1634 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001635}
1636
David Brazdil58282f42016-01-14 12:45:10 +00001637void CodeGeneratorARM64::SetupBlockedRegisters() const {
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001638 // Blocked core registers:
1639 // lr : Runtime reserved.
1640 // tr : Runtime reserved.
1641 // xSuspend : Runtime reserved. TODO: Unblock this when the runtime stops using it.
1642 // ip1 : VIXL core temp.
1643 // ip0 : VIXL core temp.
1644 //
1645 // Blocked fp registers:
1646 // d31 : VIXL fp temp.
Alexandre Rames5319def2014-10-23 10:03:10 +01001647 CPURegList reserved_core_registers = vixl_reserved_core_registers;
1648 reserved_core_registers.Combine(runtime_reserved_core_registers);
Alexandre Rames5319def2014-10-23 10:03:10 +01001649 while (!reserved_core_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001650 blocked_core_registers_[reserved_core_registers.PopLowestIndex().GetCode()] = true;
Alexandre Rames5319def2014-10-23 10:03:10 +01001651 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001652
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001653 CPURegList reserved_fp_registers = vixl_reserved_fp_registers;
Zheng Xua3ec3942015-02-15 18:39:46 +08001654 while (!reserved_fp_registers.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001655 blocked_fpu_registers_[reserved_fp_registers.PopLowestIndex().GetCode()] = true;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00001656 }
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001657
David Brazdil58282f42016-01-14 12:45:10 +00001658 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001659 // Stubs do not save callee-save floating point registers. If the graph
1660 // is debuggable, we need to deal with these registers differently. For
1661 // now, just block them.
David Brazdil58282f42016-01-14 12:45:10 +00001662 CPURegList reserved_fp_registers_debuggable = callee_saved_fp_registers;
1663 while (!reserved_fp_registers_debuggable.IsEmpty()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001664 blocked_fpu_registers_[reserved_fp_registers_debuggable.PopLowestIndex().GetCode()] = true;
Serban Constantinescu3d087de2015-01-28 11:57:05 +00001665 }
1666 }
Alexandre Rames5319def2014-10-23 10:03:10 +01001667}
1668
Alexandre Rames3e69f162014-12-10 10:36:50 +00001669size_t CodeGeneratorARM64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1670 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1671 __ Str(reg, MemOperand(sp, stack_index));
1672 return kArm64WordSize;
1673}
1674
1675size_t CodeGeneratorARM64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1676 Register reg = Register(VIXLRegCodeFromART(reg_id), kXRegSize);
1677 __ Ldr(reg, MemOperand(sp, stack_index));
1678 return kArm64WordSize;
1679}
1680
1681size_t CodeGeneratorARM64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1682 FPRegister reg = FPRegister(reg_id, kDRegSize);
1683 __ Str(reg, MemOperand(sp, stack_index));
1684 return kArm64WordSize;
1685}
1686
1687size_t CodeGeneratorARM64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1688 FPRegister reg = FPRegister(reg_id, kDRegSize);
1689 __ Ldr(reg, MemOperand(sp, stack_index));
1690 return kArm64WordSize;
1691}
1692
Alexandre Rames5319def2014-10-23 10:03:10 +01001693void CodeGeneratorARM64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001694 stream << XRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001695}
1696
1697void CodeGeneratorARM64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001698 stream << DRegister(reg);
Alexandre Rames5319def2014-10-23 10:03:10 +01001699}
1700
Alexandre Rames67555f72014-11-18 10:55:16 +00001701void CodeGeneratorARM64::MoveConstant(CPURegister destination, HConstant* constant) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001702 if (constant->IsIntConstant()) {
1703 __ Mov(Register(destination), constant->AsIntConstant()->GetValue());
1704 } else if (constant->IsLongConstant()) {
1705 __ Mov(Register(destination), constant->AsLongConstant()->GetValue());
1706 } else if (constant->IsNullConstant()) {
1707 __ Mov(Register(destination), 0);
Alexandre Rames67555f72014-11-18 10:55:16 +00001708 } else if (constant->IsFloatConstant()) {
1709 __ Fmov(FPRegister(destination), constant->AsFloatConstant()->GetValue());
1710 } else {
1711 DCHECK(constant->IsDoubleConstant());
1712 __ Fmov(FPRegister(destination), constant->AsDoubleConstant()->GetValue());
1713 }
1714}
1715
Alexandre Rames3e69f162014-12-10 10:36:50 +00001716
1717static bool CoherentConstantAndType(Location constant, Primitive::Type type) {
1718 DCHECK(constant.IsConstant());
1719 HConstant* cst = constant.GetConstant();
1720 return (cst->IsIntConstant() && type == Primitive::kPrimInt) ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001721 // Null is mapped to a core W register, which we associate with kPrimInt.
1722 (cst->IsNullConstant() && type == Primitive::kPrimInt) ||
Alexandre Rames3e69f162014-12-10 10:36:50 +00001723 (cst->IsLongConstant() && type == Primitive::kPrimLong) ||
1724 (cst->IsFloatConstant() && type == Primitive::kPrimFloat) ||
1725 (cst->IsDoubleConstant() && type == Primitive::kPrimDouble);
1726}
1727
Roland Levillain558dea12017-01-27 19:40:44 +00001728// Allocate a scratch register from the VIXL pool, querying first into
1729// the floating-point register pool, and then the the core register
1730// pool. This is essentially a reimplementation of
1731// vixl::aarch64::UseScratchRegisterScope::AcquireCPURegisterOfSize
1732// using a different allocation strategy.
1733static CPURegister AcquireFPOrCoreCPURegisterOfSize(vixl::aarch64::MacroAssembler* masm,
1734 vixl::aarch64::UseScratchRegisterScope* temps,
1735 int size_in_bits) {
1736 return masm->GetScratchFPRegisterList()->IsEmpty()
1737 ? CPURegister(temps->AcquireRegisterOfSize(size_in_bits))
1738 : CPURegister(temps->AcquireVRegisterOfSize(size_in_bits));
1739}
1740
Calin Juravlee460d1d2015-09-29 04:52:17 +01001741void CodeGeneratorARM64::MoveLocation(Location destination,
1742 Location source,
1743 Primitive::Type dst_type) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001744 if (source.Equals(destination)) {
1745 return;
1746 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001747
1748 // A valid move can always be inferred from the destination and source
1749 // locations. When moving from and to a register, the argument type can be
1750 // used to generate 32bit instead of 64bit moves. In debug mode we also
1751 // checks the coherency of the locations and the type.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001752 bool unspecified_type = (dst_type == Primitive::kPrimVoid);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001753
1754 if (destination.IsRegister() || destination.IsFpuRegister()) {
1755 if (unspecified_type) {
1756 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
1757 if (source.IsStackSlot() ||
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001758 (src_cst != nullptr && (src_cst->IsIntConstant()
1759 || src_cst->IsFloatConstant()
1760 || src_cst->IsNullConstant()))) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001761 // For stack slots and 32bit constants, a 64bit type is appropriate.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001762 dst_type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
Alexandre Rames67555f72014-11-18 10:55:16 +00001763 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001764 // If the source is a double stack slot or a 64bit constant, a 64bit
1765 // type is appropriate. Else the source is a register, and since the
1766 // type has not been specified, we chose a 64bit type to force a 64bit
1767 // move.
Calin Juravlee460d1d2015-09-29 04:52:17 +01001768 dst_type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
Alexandre Rames67555f72014-11-18 10:55:16 +00001769 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001770 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001771 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(dst_type)) ||
1772 (destination.IsRegister() && !Primitive::IsFloatingPointType(dst_type)));
1773 CPURegister dst = CPURegisterFrom(destination, dst_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001774 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
1775 DCHECK(dst.Is64Bits() == source.IsDoubleStackSlot());
1776 __ Ldr(dst, StackOperandFrom(source));
Artem Serovd4bccf12017-04-03 18:47:32 +01001777 } else if (source.IsSIMDStackSlot()) {
1778 __ Ldr(QRegisterFrom(destination), StackOperandFrom(source));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001779 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780 DCHECK(CoherentConstantAndType(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001781 MoveConstant(dst, source.GetConstant());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001782 } else if (source.IsRegister()) {
Alexandre Rames3e69f162014-12-10 10:36:50 +00001783 if (destination.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001784 __ Mov(Register(dst), RegisterFrom(source, dst_type));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001785 } else {
Zheng Xuad4450e2015-04-17 18:48:56 +08001786 DCHECK(destination.IsFpuRegister());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001787 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1788 ? Primitive::kPrimLong
1789 : Primitive::kPrimInt;
1790 __ Fmov(FPRegisterFrom(destination, dst_type), RegisterFrom(source, source_type));
1791 }
1792 } else {
1793 DCHECK(source.IsFpuRegister());
1794 if (destination.IsRegister()) {
1795 Primitive::Type source_type = Primitive::Is64BitType(dst_type)
1796 ? Primitive::kPrimDouble
1797 : Primitive::kPrimFloat;
1798 __ Fmov(RegisterFrom(destination, dst_type), FPRegisterFrom(source, source_type));
1799 } else {
1800 DCHECK(destination.IsFpuRegister());
Artem Serovd4bccf12017-04-03 18:47:32 +01001801 if (GetGraph()->HasSIMD()) {
1802 __ Mov(QRegisterFrom(destination), QRegisterFrom(source));
1803 } else {
1804 __ Fmov(FPRegister(dst), FPRegisterFrom(source, dst_type));
1805 }
1806 }
1807 }
1808 } else if (destination.IsSIMDStackSlot()) {
1809 if (source.IsFpuRegister()) {
1810 __ Str(QRegisterFrom(source), StackOperandFrom(destination));
1811 } else {
1812 DCHECK(source.IsSIMDStackSlot());
1813 UseScratchRegisterScope temps(GetVIXLAssembler());
1814 if (GetVIXLAssembler()->GetScratchFPRegisterList()->IsEmpty()) {
1815 Register temp = temps.AcquireX();
1816 __ Ldr(temp, MemOperand(sp, source.GetStackIndex()));
1817 __ Str(temp, MemOperand(sp, destination.GetStackIndex()));
1818 __ Ldr(temp, MemOperand(sp, source.GetStackIndex() + kArm64WordSize));
1819 __ Str(temp, MemOperand(sp, destination.GetStackIndex() + kArm64WordSize));
1820 } else {
1821 FPRegister temp = temps.AcquireVRegisterOfSize(kQRegSize);
1822 __ Ldr(temp, StackOperandFrom(source));
1823 __ Str(temp, StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001824 }
1825 }
Alexandre Rames3e69f162014-12-10 10:36:50 +00001826 } else { // The destination is not a register. It must be a stack slot.
1827 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
1828 if (source.IsRegister() || source.IsFpuRegister()) {
1829 if (unspecified_type) {
1830 if (source.IsRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001831 dst_type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001832 } else {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001833 dst_type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001834 }
1835 }
Calin Juravlee460d1d2015-09-29 04:52:17 +01001836 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(dst_type)) &&
1837 (source.IsFpuRegister() == Primitive::IsFloatingPointType(dst_type)));
1838 __ Str(CPURegisterFrom(source, dst_type), StackOperandFrom(destination));
Alexandre Rames3e69f162014-12-10 10:36:50 +00001839 } else if (source.IsConstant()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001840 DCHECK(unspecified_type || CoherentConstantAndType(source, dst_type))
1841 << source << " " << dst_type;
Alexandre Rames3e69f162014-12-10 10:36:50 +00001842 UseScratchRegisterScope temps(GetVIXLAssembler());
1843 HConstant* src_cst = source.GetConstant();
1844 CPURegister temp;
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001845 if (src_cst->IsZeroBitPattern()) {
Scott Wakeling79db9972017-01-19 14:08:42 +00001846 temp = (src_cst->IsLongConstant() || src_cst->IsDoubleConstant())
1847 ? Register(xzr)
1848 : Register(wzr);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001849 } else {
Alexandre Ramesb2b753c2016-08-02 13:45:28 +01001850 if (src_cst->IsIntConstant()) {
1851 temp = temps.AcquireW();
1852 } else if (src_cst->IsLongConstant()) {
1853 temp = temps.AcquireX();
1854 } else if (src_cst->IsFloatConstant()) {
1855 temp = temps.AcquireS();
1856 } else {
1857 DCHECK(src_cst->IsDoubleConstant());
1858 temp = temps.AcquireD();
1859 }
1860 MoveConstant(temp, src_cst);
Alexandre Rames3e69f162014-12-10 10:36:50 +00001861 }
Alexandre Rames67555f72014-11-18 10:55:16 +00001862 __ Str(temp, StackOperandFrom(destination));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001863 } else {
Alexandre Rames67555f72014-11-18 10:55:16 +00001864 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
Alexandre Rames3e69f162014-12-10 10:36:50 +00001865 DCHECK(source.IsDoubleStackSlot() == destination.IsDoubleStackSlot());
Alexandre Rames67555f72014-11-18 10:55:16 +00001866 UseScratchRegisterScope temps(GetVIXLAssembler());
Roland Levillain78b3d5d2017-01-04 10:27:50 +00001867 // Use any scratch register (a core or a floating-point one)
1868 // from VIXL scratch register pools as a temporary.
1869 //
1870 // We used to only use the FP scratch register pool, but in some
1871 // rare cases the only register from this pool (D31) would
1872 // already be used (e.g. within a ParallelMove instruction, when
1873 // a move is blocked by a another move requiring a scratch FP
1874 // register, which would reserve D31). To prevent this issue, we
1875 // ask for a scratch register of any type (core or FP).
Roland Levillain558dea12017-01-27 19:40:44 +00001876 //
1877 // Also, we start by asking for a FP scratch register first, as the
1878 // demand of scratch core registers is higher. This is why we
1879 // use AcquireFPOrCoreCPURegisterOfSize instead of
1880 // UseScratchRegisterScope::AcquireCPURegisterOfSize, which
1881 // allocates core scratch registers first.
1882 CPURegister temp = AcquireFPOrCoreCPURegisterOfSize(
1883 GetVIXLAssembler(),
1884 &temps,
1885 (destination.IsDoubleStackSlot() ? kXRegSize : kWRegSize));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001886 __ Ldr(temp, StackOperandFrom(source));
1887 __ Str(temp, StackOperandFrom(destination));
1888 }
1889 }
1890}
1891
1892void CodeGeneratorARM64::Load(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001893 CPURegister dst,
1894 const MemOperand& src) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001895 switch (type) {
1896 case Primitive::kPrimBoolean:
Alexandre Rames67555f72014-11-18 10:55:16 +00001897 __ Ldrb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001898 break;
1899 case Primitive::kPrimByte:
Alexandre Rames67555f72014-11-18 10:55:16 +00001900 __ Ldrsb(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001901 break;
1902 case Primitive::kPrimShort:
Alexandre Rames67555f72014-11-18 10:55:16 +00001903 __ Ldrsh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001904 break;
1905 case Primitive::kPrimChar:
Alexandre Rames67555f72014-11-18 10:55:16 +00001906 __ Ldrh(Register(dst), src);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001907 break;
1908 case Primitive::kPrimInt:
1909 case Primitive::kPrimNot:
1910 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001911 case Primitive::kPrimFloat:
1912 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00001913 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Rames67555f72014-11-18 10:55:16 +00001914 __ Ldr(dst, src);
1915 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00001916 case Primitive::kPrimVoid:
1917 LOG(FATAL) << "Unreachable type " << type;
1918 }
1919}
1920
Calin Juravle77520bc2015-01-12 18:45:46 +00001921void CodeGeneratorARM64::LoadAcquire(HInstruction* instruction,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001922 CPURegister dst,
Roland Levillain44015862016-01-22 11:47:17 +00001923 const MemOperand& src,
1924 bool needs_null_check) {
Alexandre Ramesd921d642015-04-16 15:07:16 +01001925 MacroAssembler* masm = GetVIXLAssembler();
Alexandre Ramesd921d642015-04-16 15:07:16 +01001926 UseScratchRegisterScope temps(masm);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001927 Register temp_base = temps.AcquireX();
Calin Juravle77520bc2015-01-12 18:45:46 +00001928 Primitive::Type type = instruction->GetType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001929
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001930 DCHECK(!src.IsPreIndex());
1931 DCHECK(!src.IsPostIndex());
1932
1933 // TODO(vixl): Let the MacroAssembler handle MemOperand.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001934 __ Add(temp_base, src.GetBaseRegister(), OperandFromMemOperand(src));
Artem Serov914d7a82017-02-07 14:33:49 +00001935 {
1936 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
1937 MemOperand base = MemOperand(temp_base);
1938 switch (type) {
1939 case Primitive::kPrimBoolean:
1940 {
1941 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1942 __ ldarb(Register(dst), base);
1943 if (needs_null_check) {
1944 MaybeRecordImplicitNullCheck(instruction);
1945 }
1946 }
1947 break;
1948 case Primitive::kPrimByte:
1949 {
1950 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1951 __ ldarb(Register(dst), base);
1952 if (needs_null_check) {
1953 MaybeRecordImplicitNullCheck(instruction);
1954 }
1955 }
1956 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1957 break;
1958 case Primitive::kPrimChar:
1959 {
1960 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1961 __ ldarh(Register(dst), base);
1962 if (needs_null_check) {
1963 MaybeRecordImplicitNullCheck(instruction);
1964 }
1965 }
1966 break;
1967 case Primitive::kPrimShort:
1968 {
1969 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1970 __ ldarh(Register(dst), base);
1971 if (needs_null_check) {
1972 MaybeRecordImplicitNullCheck(instruction);
1973 }
1974 }
1975 __ Sbfx(Register(dst), Register(dst), 0, Primitive::ComponentSize(type) * kBitsPerByte);
1976 break;
1977 case Primitive::kPrimInt:
1978 case Primitive::kPrimNot:
1979 case Primitive::kPrimLong:
1980 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
1981 {
1982 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1983 __ ldar(Register(dst), base);
1984 if (needs_null_check) {
1985 MaybeRecordImplicitNullCheck(instruction);
1986 }
1987 }
1988 break;
1989 case Primitive::kPrimFloat:
1990 case Primitive::kPrimDouble: {
1991 DCHECK(dst.IsFPRegister());
1992 DCHECK_EQ(dst.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00001993
Artem Serov914d7a82017-02-07 14:33:49 +00001994 Register temp = dst.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
1995 {
1996 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
1997 __ ldar(temp, base);
1998 if (needs_null_check) {
1999 MaybeRecordImplicitNullCheck(instruction);
2000 }
2001 }
2002 __ Fmov(FPRegister(dst), temp);
2003 break;
Roland Levillain44015862016-01-22 11:47:17 +00002004 }
Artem Serov914d7a82017-02-07 14:33:49 +00002005 case Primitive::kPrimVoid:
2006 LOG(FATAL) << "Unreachable type " << type;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002007 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002008 }
2009}
2010
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002011void CodeGeneratorARM64::Store(Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002012 CPURegister src,
2013 const MemOperand& dst) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002014 switch (type) {
2015 case Primitive::kPrimBoolean:
2016 case Primitive::kPrimByte:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002017 __ Strb(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002018 break;
2019 case Primitive::kPrimChar:
2020 case Primitive::kPrimShort:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002021 __ Strh(Register(src), dst);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002022 break;
2023 case Primitive::kPrimInt:
2024 case Primitive::kPrimNot:
2025 case Primitive::kPrimLong:
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002026 case Primitive::kPrimFloat:
2027 case Primitive::kPrimDouble:
Alexandre Rames542361f2015-01-29 16:57:31 +00002028 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002029 __ Str(src, dst);
Alexandre Rames67555f72014-11-18 10:55:16 +00002030 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002031 case Primitive::kPrimVoid:
2032 LOG(FATAL) << "Unreachable type " << type;
2033 }
2034}
2035
Artem Serov914d7a82017-02-07 14:33:49 +00002036void CodeGeneratorARM64::StoreRelease(HInstruction* instruction,
2037 Primitive::Type type,
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002038 CPURegister src,
Artem Serov914d7a82017-02-07 14:33:49 +00002039 const MemOperand& dst,
2040 bool needs_null_check) {
2041 MacroAssembler* masm = GetVIXLAssembler();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002042 UseScratchRegisterScope temps(GetVIXLAssembler());
2043 Register temp_base = temps.AcquireX();
2044
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002045 DCHECK(!dst.IsPreIndex());
2046 DCHECK(!dst.IsPostIndex());
2047
2048 // TODO(vixl): Let the MacroAssembler handle this.
Andreas Gampe878d58c2015-01-15 23:24:00 -08002049 Operand op = OperandFromMemOperand(dst);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002050 __ Add(temp_base, dst.GetBaseRegister(), op);
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002051 MemOperand base = MemOperand(temp_base);
Artem Serov914d7a82017-02-07 14:33:49 +00002052 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002053 switch (type) {
2054 case Primitive::kPrimBoolean:
2055 case Primitive::kPrimByte:
Artem Serov914d7a82017-02-07 14:33:49 +00002056 {
2057 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2058 __ stlrb(Register(src), base);
2059 if (needs_null_check) {
2060 MaybeRecordImplicitNullCheck(instruction);
2061 }
2062 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002063 break;
2064 case Primitive::kPrimChar:
2065 case Primitive::kPrimShort:
Artem Serov914d7a82017-02-07 14:33:49 +00002066 {
2067 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2068 __ stlrh(Register(src), base);
2069 if (needs_null_check) {
2070 MaybeRecordImplicitNullCheck(instruction);
2071 }
2072 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002073 break;
2074 case Primitive::kPrimInt:
2075 case Primitive::kPrimNot:
2076 case Primitive::kPrimLong:
Alexandre Rames542361f2015-01-29 16:57:31 +00002077 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Artem Serov914d7a82017-02-07 14:33:49 +00002078 {
2079 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2080 __ stlr(Register(src), base);
2081 if (needs_null_check) {
2082 MaybeRecordImplicitNullCheck(instruction);
2083 }
2084 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002085 break;
2086 case Primitive::kPrimFloat:
2087 case Primitive::kPrimDouble: {
Alexandre Rames542361f2015-01-29 16:57:31 +00002088 DCHECK_EQ(src.Is64Bits(), Primitive::Is64BitType(type));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002089 Register temp_src;
2090 if (src.IsZero()) {
2091 // The zero register is used to avoid synthesizing zero constants.
2092 temp_src = Register(src);
2093 } else {
2094 DCHECK(src.IsFPRegister());
2095 temp_src = src.Is64Bits() ? temps.AcquireX() : temps.AcquireW();
2096 __ Fmov(temp_src, FPRegister(src));
2097 }
Artem Serov914d7a82017-02-07 14:33:49 +00002098 {
2099 ExactAssemblyScope eas(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
2100 __ stlr(temp_src, base);
2101 if (needs_null_check) {
2102 MaybeRecordImplicitNullCheck(instruction);
2103 }
2104 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002105 break;
2106 }
2107 case Primitive::kPrimVoid:
2108 LOG(FATAL) << "Unreachable type " << type;
2109 }
2110}
2111
Calin Juravle175dc732015-08-25 15:42:32 +01002112void CodeGeneratorARM64::InvokeRuntime(QuickEntrypointEnum entrypoint,
2113 HInstruction* instruction,
2114 uint32_t dex_pc,
2115 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002116 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Artem Serov914d7a82017-02-07 14:33:49 +00002117
2118 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64PointerSize>(entrypoint).Int32Value()));
2119 {
2120 // Ensure the pc position is recorded immediately after the `blr` instruction.
2121 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
2122 __ blr(lr);
2123 if (EntrypointRequiresStackMap(entrypoint)) {
2124 RecordPcInfo(instruction, dex_pc, slow_path);
2125 }
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002126 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002127}
2128
Roland Levillaindec8f632016-07-22 17:10:06 +01002129void CodeGeneratorARM64::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2130 HInstruction* instruction,
2131 SlowPathCode* slow_path) {
2132 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Roland Levillaindec8f632016-07-22 17:10:06 +01002133 __ Ldr(lr, MemOperand(tr, entry_point_offset));
2134 __ Blr(lr);
2135}
2136
Alexandre Rames67555f72014-11-18 10:55:16 +00002137void InstructionCodeGeneratorARM64::GenerateClassInitializationCheck(SlowPathCodeARM64* slow_path,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002138 Register class_reg) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002139 UseScratchRegisterScope temps(GetVIXLAssembler());
2140 Register temp = temps.AcquireW();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002141 size_t status_offset = mirror::Class::StatusOffset().SizeValue();
2142
Serban Constantinescu02164b32014-11-13 14:05:07 +00002143 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002144 // TODO(vixl): Let the MacroAssembler handle MemOperand.
2145 __ Add(temp, class_reg, status_offset);
2146 __ Ldar(temp, HeapOperand(temp));
2147 __ Cmp(temp, mirror::Class::kStatusInitialized);
2148 __ B(lt, slow_path->GetEntryLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00002149 __ Bind(slow_path->GetExitLabel());
2150}
Alexandre Rames5319def2014-10-23 10:03:10 +01002151
Roland Levillain44015862016-01-22 11:47:17 +00002152void CodeGeneratorARM64::GenerateMemoryBarrier(MemBarrierKind kind) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00002153 BarrierType type = BarrierAll;
2154
2155 switch (kind) {
2156 case MemBarrierKind::kAnyAny:
2157 case MemBarrierKind::kAnyStore: {
2158 type = BarrierAll;
2159 break;
2160 }
2161 case MemBarrierKind::kLoadAny: {
2162 type = BarrierReads;
2163 break;
2164 }
2165 case MemBarrierKind::kStoreStore: {
2166 type = BarrierWrites;
2167 break;
2168 }
2169 default:
2170 LOG(FATAL) << "Unexpected memory barrier " << kind;
2171 }
2172 __ Dmb(InnerShareable, type);
2173}
2174
Serban Constantinescu02164b32014-11-13 14:05:07 +00002175void InstructionCodeGeneratorARM64::GenerateSuspendCheck(HSuspendCheck* instruction,
2176 HBasicBlock* successor) {
2177 SuspendCheckSlowPathARM64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01002178 down_cast<SuspendCheckSlowPathARM64*>(instruction->GetSlowPath());
2179 if (slow_path == nullptr) {
2180 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM64(instruction, successor);
2181 instruction->SetSlowPath(slow_path);
2182 codegen_->AddSlowPath(slow_path);
2183 if (successor != nullptr) {
2184 DCHECK(successor->IsLoopHeader());
2185 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
2186 }
2187 } else {
2188 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2189 }
2190
Serban Constantinescu02164b32014-11-13 14:05:07 +00002191 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
2192 Register temp = temps.AcquireW();
2193
Andreas Gampe542451c2016-07-26 09:02:02 -07002194 __ Ldrh(temp, MemOperand(tr, Thread::ThreadFlagsOffset<kArm64PointerSize>().SizeValue()));
Serban Constantinescu02164b32014-11-13 14:05:07 +00002195 if (successor == nullptr) {
2196 __ Cbnz(temp, slow_path->GetEntryLabel());
2197 __ Bind(slow_path->GetReturnLabel());
2198 } else {
2199 __ Cbz(temp, codegen_->GetLabelOf(successor));
2200 __ B(slow_path->GetEntryLabel());
2201 // slow_path will return to GetLabelOf(successor).
2202 }
2203}
2204
Alexandre Rames5319def2014-10-23 10:03:10 +01002205InstructionCodeGeneratorARM64::InstructionCodeGeneratorARM64(HGraph* graph,
2206 CodeGeneratorARM64* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002207 : InstructionCodeGenerator(graph, codegen),
Alexandre Rames5319def2014-10-23 10:03:10 +01002208 assembler_(codegen->GetAssembler()),
2209 codegen_(codegen) {}
2210
2211#define FOR_EACH_UNIMPLEMENTED_INSTRUCTION(M) \
Alexandre Rames3e69f162014-12-10 10:36:50 +00002212 /* No unimplemented IR. */
Alexandre Rames5319def2014-10-23 10:03:10 +01002213
2214#define UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name) name##UnimplementedInstructionBreakCode
2215
2216enum UnimplementedInstructionBreakCode {
Alexandre Rames67555f72014-11-18 10:55:16 +00002217 // Using a base helps identify when we hit such breakpoints.
2218 UnimplementedInstructionBreakCodeBaseCode = 0x900,
Alexandre Rames5319def2014-10-23 10:03:10 +01002219#define ENUM_UNIMPLEMENTED_INSTRUCTION(name) UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name),
2220 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(ENUM_UNIMPLEMENTED_INSTRUCTION)
2221#undef ENUM_UNIMPLEMENTED_INSTRUCTION
2222};
2223
2224#define DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS(name) \
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002225 void InstructionCodeGeneratorARM64::Visit##name(H##name* instr ATTRIBUTE_UNUSED) { \
Alexandre Rames5319def2014-10-23 10:03:10 +01002226 __ Brk(UNIMPLEMENTED_INSTRUCTION_BREAK_CODE(name)); \
2227 } \
2228 void LocationsBuilderARM64::Visit##name(H##name* instr) { \
2229 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr); \
2230 locations->SetOut(Location::Any()); \
2231 }
2232 FOR_EACH_UNIMPLEMENTED_INSTRUCTION(DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS)
2233#undef DEFINE_UNIMPLEMENTED_INSTRUCTION_VISITORS
2234
2235#undef UNIMPLEMENTED_INSTRUCTION_BREAK_CODE
Alexandre Rames67555f72014-11-18 10:55:16 +00002236#undef FOR_EACH_UNIMPLEMENTED_INSTRUCTION
Alexandre Rames5319def2014-10-23 10:03:10 +01002237
Alexandre Rames67555f72014-11-18 10:55:16 +00002238void LocationsBuilderARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002239 DCHECK_EQ(instr->InputCount(), 2U);
2240 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2241 Primitive::Type type = instr->GetResultType();
2242 switch (type) {
2243 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002244 case Primitive::kPrimLong:
Alexandre Rames5319def2014-10-23 10:03:10 +01002245 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00002246 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instr->InputAt(1), instr));
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002248 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002249
2250 case Primitive::kPrimFloat:
2251 case Primitive::kPrimDouble:
2252 locations->SetInAt(0, Location::RequiresFpuRegister());
2253 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00002254 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002255 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002256
Alexandre Rames5319def2014-10-23 10:03:10 +01002257 default:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002258 LOG(FATAL) << "Unexpected " << instr->DebugName() << " type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002259 }
2260}
2261
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002262void LocationsBuilderARM64::HandleFieldGet(HInstruction* instruction,
2263 const FieldInfo& field_info) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002264 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
2265
2266 bool object_field_get_with_read_barrier =
2267 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Rames09a99962015-04-15 11:47:56 +01002268 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002269 new (GetGraph()->GetArena()) LocationSummary(instruction,
2270 object_field_get_with_read_barrier ?
2271 LocationSummary::kCallOnSlowPath :
2272 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002273 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002274 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillaind0b51832017-01-26 19:04:23 +00002275 // We need a temporary register for the read barrier marking slow
2276 // path in CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002277 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2278 !Runtime::Current()->UseJitCompilation() &&
2279 !field_info.IsVolatile()) {
2280 // If link-time thunks for the Baker read barrier are enabled, for AOT
2281 // non-volatile loads we need a temporary only if the offset is too big.
2282 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
2283 locations->AddTemp(FixedTempLocation());
2284 }
2285 } else {
2286 locations->AddTemp(Location::RequiresRegister());
2287 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002288 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002289 locations->SetInAt(0, Location::RequiresRegister());
2290 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2291 locations->SetOut(Location::RequiresFpuRegister());
2292 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002293 // The output overlaps for an object field get when read barriers
2294 // are enabled: we do not want the load to overwrite the object's
2295 // location, as we need it to emit the read barrier.
2296 locations->SetOut(
2297 Location::RequiresRegister(),
2298 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames09a99962015-04-15 11:47:56 +01002299 }
2300}
2301
2302void InstructionCodeGeneratorARM64::HandleFieldGet(HInstruction* instruction,
2303 const FieldInfo& field_info) {
2304 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain44015862016-01-22 11:47:17 +00002305 LocationSummary* locations = instruction->GetLocations();
2306 Location base_loc = locations->InAt(0);
2307 Location out = locations->Out();
2308 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01002309 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002310 MemOperand field = HeapOperand(InputRegisterAt(instruction, 0), field_info.GetFieldOffset());
Alexandre Rames09a99962015-04-15 11:47:56 +01002311
Roland Levillain44015862016-01-22 11:47:17 +00002312 if (field_type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2313 // Object FieldGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002314 // /* HeapReference<Object> */ out = *(base + offset)
2315 Register base = RegisterFrom(base_loc, Primitive::kPrimNot);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002316 Location maybe_temp =
2317 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
Roland Levillain44015862016-01-22 11:47:17 +00002318 // Note that potential implicit null checks are handled in this
2319 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier call.
2320 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2321 instruction,
2322 out,
2323 base,
2324 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002325 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00002326 /* needs_null_check */ true,
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002327 field_info.IsVolatile());
Roland Levillain44015862016-01-22 11:47:17 +00002328 } else {
2329 // General case.
2330 if (field_info.IsVolatile()) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00002331 // Note that a potential implicit null check is handled in this
2332 // CodeGeneratorARM64::LoadAcquire call.
2333 // NB: LoadAcquire will record the pc info if needed.
2334 codegen_->LoadAcquire(
2335 instruction, OutputCPURegister(instruction), field, /* needs_null_check */ true);
Alexandre Rames09a99962015-04-15 11:47:56 +01002336 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002337 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2338 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002339 codegen_->Load(field_type, OutputCPURegister(instruction), field);
Alexandre Rames09a99962015-04-15 11:47:56 +01002340 codegen_->MaybeRecordImplicitNullCheck(instruction);
Alexandre Rames09a99962015-04-15 11:47:56 +01002341 }
Roland Levillain44015862016-01-22 11:47:17 +00002342 if (field_type == Primitive::kPrimNot) {
2343 // If read barriers are enabled, emit read barriers other than
2344 // Baker's using a slow path (and also unpoison the loaded
2345 // reference, if heap poisoning is enabled).
2346 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
2347 }
Roland Levillain4d027112015-07-01 15:41:14 +01002348 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002349}
2350
2351void LocationsBuilderARM64::HandleFieldSet(HInstruction* instruction) {
2352 LocationSummary* locations =
2353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2354 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002355 if (IsConstantZeroBitPattern(instruction->InputAt(1))) {
2356 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
2357 } else if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002358 locations->SetInAt(1, Location::RequiresFpuRegister());
2359 } else {
2360 locations->SetInAt(1, Location::RequiresRegister());
2361 }
2362}
2363
2364void InstructionCodeGeneratorARM64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002365 const FieldInfo& field_info,
2366 bool value_can_be_null) {
Alexandre Rames09a99962015-04-15 11:47:56 +01002367 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
2368
2369 Register obj = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002370 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 1);
Roland Levillain4d027112015-07-01 15:41:14 +01002371 CPURegister source = value;
Alexandre Rames09a99962015-04-15 11:47:56 +01002372 Offset offset = field_info.GetFieldOffset();
2373 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames09a99962015-04-15 11:47:56 +01002374
Roland Levillain4d027112015-07-01 15:41:14 +01002375 {
2376 // We use a block to end the scratch scope before the write barrier, thus
2377 // freeing the temporary registers so they can be used in `MarkGCCard`.
2378 UseScratchRegisterScope temps(GetVIXLAssembler());
2379
2380 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
2381 DCHECK(value.IsW());
2382 Register temp = temps.AcquireW();
2383 __ Mov(temp, value.W());
2384 GetAssembler()->PoisonHeapReference(temp.W());
2385 source = temp;
Alexandre Rames09a99962015-04-15 11:47:56 +01002386 }
Roland Levillain4d027112015-07-01 15:41:14 +01002387
2388 if (field_info.IsVolatile()) {
Artem Serov914d7a82017-02-07 14:33:49 +00002389 codegen_->StoreRelease(
2390 instruction, field_type, source, HeapOperand(obj, offset), /* needs_null_check */ true);
Roland Levillain4d027112015-07-01 15:41:14 +01002391 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00002392 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2393 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain4d027112015-07-01 15:41:14 +01002394 codegen_->Store(field_type, source, HeapOperand(obj, offset));
2395 codegen_->MaybeRecordImplicitNullCheck(instruction);
2396 }
Alexandre Rames09a99962015-04-15 11:47:56 +01002397 }
2398
2399 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002400 codegen_->MarkGCCard(obj, Register(value), value_can_be_null);
Alexandre Rames09a99962015-04-15 11:47:56 +01002401 }
2402}
2403
Alexandre Rames67555f72014-11-18 10:55:16 +00002404void InstructionCodeGeneratorARM64::HandleBinaryOp(HBinaryOperation* instr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002405 Primitive::Type type = instr->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01002406
2407 switch (type) {
2408 case Primitive::kPrimInt:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002409 case Primitive::kPrimLong: {
2410 Register dst = OutputRegister(instr);
2411 Register lhs = InputRegisterAt(instr, 0);
2412 Operand rhs = InputOperandAt(instr, 1);
Alexandre Rames5319def2014-10-23 10:03:10 +01002413 if (instr->IsAdd()) {
2414 __ Add(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002415 } else if (instr->IsAnd()) {
2416 __ And(dst, lhs, rhs);
2417 } else if (instr->IsOr()) {
2418 __ Orr(dst, lhs, rhs);
2419 } else if (instr->IsSub()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01002420 __ Sub(dst, lhs, rhs);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002421 } else if (instr->IsRor()) {
2422 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002423 uint32_t shift = rhs.GetImmediate() & (lhs.GetSizeInBits() - 1);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00002424 __ Ror(dst, lhs, shift);
2425 } else {
2426 // Ensure shift distance is in the same size register as the result. If
2427 // we are rotating a long and the shift comes in a w register originally,
2428 // we don't need to sxtw for use as an x since the shift distances are
2429 // all & reg_bits - 1.
2430 __ Ror(dst, lhs, RegisterFrom(instr->GetLocations()->InAt(1), type));
2431 }
Alexandre Rames67555f72014-11-18 10:55:16 +00002432 } else {
2433 DCHECK(instr->IsXor());
2434 __ Eor(dst, lhs, rhs);
Alexandre Rames5319def2014-10-23 10:03:10 +01002435 }
2436 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002437 }
2438 case Primitive::kPrimFloat:
2439 case Primitive::kPrimDouble: {
2440 FPRegister dst = OutputFPRegister(instr);
2441 FPRegister lhs = InputFPRegisterAt(instr, 0);
2442 FPRegister rhs = InputFPRegisterAt(instr, 1);
2443 if (instr->IsAdd()) {
2444 __ Fadd(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002445 } else if (instr->IsSub()) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002446 __ Fsub(dst, lhs, rhs);
Alexandre Rames67555f72014-11-18 10:55:16 +00002447 } else {
2448 LOG(FATAL) << "Unexpected floating-point binary operation";
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002449 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002450 break;
Alexandre Ramesa89086e2014-11-07 17:13:25 +00002451 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002452 default:
Alexandre Rames67555f72014-11-18 10:55:16 +00002453 LOG(FATAL) << "Unexpected binary operation type " << type;
Alexandre Rames5319def2014-10-23 10:03:10 +01002454 }
2455}
2456
Serban Constantinescu02164b32014-11-13 14:05:07 +00002457void LocationsBuilderARM64::HandleShift(HBinaryOperation* instr) {
2458 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2459
2460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2461 Primitive::Type type = instr->GetResultType();
2462 switch (type) {
2463 case Primitive::kPrimInt:
2464 case Primitive::kPrimLong: {
2465 locations->SetInAt(0, Location::RequiresRegister());
2466 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
Artem Serov87c97052016-09-23 13:34:31 +01002467 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002468 break;
2469 }
2470 default:
2471 LOG(FATAL) << "Unexpected shift type " << type;
2472 }
2473}
2474
2475void InstructionCodeGeneratorARM64::HandleShift(HBinaryOperation* instr) {
2476 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
2477
2478 Primitive::Type type = instr->GetType();
2479 switch (type) {
2480 case Primitive::kPrimInt:
2481 case Primitive::kPrimLong: {
2482 Register dst = OutputRegister(instr);
2483 Register lhs = InputRegisterAt(instr, 0);
2484 Operand rhs = InputOperandAt(instr, 1);
2485 if (rhs.IsImmediate()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002486 uint32_t shift_value = rhs.GetImmediate() &
Roland Levillain5b5b9312016-03-22 14:57:31 +00002487 (type == Primitive::kPrimInt ? kMaxIntShiftDistance : kMaxLongShiftDistance);
Serban Constantinescu02164b32014-11-13 14:05:07 +00002488 if (instr->IsShl()) {
2489 __ Lsl(dst, lhs, shift_value);
2490 } else if (instr->IsShr()) {
2491 __ Asr(dst, lhs, shift_value);
2492 } else {
2493 __ Lsr(dst, lhs, shift_value);
2494 }
2495 } else {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002496 Register rhs_reg = dst.IsX() ? rhs.GetRegister().X() : rhs.GetRegister().W();
Serban Constantinescu02164b32014-11-13 14:05:07 +00002497
2498 if (instr->IsShl()) {
2499 __ Lsl(dst, lhs, rhs_reg);
2500 } else if (instr->IsShr()) {
2501 __ Asr(dst, lhs, rhs_reg);
2502 } else {
2503 __ Lsr(dst, lhs, rhs_reg);
2504 }
2505 }
2506 break;
2507 }
2508 default:
2509 LOG(FATAL) << "Unexpected shift operation type " << type;
2510 }
2511}
2512
Alexandre Rames5319def2014-10-23 10:03:10 +01002513void LocationsBuilderARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002514 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002515}
2516
2517void InstructionCodeGeneratorARM64::VisitAdd(HAdd* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00002518 HandleBinaryOp(instruction);
2519}
2520
2521void LocationsBuilderARM64::VisitAnd(HAnd* instruction) {
2522 HandleBinaryOp(instruction);
2523}
2524
2525void InstructionCodeGeneratorARM64::VisitAnd(HAnd* instruction) {
2526 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01002527}
2528
Artem Serov7fc63502016-02-09 17:15:29 +00002529void LocationsBuilderARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002530 DCHECK(Primitive::IsIntegralType(instr->GetType())) << instr->GetType();
2531 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2532 locations->SetInAt(0, Location::RequiresRegister());
2533 // There is no immediate variant of negated bitwise instructions in AArch64.
2534 locations->SetInAt(1, Location::RequiresRegister());
2535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2536}
2537
Artem Serov7fc63502016-02-09 17:15:29 +00002538void InstructionCodeGeneratorARM64::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instr) {
Kevin Brodsky9ff0d202016-01-11 13:43:31 +00002539 Register dst = OutputRegister(instr);
2540 Register lhs = InputRegisterAt(instr, 0);
2541 Register rhs = InputRegisterAt(instr, 1);
2542
2543 switch (instr->GetOpKind()) {
2544 case HInstruction::kAnd:
2545 __ Bic(dst, lhs, rhs);
2546 break;
2547 case HInstruction::kOr:
2548 __ Orn(dst, lhs, rhs);
2549 break;
2550 case HInstruction::kXor:
2551 __ Eon(dst, lhs, rhs);
2552 break;
2553 default:
2554 LOG(FATAL) << "Unreachable";
2555 }
2556}
2557
Anton Kirilov74234da2017-01-13 14:42:47 +00002558void LocationsBuilderARM64::VisitDataProcWithShifterOp(
2559 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002560 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
2561 instruction->GetType() == Primitive::kPrimLong);
2562 LocationSummary* locations =
2563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2564 if (instruction->GetInstrKind() == HInstruction::kNeg) {
2565 locations->SetInAt(0, Location::ConstantLocation(instruction->InputAt(0)->AsConstant()));
2566 } else {
2567 locations->SetInAt(0, Location::RequiresRegister());
2568 }
2569 locations->SetInAt(1, Location::RequiresRegister());
2570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2571}
2572
Anton Kirilov74234da2017-01-13 14:42:47 +00002573void InstructionCodeGeneratorARM64::VisitDataProcWithShifterOp(
2574 HDataProcWithShifterOp* instruction) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002575 Primitive::Type type = instruction->GetType();
2576 HInstruction::InstructionKind kind = instruction->GetInstrKind();
2577 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
2578 Register out = OutputRegister(instruction);
2579 Register left;
2580 if (kind != HInstruction::kNeg) {
2581 left = InputRegisterAt(instruction, 0);
2582 }
Anton Kirilov74234da2017-01-13 14:42:47 +00002583 // If this `HDataProcWithShifterOp` was created by merging a type conversion as the
Alexandre Rames8626b742015-11-25 16:28:08 +00002584 // shifter operand operation, the IR generating `right_reg` (input to the type
2585 // conversion) can have a different type from the current instruction's type,
2586 // so we manually indicate the type.
2587 Register right_reg = RegisterFrom(instruction->GetLocations()->InAt(1), type);
Alexandre Rames8626b742015-11-25 16:28:08 +00002588 Operand right_operand(0);
2589
Anton Kirilov74234da2017-01-13 14:42:47 +00002590 HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
2591 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
Alexandre Rames8626b742015-11-25 16:28:08 +00002592 right_operand = Operand(right_reg, helpers::ExtendFromOpKind(op_kind));
2593 } else {
Anton Kirilov74234da2017-01-13 14:42:47 +00002594 right_operand = Operand(right_reg,
2595 helpers::ShiftFromOpKind(op_kind),
2596 instruction->GetShiftAmount());
Alexandre Rames8626b742015-11-25 16:28:08 +00002597 }
2598
2599 // Logical binary operations do not support extension operations in the
2600 // operand. Note that VIXL would still manage if it was passed by generating
2601 // the extension as a separate instruction.
2602 // `HNeg` also does not support extension. See comments in `ShifterOperandSupportsExtension()`.
2603 DCHECK(!right_operand.IsExtendedRegister() ||
2604 (kind != HInstruction::kAnd && kind != HInstruction::kOr && kind != HInstruction::kXor &&
2605 kind != HInstruction::kNeg));
2606 switch (kind) {
2607 case HInstruction::kAdd:
2608 __ Add(out, left, right_operand);
2609 break;
2610 case HInstruction::kAnd:
2611 __ And(out, left, right_operand);
2612 break;
2613 case HInstruction::kNeg:
Roland Levillain1a653882016-03-18 18:05:57 +00002614 DCHECK(instruction->InputAt(0)->AsConstant()->IsArithmeticZero());
Alexandre Rames8626b742015-11-25 16:28:08 +00002615 __ Neg(out, right_operand);
2616 break;
2617 case HInstruction::kOr:
2618 __ Orr(out, left, right_operand);
2619 break;
2620 case HInstruction::kSub:
2621 __ Sub(out, left, right_operand);
2622 break;
2623 case HInstruction::kXor:
2624 __ Eor(out, left, right_operand);
2625 break;
2626 default:
2627 LOG(FATAL) << "Unexpected operation kind: " << kind;
2628 UNREACHABLE();
2629 }
2630}
2631
Artem Serov328429f2016-07-06 16:23:04 +01002632void LocationsBuilderARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002633 LocationSummary* locations =
2634 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2635 locations->SetInAt(0, Location::RequiresRegister());
2636 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->GetOffset(), instruction));
Artem Serov87c97052016-09-23 13:34:31 +01002637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002638}
2639
Roland Levillain19c54192016-11-04 13:44:09 +00002640void InstructionCodeGeneratorARM64::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002641 __ Add(OutputRegister(instruction),
2642 InputRegisterAt(instruction, 0),
2643 Operand(InputOperandAt(instruction, 1)));
2644}
2645
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002646void LocationsBuilderARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002649 HInstruction* accumulator = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
2650 if (instr->GetOpKind() == HInstruction::kSub &&
2651 accumulator->IsConstant() &&
Roland Levillain1a653882016-03-18 18:05:57 +00002652 accumulator->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002653 // Don't allocate register for Mneg instruction.
2654 } else {
2655 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
2656 Location::RequiresRegister());
2657 }
2658 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
2659 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
Alexandre Rames418318f2015-11-20 15:55:47 +00002660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2661}
2662
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002663void InstructionCodeGeneratorARM64::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
Alexandre Rames418318f2015-11-20 15:55:47 +00002664 Register res = OutputRegister(instr);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002665 Register mul_left = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulLeftIndex);
2666 Register mul_right = InputRegisterAt(instr, HMultiplyAccumulate::kInputMulRightIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002667
2668 // Avoid emitting code that could trigger Cortex A53's erratum 835769.
2669 // This fixup should be carried out for all multiply-accumulate instructions:
2670 // madd, msub, smaddl, smsubl, umaddl and umsubl.
2671 if (instr->GetType() == Primitive::kPrimLong &&
2672 codegen_->GetInstructionSetFeatures().NeedFixCortexA53_835769()) {
2673 MacroAssembler* masm = down_cast<CodeGeneratorARM64*>(codegen_)->GetVIXLAssembler();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002674 vixl::aarch64::Instruction* prev =
2675 masm->GetCursorAddress<vixl::aarch64::Instruction*>() - kInstructionSize;
Alexandre Rames418318f2015-11-20 15:55:47 +00002676 if (prev->IsLoadOrStore()) {
2677 // Make sure we emit only exactly one nop.
Artem Serov914d7a82017-02-07 14:33:49 +00002678 ExactAssemblyScope scope(masm, kInstructionSize, CodeBufferCheckScope::kExactSize);
Alexandre Rames418318f2015-11-20 15:55:47 +00002679 __ nop();
2680 }
2681 }
2682
2683 if (instr->GetOpKind() == HInstruction::kAdd) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002684 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
Alexandre Rames418318f2015-11-20 15:55:47 +00002685 __ Madd(res, mul_left, mul_right, accumulator);
2686 } else {
2687 DCHECK(instr->GetOpKind() == HInstruction::kSub);
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002688 HInstruction* accum_instr = instr->InputAt(HMultiplyAccumulate::kInputAccumulatorIndex);
Roland Levillain1a653882016-03-18 18:05:57 +00002689 if (accum_instr->IsConstant() && accum_instr->AsConstant()->IsArithmeticZero()) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03002690 __ Mneg(res, mul_left, mul_right);
2691 } else {
2692 Register accumulator = InputRegisterAt(instr, HMultiplyAccumulate::kInputAccumulatorIndex);
2693 __ Msub(res, mul_left, mul_right, accumulator);
2694 }
Alexandre Rames418318f2015-11-20 15:55:47 +00002695 }
2696}
2697
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002698void LocationsBuilderARM64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002699 bool object_array_get_with_read_barrier =
2700 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002701 LocationSummary* locations =
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002702 new (GetGraph()->GetArena()) LocationSummary(instruction,
2703 object_array_get_with_read_barrier ?
2704 LocationSummary::kCallOnSlowPath :
2705 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01002706 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002707 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +00002708 // We need a temporary register for the read barrier marking slow
2709 // path in CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002710 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
2711 !Runtime::Current()->UseJitCompilation() &&
2712 instruction->GetIndex()->IsConstant()) {
2713 // Array loads with constant index are treated as field loads.
2714 // If link-time thunks for the Baker read barrier are enabled, for AOT
2715 // constant index loads we need a temporary only if the offset is too big.
2716 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
2717 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
2718 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
2719 if (offset >= kReferenceLoadMinFarOffset) {
2720 locations->AddTemp(FixedTempLocation());
2721 }
2722 } else {
2723 locations->AddTemp(Location::RequiresRegister());
2724 }
Vladimir Marko70e97462016-08-09 11:04:26 +01002725 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002726 locations->SetInAt(0, Location::RequiresRegister());
2727 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002728 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2730 } else {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002731 // The output overlaps in the case of an object array get with
2732 // read barriers enabled: we do not want the move to overwrite the
2733 // array's location, as we need it to emit the read barrier.
2734 locations->SetOut(
2735 Location::RequiresRegister(),
2736 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01002737 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002738}
2739
2740void InstructionCodeGeneratorARM64::VisitArrayGet(HArrayGet* instruction) {
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002741 Primitive::Type type = instruction->GetType();
2742 Register obj = InputRegisterAt(instruction, 0);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002743 LocationSummary* locations = instruction->GetLocations();
2744 Location index = locations->InAt(1);
Roland Levillain44015862016-01-22 11:47:17 +00002745 Location out = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002746 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002747 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2748 instruction->IsStringCharAt();
Alexandre Ramesd921d642015-04-16 15:07:16 +01002749 MacroAssembler* masm = GetVIXLAssembler();
2750 UseScratchRegisterScope temps(masm);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002751
Roland Levillain19c54192016-11-04 13:44:09 +00002752 // The read barrier instrumentation of object ArrayGet instructions
2753 // does not support the HIntermediateAddress instruction.
2754 DCHECK(!((type == Primitive::kPrimNot) &&
2755 instruction->GetArray()->IsIntermediateAddress() &&
2756 kEmitCompilerReadBarrier));
2757
Roland Levillain44015862016-01-22 11:47:17 +00002758 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2759 // Object ArrayGet with Baker's read barrier case.
Roland Levillain44015862016-01-22 11:47:17 +00002760 // Note that a potential implicit null check is handled in the
2761 // CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002762 if (index.IsConstant()) {
2763 // Array load with a constant index can be treated as a field load.
2764 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2765 Location maybe_temp =
2766 (locations->GetTempCount() != 0) ? locations->GetTemp(0) : Location::NoLocation();
2767 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2768 out,
2769 obj.W(),
2770 offset,
2771 maybe_temp,
2772 /* needs_null_check */ true,
2773 /* use_load_acquire */ false);
2774 } else {
2775 Register temp = WRegisterFrom(locations->GetTemp(0));
2776 codegen_->GenerateArrayLoadWithBakerReadBarrier(
2777 instruction, out, obj.W(), offset, index, temp, /* needs_null_check */ true);
2778 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002779 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002780 // General case.
2781 MemOperand source = HeapOperand(obj);
jessicahandojo05765752016-09-09 19:01:32 -07002782 Register length;
2783 if (maybe_compressed_char_at) {
2784 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2785 length = temps.AcquireW();
Artem Serov914d7a82017-02-07 14:33:49 +00002786 {
2787 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2788 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2789
2790 if (instruction->GetArray()->IsIntermediateAddress()) {
2791 DCHECK_LT(count_offset, offset);
2792 int64_t adjusted_offset =
2793 static_cast<int64_t>(count_offset) - static_cast<int64_t>(offset);
2794 // Note that `adjusted_offset` is negative, so this will be a LDUR.
2795 __ Ldr(length, MemOperand(obj.X(), adjusted_offset));
2796 } else {
2797 __ Ldr(length, HeapOperand(obj, count_offset));
2798 }
2799 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002800 }
jessicahandojo05765752016-09-09 19:01:32 -07002801 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002802 if (index.IsConstant()) {
jessicahandojo05765752016-09-09 19:01:32 -07002803 if (maybe_compressed_char_at) {
2804 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002805 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2806 "Expecting 0=compressed, 1=uncompressed");
2807 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002808 __ Ldrb(Register(OutputCPURegister(instruction)),
2809 HeapOperand(obj, offset + Int64ConstantFrom(index)));
2810 __ B(&done);
2811 __ Bind(&uncompressed_load);
2812 __ Ldrh(Register(OutputCPURegister(instruction)),
2813 HeapOperand(obj, offset + (Int64ConstantFrom(index) << 1)));
2814 __ Bind(&done);
2815 } else {
2816 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(type);
2817 source = HeapOperand(obj, offset);
2818 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002819 } else {
Roland Levillain44015862016-01-22 11:47:17 +00002820 Register temp = temps.AcquireSameSizeAs(obj);
Artem Serov328429f2016-07-06 16:23:04 +01002821 if (instruction->GetArray()->IsIntermediateAddress()) {
Roland Levillain44015862016-01-22 11:47:17 +00002822 // We do not need to compute the intermediate address from the array: the
2823 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002824 // `TryExtractArrayAccessAddress()`.
Roland Levillain44015862016-01-22 11:47:17 +00002825 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002826 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Roland Levillain44015862016-01-22 11:47:17 +00002827 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), offset);
2828 }
2829 temp = obj;
2830 } else {
2831 __ Add(temp, obj, offset);
2832 }
jessicahandojo05765752016-09-09 19:01:32 -07002833 if (maybe_compressed_char_at) {
2834 vixl::aarch64::Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002835 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2836 "Expecting 0=compressed, 1=uncompressed");
2837 __ Tbnz(length.W(), 0, &uncompressed_load);
jessicahandojo05765752016-09-09 19:01:32 -07002838 __ Ldrb(Register(OutputCPURegister(instruction)),
2839 HeapOperand(temp, XRegisterFrom(index), LSL, 0));
2840 __ B(&done);
2841 __ Bind(&uncompressed_load);
2842 __ Ldrh(Register(OutputCPURegister(instruction)),
2843 HeapOperand(temp, XRegisterFrom(index), LSL, 1));
2844 __ Bind(&done);
2845 } else {
2846 source = HeapOperand(temp, XRegisterFrom(index), LSL, Primitive::ComponentSizeShift(type));
2847 }
Roland Levillain44015862016-01-22 11:47:17 +00002848 }
jessicahandojo05765752016-09-09 19:01:32 -07002849 if (!maybe_compressed_char_at) {
Artem Serov914d7a82017-02-07 14:33:49 +00002850 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2851 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
jessicahandojo05765752016-09-09 19:01:32 -07002852 codegen_->Load(type, OutputCPURegister(instruction), source);
2853 codegen_->MaybeRecordImplicitNullCheck(instruction);
2854 }
Roland Levillain44015862016-01-22 11:47:17 +00002855
2856 if (type == Primitive::kPrimNot) {
2857 static_assert(
2858 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2859 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2860 Location obj_loc = locations->InAt(0);
2861 if (index.IsConstant()) {
2862 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset);
2863 } else {
2864 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, obj_loc, offset, index);
2865 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002866 }
Roland Levillain4d027112015-07-01 15:41:14 +01002867 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002868}
2869
Alexandre Rames5319def2014-10-23 10:03:10 +01002870void LocationsBuilderARM64::VisitArrayLength(HArrayLength* instruction) {
2871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2872 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00002873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01002874}
2875
2876void InstructionCodeGeneratorARM64::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markodce016e2016-04-28 13:10:02 +01002877 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07002878 vixl::aarch64::Register out = OutputRegister(instruction);
Artem Serov914d7a82017-02-07 14:33:49 +00002879 {
2880 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
2881 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2882 __ Ldr(out, HeapOperand(InputRegisterAt(instruction, 0), offset));
2883 codegen_->MaybeRecordImplicitNullCheck(instruction);
2884 }
jessicahandojo05765752016-09-09 19:01:32 -07002885 // Mask out compression flag from String's array length.
2886 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002887 __ Lsr(out.W(), out.W(), 1u);
jessicahandojo05765752016-09-09 19:01:32 -07002888 }
Alexandre Rames5319def2014-10-23 10:03:10 +01002889}
2890
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002891void LocationsBuilderARM64::VisitArraySet(HArraySet* instruction) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002892 Primitive::Type value_type = instruction->GetComponentType();
2893
2894 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002895 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2896 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01002897 may_need_runtime_call_for_type_check ?
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002898 LocationSummary::kCallOnSlowPath :
2899 LocationSummary::kNoCall);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002902 if (IsConstantZeroBitPattern(instruction->InputAt(2))) {
2903 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
2904 } else if (Primitive::IsFloatingPointType(value_type)) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002905 locations->SetInAt(2, Location::RequiresFpuRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002906 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002907 locations->SetInAt(2, Location::RequiresRegister());
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002908 }
2909}
2910
2911void InstructionCodeGeneratorARM64::VisitArraySet(HArraySet* instruction) {
2912 Primitive::Type value_type = instruction->GetComponentType();
Alexandre Rames97833a02015-04-16 15:07:12 +01002913 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002914 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002915 bool needs_write_barrier =
2916 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Alexandre Rames97833a02015-04-16 15:07:12 +01002917
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002918 Register array = InputRegisterAt(instruction, 0);
Alexandre Ramesbe919d92016-08-23 18:33:36 +01002919 CPURegister value = InputCPURegisterOrZeroRegAt(instruction, 2);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002920 CPURegister source = value;
2921 Location index = locations->InAt(1);
2922 size_t offset = mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
2923 MemOperand destination = HeapOperand(array);
2924 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002925
2926 if (!needs_write_barrier) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002927 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002928 if (index.IsConstant()) {
2929 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
2930 destination = HeapOperand(array, offset);
2931 } else {
2932 UseScratchRegisterScope temps(masm);
2933 Register temp = temps.AcquireSameSizeAs(array);
Artem Serov328429f2016-07-06 16:23:04 +01002934 if (instruction->GetArray()->IsIntermediateAddress()) {
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002935 // We do not need to compute the intermediate address from the array: the
2936 // input instruction has done it already. See the comment in
Artem Serov328429f2016-07-06 16:23:04 +01002937 // `TryExtractArrayAccessAddress()`.
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002938 if (kIsDebugBuild) {
Artem Serov328429f2016-07-06 16:23:04 +01002939 HIntermediateAddress* tmp = instruction->GetArray()->AsIntermediateAddress();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002940 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == offset);
2941 }
2942 temp = array;
2943 } else {
2944 __ Add(temp, array, offset);
2945 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002946 destination = HeapOperand(temp,
2947 XRegisterFrom(index),
2948 LSL,
2949 Primitive::ComponentSizeShift(value_type));
2950 }
Artem Serov914d7a82017-02-07 14:33:49 +00002951 {
2952 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
2953 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2954 codegen_->Store(value_type, value, destination);
2955 codegen_->MaybeRecordImplicitNullCheck(instruction);
2956 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00002957 } else {
Artem Serov328429f2016-07-06 16:23:04 +01002958 DCHECK(!instruction->GetArray()->IsIntermediateAddress());
Scott Wakeling97c72b72016-06-24 16:19:36 +01002959 vixl::aarch64::Label done;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002960 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames97833a02015-04-16 15:07:12 +01002961 {
2962 // We use a block to end the scratch scope before the write barrier, thus
2963 // freeing the temporary registers so they can be used in `MarkGCCard`.
2964 UseScratchRegisterScope temps(masm);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002965 Register temp = temps.AcquireSameSizeAs(array);
Alexandre Rames97833a02015-04-16 15:07:12 +01002966 if (index.IsConstant()) {
2967 offset += Int64ConstantFrom(index) << Primitive::ComponentSizeShift(value_type);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002968 destination = HeapOperand(array, offset);
Alexandre Rames97833a02015-04-16 15:07:12 +01002969 } else {
Alexandre Rames82000b02015-07-07 11:34:16 +01002970 destination = HeapOperand(temp,
2971 XRegisterFrom(index),
2972 LSL,
2973 Primitive::ComponentSizeShift(value_type));
Alexandre Rames97833a02015-04-16 15:07:12 +01002974 }
2975
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002976 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2977 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2978 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2979
Roland Levillain22ccc3a2015-11-24 13:10:05 +00002980 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002981 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM64(instruction);
2982 codegen_->AddSlowPath(slow_path);
2983 if (instruction->GetValueCanBeNull()) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002984 vixl::aarch64::Label non_zero;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002985 __ Cbnz(Register(value), &non_zero);
2986 if (!index.IsConstant()) {
2987 __ Add(temp, array, offset);
2988 }
Artem Serov914d7a82017-02-07 14:33:49 +00002989 {
2990 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools
2991 // emitted.
2992 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
2993 __ Str(wzr, destination);
2994 codegen_->MaybeRecordImplicitNullCheck(instruction);
2995 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01002996 __ B(&done);
2997 __ Bind(&non_zero);
2998 }
2999
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003000 // Note that when Baker read barriers are enabled, the type
3001 // checks are performed without read barriers. This is fine,
3002 // even in the case where a class object is in the from-space
3003 // after the flip, as a comparison involving such a type would
3004 // not produce a false positive; it may of course produce a
3005 // false negative, in which case we would take the ArraySet
3006 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01003007
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003008 Register temp2 = temps.AcquireSameSizeAs(array);
3009 // /* HeapReference<Class> */ temp = array->klass_
Artem Serov914d7a82017-02-07 14:33:49 +00003010 {
3011 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
3012 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3013 __ Ldr(temp, HeapOperand(array, class_offset));
3014 codegen_->MaybeRecordImplicitNullCheck(instruction);
3015 }
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003016 GetAssembler()->MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01003017
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003018 // /* HeapReference<Class> */ temp = temp->component_type_
3019 __ Ldr(temp, HeapOperand(temp, component_offset));
3020 // /* HeapReference<Class> */ temp2 = value->klass_
3021 __ Ldr(temp2, HeapOperand(Register(value), class_offset));
3022 // If heap poisoning is enabled, no need to unpoison `temp`
3023 // nor `temp2`, as we are comparing two poisoned references.
3024 __ Cmp(temp, temp2);
3025 temps.Release(temp2);
Roland Levillain16d9f942016-08-25 17:27:56 +01003026
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003027 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3028 vixl::aarch64::Label do_put;
3029 __ B(eq, &do_put);
3030 // If heap poisoning is enabled, the `temp` reference has
3031 // not been unpoisoned yet; unpoison it now.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003032 GetAssembler()->MaybeUnpoisonHeapReference(temp);
3033
Roland Levillain9d6e1f82016-09-05 15:57:33 +01003034 // /* HeapReference<Class> */ temp = temp->super_class_
3035 __ Ldr(temp, HeapOperand(temp, super_offset));
3036 // If heap poisoning is enabled, no need to unpoison
3037 // `temp`, as we are comparing against null below.
3038 __ Cbnz(temp, slow_path->GetEntryLabel());
3039 __ Bind(&do_put);
3040 } else {
3041 __ B(ne, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003042 }
3043 }
3044
3045 if (kPoisonHeapReferences) {
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003046 Register temp2 = temps.AcquireSameSizeAs(array);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003047 DCHECK(value.IsW());
Nicolas Geoffraya8a0fe22015-10-01 15:50:27 +01003048 __ Mov(temp2, value.W());
3049 GetAssembler()->PoisonHeapReference(temp2);
3050 source = temp2;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003051 }
3052
3053 if (!index.IsConstant()) {
3054 __ Add(temp, array, offset);
Vladimir Markod1ef8732017-04-18 13:55:13 +01003055 } else {
3056 // We no longer need the `temp` here so release it as the store below may
3057 // need a scratch register (if the constant index makes the offset too large)
3058 // and the poisoned `source` could be using the other scratch register.
3059 temps.Release(temp);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003060 }
Artem Serov914d7a82017-02-07 14:33:49 +00003061 {
3062 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3063 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3064 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003065
Artem Serov914d7a82017-02-07 14:33:49 +00003066 if (!may_need_runtime_call_for_type_check) {
3067 codegen_->MaybeRecordImplicitNullCheck(instruction);
3068 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003069 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003070 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003071
3072 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3073
3074 if (done.IsLinked()) {
3075 __ Bind(&done);
3076 }
3077
3078 if (slow_path != nullptr) {
3079 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003080 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003081 }
3082}
3083
Alexandre Rames67555f72014-11-18 10:55:16 +00003084void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003085 RegisterSet caller_saves = RegisterSet::Empty();
3086 InvokeRuntimeCallingConvention calling_convention;
3087 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3088 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3089 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003090 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003091 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003092}
3093
3094void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003095 BoundsCheckSlowPathARM64* slow_path =
3096 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003097 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003098 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3099 __ B(slow_path->GetEntryLabel(), hs);
3100}
3101
Alexandre Rames67555f72014-11-18 10:55:16 +00003102void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3103 LocationSummary* locations =
3104 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3105 locations->SetInAt(0, Location::RequiresRegister());
3106 if (check->HasUses()) {
3107 locations->SetOut(Location::SameAsFirstInput());
3108 }
3109}
3110
3111void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3112 // We assume the class is not null.
3113 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3114 check->GetLoadClass(), check, check->GetDexPc(), true);
3115 codegen_->AddSlowPath(slow_path);
3116 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3117}
3118
Roland Levillain1a653882016-03-18 18:05:57 +00003119static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3120 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3121 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3122}
3123
3124void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3125 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3126 Location rhs_loc = instruction->GetLocations()->InAt(1);
3127 if (rhs_loc.IsConstant()) {
3128 // 0.0 is the only immediate that can be encoded directly in
3129 // an FCMP instruction.
3130 //
3131 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3132 // specify that in a floating-point comparison, positive zero
3133 // and negative zero are considered equal, so we can use the
3134 // literal 0.0 for both cases here.
3135 //
3136 // Note however that some methods (Float.equal, Float.compare,
3137 // Float.compareTo, Double.equal, Double.compare,
3138 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3139 // StrictMath.min) consider 0.0 to be (strictly) greater than
3140 // -0.0. So if we ever translate calls to these methods into a
3141 // HCompare instruction, we must handle the -0.0 case with
3142 // care here.
3143 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3144 __ Fcmp(lhs_reg, 0.0);
3145 } else {
3146 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3147 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003148}
3149
Serban Constantinescu02164b32014-11-13 14:05:07 +00003150void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003151 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003152 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3153 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003154 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003155 case Primitive::kPrimBoolean:
3156 case Primitive::kPrimByte:
3157 case Primitive::kPrimShort:
3158 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003159 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003160 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003161 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003162 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3164 break;
3165 }
3166 case Primitive::kPrimFloat:
3167 case Primitive::kPrimDouble: {
3168 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003169 locations->SetInAt(1,
3170 IsFloatingPointZeroConstant(compare->InputAt(1))
3171 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3172 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003173 locations->SetOut(Location::RequiresRegister());
3174 break;
3175 }
3176 default:
3177 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3178 }
3179}
3180
3181void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3182 Primitive::Type in_type = compare->InputAt(0)->GetType();
3183
3184 // 0 if: left == right
3185 // 1 if: left > right
3186 // -1 if: left < right
3187 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003188 case Primitive::kPrimBoolean:
3189 case Primitive::kPrimByte:
3190 case Primitive::kPrimShort:
3191 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003192 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003193 case Primitive::kPrimLong: {
3194 Register result = OutputRegister(compare);
3195 Register left = InputRegisterAt(compare, 0);
3196 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003197 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003198 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3199 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003200 break;
3201 }
3202 case Primitive::kPrimFloat:
3203 case Primitive::kPrimDouble: {
3204 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003205 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003206 __ Cset(result, ne);
3207 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003208 break;
3209 }
3210 default:
3211 LOG(FATAL) << "Unimplemented compare type " << in_type;
3212 }
3213}
3214
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003215void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003217
3218 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3219 locations->SetInAt(0, Location::RequiresFpuRegister());
3220 locations->SetInAt(1,
3221 IsFloatingPointZeroConstant(instruction->InputAt(1))
3222 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3223 : Location::RequiresFpuRegister());
3224 } else {
3225 // Integer cases.
3226 locations->SetInAt(0, Location::RequiresRegister());
3227 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3228 }
3229
David Brazdilb3e773e2016-01-26 11:28:37 +00003230 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003232 }
3233}
3234
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003235void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003236 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003237 return;
3238 }
3239
3240 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003241 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003242 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003243
Roland Levillain7f63c522015-07-13 15:54:55 +00003244 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003245 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003246 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003247 } else {
3248 // Integer cases.
3249 Register lhs = InputRegisterAt(instruction, 0);
3250 Operand rhs = InputOperandAt(instruction, 1);
3251 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003252 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003253 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003254}
3255
3256#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3257 M(Equal) \
3258 M(NotEqual) \
3259 M(LessThan) \
3260 M(LessThanOrEqual) \
3261 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003262 M(GreaterThanOrEqual) \
3263 M(Below) \
3264 M(BelowOrEqual) \
3265 M(Above) \
3266 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003267#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003268void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3269void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003270FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003271#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003272#undef FOR_EACH_CONDITION_INSTRUCTION
3273
Zheng Xuc6667102015-05-15 16:08:45 +08003274void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3275 DCHECK(instruction->IsDiv() || instruction->IsRem());
3276
3277 LocationSummary* locations = instruction->GetLocations();
3278 Location second = locations->InAt(1);
3279 DCHECK(second.IsConstant());
3280
3281 Register out = OutputRegister(instruction);
3282 Register dividend = InputRegisterAt(instruction, 0);
3283 int64_t imm = Int64FromConstant(second.GetConstant());
3284 DCHECK(imm == 1 || imm == -1);
3285
3286 if (instruction->IsRem()) {
3287 __ Mov(out, 0);
3288 } else {
3289 if (imm == 1) {
3290 __ Mov(out, dividend);
3291 } else {
3292 __ Neg(out, dividend);
3293 }
3294 }
3295}
3296
3297void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3298 DCHECK(instruction->IsDiv() || instruction->IsRem());
3299
3300 LocationSummary* locations = instruction->GetLocations();
3301 Location second = locations->InAt(1);
3302 DCHECK(second.IsConstant());
3303
3304 Register out = OutputRegister(instruction);
3305 Register dividend = InputRegisterAt(instruction, 0);
3306 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003307 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003308 int ctz_imm = CTZ(abs_imm);
3309
3310 UseScratchRegisterScope temps(GetVIXLAssembler());
3311 Register temp = temps.AcquireSameSizeAs(out);
3312
3313 if (instruction->IsDiv()) {
3314 __ Add(temp, dividend, abs_imm - 1);
3315 __ Cmp(dividend, 0);
3316 __ Csel(out, temp, dividend, lt);
3317 if (imm > 0) {
3318 __ Asr(out, out, ctz_imm);
3319 } else {
3320 __ Neg(out, Operand(out, ASR, ctz_imm));
3321 }
3322 } else {
3323 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3324 __ Asr(temp, dividend, bits - 1);
3325 __ Lsr(temp, temp, bits - ctz_imm);
3326 __ Add(out, dividend, temp);
3327 __ And(out, out, abs_imm - 1);
3328 __ Sub(out, out, temp);
3329 }
3330}
3331
3332void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3333 DCHECK(instruction->IsDiv() || instruction->IsRem());
3334
3335 LocationSummary* locations = instruction->GetLocations();
3336 Location second = locations->InAt(1);
3337 DCHECK(second.IsConstant());
3338
3339 Register out = OutputRegister(instruction);
3340 Register dividend = InputRegisterAt(instruction, 0);
3341 int64_t imm = Int64FromConstant(second.GetConstant());
3342
3343 Primitive::Type type = instruction->GetResultType();
3344 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3345
3346 int64_t magic;
3347 int shift;
3348 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3349
3350 UseScratchRegisterScope temps(GetVIXLAssembler());
3351 Register temp = temps.AcquireSameSizeAs(out);
3352
3353 // temp = get_high(dividend * magic)
3354 __ Mov(temp, magic);
3355 if (type == Primitive::kPrimLong) {
3356 __ Smulh(temp, dividend, temp);
3357 } else {
3358 __ Smull(temp.X(), dividend, temp);
3359 __ Lsr(temp.X(), temp.X(), 32);
3360 }
3361
3362 if (imm > 0 && magic < 0) {
3363 __ Add(temp, temp, dividend);
3364 } else if (imm < 0 && magic > 0) {
3365 __ Sub(temp, temp, dividend);
3366 }
3367
3368 if (shift != 0) {
3369 __ Asr(temp, temp, shift);
3370 }
3371
3372 if (instruction->IsDiv()) {
3373 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3374 } else {
3375 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3376 // TODO: Strength reduction for msub.
3377 Register temp_imm = temps.AcquireSameSizeAs(out);
3378 __ Mov(temp_imm, imm);
3379 __ Msub(out, temp, temp_imm, dividend);
3380 }
3381}
3382
3383void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3384 DCHECK(instruction->IsDiv() || instruction->IsRem());
3385 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003386 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003387
3388 LocationSummary* locations = instruction->GetLocations();
3389 Register out = OutputRegister(instruction);
3390 Location second = locations->InAt(1);
3391
3392 if (second.IsConstant()) {
3393 int64_t imm = Int64FromConstant(second.GetConstant());
3394
3395 if (imm == 0) {
3396 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3397 } else if (imm == 1 || imm == -1) {
3398 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003399 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003400 DivRemByPowerOfTwo(instruction);
3401 } else {
3402 DCHECK(imm <= -2 || imm >= 2);
3403 GenerateDivRemWithAnyConstant(instruction);
3404 }
3405 } else {
3406 Register dividend = InputRegisterAt(instruction, 0);
3407 Register divisor = InputRegisterAt(instruction, 1);
3408 if (instruction->IsDiv()) {
3409 __ Sdiv(out, dividend, divisor);
3410 } else {
3411 UseScratchRegisterScope temps(GetVIXLAssembler());
3412 Register temp = temps.AcquireSameSizeAs(out);
3413 __ Sdiv(temp, dividend, divisor);
3414 __ Msub(out, temp, divisor, dividend);
3415 }
3416 }
3417}
3418
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003419void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3420 LocationSummary* locations =
3421 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3422 switch (div->GetResultType()) {
3423 case Primitive::kPrimInt:
3424 case Primitive::kPrimLong:
3425 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003426 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3428 break;
3429
3430 case Primitive::kPrimFloat:
3431 case Primitive::kPrimDouble:
3432 locations->SetInAt(0, Location::RequiresFpuRegister());
3433 locations->SetInAt(1, Location::RequiresFpuRegister());
3434 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3435 break;
3436
3437 default:
3438 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3439 }
3440}
3441
3442void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3443 Primitive::Type type = div->GetResultType();
3444 switch (type) {
3445 case Primitive::kPrimInt:
3446 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003447 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003448 break;
3449
3450 case Primitive::kPrimFloat:
3451 case Primitive::kPrimDouble:
3452 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3453 break;
3454
3455 default:
3456 LOG(FATAL) << "Unexpected div type " << type;
3457 }
3458}
3459
Alexandre Rames67555f72014-11-18 10:55:16 +00003460void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003461 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003462 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003463}
3464
3465void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3466 SlowPathCodeARM64* slow_path =
3467 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3468 codegen_->AddSlowPath(slow_path);
3469 Location value = instruction->GetLocations()->InAt(0);
3470
Alexandre Rames3e69f162014-12-10 10:36:50 +00003471 Primitive::Type type = instruction->GetType();
3472
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003473 if (!Primitive::IsIntegralType(type)) {
3474 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003475 return;
3476 }
3477
Alexandre Rames67555f72014-11-18 10:55:16 +00003478 if (value.IsConstant()) {
3479 int64_t divisor = Int64ConstantFrom(value);
3480 if (divisor == 0) {
3481 __ B(slow_path->GetEntryLabel());
3482 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003483 // A division by a non-null constant is valid. We don't need to perform
3484 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003485 }
3486 } else {
3487 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3488 }
3489}
3490
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003491void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3492 LocationSummary* locations =
3493 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3494 locations->SetOut(Location::ConstantLocation(constant));
3495}
3496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003497void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3498 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003499 // Will be generated at use site.
3500}
3501
Alexandre Rames5319def2014-10-23 10:03:10 +01003502void LocationsBuilderARM64::VisitExit(HExit* exit) {
3503 exit->SetLocations(nullptr);
3504}
3505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003506void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003507}
3508
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003509void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3510 LocationSummary* locations =
3511 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3512 locations->SetOut(Location::ConstantLocation(constant));
3513}
3514
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003515void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003516 // Will be generated at use site.
3517}
3518
David Brazdilfc6a86a2015-06-26 10:33:45 +00003519void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003520 DCHECK(!successor->IsExitBlock());
3521 HBasicBlock* block = got->GetBlock();
3522 HInstruction* previous = got->GetPrevious();
3523 HLoopInformation* info = block->GetLoopInformation();
3524
David Brazdil46e2a392015-03-16 17:31:52 +00003525 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003526 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3527 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3528 return;
3529 }
3530 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3531 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3532 }
3533 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003534 __ B(codegen_->GetLabelOf(successor));
3535 }
3536}
3537
David Brazdilfc6a86a2015-06-26 10:33:45 +00003538void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3539 got->SetLocations(nullptr);
3540}
3541
3542void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3543 HandleGoto(got, got->GetSuccessor());
3544}
3545
3546void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3547 try_boundary->SetLocations(nullptr);
3548}
3549
3550void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3551 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3552 if (!successor->IsExitBlock()) {
3553 HandleGoto(try_boundary, successor);
3554 }
3555}
3556
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003557void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003558 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003559 vixl::aarch64::Label* true_target,
3560 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003561 // FP branching requires both targets to be explicit. If either of the targets
3562 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003563 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003564 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003565
David Brazdil0debae72015-11-12 18:37:00 +00003566 if (true_target == nullptr && false_target == nullptr) {
3567 // Nothing to do. The code always falls through.
3568 return;
3569 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003570 // Constant condition, statically compared against "true" (integer value 1).
3571 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (true_target != nullptr) {
3573 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003574 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003575 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003576 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003577 if (false_target != nullptr) {
3578 __ B(false_target);
3579 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003580 }
David Brazdil0debae72015-11-12 18:37:00 +00003581 return;
3582 }
3583
3584 // The following code generates these patterns:
3585 // (1) true_target == nullptr && false_target != nullptr
3586 // - opposite condition true => branch to false_target
3587 // (2) true_target != nullptr && false_target == nullptr
3588 // - condition true => branch to true_target
3589 // (3) true_target != nullptr && false_target != nullptr
3590 // - condition true => branch to true_target
3591 // - branch to false_target
3592 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003593 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003594 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003595 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003596 if (true_target == nullptr) {
3597 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3598 } else {
3599 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3600 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003601 } else {
3602 // The condition instruction has not been materialized, use its inputs as
3603 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003604 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003605
David Brazdil0debae72015-11-12 18:37:00 +00003606 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003607 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003608 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003609 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003610 IfCondition opposite_condition = condition->GetOppositeCondition();
3611 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003612 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003613 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003614 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003615 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003616 // Integer cases.
3617 Register lhs = InputRegisterAt(condition, 0);
3618 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003619
3620 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003621 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003622 if (true_target == nullptr) {
3623 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3624 non_fallthrough_target = false_target;
3625 } else {
3626 arm64_cond = ARM64Condition(condition->GetCondition());
3627 non_fallthrough_target = true_target;
3628 }
3629
Aart Bik086d27e2016-01-20 17:02:00 -08003630 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003631 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003632 switch (arm64_cond) {
3633 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003634 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003635 break;
3636 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003637 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003638 break;
3639 case lt:
3640 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003641 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003642 break;
3643 case ge:
3644 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003645 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003646 break;
3647 default:
3648 // Without the `static_cast` the compiler throws an error for
3649 // `-Werror=sign-promo`.
3650 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3651 }
3652 } else {
3653 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003654 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003655 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003656 }
3657 }
David Brazdil0debae72015-11-12 18:37:00 +00003658
3659 // If neither branch falls through (case 3), the conditional branch to `true_target`
3660 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3661 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003662 __ B(false_target);
3663 }
David Brazdil0debae72015-11-12 18:37:00 +00003664
3665 if (fallthrough_target.IsLinked()) {
3666 __ Bind(&fallthrough_target);
3667 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003668}
3669
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003670void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3671 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003672 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003673 locations->SetInAt(0, Location::RequiresRegister());
3674 }
3675}
3676
3677void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003678 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3679 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003680 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3681 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3682 true_target = nullptr;
3683 }
3684 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3685 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3686 false_target = nullptr;
3687 }
David Brazdil0debae72015-11-12 18:37:00 +00003688 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003689}
3690
3691void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3692 LocationSummary* locations = new (GetGraph()->GetArena())
3693 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003694 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003695 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003696 locations->SetInAt(0, Location::RequiresRegister());
3697 }
3698}
3699
3700void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003701 SlowPathCodeARM64* slow_path =
3702 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003703 GenerateTestAndBranch(deoptimize,
3704 /* condition_input_index */ 0,
3705 slow_path->GetEntryLabel(),
3706 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003707}
3708
Mingyao Yang063fc772016-08-02 11:02:54 -07003709void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3710 LocationSummary* locations = new (GetGraph()->GetArena())
3711 LocationSummary(flag, LocationSummary::kNoCall);
3712 locations->SetOut(Location::RequiresRegister());
3713}
3714
3715void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3716 __ Ldr(OutputRegister(flag),
3717 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3718}
3719
David Brazdilc0b601b2016-02-08 14:20:45 +00003720static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3721 return condition->IsCondition() &&
3722 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3723}
3724
Alexandre Rames880f1192016-06-13 16:04:50 +01003725static inline Condition GetConditionForSelect(HCondition* condition) {
3726 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003727 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3728 : ARM64Condition(cond);
3729}
3730
David Brazdil74eb1b22015-12-14 11:44:01 +00003731void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003733 if (Primitive::IsFloatingPointType(select->GetType())) {
3734 locations->SetInAt(0, Location::RequiresFpuRegister());
3735 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003736 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003737 } else {
3738 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3739 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3740 bool is_true_value_constant = cst_true_value != nullptr;
3741 bool is_false_value_constant = cst_false_value != nullptr;
3742 // Ask VIXL whether we should synthesize constants in registers.
3743 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3744 Operand true_op = is_true_value_constant ?
3745 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3746 Operand false_op = is_false_value_constant ?
3747 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3748 bool true_value_in_register = false;
3749 bool false_value_in_register = false;
3750 MacroAssembler::GetCselSynthesisInformation(
3751 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3752 true_value_in_register |= !is_true_value_constant;
3753 false_value_in_register |= !is_false_value_constant;
3754
3755 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3756 : Location::ConstantLocation(cst_true_value));
3757 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3758 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003760 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003761
David Brazdil74eb1b22015-12-14 11:44:01 +00003762 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3763 locations->SetInAt(2, Location::RequiresRegister());
3764 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003765}
3766
3767void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003768 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003769 Condition csel_cond;
3770
3771 if (IsBooleanValueOrMaterializedCondition(cond)) {
3772 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003773 // Use the condition flags set by the previous instruction.
3774 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003775 } else {
3776 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003777 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003778 }
3779 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003780 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003781 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003782 } else {
3783 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003784 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003785 }
3786
Alexandre Rames880f1192016-06-13 16:04:50 +01003787 if (Primitive::IsFloatingPointType(select->GetType())) {
3788 __ Fcsel(OutputFPRegister(select),
3789 InputFPRegisterAt(select, 1),
3790 InputFPRegisterAt(select, 0),
3791 csel_cond);
3792 } else {
3793 __ Csel(OutputRegister(select),
3794 InputOperandAt(select, 1),
3795 InputOperandAt(select, 0),
3796 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003797 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003798}
3799
David Srbecky0cf44932015-12-09 14:09:59 +00003800void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3801 new (GetGraph()->GetArena()) LocationSummary(info);
3802}
3803
David Srbeckyd28f4a02016-03-14 17:14:24 +00003804void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3805 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003806}
3807
3808void CodeGeneratorARM64::GenerateNop() {
3809 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003810}
3811
Alexandre Rames5319def2014-10-23 10:03:10 +01003812void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003813 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003814}
3815
3816void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003817 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003818}
3819
3820void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003821 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003822}
3823
3824void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003825 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003826}
3827
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003828// Temp is used for read barrier.
3829static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3830 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003831 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003832 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3833 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3834 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3835 return 1;
3836 }
3837 return 0;
3838}
3839
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003840// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003841// interface pointer, one for loading the current interface.
3842// The other checks have one temp for loading the object's class.
3843static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3844 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3845 return 3;
3846 }
3847 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003848}
3849
Alexandre Rames67555f72014-11-18 10:55:16 +00003850void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003851 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003852 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003853 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003854 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003855 case TypeCheckKind::kExactCheck:
3856 case TypeCheckKind::kAbstractClassCheck:
3857 case TypeCheckKind::kClassHierarchyCheck:
3858 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859 call_kind =
3860 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003861 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003862 break;
3863 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864 case TypeCheckKind::kUnresolvedCheck:
3865 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003866 call_kind = LocationSummary::kCallOnSlowPath;
3867 break;
3868 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869
Alexandre Rames67555f72014-11-18 10:55:16 +00003870 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003871 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003872 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003873 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003874 locations->SetInAt(0, Location::RequiresRegister());
3875 locations->SetInAt(1, Location::RequiresRegister());
3876 // The "out" register is used as a temporary, so it overlaps with the inputs.
3877 // Note that TypeCheckSlowPathARM64 uses this register too.
3878 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003879 // Add temps if necessary for read barriers.
3880 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003881}
3882
3883void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003884 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003885 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003886 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003887 Register obj = InputRegisterAt(instruction, 0);
3888 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003889 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003890 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003891 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3892 DCHECK_LE(num_temps, 1u);
3893 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003894 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3895 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3896 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3897 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003898
Scott Wakeling97c72b72016-06-24 16:19:36 +01003899 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003901
3902 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003903 // Avoid null check if we know `obj` is not null.
3904 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003905 __ Cbz(obj, &zero);
3906 }
3907
Roland Levillain44015862016-01-22 11:47:17 +00003908 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003909 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003910 // /* HeapReference<Class> */ out = obj->klass_
3911 GenerateReferenceLoadTwoRegisters(instruction,
3912 out_loc,
3913 obj_loc,
3914 class_offset,
3915 maybe_temp_loc,
3916 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003917 __ Cmp(out, cls);
3918 __ Cset(out, eq);
3919 if (zero.IsLinked()) {
3920 __ B(&done);
3921 }
3922 break;
3923 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003924
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003925 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003926 // /* HeapReference<Class> */ out = obj->klass_
3927 GenerateReferenceLoadTwoRegisters(instruction,
3928 out_loc,
3929 obj_loc,
3930 class_offset,
3931 maybe_temp_loc,
3932 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003933 // If the class is abstract, we eagerly fetch the super class of the
3934 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003935 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003936 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003937 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003938 GenerateReferenceLoadOneRegister(instruction,
3939 out_loc,
3940 super_offset,
3941 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003942 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003943 // If `out` is null, we use it for the result, and jump to `done`.
3944 __ Cbz(out, &done);
3945 __ Cmp(out, cls);
3946 __ B(ne, &loop);
3947 __ Mov(out, 1);
3948 if (zero.IsLinked()) {
3949 __ B(&done);
3950 }
3951 break;
3952 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003953
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003954 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003955 // /* HeapReference<Class> */ out = obj->klass_
3956 GenerateReferenceLoadTwoRegisters(instruction,
3957 out_loc,
3958 obj_loc,
3959 class_offset,
3960 maybe_temp_loc,
3961 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003962 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003963 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003964 __ Bind(&loop);
3965 __ Cmp(out, cls);
3966 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003967 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003968 GenerateReferenceLoadOneRegister(instruction,
3969 out_loc,
3970 super_offset,
3971 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003972 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003973 __ Cbnz(out, &loop);
3974 // If `out` is null, we use it for the result, and jump to `done`.
3975 __ B(&done);
3976 __ Bind(&success);
3977 __ Mov(out, 1);
3978 if (zero.IsLinked()) {
3979 __ B(&done);
3980 }
3981 break;
3982 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003983
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003984 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003985 // /* HeapReference<Class> */ out = obj->klass_
3986 GenerateReferenceLoadTwoRegisters(instruction,
3987 out_loc,
3988 obj_loc,
3989 class_offset,
3990 maybe_temp_loc,
3991 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003992 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003993 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003994 __ Cmp(out, cls);
3995 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003996 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003997 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003998 GenerateReferenceLoadOneRegister(instruction,
3999 out_loc,
4000 component_offset,
4001 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004002 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004003 // If `out` is null, we use it for the result, and jump to `done`.
4004 __ Cbz(out, &done);
4005 __ Ldrh(out, HeapOperand(out, primitive_offset));
4006 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4007 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004008 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004009 __ Mov(out, 1);
4010 __ B(&done);
4011 break;
4012 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004014 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004015 // No read barrier since the slow path will retry upon failure.
4016 // /* HeapReference<Class> */ out = obj->klass_
4017 GenerateReferenceLoadTwoRegisters(instruction,
4018 out_loc,
4019 obj_loc,
4020 class_offset,
4021 maybe_temp_loc,
4022 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004023 __ Cmp(out, cls);
4024 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004025 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4026 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004027 codegen_->AddSlowPath(slow_path);
4028 __ B(ne, slow_path->GetEntryLabel());
4029 __ Mov(out, 1);
4030 if (zero.IsLinked()) {
4031 __ B(&done);
4032 }
4033 break;
4034 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004035
Calin Juravle98893e12015-10-02 21:05:03 +01004036 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004037 case TypeCheckKind::kInterfaceCheck: {
4038 // Note that we indeed only call on slow path, but we always go
4039 // into the slow path for the unresolved and interface check
4040 // cases.
4041 //
4042 // We cannot directly call the InstanceofNonTrivial runtime
4043 // entry point without resorting to a type checking slow path
4044 // here (i.e. by calling InvokeRuntime directly), as it would
4045 // require to assign fixed registers for the inputs of this
4046 // HInstanceOf instruction (following the runtime calling
4047 // convention), which might be cluttered by the potential first
4048 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004049 //
4050 // TODO: Introduce a new runtime entry point taking the object
4051 // to test (instead of its class) as argument, and let it deal
4052 // with the read barrier issues. This will let us refactor this
4053 // case of the `switch` code as it was previously (with a direct
4054 // call to the runtime not using a type checking slow path).
4055 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004056 DCHECK(locations->OnlyCallsOnSlowPath());
4057 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4058 /* is_fatal */ false);
4059 codegen_->AddSlowPath(slow_path);
4060 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004061 if (zero.IsLinked()) {
4062 __ B(&done);
4063 }
4064 break;
4065 }
4066 }
4067
4068 if (zero.IsLinked()) {
4069 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004070 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004071 }
4072
4073 if (done.IsLinked()) {
4074 __ Bind(&done);
4075 }
4076
4077 if (slow_path != nullptr) {
4078 __ Bind(slow_path->GetExitLabel());
4079 }
4080}
4081
4082void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4083 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4084 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4085
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004086 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4087 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004088 case TypeCheckKind::kExactCheck:
4089 case TypeCheckKind::kAbstractClassCheck:
4090 case TypeCheckKind::kClassHierarchyCheck:
4091 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4093 LocationSummary::kCallOnSlowPath :
4094 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004095 break;
4096 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004097 case TypeCheckKind::kUnresolvedCheck:
4098 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004099 call_kind = LocationSummary::kCallOnSlowPath;
4100 break;
4101 }
4102
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004103 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4104 locations->SetInAt(0, Location::RequiresRegister());
4105 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004106 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4107 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004108}
4109
4110void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004111 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004112 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004113 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004114 Register obj = InputRegisterAt(instruction, 0);
4115 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004116 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4117 DCHECK_GE(num_temps, 1u);
4118 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004119 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004120 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4121 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004122 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004123 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4124 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4125 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4126 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4127 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4128 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4129 const uint32_t object_array_data_offset =
4130 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004131
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004132 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004133 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4134 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4135 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004136 if (!kEmitCompilerReadBarrier) {
4137 is_type_check_slow_path_fatal =
4138 (type_check_kind == TypeCheckKind::kExactCheck ||
4139 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4140 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4141 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4142 !instruction->CanThrowIntoCatchBlock();
4143 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004144 SlowPathCodeARM64* type_check_slow_path =
4145 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4146 is_type_check_slow_path_fatal);
4147 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004148
Scott Wakeling97c72b72016-06-24 16:19:36 +01004149 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004150 // Avoid null check if we know obj is not null.
4151 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004152 __ Cbz(obj, &done);
4153 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004154
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004155 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004156 case TypeCheckKind::kExactCheck:
4157 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004158 // /* HeapReference<Class> */ temp = obj->klass_
4159 GenerateReferenceLoadTwoRegisters(instruction,
4160 temp_loc,
4161 obj_loc,
4162 class_offset,
4163 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004164 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004166 __ Cmp(temp, cls);
4167 // Jump to slow path for throwing the exception or doing a
4168 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004169 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004170 break;
4171 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004172
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004173 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004174 // /* HeapReference<Class> */ temp = obj->klass_
4175 GenerateReferenceLoadTwoRegisters(instruction,
4176 temp_loc,
4177 obj_loc,
4178 class_offset,
4179 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004180 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004182 // If the class is abstract, we eagerly fetch the super class of the
4183 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004184 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004185 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004186 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004187 GenerateReferenceLoadOneRegister(instruction,
4188 temp_loc,
4189 super_offset,
4190 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004191 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004192
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004193 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4194 // exception.
4195 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4196 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004197 __ Cmp(temp, cls);
4198 __ B(ne, &loop);
4199 break;
4200 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004202 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004203 // /* HeapReference<Class> */ temp = obj->klass_
4204 GenerateReferenceLoadTwoRegisters(instruction,
4205 temp_loc,
4206 obj_loc,
4207 class_offset,
4208 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004209 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004211 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004212 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004213 __ Bind(&loop);
4214 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004215 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004216
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004217 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004218 GenerateReferenceLoadOneRegister(instruction,
4219 temp_loc,
4220 super_offset,
4221 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004222 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223
4224 // If the class reference currently in `temp` is not null, jump
4225 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004226 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004227 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004228 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004229 break;
4230 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004231
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004232 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004233 // /* HeapReference<Class> */ temp = obj->klass_
4234 GenerateReferenceLoadTwoRegisters(instruction,
4235 temp_loc,
4236 obj_loc,
4237 class_offset,
4238 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004239 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004240
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004241 // Do an exact check.
4242 __ Cmp(temp, cls);
4243 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004244
4245 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004246 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004247 GenerateReferenceLoadOneRegister(instruction,
4248 temp_loc,
4249 component_offset,
4250 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004251 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004252
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004253 // If the component type is null, jump to the slow path to throw the exception.
4254 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4255 // Otherwise, the object is indeed an array. Further check that this component type is not a
4256 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004257 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4258 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004259 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004260 break;
4261 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004262
Calin Juravle98893e12015-10-02 21:05:03 +01004263 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004264 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004265 //
4266 // We cannot directly call the CheckCast runtime entry point
4267 // without resorting to a type checking slow path here (i.e. by
4268 // calling InvokeRuntime directly), as it would require to
4269 // assign fixed registers for the inputs of this HInstanceOf
4270 // instruction (following the runtime calling convention), which
4271 // might be cluttered by the potential first read barrier
4272 // emission at the beginning of this method.
4273 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004274 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004275 case TypeCheckKind::kInterfaceCheck: {
4276 // /* HeapReference<Class> */ temp = obj->klass_
4277 GenerateReferenceLoadTwoRegisters(instruction,
4278 temp_loc,
4279 obj_loc,
4280 class_offset,
4281 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004282 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004283
4284 // /* HeapReference<Class> */ temp = temp->iftable_
4285 GenerateReferenceLoadTwoRegisters(instruction,
4286 temp_loc,
4287 temp_loc,
4288 iftable_offset,
4289 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004290 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004291 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004293 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294 vixl::aarch64::Label start_loop;
4295 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004296 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004297 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4298 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004299 // Go to next interface.
4300 __ Add(temp, temp, 2 * kHeapReferenceSize);
4301 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004302 // Compare the classes and continue the loop if they do not match.
4303 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4304 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004305 break;
4306 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004307 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004308 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004309
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004310 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004311}
4312
Alexandre Rames5319def2014-10-23 10:03:10 +01004313void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4315 locations->SetOut(Location::ConstantLocation(constant));
4316}
4317
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004318void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004319 // Will be generated at use site.
4320}
4321
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004322void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4323 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4324 locations->SetOut(Location::ConstantLocation(constant));
4325}
4326
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004327void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004328 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004329}
4330
Calin Juravle175dc732015-08-25 15:42:32 +01004331void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4332 // The trampoline uses the same calling convention as dex calling conventions,
4333 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4334 // the method_idx.
4335 HandleInvoke(invoke);
4336}
4337
4338void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4339 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4340}
4341
Alexandre Rames5319def2014-10-23 10:03:10 +01004342void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004343 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004344 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004345}
4346
Alexandre Rames67555f72014-11-18 10:55:16 +00004347void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4348 HandleInvoke(invoke);
4349}
4350
4351void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4352 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004353 LocationSummary* locations = invoke->GetLocations();
4354 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004355 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004356 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004357 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004358
4359 // The register ip1 is required to be used for the hidden argument in
4360 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004361 MacroAssembler* masm = GetVIXLAssembler();
4362 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004363 scratch_scope.Exclude(ip1);
4364 __ Mov(ip1, invoke->GetDexMethodIndex());
4365
Artem Serov914d7a82017-02-07 14:33:49 +00004366 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004367 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004368 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004369 {
4370 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4371 // /* HeapReference<Class> */ temp = temp->klass_
4372 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4373 codegen_->MaybeRecordImplicitNullCheck(invoke);
4374 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004375 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004376 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004377 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004378 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004379 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004380 }
Artem Serov914d7a82017-02-07 14:33:49 +00004381
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004382 // Instead of simply (possibly) unpoisoning `temp` here, we should
4383 // emit a read barrier for the previous class reference load.
4384 // However this is not required in practice, as this is an
4385 // intermediate/temporary reference and because the current
4386 // concurrent copying collector keeps the from-space memory
4387 // intact/accessible until the end of the marking phase (the
4388 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004389 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004390 __ Ldr(temp,
4391 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4392 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004393 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004394 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004395 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004396 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004397 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004398
4399 {
4400 // Ensure the pc position is recorded immediately after the `blr` instruction.
4401 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4402
4403 // lr();
4404 __ blr(lr);
4405 DCHECK(!codegen_->IsLeafMethod());
4406 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4407 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004408}
4409
4410void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004411 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004412 if (intrinsic.TryDispatch(invoke)) {
4413 return;
4414 }
4415
Alexandre Rames67555f72014-11-18 10:55:16 +00004416 HandleInvoke(invoke);
4417}
4418
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004419void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004420 // Explicit clinit checks triggered by static invokes must have been pruned by
4421 // art::PrepareForRegisterAllocation.
4422 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004423
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004424 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004425 if (intrinsic.TryDispatch(invoke)) {
4426 return;
4427 }
4428
Alexandre Rames67555f72014-11-18 10:55:16 +00004429 HandleInvoke(invoke);
4430}
4431
Andreas Gampe878d58c2015-01-15 23:24:00 -08004432static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4433 if (invoke->GetLocations()->Intrinsified()) {
4434 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4435 intrinsic.Dispatch(invoke);
4436 return true;
4437 }
4438 return false;
4439}
4440
Vladimir Markodc151b22015-10-15 18:02:30 +01004441HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4442 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004443 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004444 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004445 return desired_dispatch_info;
4446}
4447
TatWai Chongd8c052a2016-11-02 16:12:48 +08004448Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4449 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004450 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004451 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4452 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004453 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4454 uint32_t offset =
4455 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004456 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004457 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004458 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004459 }
Vladimir Marko58155012015-08-19 12:49:41 +00004460 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004461 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004462 break;
4463 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4464 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004465 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004466 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004467 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4468 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004469 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004470 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004471 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004472 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004473 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004474 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004475 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004476 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004477 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004478 }
Vladimir Marko58155012015-08-19 12:49:41 +00004479 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004480 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004481 Register reg = XRegisterFrom(temp);
4482 Register method_reg;
4483 if (current_method.IsRegister()) {
4484 method_reg = XRegisterFrom(current_method);
4485 } else {
4486 DCHECK(invoke->GetLocations()->Intrinsified());
4487 DCHECK(!current_method.IsValid());
4488 method_reg = reg;
4489 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4490 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004491
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004492 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004493 __ Ldr(reg.X(),
4494 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004495 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004496 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004497 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4498 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004499 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4500 break;
4501 }
4502 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004503 return callee_method;
4504}
4505
4506void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4507 // All registers are assumed to be correctly set up.
4508 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004509
4510 switch (invoke->GetCodePtrLocation()) {
4511 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4512 __ Bl(&frame_entry_label_);
4513 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004514 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4515 // LR = callee_method->entry_point_from_quick_compiled_code_;
4516 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004517 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004518 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004519 {
4520 // To ensure that the pc position is recorded immediately after the `blr` instruction
4521 // BLR must be the last instruction emitted in this function.
4522 // Recording the pc will occur right after returning from this function.
4523 ExactAssemblyScope eas(GetVIXLAssembler(),
4524 kInstructionSize,
4525 CodeBufferCheckScope::kExactSize);
4526 // lr()
4527 __ blr(lr);
4528 }
Vladimir Marko58155012015-08-19 12:49:41 +00004529 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004530 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004531
Andreas Gampe878d58c2015-01-15 23:24:00 -08004532 DCHECK(!IsLeafMethod());
4533}
4534
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004535void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004536 // Use the calling convention instead of the location of the receiver, as
4537 // intrinsics may have put the receiver in a different register. In the intrinsics
4538 // slow path, the arguments have been moved to the right place, so here we are
4539 // guaranteed that the receiver is the first register of the calling convention.
4540 InvokeDexCallingConvention calling_convention;
4541 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004542 Register temp = XRegisterFrom(temp_in);
4543 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4544 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4545 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004546 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004547
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004548 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004549
4550 {
4551 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4552 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4553 // /* HeapReference<Class> */ temp = receiver->klass_
4554 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4555 MaybeRecordImplicitNullCheck(invoke);
4556 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004557 // Instead of simply (possibly) unpoisoning `temp` here, we should
4558 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004559 // intermediate/temporary reference and because the current
4560 // concurrent copying collector keeps the from-space memory
4561 // intact/accessible until the end of the marking phase (the
4562 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004563 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4564 // temp = temp->GetMethodAt(method_offset);
4565 __ Ldr(temp, MemOperand(temp, method_offset));
4566 // lr = temp->GetEntryPoint();
4567 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004568 {
4569 // To ensure that the pc position is recorded immediately after the `blr` instruction
4570 // BLR should be the last instruction emitted in this function.
4571 // Recording the pc will occur right after returning from this function.
4572 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4573 // lr();
4574 __ blr(lr);
4575 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004576}
4577
Orion Hodsonac141392017-01-13 11:53:47 +00004578void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4579 HandleInvoke(invoke);
4580}
4581
4582void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4583 codegen_->GenerateInvokePolymorphicCall(invoke);
4584}
4585
Scott Wakeling97c72b72016-06-24 16:19:36 +01004586vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4587 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004588 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004589 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004590 return
4591 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004592}
4593
Scott Wakeling97c72b72016-06-24 16:19:36 +01004594vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4595 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004596 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004597 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004598 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004599}
4600
Vladimir Marko1998cd02017-01-13 13:02:58 +00004601vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4602 const DexFile& dex_file,
4603 dex::TypeIndex type_index,
4604 vixl::aarch64::Label* adrp_label) {
4605 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4606}
4607
Scott Wakeling97c72b72016-06-24 16:19:36 +01004608vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4609 const DexFile& dex_file,
4610 uint32_t element_offset,
4611 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004612 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4613}
4614
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004615vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4616 baker_read_barrier_patches_.emplace_back(custom_data);
4617 return &baker_read_barrier_patches_.back().label;
4618}
4619
Scott Wakeling97c72b72016-06-24 16:19:36 +01004620vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4621 const DexFile& dex_file,
4622 uint32_t offset_or_index,
4623 vixl::aarch64::Label* adrp_label,
4624 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004625 // Add a patch entry and return the label.
4626 patches->emplace_back(dex_file, offset_or_index);
4627 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004628 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004629 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4630 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4631 return label;
4632}
4633
Scott Wakeling97c72b72016-06-24 16:19:36 +01004634vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004635 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004636 return boot_image_string_patches_.GetOrCreate(
4637 StringReference(&dex_file, string_index),
4638 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4639}
4640
Scott Wakeling97c72b72016-06-24 16:19:36 +01004641vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004642 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004643 return boot_image_type_patches_.GetOrCreate(
4644 TypeReference(&dex_file, type_index),
4645 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4646}
4647
Scott Wakeling97c72b72016-06-24 16:19:36 +01004648vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4649 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004650 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004651}
4652
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004653vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004654 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4655 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4656 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004657 return jit_string_patches_.GetOrCreate(
4658 StringReference(&dex_file, string_index),
4659 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4660}
4661
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004662vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004663 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4664 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4665 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004666 return jit_class_patches_.GetOrCreate(
4667 TypeReference(&dex_file, type_index),
4668 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4669}
4670
Vladimir Markoaad75c62016-10-03 08:46:48 +00004671void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4672 vixl::aarch64::Register reg) {
4673 DCHECK(reg.IsX());
4674 SingleEmissionCheckScope guard(GetVIXLAssembler());
4675 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004676 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677}
4678
4679void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4680 vixl::aarch64::Register out,
4681 vixl::aarch64::Register base) {
4682 DCHECK(out.IsX());
4683 DCHECK(base.IsX());
4684 SingleEmissionCheckScope guard(GetVIXLAssembler());
4685 __ Bind(fixup_label);
4686 __ add(out, base, Operand(/* offset placeholder */ 0));
4687}
4688
4689void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4690 vixl::aarch64::Register out,
4691 vixl::aarch64::Register base) {
4692 DCHECK(base.IsX());
4693 SingleEmissionCheckScope guard(GetVIXLAssembler());
4694 __ Bind(fixup_label);
4695 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4696}
4697
4698template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4699inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4700 const ArenaDeque<PcRelativePatchInfo>& infos,
4701 ArenaVector<LinkerPatch>* linker_patches) {
4702 for (const PcRelativePatchInfo& info : infos) {
4703 linker_patches->push_back(Factory(info.label.GetLocation(),
4704 &info.target_dex_file,
4705 info.pc_insn_label->GetLocation(),
4706 info.offset_or_index));
4707 }
4708}
4709
Vladimir Marko58155012015-08-19 12:49:41 +00004710void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4711 DCHECK(linker_patches->empty());
4712 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004713 pc_relative_dex_cache_patches_.size() +
4714 boot_image_string_patches_.size() +
4715 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004716 boot_image_type_patches_.size() +
4717 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004718 type_bss_entry_patches_.size() +
4719 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004720 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004721 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004722 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004723 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004724 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004725 info.offset_or_index));
4726 }
4727 for (const auto& entry : boot_image_string_patches_) {
4728 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004729 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4730 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004731 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004732 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004733 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004734 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004735 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4737 linker_patches);
4738 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004739 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4740 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004741 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4742 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004743 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004744 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4745 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004746 for (const auto& entry : boot_image_type_patches_) {
4747 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004748 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4749 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004750 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004751 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004752 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004753 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4754 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4755 info.custom_data));
4756 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004757 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004758}
4759
Scott Wakeling97c72b72016-06-24 16:19:36 +01004760vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004761 Uint32ToLiteralMap* map) {
4762 return map->GetOrCreate(
4763 value,
4764 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4765}
4766
Scott Wakeling97c72b72016-06-24 16:19:36 +01004767vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004768 return uint64_literals_.GetOrCreate(
4769 value,
4770 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004771}
4772
Scott Wakeling97c72b72016-06-24 16:19:36 +01004773vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004774 MethodReference target_method,
4775 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004776 return map->GetOrCreate(
4777 target_method,
4778 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004779}
4780
Andreas Gampe878d58c2015-01-15 23:24:00 -08004781void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004782 // Explicit clinit checks triggered by static invokes must have been pruned by
4783 // art::PrepareForRegisterAllocation.
4784 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004785
Andreas Gampe878d58c2015-01-15 23:24:00 -08004786 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4787 return;
4788 }
4789
Artem Serov914d7a82017-02-07 14:33:49 +00004790 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4791 // are no pools emitted.
4792 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004793 LocationSummary* locations = invoke->GetLocations();
4794 codegen_->GenerateStaticOrDirectCall(
4795 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004796 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004797}
4798
4799void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004800 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4801 return;
4802 }
4803
Artem Serov914d7a82017-02-07 14:33:49 +00004804 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4805 // are no pools emitted.
4806 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004807 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004808 DCHECK(!codegen_->IsLeafMethod());
4809 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4810}
4811
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004812HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4813 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004814 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004815 case HLoadClass::LoadKind::kInvalid:
4816 LOG(FATAL) << "UNREACHABLE";
4817 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004818 case HLoadClass::LoadKind::kReferrersClass:
4819 break;
4820 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4821 DCHECK(!GetCompilerOptions().GetCompilePic());
4822 break;
4823 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4824 DCHECK(GetCompilerOptions().GetCompilePic());
4825 break;
4826 case HLoadClass::LoadKind::kBootImageAddress:
4827 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004828 case HLoadClass::LoadKind::kBssEntry:
4829 DCHECK(!Runtime::Current()->UseJitCompilation());
4830 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004831 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004832 DCHECK(Runtime::Current()->UseJitCompilation());
4833 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004834 case HLoadClass::LoadKind::kDexCacheViaMethod:
4835 break;
4836 }
4837 return desired_class_load_kind;
4838}
4839
Alexandre Rames67555f72014-11-18 10:55:16 +00004840void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004841 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4842 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004843 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004844 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004845 cls,
4846 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004847 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004848 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004849 return;
4850 }
Vladimir Marko41559982017-01-06 14:04:23 +00004851 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004852
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004853 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4854 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004855 ? LocationSummary::kCallOnSlowPath
4856 : LocationSummary::kNoCall;
4857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004858 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004859 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004860 }
4861
Vladimir Marko41559982017-01-06 14:04:23 +00004862 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004863 locations->SetInAt(0, Location::RequiresRegister());
4864 }
4865 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004866 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4867 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4868 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004869 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004870 RegisterSet caller_saves = RegisterSet::Empty();
4871 InvokeRuntimeCallingConvention calling_convention;
4872 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4873 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4874 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4875 Primitive::kPrimNot).GetCode());
4876 locations->SetCustomSlowPathCallerSaves(caller_saves);
4877 } else {
4878 // For non-Baker read barrier we have a temp-clobbering call.
4879 }
4880 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004881}
4882
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004883// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4884// move.
4885void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004886 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4887 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4888 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004889 return;
4890 }
Vladimir Marko41559982017-01-06 14:04:23 +00004891 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004892
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004893 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004894 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004895 Register bss_entry_temp;
4896 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004897
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004898 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4899 ? kWithoutReadBarrier
4900 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004901 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004902 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004903 case HLoadClass::LoadKind::kReferrersClass: {
4904 DCHECK(!cls->CanCallRuntime());
4905 DCHECK(!cls->MustGenerateClinitCheck());
4906 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4907 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004908 GenerateGcRootFieldLoad(cls,
4909 out_loc,
4910 current_method,
4911 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004912 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004913 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004914 break;
4915 }
4916 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004917 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004918 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4919 cls->GetTypeIndex()));
4920 break;
4921 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004922 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004923 // Add ADRP with its PC-relative type patch.
4924 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004925 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004926 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004927 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004928 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004929 vixl::aarch64::Label* add_label =
4930 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004931 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004932 break;
4933 }
4934 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004935 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004936 uint32_t address = dchecked_integral_cast<uint32_t>(
4937 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4938 DCHECK_NE(address, 0u);
4939 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004940 break;
4941 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004942 case HLoadClass::LoadKind::kBssEntry: {
4943 // Add ADRP with its PC-relative Class .bss entry patch.
4944 const DexFile& dex_file = cls->GetDexFile();
4945 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004946 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004947 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4948 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004949 // Add LDR with its PC-relative Class patch.
4950 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004951 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004952 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4953 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004954 out_loc,
4955 bss_entry_temp,
4956 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004957 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004958 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004959 generate_null_check = true;
4960 break;
4961 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004962 case HLoadClass::LoadKind::kJitTableAddress: {
4963 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4964 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004965 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004966 GenerateGcRootFieldLoad(cls,
4967 out_loc,
4968 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004969 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004970 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004971 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004972 break;
4973 }
Vladimir Marko41559982017-01-06 14:04:23 +00004974 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004975 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004976 LOG(FATAL) << "UNREACHABLE";
4977 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004978 }
4979
Vladimir Markoea4c1262017-02-06 19:59:33 +00004980 bool do_clinit = cls->MustGenerateClinitCheck();
4981 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004982 DCHECK(cls->CanCallRuntime());
4983 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004984 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004985 codegen_->AddSlowPath(slow_path);
4986 if (generate_null_check) {
4987 __ Cbz(out, slow_path->GetEntryLabel());
4988 }
4989 if (cls->MustGenerateClinitCheck()) {
4990 GenerateClassInitializationCheck(slow_path, out);
4991 } else {
4992 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004993 }
4994 }
4995}
4996
David Brazdilcb1c0552015-08-04 16:22:25 +01004997static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004998 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004999}
5000
Alexandre Rames67555f72014-11-18 10:55:16 +00005001void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
5002 LocationSummary* locations =
5003 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5004 locations->SetOut(Location::RequiresRegister());
5005}
5006
5007void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005008 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5009}
5010
5011void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5012 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5013}
5014
5015void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5016 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005017}
5018
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005019HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5020 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005021 switch (desired_string_load_kind) {
5022 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5023 DCHECK(!GetCompilerOptions().GetCompilePic());
5024 break;
5025 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5026 DCHECK(GetCompilerOptions().GetCompilePic());
5027 break;
5028 case HLoadString::LoadKind::kBootImageAddress:
5029 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005030 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005031 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005032 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005033 case HLoadString::LoadKind::kJitTableAddress:
5034 DCHECK(Runtime::Current()->UseJitCompilation());
5035 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005036 case HLoadString::LoadKind::kDexCacheViaMethod:
5037 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005038 }
5039 return desired_string_load_kind;
5040}
5041
Alexandre Rames67555f72014-11-18 10:55:16 +00005042void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005043 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005045 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005046 InvokeRuntimeCallingConvention calling_convention;
5047 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5048 } else {
5049 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005050 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5051 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005052 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005053 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005054 RegisterSet caller_saves = RegisterSet::Empty();
5055 InvokeRuntimeCallingConvention calling_convention;
5056 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5057 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5058 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5059 Primitive::kPrimNot).GetCode());
5060 locations->SetCustomSlowPathCallerSaves(caller_saves);
5061 } else {
5062 // For non-Baker read barrier we have a temp-clobbering call.
5063 }
5064 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005065 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005066}
5067
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005068// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5069// move.
5070void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005071 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005072 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005073
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005074 switch (load->GetLoadKind()) {
5075 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005076 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5077 load->GetStringIndex()));
5078 return; // No dex cache slow path.
5079 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005080 // Add ADRP with its PC-relative String patch.
5081 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005082 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005083 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005084 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005085 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005086 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005087 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005088 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005089 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005090 return; // No dex cache slow path.
5091 }
5092 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005093 uint32_t address = dchecked_integral_cast<uint32_t>(
5094 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5095 DCHECK_NE(address, 0u);
5096 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005097 return; // No dex cache slow path.
5098 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005099 case HLoadString::LoadKind::kBssEntry: {
5100 // Add ADRP with its PC-relative String .bss entry patch.
5101 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005102 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005103 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005104 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005105 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005106 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005107 // Add LDR with its PC-relative String patch.
5108 vixl::aarch64::Label* ldr_label =
5109 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005110 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005111 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005112 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005113 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005114 /* offset placeholder */ 0u,
5115 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005116 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005117 SlowPathCodeARM64* slow_path =
5118 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005119 codegen_->AddSlowPath(slow_path);
5120 __ Cbz(out.X(), slow_path->GetEntryLabel());
5121 __ Bind(slow_path->GetExitLabel());
5122 return;
5123 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005124 case HLoadString::LoadKind::kJitTableAddress: {
5125 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005126 load->GetStringIndex(),
5127 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005128 GenerateGcRootFieldLoad(load,
5129 out_loc,
5130 out.X(),
5131 /* offset */ 0,
5132 /* fixup_label */ nullptr,
5133 kCompilerReadBarrierOption);
5134 return;
5135 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005136 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005137 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005138 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005139
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005140 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005141 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005142 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005143 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005144 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5145 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005146}
5147
Alexandre Rames5319def2014-10-23 10:03:10 +01005148void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5149 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5150 locations->SetOut(Location::ConstantLocation(constant));
5151}
5152
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005153void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005154 // Will be generated at use site.
5155}
5156
Alexandre Rames67555f72014-11-18 10:55:16 +00005157void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5158 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005160 InvokeRuntimeCallingConvention calling_convention;
5161 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5162}
5163
5164void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005165 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005166 instruction,
5167 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005168 if (instruction->IsEnter()) {
5169 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5170 } else {
5171 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5172 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005173}
5174
Alexandre Rames42d641b2014-10-27 14:00:51 +00005175void LocationsBuilderARM64::VisitMul(HMul* mul) {
5176 LocationSummary* locations =
5177 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5178 switch (mul->GetResultType()) {
5179 case Primitive::kPrimInt:
5180 case Primitive::kPrimLong:
5181 locations->SetInAt(0, Location::RequiresRegister());
5182 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005184 break;
5185
5186 case Primitive::kPrimFloat:
5187 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005188 locations->SetInAt(0, Location::RequiresFpuRegister());
5189 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005190 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005191 break;
5192
5193 default:
5194 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5195 }
5196}
5197
5198void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5199 switch (mul->GetResultType()) {
5200 case Primitive::kPrimInt:
5201 case Primitive::kPrimLong:
5202 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5203 break;
5204
5205 case Primitive::kPrimFloat:
5206 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005207 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005208 break;
5209
5210 default:
5211 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5212 }
5213}
5214
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005215void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5216 LocationSummary* locations =
5217 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5218 switch (neg->GetResultType()) {
5219 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005220 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005221 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005223 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005224
5225 case Primitive::kPrimFloat:
5226 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005227 locations->SetInAt(0, Location::RequiresFpuRegister());
5228 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005229 break;
5230
5231 default:
5232 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5233 }
5234}
5235
5236void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5237 switch (neg->GetResultType()) {
5238 case Primitive::kPrimInt:
5239 case Primitive::kPrimLong:
5240 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5241 break;
5242
5243 case Primitive::kPrimFloat:
5244 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005245 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005246 break;
5247
5248 default:
5249 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5250 }
5251}
5252
5253void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5254 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005256 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005257 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005258 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5259 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005260}
5261
5262void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005263 // Note: if heap poisoning is enabled, the entry point takes cares
5264 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005265 QuickEntrypointEnum entrypoint =
5266 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5267 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005268 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005269}
5270
Alexandre Rames5319def2014-10-23 10:03:10 +01005271void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5272 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005273 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005274 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005275 if (instruction->IsStringAlloc()) {
5276 locations->AddTemp(LocationFrom(kArtMethodRegister));
5277 } else {
5278 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005279 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005280 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5281}
5282
5283void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005284 // Note: if heap poisoning is enabled, the entry point takes cares
5285 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005286 if (instruction->IsStringAlloc()) {
5287 // String is allocated through StringFactory. Call NewEmptyString entry point.
5288 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005289 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005290 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5291 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005292
5293 {
5294 // Ensure the pc position is recorded immediately after the `blr` instruction.
5295 ExactAssemblyScope eas(GetVIXLAssembler(),
5296 kInstructionSize,
5297 CodeBufferCheckScope::kExactSize);
5298 __ blr(lr);
5299 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5300 }
David Brazdil6de19382016-01-08 17:37:10 +00005301 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005302 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005303 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005304 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005305}
5306
5307void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5308 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005309 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005311}
5312
5313void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005314 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005315 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005316 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005317 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005318 break;
5319
5320 default:
5321 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5322 }
5323}
5324
David Brazdil66d126e2015-04-03 16:02:44 +01005325void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5326 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5327 locations->SetInAt(0, Location::RequiresRegister());
5328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5329}
5330
5331void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005332 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005333}
5334
Alexandre Rames5319def2014-10-23 10:03:10 +01005335void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005336 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5337 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005338}
5339
Calin Juravle2ae48182016-03-16 14:05:09 +00005340void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5341 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 return;
5343 }
Artem Serov914d7a82017-02-07 14:33:49 +00005344 {
5345 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5346 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5347 Location obj = instruction->GetLocations()->InAt(0);
5348 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5349 RecordPcInfo(instruction, instruction->GetDexPc());
5350 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005351}
5352
Calin Juravle2ae48182016-03-16 14:05:09 +00005353void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005354 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005355 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005356
5357 LocationSummary* locations = instruction->GetLocations();
5358 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005359
5360 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005361}
5362
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005363void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005364 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005365}
5366
Alexandre Rames67555f72014-11-18 10:55:16 +00005367void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5368 HandleBinaryOp(instruction);
5369}
5370
5371void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5372 HandleBinaryOp(instruction);
5373}
5374
Alexandre Rames3e69f162014-12-10 10:36:50 +00005375void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5376 LOG(FATAL) << "Unreachable";
5377}
5378
5379void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5380 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5381}
5382
Alexandre Rames5319def2014-10-23 10:03:10 +01005383void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5385 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5386 if (location.IsStackSlot()) {
5387 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5388 } else if (location.IsDoubleStackSlot()) {
5389 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5390 }
5391 locations->SetOut(location);
5392}
5393
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005394void InstructionCodeGeneratorARM64::VisitParameterValue(
5395 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005396 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005397}
5398
5399void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5400 LocationSummary* locations =
5401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005402 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005403}
5404
5405void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5406 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5407 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005408}
5409
5410void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5411 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005412 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005413 locations->SetInAt(i, Location::Any());
5414 }
5415 locations->SetOut(Location::Any());
5416}
5417
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005418void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005419 LOG(FATAL) << "Unreachable";
5420}
5421
Serban Constantinescu02164b32014-11-13 14:05:07 +00005422void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005423 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005424 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005425 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5426 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5428
5429 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005430 case Primitive::kPrimInt:
5431 case Primitive::kPrimLong:
5432 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005433 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005434 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5435 break;
5436
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005437 case Primitive::kPrimFloat:
5438 case Primitive::kPrimDouble: {
5439 InvokeRuntimeCallingConvention calling_convention;
5440 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5441 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5442 locations->SetOut(calling_convention.GetReturnLocation(type));
5443
5444 break;
5445 }
5446
Serban Constantinescu02164b32014-11-13 14:05:07 +00005447 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005448 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005449 }
5450}
5451
5452void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5453 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005454
Serban Constantinescu02164b32014-11-13 14:05:07 +00005455 switch (type) {
5456 case Primitive::kPrimInt:
5457 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005458 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005459 break;
5460 }
5461
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005462 case Primitive::kPrimFloat:
5463 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005464 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5465 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005466 if (type == Primitive::kPrimFloat) {
5467 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5468 } else {
5469 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5470 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005471 break;
5472 }
5473
Serban Constantinescu02164b32014-11-13 14:05:07 +00005474 default:
5475 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005476 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005477 }
5478}
5479
Calin Juravle27df7582015-04-17 19:12:31 +01005480void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5481 memory_barrier->SetLocations(nullptr);
5482}
5483
5484void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005485 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005486}
5487
Alexandre Rames5319def2014-10-23 10:03:10 +01005488void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5489 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5490 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005491 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005492}
5493
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005494void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005495 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005496}
5497
5498void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5499 instruction->SetLocations(nullptr);
5500}
5501
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005502void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005503 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005504}
5505
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005506void LocationsBuilderARM64::VisitRor(HRor* ror) {
5507 HandleBinaryOp(ror);
5508}
5509
5510void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5511 HandleBinaryOp(ror);
5512}
5513
Serban Constantinescu02164b32014-11-13 14:05:07 +00005514void LocationsBuilderARM64::VisitShl(HShl* shl) {
5515 HandleShift(shl);
5516}
5517
5518void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5519 HandleShift(shl);
5520}
5521
5522void LocationsBuilderARM64::VisitShr(HShr* shr) {
5523 HandleShift(shr);
5524}
5525
5526void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5527 HandleShift(shr);
5528}
5529
Alexandre Rames5319def2014-10-23 10:03:10 +01005530void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005531 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005532}
5533
5534void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005535 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005536}
5537
Alexandre Rames67555f72014-11-18 10:55:16 +00005538void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005539 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005540}
5541
5542void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005543 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005544}
5545
5546void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005547 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005548}
5549
Alexandre Rames67555f72014-11-18 10:55:16 +00005550void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005551 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005552}
5553
Calin Juravlee460d1d2015-09-29 04:52:17 +01005554void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5555 HUnresolvedInstanceFieldGet* instruction) {
5556 FieldAccessCallingConventionARM64 calling_convention;
5557 codegen_->CreateUnresolvedFieldLocationSummary(
5558 instruction, instruction->GetFieldType(), calling_convention);
5559}
5560
5561void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5562 HUnresolvedInstanceFieldGet* instruction) {
5563 FieldAccessCallingConventionARM64 calling_convention;
5564 codegen_->GenerateUnresolvedFieldAccess(instruction,
5565 instruction->GetFieldType(),
5566 instruction->GetFieldIndex(),
5567 instruction->GetDexPc(),
5568 calling_convention);
5569}
5570
5571void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5572 HUnresolvedInstanceFieldSet* instruction) {
5573 FieldAccessCallingConventionARM64 calling_convention;
5574 codegen_->CreateUnresolvedFieldLocationSummary(
5575 instruction, instruction->GetFieldType(), calling_convention);
5576}
5577
5578void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5579 HUnresolvedInstanceFieldSet* instruction) {
5580 FieldAccessCallingConventionARM64 calling_convention;
5581 codegen_->GenerateUnresolvedFieldAccess(instruction,
5582 instruction->GetFieldType(),
5583 instruction->GetFieldIndex(),
5584 instruction->GetDexPc(),
5585 calling_convention);
5586}
5587
5588void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5589 HUnresolvedStaticFieldGet* instruction) {
5590 FieldAccessCallingConventionARM64 calling_convention;
5591 codegen_->CreateUnresolvedFieldLocationSummary(
5592 instruction, instruction->GetFieldType(), calling_convention);
5593}
5594
5595void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5596 HUnresolvedStaticFieldGet* instruction) {
5597 FieldAccessCallingConventionARM64 calling_convention;
5598 codegen_->GenerateUnresolvedFieldAccess(instruction,
5599 instruction->GetFieldType(),
5600 instruction->GetFieldIndex(),
5601 instruction->GetDexPc(),
5602 calling_convention);
5603}
5604
5605void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5606 HUnresolvedStaticFieldSet* instruction) {
5607 FieldAccessCallingConventionARM64 calling_convention;
5608 codegen_->CreateUnresolvedFieldLocationSummary(
5609 instruction, instruction->GetFieldType(), calling_convention);
5610}
5611
5612void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5613 HUnresolvedStaticFieldSet* instruction) {
5614 FieldAccessCallingConventionARM64 calling_convention;
5615 codegen_->GenerateUnresolvedFieldAccess(instruction,
5616 instruction->GetFieldType(),
5617 instruction->GetFieldIndex(),
5618 instruction->GetDexPc(),
5619 calling_convention);
5620}
5621
Alexandre Rames5319def2014-10-23 10:03:10 +01005622void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005623 LocationSummary* locations =
5624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005625 // In suspend check slow path, usually there are no caller-save registers at all.
5626 // If SIMD instructions are present, however, we force spilling all live SIMD
5627 // registers in full width (since the runtime only saves/restores lower part).
5628 locations->SetCustomSlowPathCallerSaves(
5629 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005630}
5631
5632void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005633 HBasicBlock* block = instruction->GetBlock();
5634 if (block->GetLoopInformation() != nullptr) {
5635 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5636 // The back edge will generate the suspend check.
5637 return;
5638 }
5639 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5640 // The goto will generate the suspend check.
5641 return;
5642 }
5643 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005644}
5645
Alexandre Rames67555f72014-11-18 10:55:16 +00005646void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5647 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005648 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005649 InvokeRuntimeCallingConvention calling_convention;
5650 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5651}
5652
5653void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005654 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005655 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005656}
5657
5658void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5659 LocationSummary* locations =
5660 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5661 Primitive::Type input_type = conversion->GetInputType();
5662 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005663 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005664 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5665 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5666 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5667 }
5668
Alexandre Rames542361f2015-01-29 16:57:31 +00005669 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005670 locations->SetInAt(0, Location::RequiresFpuRegister());
5671 } else {
5672 locations->SetInAt(0, Location::RequiresRegister());
5673 }
5674
Alexandre Rames542361f2015-01-29 16:57:31 +00005675 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005676 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5677 } else {
5678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5679 }
5680}
5681
5682void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5683 Primitive::Type result_type = conversion->GetResultType();
5684 Primitive::Type input_type = conversion->GetInputType();
5685
5686 DCHECK_NE(input_type, result_type);
5687
Alexandre Rames542361f2015-01-29 16:57:31 +00005688 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005689 int result_size = Primitive::ComponentSize(result_type);
5690 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005691 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005692 Register output = OutputRegister(conversion);
5693 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005694 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005695 // 'int' values are used directly as W registers, discarding the top
5696 // bits, so we don't need to sign-extend and can just perform a move.
5697 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5698 // top 32 bits of the target register. We theoretically could leave those
5699 // bits unchanged, but we would have to make sure that no code uses a
5700 // 32bit input value as a 64bit value assuming that the top 32 bits are
5701 // zero.
5702 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005703 } else if (result_type == Primitive::kPrimChar ||
5704 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5705 __ Ubfx(output,
5706 output.IsX() ? source.X() : source.W(),
5707 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005708 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005709 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005710 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005711 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005712 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005713 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005714 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5715 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005716 } else if (Primitive::IsFloatingPointType(result_type) &&
5717 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005718 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5719 } else {
5720 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5721 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005722 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005723}
Alexandre Rames67555f72014-11-18 10:55:16 +00005724
Serban Constantinescu02164b32014-11-13 14:05:07 +00005725void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5726 HandleShift(ushr);
5727}
5728
5729void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5730 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005731}
5732
5733void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5734 HandleBinaryOp(instruction);
5735}
5736
5737void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5738 HandleBinaryOp(instruction);
5739}
5740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005741void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005742 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005743 LOG(FATAL) << "Unreachable";
5744}
5745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005746void InstructionCodeGeneratorARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005747 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005748 LOG(FATAL) << "Unreachable";
5749}
5750
Mark Mendellfe57faa2015-09-18 09:26:15 -04005751// Simple implementation of packed switch - generate cascaded compare/jumps.
5752void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5753 LocationSummary* locations =
5754 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5755 locations->SetInAt(0, Location::RequiresRegister());
5756}
5757
5758void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5759 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005760 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005761 Register value_reg = InputRegisterAt(switch_instr, 0);
5762 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5763
Zheng Xu3927c8b2015-11-18 17:46:25 +08005764 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005765 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005766 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5767 // make sure we don't emit it if the target may run out of range.
5768 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5769 // ranges and emit the tables only as required.
5770 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005771
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005772 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005773 // Current instruction id is an upper bound of the number of HIRs in the graph.
5774 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5775 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005776 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5777 Register temp = temps.AcquireW();
5778 __ Subs(temp, value_reg, Operand(lower_bound));
5779
Zheng Xu3927c8b2015-11-18 17:46:25 +08005780 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005781 // Jump to successors[0] if value == lower_bound.
5782 __ B(eq, codegen_->GetLabelOf(successors[0]));
5783 int32_t last_index = 0;
5784 for (; num_entries - last_index > 2; last_index += 2) {
5785 __ Subs(temp, temp, Operand(2));
5786 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5787 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5788 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5789 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5790 }
5791 if (num_entries - last_index == 2) {
5792 // The last missing case_value.
5793 __ Cmp(temp, Operand(1));
5794 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005795 }
5796
5797 // And the default for any other value.
5798 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5799 __ B(codegen_->GetLabelOf(default_block));
5800 }
5801 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005802 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005803
5804 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5805
5806 // Below instructions should use at most one blocked register. Since there are two blocked
5807 // registers, we are free to block one.
5808 Register temp_w = temps.AcquireW();
5809 Register index;
5810 // Remove the bias.
5811 if (lower_bound != 0) {
5812 index = temp_w;
5813 __ Sub(index, value_reg, Operand(lower_bound));
5814 } else {
5815 index = value_reg;
5816 }
5817
5818 // Jump to default block if index is out of the range.
5819 __ Cmp(index, Operand(num_entries));
5820 __ B(hs, codegen_->GetLabelOf(default_block));
5821
5822 // In current VIXL implementation, it won't require any blocked registers to encode the
5823 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5824 // register pressure.
5825 Register table_base = temps.AcquireX();
5826 // Load jump offset from the table.
5827 __ Adr(table_base, jump_table->GetTableStartLabel());
5828 Register jump_offset = temp_w;
5829 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5830
5831 // Jump to target block by branching to table_base(pc related) + offset.
5832 Register target_address = table_base;
5833 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5834 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005835 }
5836}
5837
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005838void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5839 HInstruction* instruction,
5840 Location out,
5841 uint32_t offset,
5842 Location maybe_temp,
5843 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005844 Primitive::Type type = Primitive::kPrimNot;
5845 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005846 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005847 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005848 if (kUseBakerReadBarrier) {
5849 // Load with fast path based Baker's read barrier.
5850 // /* HeapReference<Object> */ out = *(out + offset)
5851 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5852 out,
5853 out_reg,
5854 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005855 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005856 /* needs_null_check */ false,
5857 /* use_load_acquire */ false);
5858 } else {
5859 // Load with slow path based read barrier.
5860 // Save the value of `out` into `maybe_temp` before overwriting it
5861 // in the following move operation, as we will need it for the
5862 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005863 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005864 __ Mov(temp_reg, out_reg);
5865 // /* HeapReference<Object> */ out = *(out + offset)
5866 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5867 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5868 }
5869 } else {
5870 // Plain load with no read barrier.
5871 // /* HeapReference<Object> */ out = *(out + offset)
5872 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5873 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5874 }
5875}
5876
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005877void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5878 HInstruction* instruction,
5879 Location out,
5880 Location obj,
5881 uint32_t offset,
5882 Location maybe_temp,
5883 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005884 Primitive::Type type = Primitive::kPrimNot;
5885 Register out_reg = RegisterFrom(out, type);
5886 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005887 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005888 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005889 if (kUseBakerReadBarrier) {
5890 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005891 // /* HeapReference<Object> */ out = *(obj + offset)
5892 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5893 out,
5894 obj_reg,
5895 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005896 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005897 /* needs_null_check */ false,
5898 /* use_load_acquire */ false);
5899 } else {
5900 // Load with slow path based read barrier.
5901 // /* HeapReference<Object> */ out = *(obj + offset)
5902 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5903 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5904 }
5905 } else {
5906 // Plain load with no read barrier.
5907 // /* HeapReference<Object> */ out = *(obj + offset)
5908 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5909 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5910 }
5911}
5912
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005913void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5914 HInstruction* instruction,
5915 Location root,
5916 Register obj,
5917 uint32_t offset,
5918 vixl::aarch64::Label* fixup_label,
5919 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005920 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005921 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005922 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005923 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005924 if (kUseBakerReadBarrier) {
5925 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005926 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005927 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5928 !Runtime::Current()->UseJitCompilation()) {
5929 // Note that we do not actually check the value of `GetIsGcMarking()`
5930 // to decide whether to mark the loaded GC root or not. Instead, we
5931 // load into `temp` the read barrier mark introspection entrypoint.
5932 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5933 // vice versa.
5934 //
5935 // We use link-time generated thunks for the slow path. That thunk
5936 // checks the reference and jumps to the entrypoint if needed.
5937 //
5938 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5939 // lr = &return_address;
5940 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5941 // if (temp != nullptr) {
5942 // goto gc_root_thunk<root_reg>(lr)
5943 // }
5944 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005945
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005946 UseScratchRegisterScope temps(GetVIXLAssembler());
5947 DCHECK(temps.IsAvailable(ip0));
5948 DCHECK(temps.IsAvailable(ip1));
5949 temps.Exclude(ip0, ip1);
5950 uint32_t custom_data =
5951 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5952 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005953
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005954 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5955 DCHECK_EQ(ip0.GetCode(), 16u);
5956 const int32_t entry_point_offset =
5957 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5958 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5959 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5960 vixl::aarch64::Label return_address;
5961 __ adr(lr, &return_address);
5962 if (fixup_label != nullptr) {
5963 __ Bind(fixup_label);
5964 }
5965 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5966 "GC root LDR must be 2 instruction (8B) before the return address label.");
5967 __ ldr(root_reg, MemOperand(obj.X(), offset));
5968 __ Bind(cbnz_label);
5969 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5970 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005972 // Note that we do not actually check the value of
5973 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5974 // root or not. Instead, we load into `temp` the read barrier
5975 // mark entry point corresponding to register `root`. If `temp`
5976 // is null, it means that `GetIsGcMarking()` is false, and vice
5977 // versa.
5978 //
5979 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5980 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5981 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5982 // // Slow path.
5983 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5984 // }
Roland Levillain44015862016-01-22 11:47:17 +00005985
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005986 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5987 Register temp = lr;
5988 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5989 instruction, root, /* entrypoint */ LocationFrom(temp));
5990 codegen_->AddSlowPath(slow_path);
5991
5992 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5993 const int32_t entry_point_offset =
5994 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5995 // Loading the entrypoint does not require a load acquire since it is only changed when
5996 // threads are suspended or running a checkpoint.
5997 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5998
5999 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6000 if (fixup_label == nullptr) {
6001 __ Ldr(root_reg, MemOperand(obj, offset));
6002 } else {
6003 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
6004 }
6005 static_assert(
6006 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6007 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6008 "have different sizes.");
6009 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6010 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6011 "have different sizes.");
6012
6013 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6014 // checking GetIsGcMarking.
6015 __ Cbnz(temp, slow_path->GetEntryLabel());
6016 __ Bind(slow_path->GetExitLabel());
6017 }
Roland Levillain44015862016-01-22 11:47:17 +00006018 } else {
6019 // GC root loaded through a slow path for read barriers other
6020 // than Baker's.
6021 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 if (fixup_label == nullptr) {
6023 __ Add(root_reg.X(), obj.X(), offset);
6024 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006025 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006026 }
Roland Levillain44015862016-01-22 11:47:17 +00006027 // /* mirror::Object* */ root = root->Read()
6028 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6029 }
6030 } else {
6031 // Plain GC root load with no read barrier.
6032 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006033 if (fixup_label == nullptr) {
6034 __ Ldr(root_reg, MemOperand(obj, offset));
6035 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006036 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006037 }
Roland Levillain44015862016-01-22 11:47:17 +00006038 // Note that GC roots are not affected by heap poisoning, thus we
6039 // do not have to unpoison `root_reg` here.
6040 }
6041}
6042
6043void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6044 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006045 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006046 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006047 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006048 bool needs_null_check,
6049 bool use_load_acquire) {
6050 DCHECK(kEmitCompilerReadBarrier);
6051 DCHECK(kUseBakerReadBarrier);
6052
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006053 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6054 !use_load_acquire &&
6055 !Runtime::Current()->UseJitCompilation()) {
6056 // Note that we do not actually check the value of `GetIsGcMarking()`
6057 // to decide whether to mark the loaded GC root or not. Instead, we
6058 // load into `temp` the read barrier mark introspection entrypoint.
6059 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6060 // vice versa.
6061 //
6062 // We use link-time generated thunks for the slow path. That thunk checks
6063 // the holder and jumps to the entrypoint if needed. If the holder is not
6064 // gray, it creates a fake dependency and returns to the LDR instruction.
6065 //
6066 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6067 // lr = &return_address;
6068 // if (temp != nullptr) {
6069 // goto field_thunk<holder_reg, base_reg>(lr)
6070 // }
6071 // not_gray_return_address:
6072 // // Original reference load. If the offset is too large to fit
6073 // // into LDR, we use an adjusted base register here.
6074 // GcRoot<mirror::Object> root = *(obj+offset);
6075 // gray_return_address:
6076
6077 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6078 Register base = obj;
6079 if (offset >= kReferenceLoadMinFarOffset) {
6080 DCHECK(maybe_temp.IsRegister());
6081 base = WRegisterFrom(maybe_temp);
6082 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6083 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6084 offset &= (kReferenceLoadMinFarOffset - 1u);
6085 }
6086 UseScratchRegisterScope temps(GetVIXLAssembler());
6087 DCHECK(temps.IsAvailable(ip0));
6088 DCHECK(temps.IsAvailable(ip1));
6089 temps.Exclude(ip0, ip1);
6090 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6091 base.GetCode(),
6092 obj.GetCode());
6093 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6094
6095 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6096 DCHECK_EQ(ip0.GetCode(), 16u);
6097 const int32_t entry_point_offset =
6098 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6099 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
Vladimir Markod1ef8732017-04-18 13:55:13 +01006100 EmissionCheckScope guard(GetVIXLAssembler(),
6101 (kPoisonHeapReferences ? 4u : 3u) * vixl::aarch64::kInstructionSize);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006102 vixl::aarch64::Label return_address;
6103 __ adr(lr, &return_address);
6104 __ Bind(cbnz_label);
6105 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
Vladimir Markod1ef8732017-04-18 13:55:13 +01006106 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
6107 "Field LDR must be 1 instruction (4B) before the return address label; "
6108 " 2 instructions (8B) for heap poisoning.");
6109 Register ref_reg = RegisterFrom(ref, Primitive::kPrimNot);
6110 __ ldr(ref_reg, MemOperand(base.X(), offset));
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006111 if (needs_null_check) {
6112 MaybeRecordImplicitNullCheck(instruction);
6113 }
Vladimir Markod1ef8732017-04-18 13:55:13 +01006114 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006115 __ Bind(&return_address);
6116 return;
6117 }
6118
Roland Levillain44015862016-01-22 11:47:17 +00006119 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006120 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006121 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006122 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006123 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6124 ref,
6125 obj,
6126 offset,
6127 no_index,
6128 no_scale_factor,
6129 temp,
6130 needs_null_check,
6131 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006132}
6133
6134void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6135 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006136 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006137 uint32_t data_offset,
6138 Location index,
6139 Register temp,
6140 bool needs_null_check) {
6141 DCHECK(kEmitCompilerReadBarrier);
6142 DCHECK(kUseBakerReadBarrier);
6143
6144 // Array cells are never volatile variables, therefore array loads
6145 // never use Load-Acquire instructions on ARM64.
6146 const bool use_load_acquire = false;
6147
Roland Levillainbfea3352016-06-23 13:48:47 +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.");
Roland Levillain44015862016-01-22 11:47:17 +00006151 // /* HeapReference<Object> */ ref =
6152 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006153 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6154 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6155 ref,
6156 obj,
6157 data_offset,
6158 index,
6159 scale_factor,
6160 temp,
6161 needs_null_check,
6162 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006163}
6164
6165void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6166 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006167 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006168 uint32_t offset,
6169 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006170 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006171 Register temp,
6172 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006173 bool use_load_acquire,
6174 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006175 DCHECK(kEmitCompilerReadBarrier);
6176 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006177 // If we are emitting an array load, we should not be using a
6178 // Load Acquire instruction. In other words:
6179 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6180 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006181
Roland Levillain54f869e2017-03-06 13:54:11 +00006182 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6183 // whether we need to enter the slow path to mark the reference.
6184 // Then, in the slow path, check the gray bit in the lock word of
6185 // the reference's holder (`obj`) to decide whether to mark `ref` or
6186 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006187 //
Roland Levillainba650a42017-03-06 13:52:32 +00006188 // Note that we do not actually check the value of `GetIsGcMarking()`;
6189 // instead, we load into `temp2` the read barrier mark entry point
6190 // corresponding to register `ref`. If `temp2` is null, it means
6191 // that `GetIsGcMarking()` is false, and vice versa.
6192 //
6193 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006194 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6195 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006196 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6197 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6198 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6199 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6200 // if (is_gray) {
6201 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6202 // }
6203 // } else {
6204 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006205 // }
Roland Levillain44015862016-01-22 11:47:17 +00006206
Roland Levillainba650a42017-03-06 13:52:32 +00006207 // Slow path marking the object `ref` when the GC is marking. The
6208 // entrypoint will already be loaded in `temp2`.
6209 Register temp2 = lr;
6210 Location temp2_loc = LocationFrom(temp2);
6211 SlowPathCodeARM64* slow_path;
6212 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006213 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6214 // only supports address of the form `obj + field_offset`, where
6215 // `obj` is a register and `field_offset` is a register. Thus
6216 // `offset` and `scale_factor` above are expected to be null in
6217 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006218 DCHECK_EQ(offset, 0u);
6219 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006220 Location field_offset = index;
6221 slow_path =
6222 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6223 instruction,
6224 ref,
6225 obj,
6226 offset,
6227 /* index */ field_offset,
6228 scale_factor,
6229 needs_null_check,
6230 use_load_acquire,
6231 temp,
6232 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006233 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006234 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6235 instruction,
6236 ref,
6237 obj,
6238 offset,
6239 index,
6240 scale_factor,
6241 needs_null_check,
6242 use_load_acquire,
6243 temp,
6244 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006245 }
6246 AddSlowPath(slow_path);
6247
6248 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6249 const int32_t entry_point_offset =
6250 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6251 // Loading the entrypoint does not require a load acquire since it is only changed when
6252 // threads are suspended or running a checkpoint.
6253 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006254 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6255 // checking GetIsGcMarking.
6256 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006257 // Fast path: just load the reference.
6258 GenerateRawReferenceLoad(
6259 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006260 __ Bind(slow_path->GetExitLabel());
6261}
6262
6263void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6264 Location ref,
6265 Register obj,
6266 uint32_t offset,
6267 Location index,
6268 size_t scale_factor,
6269 bool needs_null_check,
6270 bool use_load_acquire) {
6271 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006272 Primitive::Type type = Primitive::kPrimNot;
6273 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006274
Roland Levillainba650a42017-03-06 13:52:32 +00006275 // If needed, vixl::EmissionCheckScope guards are used to ensure
6276 // that no pools are emitted between the load (macro) instruction
6277 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006278
Roland Levillain44015862016-01-22 11:47:17 +00006279 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006280 // Load types involving an "index": ArrayGet,
6281 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6282 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006283 if (use_load_acquire) {
6284 // UnsafeGetObjectVolatile intrinsic case.
6285 // Register `index` is not an index in an object array, but an
6286 // offset to an object reference field within object `obj`.
6287 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6288 DCHECK(instruction->GetLocations()->Intrinsified());
6289 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6290 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006291 DCHECK_EQ(offset, 0u);
6292 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006293 DCHECK_EQ(needs_null_check, false);
6294 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006295 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6296 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006297 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006298 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6299 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006300 if (index.IsConstant()) {
6301 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006302 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006303 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006304 if (needs_null_check) {
6305 MaybeRecordImplicitNullCheck(instruction);
6306 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006307 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006308 UseScratchRegisterScope temps(GetVIXLAssembler());
6309 Register temp = temps.AcquireW();
6310 __ Add(temp, obj, offset);
6311 {
6312 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6313 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6314 if (needs_null_check) {
6315 MaybeRecordImplicitNullCheck(instruction);
6316 }
6317 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006318 }
Roland Levillain44015862016-01-22 11:47:17 +00006319 }
Roland Levillain44015862016-01-22 11:47:17 +00006320 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006321 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006322 MemOperand field = HeapOperand(obj, offset);
6323 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006324 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6325 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006326 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006327 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006328 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006329 if (needs_null_check) {
6330 MaybeRecordImplicitNullCheck(instruction);
6331 }
Roland Levillain44015862016-01-22 11:47:17 +00006332 }
6333 }
6334
6335 // Object* ref = ref_addr->AsMirrorPtr()
6336 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006337}
6338
6339void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6340 Location out,
6341 Location ref,
6342 Location obj,
6343 uint32_t offset,
6344 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006345 DCHECK(kEmitCompilerReadBarrier);
6346
Roland Levillain44015862016-01-22 11:47:17 +00006347 // Insert a slow path based read barrier *after* the reference load.
6348 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006349 // If heap poisoning is enabled, the unpoisoning of the loaded
6350 // reference will be carried out by the runtime within the slow
6351 // path.
6352 //
6353 // Note that `ref` currently does not get unpoisoned (when heap
6354 // poisoning is enabled), which is alright as the `ref` argument is
6355 // not used by the artReadBarrierSlow entry point.
6356 //
6357 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6358 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6359 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6360 AddSlowPath(slow_path);
6361
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006362 __ B(slow_path->GetEntryLabel());
6363 __ Bind(slow_path->GetExitLabel());
6364}
6365
Roland Levillain44015862016-01-22 11:47:17 +00006366void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6367 Location out,
6368 Location ref,
6369 Location obj,
6370 uint32_t offset,
6371 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006372 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006373 // Baker's read barriers shall be handled by the fast path
6374 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6375 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006376 // If heap poisoning is enabled, unpoisoning will be taken care of
6377 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006378 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006379 } else if (kPoisonHeapReferences) {
6380 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6381 }
6382}
6383
Roland Levillain44015862016-01-22 11:47:17 +00006384void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6385 Location out,
6386 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006387 DCHECK(kEmitCompilerReadBarrier);
6388
Roland Levillain44015862016-01-22 11:47:17 +00006389 // Insert a slow path based read barrier *after* the GC root load.
6390 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006391 // Note that GC roots are not affected by heap poisoning, so we do
6392 // not need to do anything special for this here.
6393 SlowPathCodeARM64* slow_path =
6394 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6395 AddSlowPath(slow_path);
6396
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006397 __ B(slow_path->GetEntryLabel());
6398 __ Bind(slow_path->GetExitLabel());
6399}
6400
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006401void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6402 LocationSummary* locations =
6403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6404 locations->SetInAt(0, Location::RequiresRegister());
6405 locations->SetOut(Location::RequiresRegister());
6406}
6407
6408void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6409 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006411 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006412 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006413 __ Ldr(XRegisterFrom(locations->Out()),
6414 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006415 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006416 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006417 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006418 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6419 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006420 __ Ldr(XRegisterFrom(locations->Out()),
6421 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006422 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006423}
6424
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006425static void PatchJitRootUse(uint8_t* code,
6426 const uint8_t* roots_data,
6427 vixl::aarch64::Literal<uint32_t>* literal,
6428 uint64_t index_in_table) {
6429 uint32_t literal_offset = literal->GetOffset();
6430 uintptr_t address =
6431 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6432 uint8_t* data = code + literal_offset;
6433 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6434}
6435
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006436void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6437 for (const auto& entry : jit_string_patches_) {
6438 const auto& it = jit_string_roots_.find(entry.first);
6439 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006440 PatchJitRootUse(code, roots_data, entry.second, it->second);
6441 }
6442 for (const auto& entry : jit_class_patches_) {
6443 const auto& it = jit_class_roots_.find(entry.first);
6444 DCHECK(it != jit_class_roots_.end());
6445 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006446 }
6447}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006448
Alexandre Rames67555f72014-11-18 10:55:16 +00006449#undef __
6450#undef QUICK_ENTRY_POINT
6451
Alexandre Rames5319def2014-10-23 10:03:10 +01006452} // namespace arm64
6453} // namespace art