blob: 8faaec1de7bc6fbd65680c367590da5702f6d572 [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.
93constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
94constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
95
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);
3055 }
Artem Serov914d7a82017-02-07 14:33:49 +00003056 {
3057 // Ensure that between store and MaybeRecordImplicitNullCheck there are no pools emitted.
3058 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
3059 __ Str(source, destination);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003060
Artem Serov914d7a82017-02-07 14:33:49 +00003061 if (!may_need_runtime_call_for_type_check) {
3062 codegen_->MaybeRecordImplicitNullCheck(instruction);
3063 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003064 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003065 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01003066
3067 codegen_->MarkGCCard(array, value.W(), instruction->GetValueCanBeNull());
3068
3069 if (done.IsLinked()) {
3070 __ Bind(&done);
3071 }
3072
3073 if (slow_path != nullptr) {
3074 __ Bind(slow_path->GetExitLabel());
Alexandre Rames97833a02015-04-16 15:07:12 +01003075 }
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003076 }
3077}
3078
Alexandre Rames67555f72014-11-18 10:55:16 +00003079void LocationsBuilderARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003080 RegisterSet caller_saves = RegisterSet::Empty();
3081 InvokeRuntimeCallingConvention calling_convention;
3082 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
3083 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1).GetCode()));
3084 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Alexandre Rames67555f72014-11-18 10:55:16 +00003085 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu760d8ef2015-03-28 18:09:56 +00003086 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
Alexandre Rames67555f72014-11-18 10:55:16 +00003087}
3088
3089void InstructionCodeGeneratorARM64::VisitBoundsCheck(HBoundsCheck* instruction) {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01003090 BoundsCheckSlowPathARM64* slow_path =
3091 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM64(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003092 codegen_->AddSlowPath(slow_path);
Alexandre Rames67555f72014-11-18 10:55:16 +00003093 __ Cmp(InputRegisterAt(instruction, 0), InputOperandAt(instruction, 1));
3094 __ B(slow_path->GetEntryLabel(), hs);
3095}
3096
Alexandre Rames67555f72014-11-18 10:55:16 +00003097void LocationsBuilderARM64::VisitClinitCheck(HClinitCheck* check) {
3098 LocationSummary* locations =
3099 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3100 locations->SetInAt(0, Location::RequiresRegister());
3101 if (check->HasUses()) {
3102 locations->SetOut(Location::SameAsFirstInput());
3103 }
3104}
3105
3106void InstructionCodeGeneratorARM64::VisitClinitCheck(HClinitCheck* check) {
3107 // We assume the class is not null.
3108 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
3109 check->GetLoadClass(), check, check->GetDexPc(), true);
3110 codegen_->AddSlowPath(slow_path);
3111 GenerateClassInitializationCheck(slow_path, InputRegisterAt(check, 0));
3112}
3113
Roland Levillain1a653882016-03-18 18:05:57 +00003114static bool IsFloatingPointZeroConstant(HInstruction* inst) {
3115 return (inst->IsFloatConstant() && (inst->AsFloatConstant()->IsArithmeticZero()))
3116 || (inst->IsDoubleConstant() && (inst->AsDoubleConstant()->IsArithmeticZero()));
3117}
3118
3119void InstructionCodeGeneratorARM64::GenerateFcmp(HInstruction* instruction) {
3120 FPRegister lhs_reg = InputFPRegisterAt(instruction, 0);
3121 Location rhs_loc = instruction->GetLocations()->InAt(1);
3122 if (rhs_loc.IsConstant()) {
3123 // 0.0 is the only immediate that can be encoded directly in
3124 // an FCMP instruction.
3125 //
3126 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
3127 // specify that in a floating-point comparison, positive zero
3128 // and negative zero are considered equal, so we can use the
3129 // literal 0.0 for both cases here.
3130 //
3131 // Note however that some methods (Float.equal, Float.compare,
3132 // Float.compareTo, Double.equal, Double.compare,
3133 // Double.compareTo, Math.max, Math.min, StrictMath.max,
3134 // StrictMath.min) consider 0.0 to be (strictly) greater than
3135 // -0.0. So if we ever translate calls to these methods into a
3136 // HCompare instruction, we must handle the -0.0 case with
3137 // care here.
3138 DCHECK(IsFloatingPointZeroConstant(rhs_loc.GetConstant()));
3139 __ Fcmp(lhs_reg, 0.0);
3140 } else {
3141 __ Fcmp(lhs_reg, InputFPRegisterAt(instruction, 1));
3142 }
Roland Levillain7f63c522015-07-13 15:54:55 +00003143}
3144
Serban Constantinescu02164b32014-11-13 14:05:07 +00003145void LocationsBuilderARM64::VisitCompare(HCompare* compare) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003146 LocationSummary* locations =
Serban Constantinescu02164b32014-11-13 14:05:07 +00003147 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
3148 Primitive::Type in_type = compare->InputAt(0)->GetType();
Alexandre Rames5319def2014-10-23 10:03:10 +01003149 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003150 case Primitive::kPrimBoolean:
3151 case Primitive::kPrimByte:
3152 case Primitive::kPrimShort:
3153 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003154 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01003155 case Primitive::kPrimLong: {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003156 locations->SetInAt(0, Location::RequiresRegister());
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00003157 locations->SetInAt(1, ARM64EncodableConstantOrRegister(compare->InputAt(1), compare));
Serban Constantinescu02164b32014-11-13 14:05:07 +00003158 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3159 break;
3160 }
3161 case Primitive::kPrimFloat:
3162 case Primitive::kPrimDouble: {
3163 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain7f63c522015-07-13 15:54:55 +00003164 locations->SetInAt(1,
3165 IsFloatingPointZeroConstant(compare->InputAt(1))
3166 ? Location::ConstantLocation(compare->InputAt(1)->AsConstant())
3167 : Location::RequiresFpuRegister());
Serban Constantinescu02164b32014-11-13 14:05:07 +00003168 locations->SetOut(Location::RequiresRegister());
3169 break;
3170 }
3171 default:
3172 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3173 }
3174}
3175
3176void InstructionCodeGeneratorARM64::VisitCompare(HCompare* compare) {
3177 Primitive::Type in_type = compare->InputAt(0)->GetType();
3178
3179 // 0 if: left == right
3180 // 1 if: left > right
3181 // -1 if: left < right
3182 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003183 case Primitive::kPrimBoolean:
3184 case Primitive::kPrimByte:
3185 case Primitive::kPrimShort:
3186 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003187 case Primitive::kPrimInt:
Serban Constantinescu02164b32014-11-13 14:05:07 +00003188 case Primitive::kPrimLong: {
3189 Register result = OutputRegister(compare);
3190 Register left = InputRegisterAt(compare, 0);
3191 Operand right = InputOperandAt(compare, 1);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003192 __ Cmp(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08003193 __ Cset(result, ne); // result == +1 if NE or 0 otherwise
3194 __ Cneg(result, result, lt); // result == -1 if LT or unchanged otherwise
Serban Constantinescu02164b32014-11-13 14:05:07 +00003195 break;
3196 }
3197 case Primitive::kPrimFloat:
3198 case Primitive::kPrimDouble: {
3199 Register result = OutputRegister(compare);
Roland Levillain1a653882016-03-18 18:05:57 +00003200 GenerateFcmp(compare);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003201 __ Cset(result, ne);
3202 __ Cneg(result, result, ARM64FPCondition(kCondLT, compare->IsGtBias()));
Alexandre Rames5319def2014-10-23 10:03:10 +01003203 break;
3204 }
3205 default:
3206 LOG(FATAL) << "Unimplemented compare type " << in_type;
3207 }
3208}
3209
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003210void LocationsBuilderARM64::HandleCondition(HCondition* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003211 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Roland Levillain7f63c522015-07-13 15:54:55 +00003212
3213 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
3214 locations->SetInAt(0, Location::RequiresFpuRegister());
3215 locations->SetInAt(1,
3216 IsFloatingPointZeroConstant(instruction->InputAt(1))
3217 ? Location::ConstantLocation(instruction->InputAt(1)->AsConstant())
3218 : Location::RequiresFpuRegister());
3219 } else {
3220 // Integer cases.
3221 locations->SetInAt(0, Location::RequiresRegister());
3222 locations->SetInAt(1, ARM64EncodableConstantOrRegister(instruction->InputAt(1), instruction));
3223 }
3224
David Brazdilb3e773e2016-01-26 11:28:37 +00003225 if (!instruction->IsEmittedAtUseSite()) {
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00003226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01003227 }
3228}
3229
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003230void InstructionCodeGeneratorARM64::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003231 if (instruction->IsEmittedAtUseSite()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003232 return;
3233 }
3234
3235 LocationSummary* locations = instruction->GetLocations();
Alexandre Rames5319def2014-10-23 10:03:10 +01003236 Register res = RegisterFrom(locations->Out(), instruction->GetType());
Roland Levillain7f63c522015-07-13 15:54:55 +00003237 IfCondition if_cond = instruction->GetCondition();
Alexandre Rames5319def2014-10-23 10:03:10 +01003238
Roland Levillain7f63c522015-07-13 15:54:55 +00003239 if (Primitive::IsFloatingPointType(instruction->InputAt(0)->GetType())) {
Roland Levillain1a653882016-03-18 18:05:57 +00003240 GenerateFcmp(instruction);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003241 __ Cset(res, ARM64FPCondition(if_cond, instruction->IsGtBias()));
Roland Levillain7f63c522015-07-13 15:54:55 +00003242 } else {
3243 // Integer cases.
3244 Register lhs = InputRegisterAt(instruction, 0);
3245 Operand rhs = InputOperandAt(instruction, 1);
3246 __ Cmp(lhs, rhs);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003247 __ Cset(res, ARM64Condition(if_cond));
Roland Levillain7f63c522015-07-13 15:54:55 +00003248 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003249}
3250
3251#define FOR_EACH_CONDITION_INSTRUCTION(M) \
3252 M(Equal) \
3253 M(NotEqual) \
3254 M(LessThan) \
3255 M(LessThanOrEqual) \
3256 M(GreaterThan) \
Aart Bike9f37602015-10-09 11:15:55 -07003257 M(GreaterThanOrEqual) \
3258 M(Below) \
3259 M(BelowOrEqual) \
3260 M(Above) \
3261 M(AboveOrEqual)
Alexandre Rames5319def2014-10-23 10:03:10 +01003262#define DEFINE_CONDITION_VISITORS(Name) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003263void LocationsBuilderARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); } \
3264void InstructionCodeGeneratorARM64::Visit##Name(H##Name* comp) { HandleCondition(comp); }
Alexandre Rames5319def2014-10-23 10:03:10 +01003265FOR_EACH_CONDITION_INSTRUCTION(DEFINE_CONDITION_VISITORS)
Alexandre Rames67555f72014-11-18 10:55:16 +00003266#undef DEFINE_CONDITION_VISITORS
Alexandre Rames5319def2014-10-23 10:03:10 +01003267#undef FOR_EACH_CONDITION_INSTRUCTION
3268
Zheng Xuc6667102015-05-15 16:08:45 +08003269void InstructionCodeGeneratorARM64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3270 DCHECK(instruction->IsDiv() || instruction->IsRem());
3271
3272 LocationSummary* locations = instruction->GetLocations();
3273 Location second = locations->InAt(1);
3274 DCHECK(second.IsConstant());
3275
3276 Register out = OutputRegister(instruction);
3277 Register dividend = InputRegisterAt(instruction, 0);
3278 int64_t imm = Int64FromConstant(second.GetConstant());
3279 DCHECK(imm == 1 || imm == -1);
3280
3281 if (instruction->IsRem()) {
3282 __ Mov(out, 0);
3283 } else {
3284 if (imm == 1) {
3285 __ Mov(out, dividend);
3286 } else {
3287 __ Neg(out, dividend);
3288 }
3289 }
3290}
3291
3292void InstructionCodeGeneratorARM64::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3293 DCHECK(instruction->IsDiv() || instruction->IsRem());
3294
3295 LocationSummary* locations = instruction->GetLocations();
3296 Location second = locations->InAt(1);
3297 DCHECK(second.IsConstant());
3298
3299 Register out = OutputRegister(instruction);
3300 Register dividend = InputRegisterAt(instruction, 0);
3301 int64_t imm = Int64FromConstant(second.GetConstant());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003302 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003303 int ctz_imm = CTZ(abs_imm);
3304
3305 UseScratchRegisterScope temps(GetVIXLAssembler());
3306 Register temp = temps.AcquireSameSizeAs(out);
3307
3308 if (instruction->IsDiv()) {
3309 __ Add(temp, dividend, abs_imm - 1);
3310 __ Cmp(dividend, 0);
3311 __ Csel(out, temp, dividend, lt);
3312 if (imm > 0) {
3313 __ Asr(out, out, ctz_imm);
3314 } else {
3315 __ Neg(out, Operand(out, ASR, ctz_imm));
3316 }
3317 } else {
3318 int bits = instruction->GetResultType() == Primitive::kPrimInt ? 32 : 64;
3319 __ Asr(temp, dividend, bits - 1);
3320 __ Lsr(temp, temp, bits - ctz_imm);
3321 __ Add(out, dividend, temp);
3322 __ And(out, out, abs_imm - 1);
3323 __ Sub(out, out, temp);
3324 }
3325}
3326
3327void InstructionCodeGeneratorARM64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3328 DCHECK(instruction->IsDiv() || instruction->IsRem());
3329
3330 LocationSummary* locations = instruction->GetLocations();
3331 Location second = locations->InAt(1);
3332 DCHECK(second.IsConstant());
3333
3334 Register out = OutputRegister(instruction);
3335 Register dividend = InputRegisterAt(instruction, 0);
3336 int64_t imm = Int64FromConstant(second.GetConstant());
3337
3338 Primitive::Type type = instruction->GetResultType();
3339 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
3340
3341 int64_t magic;
3342 int shift;
3343 CalculateMagicAndShiftForDivRem(imm, type == Primitive::kPrimLong /* is_long */, &magic, &shift);
3344
3345 UseScratchRegisterScope temps(GetVIXLAssembler());
3346 Register temp = temps.AcquireSameSizeAs(out);
3347
3348 // temp = get_high(dividend * magic)
3349 __ Mov(temp, magic);
3350 if (type == Primitive::kPrimLong) {
3351 __ Smulh(temp, dividend, temp);
3352 } else {
3353 __ Smull(temp.X(), dividend, temp);
3354 __ Lsr(temp.X(), temp.X(), 32);
3355 }
3356
3357 if (imm > 0 && magic < 0) {
3358 __ Add(temp, temp, dividend);
3359 } else if (imm < 0 && magic > 0) {
3360 __ Sub(temp, temp, dividend);
3361 }
3362
3363 if (shift != 0) {
3364 __ Asr(temp, temp, shift);
3365 }
3366
3367 if (instruction->IsDiv()) {
3368 __ Sub(out, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3369 } else {
3370 __ Sub(temp, temp, Operand(temp, ASR, type == Primitive::kPrimLong ? 63 : 31));
3371 // TODO: Strength reduction for msub.
3372 Register temp_imm = temps.AcquireSameSizeAs(out);
3373 __ Mov(temp_imm, imm);
3374 __ Msub(out, temp, temp_imm, dividend);
3375 }
3376}
3377
3378void InstructionCodeGeneratorARM64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3379 DCHECK(instruction->IsDiv() || instruction->IsRem());
3380 Primitive::Type type = instruction->GetResultType();
Calin Juravlec70d1d92017-03-27 18:10:04 -07003381 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
Zheng Xuc6667102015-05-15 16:08:45 +08003382
3383 LocationSummary* locations = instruction->GetLocations();
3384 Register out = OutputRegister(instruction);
3385 Location second = locations->InAt(1);
3386
3387 if (second.IsConstant()) {
3388 int64_t imm = Int64FromConstant(second.GetConstant());
3389
3390 if (imm == 0) {
3391 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3392 } else if (imm == 1 || imm == -1) {
3393 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003394 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003395 DivRemByPowerOfTwo(instruction);
3396 } else {
3397 DCHECK(imm <= -2 || imm >= 2);
3398 GenerateDivRemWithAnyConstant(instruction);
3399 }
3400 } else {
3401 Register dividend = InputRegisterAt(instruction, 0);
3402 Register divisor = InputRegisterAt(instruction, 1);
3403 if (instruction->IsDiv()) {
3404 __ Sdiv(out, dividend, divisor);
3405 } else {
3406 UseScratchRegisterScope temps(GetVIXLAssembler());
3407 Register temp = temps.AcquireSameSizeAs(out);
3408 __ Sdiv(temp, dividend, divisor);
3409 __ Msub(out, temp, divisor, dividend);
3410 }
3411 }
3412}
3413
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003414void LocationsBuilderARM64::VisitDiv(HDiv* div) {
3415 LocationSummary* locations =
3416 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3417 switch (div->GetResultType()) {
3418 case Primitive::kPrimInt:
3419 case Primitive::kPrimLong:
3420 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08003421 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003422 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3423 break;
3424
3425 case Primitive::kPrimFloat:
3426 case Primitive::kPrimDouble:
3427 locations->SetInAt(0, Location::RequiresFpuRegister());
3428 locations->SetInAt(1, Location::RequiresFpuRegister());
3429 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3430 break;
3431
3432 default:
3433 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3434 }
3435}
3436
3437void InstructionCodeGeneratorARM64::VisitDiv(HDiv* div) {
3438 Primitive::Type type = div->GetResultType();
3439 switch (type) {
3440 case Primitive::kPrimInt:
3441 case Primitive::kPrimLong:
Zheng Xuc6667102015-05-15 16:08:45 +08003442 GenerateDivRemIntegral(div);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00003443 break;
3444
3445 case Primitive::kPrimFloat:
3446 case Primitive::kPrimDouble:
3447 __ Fdiv(OutputFPRegister(div), InputFPRegisterAt(div, 0), InputFPRegisterAt(div, 1));
3448 break;
3449
3450 default:
3451 LOG(FATAL) << "Unexpected div type " << type;
3452 }
3453}
3454
Alexandre Rames67555f72014-11-18 10:55:16 +00003455void LocationsBuilderARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003456 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Alexandre Rames67555f72014-11-18 10:55:16 +00003457 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Alexandre Rames67555f72014-11-18 10:55:16 +00003458}
3459
3460void InstructionCodeGeneratorARM64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3461 SlowPathCodeARM64* slow_path =
3462 new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM64(instruction);
3463 codegen_->AddSlowPath(slow_path);
3464 Location value = instruction->GetLocations()->InAt(0);
3465
Alexandre Rames3e69f162014-12-10 10:36:50 +00003466 Primitive::Type type = instruction->GetType();
3467
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003468 if (!Primitive::IsIntegralType(type)) {
3469 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Alexandre Rames3e69f162014-12-10 10:36:50 +00003470 return;
3471 }
3472
Alexandre Rames67555f72014-11-18 10:55:16 +00003473 if (value.IsConstant()) {
3474 int64_t divisor = Int64ConstantFrom(value);
3475 if (divisor == 0) {
3476 __ B(slow_path->GetEntryLabel());
3477 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00003478 // A division by a non-null constant is valid. We don't need to perform
3479 // any check, so simply fall through.
Alexandre Rames67555f72014-11-18 10:55:16 +00003480 }
3481 } else {
3482 __ Cbz(InputRegisterAt(instruction, 0), slow_path->GetEntryLabel());
3483 }
3484}
3485
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003486void LocationsBuilderARM64::VisitDoubleConstant(HDoubleConstant* constant) {
3487 LocationSummary* locations =
3488 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3489 locations->SetOut(Location::ConstantLocation(constant));
3490}
3491
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003492void InstructionCodeGeneratorARM64::VisitDoubleConstant(
3493 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003494 // Will be generated at use site.
3495}
3496
Alexandre Rames5319def2014-10-23 10:03:10 +01003497void LocationsBuilderARM64::VisitExit(HExit* exit) {
3498 exit->SetLocations(nullptr);
3499}
3500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003501void InstructionCodeGeneratorARM64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003502}
3503
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003504void LocationsBuilderARM64::VisitFloatConstant(HFloatConstant* constant) {
3505 LocationSummary* locations =
3506 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3507 locations->SetOut(Location::ConstantLocation(constant));
3508}
3509
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003510void InstructionCodeGeneratorARM64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Ramesa89086e2014-11-07 17:13:25 +00003511 // Will be generated at use site.
3512}
3513
David Brazdilfc6a86a2015-06-26 10:33:45 +00003514void InstructionCodeGeneratorARM64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003515 DCHECK(!successor->IsExitBlock());
3516 HBasicBlock* block = got->GetBlock();
3517 HInstruction* previous = got->GetPrevious();
3518 HLoopInformation* info = block->GetLoopInformation();
3519
David Brazdil46e2a392015-03-16 17:31:52 +00003520 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00003521 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3522 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3523 return;
3524 }
3525 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3526 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3527 }
3528 if (!codegen_->GoesToNextBlock(block, successor)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003529 __ B(codegen_->GetLabelOf(successor));
3530 }
3531}
3532
David Brazdilfc6a86a2015-06-26 10:33:45 +00003533void LocationsBuilderARM64::VisitGoto(HGoto* got) {
3534 got->SetLocations(nullptr);
3535}
3536
3537void InstructionCodeGeneratorARM64::VisitGoto(HGoto* got) {
3538 HandleGoto(got, got->GetSuccessor());
3539}
3540
3541void LocationsBuilderARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3542 try_boundary->SetLocations(nullptr);
3543}
3544
3545void InstructionCodeGeneratorARM64::VisitTryBoundary(HTryBoundary* try_boundary) {
3546 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3547 if (!successor->IsExitBlock()) {
3548 HandleGoto(try_boundary, successor);
3549 }
3550}
3551
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003552void InstructionCodeGeneratorARM64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00003553 size_t condition_input_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01003554 vixl::aarch64::Label* true_target,
3555 vixl::aarch64::Label* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00003556 // FP branching requires both targets to be explicit. If either of the targets
3557 // is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003558 vixl::aarch64::Label fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003559 HInstruction* cond = instruction->InputAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003560
David Brazdil0debae72015-11-12 18:37:00 +00003561 if (true_target == nullptr && false_target == nullptr) {
3562 // Nothing to do. The code always falls through.
3563 return;
3564 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00003565 // Constant condition, statically compared against "true" (integer value 1).
3566 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00003567 if (true_target != nullptr) {
3568 __ B(true_target);
Serban Constantinescu02164b32014-11-13 14:05:07 +00003569 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003570 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00003571 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00003572 if (false_target != nullptr) {
3573 __ B(false_target);
3574 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00003575 }
David Brazdil0debae72015-11-12 18:37:00 +00003576 return;
3577 }
3578
3579 // The following code generates these patterns:
3580 // (1) true_target == nullptr && false_target != nullptr
3581 // - opposite condition true => branch to false_target
3582 // (2) true_target != nullptr && false_target == nullptr
3583 // - condition true => branch to true_target
3584 // (3) true_target != nullptr && false_target != nullptr
3585 // - condition true => branch to true_target
3586 // - branch to false_target
3587 if (IsBooleanValueOrMaterializedCondition(cond)) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003588 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00003589 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Alexandre Rames5319def2014-10-23 10:03:10 +01003590 DCHECK(cond_val.IsRegister());
David Brazdil0debae72015-11-12 18:37:00 +00003591 if (true_target == nullptr) {
3592 __ Cbz(InputRegisterAt(instruction, condition_input_index), false_target);
3593 } else {
3594 __ Cbnz(InputRegisterAt(instruction, condition_input_index), true_target);
3595 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003596 } else {
3597 // The condition instruction has not been materialized, use its inputs as
3598 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00003599 HCondition* condition = cond->AsCondition();
Roland Levillain7f63c522015-07-13 15:54:55 +00003600
David Brazdil0debae72015-11-12 18:37:00 +00003601 Primitive::Type type = condition->InputAt(0)->GetType();
Roland Levillain7f63c522015-07-13 15:54:55 +00003602 if (Primitive::IsFloatingPointType(type)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003603 GenerateFcmp(condition);
David Brazdil0debae72015-11-12 18:37:00 +00003604 if (true_target == nullptr) {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003605 IfCondition opposite_condition = condition->GetOppositeCondition();
3606 __ B(ARM64FPCondition(opposite_condition, condition->IsGtBias()), false_target);
David Brazdil0debae72015-11-12 18:37:00 +00003607 } else {
Vladimir Markod6e069b2016-01-18 11:11:01 +00003608 __ B(ARM64FPCondition(condition->GetCondition(), condition->IsGtBias()), true_target);
David Brazdil0debae72015-11-12 18:37:00 +00003609 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003610 } else {
Roland Levillain7f63c522015-07-13 15:54:55 +00003611 // Integer cases.
3612 Register lhs = InputRegisterAt(condition, 0);
3613 Operand rhs = InputOperandAt(condition, 1);
David Brazdil0debae72015-11-12 18:37:00 +00003614
3615 Condition arm64_cond;
Scott Wakeling97c72b72016-06-24 16:19:36 +01003616 vixl::aarch64::Label* non_fallthrough_target;
David Brazdil0debae72015-11-12 18:37:00 +00003617 if (true_target == nullptr) {
3618 arm64_cond = ARM64Condition(condition->GetOppositeCondition());
3619 non_fallthrough_target = false_target;
3620 } else {
3621 arm64_cond = ARM64Condition(condition->GetCondition());
3622 non_fallthrough_target = true_target;
3623 }
3624
Aart Bik086d27e2016-01-20 17:02:00 -08003625 if ((arm64_cond == eq || arm64_cond == ne || arm64_cond == lt || arm64_cond == ge) &&
Scott Wakeling97c72b72016-06-24 16:19:36 +01003626 rhs.IsImmediate() && (rhs.GetImmediate() == 0)) {
Roland Levillain7f63c522015-07-13 15:54:55 +00003627 switch (arm64_cond) {
3628 case eq:
David Brazdil0debae72015-11-12 18:37:00 +00003629 __ Cbz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003630 break;
3631 case ne:
David Brazdil0debae72015-11-12 18:37:00 +00003632 __ Cbnz(lhs, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003633 break;
3634 case lt:
3635 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003636 __ Tbnz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003637 break;
3638 case ge:
3639 // Test the sign bit and branch accordingly.
David Brazdil0debae72015-11-12 18:37:00 +00003640 __ Tbz(lhs, (lhs.IsX() ? kXRegSize : kWRegSize) - 1, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003641 break;
3642 default:
3643 // Without the `static_cast` the compiler throws an error for
3644 // `-Werror=sign-promo`.
3645 LOG(FATAL) << "Unexpected condition: " << static_cast<int>(arm64_cond);
3646 }
3647 } else {
3648 __ Cmp(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00003649 __ B(arm64_cond, non_fallthrough_target);
Roland Levillain7f63c522015-07-13 15:54:55 +00003650 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003651 }
3652 }
David Brazdil0debae72015-11-12 18:37:00 +00003653
3654 // If neither branch falls through (case 3), the conditional branch to `true_target`
3655 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3656 if (true_target != nullptr && false_target != nullptr) {
Alexandre Rames5319def2014-10-23 10:03:10 +01003657 __ B(false_target);
3658 }
David Brazdil0debae72015-11-12 18:37:00 +00003659
3660 if (fallthrough_target.IsLinked()) {
3661 __ Bind(&fallthrough_target);
3662 }
Alexandre Rames5319def2014-10-23 10:03:10 +01003663}
3664
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003665void LocationsBuilderARM64::VisitIf(HIf* if_instr) {
3666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00003667 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003668 locations->SetInAt(0, Location::RequiresRegister());
3669 }
3670}
3671
3672void InstructionCodeGeneratorARM64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003673 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3674 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
Scott Wakeling97c72b72016-06-24 16:19:36 +01003675 vixl::aarch64::Label* true_target = codegen_->GetLabelOf(true_successor);
3676 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor)) {
3677 true_target = nullptr;
3678 }
3679 vixl::aarch64::Label* false_target = codegen_->GetLabelOf(false_successor);
3680 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor)) {
3681 false_target = nullptr;
3682 }
David Brazdil0debae72015-11-12 18:37:00 +00003683 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003684}
3685
3686void LocationsBuilderARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
3687 LocationSummary* locations = new (GetGraph()->GetArena())
3688 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01003689 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00003690 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003691 locations->SetInAt(0, Location::RequiresRegister());
3692 }
3693}
3694
3695void InstructionCodeGeneratorARM64::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08003696 SlowPathCodeARM64* slow_path =
3697 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM64>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003698 GenerateTestAndBranch(deoptimize,
3699 /* condition_input_index */ 0,
3700 slow_path->GetEntryLabel(),
3701 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003702}
3703
Mingyao Yang063fc772016-08-02 11:02:54 -07003704void LocationsBuilderARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3705 LocationSummary* locations = new (GetGraph()->GetArena())
3706 LocationSummary(flag, LocationSummary::kNoCall);
3707 locations->SetOut(Location::RequiresRegister());
3708}
3709
3710void InstructionCodeGeneratorARM64::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3711 __ Ldr(OutputRegister(flag),
3712 MemOperand(sp, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
3713}
3714
David Brazdilc0b601b2016-02-08 14:20:45 +00003715static inline bool IsConditionOnFloatingPointValues(HInstruction* condition) {
3716 return condition->IsCondition() &&
3717 Primitive::IsFloatingPointType(condition->InputAt(0)->GetType());
3718}
3719
Alexandre Rames880f1192016-06-13 16:04:50 +01003720static inline Condition GetConditionForSelect(HCondition* condition) {
3721 IfCondition cond = condition->AsCondition()->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003722 return IsConditionOnFloatingPointValues(condition) ? ARM64FPCondition(cond, condition->IsGtBias())
3723 : ARM64Condition(cond);
3724}
3725
David Brazdil74eb1b22015-12-14 11:44:01 +00003726void LocationsBuilderARM64::VisitSelect(HSelect* select) {
3727 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexandre Rames880f1192016-06-13 16:04:50 +01003728 if (Primitive::IsFloatingPointType(select->GetType())) {
3729 locations->SetInAt(0, Location::RequiresFpuRegister());
3730 locations->SetInAt(1, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003731 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames880f1192016-06-13 16:04:50 +01003732 } else {
3733 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
3734 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
3735 bool is_true_value_constant = cst_true_value != nullptr;
3736 bool is_false_value_constant = cst_false_value != nullptr;
3737 // Ask VIXL whether we should synthesize constants in registers.
3738 // We give an arbitrary register to VIXL when dealing with non-constant inputs.
3739 Operand true_op = is_true_value_constant ?
3740 Operand(Int64FromConstant(cst_true_value)) : Operand(x1);
3741 Operand false_op = is_false_value_constant ?
3742 Operand(Int64FromConstant(cst_false_value)) : Operand(x2);
3743 bool true_value_in_register = false;
3744 bool false_value_in_register = false;
3745 MacroAssembler::GetCselSynthesisInformation(
3746 x0, true_op, false_op, &true_value_in_register, &false_value_in_register);
3747 true_value_in_register |= !is_true_value_constant;
3748 false_value_in_register |= !is_false_value_constant;
3749
3750 locations->SetInAt(1, true_value_in_register ? Location::RequiresRegister()
3751 : Location::ConstantLocation(cst_true_value));
3752 locations->SetInAt(0, false_value_in_register ? Location::RequiresRegister()
3753 : Location::ConstantLocation(cst_false_value));
Donghui Bai426b49c2016-11-08 14:55:38 +08003754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003755 }
Alexandre Rames880f1192016-06-13 16:04:50 +01003756
David Brazdil74eb1b22015-12-14 11:44:01 +00003757 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
3758 locations->SetInAt(2, Location::RequiresRegister());
3759 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003760}
3761
3762void InstructionCodeGeneratorARM64::VisitSelect(HSelect* select) {
David Brazdilc0b601b2016-02-08 14:20:45 +00003763 HInstruction* cond = select->GetCondition();
David Brazdilc0b601b2016-02-08 14:20:45 +00003764 Condition csel_cond;
3765
3766 if (IsBooleanValueOrMaterializedCondition(cond)) {
3767 if (cond->IsCondition() && cond->GetNext() == select) {
Alexandre Rames880f1192016-06-13 16:04:50 +01003768 // Use the condition flags set by the previous instruction.
3769 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003770 } else {
3771 __ Cmp(InputRegisterAt(select, 2), 0);
Alexandre Rames880f1192016-06-13 16:04:50 +01003772 csel_cond = ne;
David Brazdilc0b601b2016-02-08 14:20:45 +00003773 }
3774 } else if (IsConditionOnFloatingPointValues(cond)) {
Roland Levillain1a653882016-03-18 18:05:57 +00003775 GenerateFcmp(cond);
Alexandre Rames880f1192016-06-13 16:04:50 +01003776 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003777 } else {
3778 __ Cmp(InputRegisterAt(cond, 0), InputOperandAt(cond, 1));
Alexandre Rames880f1192016-06-13 16:04:50 +01003779 csel_cond = GetConditionForSelect(cond->AsCondition());
David Brazdilc0b601b2016-02-08 14:20:45 +00003780 }
3781
Alexandre Rames880f1192016-06-13 16:04:50 +01003782 if (Primitive::IsFloatingPointType(select->GetType())) {
3783 __ Fcsel(OutputFPRegister(select),
3784 InputFPRegisterAt(select, 1),
3785 InputFPRegisterAt(select, 0),
3786 csel_cond);
3787 } else {
3788 __ Csel(OutputRegister(select),
3789 InputOperandAt(select, 1),
3790 InputOperandAt(select, 0),
3791 csel_cond);
David Brazdilc0b601b2016-02-08 14:20:45 +00003792 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003793}
3794
David Srbecky0cf44932015-12-09 14:09:59 +00003795void LocationsBuilderARM64::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3796 new (GetGraph()->GetArena()) LocationSummary(info);
3797}
3798
David Srbeckyd28f4a02016-03-14 17:14:24 +00003799void InstructionCodeGeneratorARM64::VisitNativeDebugInfo(HNativeDebugInfo*) {
3800 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003801}
3802
3803void CodeGeneratorARM64::GenerateNop() {
3804 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003805}
3806
Alexandre Rames5319def2014-10-23 10:03:10 +01003807void LocationsBuilderARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00003808 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003809}
3810
3811void InstructionCodeGeneratorARM64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003812 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames5319def2014-10-23 10:03:10 +01003813}
3814
3815void LocationsBuilderARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01003816 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01003817}
3818
3819void InstructionCodeGeneratorARM64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003820 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01003821}
3822
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003823// Temp is used for read barrier.
3824static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3825 if (kEmitCompilerReadBarrier &&
Roland Levillain44015862016-01-22 11:47:17 +00003826 (kUseBakerReadBarrier ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003827 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3828 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3829 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3830 return 1;
3831 }
3832 return 0;
3833}
3834
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003835// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003836// interface pointer, one for loading the current interface.
3837// The other checks have one temp for loading the object's class.
3838static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3839 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
3840 return 3;
3841 }
3842 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain44015862016-01-22 11:47:17 +00003843}
3844
Alexandre Rames67555f72014-11-18 10:55:16 +00003845void LocationsBuilderARM64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003846 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003847 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01003848 bool baker_read_barrier_slow_path = false;
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003849 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003850 case TypeCheckKind::kExactCheck:
3851 case TypeCheckKind::kAbstractClassCheck:
3852 case TypeCheckKind::kClassHierarchyCheck:
3853 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003854 call_kind =
3855 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01003856 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003857 break;
3858 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003859 case TypeCheckKind::kUnresolvedCheck:
3860 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003861 call_kind = LocationSummary::kCallOnSlowPath;
3862 break;
3863 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003864
Alexandre Rames67555f72014-11-18 10:55:16 +00003865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01003866 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003867 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01003868 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003869 locations->SetInAt(0, Location::RequiresRegister());
3870 locations->SetInAt(1, Location::RequiresRegister());
3871 // The "out" register is used as a temporary, so it overlaps with the inputs.
3872 // Note that TypeCheckSlowPathARM64 uses this register too.
3873 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003874 // Add temps if necessary for read barriers.
3875 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Alexandre Rames67555f72014-11-18 10:55:16 +00003876}
3877
3878void InstructionCodeGeneratorARM64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00003879 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexandre Rames67555f72014-11-18 10:55:16 +00003880 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003881 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003882 Register obj = InputRegisterAt(instruction, 0);
3883 Register cls = InputRegisterAt(instruction, 1);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003884 Location out_loc = locations->Out();
Alexandre Rames67555f72014-11-18 10:55:16 +00003885 Register out = OutputRegister(instruction);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07003886 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
3887 DCHECK_LE(num_temps, 1u);
3888 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003889 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3890 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3891 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3892 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Alexandre Rames67555f72014-11-18 10:55:16 +00003893
Scott Wakeling97c72b72016-06-24 16:19:36 +01003894 vixl::aarch64::Label done, zero;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003895 SlowPathCodeARM64* slow_path = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00003896
3897 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003898 // Avoid null check if we know `obj` is not null.
3899 if (instruction->MustDoNullCheck()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003900 __ Cbz(obj, &zero);
3901 }
3902
Roland Levillain44015862016-01-22 11:47:17 +00003903 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003904 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003905 // /* HeapReference<Class> */ out = obj->klass_
3906 GenerateReferenceLoadTwoRegisters(instruction,
3907 out_loc,
3908 obj_loc,
3909 class_offset,
3910 maybe_temp_loc,
3911 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003912 __ Cmp(out, cls);
3913 __ Cset(out, eq);
3914 if (zero.IsLinked()) {
3915 __ B(&done);
3916 }
3917 break;
3918 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003919
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003920 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003921 // /* HeapReference<Class> */ out = obj->klass_
3922 GenerateReferenceLoadTwoRegisters(instruction,
3923 out_loc,
3924 obj_loc,
3925 class_offset,
3926 maybe_temp_loc,
3927 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003928 // If the class is abstract, we eagerly fetch the super class of the
3929 // object to avoid doing a comparison we know will fail.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003930 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003931 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003932 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003933 GenerateReferenceLoadOneRegister(instruction,
3934 out_loc,
3935 super_offset,
3936 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003937 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003938 // If `out` is null, we use it for the result, and jump to `done`.
3939 __ Cbz(out, &done);
3940 __ Cmp(out, cls);
3941 __ B(ne, &loop);
3942 __ Mov(out, 1);
3943 if (zero.IsLinked()) {
3944 __ B(&done);
3945 }
3946 break;
3947 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003948
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003949 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003950 // /* HeapReference<Class> */ out = obj->klass_
3951 GenerateReferenceLoadTwoRegisters(instruction,
3952 out_loc,
3953 obj_loc,
3954 class_offset,
3955 maybe_temp_loc,
3956 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003957 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003958 vixl::aarch64::Label loop, success;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003959 __ Bind(&loop);
3960 __ Cmp(out, cls);
3961 __ B(eq, &success);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003962 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003963 GenerateReferenceLoadOneRegister(instruction,
3964 out_loc,
3965 super_offset,
3966 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003967 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003968 __ Cbnz(out, &loop);
3969 // If `out` is null, we use it for the result, and jump to `done`.
3970 __ B(&done);
3971 __ Bind(&success);
3972 __ Mov(out, 1);
3973 if (zero.IsLinked()) {
3974 __ B(&done);
3975 }
3976 break;
3977 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003979 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08003980 // /* HeapReference<Class> */ out = obj->klass_
3981 GenerateReferenceLoadTwoRegisters(instruction,
3982 out_loc,
3983 obj_loc,
3984 class_offset,
3985 maybe_temp_loc,
3986 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003987 // Do an exact check.
Scott Wakeling97c72b72016-06-24 16:19:36 +01003988 vixl::aarch64::Label exact_check;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01003989 __ Cmp(out, cls);
3990 __ B(eq, &exact_check);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003991 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00003992 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08003993 GenerateReferenceLoadOneRegister(instruction,
3994 out_loc,
3995 component_offset,
3996 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08003997 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00003998 // If `out` is null, we use it for the result, and jump to `done`.
3999 __ Cbz(out, &done);
4000 __ Ldrh(out, HeapOperand(out, primitive_offset));
4001 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
4002 __ Cbnz(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004003 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004004 __ Mov(out, 1);
4005 __ B(&done);
4006 break;
4007 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004008
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004009 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08004010 // No read barrier since the slow path will retry upon failure.
4011 // /* HeapReference<Class> */ out = obj->klass_
4012 GenerateReferenceLoadTwoRegisters(instruction,
4013 out_loc,
4014 obj_loc,
4015 class_offset,
4016 maybe_temp_loc,
4017 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004018 __ Cmp(out, cls);
4019 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004020 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4021 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004022 codegen_->AddSlowPath(slow_path);
4023 __ B(ne, slow_path->GetEntryLabel());
4024 __ Mov(out, 1);
4025 if (zero.IsLinked()) {
4026 __ B(&done);
4027 }
4028 break;
4029 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004030
Calin Juravle98893e12015-10-02 21:05:03 +01004031 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004032 case TypeCheckKind::kInterfaceCheck: {
4033 // Note that we indeed only call on slow path, but we always go
4034 // into the slow path for the unresolved and interface check
4035 // cases.
4036 //
4037 // We cannot directly call the InstanceofNonTrivial runtime
4038 // entry point without resorting to a type checking slow path
4039 // here (i.e. by calling InvokeRuntime directly), as it would
4040 // require to assign fixed registers for the inputs of this
4041 // HInstanceOf instruction (following the runtime calling
4042 // convention), which might be cluttered by the potential first
4043 // read barrier emission at the beginning of this method.
Roland Levillain44015862016-01-22 11:47:17 +00004044 //
4045 // TODO: Introduce a new runtime entry point taking the object
4046 // to test (instead of its class) as argument, and let it deal
4047 // with the read barrier issues. This will let us refactor this
4048 // case of the `switch` code as it was previously (with a direct
4049 // call to the runtime not using a type checking slow path).
4050 // This should also be beneficial for the other cases above.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004051 DCHECK(locations->OnlyCallsOnSlowPath());
4052 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4053 /* is_fatal */ false);
4054 codegen_->AddSlowPath(slow_path);
4055 __ B(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004056 if (zero.IsLinked()) {
4057 __ B(&done);
4058 }
4059 break;
4060 }
4061 }
4062
4063 if (zero.IsLinked()) {
4064 __ Bind(&zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004065 __ Mov(out, 0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004066 }
4067
4068 if (done.IsLinked()) {
4069 __ Bind(&done);
4070 }
4071
4072 if (slow_path != nullptr) {
4073 __ Bind(slow_path->GetExitLabel());
4074 }
4075}
4076
4077void LocationsBuilderARM64::VisitCheckCast(HCheckCast* instruction) {
4078 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4079 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
4080
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004081 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
4082 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004083 case TypeCheckKind::kExactCheck:
4084 case TypeCheckKind::kAbstractClassCheck:
4085 case TypeCheckKind::kClassHierarchyCheck:
4086 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004087 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
4088 LocationSummary::kCallOnSlowPath :
4089 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004090 break;
4091 case TypeCheckKind::kArrayCheck:
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004092 case TypeCheckKind::kUnresolvedCheck:
4093 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004094 call_kind = LocationSummary::kCallOnSlowPath;
4095 break;
4096 }
4097
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004098 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4099 locations->SetInAt(0, Location::RequiresRegister());
4100 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004101 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathARM64.
4102 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004103}
4104
4105void InstructionCodeGeneratorARM64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain44015862016-01-22 11:47:17 +00004106 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004107 LocationSummary* locations = instruction->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004108 Location obj_loc = locations->InAt(0);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004109 Register obj = InputRegisterAt(instruction, 0);
4110 Register cls = InputRegisterAt(instruction, 1);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004111 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
4112 DCHECK_GE(num_temps, 1u);
4113 DCHECK_LE(num_temps, 3u);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004114 Location temp_loc = locations->GetTemp(0);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004115 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
4116 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004117 Register temp = WRegisterFrom(temp_loc);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004118 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4119 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4120 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4121 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
4122 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
4123 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
4124 const uint32_t object_array_data_offset =
4125 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004126
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004127 bool is_type_check_slow_path_fatal = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004128 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
4129 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
4130 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004131 if (!kEmitCompilerReadBarrier) {
4132 is_type_check_slow_path_fatal =
4133 (type_check_kind == TypeCheckKind::kExactCheck ||
4134 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
4135 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
4136 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
4137 !instruction->CanThrowIntoCatchBlock();
4138 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004139 SlowPathCodeARM64* type_check_slow_path =
4140 new (GetGraph()->GetArena()) TypeCheckSlowPathARM64(instruction,
4141 is_type_check_slow_path_fatal);
4142 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004143
Scott Wakeling97c72b72016-06-24 16:19:36 +01004144 vixl::aarch64::Label done;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004145 // Avoid null check if we know obj is not null.
4146 if (instruction->MustDoNullCheck()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01004147 __ Cbz(obj, &done);
4148 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004149
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004150 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004151 case TypeCheckKind::kExactCheck:
4152 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004153 // /* HeapReference<Class> */ temp = obj->klass_
4154 GenerateReferenceLoadTwoRegisters(instruction,
4155 temp_loc,
4156 obj_loc,
4157 class_offset,
4158 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004159 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004160
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004161 __ Cmp(temp, cls);
4162 // Jump to slow path for throwing the exception or doing a
4163 // more involved array check.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004164 __ B(ne, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004165 break;
4166 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004167
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004168 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004169 // /* HeapReference<Class> */ temp = obj->klass_
4170 GenerateReferenceLoadTwoRegisters(instruction,
4171 temp_loc,
4172 obj_loc,
4173 class_offset,
4174 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004175 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004176
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004177 // If the class is abstract, we eagerly fetch the super class of the
4178 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004179 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004180 __ Bind(&loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004181 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004182 GenerateReferenceLoadOneRegister(instruction,
4183 temp_loc,
4184 super_offset,
4185 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004186 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004187
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004188 // If the class reference currently in `temp` is null, jump to the slow path to throw the
4189 // exception.
4190 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4191 // Otherwise, compare classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004192 __ Cmp(temp, cls);
4193 __ B(ne, &loop);
4194 break;
4195 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004196
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004197 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004198 // /* HeapReference<Class> */ temp = obj->klass_
4199 GenerateReferenceLoadTwoRegisters(instruction,
4200 temp_loc,
4201 obj_loc,
4202 class_offset,
4203 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004204 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004206 // Walk over the class hierarchy to find a match.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004207 vixl::aarch64::Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004208 __ Bind(&loop);
4209 __ Cmp(temp, cls);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004210 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004211
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004212 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004213 GenerateReferenceLoadOneRegister(instruction,
4214 temp_loc,
4215 super_offset,
4216 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004217 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004218
4219 // If the class reference currently in `temp` is not null, jump
4220 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004221 __ Cbnz(temp, &loop);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004222 // Otherwise, jump to the slow path to throw the exception.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004223 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004224 break;
4225 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004227 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004228 // /* HeapReference<Class> */ temp = obj->klass_
4229 GenerateReferenceLoadTwoRegisters(instruction,
4230 temp_loc,
4231 obj_loc,
4232 class_offset,
4233 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004234 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004235
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01004236 // Do an exact check.
4237 __ Cmp(temp, cls);
4238 __ B(eq, &done);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004239
4240 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004241 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08004242 GenerateReferenceLoadOneRegister(instruction,
4243 temp_loc,
4244 component_offset,
4245 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004246 kWithoutReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004247
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004248 // If the component type is null, jump to the slow path to throw the exception.
4249 __ Cbz(temp, type_check_slow_path->GetEntryLabel());
4250 // Otherwise, the object is indeed an array. Further check that this component type is not a
4251 // primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004252 __ Ldrh(temp, HeapOperand(temp, primitive_offset));
4253 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08004254 __ Cbnz(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004255 break;
4256 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004257
Calin Juravle98893e12015-10-02 21:05:03 +01004258 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004259 // We always go into the type check slow path for the unresolved check cases.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004260 //
4261 // We cannot directly call the CheckCast runtime entry point
4262 // without resorting to a type checking slow path here (i.e. by
4263 // calling InvokeRuntime directly), as it would require to
4264 // assign fixed registers for the inputs of this HInstanceOf
4265 // instruction (following the runtime calling convention), which
4266 // might be cluttered by the potential first read barrier
4267 // emission at the beginning of this method.
4268 __ B(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004269 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004270 case TypeCheckKind::kInterfaceCheck: {
4271 // /* HeapReference<Class> */ temp = obj->klass_
4272 GenerateReferenceLoadTwoRegisters(instruction,
4273 temp_loc,
4274 obj_loc,
4275 class_offset,
4276 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004277 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004278
4279 // /* HeapReference<Class> */ temp = temp->iftable_
4280 GenerateReferenceLoadTwoRegisters(instruction,
4281 temp_loc,
4282 temp_loc,
4283 iftable_offset,
4284 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004285 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08004286 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004287 __ Ldr(WRegisterFrom(maybe_temp2_loc), HeapOperand(temp.W(), array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08004288 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004289 vixl::aarch64::Label start_loop;
4290 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004291 __ Cbz(WRegisterFrom(maybe_temp2_loc), type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004292 __ Ldr(WRegisterFrom(maybe_temp3_loc), HeapOperand(temp.W(), object_array_data_offset));
4293 GetAssembler()->MaybeUnpoisonHeapReference(WRegisterFrom(maybe_temp3_loc));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004294 // Go to next interface.
4295 __ Add(temp, temp, 2 * kHeapReferenceSize);
4296 __ Sub(WRegisterFrom(maybe_temp2_loc), WRegisterFrom(maybe_temp2_loc), 2);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08004297 // Compare the classes and continue the loop if they do not match.
4298 __ Cmp(cls, WRegisterFrom(maybe_temp3_loc));
4299 __ B(ne, &start_loop);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07004300 break;
4301 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004302 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00004303 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00004304
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004305 __ Bind(type_check_slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004306}
4307
Alexandre Rames5319def2014-10-23 10:03:10 +01004308void LocationsBuilderARM64::VisitIntConstant(HIntConstant* constant) {
4309 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4310 locations->SetOut(Location::ConstantLocation(constant));
4311}
4312
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004313void InstructionCodeGeneratorARM64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01004314 // Will be generated at use site.
4315}
4316
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004317void LocationsBuilderARM64::VisitNullConstant(HNullConstant* constant) {
4318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
4319 locations->SetOut(Location::ConstantLocation(constant));
4320}
4321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004322void InstructionCodeGeneratorARM64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004323 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004324}
4325
Calin Juravle175dc732015-08-25 15:42:32 +01004326void LocationsBuilderARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4327 // The trampoline uses the same calling convention as dex calling conventions,
4328 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4329 // the method_idx.
4330 HandleInvoke(invoke);
4331}
4332
4333void InstructionCodeGeneratorARM64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4334 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4335}
4336
Alexandre Rames5319def2014-10-23 10:03:10 +01004337void LocationsBuilderARM64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01004338 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01004339 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Alexandre Rames5319def2014-10-23 10:03:10 +01004340}
4341
Alexandre Rames67555f72014-11-18 10:55:16 +00004342void LocationsBuilderARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4343 HandleInvoke(invoke);
4344}
4345
4346void InstructionCodeGeneratorARM64::VisitInvokeInterface(HInvokeInterface* invoke) {
4347 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004348 LocationSummary* locations = invoke->GetLocations();
4349 Register temp = XRegisterFrom(locations->GetTemp(0));
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004350 Location receiver = locations->InAt(0);
Alexandre Rames67555f72014-11-18 10:55:16 +00004351 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004352 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Alexandre Rames67555f72014-11-18 10:55:16 +00004353
4354 // The register ip1 is required to be used for the hidden argument in
4355 // art_quick_imt_conflict_trampoline, so prevent VIXL from using it.
Alexandre Ramesd921d642015-04-16 15:07:16 +01004356 MacroAssembler* masm = GetVIXLAssembler();
4357 UseScratchRegisterScope scratch_scope(masm);
Alexandre Rames67555f72014-11-18 10:55:16 +00004358 scratch_scope.Exclude(ip1);
4359 __ Mov(ip1, invoke->GetDexMethodIndex());
4360
Artem Serov914d7a82017-02-07 14:33:49 +00004361 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
Alexandre Rames67555f72014-11-18 10:55:16 +00004362 if (receiver.IsStackSlot()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004363 __ Ldr(temp.W(), StackOperandFrom(receiver));
Artem Serov914d7a82017-02-07 14:33:49 +00004364 {
4365 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4366 // /* HeapReference<Class> */ temp = temp->klass_
4367 __ Ldr(temp.W(), HeapOperand(temp.W(), class_offset));
4368 codegen_->MaybeRecordImplicitNullCheck(invoke);
4369 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004370 } else {
Artem Serov914d7a82017-02-07 14:33:49 +00004371 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004372 // /* HeapReference<Class> */ temp = receiver->klass_
Mathieu Chartiere401d142015-04-22 13:56:20 -07004373 __ Ldr(temp.W(), HeapOperandFrom(receiver, class_offset));
Artem Serov914d7a82017-02-07 14:33:49 +00004374 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexandre Rames67555f72014-11-18 10:55:16 +00004375 }
Artem Serov914d7a82017-02-07 14:33:49 +00004376
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004377 // Instead of simply (possibly) unpoisoning `temp` here, we should
4378 // emit a read barrier for the previous class reference load.
4379 // However this is not required in practice, as this is an
4380 // intermediate/temporary reference and because the current
4381 // concurrent copying collector keeps the from-space memory
4382 // intact/accessible until the end of the marking phase (the
4383 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01004384 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00004385 __ Ldr(temp,
4386 MemOperand(temp, mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
4387 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004388 invoke->GetImtIndex(), kArm64PointerSize));
Alexandre Rames67555f72014-11-18 10:55:16 +00004389 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07004390 __ Ldr(temp, MemOperand(temp, method_offset));
Alexandre Rames67555f72014-11-18 10:55:16 +00004391 // lr = temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004392 __ Ldr(lr, MemOperand(temp, entry_point.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004393
4394 {
4395 // Ensure the pc position is recorded immediately after the `blr` instruction.
4396 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4397
4398 // lr();
4399 __ blr(lr);
4400 DCHECK(!codegen_->IsLeafMethod());
4401 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4402 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004403}
4404
4405void LocationsBuilderARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004406 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004407 if (intrinsic.TryDispatch(invoke)) {
4408 return;
4409 }
4410
Alexandre Rames67555f72014-11-18 10:55:16 +00004411 HandleInvoke(invoke);
4412}
4413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004414void LocationsBuilderARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004415 // Explicit clinit checks triggered by static invokes must have been pruned by
4416 // art::PrepareForRegisterAllocation.
4417 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004418
Nicolas Geoffray331605a2017-03-01 11:01:41 +00004419 IntrinsicLocationsBuilderARM64 intrinsic(GetGraph()->GetArena(), codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08004420 if (intrinsic.TryDispatch(invoke)) {
4421 return;
4422 }
4423
Alexandre Rames67555f72014-11-18 10:55:16 +00004424 HandleInvoke(invoke);
4425}
4426
Andreas Gampe878d58c2015-01-15 23:24:00 -08004427static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM64* codegen) {
4428 if (invoke->GetLocations()->Intrinsified()) {
4429 IntrinsicCodeGeneratorARM64 intrinsic(codegen);
4430 intrinsic.Dispatch(invoke);
4431 return true;
4432 }
4433 return false;
4434}
4435
Vladimir Markodc151b22015-10-15 18:02:30 +01004436HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM64::GetSupportedInvokeStaticOrDirectDispatch(
4437 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Roland Levillain44015862016-01-22 11:47:17 +00004439 // On ARM64 we support all dispatch types.
Vladimir Markodc151b22015-10-15 18:02:30 +01004440 return desired_dispatch_info;
4441}
4442
TatWai Chongd8c052a2016-11-02 16:12:48 +08004443Location CodeGeneratorARM64::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4444 Location temp) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004445 // Make sure that ArtMethod* is passed in kArtMethodRegister as per the calling convention.
Vladimir Marko58155012015-08-19 12:49:41 +00004446 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4447 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004448 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
4449 uint32_t offset =
4450 GetThreadOffset<kArm64PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00004451 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004452 __ Ldr(XRegisterFrom(temp), MemOperand(tr, offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004453 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004454 }
Vladimir Marko58155012015-08-19 12:49:41 +00004455 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004456 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004457 break;
4458 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4459 // Load method address from literal pool.
Alexandre Rames6dc01742015-11-12 14:44:19 +00004460 __ Ldr(XRegisterFrom(temp), DeduplicateUint64Literal(invoke->GetMethodAddress()));
Vladimir Marko58155012015-08-19 12:49:41 +00004461 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004462 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4463 // Add ADRP with its PC-relative DexCache access patch.
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004464 const DexFile& dex_file = invoke->GetDexFileForPcRelativeDexCache();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004465 uint32_t element_offset = invoke->GetDexCacheArrayOffset();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004466 vixl::aarch64::Label* adrp_label = NewPcRelativeDexCacheArrayPatch(dex_file, element_offset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004467 EmitAdrpPlaceholder(adrp_label, XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004468 // Add LDR with its PC-relative DexCache access patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004469 vixl::aarch64::Label* ldr_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004470 NewPcRelativeDexCacheArrayPatch(dex_file, element_offset, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004471 EmitLdrOffsetPlaceholder(ldr_label, XRegisterFrom(temp), XRegisterFrom(temp));
Vladimir Marko58155012015-08-19 12:49:41 +00004472 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004473 }
Vladimir Marko58155012015-08-19 12:49:41 +00004474 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004475 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004476 Register reg = XRegisterFrom(temp);
4477 Register method_reg;
4478 if (current_method.IsRegister()) {
4479 method_reg = XRegisterFrom(current_method);
4480 } else {
4481 DCHECK(invoke->GetLocations()->Intrinsified());
4482 DCHECK(!current_method.IsValid());
4483 method_reg = reg;
4484 __ Ldr(reg.X(), MemOperand(sp, kCurrentMethodStackOffset));
4485 }
Vladimir Markob2c431e2015-08-19 12:45:42 +00004486
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004487 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004488 __ Ldr(reg.X(),
4489 MemOperand(method_reg.X(),
Andreas Gampe542451c2016-07-26 09:02:02 -07004490 ArtMethod::DexCacheResolvedMethodsOffset(kArm64PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004491 // temp = temp[index_in_cache];
Vladimir Marko40ecb122016-04-06 17:33:41 +01004492 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4493 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004494 __ Ldr(reg.X(), MemOperand(reg.X(), GetCachePointerOffset(index_in_cache)));
4495 break;
4496 }
4497 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08004498 return callee_method;
4499}
4500
4501void CodeGeneratorARM64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4502 // All registers are assumed to be correctly set up.
4503 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004504
4505 switch (invoke->GetCodePtrLocation()) {
4506 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4507 __ Bl(&frame_entry_label_);
4508 break;
Vladimir Marko58155012015-08-19 12:49:41 +00004509 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4510 // LR = callee_method->entry_point_from_quick_compiled_code_;
4511 __ Ldr(lr, MemOperand(
Alexandre Rames6dc01742015-11-12 14:44:19 +00004512 XRegisterFrom(callee_method),
Andreas Gampe542451c2016-07-26 09:02:02 -07004513 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize).Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00004514 {
4515 // To ensure that the pc position is recorded immediately after the `blr` instruction
4516 // BLR must be the last instruction emitted in this function.
4517 // Recording the pc will occur right after returning from this function.
4518 ExactAssemblyScope eas(GetVIXLAssembler(),
4519 kInstructionSize,
4520 CodeBufferCheckScope::kExactSize);
4521 // lr()
4522 __ blr(lr);
4523 }
Vladimir Marko58155012015-08-19 12:49:41 +00004524 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00004525 }
Alexandre Rames5319def2014-10-23 10:03:10 +01004526
Andreas Gampe878d58c2015-01-15 23:24:00 -08004527 DCHECK(!IsLeafMethod());
4528}
4529
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004530void CodeGeneratorARM64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004531 // Use the calling convention instead of the location of the receiver, as
4532 // intrinsics may have put the receiver in a different register. In the intrinsics
4533 // slow path, the arguments have been moved to the right place, so here we are
4534 // guaranteed that the receiver is the first register of the calling convention.
4535 InvokeDexCallingConvention calling_convention;
4536 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004537 Register temp = XRegisterFrom(temp_in);
4538 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4539 invoke->GetVTableIndex(), kArm64PointerSize).SizeValue();
4540 Offset class_offset = mirror::Object::ClassOffset();
Andreas Gampe542451c2016-07-26 09:02:02 -07004541 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004542
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004543 DCHECK(receiver.IsRegister());
Artem Serov914d7a82017-02-07 14:33:49 +00004544
4545 {
4546 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
4547 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
4548 // /* HeapReference<Class> */ temp = receiver->klass_
4549 __ Ldr(temp.W(), HeapOperandFrom(LocationFrom(receiver), class_offset));
4550 MaybeRecordImplicitNullCheck(invoke);
4551 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004552 // Instead of simply (possibly) unpoisoning `temp` here, we should
4553 // emit a read barrier for the previous class reference load.
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004554 // intermediate/temporary reference and because the current
4555 // concurrent copying collector keeps the from-space memory
4556 // intact/accessible until the end of the marking phase (the
4557 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004558 GetAssembler()->MaybeUnpoisonHeapReference(temp.W());
4559 // temp = temp->GetMethodAt(method_offset);
4560 __ Ldr(temp, MemOperand(temp, method_offset));
4561 // lr = temp->GetEntryPoint();
4562 __ Ldr(lr, MemOperand(temp, entry_point.SizeValue()));
Artem Serov914d7a82017-02-07 14:33:49 +00004563 {
4564 // To ensure that the pc position is recorded immediately after the `blr` instruction
4565 // BLR should be the last instruction emitted in this function.
4566 // Recording the pc will occur right after returning from this function.
4567 ExactAssemblyScope eas(GetVIXLAssembler(), kInstructionSize, CodeBufferCheckScope::kExactSize);
4568 // lr();
4569 __ blr(lr);
4570 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004571}
4572
Orion Hodsonac141392017-01-13 11:53:47 +00004573void LocationsBuilderARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4574 HandleInvoke(invoke);
4575}
4576
4577void InstructionCodeGeneratorARM64::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
4578 codegen_->GenerateInvokePolymorphicCall(invoke);
4579}
4580
Scott Wakeling97c72b72016-06-24 16:19:36 +01004581vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeStringPatch(
4582 const DexFile& dex_file,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004583 dex::StringIndex string_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004584 vixl::aarch64::Label* adrp_label) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004585 return
4586 NewPcRelativePatch(dex_file, string_index.index_, adrp_label, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004587}
4588
Scott Wakeling97c72b72016-06-24 16:19:36 +01004589vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeTypePatch(
4590 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004591 dex::TypeIndex type_index,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004592 vixl::aarch64::Label* adrp_label) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004593 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004594}
4595
Vladimir Marko1998cd02017-01-13 13:02:58 +00004596vixl::aarch64::Label* CodeGeneratorARM64::NewBssEntryTypePatch(
4597 const DexFile& dex_file,
4598 dex::TypeIndex type_index,
4599 vixl::aarch64::Label* adrp_label) {
4600 return NewPcRelativePatch(dex_file, type_index.index_, adrp_label, &type_bss_entry_patches_);
4601}
4602
Scott Wakeling97c72b72016-06-24 16:19:36 +01004603vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativeDexCacheArrayPatch(
4604 const DexFile& dex_file,
4605 uint32_t element_offset,
4606 vixl::aarch64::Label* adrp_label) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004607 return NewPcRelativePatch(dex_file, element_offset, adrp_label, &pc_relative_dex_cache_patches_);
4608}
4609
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004610vixl::aarch64::Label* CodeGeneratorARM64::NewBakerReadBarrierPatch(uint32_t custom_data) {
4611 baker_read_barrier_patches_.emplace_back(custom_data);
4612 return &baker_read_barrier_patches_.back().label;
4613}
4614
Scott Wakeling97c72b72016-06-24 16:19:36 +01004615vixl::aarch64::Label* CodeGeneratorARM64::NewPcRelativePatch(
4616 const DexFile& dex_file,
4617 uint32_t offset_or_index,
4618 vixl::aarch64::Label* adrp_label,
4619 ArenaDeque<PcRelativePatchInfo>* patches) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004620 // Add a patch entry and return the label.
4621 patches->emplace_back(dex_file, offset_or_index);
4622 PcRelativePatchInfo* info = &patches->back();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004623 vixl::aarch64::Label* label = &info->label;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004624 // If adrp_label is null, this is the ADRP patch and needs to point to its own label.
4625 info->pc_insn_label = (adrp_label != nullptr) ? adrp_label : label;
4626 return label;
4627}
4628
Scott Wakeling97c72b72016-06-24 16:19:36 +01004629vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageStringLiteral(
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004630 const DexFile& dex_file, dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004631 return boot_image_string_patches_.GetOrCreate(
4632 StringReference(&dex_file, string_index),
4633 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4634}
4635
Scott Wakeling97c72b72016-06-24 16:19:36 +01004636vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageTypeLiteral(
Andreas Gampea5b09a62016-11-17 15:21:22 -08004637 const DexFile& dex_file, dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004638 return boot_image_type_patches_.GetOrCreate(
4639 TypeReference(&dex_file, type_index),
4640 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4641}
4642
Scott Wakeling97c72b72016-06-24 16:19:36 +01004643vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateBootImageAddressLiteral(
4644 uint64_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00004645 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004646}
4647
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004648vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitStringLiteral(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00004649 const DexFile& dex_file, dex::StringIndex string_index, Handle<mirror::String> handle) {
4650 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
4651 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00004652 return jit_string_patches_.GetOrCreate(
4653 StringReference(&dex_file, string_index),
4654 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4655}
4656
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004657vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateJitClassLiteral(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004658 const DexFile& dex_file, dex::TypeIndex type_index, Handle<mirror::Class> handle) {
4659 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
4660 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004661 return jit_class_patches_.GetOrCreate(
4662 TypeReference(&dex_file, type_index),
4663 [this]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(/* placeholder */ 0u); });
4664}
4665
Vladimir Markoaad75c62016-10-03 08:46:48 +00004666void CodeGeneratorARM64::EmitAdrpPlaceholder(vixl::aarch64::Label* fixup_label,
4667 vixl::aarch64::Register reg) {
4668 DCHECK(reg.IsX());
4669 SingleEmissionCheckScope guard(GetVIXLAssembler());
4670 __ Bind(fixup_label);
Scott Wakelingb77051e2016-11-21 19:46:00 +00004671 __ adrp(reg, /* offset placeholder */ static_cast<int64_t>(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004672}
4673
4674void CodeGeneratorARM64::EmitAddPlaceholder(vixl::aarch64::Label* fixup_label,
4675 vixl::aarch64::Register out,
4676 vixl::aarch64::Register base) {
4677 DCHECK(out.IsX());
4678 DCHECK(base.IsX());
4679 SingleEmissionCheckScope guard(GetVIXLAssembler());
4680 __ Bind(fixup_label);
4681 __ add(out, base, Operand(/* offset placeholder */ 0));
4682}
4683
4684void CodeGeneratorARM64::EmitLdrOffsetPlaceholder(vixl::aarch64::Label* fixup_label,
4685 vixl::aarch64::Register out,
4686 vixl::aarch64::Register base) {
4687 DCHECK(base.IsX());
4688 SingleEmissionCheckScope guard(GetVIXLAssembler());
4689 __ Bind(fixup_label);
4690 __ ldr(out, MemOperand(base, /* offset placeholder */ 0));
4691}
4692
4693template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4694inline void CodeGeneratorARM64::EmitPcRelativeLinkerPatches(
4695 const ArenaDeque<PcRelativePatchInfo>& infos,
4696 ArenaVector<LinkerPatch>* linker_patches) {
4697 for (const PcRelativePatchInfo& info : infos) {
4698 linker_patches->push_back(Factory(info.label.GetLocation(),
4699 &info.target_dex_file,
4700 info.pc_insn_label->GetLocation(),
4701 info.offset_or_index));
4702 }
4703}
4704
Vladimir Marko58155012015-08-19 12:49:41 +00004705void CodeGeneratorARM64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4706 DCHECK(linker_patches->empty());
4707 size_t size =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004708 pc_relative_dex_cache_patches_.size() +
4709 boot_image_string_patches_.size() +
4710 pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004711 boot_image_type_patches_.size() +
4712 pc_relative_type_patches_.size() +
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004713 type_bss_entry_patches_.size() +
4714 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00004715 linker_patches->reserve(size);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004716 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01004717 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(info.label.GetLocation(),
Vladimir Marko58155012015-08-19 12:49:41 +00004718 &info.target_dex_file,
Scott Wakeling97c72b72016-06-24 16:19:36 +01004719 info.pc_insn_label->GetLocation(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004720 info.offset_or_index));
4721 }
4722 for (const auto& entry : boot_image_string_patches_) {
4723 const StringReference& target_string = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004724 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4725 linker_patches->push_back(LinkerPatch::StringPatch(literal->GetOffset(),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004726 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004727 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004728 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004729 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00004730 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00004731 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
4732 linker_patches);
4733 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004734 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
4735 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004736 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
4737 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004738 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004739 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
4740 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004741 for (const auto& entry : boot_image_type_patches_) {
4742 const TypeReference& target_type = entry.first;
Scott Wakeling97c72b72016-06-24 16:19:36 +01004743 vixl::aarch64::Literal<uint32_t>* literal = entry.second;
4744 linker_patches->push_back(LinkerPatch::TypePatch(literal->GetOffset(),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004745 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004746 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004747 }
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004748 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
4749 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.GetLocation(),
4750 info.custom_data));
4751 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00004752 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00004753}
4754
Scott Wakeling97c72b72016-06-24 16:19:36 +01004755vixl::aarch64::Literal<uint32_t>* CodeGeneratorARM64::DeduplicateUint32Literal(uint32_t value,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004756 Uint32ToLiteralMap* map) {
4757 return map->GetOrCreate(
4758 value,
4759 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint32_t>(value); });
4760}
4761
Scott Wakeling97c72b72016-06-24 16:19:36 +01004762vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateUint64Literal(uint64_t value) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004763 return uint64_literals_.GetOrCreate(
4764 value,
4765 [this, value]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00004766}
4767
Scott Wakeling97c72b72016-06-24 16:19:36 +01004768vixl::aarch64::Literal<uint64_t>* CodeGeneratorARM64::DeduplicateMethodLiteral(
Vladimir Marko58155012015-08-19 12:49:41 +00004769 MethodReference target_method,
4770 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004771 return map->GetOrCreate(
4772 target_method,
4773 [this]() { return __ CreateLiteralDestroyedWithPool<uint64_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00004774}
4775
Andreas Gampe878d58c2015-01-15 23:24:00 -08004776void InstructionCodeGeneratorARM64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00004777 // Explicit clinit checks triggered by static invokes must have been pruned by
4778 // art::PrepareForRegisterAllocation.
4779 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004780
Andreas Gampe878d58c2015-01-15 23:24:00 -08004781 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4782 return;
4783 }
4784
Artem Serov914d7a82017-02-07 14:33:49 +00004785 // Ensure that between the BLR (emitted by GenerateStaticOrDirectCall) and RecordPcInfo there
4786 // are no pools emitted.
4787 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01004788 LocationSummary* locations = invoke->GetLocations();
4789 codegen_->GenerateStaticOrDirectCall(
4790 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00004791 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Alexandre Rames5319def2014-10-23 10:03:10 +01004792}
4793
4794void InstructionCodeGeneratorARM64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08004795 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
4796 return;
4797 }
4798
Artem Serov914d7a82017-02-07 14:33:49 +00004799 // Ensure that between the BLR (emitted by GenerateVirtualCall) and RecordPcInfo there
4800 // are no pools emitted.
4801 EmissionCheckScope guard(GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004802 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Alexandre Rames5319def2014-10-23 10:03:10 +01004803 DCHECK(!codegen_->IsLeafMethod());
4804 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
4805}
4806
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004807HLoadClass::LoadKind CodeGeneratorARM64::GetSupportedLoadClassKind(
4808 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004809 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004810 case HLoadClass::LoadKind::kInvalid:
4811 LOG(FATAL) << "UNREACHABLE";
4812 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004813 case HLoadClass::LoadKind::kReferrersClass:
4814 break;
4815 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
4816 DCHECK(!GetCompilerOptions().GetCompilePic());
4817 break;
4818 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
4819 DCHECK(GetCompilerOptions().GetCompilePic());
4820 break;
4821 case HLoadClass::LoadKind::kBootImageAddress:
4822 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004823 case HLoadClass::LoadKind::kBssEntry:
4824 DCHECK(!Runtime::Current()->UseJitCompilation());
4825 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004826 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004827 DCHECK(Runtime::Current()->UseJitCompilation());
4828 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004829 case HLoadClass::LoadKind::kDexCacheViaMethod:
4830 break;
4831 }
4832 return desired_class_load_kind;
4833}
4834
Alexandre Rames67555f72014-11-18 10:55:16 +00004835void LocationsBuilderARM64::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00004836 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4837 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004838 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00004839 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004840 cls,
4841 LocationFrom(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00004842 LocationFrom(vixl::aarch64::x0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004843 DCHECK(calling_convention.GetRegisterAt(0).Is(vixl::aarch64::x0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004844 return;
4845 }
Vladimir Marko41559982017-01-06 14:04:23 +00004846 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004847
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004848 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
4849 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004850 ? LocationSummary::kCallOnSlowPath
4851 : LocationSummary::kNoCall;
4852 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004853 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004854 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004855 }
4856
Vladimir Marko41559982017-01-06 14:04:23 +00004857 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004858 locations->SetInAt(0, Location::RequiresRegister());
4859 }
4860 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004861 if (cls->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
4862 if (!kUseReadBarrier || kUseBakerReadBarrier) {
4863 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004864 locations->AddTemp(FixedTempLocation());
Vladimir Markoea4c1262017-02-06 19:59:33 +00004865 RegisterSet caller_saves = RegisterSet::Empty();
4866 InvokeRuntimeCallingConvention calling_convention;
4867 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
4868 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
4869 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
4870 Primitive::kPrimNot).GetCode());
4871 locations->SetCustomSlowPathCallerSaves(caller_saves);
4872 } else {
4873 // For non-Baker read barrier we have a temp-clobbering call.
4874 }
4875 }
Alexandre Rames67555f72014-11-18 10:55:16 +00004876}
4877
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004878// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
4879// move.
4880void InstructionCodeGeneratorARM64::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00004881 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
4882 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
4883 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01004884 return;
4885 }
Vladimir Marko41559982017-01-06 14:04:23 +00004886 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01004887
Roland Levillain22ccc3a2015-11-24 13:10:05 +00004888 Location out_loc = cls->GetLocations()->Out();
Calin Juravle580b6092015-10-06 17:35:58 +01004889 Register out = OutputRegister(cls);
Vladimir Markoea4c1262017-02-06 19:59:33 +00004890 Register bss_entry_temp;
4891 vixl::aarch64::Label* bss_entry_adrp_label = nullptr;
Alexandre Rames67555f72014-11-18 10:55:16 +00004892
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004893 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
4894 ? kWithoutReadBarrier
4895 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004896 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00004897 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004898 case HLoadClass::LoadKind::kReferrersClass: {
4899 DCHECK(!cls->CanCallRuntime());
4900 DCHECK(!cls->MustGenerateClinitCheck());
4901 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
4902 Register current_method = InputRegisterAt(cls, 0);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004903 GenerateGcRootFieldLoad(cls,
4904 out_loc,
4905 current_method,
4906 ArtMethod::DeclaringClassOffset().Int32Value(),
Roland Levillain00468f32016-10-27 18:02:48 +01004907 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004908 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004909 break;
4910 }
4911 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004912 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004913 __ Ldr(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
4914 cls->GetTypeIndex()));
4915 break;
4916 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004917 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004918 // Add ADRP with its PC-relative type patch.
4919 const DexFile& dex_file = cls->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -08004920 dex::TypeIndex type_index = cls->GetTypeIndex();
Scott Wakeling97c72b72016-06-24 16:19:36 +01004921 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeTypePatch(dex_file, type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004922 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004923 // Add ADD with its PC-relative type patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01004924 vixl::aarch64::Label* add_label =
4925 codegen_->NewPcRelativeTypePatch(dex_file, type_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004926 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004927 break;
4928 }
4929 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08004930 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004931 uint32_t address = dchecked_integral_cast<uint32_t>(
4932 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
4933 DCHECK_NE(address, 0u);
4934 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004935 break;
4936 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004937 case HLoadClass::LoadKind::kBssEntry: {
4938 // Add ADRP with its PC-relative Class .bss entry patch.
4939 const DexFile& dex_file = cls->GetDexFile();
4940 dex::TypeIndex type_index = cls->GetTypeIndex();
Vladimir Markof4f2daa2017-03-20 18:26:59 +00004941 bss_entry_temp = XRegisterFrom(cls->GetLocations()->GetTemp(0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00004942 bss_entry_adrp_label = codegen_->NewBssEntryTypePatch(dex_file, type_index);
4943 codegen_->EmitAdrpPlaceholder(bss_entry_adrp_label, bss_entry_temp);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004944 // Add LDR with its PC-relative Class patch.
4945 vixl::aarch64::Label* ldr_label =
Vladimir Markoea4c1262017-02-06 19:59:33 +00004946 codegen_->NewBssEntryTypePatch(dex_file, type_index, bss_entry_adrp_label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004947 // /* GcRoot<mirror::Class> */ out = *(base_address + offset) /* PC-relative */
4948 GenerateGcRootFieldLoad(cls,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004949 out_loc,
4950 bss_entry_temp,
4951 /* offset placeholder */ 0u,
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004952 ldr_label,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004953 read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00004954 generate_null_check = true;
4955 break;
4956 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004957 case HLoadClass::LoadKind::kJitTableAddress: {
4958 __ Ldr(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
4959 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004960 cls->GetClass()));
Mathieu Chartier31b12e32016-09-02 17:11:57 -07004961 GenerateGcRootFieldLoad(cls,
4962 out_loc,
4963 out.X(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00004964 /* offset */ 0,
Roland Levillain00468f32016-10-27 18:02:48 +01004965 /* fixup_label */ nullptr,
Vladimir Markoea4c1262017-02-06 19:59:33 +00004966 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004967 break;
4968 }
Vladimir Marko41559982017-01-06 14:04:23 +00004969 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00004970 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00004971 LOG(FATAL) << "UNREACHABLE";
4972 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004973 }
4974
Vladimir Markoea4c1262017-02-06 19:59:33 +00004975 bool do_clinit = cls->MustGenerateClinitCheck();
4976 if (generate_null_check || do_clinit) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004977 DCHECK(cls->CanCallRuntime());
4978 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM64(
Vladimir Markoea4c1262017-02-06 19:59:33 +00004979 cls, cls, cls->GetDexPc(), do_clinit, bss_entry_temp, bss_entry_adrp_label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004980 codegen_->AddSlowPath(slow_path);
4981 if (generate_null_check) {
4982 __ Cbz(out, slow_path->GetEntryLabel());
4983 }
4984 if (cls->MustGenerateClinitCheck()) {
4985 GenerateClassInitializationCheck(slow_path, out);
4986 } else {
4987 __ Bind(slow_path->GetExitLabel());
Alexandre Rames67555f72014-11-18 10:55:16 +00004988 }
4989 }
4990}
4991
David Brazdilcb1c0552015-08-04 16:22:25 +01004992static MemOperand GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07004993 return MemOperand(tr, Thread::ExceptionOffset<kArm64PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01004994}
4995
Alexandre Rames67555f72014-11-18 10:55:16 +00004996void LocationsBuilderARM64::VisitLoadException(HLoadException* load) {
4997 LocationSummary* locations =
4998 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
4999 locations->SetOut(Location::RequiresRegister());
5000}
5001
5002void InstructionCodeGeneratorARM64::VisitLoadException(HLoadException* instruction) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005003 __ Ldr(OutputRegister(instruction), GetExceptionTlsAddress());
5004}
5005
5006void LocationsBuilderARM64::VisitClearException(HClearException* clear) {
5007 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5008}
5009
5010void InstructionCodeGeneratorARM64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5011 __ Str(wzr, GetExceptionTlsAddress());
Alexandre Rames67555f72014-11-18 10:55:16 +00005012}
5013
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005014HLoadString::LoadKind CodeGeneratorARM64::GetSupportedLoadStringKind(
5015 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005016 switch (desired_string_load_kind) {
5017 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5018 DCHECK(!GetCompilerOptions().GetCompilePic());
5019 break;
5020 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5021 DCHECK(GetCompilerOptions().GetCompilePic());
5022 break;
5023 case HLoadString::LoadKind::kBootImageAddress:
5024 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005025 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005026 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005027 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005028 case HLoadString::LoadKind::kJitTableAddress:
5029 DCHECK(Runtime::Current()->UseJitCompilation());
5030 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005031 case HLoadString::LoadKind::kDexCacheViaMethod:
5032 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005033 }
5034 return desired_string_load_kind;
5035}
5036
Alexandre Rames67555f72014-11-18 10:55:16 +00005037void LocationsBuilderARM64::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005038 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005039 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005040 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005041 InvokeRuntimeCallingConvention calling_convention;
5042 locations->SetOut(calling_convention.GetReturnLocation(load->GetType()));
5043 } else {
5044 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005045 if (load->GetLoadKind() == HLoadString::LoadKind::kBssEntry) {
5046 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005047 // Rely on the pResolveString and marking to save everything we need.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005048 locations->AddTemp(FixedTempLocation());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005049 RegisterSet caller_saves = RegisterSet::Empty();
5050 InvokeRuntimeCallingConvention calling_convention;
5051 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
5052 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(),
5053 RegisterFrom(calling_convention.GetReturnLocation(Primitive::kPrimNot),
5054 Primitive::kPrimNot).GetCode());
5055 locations->SetCustomSlowPathCallerSaves(caller_saves);
5056 } else {
5057 // For non-Baker read barrier we have a temp-clobbering call.
5058 }
5059 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005060 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005061}
5062
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005063// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5064// move.
5065void InstructionCodeGeneratorARM64::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexandre Rames67555f72014-11-18 10:55:16 +00005066 Register out = OutputRegister(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005067 Location out_loc = load->GetLocations()->Out();
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005068
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005069 switch (load->GetLoadKind()) {
5070 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005071 __ Ldr(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5072 load->GetStringIndex()));
5073 return; // No dex cache slow path.
5074 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005075 // Add ADRP with its PC-relative String patch.
5076 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005077 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005078 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Scott Wakeling97c72b72016-06-24 16:19:36 +01005079 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005080 codegen_->EmitAdrpPlaceholder(adrp_label, out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005081 // Add ADD with its PC-relative String patch.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005082 vixl::aarch64::Label* add_label =
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005083 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005084 codegen_->EmitAddPlaceholder(add_label, out.X(), out.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005085 return; // No dex cache slow path.
5086 }
5087 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005088 uint32_t address = dchecked_integral_cast<uint32_t>(
5089 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5090 DCHECK_NE(address, 0u);
5091 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005092 return; // No dex cache slow path.
5093 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005094 case HLoadString::LoadKind::kBssEntry: {
5095 // Add ADRP with its PC-relative String .bss entry patch.
5096 const DexFile& dex_file = load->GetDexFile();
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005097 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005098 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005099 Register temp = XRegisterFrom(load->GetLocations()->GetTemp(0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005100 vixl::aarch64::Label* adrp_label = codegen_->NewPcRelativeStringPatch(dex_file, string_index);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005101 codegen_->EmitAdrpPlaceholder(adrp_label, temp);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005102 // Add LDR with its PC-relative String patch.
5103 vixl::aarch64::Label* ldr_label =
5104 codegen_->NewPcRelativeStringPatch(dex_file, string_index, adrp_label);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005105 // /* GcRoot<mirror::String> */ out = *(base_address + offset) /* PC-relative */
Vladimir Markoaad75c62016-10-03 08:46:48 +00005106 GenerateGcRootFieldLoad(load,
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005107 out_loc,
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005108 temp,
Roland Levillain00468f32016-10-27 18:02:48 +01005109 /* offset placeholder */ 0u,
5110 ldr_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005111 kCompilerReadBarrierOption);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005112 SlowPathCodeARM64* slow_path =
5113 new (GetGraph()->GetArena()) LoadStringSlowPathARM64(load, temp, adrp_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005114 codegen_->AddSlowPath(slow_path);
5115 __ Cbz(out.X(), slow_path->GetEntryLabel());
5116 __ Bind(slow_path->GetExitLabel());
5117 return;
5118 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005119 case HLoadString::LoadKind::kJitTableAddress: {
5120 __ Ldr(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005121 load->GetStringIndex(),
5122 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005123 GenerateGcRootFieldLoad(load,
5124 out_loc,
5125 out.X(),
5126 /* offset */ 0,
5127 /* fixup_label */ nullptr,
5128 kCompilerReadBarrierOption);
5129 return;
5130 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005131 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005132 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005133 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +00005134
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005135 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005136 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005137 DCHECK_EQ(calling_convention.GetRegisterAt(0).GetCode(), out.GetCode());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005138 __ Mov(calling_convention.GetRegisterAt(0).W(), load->GetStringIndex().index_);
Christina Wadsworth1fe89ea2016-08-31 16:14:38 -07005139 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5140 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005141}
5142
Alexandre Rames5319def2014-10-23 10:03:10 +01005143void LocationsBuilderARM64::VisitLongConstant(HLongConstant* constant) {
5144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
5145 locations->SetOut(Location::ConstantLocation(constant));
5146}
5147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005148void InstructionCodeGeneratorARM64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005149 // Will be generated at use site.
5150}
5151
Alexandre Rames67555f72014-11-18 10:55:16 +00005152void LocationsBuilderARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
5153 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005155 InvokeRuntimeCallingConvention calling_convention;
5156 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5157}
5158
5159void InstructionCodeGeneratorARM64::VisitMonitorOperation(HMonitorOperation* instruction) {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01005160 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005161 instruction,
5162 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005163 if (instruction->IsEnter()) {
5164 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
5165 } else {
5166 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
5167 }
Alexandre Rames67555f72014-11-18 10:55:16 +00005168}
5169
Alexandre Rames42d641b2014-10-27 14:00:51 +00005170void LocationsBuilderARM64::VisitMul(HMul* mul) {
5171 LocationSummary* locations =
5172 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
5173 switch (mul->GetResultType()) {
5174 case Primitive::kPrimInt:
5175 case Primitive::kPrimLong:
5176 locations->SetInAt(0, Location::RequiresRegister());
5177 locations->SetInAt(1, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005179 break;
5180
5181 case Primitive::kPrimFloat:
5182 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005183 locations->SetInAt(0, Location::RequiresFpuRegister());
5184 locations->SetInAt(1, Location::RequiresFpuRegister());
Alexandre Rames67555f72014-11-18 10:55:16 +00005185 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Rames42d641b2014-10-27 14:00:51 +00005186 break;
5187
5188 default:
5189 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5190 }
5191}
5192
5193void InstructionCodeGeneratorARM64::VisitMul(HMul* mul) {
5194 switch (mul->GetResultType()) {
5195 case Primitive::kPrimInt:
5196 case Primitive::kPrimLong:
5197 __ Mul(OutputRegister(mul), InputRegisterAt(mul, 0), InputRegisterAt(mul, 1));
5198 break;
5199
5200 case Primitive::kPrimFloat:
5201 case Primitive::kPrimDouble:
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005202 __ Fmul(OutputFPRegister(mul), InputFPRegisterAt(mul, 0), InputFPRegisterAt(mul, 1));
Alexandre Rames42d641b2014-10-27 14:00:51 +00005203 break;
5204
5205 default:
5206 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
5207 }
5208}
5209
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005210void LocationsBuilderARM64::VisitNeg(HNeg* neg) {
5211 LocationSummary* locations =
5212 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
5213 switch (neg->GetResultType()) {
5214 case Primitive::kPrimInt:
Alexandre Rames67555f72014-11-18 10:55:16 +00005215 case Primitive::kPrimLong:
Serban Constantinescu2d35d9d2015-02-22 22:08:01 +00005216 locations->SetInAt(0, ARM64EncodableConstantOrRegister(neg->InputAt(0), neg));
Alexandre Rames67555f72014-11-18 10:55:16 +00005217 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005218 break;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005219
5220 case Primitive::kPrimFloat:
5221 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005222 locations->SetInAt(0, Location::RequiresFpuRegister());
5223 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005224 break;
5225
5226 default:
5227 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5228 }
5229}
5230
5231void InstructionCodeGeneratorARM64::VisitNeg(HNeg* neg) {
5232 switch (neg->GetResultType()) {
5233 case Primitive::kPrimInt:
5234 case Primitive::kPrimLong:
5235 __ Neg(OutputRegister(neg), InputOperandAt(neg, 0));
5236 break;
5237
5238 case Primitive::kPrimFloat:
5239 case Primitive::kPrimDouble:
Alexandre Rames67555f72014-11-18 10:55:16 +00005240 __ Fneg(OutputFPRegister(neg), InputFPRegisterAt(neg, 0));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005241 break;
5242
5243 default:
5244 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
5245 }
5246}
5247
5248void LocationsBuilderARM64::VisitNewArray(HNewArray* instruction) {
5249 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005250 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005251 InvokeRuntimeCallingConvention calling_convention;
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005252 locations->SetOut(LocationFrom(x0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005253 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5254 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005255}
5256
5257void InstructionCodeGeneratorARM64::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005258 // Note: if heap poisoning is enabled, the entry point takes cares
5259 // of poisoning the reference.
Nicolas Geoffrayb048cb72017-01-23 22:50:24 +00005260 QuickEntrypointEnum entrypoint =
5261 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5262 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005263 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Alexandre Ramesfc19de82014-11-07 17:13:31 +00005264}
5265
Alexandre Rames5319def2014-10-23 10:03:10 +01005266void LocationsBuilderARM64::VisitNewInstance(HNewInstance* instruction) {
5267 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005268 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames5319def2014-10-23 10:03:10 +01005269 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00005270 if (instruction->IsStringAlloc()) {
5271 locations->AddTemp(LocationFrom(kArtMethodRegister));
5272 } else {
5273 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005274 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005275 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
5276}
5277
5278void InstructionCodeGeneratorARM64::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005279 // Note: if heap poisoning is enabled, the entry point takes cares
5280 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005281 if (instruction->IsStringAlloc()) {
5282 // String is allocated through StringFactory. Call NewEmptyString entry point.
5283 Location temp = instruction->GetLocations()->GetTemp(0);
Andreas Gampe542451c2016-07-26 09:02:02 -07005284 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArm64PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005285 __ Ldr(XRegisterFrom(temp), MemOperand(tr, QUICK_ENTRY_POINT(pNewEmptyString)));
5286 __ Ldr(lr, MemOperand(XRegisterFrom(temp), code_offset.Int32Value()));
Artem Serov914d7a82017-02-07 14:33:49 +00005287
5288 {
5289 // Ensure the pc position is recorded immediately after the `blr` instruction.
5290 ExactAssemblyScope eas(GetVIXLAssembler(),
5291 kInstructionSize,
5292 CodeBufferCheckScope::kExactSize);
5293 __ blr(lr);
5294 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5295 }
David Brazdil6de19382016-01-08 17:37:10 +00005296 } else {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005297 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005298 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005299 }
Alexandre Rames5319def2014-10-23 10:03:10 +01005300}
5301
5302void LocationsBuilderARM64::VisitNot(HNot* instruction) {
5303 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexandre Rames4e596512014-11-07 15:56:50 +00005304 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Ramesfb4e5fa2014-11-06 12:41:16 +00005305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Alexandre Rames5319def2014-10-23 10:03:10 +01005306}
5307
5308void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005309 switch (instruction->GetResultType()) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005310 case Primitive::kPrimInt:
Alexandre Rames5319def2014-10-23 10:03:10 +01005311 case Primitive::kPrimLong:
Roland Levillain55dcfb52014-10-24 18:09:09 +01005312 __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
Alexandre Rames5319def2014-10-23 10:03:10 +01005313 break;
5314
5315 default:
5316 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
5317 }
5318}
5319
David Brazdil66d126e2015-04-03 16:02:44 +01005320void LocationsBuilderARM64::VisitBooleanNot(HBooleanNot* instruction) {
5321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5322 locations->SetInAt(0, Location::RequiresRegister());
5323 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5324}
5325
5326void InstructionCodeGeneratorARM64::VisitBooleanNot(HBooleanNot* instruction) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01005327 __ Eor(OutputRegister(instruction), InputRegisterAt(instruction, 0), vixl::aarch64::Operand(1));
David Brazdil66d126e2015-04-03 16:02:44 +01005328}
5329
Alexandre Rames5319def2014-10-23 10:03:10 +01005330void LocationsBuilderARM64::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005331 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5332 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames5319def2014-10-23 10:03:10 +01005333}
5334
Calin Juravle2ae48182016-03-16 14:05:09 +00005335void CodeGeneratorARM64::GenerateImplicitNullCheck(HNullCheck* instruction) {
5336 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 return;
5338 }
Artem Serov914d7a82017-02-07 14:33:49 +00005339 {
5340 // Ensure that between load and MaybeRecordImplicitNullCheck there are no pools emitted.
5341 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
5342 Location obj = instruction->GetLocations()->InAt(0);
5343 __ Ldr(wzr, HeapOperandFrom(obj, Offset(0)));
5344 RecordPcInfo(instruction, instruction->GetDexPc());
5345 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005346}
5347
Calin Juravle2ae48182016-03-16 14:05:09 +00005348void CodeGeneratorARM64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005349 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM64(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005350 AddSlowPath(slow_path);
Alexandre Rames5319def2014-10-23 10:03:10 +01005351
5352 LocationSummary* locations = instruction->GetLocations();
5353 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005354
5355 __ Cbz(RegisterFrom(obj, instruction->InputAt(0)->GetType()), slow_path->GetEntryLabel());
Alexandre Rames5319def2014-10-23 10:03:10 +01005356}
5357
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005358void InstructionCodeGeneratorARM64::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005359 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005360}
5361
Alexandre Rames67555f72014-11-18 10:55:16 +00005362void LocationsBuilderARM64::VisitOr(HOr* instruction) {
5363 HandleBinaryOp(instruction);
5364}
5365
5366void InstructionCodeGeneratorARM64::VisitOr(HOr* instruction) {
5367 HandleBinaryOp(instruction);
5368}
5369
Alexandre Rames3e69f162014-12-10 10:36:50 +00005370void LocationsBuilderARM64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
5371 LOG(FATAL) << "Unreachable";
5372}
5373
5374void InstructionCodeGeneratorARM64::VisitParallelMove(HParallelMove* instruction) {
5375 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5376}
5377
Alexandre Rames5319def2014-10-23 10:03:10 +01005378void LocationsBuilderARM64::VisitParameterValue(HParameterValue* instruction) {
5379 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5380 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5381 if (location.IsStackSlot()) {
5382 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5383 } else if (location.IsDoubleStackSlot()) {
5384 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5385 }
5386 locations->SetOut(location);
5387}
5388
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005389void InstructionCodeGeneratorARM64::VisitParameterValue(
5390 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005391 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005392}
5393
5394void LocationsBuilderARM64::VisitCurrentMethod(HCurrentMethod* instruction) {
5395 LocationSummary* locations =
5396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray38207af2015-06-01 15:46:22 +01005397 locations->SetOut(LocationFrom(kArtMethodRegister));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005398}
5399
5400void InstructionCodeGeneratorARM64::VisitCurrentMethod(
5401 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5402 // Nothing to do, the method is already at its location.
Alexandre Rames5319def2014-10-23 10:03:10 +01005403}
5404
5405void LocationsBuilderARM64::VisitPhi(HPhi* instruction) {
5406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005407 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005408 locations->SetInAt(i, Location::Any());
5409 }
5410 locations->SetOut(Location::Any());
5411}
5412
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005413void InstructionCodeGeneratorARM64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005414 LOG(FATAL) << "Unreachable";
5415}
5416
Serban Constantinescu02164b32014-11-13 14:05:07 +00005417void LocationsBuilderARM64::VisitRem(HRem* rem) {
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005418 Primitive::Type type = rem->GetResultType();
Alexandre Rames542361f2015-01-29 16:57:31 +00005419 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005420 Primitive::IsFloatingPointType(type) ? LocationSummary::kCallOnMainOnly
5421 : LocationSummary::kNoCall;
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
5423
5424 switch (type) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005425 case Primitive::kPrimInt:
5426 case Primitive::kPrimLong:
5427 locations->SetInAt(0, Location::RequiresRegister());
Zheng Xuc6667102015-05-15 16:08:45 +08005428 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Serban Constantinescu02164b32014-11-13 14:05:07 +00005429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5430 break;
5431
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005432 case Primitive::kPrimFloat:
5433 case Primitive::kPrimDouble: {
5434 InvokeRuntimeCallingConvention calling_convention;
5435 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
5436 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
5437 locations->SetOut(calling_convention.GetReturnLocation(type));
5438
5439 break;
5440 }
5441
Serban Constantinescu02164b32014-11-13 14:05:07 +00005442 default:
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005443 LOG(FATAL) << "Unexpected rem type " << type;
Serban Constantinescu02164b32014-11-13 14:05:07 +00005444 }
5445}
5446
5447void InstructionCodeGeneratorARM64::VisitRem(HRem* rem) {
5448 Primitive::Type type = rem->GetResultType();
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005449
Serban Constantinescu02164b32014-11-13 14:05:07 +00005450 switch (type) {
5451 case Primitive::kPrimInt:
5452 case Primitive::kPrimLong: {
Zheng Xuc6667102015-05-15 16:08:45 +08005453 GenerateDivRemIntegral(rem);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005454 break;
5455 }
5456
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005457 case Primitive::kPrimFloat:
5458 case Primitive::kPrimDouble: {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005459 QuickEntrypointEnum entrypoint = (type == Primitive::kPrimFloat) ? kQuickFmodf : kQuickFmod;
5460 codegen_->InvokeRuntime(entrypoint, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005461 if (type == Primitive::kPrimFloat) {
5462 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
5463 } else {
5464 CheckEntrypointTypes<kQuickFmod, double, double, double>();
5465 }
Serban Constantinescu02d81cc2015-01-05 16:08:49 +00005466 break;
5467 }
5468
Serban Constantinescu02164b32014-11-13 14:05:07 +00005469 default:
5470 LOG(FATAL) << "Unexpected rem type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005471 UNREACHABLE();
Serban Constantinescu02164b32014-11-13 14:05:07 +00005472 }
5473}
5474
Calin Juravle27df7582015-04-17 19:12:31 +01005475void LocationsBuilderARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
5476 memory_barrier->SetLocations(nullptr);
5477}
5478
5479void InstructionCodeGeneratorARM64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain44015862016-01-22 11:47:17 +00005480 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01005481}
5482
Alexandre Rames5319def2014-10-23 10:03:10 +01005483void LocationsBuilderARM64::VisitReturn(HReturn* instruction) {
5484 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
5485 Primitive::Type return_type = instruction->InputAt(0)->GetType();
Alexandre Ramesa89086e2014-11-07 17:13:25 +00005486 locations->SetInAt(0, ARM64ReturnLocation(return_type));
Alexandre Rames5319def2014-10-23 10:03:10 +01005487}
5488
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005489void InstructionCodeGeneratorARM64::VisitReturn(HReturn* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005490 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005491}
5492
5493void LocationsBuilderARM64::VisitReturnVoid(HReturnVoid* instruction) {
5494 instruction->SetLocations(nullptr);
5495}
5496
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005497void InstructionCodeGeneratorARM64::VisitReturnVoid(HReturnVoid* instruction ATTRIBUTE_UNUSED) {
Alexandre Rames5319def2014-10-23 10:03:10 +01005498 codegen_->GenerateFrameExit();
Alexandre Rames5319def2014-10-23 10:03:10 +01005499}
5500
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005501void LocationsBuilderARM64::VisitRor(HRor* ror) {
5502 HandleBinaryOp(ror);
5503}
5504
5505void InstructionCodeGeneratorARM64::VisitRor(HRor* ror) {
5506 HandleBinaryOp(ror);
5507}
5508
Serban Constantinescu02164b32014-11-13 14:05:07 +00005509void LocationsBuilderARM64::VisitShl(HShl* shl) {
5510 HandleShift(shl);
5511}
5512
5513void InstructionCodeGeneratorARM64::VisitShl(HShl* shl) {
5514 HandleShift(shl);
5515}
5516
5517void LocationsBuilderARM64::VisitShr(HShr* shr) {
5518 HandleShift(shr);
5519}
5520
5521void InstructionCodeGeneratorARM64::VisitShr(HShr* shr) {
5522 HandleShift(shr);
5523}
5524
Alexandre Rames5319def2014-10-23 10:03:10 +01005525void LocationsBuilderARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005526 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005527}
5528
5529void InstructionCodeGeneratorARM64::VisitSub(HSub* instruction) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005530 HandleBinaryOp(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005531}
5532
Alexandre Rames67555f72014-11-18 10:55:16 +00005533void LocationsBuilderARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005534 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005535}
5536
5537void InstructionCodeGeneratorARM64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005538 HandleFieldGet(instruction, instruction->GetFieldInfo());
Alexandre Rames67555f72014-11-18 10:55:16 +00005539}
5540
5541void LocationsBuilderARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Alexandre Rames09a99962015-04-15 11:47:56 +01005542 HandleFieldSet(instruction);
Alexandre Rames5319def2014-10-23 10:03:10 +01005543}
5544
Alexandre Rames67555f72014-11-18 10:55:16 +00005545void InstructionCodeGeneratorARM64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005546 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Alexandre Rames5319def2014-10-23 10:03:10 +01005547}
5548
Calin Juravlee460d1d2015-09-29 04:52:17 +01005549void LocationsBuilderARM64::VisitUnresolvedInstanceFieldGet(
5550 HUnresolvedInstanceFieldGet* instruction) {
5551 FieldAccessCallingConventionARM64 calling_convention;
5552 codegen_->CreateUnresolvedFieldLocationSummary(
5553 instruction, instruction->GetFieldType(), calling_convention);
5554}
5555
5556void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldGet(
5557 HUnresolvedInstanceFieldGet* instruction) {
5558 FieldAccessCallingConventionARM64 calling_convention;
5559 codegen_->GenerateUnresolvedFieldAccess(instruction,
5560 instruction->GetFieldType(),
5561 instruction->GetFieldIndex(),
5562 instruction->GetDexPc(),
5563 calling_convention);
5564}
5565
5566void LocationsBuilderARM64::VisitUnresolvedInstanceFieldSet(
5567 HUnresolvedInstanceFieldSet* instruction) {
5568 FieldAccessCallingConventionARM64 calling_convention;
5569 codegen_->CreateUnresolvedFieldLocationSummary(
5570 instruction, instruction->GetFieldType(), calling_convention);
5571}
5572
5573void InstructionCodeGeneratorARM64::VisitUnresolvedInstanceFieldSet(
5574 HUnresolvedInstanceFieldSet* instruction) {
5575 FieldAccessCallingConventionARM64 calling_convention;
5576 codegen_->GenerateUnresolvedFieldAccess(instruction,
5577 instruction->GetFieldType(),
5578 instruction->GetFieldIndex(),
5579 instruction->GetDexPc(),
5580 calling_convention);
5581}
5582
5583void LocationsBuilderARM64::VisitUnresolvedStaticFieldGet(
5584 HUnresolvedStaticFieldGet* instruction) {
5585 FieldAccessCallingConventionARM64 calling_convention;
5586 codegen_->CreateUnresolvedFieldLocationSummary(
5587 instruction, instruction->GetFieldType(), calling_convention);
5588}
5589
5590void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldGet(
5591 HUnresolvedStaticFieldGet* instruction) {
5592 FieldAccessCallingConventionARM64 calling_convention;
5593 codegen_->GenerateUnresolvedFieldAccess(instruction,
5594 instruction->GetFieldType(),
5595 instruction->GetFieldIndex(),
5596 instruction->GetDexPc(),
5597 calling_convention);
5598}
5599
5600void LocationsBuilderARM64::VisitUnresolvedStaticFieldSet(
5601 HUnresolvedStaticFieldSet* instruction) {
5602 FieldAccessCallingConventionARM64 calling_convention;
5603 codegen_->CreateUnresolvedFieldLocationSummary(
5604 instruction, instruction->GetFieldType(), calling_convention);
5605}
5606
5607void InstructionCodeGeneratorARM64::VisitUnresolvedStaticFieldSet(
5608 HUnresolvedStaticFieldSet* instruction) {
5609 FieldAccessCallingConventionARM64 calling_convention;
5610 codegen_->GenerateUnresolvedFieldAccess(instruction,
5611 instruction->GetFieldType(),
5612 instruction->GetFieldIndex(),
5613 instruction->GetDexPc(),
5614 calling_convention);
5615}
5616
Alexandre Rames5319def2014-10-23 10:03:10 +01005617void LocationsBuilderARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005618 LocationSummary* locations =
5619 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Artem Serov7957d952017-04-04 15:44:09 +01005620 // In suspend check slow path, usually there are no caller-save registers at all.
5621 // If SIMD instructions are present, however, we force spilling all live SIMD
5622 // registers in full width (since the runtime only saves/restores lower part).
5623 locations->SetCustomSlowPathCallerSaves(
5624 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Alexandre Rames5319def2014-10-23 10:03:10 +01005625}
5626
5627void InstructionCodeGeneratorARM64::VisitSuspendCheck(HSuspendCheck* instruction) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005628 HBasicBlock* block = instruction->GetBlock();
5629 if (block->GetLoopInformation() != nullptr) {
5630 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5631 // The back edge will generate the suspend check.
5632 return;
5633 }
5634 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5635 // The goto will generate the suspend check.
5636 return;
5637 }
5638 GenerateSuspendCheck(instruction, nullptr);
Alexandre Rames5319def2014-10-23 10:03:10 +01005639}
5640
Alexandre Rames67555f72014-11-18 10:55:16 +00005641void LocationsBuilderARM64::VisitThrow(HThrow* instruction) {
5642 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Alexandre Rames67555f72014-11-18 10:55:16 +00005644 InvokeRuntimeCallingConvention calling_convention;
5645 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
5646}
5647
5648void InstructionCodeGeneratorARM64::VisitThrow(HThrow* instruction) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00005649 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08005650 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Alexandre Rames67555f72014-11-18 10:55:16 +00005651}
5652
5653void LocationsBuilderARM64::VisitTypeConversion(HTypeConversion* conversion) {
5654 LocationSummary* locations =
5655 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
5656 Primitive::Type input_type = conversion->GetInputType();
5657 Primitive::Type result_type = conversion->GetResultType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00005658 DCHECK_NE(input_type, result_type);
Alexandre Rames67555f72014-11-18 10:55:16 +00005659 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
5660 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
5661 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
5662 }
5663
Alexandre Rames542361f2015-01-29 16:57:31 +00005664 if (Primitive::IsFloatingPointType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005665 locations->SetInAt(0, Location::RequiresFpuRegister());
5666 } else {
5667 locations->SetInAt(0, Location::RequiresRegister());
5668 }
5669
Alexandre Rames542361f2015-01-29 16:57:31 +00005670 if (Primitive::IsFloatingPointType(result_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005671 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5672 } else {
5673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5674 }
5675}
5676
5677void InstructionCodeGeneratorARM64::VisitTypeConversion(HTypeConversion* conversion) {
5678 Primitive::Type result_type = conversion->GetResultType();
5679 Primitive::Type input_type = conversion->GetInputType();
5680
5681 DCHECK_NE(input_type, result_type);
5682
Alexandre Rames542361f2015-01-29 16:57:31 +00005683 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexandre Rames67555f72014-11-18 10:55:16 +00005684 int result_size = Primitive::ComponentSize(result_type);
5685 int input_size = Primitive::ComponentSize(input_type);
Alexandre Rames3e69f162014-12-10 10:36:50 +00005686 int min_size = std::min(result_size, input_size);
Serban Constantinescu02164b32014-11-13 14:05:07 +00005687 Register output = OutputRegister(conversion);
5688 Register source = InputRegisterAt(conversion, 0);
Alexandre Rames8626b742015-11-25 16:28:08 +00005689 if (result_type == Primitive::kPrimInt && input_type == Primitive::kPrimLong) {
Alexandre Rames4dff2fd2015-08-20 13:36:35 +01005690 // 'int' values are used directly as W registers, discarding the top
5691 // bits, so we don't need to sign-extend and can just perform a move.
5692 // We do not pass the `kDiscardForSameWReg` argument to force clearing the
5693 // top 32 bits of the target register. We theoretically could leave those
5694 // bits unchanged, but we would have to make sure that no code uses a
5695 // 32bit input value as a 64bit value assuming that the top 32 bits are
5696 // zero.
5697 __ Mov(output.W(), source.W());
Alexandre Rames8626b742015-11-25 16:28:08 +00005698 } else if (result_type == Primitive::kPrimChar ||
5699 (input_type == Primitive::kPrimChar && input_size < result_size)) {
5700 __ Ubfx(output,
5701 output.IsX() ? source.X() : source.W(),
5702 0, Primitive::ComponentSize(Primitive::kPrimChar) * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005703 } else {
Alexandre Rames3e69f162014-12-10 10:36:50 +00005704 __ Sbfx(output, output.IsX() ? source.X() : source.W(), 0, min_size * kBitsPerByte);
Alexandre Rames67555f72014-11-18 10:55:16 +00005705 }
Alexandre Rames542361f2015-01-29 16:57:31 +00005706 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005707 __ Scvtf(OutputFPRegister(conversion), InputRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005708 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005709 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
5710 __ Fcvtzs(OutputRegister(conversion), InputFPRegisterAt(conversion, 0));
Alexandre Rames542361f2015-01-29 16:57:31 +00005711 } else if (Primitive::IsFloatingPointType(result_type) &&
5712 Primitive::IsFloatingPointType(input_type)) {
Serban Constantinescu02164b32014-11-13 14:05:07 +00005713 __ Fcvt(OutputFPRegister(conversion), InputFPRegisterAt(conversion, 0));
5714 } else {
5715 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
5716 << " to " << result_type;
Alexandre Rames67555f72014-11-18 10:55:16 +00005717 }
Serban Constantinescu02164b32014-11-13 14:05:07 +00005718}
Alexandre Rames67555f72014-11-18 10:55:16 +00005719
Serban Constantinescu02164b32014-11-13 14:05:07 +00005720void LocationsBuilderARM64::VisitUShr(HUShr* ushr) {
5721 HandleShift(ushr);
5722}
5723
5724void InstructionCodeGeneratorARM64::VisitUShr(HUShr* ushr) {
5725 HandleShift(ushr);
Alexandre Rames67555f72014-11-18 10:55:16 +00005726}
5727
5728void LocationsBuilderARM64::VisitXor(HXor* instruction) {
5729 HandleBinaryOp(instruction);
5730}
5731
5732void InstructionCodeGeneratorARM64::VisitXor(HXor* instruction) {
5733 HandleBinaryOp(instruction);
5734}
5735
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005736void LocationsBuilderARM64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005737 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005738 LOG(FATAL) << "Unreachable";
5739}
5740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005741void InstructionCodeGeneratorARM64::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
Mark Mendellfe57faa2015-09-18 09:26:15 -04005746// Simple implementation of packed switch - generate cascaded compare/jumps.
5747void LocationsBuilderARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5748 LocationSummary* locations =
5749 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5750 locations->SetInAt(0, Location::RequiresRegister());
5751}
5752
5753void InstructionCodeGeneratorARM64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5754 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu3927c8b2015-11-18 17:46:25 +08005755 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04005756 Register value_reg = InputRegisterAt(switch_instr, 0);
5757 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5758
Zheng Xu3927c8b2015-11-18 17:46:25 +08005759 // Roughly set 16 as max average assemblies generated per HIR in a graph.
Scott Wakeling97c72b72016-06-24 16:19:36 +01005760 static constexpr int32_t kMaxExpectedSizePerHInstruction = 16 * kInstructionSize;
Zheng Xu3927c8b2015-11-18 17:46:25 +08005761 // ADR has a limited range(+/-1MB), so we set a threshold for the number of HIRs in the graph to
5762 // make sure we don't emit it if the target may run out of range.
5763 // TODO: Instead of emitting all jump tables at the end of the code, we could keep track of ADR
5764 // ranges and emit the tables only as required.
5765 static constexpr int32_t kJumpTableInstructionThreshold = 1* MB / kMaxExpectedSizePerHInstruction;
Mark Mendellfe57faa2015-09-18 09:26:15 -04005766
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005767 if (num_entries <= kPackedSwitchCompareJumpThreshold ||
Zheng Xu3927c8b2015-11-18 17:46:25 +08005768 // Current instruction id is an upper bound of the number of HIRs in the graph.
5769 GetGraph()->GetCurrentInstructionId() > kJumpTableInstructionThreshold) {
5770 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005771 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5772 Register temp = temps.AcquireW();
5773 __ Subs(temp, value_reg, Operand(lower_bound));
5774
Zheng Xu3927c8b2015-11-18 17:46:25 +08005775 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00005776 // Jump to successors[0] if value == lower_bound.
5777 __ B(eq, codegen_->GetLabelOf(successors[0]));
5778 int32_t last_index = 0;
5779 for (; num_entries - last_index > 2; last_index += 2) {
5780 __ Subs(temp, temp, Operand(2));
5781 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
5782 __ B(lo, codegen_->GetLabelOf(successors[last_index + 1]));
5783 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
5784 __ B(eq, codegen_->GetLabelOf(successors[last_index + 2]));
5785 }
5786 if (num_entries - last_index == 2) {
5787 // The last missing case_value.
5788 __ Cmp(temp, Operand(1));
5789 __ B(eq, codegen_->GetLabelOf(successors[last_index + 1]));
Zheng Xu3927c8b2015-11-18 17:46:25 +08005790 }
5791
5792 // And the default for any other value.
5793 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5794 __ B(codegen_->GetLabelOf(default_block));
5795 }
5796 } else {
Alexandre Ramesc01a6642016-04-15 11:54:06 +01005797 JumpTableARM64* jump_table = codegen_->CreateJumpTable(switch_instr);
Zheng Xu3927c8b2015-11-18 17:46:25 +08005798
5799 UseScratchRegisterScope temps(codegen_->GetVIXLAssembler());
5800
5801 // Below instructions should use at most one blocked register. Since there are two blocked
5802 // registers, we are free to block one.
5803 Register temp_w = temps.AcquireW();
5804 Register index;
5805 // Remove the bias.
5806 if (lower_bound != 0) {
5807 index = temp_w;
5808 __ Sub(index, value_reg, Operand(lower_bound));
5809 } else {
5810 index = value_reg;
5811 }
5812
5813 // Jump to default block if index is out of the range.
5814 __ Cmp(index, Operand(num_entries));
5815 __ B(hs, codegen_->GetLabelOf(default_block));
5816
5817 // In current VIXL implementation, it won't require any blocked registers to encode the
5818 // immediate value for Adr. So we are free to use both VIXL blocked registers to reduce the
5819 // register pressure.
5820 Register table_base = temps.AcquireX();
5821 // Load jump offset from the table.
5822 __ Adr(table_base, jump_table->GetTableStartLabel());
5823 Register jump_offset = temp_w;
5824 __ Ldr(jump_offset, MemOperand(table_base, index, UXTW, 2));
5825
5826 // Jump to target block by branching to table_base(pc related) + offset.
5827 Register target_address = table_base;
5828 __ Add(target_address, table_base, Operand(jump_offset, SXTW));
5829 __ Br(target_address);
Mark Mendellfe57faa2015-09-18 09:26:15 -04005830 }
5831}
5832
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005833void InstructionCodeGeneratorARM64::GenerateReferenceLoadOneRegister(
5834 HInstruction* instruction,
5835 Location out,
5836 uint32_t offset,
5837 Location maybe_temp,
5838 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005839 Primitive::Type type = Primitive::kPrimNot;
5840 Register out_reg = RegisterFrom(out, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005841 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005842 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005843 if (kUseBakerReadBarrier) {
5844 // Load with fast path based Baker's read barrier.
5845 // /* HeapReference<Object> */ out = *(out + offset)
5846 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5847 out,
5848 out_reg,
5849 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005850 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005851 /* needs_null_check */ false,
5852 /* use_load_acquire */ false);
5853 } else {
5854 // Load with slow path based read barrier.
5855 // Save the value of `out` into `maybe_temp` before overwriting it
5856 // in the following move operation, as we will need it for the
5857 // read barrier below.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005858 Register temp_reg = RegisterFrom(maybe_temp, type);
Roland Levillain44015862016-01-22 11:47:17 +00005859 __ Mov(temp_reg, out_reg);
5860 // /* HeapReference<Object> */ out = *(out + offset)
5861 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5862 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
5863 }
5864 } else {
5865 // Plain load with no read barrier.
5866 // /* HeapReference<Object> */ out = *(out + offset)
5867 __ Ldr(out_reg, HeapOperand(out_reg, offset));
5868 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5869 }
5870}
5871
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005872void InstructionCodeGeneratorARM64::GenerateReferenceLoadTwoRegisters(
5873 HInstruction* instruction,
5874 Location out,
5875 Location obj,
5876 uint32_t offset,
5877 Location maybe_temp,
5878 ReadBarrierOption read_barrier_option) {
Roland Levillain44015862016-01-22 11:47:17 +00005879 Primitive::Type type = Primitive::kPrimNot;
5880 Register out_reg = RegisterFrom(out, type);
5881 Register obj_reg = RegisterFrom(obj, type);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005882 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08005883 CHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005884 if (kUseBakerReadBarrier) {
5885 // Load with fast path based Baker's read barrier.
Roland Levillain44015862016-01-22 11:47:17 +00005886 // /* HeapReference<Object> */ out = *(obj + offset)
5887 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5888 out,
5889 obj_reg,
5890 offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005891 maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00005892 /* needs_null_check */ false,
5893 /* use_load_acquire */ false);
5894 } else {
5895 // Load with slow path based read barrier.
5896 // /* HeapReference<Object> */ out = *(obj + offset)
5897 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5898 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
5899 }
5900 } else {
5901 // Plain load with no read barrier.
5902 // /* HeapReference<Object> */ out = *(obj + offset)
5903 __ Ldr(out_reg, HeapOperand(obj_reg, offset));
5904 GetAssembler()->MaybeUnpoisonHeapReference(out_reg);
5905 }
5906}
5907
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005908void InstructionCodeGeneratorARM64::GenerateGcRootFieldLoad(
5909 HInstruction* instruction,
5910 Location root,
5911 Register obj,
5912 uint32_t offset,
5913 vixl::aarch64::Label* fixup_label,
5914 ReadBarrierOption read_barrier_option) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005915 DCHECK(fixup_label == nullptr || offset == 0u);
Roland Levillain44015862016-01-22 11:47:17 +00005916 Register root_reg = RegisterFrom(root, Primitive::kPrimNot);
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005917 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005918 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain44015862016-01-22 11:47:17 +00005919 if (kUseBakerReadBarrier) {
5920 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00005921 // Baker's read barrier are used.
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005922 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
5923 !Runtime::Current()->UseJitCompilation()) {
5924 // Note that we do not actually check the value of `GetIsGcMarking()`
5925 // to decide whether to mark the loaded GC root or not. Instead, we
5926 // load into `temp` the read barrier mark introspection entrypoint.
5927 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
5928 // vice versa.
5929 //
5930 // We use link-time generated thunks for the slow path. That thunk
5931 // checks the reference and jumps to the entrypoint if needed.
5932 //
5933 // temp = Thread::Current()->pReadBarrierMarkIntrospection
5934 // lr = &return_address;
5935 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5936 // if (temp != nullptr) {
5937 // goto gc_root_thunk<root_reg>(lr)
5938 // }
5939 // return_address:
Roland Levillain44015862016-01-22 11:47:17 +00005940
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005941 UseScratchRegisterScope temps(GetVIXLAssembler());
5942 DCHECK(temps.IsAvailable(ip0));
5943 DCHECK(temps.IsAvailable(ip1));
5944 temps.Exclude(ip0, ip1);
5945 uint32_t custom_data =
5946 linker::Arm64RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg.GetCode());
5947 vixl::aarch64::Label* cbnz_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00005948
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005949 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
5950 DCHECK_EQ(ip0.GetCode(), 16u);
5951 const int32_t entry_point_offset =
5952 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
5953 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
5954 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
5955 vixl::aarch64::Label return_address;
5956 __ adr(lr, &return_address);
5957 if (fixup_label != nullptr) {
5958 __ Bind(fixup_label);
5959 }
5960 static_assert(BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
5961 "GC root LDR must be 2 instruction (8B) before the return address label.");
5962 __ ldr(root_reg, MemOperand(obj.X(), offset));
5963 __ Bind(cbnz_label);
5964 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
5965 __ Bind(&return_address);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966 } else {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005967 // Note that we do not actually check the value of
5968 // `GetIsGcMarking()` to decide whether to mark the loaded GC
5969 // root or not. Instead, we load into `temp` the read barrier
5970 // mark entry point corresponding to register `root`. If `temp`
5971 // is null, it means that `GetIsGcMarking()` is false, and vice
5972 // versa.
5973 //
5974 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5975 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
5976 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
5977 // // Slow path.
5978 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
5979 // }
Roland Levillain44015862016-01-22 11:47:17 +00005980
Vladimir Markof4f2daa2017-03-20 18:26:59 +00005981 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
5982 Register temp = lr;
5983 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM64(
5984 instruction, root, /* entrypoint */ LocationFrom(temp));
5985 codegen_->AddSlowPath(slow_path);
5986
5987 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
5988 const int32_t entry_point_offset =
5989 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(root.reg());
5990 // Loading the entrypoint does not require a load acquire since it is only changed when
5991 // threads are suspended or running a checkpoint.
5992 __ Ldr(temp, MemOperand(tr, entry_point_offset));
5993
5994 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
5995 if (fixup_label == nullptr) {
5996 __ Ldr(root_reg, MemOperand(obj, offset));
5997 } else {
5998 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj);
5999 }
6000 static_assert(
6001 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6002 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6003 "have different sizes.");
6004 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6005 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6006 "have different sizes.");
6007
6008 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6009 // checking GetIsGcMarking.
6010 __ Cbnz(temp, slow_path->GetEntryLabel());
6011 __ Bind(slow_path->GetExitLabel());
6012 }
Roland Levillain44015862016-01-22 11:47:17 +00006013 } else {
6014 // GC root loaded through a slow path for read barriers other
6015 // than Baker's.
6016 // /* GcRoot<mirror::Object>* */ root = obj + offset
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006017 if (fixup_label == nullptr) {
6018 __ Add(root_reg.X(), obj.X(), offset);
6019 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006020 codegen_->EmitAddPlaceholder(fixup_label, root_reg.X(), obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006021 }
Roland Levillain44015862016-01-22 11:47:17 +00006022 // /* mirror::Object* */ root = root->Read()
6023 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6024 }
6025 } else {
6026 // Plain GC root load with no read barrier.
6027 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006028 if (fixup_label == nullptr) {
6029 __ Ldr(root_reg, MemOperand(obj, offset));
6030 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006031 codegen_->EmitLdrOffsetPlaceholder(fixup_label, root_reg, obj.X());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006032 }
Roland Levillain44015862016-01-22 11:47:17 +00006033 // Note that GC roots are not affected by heap poisoning, thus we
6034 // do not have to unpoison `root_reg` here.
6035 }
6036}
6037
6038void CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6039 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006040 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006041 uint32_t offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006042 Location maybe_temp,
Roland Levillain44015862016-01-22 11:47:17 +00006043 bool needs_null_check,
6044 bool use_load_acquire) {
6045 DCHECK(kEmitCompilerReadBarrier);
6046 DCHECK(kUseBakerReadBarrier);
6047
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006048 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6049 !use_load_acquire &&
6050 !Runtime::Current()->UseJitCompilation()) {
6051 // Note that we do not actually check the value of `GetIsGcMarking()`
6052 // to decide whether to mark the loaded GC root or not. Instead, we
6053 // load into `temp` the read barrier mark introspection entrypoint.
6054 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6055 // vice versa.
6056 //
6057 // We use link-time generated thunks for the slow path. That thunk checks
6058 // the holder and jumps to the entrypoint if needed. If the holder is not
6059 // gray, it creates a fake dependency and returns to the LDR instruction.
6060 //
6061 // temp = Thread::Current()->pReadBarrierMarkIntrospection
6062 // lr = &return_address;
6063 // if (temp != nullptr) {
6064 // goto field_thunk<holder_reg, base_reg>(lr)
6065 // }
6066 // not_gray_return_address:
6067 // // Original reference load. If the offset is too large to fit
6068 // // into LDR, we use an adjusted base register here.
6069 // GcRoot<mirror::Object> root = *(obj+offset);
6070 // gray_return_address:
6071
6072 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
6073 Register base = obj;
6074 if (offset >= kReferenceLoadMinFarOffset) {
6075 DCHECK(maybe_temp.IsRegister());
6076 base = WRegisterFrom(maybe_temp);
6077 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
6078 __ Add(base, obj, Operand(offset & ~(kReferenceLoadMinFarOffset - 1u)));
6079 offset &= (kReferenceLoadMinFarOffset - 1u);
6080 }
6081 UseScratchRegisterScope temps(GetVIXLAssembler());
6082 DCHECK(temps.IsAvailable(ip0));
6083 DCHECK(temps.IsAvailable(ip1));
6084 temps.Exclude(ip0, ip1);
6085 uint32_t custom_data = linker::Arm64RelativePatcher::EncodeBakerReadBarrierFieldData(
6086 base.GetCode(),
6087 obj.GetCode());
6088 vixl::aarch64::Label* cbnz_label = NewBakerReadBarrierPatch(custom_data);
6089
6090 // ip1 = Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
6091 DCHECK_EQ(ip0.GetCode(), 16u);
6092 const int32_t entry_point_offset =
6093 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ip0.GetCode());
6094 __ Ldr(ip1, MemOperand(tr, entry_point_offset));
6095 EmissionCheckScope guard(GetVIXLAssembler(), 3 * vixl::aarch64::kInstructionSize);
6096 vixl::aarch64::Label return_address;
6097 __ adr(lr, &return_address);
6098 __ Bind(cbnz_label);
6099 __ cbnz(ip1, static_cast<int64_t>(0)); // Placeholder, patched at link-time.
6100 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == -4,
6101 "Field LDR must be 1 instruction (4B) before the return address label.");
6102 __ ldr(RegisterFrom(ref, Primitive::kPrimNot), MemOperand(base.X(), offset));
6103 if (needs_null_check) {
6104 MaybeRecordImplicitNullCheck(instruction);
6105 }
6106 __ Bind(&return_address);
6107 return;
6108 }
6109
Roland Levillain44015862016-01-22 11:47:17 +00006110 // /* HeapReference<Object> */ ref = *(obj + offset)
Vladimir Markof4f2daa2017-03-20 18:26:59 +00006111 Register temp = WRegisterFrom(maybe_temp);
Roland Levillain44015862016-01-22 11:47:17 +00006112 Location no_index = Location::NoLocation();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006113 size_t no_scale_factor = 0u;
Roland Levillainbfea3352016-06-23 13:48:47 +01006114 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6115 ref,
6116 obj,
6117 offset,
6118 no_index,
6119 no_scale_factor,
6120 temp,
6121 needs_null_check,
6122 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006123}
6124
6125void CodeGeneratorARM64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6126 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006127 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006128 uint32_t data_offset,
6129 Location index,
6130 Register temp,
6131 bool needs_null_check) {
6132 DCHECK(kEmitCompilerReadBarrier);
6133 DCHECK(kUseBakerReadBarrier);
6134
6135 // Array cells are never volatile variables, therefore array loads
6136 // never use Load-Acquire instructions on ARM64.
6137 const bool use_load_acquire = false;
6138
Roland Levillainbfea3352016-06-23 13:48:47 +01006139 static_assert(
6140 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6141 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain44015862016-01-22 11:47:17 +00006142 // /* HeapReference<Object> */ ref =
6143 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006144 size_t scale_factor = Primitive::ComponentSizeShift(Primitive::kPrimNot);
6145 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6146 ref,
6147 obj,
6148 data_offset,
6149 index,
6150 scale_factor,
6151 temp,
6152 needs_null_check,
6153 use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006154}
6155
6156void CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6157 Location ref,
Scott Wakeling97c72b72016-06-24 16:19:36 +01006158 Register obj,
Roland Levillain44015862016-01-22 11:47:17 +00006159 uint32_t offset,
6160 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006161 size_t scale_factor,
Roland Levillain44015862016-01-22 11:47:17 +00006162 Register temp,
6163 bool needs_null_check,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006164 bool use_load_acquire,
6165 bool always_update_field) {
Roland Levillain44015862016-01-22 11:47:17 +00006166 DCHECK(kEmitCompilerReadBarrier);
6167 DCHECK(kUseBakerReadBarrier);
Roland Levillainbfea3352016-06-23 13:48:47 +01006168 // If we are emitting an array load, we should not be using a
6169 // Load Acquire instruction. In other words:
6170 // `instruction->IsArrayGet()` => `!use_load_acquire`.
6171 DCHECK(!instruction->IsArrayGet() || !use_load_acquire);
Roland Levillain44015862016-01-22 11:47:17 +00006172
Roland Levillain54f869e2017-03-06 13:54:11 +00006173 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
6174 // whether we need to enter the slow path to mark the reference.
6175 // Then, in the slow path, check the gray bit in the lock word of
6176 // the reference's holder (`obj`) to decide whether to mark `ref` or
6177 // not.
Roland Levillain44015862016-01-22 11:47:17 +00006178 //
Roland Levillainba650a42017-03-06 13:52:32 +00006179 // Note that we do not actually check the value of `GetIsGcMarking()`;
6180 // instead, we load into `temp2` the read barrier mark entry point
6181 // corresponding to register `ref`. If `temp2` is null, it means
6182 // that `GetIsGcMarking()` is false, and vice versa.
6183 //
6184 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00006185 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
6186 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00006187 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6188 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6189 // HeapReference<mirror::Object> ref = *src; // Original reference load.
6190 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6191 // if (is_gray) {
6192 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
6193 // }
6194 // } else {
6195 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillain44015862016-01-22 11:47:17 +00006196 // }
Roland Levillain44015862016-01-22 11:47:17 +00006197
Roland Levillainba650a42017-03-06 13:52:32 +00006198 // Slow path marking the object `ref` when the GC is marking. The
6199 // entrypoint will already be loaded in `temp2`.
6200 Register temp2 = lr;
6201 Location temp2_loc = LocationFrom(temp2);
6202 SlowPathCodeARM64* slow_path;
6203 if (always_update_field) {
Roland Levillain54f869e2017-03-06 13:54:11 +00006204 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64
6205 // only supports address of the form `obj + field_offset`, where
6206 // `obj` is a register and `field_offset` is a register. Thus
6207 // `offset` and `scale_factor` above are expected to be null in
6208 // this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00006209 DCHECK_EQ(offset, 0u);
6210 DCHECK_EQ(scale_factor, 0u); /* "times 1" */
Roland Levillain54f869e2017-03-06 13:54:11 +00006211 Location field_offset = index;
6212 slow_path =
6213 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM64(
6214 instruction,
6215 ref,
6216 obj,
6217 offset,
6218 /* index */ field_offset,
6219 scale_factor,
6220 needs_null_check,
6221 use_load_acquire,
6222 temp,
6223 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006224 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00006225 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM64(
6226 instruction,
6227 ref,
6228 obj,
6229 offset,
6230 index,
6231 scale_factor,
6232 needs_null_check,
6233 use_load_acquire,
6234 temp,
6235 /* entrypoint */ temp2_loc);
Roland Levillainba650a42017-03-06 13:52:32 +00006236 }
6237 AddSlowPath(slow_path);
6238
6239 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
6240 const int32_t entry_point_offset =
6241 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(ref.reg());
6242 // Loading the entrypoint does not require a load acquire since it is only changed when
6243 // threads are suspended or running a checkpoint.
6244 __ Ldr(temp2, MemOperand(tr, entry_point_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006245 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6246 // checking GetIsGcMarking.
6247 __ Cbnz(temp2, slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00006248 // Fast path: just load the reference.
6249 GenerateRawReferenceLoad(
6250 instruction, ref, obj, offset, index, scale_factor, needs_null_check, use_load_acquire);
Roland Levillainba650a42017-03-06 13:52:32 +00006251 __ Bind(slow_path->GetExitLabel());
6252}
6253
6254void CodeGeneratorARM64::GenerateRawReferenceLoad(HInstruction* instruction,
6255 Location ref,
6256 Register obj,
6257 uint32_t offset,
6258 Location index,
6259 size_t scale_factor,
6260 bool needs_null_check,
6261 bool use_load_acquire) {
6262 DCHECK(obj.IsW());
Roland Levillain44015862016-01-22 11:47:17 +00006263 Primitive::Type type = Primitive::kPrimNot;
6264 Register ref_reg = RegisterFrom(ref, type);
Roland Levillain44015862016-01-22 11:47:17 +00006265
Roland Levillainba650a42017-03-06 13:52:32 +00006266 // If needed, vixl::EmissionCheckScope guards are used to ensure
6267 // that no pools are emitted between the load (macro) instruction
6268 // and MaybeRecordImplicitNullCheck.
Roland Levillain44015862016-01-22 11:47:17 +00006269
Roland Levillain44015862016-01-22 11:47:17 +00006270 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006271 // Load types involving an "index": ArrayGet,
6272 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6273 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006274 if (use_load_acquire) {
6275 // UnsafeGetObjectVolatile intrinsic case.
6276 // Register `index` is not an index in an object array, but an
6277 // offset to an object reference field within object `obj`.
6278 DCHECK(instruction->IsInvoke()) << instruction->DebugName();
6279 DCHECK(instruction->GetLocations()->Intrinsified());
6280 DCHECK(instruction->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)
6281 << instruction->AsInvoke()->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006282 DCHECK_EQ(offset, 0u);
6283 DCHECK_EQ(scale_factor, 0u);
Roland Levillainba650a42017-03-06 13:52:32 +00006284 DCHECK_EQ(needs_null_check, false);
6285 // /* HeapReference<mirror::Object> */ ref = *(obj + index)
Roland Levillainbfea3352016-06-23 13:48:47 +01006286 MemOperand field = HeapOperand(obj, XRegisterFrom(index));
6287 LoadAcquire(instruction, ref_reg, field, /* needs_null_check */ false);
Roland Levillain44015862016-01-22 11:47:17 +00006288 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006289 // ArrayGet and UnsafeGetObject and UnsafeCASObject intrinsics cases.
6290 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainbfea3352016-06-23 13:48:47 +01006291 if (index.IsConstant()) {
6292 uint32_t computed_offset = offset + (Int64ConstantFrom(index) << scale_factor);
Roland Levillainba650a42017-03-06 13:52:32 +00006293 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillainbfea3352016-06-23 13:48:47 +01006294 Load(type, ref_reg, HeapOperand(obj, computed_offset));
Roland Levillainba650a42017-03-06 13:52:32 +00006295 if (needs_null_check) {
6296 MaybeRecordImplicitNullCheck(instruction);
6297 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006298 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006299 UseScratchRegisterScope temps(GetVIXLAssembler());
6300 Register temp = temps.AcquireW();
6301 __ Add(temp, obj, offset);
6302 {
6303 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
6304 Load(type, ref_reg, HeapOperand(temp, XRegisterFrom(index), LSL, scale_factor));
6305 if (needs_null_check) {
6306 MaybeRecordImplicitNullCheck(instruction);
6307 }
6308 }
Roland Levillainbfea3352016-06-23 13:48:47 +01006309 }
Roland Levillain44015862016-01-22 11:47:17 +00006310 }
Roland Levillain44015862016-01-22 11:47:17 +00006311 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006312 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillain44015862016-01-22 11:47:17 +00006313 MemOperand field = HeapOperand(obj, offset);
6314 if (use_load_acquire) {
Roland Levillainba650a42017-03-06 13:52:32 +00006315 // Implicit null checks are handled by CodeGeneratorARM64::LoadAcquire.
6316 LoadAcquire(instruction, ref_reg, field, needs_null_check);
Roland Levillain44015862016-01-22 11:47:17 +00006317 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00006318 EmissionCheckScope guard(GetVIXLAssembler(), kMaxMacroInstructionSizeInBytes);
Roland Levillain44015862016-01-22 11:47:17 +00006319 Load(type, ref_reg, field);
Roland Levillainba650a42017-03-06 13:52:32 +00006320 if (needs_null_check) {
6321 MaybeRecordImplicitNullCheck(instruction);
6322 }
Roland Levillain44015862016-01-22 11:47:17 +00006323 }
6324 }
6325
6326 // Object* ref = ref_addr->AsMirrorPtr()
6327 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain44015862016-01-22 11:47:17 +00006328}
6329
6330void CodeGeneratorARM64::GenerateReadBarrierSlow(HInstruction* instruction,
6331 Location out,
6332 Location ref,
6333 Location obj,
6334 uint32_t offset,
6335 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006336 DCHECK(kEmitCompilerReadBarrier);
6337
Roland Levillain44015862016-01-22 11:47:17 +00006338 // Insert a slow path based read barrier *after* the reference load.
6339 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006340 // If heap poisoning is enabled, the unpoisoning of the loaded
6341 // reference will be carried out by the runtime within the slow
6342 // path.
6343 //
6344 // Note that `ref` currently does not get unpoisoned (when heap
6345 // poisoning is enabled), which is alright as the `ref` argument is
6346 // not used by the artReadBarrierSlow entry point.
6347 //
6348 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6349 SlowPathCodeARM64* slow_path = new (GetGraph()->GetArena())
6350 ReadBarrierForHeapReferenceSlowPathARM64(instruction, out, ref, obj, offset, index);
6351 AddSlowPath(slow_path);
6352
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006353 __ B(slow_path->GetEntryLabel());
6354 __ Bind(slow_path->GetExitLabel());
6355}
6356
Roland Levillain44015862016-01-22 11:47:17 +00006357void CodeGeneratorARM64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6358 Location out,
6359 Location ref,
6360 Location obj,
6361 uint32_t offset,
6362 Location index) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006363 if (kEmitCompilerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +00006364 // Baker's read barriers shall be handled by the fast path
6365 // (CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier).
6366 DCHECK(!kUseBakerReadBarrier);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006367 // If heap poisoning is enabled, unpoisoning will be taken care of
6368 // by the runtime within the slow path.
Roland Levillain44015862016-01-22 11:47:17 +00006369 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006370 } else if (kPoisonHeapReferences) {
6371 GetAssembler()->UnpoisonHeapReference(WRegisterFrom(out));
6372 }
6373}
6374
Roland Levillain44015862016-01-22 11:47:17 +00006375void CodeGeneratorARM64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6376 Location out,
6377 Location root) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006378 DCHECK(kEmitCompilerReadBarrier);
6379
Roland Levillain44015862016-01-22 11:47:17 +00006380 // Insert a slow path based read barrier *after* the GC root load.
6381 //
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006382 // Note that GC roots are not affected by heap poisoning, so we do
6383 // not need to do anything special for this here.
6384 SlowPathCodeARM64* slow_path =
6385 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM64(instruction, out, root);
6386 AddSlowPath(slow_path);
6387
Roland Levillain22ccc3a2015-11-24 13:10:05 +00006388 __ B(slow_path->GetEntryLabel());
6389 __ Bind(slow_path->GetExitLabel());
6390}
6391
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006392void LocationsBuilderARM64::VisitClassTableGet(HClassTableGet* instruction) {
6393 LocationSummary* locations =
6394 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6395 locations->SetInAt(0, Location::RequiresRegister());
6396 locations->SetOut(Location::RequiresRegister());
6397}
6398
6399void InstructionCodeGeneratorARM64::VisitClassTableGet(HClassTableGet* instruction) {
6400 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006402 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006403 instruction->GetIndex(), kArm64PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006404 __ Ldr(XRegisterFrom(locations->Out()),
6405 MemOperand(XRegisterFrom(locations->InAt(0)), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006406 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006407 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006408 instruction->GetIndex(), kArm64PointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006409 __ Ldr(XRegisterFrom(locations->Out()), MemOperand(XRegisterFrom(locations->InAt(0)),
6410 mirror::Class::ImtPtrOffset(kArm64PointerSize).Uint32Value()));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01006411 __ Ldr(XRegisterFrom(locations->Out()),
6412 MemOperand(XRegisterFrom(locations->Out()), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006413 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006414}
6415
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006416static void PatchJitRootUse(uint8_t* code,
6417 const uint8_t* roots_data,
6418 vixl::aarch64::Literal<uint32_t>* literal,
6419 uint64_t index_in_table) {
6420 uint32_t literal_offset = literal->GetOffset();
6421 uintptr_t address =
6422 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
6423 uint8_t* data = code + literal_offset;
6424 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
6425}
6426
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006427void CodeGeneratorARM64::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
6428 for (const auto& entry : jit_string_patches_) {
6429 const auto& it = jit_string_roots_.find(entry.first);
6430 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006431 PatchJitRootUse(code, roots_data, entry.second, it->second);
6432 }
6433 for (const auto& entry : jit_class_patches_) {
6434 const auto& it = jit_class_roots_.find(entry.first);
6435 DCHECK(it != jit_class_roots_.end());
6436 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006437 }
6438}
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00006439
Alexandre Rames67555f72014-11-18 10:55:16 +00006440#undef __
6441#undef QUICK_ENTRY_POINT
6442
Alexandre Rames5319def2014-10-23 10:03:10 +01006443} // namespace arm64
6444} // namespace art